Stream: git-wasmtime

Topic: wasmtime / Issue #1440 How to make: wasm calls wasm


view this post on Zulip Wasmtime GitHub notifications bot (Mar 31 2020 at 08:32):

aleksmelnikov opened Issue #1440:

Hi,
please advise me how to make:

  1. I created a simple library crate:
$cat a/src/lib.rs

#[no_mangle]
pub extern "C" fn funca() {
   println!("funca");
}

#[no_mangle]
pub extern "C" fn _start() {
}

And compiled it:
cargo build --manifest-path=a/Cargo.toml --target=wasm32-wasi

I can invoke funca:

$wasmtime run --invoke funca a/target/wasm32-wasi/debug/a.wasm
funca
  1. I created a simple binary crate:
$cat d/src/main.rs

use anyhow::Result;
use wasmtime::*;
use wasmtime_wasi::{Wasi, WasiCtx};

fn main() -> Result<()> {
    let store = Store::default();
    let module = Module::from_file(&store, "/at/rust/wasmer/a/target/wasm32-wasi/debug/a.wasm")?;
    let wasi = Wasi::new(&store, WasiCtx::new(std::env::args())?);
    let mut imports = Vec::new();
    for import in module.imports() {
        if import.module() == "wasi_snapshot_preview1" {
            if let Some(export) = wasi.get_export(import.name()) {
                imports.push(Extern::from(export.clone()));
                continue;
            }
        }
        panic!(
            "couldn't find import for `{}::{}`",
            import.module(),
            import.name()
        );
    }

    let instance = Instance::new(&module, &imports)?;

    // 1
/*
    let start = instance
        .get_export("_start")
        .and_then(|e| e.func())
        .unwrap();
    let start = start.get0::<()>()?;
    start()?;
*/
    // 2
    let funca = instance
        .get_export("funca")
        .expect("export named `answer` not found")
        .func()
        .expect("export `answer` was not a function");
    let funca = funca.get0::<()>()?;
    funca()?;

    Ok(())
}

And compiled it:
cargo build --manifest-path=d/Cargo.toml

I ran it and got:

$cargo run --manifest-path=d/Cargo.toml
funca

Good! Now I want change d from binary crate to library crate. So, I want wasm lib calls wasm lib.
The first, I tried to compile my bin crate with --target=wasm32-wasi to get wasm and failed:

$cargo +nightly build --manifest-path=d/Cargo.toml --target=wasm32-wasi

   Compiling wat v1.0.13
   Compiling jobserver v0.1.21
   Compiling generic-array v0.12.3
   Compiling region v2.1.2
   Compiling gimli v0.20.0
error[E0425]: cannot find function `lock` in module `os`
  --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/lock.rs:30:7
   |
