Skip to content

Commit 1767a11

Browse files
committed
[JDK-8340655] [GR-58466] Fix source launcher regression.
PullRequest: graal/18881
2 parents 217a1e8 + 5b9fa2c commit 1767a11

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

compiler/mx.compiler/mx_compiler.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,11 @@ def apply(self, config):
820820
if limited_modules is None or jmd.name in limited_modules:
821821
mainClassArgs.extend(['-JUnitOpenPackages', jmd.name + '/*'])
822822
vmArgs.append('--add-modules=' + jmd.name)
823+
for dependency, packages in jmd.concealedRequires.items():
824+
if dependency != "jdk.internal.vm.ci":
825+
# JVMCI exporting is done dynamically
826+
for p in packages:
827+
vmArgs.append(f'--add-exports={dependency}/{p}={jmd.name}')
823828

824829
vmArgs.append('-Djdk.graal.TrackNodeSourcePosition=true')
825830
vmArgs.append('-esa')

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/HotSpotGraalCompilerFactory.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ static class Options {
169169

170170
@Override
171171
public HotSpotGraalCompiler createCompiler(JVMCIRuntime runtime) {
172-
ensureInitialized();
173172
HotSpotJVMCIRuntime hsRuntime = (HotSpotJVMCIRuntime) runtime;
173+
checkUnsafeAccess(hsRuntime);
174+
ensureInitialized();
174175
if (optionsFailure != null) {
175176
System.err.printf("Error parsing Graal options: %s%nError: A fatal exception has occurred. Program will exit.%n", optionsFailure.getMessage());
176177
HotSpotGraalServices.exit(1, hsRuntime);
@@ -195,6 +196,28 @@ public HotSpotGraalCompiler createCompiler(JVMCIRuntime runtime) {
195196
return compiler;
196197
}
197198

199+
/**
200+
* Exit the VM now if {@code jdk.internal.misc.Unsafe} is not accessible.
201+
*/
202+
private void checkUnsafeAccess(HotSpotJVMCIRuntime hsRuntime) {
203+
if (Services.IS_IN_NATIVE_IMAGE) {
204+
// Access checks were performed when building libgraal.
205+
return;
206+
}
207+
try {
208+
jdk.internal.misc.Unsafe.getUnsafe();
209+
} catch (IllegalAccessError e) {
210+
Module module = getClass().getModule();
211+
String targets = module.getName();
212+
String ee = "com.oracle.graal.graal_enterprise";
213+
if (module.getDescriptor().exports().stream().anyMatch(export -> export.targets().contains(ee))) {
214+
targets += "," + ee;
215+
}
216+
System.err.printf("Error: jargraal requires --add-exports=java.base/jdk.internal.misc=%s to be specified to the launcher.%n", targets);
217+
HotSpotGraalServices.exit(1, hsRuntime);
218+
}
219+
}
220+
198221
/**
199222
* Creates a new {@link HotSpotGraalRuntime} object and a new {@link HotSpotGraalCompiler} and
200223
* returns the latter.

sdk/mx.sdk/mx_sdk_vm.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,11 @@ def _get_image_vm_options(jdk, use_upgrade_module_path, modules, synthetic_modul
837837
if default_to_jvmci == 'lib':
838838
vm_options.append('-XX:+UseJVMCINativeLibrary')
839839
vm_options.extend(['-XX:-UnlockExperimentalVMOptions'])
840-
if 'jdk.graal.compiler' in non_synthetic_modules:
840+
import mx_sdk_vm_impl
841+
if 'jdk.graal.compiler' in non_synthetic_modules and mx_sdk_vm_impl._get_libgraal_component() is None:
842+
# If libgraal is absent, jargraal is used by default.
843+
# Use of jargraal requires exporting jdk.internal.misc to
844+
# Graal as it uses jdk.internal.misc.Unsafe.
841845
if 'com.oracle.graal.graal_enterprise' in non_synthetic_modules:
842846
vm_options.extend(['--add-exports=java.base/jdk.internal.misc=jdk.graal.compiler,com.oracle.graal.graal_enterprise'])
843847
else:

substratevm/src/com.oracle.svm.graal.hotspot/src/com/oracle/svm/graal/hotspot/GetCompilerConfig.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
*/
2525
package com.oracle.svm.graal.hotspot;
2626

27+
import java.io.BufferedReader;
2728
import java.io.IOException;
29+
import java.io.InputStreamReader;
2830
import java.nio.file.Files;
2931
import java.nio.file.Path;
3032
import java.util.ArrayList;
@@ -65,6 +67,35 @@ public class GetCompilerConfig {
6567
public record Result(String encodedConfig, Map<String, Set<String>> opens) {
6668
}
6769

70+
/**
71+
* Tests whether {@code module} is in the boot layer.
72+
*
73+
* @param javaExe java executable
74+
* @param module name of the module to test
75+
*/
76+
private static boolean isInBootLayer(Path javaExe, String module) {
77+
String search = "jrt:/" + module;
78+
String[] command = {javaExe.toString(), "--show-module-resolution", "--version"};
79+
try {
80+
Process p = new ProcessBuilder(command).start();
81+
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
82+
String line;
83+
boolean found = false;
84+
while ((line = reader.readLine()) != null) {
85+
if (line.contains(search)) {
86+
found = true;
87+
}
88+
}
89+
int exitValue = p.waitFor();
90+
if (exitValue != 0) {
91+
throw new GraalError("Command finished with exit value %d: %s", exitValue, String.join(" ", command));
92+
}
93+
return found;
94+
} catch (Exception e) {
95+
throw new GraalError(e, "Error running command: %s", String.join(" ", command));
96+
}
97+
}
98+
6899
/**
69100
* Launches the JVM in {@code javaHome} to run {@link CompilerConfig}.
70101
*
@@ -81,11 +112,18 @@ public static Result from(Path javaHome, OptionValues options) {
81112
// java.util.ImmutableCollections.EMPTY
82113
"java.base", Set.of("java.util"));
83114

115+
// Only modules in the boot layer can be the target of --add-exports
116+
String addExports = "--add-exports=java.base/jdk.internal.misc=jdk.graal.compiler";
117+
if (isInBootLayer(javaExe, "com.oracle.graal.graal_enterprise")) {
118+
addExports += ",com.oracle.graal.graal_enterprise";
119+
}
120+
84121
List<String> command = new ArrayList<>(List.of(
85122
javaExe.toFile().getAbsolutePath(),
86123
"-XX:+UnlockExperimentalVMOptions",
87124
"-XX:+EnableJVMCI",
88125
"-XX:-UseJVMCICompiler", // avoid deadlock with jargraal
126+
addExports,
89127
"-Djdk.vm.ci.services.aot=true"));
90128

91129
Module module = ObjectCopier.class.getModule();

0 commit comments

Comments
 (0)