Skip to content

Commit

Permalink
Set context classloader before calling Polyglot to bylass module CL
Browse files Browse the repository at this point in the history
  • Loading branch information
sdedic committed May 7, 2023
1 parent 630d063 commit 5c81e4e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,18 @@ final synchronized Context ctx() {
} else {
b.allowHostAccess(SANDBOX);
}
ctx = b.build();
if (globals != null) {
for (String k : globals.keySet()) {
if (!ALLOW_ALL_ACCESS.equals(k)) {
ctx.getPolyglotBindings().putMember(k, globals.get(k));

executeWithClassLoader(() -> {
ctx = b.build();
if (globals != null) {
for (String k : globals.keySet()) {
if (!ALLOW_ALL_ACCESS.equals(k)) {
ctx.getPolyglotBindings().putMember(k, globals.get(k));
}
}
}
}
return null;
}, org.graalvm.polyglot.Engine.class.getClassLoader());
}
return ctx;
}
Expand Down Expand Up @@ -130,7 +134,10 @@ public void setBindings(Bindings bindings, int scope) {
public Bindings getBindings(int scope) {
assertGlobalScope(scope);
if (bindings == null) {
Map<String,Object> map = ctx().getPolyglotBindings().as(Map.class);
Map<String,Object> map = executeWithClassLoader(
() -> ctx().getPolyglotBindings().as(Map.class),
org.graalvm.polyglot.Engine.class.getClassLoader()
);
bindings = new SimpleBindings(map);
}
return bindings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.PolyglotException;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
Expand Down Expand Up @@ -63,7 +64,9 @@ private static interface ScriptAction {
}

private Value handleException(ScriptAction r) throws ScriptException {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(Engine.class.getClassLoader());
return r.run();
} catch (PolyglotException e) {
if (e.isHostException()) {
Expand All @@ -72,6 +75,8 @@ private Value handleException(ScriptAction r) throws ScriptException {
}
// avoid exposing polyglot stack frames - might be confusing.
throw new ScriptException(e);
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}

Expand Down Expand Up @@ -118,7 +123,8 @@ public Object get(String arg0) {

@Override
public Bindings getBindings(int scope) {
return getContext().getBindings(scope);
return GraalContext.executeWithClassLoader(() ->getContext().getBindings(scope),
Engine.class.getClassLoader());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ public List<ScriptEngineFactory> factories(ScriptEngineManager m) {

private void enumerateLanguages(List<ScriptEngineFactory> arr, Bindings globals) {
final GraalContext ctx = new GraalContext(globals, Lookup.getDefault().lookup(ClassLoader.class));
try (Engine engine = Engine.newBuilder().build()) {
for (Map.Entry<String, Language> entry : engine.getLanguages().entrySet()) {
arr.add(new GraalEngineFactory(ctx, entry.getKey(), entry.getValue()));
GraalContext.executeWithClassLoader(() -> {
Engine.Builder b = Engine.newBuilder();
try (Engine engine = b.build()) {
for (Map.Entry<String, Language> entry : engine.getLanguages().entrySet()) {
arr.add(new GraalEngineFactory(ctx, entry.getKey(), entry.getValue()));
}
}
}
return null;
}, Engine.class.getClassLoader());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public Bindings getBindings(int scope) {
Context c = global.ctx();
synchronized (this) {
if (langBindings == null) {
langBindings = new SimpleBindings(c.getBindings(langId).as(Map.class));
langBindings = new SimpleBindings(
GraalContext.executeWithClassLoader(() -> c.getBindings(langId).as(Map.class),
org.graalvm.polyglot.Engine.class.getClassLoader()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ private ClassLoader createGraalDependentClassLoader() {
added = false;
for (Iterator<? extends ModuleInfo> it = modules.iterator(); it.hasNext(); ) {
ModuleInfo m = it.next();
if (!m.isEnabled()) {
continue;
}
for (Dependency d : m.getDependencies()) {
if (d.getType() == Dependency.TYPE_MODULE) {
if (dependentsOfSDK.keySet().contains(d.getName())) {
Expand Down Expand Up @@ -137,6 +140,15 @@ private ClassLoader createGraalDependentClassLoader() {
private void enumerateLanguages(List<ScriptEngineFactory> arr, Bindings globals) {
ClassLoader langLoader = createGraalDependentClassLoader();
final GraalContext ctx = new GraalContext(globals, langLoader);
String specVersion = System.getProperty("java.specification.version"); //NOI18N
String vmVersion = System.getProperty("java.vm.version"); //NOI18N
if ("1.8".equals(specVersion) && vmVersion.contains("jvmci-")) { //NOI18N
// this is GraalVM 8, whose JVMCI support returns a PolyglotImpl from the system classloader
// incompatible with PolyglotImpl bundled in this module's libraries.
// GraalVM 8 always contains (mandatory) JS implementation, so the JS is loaded by libs.graalsdk.system module.
// No need to offer a bundled GraalVM implementation and in fact, it is not even possible.
return;
}
GraalContext.executeWithClassLoader(() -> {
try (Engine engine = Engine.newBuilder().build()) {
for (Map.Entry<String, Language> entry : engine.getLanguages().entrySet()) {
Expand Down

0 comments on commit 5c81e4e

Please sign in to comment.