Skip to content

[svm] Check for hiding reflection members only after the registration is published - #14913

Draft
MarkusBernhardt wants to merge 1 commit into
oracle:masterfrom
MarkusBernhardt:reflection-hiding-check-after-publish
Draft

MarkusBernhardt wants to merge 1 commit into
oracle:masterfrom
MarkusBernhardt:reflection-hiding-check-after-publish

Conversation

@MarkusBernhardt

Copy link
Copy Markdown

Problem

Without exact reachability metadata, ReflectionDataBuilder records subtype members that override or hide a registered method or field, the "hiding" members. This way, Sub.class.getMethod("m") does not return the registered Super.m() when Sub declares its own, unregistered m().

registerMethod and registerField start this check (checkHidingMethods / checkHidingFields) from inside the ConcurrentHashMap.compute call that creates the registration. At that point, the new mapping is not yet visible to other threads. The check runs checkSubtypeForOverridingMethod / checkSubtypeForOverridingField as an analysis task:

  • For the first registered member of a type, it goes through registerSubtypeReachabilityHandler, which notifies already reachable subtypes synchronously, and the callback posts the task.
  • For later members, one task per reachable subtype is posted directly.

That task first checks methods.containsKey(supertypeMethod) (or fields.containsKey(...)) and returns if the member is absent. If a worker thread picks it up before the registering thread leaves compute (for ACCESSED methods, the registering thread is still running registerMethodAccessor), the check sees no registration and does nothing. The subtype has already been notified, so nothing re-runs the check. The image then lacks the hiding entry, Sub.class.getMethod("m") returns Super.m(), and the reflection metadata depends on thread timing.

We found this while making native-image builds bit-for-bit reproducible. To confirm it on current master, we used a fixture that wraps the builder's methods/fields maps and only delays the end of the compute call; the production code inside compute is unchanged. The fixture registers a method and a field of a supertype after their overriding subtypes are reachable. Results:

  • On master 3810b9fb0f4, the posted check ran while the registration was unpublished in 4 of 4 builds. In 3 of the 4, the hiding entries were never recorded, and the image returned MethodBase.raceMarker() / FieldBase.raceField for MethodChild.class.getMethod(...) / FieldChild.class.getField(...). In the remaining build, another path still recorded them.
  • With this change, the check never ran early and both hiding entries were recorded in 3 of 3 builds.

We are happy to share the fixture.

Fix

Decide inside compute whether the hiding check is needed, so that it still runs exactly once per member, on the registration that first makes it QUERIED. Call checkHidingMethods / checkHidingFields only after compute has returned. Posting the check then happens-after the mapping is published.

Test

New com.oracle.svm.test.reflect.HidingMemberRegistrationTest, run with its own feature via @NativeImageBuildArgs. The feature makes MethodChild and FieldChild reachable in beforeAnalysis and registers MethodBase.member() and FieldBase.member only in the first duringAnalysis, the order in which the race exists. The tests check that the registered members work and that getMethod/getField on the subtypes do not return the supertype members.

This is a regression test for the scenario, not a reproducer. Without the fix, it passed in 3 of 3 runs on our machine, because the race window is narrow without the fixture's delay. A deterministic reproducer has to interpose on the builder's private maps, which did not seem appropriate for the test suite.

On darwin-aarch64 (labsjdk 25.0.5.1, JVMCI 25.6-b27), the test passes with the fix (2 of 2 runs), and mx checkstyle --primary passes. mx eclipseformat was not run.

Notes

🤖 Generated with Claude Code

Without exact reachability metadata, ReflectionDataBuilder records
subtype members that hide a registered method or field, so that a
lookup on the subtype does not return the supertype member. The check
was started from inside the ConcurrentHashMap.compute call that
registers the supertype member, i.e. before the new mapping is visible
to other threads.

checkHidingMethods and checkHidingFields either register a subtype
reachability handler, which is notified synchronously for subtypes that
are already reachable, or post a task for each reachable subtype. Both
paths run checkSubtypeForOverriding{Method,Field} as an analysis task
that can start on another thread right away. That task first checks
methods.containsKey or fields.containsKey for the supertype member and
skips it if absent. If it ran before compute returned, it saw no
registration and the hiding member was never recorded. Nothing re-runs
the check for an already reachable subtype, so the image then returns
the supertype member for a lookup on the subtype, and the reflection
metadata depends on thread timing.

Decide inside compute whether the check is needed, so that it still
runs exactly once per member, but start it only after compute has
returned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@oracle-contributor-agreement

Copy link
Copy Markdown

Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
The following contributors of this PR have not signed the OCA:

To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application.

When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated.

If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public.

@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. label Nov 5, 2026
@oracle-contributor-agreement

Copy link
Copy Markdown

Thank you for signing the OCA.

@oracle-contributor-agreement oracle-contributor-agreement Bot added OCA Verified All contributors have signed the Oracle Contributor Agreement. and removed OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. labels Nov 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant