Repository navigation
Description
Description
Wasm modules built with mono incorrectly includes wasi imports that aren't used by the module.
This causes tests to fail for https://github.com/bytecodealliance/wit-bindgen.
Related: #96419
@AaronRobinsonMSFT @lewing @yowl @jsturtevant
Reproduction Steps
Build mono example: https://github.com/silesmo/wasm-unmanaged-callers-bug
Expected behavior
Imports for the same code built with native-aot-llvm:
Function { module: "test:numbers/test", field: "get-scalar", import_idx: 0, index: 10, params: [], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-char", import_idx: 0, index: 1, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-float32", import_idx: 0, index: 17, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-float64", import_idx: 0, index: 16, params: [F64], results: [F64] }
Function { module: "test:numbers/test", field: "roundtrip-s16", import_idx: 0, index: 1, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-s32", import_idx: 0, index: 1, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-s64", import_idx: 0, index: 18, params: [I64], results: [I64] }
Function { module: "test:numbers/test", field: "roundtrip-s8", import_idx: 0, index: 1, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-u16", import_idx: 0, index: 1, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-u32", import_idx: 0, index: 1, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-u64", import_idx: 0, index: 18, params: [I64], results: [I64] }
Function { module: "test:numbers/test", field: "roundtrip-u8", import_idx: 0, index: 1, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "set-scalar", import_idx: 0, index: 0, params: [I32], results: [] }
Function { module: "wasi_snapshot_preview1", field: "environ_get", import_idx: 0, index: 3, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "environ_sizes_get", import_idx: 0, index: 3, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "clock_time_get", import_idx: 0, index: 33, params: [I32, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_close", import_idx: 0, index: 1, params: [I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_fdstat_get", import_idx: 0, index: 3, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_fdstat_set_flags", import_idx: 0, index: 3, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_prestat_get", import_idx: 0, index: 3, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_prestat_dir_name", import_idx: 0, index: 2, params: [I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_read", import_idx: 0, index: 6, params: [I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_seek", import_idx: 0, index: 48, params: [I32, I64, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_write", import_idx: 0, index: 6, params: [I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_filestat_get", import_idx: 0, index: 23, params: [I32, I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_open", import_idx: 0, index: 60, params: [I32, I32, I32, I32, I32, I64, I64, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_unlink_file", import_idx: 0, index: 2, params: [I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "poll_oneoff", import_idx: 0, index: 6, params: [I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "proc_exit", import_idx: 0, index: 0, params: [I32], results: [] }
Function { module: "wasi_snapshot_preview1", field: "sched_yield", import_idx: 0, index: 10, params: [], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "random_get", import_idx: 0, index: 3, params: [I32, I32], results: [I32] }
Actual behavior
Imports built with mono:
Function { module: "wasi_snapshot_preview1", field: "sock_accept", import_idx: 0, index: 7, params: [I32, I32, I32], results: [I32] }
Function { module: "test:numbers/test", field: "get-scalar", import_idx: 0, index: 9, params: [], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-char", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-float32", import_idx: 0, index: 59, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-float64", import_idx: 0, index: 55, params: [F64], results: [F64] }
Function { module: "test:numbers/test", field: "roundtrip-s16", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-s32", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-s64", import_idx: 0, index: 23, params: [I64], results: [I64] }
Function { module: "test:numbers/test", field: "roundtrip-s8", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-u16", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-u32", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "roundtrip-u64", import_idx: 0, index: 23, params: [I64], results: [I64] }
Function { module: "test:numbers/test", field: "roundtrip-u8", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "test:numbers/test", field: "set-scalar", import_idx: 0, index: 1, params: [I32], results: [] }
Function { module: "wasi_snapshot_preview1", field: "args_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "args_sizes_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "environ_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "environ_sizes_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "clock_res_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "clock_time_get", import_idx: 0, index: 26, params: [I32, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_advise", import_idx: 0, index: 66, params: [I32, I64, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_close", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_fdstat_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_fdstat_set_flags", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_filestat_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_filestat_set_size", import_idx: 0, index: 24, params: [I32, I64], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_filestat_set_times", import_idx: 0, index: 66, params: [I32, I64, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_pread", import_idx: 0, index: 71, params: [I32, I32, I32, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_prestat_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_prestat_dir_name", import_idx: 0, index: 7, params: [I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_pwrite", import_idx: 0, index: 71, params: [I32, I32, I32, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_read", import_idx: 0, index: 12, params: [I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_readdir", import_idx: 0, index: 71, params: [I32, I32, I32, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_seek", import_idx: 0, index: 41, params: [I32, I64, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_sync", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_tell", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_write", import_idx: 0, index: 12, params: [I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_create_directory", import_idx: 0, index: 7, params: [I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_filestat_get", import_idx: 0, index: 11, params: [I32, I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_filestat_set_times", import_idx: 0, index: 72, params: [I32, I32, I32, I32, I64, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_link", import_idx: 0, index: 14, params: [I32, I32, I32, I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_open", import_idx: 0, index: 73, params: [I32, I32, I32, I32, I32, I64, I64, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_readlink", import_idx: 0, index: 10, params: [I32, I32, I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_remove_directory", import_idx: 0, index: 7, params: [I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_rename", import_idx: 0, index: 10, params: [I32, I32, I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_symlink", import_idx: 0, index: 11, params: [I32, I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_unlink_file", import_idx: 0, index: 7, params: [I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "poll_oneoff", import_idx: 0, index: 12, params: [I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "proc_exit", import_idx: 0, index: 1, params: [I32], results: [] }
Function { module: "wasi_snapshot_preview1", field: "random_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
Activity
silesmo commentedon Jan 11, 2024
These are all the imports that shouldn't be there:
Function { module: "wasi_snapshot_preview1", field: "sock_accept", import_idx: 0, index: 7, params: [I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "args_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "args_sizes_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "clock_res_get", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_advise", import_idx: 0, index: 66, params: [I32, I64, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_pread", import_idx: 0, index: 71, params: [I32, I32, I32, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_pwrite", import_idx: 0, index: 71, params: [I32, I32, I32, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_sync", import_idx: 0, index: 4, params: [I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "fd_tell", import_idx: 0, index: 5, params: [I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_create_directory", import_idx: 0, index: 7, params: [I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_filestat_set_times", import_idx: 0, index: 72, params: [I32, I32, I32, I32, I64, I64, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_link", import_idx: 0, index: 14, params: [I32, I32, I32, I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_readlink", import_idx: 0, index: 10, params: [I32, I32, I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_remove_directory", import_idx: 0, index: 7, params: [I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_rename", import_idx: 0, index: 10, params: [I32, I32, I32, I32, I32, I32], results: [I32] }
Function { module: "wasi_snapshot_preview1", field: "path_symlink", import_idx: 0, index: 11, params: [I32, I32, I32, I32, I32], results: [I32] }
radical commentedon Jan 11, 2024
cc @pavelsavara @radekdoulik
pavelsavara commentedon Jan 12, 2024
@maraf
lambdageek commentedon Jan 12, 2024
we probably need to run something like
wasm-opt --remove-unused-module-elementsas part of the module build.lewing commentedon Jan 26, 2024
I haven't had time to look into this directly but are you running
dotnet publishor just build. The imports should be linked away if you are running publish. At the moment it isn't really possible to do iltriming during build which is what is required for the import table generator to remove the linkage.The NativeAOT build operates differently.
lewing commentedon Mar 27, 2024
It appears something has broken in the build and the the component selection logic is not working at all so things like sock_accept are coming from the debugger component. With a nightly build I see
For an application that should have none of the optional components
lewing commentedon Mar 27, 2024
@maraf please take a look at the components logic, it was working briefly before the browser/wasm target unification
maraf commentedon Mar 28, 2024
I got away
sock_acceptby linking withdebugger-stub, but all the remaining are still there (even after using the rest of the stubs)I'm still getting
Manually running
wasm-opt --remove-unused-module-elements ...didn't remove any of thoseThese are imports differ native aot
They might be use from https://github.com/search?q=repo%3AWebAssembly%2Fwasi-libc+clock_res_get&type=code, I'll track them down to have clear understanding
pavelsavara commentedon May 21, 2024
I'm thinking that's mono corelib -> using dotnet PAL -> is using libc from SDK -> is using those WASM imports ?
Should ILink trim some of that ? If so, would it also mean that the native symbols should be trimmed ?
Or is that just Mono VM -> libc -> WASM imports ?
Or both.
maraf commentedon Jul 24, 2024
The socket import is gone by proper mono component selection. Moving tracking down where the rest of the imports comes from to vNext.