kawamuray / wasmtime-java

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

wasmtime exception preventing export invocation #45

Open krisbitney opened 1 year ago

krisbitney commented 1 year ago

Hi,

I am trying to call an exported function. I am getting an opaque wasmtime runtime exception and I can't figure out why. I know the wasm module is correct because we (https://polywrap.io/) have this same invocation working in TypeScript, Rust, and Python. We want to get it working on the JVM as well. The exception is thrown at the actual export.call(...) point, so it seems I have the imports correct and am able to obtain the export reference. But when I call the export function, it fails.

Do you have any advice?

Relevant file in repo (exception thrown at line 36): https://github.com/krisbitney/polywrap-kt/blob/main/src/jvmMain/kotlin/eth.krisbitney.polywrap/wasm/runtime/WasmInstanceJvm.kt

The Wasm module export signature in Rust and AssemblyScript

#[no_mangle]
pub extern "C" fn _wrap_invoke(method_size: u32, args_size: u32, env_size: u32) -> bool
export function _wrap_invoke(method_size: u32, args_size: u32, env_size: u32): bool 

Stacktrace

io.github.kawamuray.wasmtime.WasmtimeException: error while executing at wasm backtrace:
    0: 0xa35a - <unknown>!<wasm function 72>
    at app//io.github.kawamuray.wasmtime.Func.nativeCall(Native Method)
    at app//io.github.kawamuray.wasmtime.Func.call(Func.java:46)
    at app//io.github.kawamuray.wasmtime.Func.call(Func.java:57)
    at app//io.github.kawamuray.wasmtime.WasmFunctions.lambda$func$35(WasmFunctions.java:393)
    at app//eth.krisbitney.polywrap.wasm.runtime.WasmInstanceJvm.invoke-BWLJW6A(WasmInstanceJvm.kt:36)
    at app//eth.krisbitney.polywrap.wasm.WasmWrapper$invoke$2$1.invokeSuspend(WasmWrapper.kt:37)
    at ???(Coroutine boundary.?(?)
    at wasm.WasmWrapperTest$canInvokeWrapper$1.invokeSuspend(WasmWrapperTest.kt:31)
    at kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt$runTestCoroutine$2.invokeSuspend(TestBuilders.kt:212)
Caused by: io.github.kawamuray.wasmtime.WasmtimeException: error while executing at wasm backtrace:
    0: 0xa35a - <unknown>!<wasm function 72>
    at app//io.github.kawamuray.wasmtime.Func.nativeCall(Native Method)
    at app//io.github.kawamuray.wasmtime.Func.call(Func.java:46)
    at app//io.github.kawamuray.wasmtime.Func.call(Func.java:57)
    at app//io.github.kawamuray.wasmtime.WasmFunctions.lambda$func$35(WasmFunctions.java:393)
    at app//eth.krisbitney.polywrap.wasm.runtime.WasmInstanceJvm.invoke-BWLJW6A(WasmInstanceJvm.kt:36)
kawamuray commented 1 year ago

The error "error while executing at wasm backtrace" is originally from the wasmtime runtime https://github.com/bytecodealliance/wasmtime/blob/main/crates/wasmtime/src/trap.rs#L354 so that it indicates that there's a trap generated while executing your wasmtime module. It is difficult to tell why only with the given error but one possibility I can imagine is the compatibility with the older wasmtime runtime that this library is depending on. You may wanna try running your module with the older wasmtime version and see if it suceeds: https://github.com/kawamuray/wasmtime-java/blob/master/wasmtime-jni/Cargo.toml#L14

krisbitney commented 1 year ago

Thanks!!

krisbitney commented 1 year ago

Hey @kawamuray

For Kotlin/Native, I wrote a Wasmtime implementation that wraps the C API: https://github.com/krisbitney/wasmtime-kotlin

I integrated it into my Kotlin multiplatform project. It is working as expected.

Only the JVM target is broken. I am fairly certain the issue is either a nuance/limitation in how Java/JNI works, or there is a bug in the wasmtime-java implementation.

kawamuray commented 1 year ago

You mean when you compile your wasmtime-kotlin for the JVM target and execute your original wasm using it, the same error occurs?

It's still difficult to think this is a problem of JNI so it might be a bug of wasmtime-java and/or your project indeed. Did you used the latest wasmtime dependency in your project? and still getting the same error?

krisbitney commented 1 year ago

You mean when you compile your wasmtime-kotlin for the JVM target and execute your original wasm using it, the same error occurs?

It's still difficult to think this is a problem of JNI so it might be a bug of wasmtime-java and/or your project indeed. Did you used the latest wasmtime dependency in your project? and still getting the same error?

Wasmtime-Kotlin only supports the kotlin/native target for now. I am using mostly common code that is shared between the kotlin/jvm and kotlin/native targets. There could be a bug in my code, of course, but it really doesn't look like it.

krisbitney commented 1 year ago

Maybe you could add the trap message or trap code to the trap error?