I'm trying to make http request from a wasm component, in first request there is no issue but if I wanna call component again I'm getting wasm trap: out of bounds memory access error. I'm not sure if this is a bug or I'm doing something wrong.
for reproduce this is my host code:
use wasmtime::component::Val;
use wasmtime_wasi::{ResourceTable, WasiCtx, WasiCtxBuilder, WasiView};
use wasmtime_wasi_http::{WasiHttpCtx, WasiHttpView};
pub fn main() -> wasmtime::Result<()> {
let engine = wasmtime::Engine::default();
let bytes = std::fs::read("./components/js/test/test.wasm")?;
let component = wasmtime::component::Component::new(&engine, bytes)?;
let mut linker = wasmtime::component::Linker::<MyState>::new(&engine);
wasmtime_wasi::add_to_linker_sync(&mut linker)?;
wasmtime_wasi_http::add_only_http_to_linker_sync(&mut linker)?;
let mut builder = WasiCtxBuilder::new();
let mut store = wasmtime::Store::new(
&engine,
MyState {
http: WasiHttpCtx::new(),
ctx: builder.inherit_stdio().build(),
table: ResourceTable::new(),
},
);
let instance = linker.instantiate(&mut store, &component)?;
let func = instance.get_func(&mut store, "testhttp").expect("testhttp export not found");
let mut result = [Val::String("".to_string())];
func.call(&mut store, &[Val::S32(10)], &mut result)?;
func.post_return(&mut store)?;
println!("testHttp result1: {:?}", result[0]);
let mut result2 = [Val::String("".to_string())];
func.call(&mut store, &[Val::S32(9)], &mut result2)?;
func.post_return(&mut store)?;
println!("testHttp result2: {:?}", result[0]);
Ok(())
}
struct MyState {
ctx: WasiCtx,
http: WasiHttpCtx,
table: ResourceTable,
}
impl WasiView for MyState {
fn ctx(&mut self) -> &mut WasiCtx { &mut self.ctx }
fn table(&mut self) -> &mut ResourceTable { &mut self.table }
}
impl WasiHttpView for MyState {
fn ctx(&mut self) -> &mut WasiHttpCtx { &mut self.http }
fn table(&mut self) -> &mut ResourceTable { &mut self.table }
}
this is my component:
export const testhttp = async (x) => {
console.log(x);
try {
const response = await fetch("https://jsonplaceholder.typicode.com/todos/1");
const data = await response.json();
console.log("HTTP Response:", data);
return JSON.stringify(data);
} catch (error) {
console.error("HTTP Request Error:", error);
return error.toString();
}
};
and wit file
package component:testhttp;
world test {
export testhttp: func(x: s32) -> string;
}
I'm compiling js component with jco componentize test.js --wit wit/world.wit --world-name test --out test.wasm
and here is the stdout:
HTTP Response: { userId: 1, id: 1, title: "delectus aut autem", completed: false }
testHttp result1: String("{\"userId\":1,\"id\":1,\"title\":\"delectus aut autem\",\"completed\":false}")
HTTP Response: { userId: 1, id: 1, title: "delectus aut autem", completed: false }
Error: error while executing at wasm backtrace:
0: 0x26facc - <unknown>!<wasm function 5415>
1: 0x35b016 - <unknown>!<wasm function 7064>
2: 0x25a4ae - <unknown>!<wasm function 5286>
3: 0x24d192 - <unknown>!<wasm function 5251>
4: 0x2470e8 - <unknown>!<wasm function 5249>
5: 0x2557ab - <unknown>!<wasm function 5252>
6: 0x256516 - <unknown>!<wasm function 5255>
7: 0x382837 - <unknown>!<wasm function 7394>
8: 0x2c3c91 - <unknown>!<wasm function 6095>
9: 0x3438a3 - <unknown>!<wasm function 6970>
10: 0x2558e5 - <unknown>!<wasm function 5252>
11: 0x256516 - <unknown>!<wasm function 5255>
12: 0x2d707e - <unknown>!<wasm function 6248>
13: 0x313cba - <unknown>!<wasm function 6645>
14: 0x222ec - <unknown>!<wasm function 88>
15: 0x226549 - <unknown>!<wasm function 4979>
16: 0x79cd2c - <unknown>!testhttp
Caused by:
0: memory fault at wasm address 0x224e5af5 in linear memory of size 0x760000
1: wasm trap: out of bounds memory access
I'm trying to make http request from a wasm component, in first request there is no issue but if I wanna call component again I'm getting
wasm trap: out of bounds memory accesserror. I'm not sure if this is a bug or I'm doing something wrong.for reproduce this is my host code:
this is my component:
and wit file
I'm compiling js component with
jco componentize test.js --wit wit/world.wit --world-name test --out test.wasmand here is the stdout: