wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

where can i find the impl code of `canonical_abi_realloc` for assemblyscript?

Open xiaosongfu opened this issue 3 years ago • 8 comments

I use this tool to generate the binding for wasmtime, and now i want to compile assemblyscript code to wasm module and run it in wasmtime, i need canonical_abi_realloc code for assemblyscript.

xiaosongfu avatar Jun 16 '22 12:06 xiaosongfu

Based on what you've said, it sounds like you've used a host bindings generator (e.g. wasmtime-py or wasmtime) and are now trying to compile an AssemblyScript module that you can use with them. Is that correct?

For languages with guest bindings support, like C or Rust, you can just run the generator and it will provide everything your module needs. However, wit-bindgen doesn't have AssemblyScript guest bindings support, so you would have to implement the Canonical ABI code yourself in AssemblyScript.

If you want more info on canonical_abi_realloc, check out these links.

esoterra avatar Jun 16 '22 14:06 esoterra

To add to this there is a realloc method implemented in AS's stdlib, however, it has a different signature. AS's allocator will be able to look up the original size and alignment, so you can drop those two when calling the inner function.

willemneal avatar Jun 16 '22 15:06 willemneal

@Kylebrown9 yes, your understanding is right.

xiaosongfu avatar Jun 16 '22 15:06 xiaosongfu

@willemneal i have tried it, this is my code:

export function canonical_abi_realloc(old_ptr: u8, old_len: usize, align: usize, new_len: usize): usize {
    return heap.realloc(old_ptr, new_len);
}

but it does not work, there is something wrong? thank you! and i need your help

xiaosongfu avatar Jun 16 '22 15:06 xiaosongfu

What errors are you getting?

willemneal avatar Jun 16 '22 15:06 willemneal

@willemneal thanks

Error: wasm trap: wasm `unreachable` instruction executed
wasm backtrace:
    0:  0x69b - <unknown>!<wasm function 7>
    1:  0x76e - <unknown>!<wasm function 8>


Stack backtrace:
   0: std::backtrace_rs::backtrace::libunwind::trace
             at /rustc/082e4ca49770ebc9cb0ee616f3726a67471be8cb/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1: std::backtrace_rs::backtrace::trace_unsynchronized
             at /rustc/082e4ca49770ebc9cb0ee616f3726a67471be8cb/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2: std::backtrace::Backtrace::create
             at /rustc/082e4ca49770ebc9cb0ee616f3726a67471be8cb/library/std/src/backtrace.rs:328:13
   3: std::backtrace::Backtrace::capture
             at /rustc/082e4ca49770ebc9cb0ee616f3726a67471be8cb/library/std/src/backtrace.rs:296:9
   4: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from
             at /Users/xiaosongfu/.cargo/registry/src/rsproxy.cn-8f6827c7555bfaf8/anyhow-1.0.57/src/error.rs:533:25
   5: <core::result::Result<T,F> as core::ops::try_trait::FromResidual<core::result::Result<core::convert::Infallible,E>>>::from_residual
             at /rustc/082e4ca49770ebc9cb0ee616f3726a67471be8cb/library/core/src/result.rs:2105:27
   6: run::main
             at ./run/src/main.rs:39:5

xiaosongfu avatar Jun 16 '22 15:06 xiaosongfu

Is there anything else need I provide? @willemneal

xiaosongfu avatar Jun 16 '22 15:06 xiaosongfu

A link to your repo would be the best. Hard to tell exactly where the unreachable is occurring.

willemneal avatar Jun 16 '22 15:06 willemneal

I believe that the original question here was somewhat answered and while there may be more issues remaining it might be best to raise this as a question on Zulip or open a new issue, so closing.

alexcrichton avatar Sep 01 '22 19:09 alexcrichton