32
32
import java .nio .file .NoSuchFileException ;
33
33
import java .nio .file .Path ;
34
34
import java .util .Collection ;
35
+ import java .util .EnumSet ;
36
+ import java .util .HashSet ;
35
37
import java .util .List ;
38
+ import java .util .Set ;
36
39
import java .util .SortedMap ;
37
40
import java .util .SortedSet ;
38
41
import java .util .TreeMap ;
41
44
import java .util .concurrent .ConcurrentMap ;
42
45
import java .util .stream .Stream ;
43
46
44
- import jdk .graal .compiler .debug .DebugContext ;
45
- import jdk .graal .compiler .debug .DebugContext .Activation ;
46
- import jdk .graal .compiler .debug .DebugContext .Scope ;
47
- import jdk .graal .compiler .debug .Indent ;
48
- import jdk .graal .compiler .nodes .ValueNode ;
49
- import jdk .graal .compiler .nodes .graphbuilderconf .GraphBuilderConfiguration .Plugins ;
50
- import jdk .graal .compiler .nodes .graphbuilderconf .GraphBuilderContext ;
51
- import jdk .graal .compiler .nodes .graphbuilderconf .InvocationPlugin .RequiredInvocationPlugin ;
52
- import jdk .graal .compiler .nodes .graphbuilderconf .InvocationPlugins .Registration ;
53
- import jdk .graal .compiler .options .Option ;
54
- import jdk .graal .compiler .phases .util .Providers ;
55
47
import org .graalvm .nativeimage .ImageSingletons ;
56
48
import org .graalvm .nativeimage .Platforms ;
57
49
import org .graalvm .nativeimage .impl .InternalPlatform ;
61
53
import com .oracle .svm .core .SubstrateOptions ;
62
54
import com .oracle .svm .core .feature .AutomaticallyRegisteredFeature ;
63
55
import com .oracle .svm .core .feature .InternalFeature ;
56
+ import com .oracle .svm .core .imagelayer .ImageLayerBuildingSupport ;
64
57
import com .oracle .svm .core .jdk .JNIRegistrationUtil ;
65
58
import com .oracle .svm .core .jdk .NativeLibrarySupport ;
59
+ import com .oracle .svm .core .layeredimagesingleton .ImageSingletonLoader ;
60
+ import com .oracle .svm .core .layeredimagesingleton .ImageSingletonWriter ;
61
+ import com .oracle .svm .core .layeredimagesingleton .LayeredImageSingleton ;
62
+ import com .oracle .svm .core .layeredimagesingleton .LayeredImageSingletonBuilderFlags ;
66
63
import com .oracle .svm .core .option .HostedOptionKey ;
67
64
import com .oracle .svm .core .util .InterruptImageBuilding ;
68
65
import com .oracle .svm .core .util .VMError ;
72
69
import com .oracle .svm .hosted .c .NativeLibraries ;
73
70
import com .oracle .svm .hosted .c .codegen .CCompilerInvoker ;
74
71
import com .oracle .svm .hosted .c .util .FileUtils ;
72
+ import com .oracle .svm .hosted .imagelayer .SVMImageLayerLoader ;
73
+ import com .oracle .svm .hosted .imagelayer .SVMImageLayerSingletonLoader ;
74
+ import com .oracle .svm .hosted .imagelayer .SVMImageLayerWriter ;
75
75
76
+ import jdk .graal .compiler .debug .DebugContext ;
77
+ import jdk .graal .compiler .debug .DebugContext .Activation ;
78
+ import jdk .graal .compiler .debug .DebugContext .Scope ;
79
+ import jdk .graal .compiler .debug .Indent ;
80
+ import jdk .graal .compiler .nodes .ValueNode ;
81
+ import jdk .graal .compiler .nodes .graphbuilderconf .GraphBuilderConfiguration .Plugins ;
82
+ import jdk .graal .compiler .nodes .graphbuilderconf .GraphBuilderContext ;
83
+ import jdk .graal .compiler .nodes .graphbuilderconf .InvocationPlugin .RequiredInvocationPlugin ;
84
+ import jdk .graal .compiler .nodes .graphbuilderconf .InvocationPlugins .Registration ;
85
+ import jdk .graal .compiler .options .Option ;
86
+ import jdk .graal .compiler .phases .util .Providers ;
76
87
import jdk .internal .loader .BootLoader ;
77
88
import jdk .vm .ci .meta .ResolvedJavaMethod ;
78
89
@@ -86,14 +97,24 @@ public static class Options {
86
97
public static final HostedOptionKey <Boolean > CreateJvmShim = new HostedOptionKey <>(false );
87
98
}
88
99
89
- private final ConcurrentMap <String , Boolean > registeredLibraries = new ConcurrentHashMap <>();
90
100
private NativeLibraries nativeLibraries = null ;
101
+ private JNIRegistrationSupportSingleton jniRegistrationSupportSingleton = null ;
91
102
private boolean isSunMSCAPIProviderReachable = false ;
92
103
93
104
public static JNIRegistrationSupport singleton () {
94
105
return ImageSingletons .lookup (JNIRegistrationSupport .class );
95
106
}
96
107
108
+ @ Override
109
+ public void afterRegistration (AfterRegistrationAccess access ) {
110
+ if (ImageLayerBuildingSupport .firstImageBuild ()) {
111
+ jniRegistrationSupportSingleton = new JNIRegistrationSupportSingleton ();
112
+ ImageSingletons .add (JNIRegistrationSupportSingleton .class , jniRegistrationSupportSingleton );
113
+ } else {
114
+ jniRegistrationSupportSingleton = JNIRegistrationSupportSingleton .singleton ();
115
+ }
116
+ }
117
+
97
118
@ Override
98
119
public void beforeAnalysis (BeforeAnalysisAccess access ) {
99
120
nativeLibraries = ((BeforeAnalysisAccessImpl ) access ).getNativeLibraries ();
@@ -105,6 +126,11 @@ public void afterAnalysis(AfterAnalysisAccess access) {
105
126
var optSunMSCAPIClass = optionalClazz (access , "sun.security.mscapi.SunMSCAPI" );
106
127
isSunMSCAPIProviderReachable = optSunMSCAPIClass .isPresent () && access .isReachable (optSunMSCAPIClass .get ());
107
128
}
129
+ if (ImageLayerBuildingSupport .buildingExtensionLayer ()) {
130
+ for (String library : jniRegistrationSupportSingleton .baseLayerRegisteredLibraries ) {
131
+ addLibrary (library );
132
+ }
133
+ }
108
134
}
109
135
110
136
@ Override
@@ -133,19 +159,23 @@ public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Rec
133
159
}
134
160
135
161
void registerLibrary (String libname ) {
136
- if (libname != null && registeredLibraries .putIfAbsent (libname , Boolean .TRUE ) == null ) {
137
- /*
138
- * If a library is in our list of static standard libraries, add the library to the
139
- * linker command.
140
- */
141
- if (NativeLibrarySupport .singleton ().isPreregisteredBuiltinLibrary (libname )) {
142
- nativeLibraries .addStaticJniLibrary (libname );
143
- }
162
+ if (libname != null && jniRegistrationSupportSingleton .registeredLibraries .putIfAbsent (libname , Boolean .TRUE ) == null ) {
163
+ addLibrary (libname );
164
+ }
165
+ }
166
+
167
+ private void addLibrary (String libname ) {
168
+ /*
169
+ * If a library is in our list of static standard libraries, add the library to the linker
170
+ * command.
171
+ */
172
+ if (NativeLibrarySupport .singleton ().isPreregisteredBuiltinLibrary (libname )) {
173
+ nativeLibraries .addStaticJniLibrary (libname );
144
174
}
145
175
}
146
176
147
177
public boolean isRegisteredLibrary (String libname ) {
148
- return registeredLibraries .containsKey (libname );
178
+ return jniRegistrationSupportSingleton . registeredLibraries .containsKey (libname );
149
179
}
150
180
151
181
/** Adds exports that `jvm` shim should re-export. */
@@ -232,7 +262,13 @@ private void copyJDKLibraries(Path jdkLibDir) {
232
262
DebugContext debug = accessImpl .getDebugContext ();
233
263
try (Scope s = debug .scope ("copy" );
234
264
Indent i = debug .logAndIndent ("from: %s" , jdkLibDir )) {
235
- for (String libname : new TreeSet <>(registeredLibraries .keySet ())) {
265
+ for (String libname : new TreeSet <>(jniRegistrationSupportSingleton .registeredLibraries .keySet ())) {
266
+ if (jniRegistrationSupportSingleton .baseLayerRegisteredLibraries .contains (libname )) {
267
+ /* Skip libraries copied in the base layer. */
268
+ debug .log (DebugContext .INFO_LEVEL , "%s: SKIPPED" , libname );
269
+ continue ;
270
+ }
271
+
236
272
String library = System .mapLibraryName (libname );
237
273
238
274
if (NativeLibrarySupport .singleton ().isPreregisteredBuiltinLibrary (libname )) {
@@ -334,4 +370,33 @@ private Path getImageImportLib() {
334
370
assert Files .exists (importLib );
335
371
return importLib ;
336
372
}
373
+
374
+ private static final class JNIRegistrationSupportSingleton implements LayeredImageSingleton {
375
+ private final ConcurrentMap <String , Boolean > registeredLibraries = new ConcurrentHashMap <>();
376
+ private final Set <String > baseLayerRegisteredLibraries = new HashSet <>();
377
+
378
+ public static JNIRegistrationSupportSingleton singleton () {
379
+ return ImageSingletons .lookup (JNIRegistrationSupportSingleton .class );
380
+ }
381
+
382
+ @ Override
383
+ public EnumSet <LayeredImageSingletonBuilderFlags > getImageBuilderFlags () {
384
+ return LayeredImageSingletonBuilderFlags .BUILDTIME_ACCESS_ONLY ;
385
+ }
386
+
387
+ @ Override
388
+ public PersistFlags preparePersist (ImageSingletonWriter writer ) {
389
+ var snapshotWriter = ((SVMImageLayerWriter .ImageSingletonWriterImpl ) writer ).getSnapshotBuilder ();
390
+ SVMImageLayerWriter .initStringList (snapshotWriter ::initRegisteredJNILibraries , registeredLibraries .keySet ().stream ());
391
+ return PersistFlags .CREATE ;
392
+ }
393
+
394
+ @ SuppressWarnings ("unused" )
395
+ public static Object createFromLoader (ImageSingletonLoader loader ) {
396
+ JNIRegistrationSupportSingleton singleton = new JNIRegistrationSupportSingleton ();
397
+ var snapshotReader = ((SVMImageLayerSingletonLoader .ImageSingletonLoaderImpl ) loader ).getSnapshotReader ();
398
+ SVMImageLayerLoader .streamStrings (snapshotReader .getRegisteredJNILibraries ()).forEach (singleton .baseLayerRegisteredLibraries ::add );
399
+ return singleton ;
400
+ }
401
+ }
337
402
}
0 commit comments