Skip to content

Commit

Permalink
[GR-12778][GR-12984] Don't allow polyglot_eval for internal languages.
Browse files Browse the repository at this point in the history
PullRequest: graal/2637
  • Loading branch information
Palez committed Dec 13, 2018
2 parents f65acaa + c477a56 commit c39d151
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions sulong/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changes:

* Stack traces report line numbers even if the original source file can not
be found.
* Don't allow `polyglot_eval` for internal languages.

Deprecations:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.NodeChild;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.LanguageInfo;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.llvm.nodes.intrinsics.interop.LLVMPolyglotEvalNodeGen.GetSourceFileNodeGen;
import com.oracle.truffle.llvm.nodes.intrinsics.interop.LLVMPolyglotEvalNodeGen.GetSourceStringNodeGen;
Expand Down Expand Up @@ -119,6 +120,13 @@ CallTarget doCached(String id, String code,
CallTarget uncached(String id, String code,
@Cached("getContextReference()") ContextReference<LLVMContext> ctxRef) {
Source sourceObject;
Env env = ctxRef.get().getEnv();
LanguageInfo lang = env.getLanguages().get(id);
if (lang == null) {
throw new LLVMPolyglotException(this, "Language '%s' not found.", id);
} else if (lang.isInternal()) {
throw new LLVMPolyglotException(this, "Access to internal language '%s' is not allowed.", id);
}
if (legacyMimeTypeEval) {
String language = Source.findLanguage(id);
sourceObject = Source.newBuilder(language, code, "<eval>").mimeType(id).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.interop.InteropException;
import com.oracle.truffle.llvm.runtime.LLVMBoxedPrimitive;
import com.oracle.truffle.llvm.runtime.except.LLVMPolyglotException;
import com.oracle.truffle.llvm.runtime.nodes.api.LLVMToNativeNodeGen.LLVMObjectToNativeNodeGen;
import com.oracle.truffle.llvm.runtime.pointer.LLVMNativePointer;

Expand Down Expand Up @@ -66,8 +67,8 @@ protected LLVMNativePointer doLLVMBoxedPrimitive(LLVMBoxedPrimitive from) {
return LLVMNativePointer.create((long) from.getValue());
} else {
CompilerDirectives.transferToInterpreter();
throw new IllegalAccessError(String.format("Cannot convert a primitive value (type: %s, value: %s) to an LLVMNativePointer).", String.valueOf(from.getValue().getClass()),
String.valueOf(from.getValue())));
throw new LLVMPolyglotException(this, "Cannot convert a primitive value (type: %s, value: %s) to an LLVMNativePointer).", String.valueOf(from.getValue().getClass()),
String.valueOf(from.getValue()));
}
}

Expand Down Expand Up @@ -102,7 +103,7 @@ protected LLVMNativePointer handlePointer(Object pointer,
return LLVMNativePointer.create(lib.asPointer(pointer));
} catch (InteropException e) {
CompilerDirectives.transferToInterpreter();
throw new IllegalStateException("Cannot convert " + pointer + " to LLVMNativePointer", e);
throw new LLVMPolyglotException(this, "Cannot convert " + pointer + " to LLVMNativePointer");
}
}

Expand All @@ -114,7 +115,7 @@ protected LLVMNativePointer transitionToNative(Object pointer,
return LLVMNativePointer.create(lib.asPointer(n));
} catch (InteropException e) {
CompilerDirectives.transferToInterpreter();
throw new IllegalStateException("Cannot convert " + pointer + " to LLVMNativePointer", e);
throw new LLVMPolyglotException(this, "Cannot convert " + pointer + " to LLVMNativePointer");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,29 @@
*/
package com.oracle.truffle.llvm.test.interop;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;

import java.math.BigInteger;
import java.util.HashMap;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.oracle.truffle.api.CallTarget;
import com.oracle.truffle.api.TruffleOptions;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.llvm.runtime.except.LLVMPolyglotException;
import com.oracle.truffle.llvm.test.interop.values.ArrayObject;
import com.oracle.truffle.llvm.test.interop.values.BoxedTestValue;
import com.oracle.truffle.llvm.test.interop.values.NullValue;
import com.oracle.truffle.llvm.test.interop.values.StructObject;
import com.oracle.truffle.llvm.test.interop.values.TestConstructor;
import com.oracle.truffle.tck.TruffleRunner;
import com.oracle.truffle.tck.TruffleRunner.Inject;
import java.math.BigInteger;
import java.util.HashMap;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import org.junit.Assume;

@RunWith(TruffleRunner.class)
public class PolyglotBuiltinTest extends InteropTestBase {
Expand Down Expand Up @@ -157,4 +160,38 @@ public void testHostInterop(@Inject(TestHostInteropNode.class) CallTarget testHo
Assert.assertTrue("isHostObject", runWithPolyglot.getTruffleTestEnv().isHostObject(ret));
Assert.assertSame("ret", BigInteger.class, runWithPolyglot.getTruffleTestEnv().asHostObject(ret));
}

public static class TestEvalNoLang extends SulongTestNode {

public TestEvalNoLang() {
super(testLibrary, "test_eval_no_lang");
}
}

@Test
public void testHasEvalNoLang(@Inject(TestEvalNoLang.class) CallTarget testEvalNolang) {
try {
testEvalNolang.call();
Assert.fail("Should have thrown an exception.");
} catch (LLVMPolyglotException e) {
Assert.assertEquals("err_eval_no_lang", "Language 'not_impl_lang' not found.", e.getMessage());
}
}

public static class TestEvalNoInternal extends SulongTestNode {

public TestEvalNoInternal() {
super(testLibrary, "test_eval_internal_lang");
}
}

@Test
public void testHasEvalNoInternal(@Inject(TestEvalNoInternal.class) CallTarget testEvalNoInternal) {
try {
testEvalNoInternal.call();
Assert.fail("Should have thrown an exception.");
} catch (LLVMPolyglotException e) {
Assert.assertEquals("err_eval_no_lang", "Access to internal language 'nfi' is not allowed.", e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ bool test_has_member(void *object) {
void *test_host_interop() {
return polyglot_java_type("java.math.BigInteger");
}

void test_eval_no_lang(){
polyglot_eval("not_impl_lang", "dont_care()");
}

void test_eval_internal_lang(){
polyglot_eval("nfi", "dont_care()");
}

0 comments on commit c39d151

Please sign in to comment.