WasmEdge / wasmedge-rust-sdk

Embed WasmEdge functions in a Rust host app
Apache License 2.0
30 stars 15 forks source link

`VmBuilder#with_plugin()` doesn't work as expected #80

Closed moznion closed 1 year ago

moznion commented 1 year ago

I installed WasmEdge according to the following,

$ bash install.sh --plugins wasi_crypto

And I wrote a code to execute the wasm file by using wasmedge_sdk like the below:

use wasmedge_sdk::{
    config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions},
    params, VmBuilder,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wasm_app_file = std::env::args().nth(1).expect("Please specify a wasm file");

    let config = ConfigBuilder::new(CommonConfigOptions::default())
        .with_host_registration_config(HostRegistrationConfigOptions::default().wasi(true))
        .build()?;
    assert!(config.wasi_enabled());

    let mut vm = VmBuilder::new().with_config(config)
        .with_plugin("wasmedge_rustls", "rustls_client")
        .build()?;

    vm.wasi_module_mut()
        .expect("Not found wasi module")
        .initialize(None, None, None);

    vm.register_module_from_file("wasm-app", &wasm_app_file)?
        .run_func(Some("wasm-app"), "_start", params!())?;

    Ok(())
}

then executed this program, it raises Error: Plugin(NotFound("wasmedge_rustls")) runtime error.

As far as I checked, the plugin file is located the legit place:

$ pwd
/home/moznion/.wasmedge/plugin
$ ls -l
total 13508
drwxrwxr-x 2 moznion      73 Oct 20 17:08 ./
drwxrwxr-x 6 moznion      68 Oct 20 17:00 ../
-rwxr-xr-x 1 moznion 6720776 Sep  5 06:36 libwasmedgePluginWasiCrypto.so*
-rwxr-xr-x 1 moznion 7109984 Sep  5 06:48 libwasmedge_rustls.so*

And I also checked the behavior with VmBuilder#with_plugin_wasi_crypto() instead of #with_plugin(), the result was the same: Error: Plugin(NotFound("wasi_crypto")).

Is there any workaround?

FYI: I tested the same wasm file with wasmedge command line, that worked fine.


Environment

apepkuss commented 1 year ago

@moznion Sorry for late reply.

We reviewed your example and found some incorrect places. The revised version is

use wasmedge_sdk::{
    config::{CommonConfigOptions, ConfigBuilder, HostRegistrationConfigOptions},
    params,
    plugin::PluginManager,
    VmBuilder,
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // load plugins from the default plugin path: $HOME/.wasmedge/plugin
    PluginManager::load(None)?;

    let wasm_app_file = std::env::args().nth(1).expect("Please specify a wasm file");

    let config = ConfigBuilder::new(CommonConfigOptions::default())
        .with_host_registration_config(HostRegistrationConfigOptions::default().wasi(true))
        .build()?;
    assert!(config.wasi_enabled());

    let mut vm = VmBuilder::new()
        .with_config(config)
        .with_plugin_wasi_crypto()
        .with_plugin("rustls", "rustls_client")
        .build()?;

    vm.wasi_module_mut()
        .expect("Not found wasi module")
        .initialize(None, None, None);

    vm.register_module_from_file("wasm-app", &wasm_app_file)?
        .run_func(Some("wasm-app"), "_start", params!())?;

    Ok(())
}

The contents of $HOME/.wasmedge/:

ubuntu@ip-172-31-31-132:~/.wasmedge$ pwd
/home/ubuntu/.wasmedge
ubuntu@ip-172-31-31-132:~/.wasmedge$ tree .
.
├── bin
│   ├── wasmedge
│   └── wasmedgec
├── env
├── include
│   └── wasmedge
│       ├── enum.inc
│       ├── enum_configure.h
│       ├── enum_errcode.h
│       ├── enum_types.h
│       ├── int128.h
│       ├── version.h
│       └── wasmedge.h
├── lib
│   ├── libwasmedge.so -> libwasmedge.so.0
│   ├── libwasmedge.so.0 -> libwasmedge.so.0.0.3
│   └── libwasmedge.so.0.0.3
└── plugin
    ├── libwasmedgePluginWasiCrypto.so
    ├── libwasmedgePluginWasiNN.so
    └── libwasmedge_rustls.so

5 directories, 16 files
moznion commented 1 year ago

@apepkuss Thank you for your reply. I tweaked my code by adding PluginManager::load(None)?; and replacing wasmedge_rustls by rustls, the it worked as expected.

I appreciate your precise advice.