From a4d29ddb80ef4bf9a95cad0481ea2f6972e1b0de Mon Sep 17 00:00:00 2001 From: Tomas Zezula Date: Mon, 11 Sep 2023 21:05:47 +0200 Subject: [PATCH 1/2] Fixed incorrect ApiAccess instance when an enterprise truffle runtime is used from the classpath. (cherry picked from commit 0f2b8fce09534b57bec9bd3c5c3c56b972cf6d03) --- .../com/oracle/truffle/api/impl/Accessor.java | 2 ++ .../truffle/polyglot/EngineAccessor.java | 12 +++++++- .../oracle/truffle/polyglot/PolyglotImpl.java | 29 ++++++++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java b/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java index 181c9add1d9e..c6430f57e788 100644 --- a/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java +++ b/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/Accessor.java @@ -755,6 +755,8 @@ public abstract Iterator mergeHostGuestFrames(Object polyglotEngine, S public abstract TruffleFile getInternalResource(Object owner, String resourceId) throws IOException; public abstract Collection getResourceIds(String componentId); + + public abstract void setIsolatePolyglot(AbstractPolyglotImpl instance); } public abstract static class LanguageSupport extends Support { diff --git a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/EngineAccessor.java b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/EngineAccessor.java index 82bd35560852..ecb4d4f4ca63 100644 --- a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/EngineAccessor.java +++ b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/EngineAccessor.java @@ -1988,7 +1988,12 @@ public Object installGuestToHostCodeCache(Object polyglotContextImpl, Object cac @Override public AutoCloseable createPolyglotThreadScope() { - return PolyglotImpl.findInstance().getRootImpl().createThreadScope(); + AbstractPolyglotImpl impl = PolyglotImpl.findIsolatePolyglot(); + if (impl != null) { + return impl.createThreadScope(); + } else { + return null; + } } @Override @@ -2117,6 +2122,11 @@ public Collection getResourceIds(String componentId) { } throw new IllegalArgumentException(componentId); } + + @Override + public void setIsolatePolyglot(AbstractPolyglotImpl instance) { + PolyglotImpl.setIsolatePolyglot(instance); + } } abstract static class AbstractClassLoaderSupplier implements Supplier { diff --git a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java index 35ec6b4e73c6..0b0c344d872a 100644 --- a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java +++ b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java @@ -122,6 +122,8 @@ public final class PolyglotImpl extends AbstractPolyglotImpl { private PolyglotValueDispatch disconnectedBigIntegerHostValue; private volatile Object defaultFileSystemContext; + private static volatile AbstractPolyglotImpl isolatePolyglot; + /** * Internal method do not use. */ @@ -158,6 +160,16 @@ static PolyglotImpl findInstance() { return (PolyglotImpl) polyglot; } + static AbstractPolyglotImpl findIsolatePolyglot() { + return isolatePolyglot; + } + + static void setIsolatePolyglot(AbstractPolyglotImpl instance) { + assert instance != null; + assert isolatePolyglot == null; + isolatePolyglot = instance; + } + PolyglotEngineImpl getPreinitializedEngine() { return preInitializedEngineRef.get(); } @@ -174,13 +186,22 @@ public void initialize() { @Override public Object initializeModuleToUnnamedAccess(Lookup unnamedLookup, Object unnamedAccess, Object unnamedAPIAccess, Object unnamedIOAccess, Object unnamedManagementAccess) { ModuleToUnnamedBridge bridge = ModuleToUnnamedBridge.create(unnamedLookup, unnamedAccess, unnamedAPIAccess, unnamedIOAccess, unnamedManagementAccess); - setConstructors(Objects.requireNonNull(bridge.getAPIAccess())); - setIO(Objects.requireNonNull(bridge.getIOAccess())); - setMonitoring(Objects.requireNonNull(bridge.getManagementAccess())); - initialize(); + AbstractPolyglotImpl impl = getRootImpl(); + initializeModuleToUnnamedBridge(impl, bridge); + while (impl != this) { + impl = impl.getNext(); + initializeModuleToUnnamedBridge(impl, bridge); + } return bridge.getModuleAccess(); } + private static void initializeModuleToUnnamedBridge(AbstractPolyglotImpl impl, ModuleToUnnamedBridge bridge) { + impl.setConstructors(Objects.requireNonNull(bridge.getAPIAccess())); + impl.setIO(Objects.requireNonNull(bridge.getIOAccess())); + impl.setMonitoring(Objects.requireNonNull(bridge.getManagementAccess())); + impl.initialize(); + } + @Override public Object buildLimits(long statementLimit, Predicate statementLimitSourceFilter, Consumer onLimit) { From 732176f9b6957541edcb62dd695e837d8e8f60b2 Mon Sep 17 00:00:00 2001 From: Tomas Zezula Date: Mon, 11 Sep 2023 21:35:43 +0200 Subject: [PATCH 2/2] Fixed review comments. (cherry picked from commit 30e6185740b00cc310d108e54a83dbdbb86e10e8) --- .../src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java | 4 ++++ .../src/com/oracle/truffle/polyglot/PolyglotImpl.java | 5 ++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java b/sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java index 597d5f4dca18..79038185b3b5 100644 --- a/sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java +++ b/sdk/src/org.graalvm.polyglot/src/org/graalvm/polyglot/impl/AbstractPolyglotImpl.java @@ -487,6 +487,10 @@ public final AbstractPolyglotImpl getNext() { return next; } + public final AbstractPolyglotImpl getNextOrNull() { + return next; + } + public final void setIO(IOAccessor ioAccess) { Objects.requireNonNull(ioAccess, "IOAccess must be non null."); this.io = ioAccess; diff --git a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java index 0b0c344d872a..16c61b7e35bf 100644 --- a/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java +++ b/truffle/src/com.oracle.truffle.polyglot/src/com/oracle/truffle/polyglot/PolyglotImpl.java @@ -187,10 +187,9 @@ public void initialize() { public Object initializeModuleToUnnamedAccess(Lookup unnamedLookup, Object unnamedAccess, Object unnamedAPIAccess, Object unnamedIOAccess, Object unnamedManagementAccess) { ModuleToUnnamedBridge bridge = ModuleToUnnamedBridge.create(unnamedLookup, unnamedAccess, unnamedAPIAccess, unnamedIOAccess, unnamedManagementAccess); AbstractPolyglotImpl impl = getRootImpl(); - initializeModuleToUnnamedBridge(impl, bridge); - while (impl != this) { - impl = impl.getNext(); + while (impl != null) { initializeModuleToUnnamedBridge(impl, bridge); + impl = impl.getNextOrNull(); } return bridge.getModuleAccess(); }