30 |   os::lock(
   |       ^^^^ not found in `os`
   |
help: possible candidate is found in another module, you can import it into scope
   |
1  | use lock::lock;
   |

error[E0425]: cannot find function `unlock` in module `os`
  --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/lock.rs:58:7
   |
58 |   os::unlock(
   |       ^^^^^^ not found in `os`
   |
help: possible candidate is found in another module, you can import it into scope
   |
1  | use lock::unlock;
   |

error[E0425]: cannot find function `page_size` in module `os`
  --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/page.rs:16:39
   |
16 |     INIT.call_once(|| PAGE_SIZE = os::page_size());
   |                                       ^^^^^^^^^ not found in `os`

error[E0425]: cannot find function `set_protection` in module `os`
  --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/protect.rs:40:7
   |
40 |   os::set_protection(
   |       ^^^^^^^^^^^^^^ not found in `os`

error[E0425]: cannot find function `get_region` in module `os`
   --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/lib.rs:133:7
    |
133 |   os::get_region(page::floor(address as usize) as *const u8)
    |       ^^^^^^^^^^ not found in `os`

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0425`.
error: could not compile `region`.

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed

So, I think doesn't region support --target=wasm32-wasi? How to create wasm calls wasm?

view this post on Zulip Wasmtime GitHub notifications bot (Mar 31 2020 at 11:11):

bjorn3 commented on Issue #1440:

You can't compile wasmtime for wasm, as wasm doesn't allow any JIT compilation. What do you want to actually do?

view this post on Zulip Wasmtime GitHub notifications bot (Mar 31 2020 at 14:19):

aleksmelnikov commented on Issue #1440:

@bjorn3 , ok. Thank you for the notes about wasmtime. I see my mistake.

view this post on Zulip Wasmtime GitHub notifications bot (Mar 31 2020 at 14:23):

aleksmelnikov closed Issue #1440:

Hi,
please advise me how to make:

  1. I created a simple library crate:
$cat a/src/lib.rs

#[no_mangle]
pub extern "C" fn funca() {
   println!("funca");
}

#[no_mangle]
pub extern "C" fn _start() {
}

And compiled it:
cargo build --manifest-path=a/Cargo.toml --target=wasm32-wasi

I can invoke funca:

$wasmtime run --invoke funca a/target/wasm32-wasi/debug/a.wasm
funca
  1. I created a simple binary crate:
$cat d/src/main.rs

use anyhow::Result;
use wasmtime::*;
use wasmtime_wasi::{Wasi, WasiCtx};

fn main() -> Result<()> {
    let store = Store::default();
    let module = Module::from_file(&store, "/at/rust/wasmer/a/target/wasm32-wasi/debug/a.wasm")?;
    let wasi = Wasi::new(&store, WasiCtx::new(std::env::args())?);
    let mut imports = Vec::new();
    for import in module.imports() {
        if import.module() == "wasi_snapshot_preview1" {
            if let Some(export) = wasi.get_export(import.name()) {
                imports.push(Extern::from(export.clone()));
                continue;
            }
        }
        panic!(
            "couldn't find import for `{}::{}`",
            import.module(),
            import.name()
        );
    }

    let instance = Instance::new(&module, &imports)?;

    // 1
/*
    let start = instance
        .get_export("_start")
        .and_then(|e| e.func())
        .unwrap();
    let start = start.get0::<()>()?;
    start()?;
*/
    // 2
    let funca = instance
        .get_export("funca")
        .expect("export named `answer` not found")
        .func()
        .expect("export `answer` was not a function");
    let funca = funca.get0::<()>()?;
    funca()?;

    Ok(())
}

And compiled it:
cargo build --manifest-path=d/Cargo.toml

I ran it and got:

$cargo run --manifest-path=d/Cargo.toml
funca

Good! Now I want change d from binary crate to library crate. So, I want wasm lib calls wasm lib.
The first, I tried to compile my bin crate with --target=wasm32-wasi to get wasm and failed:

$cargo +nightly build --manifest-path=d/Cargo.toml --target=wasm32-wasi

   Compiling wat v1.0.13
   Compiling jobserver v0.1.21
   Compiling generic-array v0.12.3
   Compiling region v2.1.2
   Compiling gimli v0.20.0
error[E0425]: cannot find function `lock` in module `os`
  --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/lock.rs:30:7
   |
30 |   os::lock(
   |       ^^^^ not found in `os`
   |
help: possible candidate is found in another module, you can import it into scope
   |
1  | use lock::lock;
   |

error[E0425]: cannot find function `unlock` in module `os`
  --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/lock.rs:58:7
   |
58 |   os::unlock(
   |       ^^^^^^ not found in `os`
   |
help: possible candidate is found in another module, you can import it into scope
   |
1  | use lock::unlock;
   |

error[E0425]: cannot find function `page_size` in module `os`
  --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/page.rs:16:39
   |
16 |     INIT.call_once(|| PAGE_SIZE = os::page_size());
   |                                       ^^^^^^^^^ not found in `os`

error[E0425]: cannot find function `set_protection` in module `os`
  --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/protect.rs:40:7
   |
40 |   os::set_protection(
   |       ^^^^^^^^^^^^^^ not found in `os`

error[E0425]: cannot find function `get_region` in module `os`
   --> /opt/rust/cargo/registry/src/github.com-1ecc6299db9ec823/region-2.1.2/src/lib.rs:133:7
    |
133 |   os::get_region(page::floor(address as usize) as *const u8)
    |       ^^^^^^^^^^ not found in `os`

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0425`.
error: could not compile `region`.

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed

So, I think doesn't region support --target=wasm32-wasi? How to create wasm calls wasm?

view this post on Zulip Wasmtime GitHub notifications bot (Mar 31 2020 at 15:29):

yurydelendik commented on Issue #1440:

Per zullip chat, more notes:

-wasmtime-cli does not have a way to configure a linking, (--preload is not a parameter that does that)

view this post on Zulip Wasmtime GitHub notifications bot (Mar 31 2020 at 15:29):

yurydelendik edited a comment on Issue #1440:

Per zullip chat, more notes:

view this post on Zulip Wasmtime GitHub notifications bot (Mar 31 2020 at 15:30):

yurydelendik edited a comment on Issue #1440:

Per zullip chat, more notes:


Last updated: Oct 23 2024 at 20:03 UTC