Skip to content

Commit 2c7c747

Browse files
committed
[GR-71065] Introduce ResolvedJavaModule and ResolvedJavaPackage
PullRequest: graal/22534
2 parents 8990ae7 + 9d0f7bc commit 2c7c747

28 files changed

+678
-102
lines changed

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/darwin/DarwinSubstitutions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.oracle.svm.core.posix.PosixUtils;
4444
import com.oracle.svm.core.posix.headers.darwin.DarwinTime;
4545
import com.oracle.svm.core.util.BasedOnJDKFile;
46+
import com.oracle.svm.util.ResolvedJavaModuleLayer;
4647

4748
import jdk.internal.misc.Unsafe;
4849

@@ -138,7 +139,7 @@ final class Target_java_util_prefs_FileSystemPreferences {
138139
final class IsJavaUtilPrefsPresent implements BooleanSupplier {
139140
@Override
140141
public boolean getAsBoolean() {
141-
var prefsMod = ModuleLayer.boot().findModule("java.prefs");
142+
var prefsMod = ResolvedJavaModuleLayer.boot().findModule("java.prefs");
142143
return prefsMod.isPresent();
143144
}
144145
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import com.oracle.svm.core.util.UserError;
8282
import com.oracle.svm.core.util.VMError;
8383
import com.oracle.svm.util.LogUtils;
84+
import com.oracle.svm.util.ResolvedJavaModuleLayer;
8485

8586
import jdk.graal.compiler.api.replacements.Fold;
8687
import jdk.graal.compiler.asm.amd64.AMD64Assembler;
@@ -975,7 +976,7 @@ public static boolean hasFramePointer() {
975976
@Override
976977
protected void onValueUpdate(EconomicMap<OptionKey<?>, Object> values, String oldValue, String newValue) {
977978
if ("llvm".equals(newValue)) {
978-
boolean isLLVMBackendMissing = ModuleLayer.boot().findModule("org.graalvm.nativeimage.llvm").isEmpty();
979+
boolean isLLVMBackendMissing = ResolvedJavaModuleLayer.boot().findModule("org.graalvm.nativeimage.llvm").isEmpty();
979980
if (isLLVMBackendMissing) {
980981
throw UserError.invalidOptionValue(CompilerBackend, newValue,
981982
"The LLVM backend for GraalVM Native Image is missing and needs to be built from source. " +

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/JavaNetHttpFeature.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@
3838
import com.oracle.svm.core.traits.BuiltinTraits.NoLayeredCallbacks;
3939
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind.Independent;
4040
import com.oracle.svm.core.traits.SingletonTraits;
41+
import com.oracle.svm.util.HostModuleUtil;
42+
import com.oracle.svm.util.ResolvedJavaModule;
43+
import com.oracle.svm.util.ResolvedJavaModuleLayer;
4144

4245
@AutomaticallyRegisteredFeature
4346
@SingletonTraits(access = BuildtimeAccessOnly.class, layeredCallbacks = NoLayeredCallbacks.class, layeredInstallationKind = Independent.class)
4447
public class JavaNetHttpFeature extends JNIRegistrationUtil implements InternalFeature {
4548

46-
private static Optional<Module> requiredModule() {
47-
return ModuleLayer.boot().findModule("java.net.http");
49+
private static Optional<ResolvedJavaModule> requiredModule() {
50+
return ResolvedJavaModuleLayer.boot().findModule("java.net.http");
4851
}
4952

5053
@Override
@@ -54,7 +57,7 @@ public boolean isInConfiguration(IsInConfigurationAccess access) {
5457

5558
@Override
5659
public void afterRegistration(AfterRegistrationAccess access) {
57-
JavaNetHttpFeature.class.getModule().addReads(requiredModule().get());
60+
HostModuleUtil.addReads(JavaNetHttpFeature.class, requiredModule().get());
5861
}
5962

6063
@Override
@@ -80,8 +83,8 @@ private static void registerInitFiltersAccess(DuringAnalysisAccess a) {
8083
@SingletonTraits(access = BuildtimeAccessOnly.class, layeredCallbacks = NoLayeredCallbacks.class, layeredInstallationKind = Independent.class)
8184
class SimpleWebServerFeature implements InternalFeature {
8285

83-
private static Optional<Module> requiredModule() {
84-
return ModuleLayer.boot().findModule("jdk.httpserver");
86+
private static Optional<ResolvedJavaModule> requiredModule() {
87+
return ResolvedJavaModuleLayer.boot().findModule("jdk.httpserver");
8588
}
8689

8790
@Override
@@ -91,7 +94,7 @@ public boolean isInConfiguration(IsInConfigurationAccess access) {
9194

9295
@Override
9396
public void afterRegistration(AfterRegistrationAccess access) {
94-
SimpleWebServerFeature.class.getModule().addReads(requiredModule().get());
97+
HostModuleUtil.addReads(SimpleWebServerFeature.class, requiredModule().get());
9598

9699
RuntimeClassInitializationSupport rci = ImageSingletons.lookup(RuntimeClassInitializationSupport.class);
97100
rci.initializeAtRunTime("sun.net.httpserver.simpleserver", "Allocates InetAddress in class initializers");

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_sun_misc_Unsafe.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.oracle.svm.core.annotate.RecomputeFieldValue;
3131
import com.oracle.svm.core.annotate.RecomputeFieldValue.Kind;
3232
import com.oracle.svm.core.annotate.TargetClass;
33+
import com.oracle.svm.util.ResolvedJavaModuleLayer;
3334

3435
/**
3536
* These substitutions are necessary because the static initializations of these fields are copies
@@ -101,6 +102,6 @@ final class Target_sun_misc_Unsafe {
101102
class JdkUnsupportedIsEnabled implements BooleanSupplier {
102103
@Override
103104
public boolean getAsBoolean() {
104-
return ModuleLayer.boot().findModule("jdk.unsupported").isPresent();
105+
return ResolvedJavaModuleLayer.boot().findModule("jdk.unsupported").isPresent();
105106
}
106107
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/Target_sun_rmi_transport_GC.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.oracle.svm.core.annotate.Substitute;
3434
import com.oracle.svm.core.annotate.TargetClass;
3535
import com.oracle.svm.core.heap.Heap;
36+
import com.oracle.svm.util.HostModuleUtil;
37+
import com.oracle.svm.util.ResolvedJavaModuleLayer;
3638

3739
/**
3840
* Note that sun.rmi.transport.GC is initialized at build-time to avoid including the rmi library,
@@ -55,9 +57,9 @@ class JavaRMIModuleAvailable implements BooleanSupplier {
5557
private static final boolean hasModule;
5658

5759
static {
58-
var module = ModuleLayer.boot().findModule("java.rmi");
60+
var module = ResolvedJavaModuleLayer.boot().findModule("java.rmi");
5961
if (module.isPresent()) {
60-
JavaRMIModuleAvailable.class.getModule().addReads(module.get());
62+
HostModuleUtil.addReads(JavaRMIModuleAvailable.class, module.get());
6163
}
6264
hasModule = module.isPresent();
6365
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/management/ManagementAgentModule.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
import java.util.function.BooleanSupplier;
3030

3131
import com.oracle.svm.core.util.VMError;
32+
import com.oracle.svm.util.HostModuleUtil;
3233
import com.oracle.svm.util.ReflectionUtil;
34+
import com.oracle.svm.util.ResolvedJavaModule;
35+
import com.oracle.svm.util.ResolvedJavaModuleLayer;
3336

3437
public class ManagementAgentModule {
3538

@@ -42,9 +45,9 @@ public class ManagementAgentModule {
4245
static final String CONFIG_FILE_OPEN_FAILED;
4346

4447
static {
45-
Optional<Module> agentModule = ModuleLayer.boot().findModule("jdk.management.agent");
48+
Optional<ResolvedJavaModule> agentModule = ResolvedJavaModuleLayer.boot().findModule("jdk.management.agent");
4649
if (agentModule.isPresent()) {
47-
ManagementAgentModule.class.getModule().addReads(agentModule.get());
50+
HostModuleUtil.addReads(ManagementAgentModule.class, agentModule.get());
4851
var agentClass = ReflectionUtil.lookupClass(false, "jdk.internal.agent.Agent");
4952
agentStartAgent = ReflectionUtil.lookupMethod(agentClass, "startAgent");
5053
agentError = ReflectionUtil.lookupMethod(agentClass, "error", String.class, String.class);

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/management/ManagementSupport.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
6464
import com.oracle.svm.core.thread.ThreadListener;
6565
import com.oracle.svm.core.util.UserError;
6666
import com.oracle.svm.core.util.VMError;
67+
import com.oracle.svm.util.HostModuleUtil;
68+
import com.oracle.svm.util.ResolvedJavaModuleLayer;
6769
import com.sun.jmx.mbeanserver.MXBeanLookup;
6870

6971
import jdk.graal.compiler.api.replacements.Fold;
@@ -297,9 +299,9 @@ private synchronized PlatformManagedObject getFlightRecorderMXBean() {
297299
@Platforms(Platform.HOSTED_ONLY.class)
298300
@SuppressWarnings("unchecked")
299301
private static Class<? extends PlatformManagedObject> getFlightRecorderMXBeanClass() {
300-
var jfrModule = ModuleLayer.boot().findModule("jdk.management.jfr");
302+
var jfrModule = ResolvedJavaModuleLayer.boot().findModule("jdk.management.jfr");
301303
if (jfrModule.isPresent()) {
302-
ManagementSupport.class.getModule().addReads(jfrModule.get());
304+
HostModuleUtil.addReads(ManagementSupport.class, jfrModule.get());
303305
try {
304306
return (Class<? extends PlatformManagedObject>) Class.forName("jdk.management.jfr.FlightRecorderMXBean", false, Object.class.getClassLoader());
305307
} catch (ClassNotFoundException ex) {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@
2424
*/
2525
package com.oracle.svm.hosted;
2626

27-
import com.oracle.graal.pointsto.meta.AnalysisMetaAccess;
28-
import com.oracle.svm.util.ReflectionUtil;
29-
30-
import org.graalvm.collections.EconomicMap;
31-
import org.graalvm.nativeimage.ImageSingletons;
32-
3327
import java.io.ObjectInputStream;
3428
import java.io.ObjectOutputStream;
3529
import java.io.ObjectStreamClass;
@@ -54,6 +48,13 @@
5448
import java.util.ResourceBundle;
5549
import java.util.Set;
5650

51+
import org.graalvm.collections.EconomicMap;
52+
import org.graalvm.nativeimage.ImageSingletons;
53+
54+
import com.oracle.graal.pointsto.meta.AnalysisMetaAccess;
55+
import com.oracle.svm.util.ReflectionUtil;
56+
import com.oracle.svm.util.ResolvedJavaModuleLayer;
57+
5758
import jdk.internal.access.JavaLangAccess;
5859
import jdk.internal.loader.BuiltinClassLoader;
5960
import jdk.internal.misc.Unsafe;
@@ -99,7 +100,7 @@ public static DynamicAccessDetectionSupport instance() {
99100

100101
public DynamicAccessDetectionSupport(AnalysisMetaAccess metaAccess) {
101102
this.metaAccess = metaAccess;
102-
boolean jdkUnsupportedModulePresent = ModuleLayer.boot().findModule("jdk.unsupported").isPresent();
103+
boolean jdkUnsupportedModulePresent = ResolvedJavaModuleLayer.boot().findModule("jdk.unsupported").isPresent();
103104

104105
put(reflectionMethods, Class.class, Set.of(
105106
new MethodSignature("forName", String.class),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ public void registerReachabilityHandler(Consumer<DuringAnalysisAccess> callback,
537537
case Class<?> clazz -> getMetaAccess().lookupJavaType(clazz);
538538
case Field field -> getMetaAccess().lookupJavaField(field);
539539
case Executable executable -> getMetaAccess().lookupJavaMethod(executable);
540+
case AnalysisElement ae -> ae;
540541
default -> throw UserError.abort("'registerReachabilityHandler' called with an element that is not a Class, Field, or Executable: %s",
541542
trigger.getClass().getTypeName());
542543
};

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,33 @@
2626

2727
import java.io.IOException;
2828
import java.io.InputStream;
29-
import java.lang.reflect.Method;
3029
import java.net.URISyntaxException;
3130
import java.net.URL;
3231
import java.nio.file.FileSystem;
3332
import java.nio.file.FileSystems;
3433
import java.nio.file.Files;
3534
import java.nio.file.Path;
3635
import java.nio.file.Paths;
37-
import java.security.CodeSource;
38-
import java.security.ProtectionDomain;
3936
import java.util.HashSet;
4037
import java.util.Optional;
4138
import java.util.Properties;
4239
import java.util.Set;
4340
import java.util.stream.Stream;
4441

42+
import com.oracle.graal.pointsto.meta.AnalysisMethod;
43+
import com.oracle.graal.pointsto.meta.AnalysisType;
4544
import com.oracle.svm.core.feature.AutomaticallyRegisteredFeature;
4645
import com.oracle.svm.core.feature.InternalFeature;
4746
import com.oracle.svm.core.traits.BuiltinTraits.BuildtimeAccessOnly;
4847
import com.oracle.svm.core.traits.BuiltinTraits.NoLayeredCallbacks;
4948
import com.oracle.svm.core.traits.SingletonLayeredInstallationKind.Independent;
5049
import com.oracle.svm.core.traits.SingletonTraits;
50+
import com.oracle.svm.hosted.FeatureImpl.AfterAnalysisAccessImpl;
51+
import com.oracle.svm.util.JVMCIReflectionUtil;
5152
import com.oracle.svm.util.LogUtils;
53+
import com.oracle.svm.util.ResolvedJavaPackage;
54+
55+
import jdk.vm.ci.meta.ResolvedJavaType;
5256

5357
/**
5458
* A feature that detects whether a native image may be vulnerable to Log4Shell.
@@ -67,14 +71,8 @@ public class Log4ShellFeature implements InternalFeature {
6771
/* Different versions of log4j overload all these methods. */
6872
private static final Set<String> targetMethods = Set.of("debug", "error", "fatal", "info", "log", "trace", "warn");
6973

70-
private static Optional<String> getPomVersion(Class<?> log4jClass) {
71-
ProtectionDomain pd = log4jClass.getProtectionDomain();
72-
CodeSource cs = pd.getCodeSource();
73-
74-
if (cs == null) {
75-
return Optional.empty();
76-
}
77-
URL location = cs.getLocation();
74+
private static Optional<String> getPomVersion(ResolvedJavaType log4jClass) {
75+
URL location = JVMCIReflectionUtil.getOrigin(log4jClass);
7876
if (location == null) {
7977
return Optional.empty();
8078
}
@@ -148,20 +146,20 @@ private static boolean vulnerableLog4jTwo(String[] components) {
148146
return false;
149147
}
150148

151-
private AfterAnalysisAccess afterAnalysisAccess;
149+
private AfterAnalysisAccessImpl afterAnalysisAccess;
152150

153151
@Override
154152
public void afterAnalysis(AfterAnalysisAccess access) {
155-
this.afterAnalysisAccess = access;
153+
this.afterAnalysisAccess = (AfterAnalysisAccessImpl) access;
156154
}
157155

158156
public String getUserWarning() {
159-
Class<?> log4jClass = afterAnalysisAccess.findClassByName(log4jClassName);
157+
AnalysisType log4jClass = afterAnalysisAccess.findTypeByName(log4jClassName);
160158
if (log4jClass == null) {
161159
return null;
162160
}
163161

164-
Package log4jPackage = log4jClass.getPackage();
162+
ResolvedJavaPackage log4jPackage = JVMCIReflectionUtil.getPackage(log4jClass);
165163
String version = log4jPackage.getImplementationVersion();
166164

167165
if (version == null) {
@@ -186,21 +184,21 @@ public String getUserWarning() {
186184
Set<String> vulnerableMethods = new HashSet<>();
187185

188186
if (("1".equals(components[0]) && vulnerableLog4jOne(components)) || ("2".equals(components[0]) && vulnerableLog4jTwo(components))) {
189-
for (Method method : log4jClass.getMethods()) {
187+
for (AnalysisMethod method : log4jClass.getDeclaredMethods(false)) {
190188
String methodName = method.getName();
191-
if (targetMethods.contains(methodName) && (afterAnalysisAccess.isReachable(method) || (afterAnalysisAccess.reachableMethodOverrides(method).size() > 0))) {
192-
vulnerableMethods.add(method.getDeclaringClass().getName() + "." + method.getName());
189+
if (targetMethods.contains(methodName) && (afterAnalysisAccess.isReachable(method) || (!afterAnalysisAccess.reachableMethodOverrides(method).isEmpty()))) {
190+
vulnerableMethods.add(method.getDeclaringClass().toClassName() + "." + method.getName());
193191
}
194192
}
195193
}
196194

197-
if (vulnerableMethods.size() == 0) {
195+
if (vulnerableMethods.isEmpty()) {
198196
return null;
199197
}
200198

201199
StringBuilder renderedErrorMessage = new StringBuilder(String.format(log4jVulnerableErrorMessage));
202200
for (String method : vulnerableMethods) {
203-
renderedErrorMessage.append(System.lineSeparator() + " - " + method);
201+
renderedErrorMessage.append(System.lineSeparator()).append(" - ").append(method);
204202
}
205203
return renderedErrorMessage.toString();
206204
}

0 commit comments

Comments
 (0)