Search in sources :

Example 31 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class HostAccessTest method testBuilderCannotChangeMembersAndTargetMappingsOfHostAccess.

/*
     * Test for GR-32346.
     */
@Test
public void testBuilderCannotChangeMembersAndTargetMappingsOfHostAccess() throws Exception {
    // Set up hostAccess
    Builder builder = HostAccess.newBuilder();
    builder.allowAccess(OK.class.getField("value"));
    builder.targetTypeMapping(Value.class, String.class, (v) -> v.isString(), (v) -> "foo");
    HostAccess hostAccess = builder.build();
    // Try to change members or targetMappings through child builder
    Builder childBuilder = HostAccess.newBuilder(hostAccess);
    childBuilder.allowAccess(Ban.class.getField("value"));
    childBuilder.targetTypeMapping(Value.class, Integer.class, null, (v) -> 42);
    // Ensure hostAccess has not been altered by child builder
    try (Context c = Context.newBuilder().allowHostAccess(hostAccess).build()) {
        assertAccess(c);
        assertEquals("foo", c.asValue("a string").as(String.class));
        assertEquals(123, (int) c.asValue(123).as(Integer.class));
    }
}
Also used : Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Builder(org.graalvm.polyglot.HostAccess.Builder) Test(org.junit.Test)

Example 32 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class HostAccessTest method banAccessToReflection.

@Test
public void banAccessToReflection() throws Exception {
    // @formatter:off
    HostAccess config = HostAccess.newBuilder().allowPublicAccess(true).denyAccess(Class.class).denyAccess(Method.class).denyAccess(Field.class).denyAccess(Proxy.class).denyAccess(Object.class, false).build();
    // @formatter:on
    setupEnv(config);
    Value readValue = context.eval("sl", "" + "function readValue(x, y) {\n" + "  return x.equals(y);\n" + "}\n" + "function main() {\n" + "  return readValue;\n" + "}\n");
    MyEquals myEquals = new MyEquals();
    assertTrue("MyEquals.equals method is accessible", readValue.execute(myEquals, myEquals).asBoolean());
    Value res;
    try {
        res = readValue.execute(new Object());
    } catch (PolyglotException ex) {
        return;
    }
    fail("expecting no result: " + res);
}
Also used : Field(java.lang.reflect.Field) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) ProxyObject(org.graalvm.polyglot.proxy.ProxyObject) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 33 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class HostAccessTest method banAccessToLoadClass.

@Test
public void banAccessToLoadClass() throws Exception {
    // @formatter:off
    HostAccess config = HostAccess.newBuilder().allowPublicAccess(true).denyAccess(ClassLoader.class).build();
    // @formatter:on
    setupEnv(config);
    Value loadClass = context.eval("sl", "" + "function loadClass(x) {\n" + "  return x.loadClass(\"java.lang.String\");\n" + "}\n" + "function main() {\n" + "  return loadClass;\n" + "}\n");
    URLClassLoader loader = new URLClassLoader(new URL[0]);
    Value res;
    try {
        res = loadClass.execute(loader);
    } catch (PolyglotException ex) {
        return;
    }
    fail("expecting no result: " + res);
}
Also used : HostAccess(org.graalvm.polyglot.HostAccess) URLClassLoader(java.net.URLClassLoader) Value(org.graalvm.polyglot.Value) URLClassLoader(java.net.URLClassLoader) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 34 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class ExposeToGuestTest method testAdapterClass.

@SuppressWarnings("unchecked")
@Test
public void testAdapterClass() {
    HostAccess access = HostAccess.newBuilder().allowAccessAnnotatedBy(Export.class).allowImplementations(MarkedClass.class).build();
    try (Context c = Context.newBuilder().allowHostAccess(access).build()) {
        c.initialize(ProxyLanguage.ID);
        Value v = c.asValue(new Impl());
        Value f = v.getMember("noArg");
        MarkedClass markedClass = v.as(MarkedClass.class);
        assertEquals("42", markedClass.exported("42"));
        assertSame("adapter class should be cached", markedClass.getClass(), v.as(MarkedClass.class).getClass());
        assertEquals(42, f.as(Function.class).apply(null));
    }
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 35 with HostAccess

use of org.graalvm.polyglot.HostAccess in project graal by oracle.

the class GR30288Test method test.

@Test
public void test() throws Exception {
    Assume.assumeFalse(CompileImmediatelyCheck.isCompileImmediately());
    // it is important that a new host access is created each time for this test to fail
    for (int i = 0; i < 10000; i++) {
        HostAccess access = HostAccess.newBuilder().allowPublicAccess(true).build();
        try (Context context = Context.newBuilder().allowHostAccess(access).build()) {
            /*
                 * We use several classes to hit the problem earlier.
                 */
            for (Object value : TEST_VALUES) {
                Value v = context.asValue(value);
                // materialize all member access
                v.getMemberKeys().size();
            }
        }
    }
}
Also used : Context(org.graalvm.polyglot.Context) HostAccess(org.graalvm.polyglot.HostAccess) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Aggregations

HostAccess (org.graalvm.polyglot.HostAccess)35 Test (org.junit.Test)30 Context (org.graalvm.polyglot.Context)25 Value (org.graalvm.polyglot.Value)25 LanguageContext (com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext)13 PolyglotException (org.graalvm.polyglot.PolyglotException)6 ProxyObject (org.graalvm.polyglot.proxy.ProxyObject)6 Export (org.graalvm.polyglot.HostAccess.Export)4 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)2 ValueAssert.assertValue (com.oracle.truffle.tck.tests.ValueAssert.assertValue)2 Field (java.lang.reflect.Field)2 URLClassLoader (java.net.URLClassLoader)2 List (java.util.List)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Function (java.util.function.Function)2 TypeLiteral (org.graalvm.polyglot.TypeLiteral)2 ProxyArray (org.graalvm.polyglot.proxy.ProxyArray)2 Assert (org.junit.Assert)2 Before (org.junit.Before)2