Skip to content

Commit 9df1ea1

Browse files
committed
[GR-68445] Filter out non-user class and module path entries when running -H:TrackDynamicAccess=all and make com.oracle.svm.hosted.NativeImageClassLoaderSupport#getModulePathsFinder public
PullRequest: graal/21752
2 parents fc3dc54 + 34898a5 commit 9df1ea1

File tree

5 files changed

+46
-10
lines changed

5 files changed

+46
-10
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/SharedConstants.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ public final class SharedConstants {
4141
* environment variable is not set for all other builds.
4242
*/
4343
public static final String REBUILD_AFTER_ANALYSIS_MARKER = "com.oracle.svm.rebuild";
44+
45+
/**
46+
* The name of the environment variable containing paths to JAR files that are provided
47+
* internally by the native image on its image class or module path.
48+
*/
49+
public static final String IMAGE_PROVIDED_JARS_ENV_VARIABLE = "com.oracle.svm.provided";
4450
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/doc-files/TrackDynamicAccessHelp.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ The flag can be used in following ways:
99
1. -H:TrackDynamicAccess=all reports all dynamic access calls made across the entire project
1010
2. -H:TrackDynamicAccess=path=<cp-entry> reports all dynamic access calls made from the specified class-path entry
1111
3. -H:TrackDynamicAccess=module=<module> reports all dynamic access calls made from the specified module
12-
4. -H:TrackDynamicAccess=package=<package> reports all dynamic access calls made from the specified package
13-
5. -H:TrackDynamicAccess=none disables all previous selections for dynamic access detection
14-
6. -H:TrackDynamicAccess=to-console outputs all detected dynamic access calls to the console
15-
7. -H:TrackDynamicAccess=no-dump disables the serialization of detected dynamic access calls
16-
8. A comma-separated list of the previous cases. For example, -H:TrackDynamicAccess=path=<cp-entry>,module=<module>,package=<package>
12+
4. -H:TrackDynamicAccess=module=ALL-UNNAMED reports all dynamic access calls made from all class-path entries
13+
5. -H:TrackDynamicAccess=package=<package> reports all dynamic access calls made from the specified package
14+
6. -H:TrackDynamicAccess=none disables all previous selections for dynamic access detection
15+
7. -H:TrackDynamicAccess=to-console outputs all detected dynamic access calls to the console
16+
8. -H:TrackDynamicAccess=no-dump disables the serialization of detected dynamic access calls
17+
9. A comma-separated list of the previous cases. For example, -H:TrackDynamicAccess=path=<cp-entry>,module=<module>,package=<package>
1718

1819
Example of the option usage:
1920

substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,7 @@ private int completeImageBuild() {
14741474
finalImageClasspath.addAll(finalImageProvidedJars);
14751475
}
14761476
finalImageProvidedJars.forEach(this::processClasspathNativeImageMetaInf);
1477+
imageBuilderJavaArgs.add("-D" + SharedConstants.IMAGE_PROVIDED_JARS_ENV_VARIABLE + "=" + String.join(File.pathSeparator, finalImageProvidedJars.stream().map(Path::toString).toList()));
14771478

14781479
if (!config.buildFallbackImage()) {
14791480
Optional<ArgumentEntry> fallbackThresholdEntry = getHostedOptionArgument(imageBuilderArgs, oHFallbackThreshold);

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
import org.graalvm.nativeimage.libgraal.hosted.LibGraalLoader;
7979

8080
import com.oracle.svm.core.NativeImageClassLoaderOptions;
81+
import com.oracle.svm.core.SharedConstants;
8182
import com.oracle.svm.core.SubstrateOptions;
8283
import com.oracle.svm.core.SubstrateUtil;
8384
import com.oracle.svm.core.option.AccumulatingLocatableMultiOptionValue;
@@ -114,6 +115,8 @@ public final class NativeImageClassLoaderSupport {
114115
private final List<Path> imagemp;
115116
private final List<Path> buildmp;
116117

118+
private final Set<Path> imageProvidedJars;
119+
117120
private final EconomicMap<URI, EconomicSet<String>> classes;
118121
private final EconomicMap<URI, EconomicSet<String>> packages;
119122
private final EconomicSet<String> emptySet;
@@ -245,6 +248,8 @@ protected NativeImageClassLoaderSupport(ClassLoader defaultSystemClassLoader, St
245248

246249
upgradeAndSystemModuleFinder = createUpgradeAndSystemModuleFinder();
247250

251+
imageProvidedJars = parseImageProvidedJarsProperty();
252+
248253
ModuleFinder modulePathsFinder = getModulePathsFinder();
249254
Set<String> moduleNames = modulePathsFinder.findAll().stream()
250255
.map(moduleReference -> moduleReference.descriptor().name())
@@ -305,7 +310,7 @@ public List<ClassLoader> getClassLoaders() {
305310
return classLoaders;
306311
}
307312

308-
private ModuleFinder getModulePathsFinder() {
313+
public ModuleFinder getModulePathsFinder() {
309314
return ModuleFinder.of(imagemp.toArray(Path[]::new));
310315
}
311316

@@ -324,7 +329,12 @@ public void loadAllClasses(ForkJoinPool executor, ImageClassLoader imageClassLoa
324329
LogUtils.warning(msg);
325330

326331
var origin = new IncludeOptionsSupport.ExtendedOptionWithOrigin(new IncludeOptionsSupport.ExtendedOption("", PreserveOptionsSupport.PRESERVE_ALL), preserveAllOrigin);
327-
getModulePathsFinder().findAll().forEach(m -> preserveSelectors.addModule(m.descriptor().name(), origin));
332+
for (ModuleReference m : getModulePathsFinder().findAll()) {
333+
var modulePath = m.location().map(Path::of).orElse(null);
334+
if (!imageProvidedJars.contains(modulePath)) {
335+
preserveSelectors.addModule(m.descriptor().name(), origin);
336+
}
337+
}
328338
PreserveOptionsSupport.JDK_MODULES_TO_PRESERVE.forEach(moduleName -> preserveSelectors.addModule(moduleName, origin));
329339
preserveSelectors.addModule(ALL_UNNAMED, origin);
330340
}
@@ -775,6 +785,18 @@ static Optional<String> getMainClassFromModule(Object module) {
775785
return ((Module) module).getDescriptor().mainClass();
776786
}
777787

788+
private static Set<Path> parseImageProvidedJarsProperty() {
789+
Set<Path> imageProvidedJars = new HashSet<>();
790+
String args = System.getProperty(SharedConstants.IMAGE_PROVIDED_JARS_ENV_VARIABLE, "");
791+
if (!args.isEmpty()) {
792+
String[] parts = args.split(File.pathSeparator);
793+
for (String part : parts) {
794+
imageProvidedJars.add(Path.of(part));
795+
}
796+
}
797+
return Collections.unmodifiableSet(imageProvidedJars);
798+
}
799+
778800
private final class LoadClassHandler {
779801

780802
private final ForkJoinPool executor;
@@ -1180,7 +1202,12 @@ public void setPreserveAll(ValueWithOrigin<String> valueWithOrigin) {
11801202

11811203
public void setTrackAllDynamicAccess(ValueWithOrigin<String> valueWithOrigin) {
11821204
var origin = new IncludeOptionsSupport.ExtendedOptionWithOrigin(new IncludeOptionsSupport.ExtendedOption("", DynamicAccessDetectionFeature.TRACK_ALL), valueWithOrigin);
1183-
getModulePathsFinder().findAll().forEach(m -> dynamicAccessSelectors.addModule(m.descriptor().name(), origin));
1205+
for (ModuleReference m : getModulePathsFinder().findAll()) {
1206+
var modulePath = m.location().map(Path::of).orElse(null);
1207+
if (!imageProvidedJars.contains(modulePath)) {
1208+
dynamicAccessSelectors.addModule(m.descriptor().name(), origin);
1209+
}
1210+
}
11841211
dynamicAccessSelectors.addModule(ALL_UNNAMED, origin);
11851212
}
11861213

@@ -1307,7 +1334,9 @@ public void addModule(String moduleName, IncludeOptionsSupport.ExtendedOptionWit
13071334
IncludeOptionsSupport.ExtendedOptionWithOrigin includeOptionsSupport = new IncludeOptionsSupport.ExtendedOptionWithOrigin(extendedOptionWithOrigin.option(),
13081335
extendedOptionWithOrigin.valueWithOrigin());
13091336
for (Path path : applicationClassPath()) {
1310-
classpathEntries.put(path, includeOptionsSupport);
1337+
if (!imageProvidedJars.contains(path)) {
1338+
classpathEntries.put(path, includeOptionsSupport);
1339+
}
13111340
}
13121341
} else {
13131342
moduleNames.put(moduleName, extendedOptionWithOrigin);

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/phases/DynamicAccessDetectionPhase.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ public record MethodInfo(DynamicAccessKind accessKind, String signature) {
221221
new MethodSignature("getResourceAsStream", String.class)));
222222
resourceMethodSignatures.put(Class.class, Set.of(
223223
new MethodSignature("getResource", String.class),
224-
225224
new MethodSignature("getResourceAsStream", String.class)));
226225

227226
foreignMethodSignatures.put(Linker.class, Set.of(

0 commit comments

Comments
 (0)