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

Accessing global arrays in the guest from host, when embedded? #319

Closed
hrydgard opened this issue Apr 2, 2019 · 4 comments
Closed

Accessing global arrays in the guest from host, when embedded? #319

hrydgard opened this issue Apr 2, 2019 · 4 comments
Labels
❓ question I've a question!

Comments

@hrydgard
Copy link

hrydgard commented Apr 2, 2019

Summary

When embedded in a host Rust application, it does not seem to be possible to access the contents of guest global arrays from the host. And it's not even possible to list the exports without linking to wasmer_runtime_core.

Additional details

For example, if I add the following line to the example in https://github.com/wasmerio/wasmer-rust-example :

#[no_mangle]
pub static mut SCRATCH_I : [u8; 1024] = [0; 1024];

And then loop through instance.exports() in main.rs:

    // (at the top of file: ) use wasmer_runtime_core::{export};
    for (name, export) in instance.exports() {
        match export {
            export::Export::Function{ func, ctx, signature } => {println!("  func: {}", name);},
            export::Export::Global( global ) => {println!("  global: {}", name);},
            _ => {},
        }
    }

I do get the following:

  func: hello_string_from_rust
  global: __data_end
  func: hello_wasm
  global: __heap_base
  global: SCRATCH_I

So SCRATCH_I is there.

But how can I access the memory? Global does not seem to have a public way to get the address - if I had that, I could stash it away and then go directly to memory.

Or am I missing something?

@hrydgard hrydgard added the ❓ question I've a question! label Apr 2, 2019
@bjfish
Copy link
Contributor

bjfish commented Apr 2, 2019

@lachlansneff I think we are missing Export from the public runtime API, can you confirm?

@hrydgard I modified your example to inspect the global value:

Export::Global(global) => {
   println!("  global: {}, {:?}", name, global);
}

which resulting in the following:

global: SCRATCH_I, Global { desc: GlobalDescriptor { mutable: false, ty: I32 }, value: I32(1053432) }

I suspect this value 1053432 to be an index into the memory, maybe some space setup for statics like this. So, I setup the array to contain some values to confirm this suspicion:

#[no_mangle]
pub static mut SCRATCH_I : [u8; 5] = [1, 2, 3, 4, 5];

and then print this part of the memory:

let i = 1053432;
let memory = instance.context().memory(0);
for b in memory.view::<u8>()[i as usize..(i + 5) as usize].iter() {
    println!("{:?}", b);
}

Results in:

Cell { value: 1 }
Cell { value: 2 }
Cell { value: 3 }
Cell { value: 4 }
Cell { value: 5 }

So, it appears that the SCRATCH_I global contains an index into the memory to the first value in the array.

@hrydgard
Copy link
Author

hrydgard commented Apr 2, 2019

Ah, thanks! Good that it's there. Could be clearer in the documentation though...

GlobalDescriptor is even entirely missing from the docs due to a missing /// comment line.

Also it could be nice to add this stuff to the official example :) I'm sure other people will want to do similar things.

bors bot added a commit that referenced this issue Apr 3, 2019
320: Add Export and GlobalDescriptor to Runtime API r=syrusakbary a=bjfish

This was mentioned in the issue: #319

It would be nice to be able to iterate exports without requiring `runtime-core`.

Co-authored-by: Brandon Fish <brandon.j.fish@gmail.com>
@bjfish
Copy link
Contributor

bjfish commented Apr 4, 2019

@hrydgard I've added GlobalDescriptor to runtime with documentation and added Export as well in this PR: #320 Yes, we'll be adding more examples over time.

I'll close this issue as I think it is resolved now. Let us know if you have any more questions, thanks.

@bjfish bjfish closed this as completed Apr 4, 2019
@hrydgard
Copy link
Author

hrydgard commented Apr 4, 2019

@bjfish I still believe you're missing documentation that you find the address of global arrays in the Value::I32, which I find somewhat unintuitive.

Apart from that, all good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
❓ question I've a question!
Projects
None yet
Development

No branches or pull requests

2 participants