-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
imported functions #4
Comments
There is no mechanism yet, but I can write out the steps. We have two main options here. The place where you can plug a raw function address in is the
The other option is to port wasmstandalone to the new SimpleJIT API. The demo is an example of how to use the API. This option would be more work, but simplejit provides more features, such as built in dlsym support and basic linking. In the future, it may offer more features like jit caching and invalidation. If you choose this way, I can write up a more detailed plan, but for now, some high-level considerations are:
If you have any questions or something here isn't clear, please let me know! |
Hello Sunfishcode, Thank you for taking the time to write this out. relocate() question: I'm good up until write_unaligned(reloc_address as *mut i32, <what_goes_here>); It's really not a reloc_delta_i32 anymore right? Thanks! |
Sounds good!
The short answer is, if you arrange for The
where (a) is the beginning of the call, (b) is where the offset field begins, and (c) is the end of the call, which is the point that the offset is relative to. The relocation is requesting a patch at address (b), so we have to add 4 to compute the address of (c) in order to compute the offset from (c) to the target address. That offset is what we write into those |
Thank you for explaining. Is it possible for us to: Also, iirc there were some ideas tossed around involving mmap'ing a 4GB linear array so we wouldn't have to do bounds checks. Wouldn't this ensure that the native code wasm is calling could likely be > i32 offset? |
Yes, in recent versions of cretonne-native, external functions have a Cretonne supports the "4GiB trick" for avoiding bounds checks in linear memory, however linear memory is typically not allocated in the same place as compiled code, so it typically doesn't affect code offsets. |
colocated : glad to see that. I've walked through how colocated is set, and I believe it will always be false for imported functions (Linkage.is_definable() will always be false for an 'Import'). This looks like it means I don't have any work to do :-) Minor nit:
Also, is it true that lib/module/src/module.rs/define_function() is never called for imported functions? I'm guessing that declare_func_in_func() is used for imported fns... Wrt the 4GiB trick, I quite like it :-) |
Yes, that diff looks right. And yes, define_function is not called for imported functions. You declare an import with declare_function with a Linkage of Import. declare_func_in_func is for translating a module-level |
I should also mention that cretonne-module, the code in lib/module isn't currently used by wasmstandalone. cretonne-module is one of the higher-level API layers, and it's what SimpleJIT uses, while wasmstandalone is currently just using the lower-level APIs and doing the "module" bookkeeping itself. |
Ah, good to know - thanks. |
I seem to be stuck on this bug: bytecodealliance/cranelift#328 Maybe there is still some issue with cretonne-wasm ? My fork/branch containing the new relocate() fn: Thanks! |
I'll copy my test code into that branch too, so you can see how I'm setting up the environment, compiling, and executing... |
I'm not aware of any problems in cretonne-wasm that would cause trouble here. In any case, I've now added a patch to master to update wasmstandalone to the latest cretonne version, so you may want to add that patch to your branch, just to avoid any possible issues in the older version. I looked at your wasmstandalone branch, and it looks like you're on the right track. It's not immediately obvious to me why it's not working for you. I'll take another look once you add the test code. |
I've applied your patch to my branch. Thanks for helping with that. I've pushed to github, but you might not even need to look at it because the stack trace may explain enough: thread 'main' panicked at 'this variable is used but its type has not been declared', libcore/option.rs:914:5
I'm guessing I need to call declare_function() first? |
Hey there, were you able to look at my branch updates and try my make? |
This doesn't look like a |
The problem is this code in wasmstandalone. It's looking up the signature for a function by its index, I created a patch here which seems to fix the issue. With this, the testcase here now fails with "Wasm file './tmp/sip_test.wasm' failed to compile with error: "Wasm did not define function: call_test", but that may be a different issue. |
The compile error was my fault. I've changed that so my test code calls test() instead (a real wasm exported function).
The sip.rs code snippet is:
Thinking that maybe the indexed function may be affecting the index I tried this:
And this gives me: I modified sip-test/src/template.rs to not call the imported function 'call_test' and it worked successfully. I changed call_test(i32) -> i32 into call_test() -> i32. Any thoughts why calling the imported function fails? I have my changes checked into github (includes your patch). Thanks! |
A similar issue but unrelated to the immediate problem, the new code in (As an aside, handling function index spaces is error-prone in all wasm codebases I've worked on, not just cretonne. Perhaps we should introduce distinct types for function definition indices vs function indices, so keep the two from getting confused). The next issue is in the relocation patching code. With the newer cretonne version emitting non-colocated calls via movabs + indirect call, the code now needs an 8-byte absolute immediate, rather than being able to assume all calls use 4-byte pc-relative offsets. I implemented this in the following patch: d162f8b While there, I also remembered that cretonne-codegen is now folding the magic "+ 4" offset into the addend now, so we no longer need to do it explicitly in With those three things fixed, your testcase executes successfully, for me :). |
Oh that's such great news!!:-D \o/ |
Ok, it also works for me! :-) Thanks so much for your help with this. Wrt enhancing error-prone function indexes: +1 Fyi my fork should demo this successfully ootb. |
* Add some basic sanity tests for Region This commit adds some basic sanity tests for `overlap` method of `Region`. * Refactor overlaps method of Region struct This commit refactors `Region::overlaps` method. * Add some docs * Assert Region's len is nonzero
Returns the underlying data as a `[]byte` type which is much easier to work with than a raw `unsafe.Pointer`. Closes bytecodealliance#4
* Integrate experimental HTTP into wasmtime. * Reset Cargo.lock * Switch to bail!, plumb options partially. * Implement timeouts. * Remove generated files & wasm, add Makefile * Remove generated code textfile * Update crates/wasi-http/Cargo.toml Co-authored-by: Eduardo de Moura Rodrigues <16357187+eduardomourar@users.noreply.github.com> * Update crates/wasi-http/Cargo.toml Co-authored-by: Eduardo de Moura Rodrigues <16357187+eduardomourar@users.noreply.github.com> * Extract streams from request/response. * Fix read for len < buffer length. * Formatting. * types impl: swap todos for traps * streams_impl: idioms, and swap todos for traps * component impl: idioms, swap all unwraps for traps, swap all todos for traps * http impl: idiom * Remove an unnecessary mut. * Remove an unsupported function. * Switch to the tokio runtime for the HTTP request. * Add a rust example. * Update to latest wit definition * Remove example code. * wip: start writing a http test... * finish writing the outbound request example havent executed it yet * better debug output * wasi-http: some stubs required for rust rewrite of the example * add wasi_http tests to test-programs * CI: run the http tests * Fix some warnings. * bump new deps to latest releases (#3) * Add tests for wasi-http to test-programs (#2) * wip: start writing a http test... * finish writing the outbound request example havent executed it yet * better debug output * wasi-http: some stubs required for rust rewrite of the example * add wasi_http tests to test-programs * CI: run the http tests * bump new deps to latest releases h2 0.3.16 http 0.2.9 mio 0.8.6 openssl 0.10.48 openssl-sys 0.9.83 tokio 1.26.0 --------- Co-authored-by: Brendan Burns <bburns@microsoft.com> * Update crates/test-programs/tests/http_tests/runtime/wasi_http_tests.rs * Update crates/test-programs/tests/http_tests/runtime/wasi_http_tests.rs * Update crates/test-programs/tests/http_tests/runtime/wasi_http_tests.rs * wasi-http: fix cargo.toml file and publish script to work together (#4) unfortunately, the publish script doesn't use a proper toml parser (in order to not have any dependencies), so the whitespace has to be the trivial expected case. then, add wasi-http to the list of crates to publish. * Update crates/test-programs/build.rs * Switch to rustls * Cleanups. * Merge switch to rustls. * Formatting * Remove libssl install * Fix tests. * Rename wasi-http -> wasmtime-wasi-http * prtest:full Conditionalize TLS on riscv64gc. * prtest:full Fix formatting, also disable tls on s390x * prtest:full Add a path parameter to wit-bindgen, remove symlink. * prtest:full Fix tests for places where SSL isn't supported. * Update crates/wasi-http/Cargo.toml --------- Co-authored-by: Eduardo de Moura Rodrigues <16357187+eduardomourar@users.noreply.github.com> Co-authored-by: Pat Hickey <phickey@fastly.com> Co-authored-by: Pat Hickey <pat@moreproductive.org>
…codealliance#4) I missed this the first time around. Thanks to Roman for catching this and providing a test case. Fixes #2. Signed-off-by: Joel Dice <joel.dice@fermyon.com>
How do I configure the wasm environment so imported functions work?
If I use dlopen() and dlsym() to get the address of a function (call_test), how do I inject that function address into the wasm linear memory so when I call wasm functions they work correctly (because those wasm functions call my imported function).
Example: this Rust fn compiles down to wasm
If there is no mechanism, and you have a moment, please consider writing out the steps to implement this and I'll do it.
Thanks!
The text was updated successfully, but these errors were encountered: