Repository navigation
Register the linked downcall shapes for native image #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jnbdz
wants to merge
1
commit into
smallrye:main
Choose a base branch
from
SiteNetSoft:fix-native-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+417
−3
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
maven-plugin/src/main/java/io/smallrye/ffm/maven/NativeMetadata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| package io.smallrye.ffm.maven; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.TreeSet; | ||
|
|
||
| import io.smallrye.classfile.ClassModel; | ||
| import io.smallrye.classfile.CodeElement; | ||
| import io.smallrye.classfile.MethodModel; | ||
| import io.smallrye.classfile.instruction.InvokeDynamicInstruction; | ||
|
|
||
| /** | ||
| * The GraalVM native image reachability metadata for the downcalls linked by transformed classes. | ||
| * <p> | ||
| * native-image compiles one stub for each downcall shape at build time, and each shape must be registered | ||
| * in the {@code foreign} section of a {@code reachability-metadata.json} file. | ||
| * The shapes are read back from the {@code invokedynamic} instructions which use the {@code Bootstraps.downcall} | ||
| * bootstrap, so classes which were transformed by an earlier build are included as well. | ||
| * <p> | ||
| * The layouts are those which {@code Bootstraps} selects on a 64-bit platform (the only kind native-image supports). | ||
| * The C {@code long} and {@code wchar_t} types are given by their canonical layout names, which native-image resolves | ||
| * for the target platform. | ||
| * The C {@code char} type is a {@code jbyte} where it is signed and a {@code jint} where it is unsigned, | ||
| * so a shape which uses it is registered in both forms. | ||
| */ | ||
| final class NativeMetadata { | ||
| private static final String BOOTSTRAPS = "io/smallrye/ffm/Bootstraps"; | ||
|
|
||
| private final Set<String> downcalls = new TreeSet<>(); | ||
|
|
||
| NativeMetadata() { | ||
| } | ||
|
|
||
| /** | ||
| * Add the downcall shapes linked by the given class. | ||
| * | ||
| * @param cm the class model (must not be {@code null}) | ||
| */ | ||
| void addClass(ClassModel cm) { | ||
| for (MethodModel mm : cm.methods()) { | ||
| mm.code().ifPresent(code -> { | ||
| for (CodeElement ce : code) { | ||
| if (ce instanceof InvokeDynamicInstruction idi | ||
| && idi.bootstrapMethod().owner().descriptorString().equals("L" + BOOTSTRAPS + ";") | ||
| && idi.bootstrapMethod().methodName().equals("downcall")) { | ||
| downcalls.addAll(entries(idi.name().stringValue())); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * {@return the downcall entries, in a stable order} | ||
| */ | ||
| Set<String> downcalls() { | ||
| return downcalls; | ||
| } | ||
|
|
||
| /** | ||
| * Write the metadata file, or delete it if there are no downcalls. | ||
| * | ||
| * @param file the file to write (must not be {@code null}) | ||
| * @throws IOException if the file could not be written | ||
| */ | ||
| void write(Path file) throws IOException { | ||
| if (downcalls.isEmpty()) { | ||
| Files.deleteIfExists(file); | ||
| return; | ||
| } | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("{\n"); | ||
| sb.append(" \"comment\": \"Generated by smallrye-ffm-maven-plugin for the downcalls linked by this module\",\n"); | ||
| sb.append(" \"foreign\": {\n"); | ||
| sb.append(" \"downcalls\": [\n"); | ||
| boolean first = true; | ||
| for (String entry : downcalls) { | ||
| if (!first) { | ||
| sb.append(",\n"); | ||
| } | ||
| first = false; | ||
| sb.append(" ").append(entry); | ||
| } | ||
| sb.append("\n ]\n"); | ||
| sb.append(" }\n"); | ||
| 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)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.? |
||
| // keep the time stamp | ||
| return; | ||
| } | ||
| Files.write(file, bytes); | ||
| } | ||
|
|
||
| /** | ||
| * Get the {@code foreign.downcalls} entries for a downcall descriptor of the form produced by the generator, | ||
| * for example {@code e(*sI~*)sI}. | ||
| * | ||
| * @param desc the downcall descriptor (must not be {@code null}) | ||
| * @return the JSON entries (one, or two if the descriptor uses the C {@code char} type) | ||
| */ | ||
| static List<String> entries(String desc) { | ||
| String signedChar = entry(desc, "jbyte"); | ||
| String unsignedChar = entry(desc, "jint"); | ||
| return signedChar.equals(unsignedChar) ? List.of(signedChar) : List.of(signedChar, unsignedChar); | ||
| } | ||
|
|
||
| private static String entry(String desc, String charLayout) { | ||
| boolean capture = false; | ||
| int critical = -1; | ||
| int i = 0; | ||
| loop: for (;; i++) { | ||
| switch (desc.charAt(i)) { | ||
| case 'e', 'E', 'W' -> capture = true; | ||
| case 'r' -> critical = Math.max(critical, 0); | ||
| case 'R' -> critical = 1; | ||
| case '(' -> { | ||
| i++; | ||
| break loop; | ||
| } | ||
| default -> throw invalid(desc); | ||
| } | ||
| } | ||
| int firstVariadic = -1; | ||
| List<String> params = new ArrayList<>(); | ||
| while (desc.charAt(i) != ')') { | ||
| if (desc.charAt(i) == '~') { | ||
| firstVariadic = params.size(); | ||
| i++; | ||
| } else { | ||
| params.add(layout(desc, i, firstVariadic != -1, charLayout)); | ||
| i += desc.charAt(i) == 'u' || desc.charAt(i) == 's' ? 2 : 1; | ||
| } | ||
| } | ||
| i++; | ||
| String ret = desc.charAt(i) == 'V' ? "void" : layout(desc, i, false, charLayout); | ||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("{\"returnType\": \"").append(ret).append("\", \"parameterTypes\": ["); | ||
| for (int j = 0; j < params.size(); j++) { | ||
| if (j > 0) { | ||
| sb.append(", "); | ||
| } | ||
| sb.append('"').append(params.get(j)).append('"'); | ||
| } | ||
| sb.append(']'); | ||
| if (capture || firstVariadic != -1 || critical != -1) { | ||
| sb.append(", \"options\": {"); | ||
| String sep = ""; | ||
| if (capture) { | ||
| sb.append("\"captureCallState\": true"); | ||
| sep = ", "; | ||
| } | ||
| if (firstVariadic != -1) { | ||
| sb.append(sep).append("\"firstVariadicArg\": ").append(firstVariadic); | ||
| sep = ", "; | ||
| } | ||
| if (critical != -1) { | ||
| sb.append(sep).append("\"critical\": {\"allowHeapAccess\": ").append(critical == 1).append('}'); | ||
| } | ||
| sb.append('}'); | ||
| } | ||
| sb.append('}'); | ||
| return sb.toString(); | ||
| } | ||
|
|
||
| /** | ||
| * {@return the canonical layout name for the descriptor item at the given offset} | ||
| * This follows {@code Bootstraps.layoutOf} for a 64-bit platform. | ||
| */ | ||
| private static String layout(String desc, int offs, boolean variadic, String charLayout) { | ||
| return switch (desc.charAt(offs)) { | ||
| case 'u' -> switch (desc.charAt(offs + 1)) { | ||
| case 'B', 'c', 's', 'S' -> "jint"; | ||
| case 'I', 'i', 'J', 'l', '*' -> "jlong"; | ||
| default -> throw invalid(desc); | ||
| }; | ||
| case 'e' -> "jint"; | ||
| case 's' -> switch (desc.charAt(offs + 1)) { | ||
| case 'B', 'c' -> "jbyte"; | ||
| case 'S', 's' -> "jshort"; | ||
| case 'I', 'i' -> "jint"; | ||
| case 'l' -> "long"; | ||
| case 'J', '*' -> "jlong"; | ||
| default -> throw invalid(desc); | ||
| }; | ||
| case 'c' -> charLayout; | ||
| case 'w' -> "wchar_t"; | ||
| case '*' -> "void*"; | ||
| case 'Z' -> "jboolean"; | ||
| case 'F' -> variadic ? "jdouble" : "jfloat"; | ||
| case 'D' -> "jdouble"; | ||
| default -> throw invalid(desc); | ||
| }; | ||
| } | ||
|
|
||
| private static IllegalArgumentException invalid(String desc) { | ||
| return new IllegalArgumentException("Invalid downcall descriptor " + desc); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?