Skip to content
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

[wasmtime_wasi_http] wasm trap: out of bounds memory access on second function call #9972

Open
Mr-1311 opened this issue Jan 10, 2025 · 0 comments
Labels
bug Incorrect behavior in the current implementation that needs fixing

Comments

@Mr-1311
Copy link

Mr-1311 commented Jan 10, 2025

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
@Mr-1311 Mr-1311 added the bug Incorrect behavior in the current implementation that needs fixing label Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Incorrect behavior in the current implementation that needs fixing
Projects
None yet
Development

No branches or pull requests

1 participant