Skip to content

Commit

Permalink
Prevent automatic leave of an explicitly entered context during close…
Browse files Browse the repository at this point in the history
… called from a host call.
  • Loading branch information
jchalou committed Nov 1, 2023
1 parent a279f6e commit 0b14c1e
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2135,6 +2135,11 @@ public ThreadScope createThreadScope() {
return null;
}

@Override
public boolean isInCurrentEngineHostCallback(Object engine) {
return false;
}

@Override
public OptionDescriptors createUnionOptionDescriptors(OptionDescriptors... optionDescriptors) {
return OptionDescriptors.createUnion(optionDescriptors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,10 @@ public ThreadScope createThreadScope() {
return getNext().createThreadScope();
}

public boolean isInCurrentEngineHostCallback(Object engine) {
return getNext().isInCurrentEngineHostCallback(engine);
}

public Object newLogHandler(Object logHandlerOrStream) {
return getNext().newLogHandler(logHandlerOrStream);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,9 @@
"methods": [
{ "name": "setTestCacheRoot", "parameterTypes": ["java.nio.file.Path", "boolean"] }
]
},
{
"name": "com.oracle.truffle.api.test.polyglot.AutomaticLeaveTest$ContextCloser",
"allPublicMethods": true
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package com.oracle.truffle.api.test.polyglot;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.HostAccess;
import org.junit.Assert;
import org.junit.Test;

import com.oracle.truffle.api.ContextThreadLocal;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.test.common.AbstractExecutableTestLanguage;

public class AutomaticLeaveTest {

static class ThreadContext {

}

@TruffleLanguage.Registration
static class AutomaticLeaveInHostCallTestLanguage extends AbstractExecutableTestLanguage {

final ContextThreadLocal<ThreadContext> threadContext = locals.createContextThreadLocal((c, t) -> new ThreadContext());

@Override
protected Object execute(RootNode node, Env env, Object[] contextArguments, Object[] frameArguments) throws Exception {
if (contextArguments[0] != null) {
InteropLibrary.getUncached().invokeMember(contextArguments[0], "close");
}
// If the close operation automatically leaves the explicitly entered context, then
// context thread local get fails.
threadContext.get();
return null;
}
}

public static class ContextCloser {
private final Context context;

public ContextCloser(Context context) {
this.context = context;
}

public void close() {
context.close();
}
}

@Test
public void testAutomaticLeave() {
try (Context context = Context.newBuilder().allowHostAccess(HostAccess.ALL).build()) {
context.enter();
AbstractExecutableTestLanguage.evalTestLanguage(context, AutomaticLeaveInHostCallTestLanguage.class, "", new Object[]{null});
Context.getCurrent();
}
AbstractPolyglotTest.assertFails(Context::getCurrent, IllegalStateException.class, (e) -> Assert.assertTrue(e.getMessage().startsWith("No current context is available.")));
}

@Test
public void testNoAutomaticLeaveInHostCall() {
try (Context context = Context.newBuilder().allowHostAccess(HostAccess.ALL).build()) {
context.enter();
AbstractExecutableTestLanguage.evalTestLanguage(context, AutomaticLeaveInHostCallTestLanguage.class, "", new ContextCloser(context));
Context.getCurrent();
}
AbstractPolyglotTest.assertFails(Context::getCurrent, IllegalStateException.class, (e) -> Assert.assertTrue(e.getMessage().startsWith("No current context is available.")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.ThreadLocalAction;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleContext;
Expand Down Expand Up @@ -1741,27 +1740,6 @@ static void printResult(PolyglotLanguageContext languageContext, Object result)
}
}

private static boolean isCurrentEngineHostCallback(PolyglotEngineImpl engine) {
RootNode topMostGuestToHostRootNode = Truffle.getRuntime().iterateFrames((f) -> {
RootNode root = ((RootCallTarget) f.getCallTarget()).getRootNode();
if (EngineAccessor.HOST.isGuestToHostRootNode(root)) {
return root;
}
return null;
});
if (topMostGuestToHostRootNode == null) {
return false;
} else {
PolyglotSharingLayer sharing = (PolyglotSharingLayer) EngineAccessor.NODES.getSharingLayer(topMostGuestToHostRootNode);
PolyglotEngineImpl rootEngine = sharing.engine;
if (rootEngine == engine) {
return true;
} else {
return false;
}
}
}

/**
* Embedder close.
*/
Expand Down Expand Up @@ -2690,7 +2668,7 @@ synchronized void clearExplicitContextStack() {
if (parent == null) {
engine.polyglotHostService.notifyClearExplicitContextStack(this);
}
if (isActive(Thread.currentThread()) && !isCurrentEngineHostCallback(engine)) {
if (isActive(Thread.currentThread()) && !engine.getImpl().getRootImpl().isInCurrentEngineHostCallback(engine)) {
PolyglotThreadInfo threadInfo = getCurrentThreadInfo();
if (!threadInfo.explicitContextStack.isEmpty()) {
PolyglotContextImpl c = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@
import org.graalvm.polyglot.io.ProcessHandler;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleFile;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleOptions;
import com.oracle.truffle.api.impl.DefaultTruffleRuntime;
import com.oracle.truffle.api.impl.DispatchOutputStream;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.strings.TruffleString;
import com.oracle.truffle.polyglot.EngineAccessor.AbstractClassLoaderSupplier;
Expand Down Expand Up @@ -590,6 +592,28 @@ public ThreadScope createThreadScope() {
return null;
}

@Override
public boolean isInCurrentEngineHostCallback(Object engine) {
RootNode topMostGuestToHostRootNode = Truffle.getRuntime().iterateFrames((f) -> {
RootNode root = ((RootCallTarget) f.getCallTarget()).getRootNode();
if (EngineAccessor.HOST.isGuestToHostRootNode(root)) {
return root;
}
return null;
});
if (topMostGuestToHostRootNode == null) {
return false;
} else {
PolyglotSharingLayer sharing = (PolyglotSharingLayer) EngineAccessor.NODES.getSharingLayer(topMostGuestToHostRootNode);
PolyglotEngineImpl rootEngine = sharing.engine;
if (rootEngine == engine) {
return true;
} else {
return false;
}
}
}

@Override
public LogHandler newLogHandler(Object logHandlerOrStream) {
return PolyglotLoggers.asLogHandler(logHandlerOrStream);
Expand Down

0 comments on commit 0b14c1e

Please sign in to comment.