Skip to content

Commit a1f5b16

Browse files
committed
"poison" DefaultFileSystemHolder.FS on hosted
1 parent 05a92e9 commit a1f5b16

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/runtimeinit/FileSystemProviderRuntimeInitSupport.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.oracle.svm.core.jdk.JDKInitializedAtRunTime;
3939
import com.oracle.svm.core.util.BasedOnJDKFile;
4040
import com.oracle.svm.core.util.VMError;
41+
import com.oracle.svm.util.ReflectionUtil;
4142

4243
/**
4344
* This file contains substitutions that are required for initializing {@link FileSystemProvider} at
@@ -79,11 +80,19 @@ final class Target_java_io_DefaultFileSystem_RunTime {
7980
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-25+25/src/java.base/unix/classes/java/io/DefaultFileSystem.java#L39-L41")
8081
@BasedOnJDKFile("https://github.com/openjdk/jdk/blob/jdk-25+25/src/java.base/windows/classes/java/io/DefaultFileSystem.java#L39-L41")
8182
class DefaultFileSystemHolder {
82-
static final Target_java_io_FileSystem_RunTime FS;
83+
static final Object FS;
8384
static {
8485
if (SubstrateUtil.HOSTED) {
85-
// unused - layered images might want to initialize during image build
86-
FS = null;
86+
/*
87+
* Should be unused, but layered images might want to initialize it during image build.
88+
* We set it to the real default file system instead of null to guard against
89+
* unintentional usages. In run-time init mode, we don't allow FileSystems in the image
90+
* heap, so we would fail the image build with an exception if it happens, which helps
91+
* to detect problems.
92+
*/
93+
var defaultFileSystem = ReflectionUtil.lookupClass("java.io.DefaultFileSystem");
94+
var getFileSystem = ReflectionUtil.lookupMethod(defaultFileSystem, "getFileSystem");
95+
FS = ReflectionUtil.invokeMethod(getFileSystem, null);
8796
} else {
8897
FS = Target_java_io_DefaultFileSystem_RunTime.getFileSystem();
8998
}
@@ -94,7 +103,7 @@ class DefaultFileSystemAccessor {
94103
@SuppressWarnings("unused")
95104
static Target_java_io_FileSystem_RunTime get() {
96105
VMError.guarantee(DefaultFileSystemHolder.FS != null, "DefaultFileSystemHolder.FS is null");
97-
return DefaultFileSystemHolder.FS;
106+
return SubstrateUtil.cast(DefaultFileSystemHolder.FS, Target_java_io_FileSystem_RunTime.class);
98107
}
99108
}
100109

0 commit comments

Comments
 (0)