通过Polyglot API执行Python始终需要完全的系统IO访问权限

时间:2018-08-22 13:03:32

标签: graalvm

我正在尝试通过polyglot API执行一些非常简单的python代码。尝试运行甚至如下的简单测试时:

@Test
public void printSomTextPythonTest() throws ScriptExecutionException, IOException {

    String code = "print('Python will print this to the console!')";    

    String[] supportedLangs = { "js", "python", "R" };

    Context testContext = Context.newBuilder(supportedLangs)
            .allowAllAccess(false)
            .allowHostAccess(false)
            .allowHostClassLoading(false)
            .allowIO(false)
            .allowNativeAccess(false)
            .allowCreateThread(false)               
            .build();

    Source source = Source.newBuilder("python", code, "pyScript").build();              
    Value result = testContext.eval(source);

    testContext.close();
}

或者:

@Test
public void setAVariablePythonTest() throws ScriptExecutionException, IOException {

    String code = "someNumber = 11";

    String[] supportedLangs = { "js", "python", "R" };

    Context testContext = Context.newBuilder(supportedLangs)
            .allowAllAccess(false)
            .allowHostAccess(false)
            .allowHostClassLoading(false)
            .allowIO(false)
            .allowNativeAccess(false)
            .allowCreateThread(false)               
            .build();

    Source source = Source.newBuilder("python", code, "pyScript").build();              
    Value result = testContext.eval(source);

    testContext.close();
}

我收到此错误(下面的堆栈跟踪):

org.graalvm.polyglot.PolyglotException: java.lang.SecurityException: Operation is not allowed for: /code/polyglot-test

如果在构建Context时将allowIO更改为true,则代码可以正常运行并给出预期的结果。我也用更复杂的代码尝试了同样的结果。

为什么要执行Python代码需要IO访问?

用JS编写和执行的等效代码不需要将allowIO设置为true,因此在我看来,这是Python特有的东西。

感谢您的帮助。

更新

我也一直在用R作为来宾语言进行测试。运行以下测试:

@Test
public void helloWorldRTest() throws ScriptExecutionException, IOException {

    String code = "print(\"R will print this to the console!\")";       

    String[] supportedLangs = { "js", "python", "R" };

    Context testContext = Context.newBuilder(supportedLangs)
            .allowAllAccess(false)
            .allowHostAccess(false)
            .allowHostClassLoading(false)
            .allowIO(false)
            .allowNativeAccess(false)
            .allowCreateThread(false)               
            .build();

    Source source = Source.newBuilder("R", code, "rScript").build();                
    Value result = testContext.eval(source);

    testContext.close();
}

我收到以下错误:

FastR意外故障:从/Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/R/lib/libR.dylib加载libR时出错。 如果在NFI后端上运行,您是否提供libtrufflenfi.so的位置作为系统属性'truffle.nfi.library'的值? 当前值为“ null”。 系统上是否存在OpenMP运行时库(libgomp.so)?例如,该库通常是GCC软件包的一部分。 详细信息:主机环境不允许访问本机代码。

但是,通过将“ allowNativeAccess”设置为true,代码可以正常运行而不会出错。

不同的语言是否需要运行不同类型的访问权限?

在我的用例中,我试图尽可能沙盒执行。我希望这些脚本(无论使用哪种语言编写)都只能访问提供给它们的数据。 以来宾语言运行的脚本不应具有对主机系统的任何访问权限。这可以实现吗?

部分堆栈跟踪(如果需要调试,我可以提供完整的堆栈跟踪):

org.graalvm.polyglot.PolyglotException:java.lang.SecurityException:不允许对以下代码进行操作:/ code / polyglot-test     在com.oracle.truffle.api.vm.FileSystems $ DeniedIOFileSystem.forbidden(FileSystems.java:489)     在com.oracle.truffle.api.vm.FileSystems $ DeniedIOFileSystem.checkAccess(FileSystems.java:367)     在com.oracle.truffle.api.TruffleFile.checkAccess(TruffleFile.java:983)     在com.oracle.truffle.api.TruffleFile.exists(TruffleFile.java:102)     在com.oracle.graal.python.builtins.modules.PosixModuleBuiltins $ StatNode.stat(PosixModuleBuiltins.java:404)     在com.oracle.graal.python.builtins.modules.PosixModuleBuiltins $ StatNode.doStat(PosixModuleBuiltins.java:397)     在com.oracle.graal.python.builtins.modules.PosixModuleBuiltinsFactory $ StatNodeFactory $ StatNodeGen.executeAndSpecialize(PosixModuleBuiltinsFactory.java:855)     在com.oracle.graal.python.builtins.modules.PosixModuleBuiltinsFactory $ StatNodeFactory $ StatNodeGen.execute(PosixModuleBuiltinsFactory.java:807)     在com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode $ BuiltinBinaryCallNode.execute(BuiltinFunctionRootNode.java:103)处     在com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode.execute(BuiltinFunctionRootNode.java:229)     stat(未知)     stat(../../../../../Library/Java/JavaVirtualMachines/graalvm-ee-1.0.0-rc4/Contents/Home/jre/languages/python/lib-graalpython/posix。 py:51:2230-2247)     在_path_stat(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap_external.py:82:2759-2772)     在_path_is_mode_type(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap_external.py:88:2901-2916)     在_path_isdir(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap_external.py:103:3245-3278)     在path_hook_for_FileFinder(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap_external.py:1333:50397-50413)     在PathFinder._path_hooks(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap_external.py:1083:40517-40526)     在PathFinder._path_importer_cache(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap_external.py:1107:41279-41299)     在PathFinder._get_spec(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap_external.py:1135:42391-42421)     在PathFinder.find_spec(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap_external.py:1166:43686-43722)     在_find_spec(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap.py:892:28932-28960)     在_find_and_load_unlocked(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap.py:953:31192-31213)     在_find_and_load(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap.py:968:31701-31738)     在_gcd_import(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-python / 3 / importlib / _bootstrap.py:985:32285-32317)     在 import (../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python /lib-python/3/importlib/_bootstrap.py:1066:35366-35382)     在(../../../../../ Library / Java / JavaVirtualMachines / graalvm-ee-1.0.0-rc4 / Contents / Home / jre / languages / python / lib-graalpython / Builtins_patches .py:48:2220-2224)     在org.graalvm.polyglot.Context.eval(Context.java:313)     在bolt.tests.BoltEngineGraalTest.helloWorldPythonTest(BoltEngineGraalTest.java:66)处     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处     在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)     在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)     在org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)     在org.junit.runners.ParentRunner上$ 2.evaluate(ParentRunner.java:268)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)     在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)     在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) 原始内部错误: java.lang.SecurityException:不允许对以下代码进行操作:/ code / polyglot-test     在com.oracle.truffle.api.vm.FileSystems $ DeniedIOFileSystem.forbidden(FileSystems.java:489)     在com.oracle.truffle.api.vm.FileSystems $ DeniedIOFileSystem.checkAccess(FileSystems.java:367) ...

0 个答案:

没有答案