Skip to content

Register the linked downcall shapes for native image - #88

Open
jnbdz wants to merge 1 commit into
smallrye:mainfrom
SiteNetSoft:fix-native-metadata
Open

jnbdz wants to merge 1 commit into
smallrye:mainfrom
SiteNetSoft:fix-native-metadata

Conversation

@jnbdz

@jnbdz jnbdz commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

The transform and transform-test goals now write META-INF/native-image/<groupId>/<artifactId>/smallrye-ffm/reachability-metadata.json (GraalVM 25 schema v1.2.0) with one foreign.downcalls entry per distinct downcall shape, so that a native-image build no longer needs the shapes registered by hand. The shapes are read back from the invokedynamic instructions which use Bootstraps.downcall, after the transform, so classes transformed by an earlier (incremental) build are included and @Dispatch call sites are covered too. Each entry has the return and parameter layouts and, as applicable, firstVariadicArg, captureCallState and critical (allowHeapAccess); entries are deduplicated and sorted, and the file is only rewritten when its content changes (it is removed when there are no downcalls). The layouts follow Bootstraps.layoutOf on a 64-bit platform (the only kind native-image supports), with the names Quarkus uses in FfmType (jint, jlong, void*, ...); C long and wchar_t use their canonical names, which native-image resolves for the target, and a shape with a C char is registered twice, as jbyte and as jint, because Bootstraps picks one or the other by the signedness of char on the platform. The file is in its own smallrye-ffm directory below <groupId>/<artifactId> so that it never overwrites a reachability-metadata.json the project ships itself (native-image walks every directory under META-INF/native-image), and no JSON merging is needed. -Dsmallrye-ffm.skipNativeMetadata (skipNativeMetadata) turns it off. The reflection and run-time initialization entries that the bootstrap may need (Linker.downcallHandle looked up with findVirtual, Bootstraps.<clinit>) are not included, since I could not verify them without a native build; this PR only covers the foreign section.

The mapping duplicates Bootstraps.layoutOf, so a change there needs the same change in NativeMetadata; in particular #86 promotes narrow variadic arguments to int, and whichever of the two is merged second should map those to jint.

NativeMetadataTest builds a class with @Link methods (fixed, variadic with the marker on a parameter and on the method, @Critical, ErrnoConsumer, @As(stdc_long), a duplicate shape, a void function), runs the transform goal, and compares the whole file; it also runs the goal again over the already-transformed class, checks the skip parameter, that no file is written without downcalls, and the two forms of a char shape. It fails on main, where nothing is written. The layout names were also checked against Bootstraps.layoutOf at run time on Linux x86_64 for every descriptor code.

mvn verify on JDK 25 (Linux x86_64): 38 tests, 3 skipped.

The transform goals now write META-INF/native-image/<groupId>/<artifactId>/smallrye-ffm/reachability-metadata.json with a foreign.downcalls entry for each distinct downcall shape, read back from the invokedynamic instructions which use the Bootstraps.downcall bootstrap, so that classes transformed by an earlier build are included. The layouts follow Bootstraps.layoutOf on a 64-bit platform; C long and wchar_t use their canonical names, and a shape with a C char is registered both as jbyte and as jint. The skipNativeMetadata parameter turns it off.
Comment on lines +40 to +41
@Parameter(defaultValue = "false", property = "smallrye-ffm.skipNativeMetadata")
private boolean skipNativeMetadata;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please make this opt-in instead of opt-out?

sb.append("}\n");
Files.createDirectories(file.getParent());
byte[] bytes = sb.toString().getBytes(StandardCharsets.UTF_8);
if (Files.exists(file) && Arrays.equals(Files.readAllBytes(file), bytes)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if something else is competing to write the metadata for other things, like reflection etc.?

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate the GraalVM native-image registration for the call shapes the plugin links

2 participants