bytecodealliance / jco

JavaScript toolchain for working with WebAssembly Components
https://bytecodealliance.github.io/jco/
Apache License 2.0
642 stars 65 forks source link

Wasm modules with shared memories are rejected even if they do not spawn threads #470

Closed whitequark closed 4 months ago

whitequark commented 4 months ago

To reproduce:

$ cat test.c
#include <stdio.h>

int main() {
  puts("hello");
}
$ .../wasi-sdk-22.0/bin/clang -target wasm32-wasip1-threads -pthread test.c -o test.wasm
$ jco new test.wasm --wasi-command -o test.component.wasm
(jco componentNew) ComponentError: failed to encode a component from module
$failed to validate component output

Caused by:
    type mismatch for export `memory` of module instantiation argument `env`
    mismatch in the shared flag for memories (at offset 0x7fd8)
    at componentNew (file:///home/whitequark/.npm-packages/lib/node_modules/@bytecodealliance/jco/obj/wasm-tools.js:2462:11)
    at componentNew (file:///home/whitequark/.npm-packages/lib/node_modules/@bytecodealliance/jco/src/cmd/wasm-tools.js:58:18)
    at async file:///home/whitequark/.npm-packages/lib/node_modules/@bytecodealliance/jco/src/jco.js:134:9

This example is extracted from a larger project (YoWASP Clang). LLVM currently does not compile for the wasm32-wasip1 target (it requires wasm32-wasip1-threads) because it extensively uses atomics even in a single-threaded build. This is difficult to change. However, an LTO build of LLVM/Clang/LLD (with the WebAssembly patchset that's likely going to be accepted in near future) doesn't import thread.spawn and so doesn't need to be treated differently.

Should this restriction be relaxed in jco?

pchickey commented 4 months ago

Thanks for identifying this issue! The responsible code here is the wit-component crate, which is getting invoked by jco as a component binary (!). can you please refile here https://github.com/bytecodealliance/wasm-tools/issues as "wit-component should accept modules with shared memories" and we can work out how we can resolve this.

whitequark commented 4 months ago

@pchickey Done, and closing this in favor of https://github.com/bytecodealliance/wasm-tools/issues/1674.

whitequark commented 4 months ago

which is getting invoked by jco as a component binary (!)

I find this arrangement very clever and I admire it from a technical elegance point of view, but it definitely makes contribution more difficult at times...