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

Question: Memory export API of an instance #94

Open
khagankhan opened this issue Jan 9, 2025 · 3 comments
Open

Question: Memory export API of an instance #94

khagankhan opened this issue Jan 9, 2025 · 3 comments

Comments

@khagankhan
Copy link

Hi,
is there any API to export memory of an instance using wamr-rust-sdk?
Like wasmtime does: let memory = instance.get_memory(&mut store, "_memory").unwrap();
or WasmEdge: module_instance.get_memory_ref("_memory") I would expect something like :find_export_mem like find_export_func.
thanks a lot

@lum1n0us
Copy link
Collaborator

Currently, there isn't an API like that in the Rust SDK. We are planning to introduce some new APIs.

Could you please tell me about your use case?

@khagankhan
Copy link
Author

Thank you for your response! Here's an example of our use case and the implementation we’ve worked on so far:

Wasm Module Example

We are working with Wasm modules that export functions like _main, _crc_globals, and memory like _memory. A typical module might look like:

(module
  (import "env" "addToCrc" (func $addToCrc (param i32)))
  (memory $mem 1)
  (export "_memory" (memory $mem))
  (func $main
    (export "_main")
    (result i32)
    i32.const 42))

you can see the working wasmtime example:

pub fn run_wasmtime(args: Cli) -> anyhow::Result<u32> {
    let wasm_bytes = fs::read(args.program)?;

    let engine =
        if args.should_optimize {
            Engine::new(Config::new().cranelift_opt_level(OptLevel::SpeedAndSize))?
        } else {
            Engine::new(Config::new().cranelift_opt_level(OptLevel::None))?
        };

    let module = Module::new(&engine, wasm_bytes)?;

    let shared_digest: Arc<Mutex<Option<crc::Digest<u32>>>> = Arc::new(Mutex::new(Some(CRC_ALGORITHM.digest())));

    let mut store = Store::new(
        &engine,
        Env { digest: shared_digest.clone() }
    );

    let add_to_crc_func = Func::wrap(&mut store, |caller: Caller<'_, Env>, val: i32| {
        add_to_crc(caller.data(), val);
    });

    let import_object = [add_to_crc_func.into()];

    let instance = Instance::new(&mut store, &module, &import_object)?;

    let main_func = instance.get_typed_func::<(), i32>(&mut store, "_main")?;
    let crc_globals_func = instance.get_typed_func::<(), ()>(&mut store, "_crc_globals")?;
    let memory = instance.get_memory(&mut store, "_memory").unwrap();

    let main_result: i32 = main_func.call(&mut store, ())?;
    add_to_crc(&Env { digest: shared_digest.clone() }, main_result);

    crc_globals_func.call(&mut store, ())?;

    let mem_ptr: *mut i32 = memory.data_ptr(&store) as *mut i32;
    let mem_size = (memory.data_size(&store) / 4) as isize;

    for address in 0..mem_size {
        let mem_value: i32;
        unsafe {
            mem_value = *mem_ptr.offset(address) as i32;
        }
        add_to_crc(&Env { digest: shared_digest.clone() }, mem_value);
    }

    Ok(get_crc(&Env { digest: shared_digest.clone() }))
}

@khagankhan
Copy link
Author

I guess WAMR has APIs (find_export_func) equivalent to get_typed_func but not equivalent for get_memory here:

    let main_func = instance.get_typed_func::<(), i32>(&mut store, "_main")?;
    let crc_globals_func = instance.get_typed_func::<(), ()>(&mut store, "_crc_globals")?;
    let memory = instance.get_memory(&mut store, "_memory").unwrap();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants