From ba961a198754e5835f1e1b98f6ed2fa3d53bba05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lazar=20Mitrovi=C4=87?= Date: Mon, 4 May 2020 19:09:52 +0200 Subject: [PATCH] Patch for `PosixTruffleNFIFeature` folding --- .../nfi/posix/PosixTruffleNFIFeature.java | 17 ++++++++++++----- ...oracle_truffle_nfi_impl_NFIContextLinux.java | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/substratevm/src/com.oracle.svm.truffle.nfi.posix/src/com/oracle/svm/truffle/nfi/posix/PosixTruffleNFIFeature.java b/substratevm/src/com.oracle.svm.truffle.nfi.posix/src/com/oracle/svm/truffle/nfi/posix/PosixTruffleNFIFeature.java index 95e0c0e0afb0..83f66487df09 100644 --- a/substratevm/src/com.oracle.svm.truffle.nfi.posix/src/com/oracle/svm/truffle/nfi/posix/PosixTruffleNFIFeature.java +++ b/substratevm/src/com.oracle.svm.truffle.nfi.posix/src/com/oracle/svm/truffle/nfi/posix/PosixTruffleNFIFeature.java @@ -26,7 +26,7 @@ import static com.oracle.svm.core.posix.headers.Dlfcn.GNUExtensions.LM_ID_NEWLM; -import com.oracle.svm.core.c.libc.LibCBase; +import org.graalvm.compiler.api.replacements.Fold; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.Platforms; @@ -38,6 +38,7 @@ import org.graalvm.word.WordFactory; import com.oracle.svm.core.annotate.AutomaticFeature; +import com.oracle.svm.core.c.libc.LibCBase; import com.oracle.svm.core.jdk.PlatformNativeLibrarySupport; import com.oracle.svm.core.posix.PosixUtils; import com.oracle.svm.core.posix.headers.Dlfcn; @@ -65,15 +66,20 @@ final class PosixTruffleNFISupport extends TruffleNFISupport { private static final int ISOLATED_NAMESPACE_FLAG = 0x10000; private static final int ISOLATED_NAMESPACE_NOT_SUPPORTED_FLAG = 0; - static int isolatedNamespaceFlag; + final int isolatedNamespaceFlag; + + @Fold + public static PosixTruffleNFISupport singleton() { + return (PosixTruffleNFISupport) ImageSingletons.lookup(TruffleNFISupport.class); + } static void initialize() { - isolatedNamespaceFlag = LibCBase.singleton().hasIsolatedNamespaces() ? ISOLATED_NAMESPACE_FLAG : ISOLATED_NAMESPACE_NOT_SUPPORTED_FLAG; ImageSingletons.add(TruffleNFISupport.class, new PosixTruffleNFISupport()); } private PosixTruffleNFISupport() { super(getErrnoGetterFunctionName()); + isolatedNamespaceFlag = LibCBase.singleton().hasIsolatedNamespaces() ? ISOLATED_NAMESPACE_FLAG : ISOLATED_NAMESPACE_NOT_SUPPORTED_FLAG; } private static String getErrnoGetterFunctionName() { @@ -102,7 +108,7 @@ private static PointerBase dlmopen(Lmid_t lmid, String filename, int mode) { * A single linking namespace is created lazily and registered on the NFI context instance. */ private static PointerBase loadLibraryInNamespace(long nativeContext, String name, int mode) { - assert (mode & isolatedNamespaceFlag) == 0; + assert (mode & singleton().isolatedNamespaceFlag) == 0; Target_com_oracle_truffle_nfi_impl_NFIContextLinux context = // KnownIntrinsics.convertUnknownValue(getContext(nativeContext), Target_com_oracle_truffle_nfi_impl_NFIContextLinux.class); @@ -141,7 +147,8 @@ private static PointerBase loadLibraryInNamespace(long nativeContext, String nam @Override protected long loadLibraryImpl(long nativeContext, String name, int flags) { PointerBase handle; - if (Platform.includedIn(Platform.LINUX.class) && (flags & isolatedNamespaceFlag) != 0) { + if (Platform.includedIn(Platform.LINUX.class) && isolatedNamespaceFlag == ISOLATED_NAMESPACE_FLAG // constant folding + && (flags & isolatedNamespaceFlag) != 0) { handle = loadLibraryInNamespace(nativeContext, name, flags & ~isolatedNamespaceFlag); } else { handle = PosixUtils.dlopen(name, flags); diff --git a/substratevm/src/com.oracle.svm.truffle.nfi.posix/src/com/oracle/svm/truffle/nfi/posix/Target_com_oracle_truffle_nfi_impl_NFIContextLinux.java b/substratevm/src/com.oracle.svm.truffle.nfi.posix/src/com/oracle/svm/truffle/nfi/posix/Target_com_oracle_truffle_nfi_impl_NFIContextLinux.java index a026a1a3f802..fea7a3a9e6be 100644 --- a/substratevm/src/com.oracle.svm.truffle.nfi.posix/src/com/oracle/svm/truffle/nfi/posix/Target_com_oracle_truffle_nfi_impl_NFIContextLinux.java +++ b/substratevm/src/com.oracle.svm.truffle.nfi.posix/src/com/oracle/svm/truffle/nfi/posix/Target_com_oracle_truffle_nfi_impl_NFIContextLinux.java @@ -43,7 +43,7 @@ final class Target_com_oracle_truffle_nfi_impl_NFIContextLinux { static class IsolatedAccessor { static int getISOLATED_NAMESPACE(@SuppressWarnings("unused") Target_com_oracle_truffle_nfi_impl_NFIContextLinux ctx) { - return PosixTruffleNFISupport.isolatedNamespaceFlag; + return PosixTruffleNFISupport.singleton().isolatedNamespaceFlag; } } // Checkstyle: resume