Skip to content

Preopen directories are not available in Wasm Reactors #79199

Closed
@PiotrSikora

Description

@PiotrSikora

It looks that preopen directories are not available in Wasm Reactors (i.e. when compiling crate as cdylib). Presumably, because __wasm_call_ctors and therefore __wasilibc_populate_preopens are not called.

It's also possible that I'm creating Wasm Reactors the wrong way, but I couldn't find any documentation, and this way seems to work fine, other than this particular issue.

cat Cargo.toml

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

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

Wasm Command:

cat src/main.rs

use std::fs;
use std::io::Read;

fn main() {
    let mut file = fs::File::open("/etc/timezone").unwrap();
    let mut contents = Vec::new();
    file.read_to_end(&mut contents).unwrap();
    println!("{}", std::str::from_utf8(&contents).unwrap());
}

rm -rf target/; cargo build --release --target wasm32-wasi --bin preopen
wasm-objdump -x target/wasm32-wasi/release/*.wasm | awk '/Import/{p=1} /Function/{p=0} p' | cut -d"<" -f3-

Import[9]:
- wasi_snapshot_preview1.proc_exit
- wasi_snapshot_preview1.fd_close
- wasi_snapshot_preview1.fd_read
- wasi_snapshot_preview1.fd_write
- wasi_snapshot_preview1.path_open
- wasi_snapshot_preview1.environ_sizes_get
- wasi_snapshot_preview1.environ_get
- wasi_snapshot_preview1.fd_prestat_get
- wasi_snapshot_preview1.fd_prestat_dir_name

wasmtime --dir=/etc target/wasm32-wasi/release/preopen.wasm

Etc/UTC

Wasm Reactor:

cat src/lib.rs

use std::fs;
use std::io::Read;

#[no_mangle]
fn _initialize() {
    let mut file = fs::File::open("/etc/timezone").unwrap();
    let mut contents = Vec::new();
    file.read_to_end(&mut contents).unwrap();
    println!("{}", std::str::from_utf8(&contents).unwrap());
}

rm -rf target/; cargo build --release --target wasm32-wasi --lib
wasm-objdump -x target/wasm32-wasi/release/*.wasm | awk '/Import/{p=1} /Function/{p=0} p' | cut -d"<" -f3-

Import[7]:
- wasi_snapshot_preview1.fd_close
- wasi_snapshot_preview1.fd_read
- wasi_snapshot_preview1.fd_write
- wasi_snapshot_preview1.path_open
- wasi_snapshot_preview1.environ_sizes_get
- wasi_snapshot_preview1.proc_exit
- wasi_snapshot_preview1.environ_get

(notice missing wasi_snapshot_preview1.fd_prestat_get and wasi_snapshot_preview1.fd_prestat_dir_name imports)

wasmtime --dir=/etc target/wasm32-wasi/release/preopen.wasm

thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: Custom { kind: Other, error: "failed to find a pre-opened file descriptor through which \"/etc/timezone\" could be opened" }', src/lib.rs:6:52
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: failed to run main module `target/wasm32-wasi/release/preopen.wasm`

Caused by:
    0: failed to instantiate "target/wasm32-wasi/release/preopen.wasm"
    1: calling the Reactor initialization function
    2: wasm trap: unreachable
       wasm backtrace:
         0: 0x8199 - <unknown>!__rust_start_panic
         1: 0x7fab - <unknown>!rust_panic
         2: 0x7c21 - <unknown>!std::panicking::rust_panic_with_hook::h0b71fe06740b049c
         3: 0x72ee - <unknown>!std::panicking::begin_panic_handler::{{closure}}::h5b60dc2bfcb2c446
         4: 0x722f - <unknown>!std::sys_common::backtrace::__rust_end_short_backtrace::h3244caab767433b0
         5: 0x7a27 - <unknown>!rust_begin_unwind
         6: 0xd1e5 - <unknown>!core::panicking::panic_fmt::ha4cbfe39ae9ea3c9
         7: 0xdd63 - <unknown>!core::option::expect_none_failed::h152d592dba3b4951
         8:  0x573 - <unknown>!_initialize

rustc --version --verbose

rustc 1.50.0-nightly (825637983 2020-11-18)
binary: rustc
commit-hash: 8256379832b5ecb7f71e8c5e2018446482223c12
commit-date: 2020-11-18
host: x86_64-unknown-linux-gnu
release: 1.50.0-nightly

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.O-wasmTarget: WASM (WebAssembly), http://webassembly.org/

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions