kawamuray / wasmtime-java

Java or JVM-language binding for Wasmtime
Apache License 2.0
127 stars 29 forks source link

[Help Wanted] How to use WasiConfig.PreopenDir? #9

Closed SuperIceCN closed 3 years ago

SuperIceCN commented 3 years ago

I tried to pass a preopendir to wasi, but whatever the paths are, I get the same error:

java.lang.RuntimeException: java.lang.RuntimeException: IO error: 拒绝访问。 (os error 5) Caused by: java.lang.RuntimeException: IO error: 拒绝访问。 (os error 5) at io.github.kawamuray.wasmtime.wasi.Wasi.newWasi(Native Method) ~[WebassemblyBN.jar:?] at io.github.kawamuray.wasmtime.wasi.Wasi.(Wasi.java:18) ~[WebassemblyBN.jar:?] at com.blocklynukkit.loader.scriptloader.scriptengines.WasmScriptEngine.buildWasi(WasmScriptEngine.java:84) ~[BlocklyNukkit.jar:1.7.25] at com.blocklynukkit.loader.scriptloader.scriptengines.WasmScriptEngine.(WasmScriptEngine.java:62) ~[BlocklyNukkit.jar:1.7.25] at com.blocklynukkit.loader.scriptloader.WasmLoader.putWasmEngine(WasmLoader.java:85) ~[BlocklyNukkit.jar:1.7.25] at com.blocklynukkit.loader.scriptloader.WasmLoader.loadplugins(WasmLoader.java:34) ~[BlocklyNukkit.jar:1.7.25] at com.blocklynukkit.loader.Loader.onEnable(Loader.java:250) ~[BlocklyNukkit.jar:1.7.25] ... 4 more

private void buildWasi(){
    wasiConfig = new WasiConfig(new String[]{},new WasiConfig.PreopenDir[]{
            new WasiConfig.PreopenDir("./","/root");
    });
    wasi = new Wasi(store,wasiConfig);
    wasi.addToLinker(linker);
}

What should I do? Is it only happens on Windows? Any help would be appreciated!

kawamuray commented 3 years ago

I guess the error is related to filesystem permission? I could imagine two possible causes:

  1. Your application really doesn't have permission to the target directory.
  2. The filesystem path "./" is not valid on windows platform. As I previously wrote here, java automatically converts unix-style path to platform specific path when dealing with filesystems, but it is not the case for the PreopenDir.hostPath because, 1. it is a type of String and 2. the actual processing of the path (filesystem open) happens at Rust code, so we don't expect implicit path conversion performed here.
kawamuray commented 3 years ago

FYI, here's some example code and blog entry that I've done previously and know it works (on unix platform):

SuperIceCN commented 3 years ago

Thanks a lot! Let me see.

SuperIceCN commented 3 years ago

Solved, close.