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

Rust and Wasm problems when trying to access a file #81703

Closed
AndreaEsposit opened this issue Feb 3, 2021 · 1 comment
Closed

Rust and Wasm problems when trying to access a file #81703

AndreaEsposit opened this issue Feb 3, 2021 · 1 comment

Comments

@AndreaEsposit
Copy link

I have been trying to create/read/edit a file though a WebAssembly module originally written in Rust, but it is not working.

The code looks like this(src/lib.rs):

#[no_mangle]
pub fn write() -> Result<(), io::Error> {
    let mut file = File::create("./foo.txt")?;
    file.write_all(b"Hello, world!")?;
    Ok(())
}
#[no_mangle]
pub extern "C" fn hello() -> (){
    let err = write();
    match err {
        Ok(()) => print!("Managed"),
        Err(_e) => print!("Didn't manage");
    }
}

This is my cargo.toml:

[package]
name = "rust_write"
version = "0.1.0"
edition ="2018"

[lib]
crate-type = ["cdylib"]

I am using rustc 1.51.0-nightly, and I compiled the file with this command cargo build --target wasm32-wasi.
The Wa. module will not work either with wasmtime CLI ( wasmtime --dir=. --invoke hello rust_write.wasm ) or embedded, however if I remove the crate-type = ["cdylib"] from cargo.toml and write the exact same hello function as a main function in ````src/main.rs```, then it works without problems.

Could it be that the issue is that _start is not called when you directly --invoke the hello function.

Am I intended to not be able to access files if I am using a WASI library, or is it a bug?
Am I just straight up doing something wrong when I compile?

@AndreaEsposit AndreaEsposit changed the title Rust and Wasm problems with file access. Rust and Wasm problems when trying to access a file Feb 3, 2021
@AndreaEsposit
Copy link
Author

AndreaEsposit commented Feb 3, 2021

Found out a way to do it here: #79997 (comment)

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

1 participant