Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WASM Binary compatibility with wasmtime #32

Closed
Deepansharora27 opened this issue Mar 25, 2022 · 6 comments
Closed

WASM Binary compatibility with wasmtime #32

Deepansharora27 opened this issue Mar 25, 2022 · 6 comments
Labels

Comments

@Deepansharora27
Copy link

Deepansharora27 commented Mar 25, 2022

Hi,
I just came across the JWebAssembly project and I have this question that, after I am done compiling my Java ByteCode to WebAssembly, can the WebAssembly Binary be used with the Wasmtime runtime(which is a runtime for WebAssembly and WASI) ?

@m-doescode
Copy link

Well, from a quick google search, it shows that wasmtime is a standalone wasm interpreter.

Provided it works with the way JWebAssembly compiles stuff and if it doesn't access the DOM, I think it would run just fine.

It's worth experimenting though.

@Horcrux7
Copy link
Member

I does not know any over the Wasmtime runtime and its features. Theoretical this should be possible.

  • If you do not use object allocations (only static Java code), this should be possible.
  • I think Wasmtime does not support JavaScript support. The current version use JavaScript via polyfill to allocate objects. The GC support of WASM is under heavy construction in the spec. You can enable this in JWebAssembly with an flag. Does Wasmtime supports the GC feature?
  • A library must be implemented for WASI. The project JWebAssembly-API is written for the browser or node and required JavaScript support. It has also no file support because this does not exists in browser. It will be nice if any want start such library for WASI.

@Deepansharora27
Copy link
Author

I does not know any over the Wasmtime runtime and its features. Theoretical this should be possible.

* If you do not use object allocations (only static Java code), this should be possible.

* I think Wasmtime does not support JavaScript support. The current version use JavaScript via polyfill to allocate objects. The GC support of WASM is under heavy construction in the spec. You can enable this in JWebAssembly with an flag. Does Wasmtime supports the GC feature?

* A library must be implemented for WASI. The project [JWebAssembly-API](https://github.com/i-net-software/JWebAssembly-API) is written for the browser or node and required JavaScript support. It has also no file support because this does not exists in browser. It will be nice if any want start such library for WASI.

@Horcrux7 As per your last point, it seems that the Current version of JWebAssembly API won't give a binary that is in coherence with the Wasmtime specs ? Am I correct ?

@Horcrux7
Copy link
Member

You need to differ between the compiler and some libraries. The output of the compiler will be compatible with any WASM runtime that support the GC feature. Currently you need to set the flag "wasm.use_gc" to true that the compiler use the GC instead pf the JavaScript polyfill.

The GC feature is currently in phase 2 of the spec (I think) and the last changes are not reflected in JWebAssembly. Does the Wasmtime supports the GC feature? On a quick search I have not found any over supported WASM features of Wasmtime. Also the exception feature can be helpful. This test suite show the supported WASSM features of a browser.

If Wasmtime does not support the GC feature then there is no chance.

Another thing are libraries if you want access to native API of the runtime. For example as an replacement for native Java API. This native API of the library can declare with a library. The project JWebAssembly API declare such native API only for the browser. For support different targets like browser and Wasmtime there is a difference between compiler and the needed libraries.

If you want do some first test then you can write a method that allocate an object and return some int value and compile it wuth use_gc. In Gradle you can do this with:

properties[ 'wasm.use_gc' ] = 'true'

@Horcrux7
Copy link
Member

I try a very small Java sample:

@Export
public static int test() {
    new Object();
    return 42;
}

It compile to (wat syntax)

(module
  (type $t0 (func))
  (type $t1 (func(result i32)))
  (type $t2 (func(param (ref null $java/lang/Object))))
  (start $.<start>)
  (global $java/lang/Object.<class_isInit> (mut i32)i32.const 0)
  (type $java/lang/Object (struct
    (field $java/lang/Object..vtable (mut i32))
    (field $java/lang/Object..hashcode (mut i32))
  ))
  (export "test" (func $debug/TestDebug$TestClass.test))
  (func $.<start>
    call $java/lang/Object.<clinit>
  )
  (func $java/lang/Object.registerNatives
    return
  )
  (func $debug/TestDebug$TestClass.test
        (result i32)
        (local (ref null $java/lang/Object))
    rtt.canon $java/lang/Object
    struct.new_default_with_rtt $java/lang/Object
    local.tee 0
    local.get 0
    i32.const 252
    struct.set $java/lang/Object 0 ;; $.vtable
    call $java/lang/Object.<init>
    i32.const 42
    return
  )
  (func $java/lang/Object.<init>
        (param $this (ref null $java/lang/Object))
    return
  )
  (func $java/lang/Object.<clinit>
    global.get $java/lang/Object.<class_isInit>
    br_if 0
    i32.const 1
    global.set $java/lang/Object.<class_isInit>
    call $java/lang/Object.registerNatives
    return
  )
  (table $functions 0 funcref)
  (table $strings 10 externref)
  (table $classes 10 externref)
  (memory 1)
  (data (i32.const 0) "\10\00\00\00\14\00\00\00\00\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\00\00\00\00\10\00\00\00\14\00\00\00\01\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\01\00\00\00\10\00\00\00\14\00\00\00\02\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\02\00\00\00\10\00\00\00\14\00\00\00\03\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\03\00\00\00\10\00\00\00\14\00\00\00\04\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\04\00\00\00\10\00\00\00\14\00\00\00\05\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\05\00\00\00\10\00\00\00\14\00\00\00\06\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\06\00\00\00\10\00\00\00\14\00\00\00\07\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\07\00\00\00\10\00\00\00\14\00\00\00\08\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\08\00\00\00\10\00\00\00\14\00\00\00\09\00\00\00\ff\ff\ff\ff\00\00\00\00\01\00\00\00\09\00\00\00\00\00\00\00\1c\00\00\008\00\00\00T\00\00\00p\00\00\00\8c\00\00\00\a8\00\00\00\c4\00\00\00\e0\00\00\00\fc\00\00\00h\01\00\00p\01\00\00u\01\00\00z\01\00\00\81\01\00\00\87\01\00\00\8b\01\00\00\90\01\00\00\96\01\00\00\9b\01\00\00\07boolean\04byte\04char\06double\05float\03int\04long\05short\04void\10java.lang.Object")
)

Running with wasmtime results in the error:

Error: failed to run main module `test.wasm`

Caused by:
    0: failed to parse WebAssembly module
    1: Invalid input WebAssembly code at offset 20: Invalid type

On offset 20 there is the type definition of (type $java/lang/Object (struct .... This is a base instruction of the GC spec. This means Wasmtime does not support the GC feature. This means you can currently only use static Java code without any Object allocation.

@Deepansharora27
Copy link
Author

@Horcrux7 Thanks a lot for all your help and detailed feedback !!

Yes, I also did some research and Wasmtime runtime environment does not support the GC Feature as of now. But I guess the good news is that we can make it work with static Java Code without using any Object Allocation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants