Skip to content

Commit ab482b5

Browse files
committed
removed unnecessary exporting of internal packages
1 parent 8de06d3 commit ab482b5

File tree

7 files changed

+69
-395
lines changed

7 files changed

+69
-395
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/libgraal/GetCompilerConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public static Result from(Path javaHome) {
119119
// Required to use Modules class
120120
"--add-exports=java.base/jdk.internal.module=jdk.graal.compiler",
121121
addExports,
122-
"-Djdk.vm.ci.services.aot=true",
122+
"-Djdk.vm.ci.services.aot=true", // Remove after JDK-8346781
123123
"-D%s=%s".formatted(ImageInfo.PROPERTY_IMAGE_CODE_KEY, ImageInfo.PROPERTY_IMAGE_CODE_VALUE_BUILDTIME)));
124124

125125
for (var e : opens.entrySet()) {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/libgraal/GetJNIConfig.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,6 @@ private GraalError error(String format, Object... args) {
197197
*/
198198
@SuppressWarnings("try")
199199
public static void register(ClassLoader loader) {
200-
// Export all JVMCI packages to this class
201-
LibGraalFeature.exportModulesToLibGraal("jdk.internal.vm.ci");
202-
203200
try (GetJNIConfig source = new GetJNIConfig(loader)) {
204201
Map<String, Class<?>> classes = new HashMap<>();
205202
for (String line : source.lines) {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/libgraal/LibGraalFeature.java

Lines changed: 58 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@
2424
*/
2525
package jdk.graal.compiler.libgraal;
2626

27-
import static java.lang.invoke.MethodType.methodType;
28-
29-
import java.lang.invoke.MethodHandle;
30-
import java.lang.invoke.MethodHandles;
27+
import java.lang.module.ModuleDescriptor;
3128
import java.lang.reflect.Field;
32-
import java.lang.reflect.Method;
3329
import java.nio.file.Path;
3430
import java.util.ArrayList;
3531
import java.util.Collections;
@@ -52,10 +48,10 @@
5248
import jdk.graal.compiler.serviceprovider.GraalServices;
5349
import jdk.graal.compiler.truffle.host.TruffleHostEnvironment;
5450
import jdk.graal.compiler.util.ObjectCopier;
51+
import jdk.internal.module.Modules;
5552
import jdk.vm.ci.hotspot.HotSpotJVMCIBackendFactory;
5653
import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime;
5754
import jdk.vm.ci.services.JVMCIServiceLocator;
58-
import jdk.vm.ci.services.Services;
5955
import org.graalvm.collections.EconomicMap;
6056
import org.graalvm.jniutils.NativeBridgeSupport;
6157
import org.graalvm.nativeimage.ImageSingletons;
@@ -116,6 +112,35 @@ public static LibGraalFeature singleton() {
116112
return singleton;
117113
}
118114

115+
/**
116+
* Looks up a class in the libgraal class loader.
117+
*
118+
* @throws Error if the lookup fails
119+
*/
120+
public static Class<?> lookupClass(String className) {
121+
try {
122+
return Class.forName(className, false, LibGraalFeature.class.getClassLoader());
123+
} catch (ClassNotFoundException ex) {
124+
throw new GraalError(ex);
125+
}
126+
}
127+
128+
/**
129+
* Looks up a field via reflection and makes it accessible for reading.
130+
*
131+
* @throws Error if the operation fails
132+
*/
133+
public static Field lookupField(Class<?> declaringClass, String fieldName) {
134+
try {
135+
Field result = declaringClass.getDeclaredField(fieldName);
136+
Modules.addOpensToAllUnnamed(declaringClass.getModule(), declaringClass.getPackageName());
137+
result.setAccessible(true);
138+
return result;
139+
} catch (ReflectiveOperationException ex) {
140+
throw new GraalError(ex);
141+
}
142+
}
143+
119144
public static final class IsEnabled implements BooleanSupplier {
120145
@Override
121146
public boolean getAsBoolean() {
@@ -124,8 +149,6 @@ public boolean getAsBoolean() {
124149
}
125150
}
126151

127-
final MethodHandles.Lookup mhl = MethodHandles.lookup();
128-
129152
final LibGraalLoader libgraalLoader = (LibGraalLoader) getClass().getClassLoader();
130153

131154
/**
@@ -139,41 +162,21 @@ public void addFeatureComponent(FeatureComponent fc) {
139162

140163
@Override
141164
public void afterRegistration(AfterRegistrationAccess access) {
142-
// LibGraalEntryPoints uses a number of classes in org.graalvm.nativeimage.builder
143-
exportModulesToLibGraal("org.graalvm.nativeimage.builder");
144-
145-
// LibGraalFeature accesses a few Graal classes (see import statements above)
146-
exportModulesToLibGraal("jdk.graal.compiler");
147-
148-
// LibGraalTruffleToLibGraalEntryPoints access TruffleToLibGraal.Id
149-
exportModulesToLibGraal("org.graalvm.truffle.compiler");
150-
151165
ImageSingletons.add(NativeBridgeSupport.class, new LibGraalNativeBridgeSupport());
152166

153-
// Guest JVMCI and Graal need access to some JDK internal packages
154-
String[] basePackages = {
155-
"jdk.internal.misc",
156-
"jdk.internal.util",
157-
"jdk.internal.vm"};
158-
LibGraalUtil.accessPackagesToClass(LibGraalUtil.Access.EXPORT, null, false, "java.base", basePackages);
159-
}
160-
161-
static void exportModulesToLibGraal(String... moduleNames) {
162-
accessModulesToClass(LibGraalUtil.Access.EXPORT, LibGraalFeature.class, moduleNames);
163-
}
164-
165-
static void accessModulesToClass(LibGraalUtil.Access access, Class<?> accessingClass, String... moduleNames) {
166-
for (String moduleName : moduleNames) {
167-
var module = getBootModule(moduleName);
168-
LibGraalUtil.accessPackagesToClass(access, accessingClass, false,
169-
module.getName(), module.getPackages().toArray(String[]::new));
167+
// The qualified exports from java.base to jdk.internal.vm.ci
168+
// and jdk.graal.compiler need to be expressed as exports to
169+
// ALL-UNNAMED so that access is also possible when these classes
170+
// are loaded via the libgraal loader.
171+
Module javaBase = ModuleLayer.boot().findModule("java.base").orElseThrow();
172+
Set<ModuleDescriptor.Exports> exports = javaBase.getDescriptor().exports();
173+
for (ModuleDescriptor.Exports e : exports) {
174+
if (e.targets().contains("jdk.internal.vm.ci") || e.targets().contains("jdk.graal.compiler")) {
175+
Modules.addExportsToAllUnnamed(javaBase, e.source());
176+
}
170177
}
171178
}
172179

173-
static Module getBootModule(String moduleName) {
174-
return ModuleLayer.boot().findModule(moduleName).orElseThrow();
175-
}
176-
177180
@Override
178181
public void duringSetup(DuringSetupAccess access) {
179182
optionCollector = new OptionCollector();
@@ -247,18 +250,12 @@ class FieldOffsetsTransformer implements FieldValueTransformer {
247250
*/
248251
private final Map<Object, Map.Entry<long[], Long>> replacements = new IdentityHashMap<>();
249252

250-
final Class<?> edgesClass;
251-
final Class<?> fieldsClass;
252253
final Field fieldsOffsetsField;
253254
final Field edgesIterationMaskField;
254-
final Method recomputeOffsetsAndIterationMaskMethod;
255255

256256
FieldOffsetsTransformer() {
257-
edgesClass = Edges.class;
258-
fieldsClass = Fields.class;
259-
fieldsOffsetsField = LibGraalUtil.lookupField(fieldsClass, "offsets");
260-
edgesIterationMaskField = LibGraalUtil.lookupField(edgesClass, "iterationMask");
261-
recomputeOffsetsAndIterationMaskMethod = LibGraalUtil.lookupMethod(fieldsClass, "recomputeOffsetsAndIterationMask", BeforeCompilationAccess.class);
257+
fieldsOffsetsField = lookupField(Fields.class, "offsets");
258+
edgesIterationMaskField = lookupField(Edges.class, "iterationMask");
262259
}
263260

264261
void register(BeforeAnalysisAccess access) {
@@ -286,38 +283,28 @@ private Map.Entry<long[], Long> getReplacement(Object receiver) {
286283
}
287284
}
288285

289-
@SuppressWarnings("unchecked")
290286
private Map.Entry<long[], Long> computeReplacement(Object receiver) {
291-
try {
292-
return (Map.Entry<long[], Long>) recomputeOffsetsAndIterationMaskMethod.invoke(receiver, beforeCompilationAccess);
293-
} catch (Throwable e) {
294-
throw GraalError.shouldNotReachHere(e);
295-
}
287+
Fields fields = (Fields) receiver;
288+
return fields.recomputeOffsetsAndIterationMask(beforeCompilationAccess);
296289
}
297290
}
298291

299292
/**
300293
* Transforms {@code GlobalAtomicLong.addressSupplier} by replacing it with a {@link GlobalData}
301294
* backed address supplier.
302295
*/
303-
class GlobalAtomicLongTransformer implements FieldValueTransformer {
304-
private MethodHandle globalAtomicLongGetInitialValue;
296+
static class GlobalAtomicLongTransformer implements FieldValueTransformer {
305297

306298
void register(BeforeAnalysisAccess access) {
307-
Field addressSupplierField = LibGraalUtil.lookupField(GlobalAtomicLong.class, "addressSupplier");
299+
Field addressSupplierField = lookupField(GlobalAtomicLong.class, "addressSupplier");
308300
access.registerFieldValueTransformer(addressSupplierField, this);
309-
try {
310-
globalAtomicLongGetInitialValue = mhl.findVirtual(GlobalAtomicLong.class, "getInitialValue", methodType(long.class));
311-
} catch (Throwable e) {
312-
GraalError.shouldNotReachHere(e);
313-
}
314301
}
315302

316303
@Override
317304
public Object transform(Object receiver, Object originalValue) {
318305
long initialValue;
319306
try {
320-
initialValue = (long) globalAtomicLongGetInitialValue.invoke(receiver);
307+
initialValue = ((GlobalAtomicLong) receiver).getInitialValue();
321308
} catch (Throwable e) {
322309
throw GraalError.shouldNotReachHere(e);
323310
}
@@ -334,28 +321,29 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
334321

335322
/* Contains static fields that depend on HotSpotJVMCIRuntime */
336323
RuntimeClassInitialization.initializeAtRunTime(HotSpotModifiers.class);
337-
RuntimeClassInitialization.initializeAtRunTime(LibGraalUtil.lookupClass("jdk.vm.ci.hotspot.HotSpotCompiledCodeStream"));
338-
RuntimeClassInitialization.initializeAtRunTime(LibGraalUtil.lookupClass("jdk.vm.ci.hotspot.HotSpotCompiledCodeStream$Tag"));
324+
RuntimeClassInitialization.initializeAtRunTime(lookupClass("jdk.vm.ci.hotspot.HotSpotCompiledCodeStream"));
325+
RuntimeClassInitialization.initializeAtRunTime(lookupClass("jdk.vm.ci.hotspot.HotSpotCompiledCodeStream$Tag"));
339326

340327
/* Needed for runtime calls to BoxingSnippets.Templates.getCacheClass(JavaKind) */
341328
RuntimeReflection.registerAllDeclaredClasses(Character.class);
342-
RuntimeReflection.register(LibGraalUtil.lookupField(LibGraalUtil.lookupClass("java.lang.Character$CharacterCache"), "cache"));
329+
RuntimeReflection.register(lookupField(lookupClass("java.lang.Character$CharacterCache"), "cache"));
343330
RuntimeReflection.registerAllDeclaredClasses(Byte.class);
344-
RuntimeReflection.register(LibGraalUtil.lookupField(LibGraalUtil.lookupClass("java.lang.Byte$ByteCache"), "cache"));
331+
RuntimeReflection.register(lookupField(lookupClass("java.lang.Byte$ByteCache"), "cache"));
345332
RuntimeReflection.registerAllDeclaredClasses(Short.class);
346-
RuntimeReflection.register(LibGraalUtil.lookupField(LibGraalUtil.lookupClass("java.lang.Short$ShortCache"), "cache"));
333+
RuntimeReflection.register(lookupField(lookupClass("java.lang.Short$ShortCache"), "cache"));
347334
RuntimeReflection.registerAllDeclaredClasses(Integer.class);
348-
RuntimeReflection.register(LibGraalUtil.lookupField(LibGraalUtil.lookupClass("java.lang.Integer$IntegerCache"), "cache"));
335+
RuntimeReflection.register(lookupField(lookupClass("java.lang.Integer$IntegerCache"), "cache"));
349336
RuntimeReflection.registerAllDeclaredClasses(Long.class);
350-
RuntimeReflection.register(LibGraalUtil.lookupField(LibGraalUtil.lookupClass("java.lang.Long$LongCache"), "cache"));
337+
RuntimeReflection.register(lookupField(lookupClass("java.lang.Long$LongCache"), "cache"));
351338

352339
doLegacyJVMCIInitialization();
353340

354341
Path libGraalJavaHome = libgraalLoader.getJavaHome();
355342
GetCompilerConfig.Result configResult = GetCompilerConfig.from(libGraalJavaHome);
356343
for (var e : configResult.opens().entrySet()) {
344+
Module module = ModuleLayer.boot().findModule(e.getKey()).orElseThrow();
357345
for (String source : e.getValue()) {
358-
LibGraalUtil.accessPackagesToClass(LibGraalUtil.Access.OPEN, getClass(), false, e.getKey(), source);
346+
Modules.addOpensToAllUnnamed(module, source);
359347
}
360348
}
361349

@@ -385,8 +373,7 @@ private void doLegacyJVMCIInitialization() {
385373
try {
386374
String rawArch = GraalServices.getSavedProperty("os.arch");
387375
String arch = switch (rawArch) {
388-
case "x86_64" -> "AMD64";
389-
case "amd64" -> "AMD64";
376+
case "x86_64", "amd64" -> "AMD64";
390377
case "aarch64" -> "aarch64";
391378
case "riscv64" -> "riscv64";
392379
default -> throw new GraalError("Unknown or unsupported arch: %s", rawArch);
@@ -414,19 +401,6 @@ private void doLegacyJVMCIInitialization() {
414401
}
415402
}
416403

417-
/**
418-
* Determines if the JDK runtime includes JDK-8346781. Without it, initialization of some JVMCI
419-
* static cache fields must be done explicitly by {@link LibGraalFeature}.
420-
*/
421-
static boolean has8346781() {
422-
try {
423-
Services.class.getField("IS_BUILDING_NATIVE_IMAGE");
424-
return false;
425-
} catch (NoSuchFieldException e) {
426-
return true;
427-
}
428-
}
429-
430404
@Override
431405
public void duringAnalysis(DuringAnalysisAccess access) {
432406
for (var c : libGraalFeatureComponents) {

0 commit comments

Comments
 (0)