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));
}
}
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);
}
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);
}
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));
}
}
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();
}
}
}
}
Aggregations