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

Error: "Exception: create singleton exec_env failed" on ESP32 and Rust #3997

Open
SeaRoll opened this issue Dec 31, 2024 · 1 comment
Open

Comments

@SeaRoll
Copy link
Contributor

SeaRoll commented Dec 31, 2024

Hi!

I am using ESP32 on Rust via esp-idf-sys and WAMR linking component as such:

[[package.metadata.esp-idf-sys.extra_components]]
component_dirs = ["/esp/wasm-micro-runtime/build-scripts/esp-idf"]
bindings_header = "/esp/wasm-micro-runtime/core/iwasm/include/wasm_export.h"
bindings_module = "wamr"

And the code looking something like this for main.rs

use std::ffi::CStr;

use esp_idf_sys::wamr::{
    wasm_application_execute_main, wasm_runtime_full_init, wasm_runtime_get_exception,
    wasm_runtime_instantiate, wasm_runtime_load, RuntimeInitArgs,
};

fn main() {
    // It is necessary to call this function once. Otherwise some patches to the runtime
    // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
    esp_idf_svc::sys::link_patches();

    // Bind the log crate to the ESP Logging facilities
    esp_idf_svc::log::EspLogger::initialize_default();

    log::info!("Initializing WAMR runtime");

    // RuntimeInitArgs init_args;
    // /* configure memory allocation */
    // memset(&init_args, 0, sizeof(RuntimeInitArgs));
    // init_args.mem_alloc_type = Alloc_With_Allocator;
    // init_args.mem_alloc_option.allocator.malloc_func = (void *)os_malloc;
    // init_args.mem_alloc_option.allocator.realloc_func = (void *)os_realloc;
    // init_args.mem_alloc_option.allocator.free_func = (void *)os_free;

    let mut init_args = RuntimeInitArgs::default();
    init_args.mem_alloc_type = 1;
    init_args.mem_alloc_option.allocator.malloc_func = esp_idf_svc::sys::malloc as _;
    init_args.mem_alloc_option.allocator.realloc_func = esp_idf_svc::sys::realloc as _;
    init_args.mem_alloc_option.allocator.free_func = esp_idf_svc::sys::free as _;

    // Initialize the WAMR runtime
    unsafe {
        wasm_runtime_full_init(&mut init_args);
    }

    let mut wasm_module = include_bytes!("main.wasm").to_vec();
    log::info!("Wasm file bytes loaded! {:?}", wasm_module.len());
    let mut error_buf = [0i8; 128];

    let module_handle = unsafe {
        wasm_runtime_load(
            wasm_module.as_mut_ptr(),
            wasm_module.len() as u32,
            error_buf.as_mut_ptr(),
            error_buf.len() as u32,
        )
    };

    if module_handle.is_null() {
        log::error!("Failed to load Wasm module: {:?}", error_buf);
        return;
    }

    log::info!("Wasm module loaded!");
    // Instantiate the module
    let instance_handle = unsafe {
        wasm_runtime_instantiate(
            module_handle,
            4 * 1024 * 1024, // stack size
            4 * 1024 * 1024, // heap size
            error_buf.as_mut_ptr(),
            error_buf.len() as u32,
        )
    };

    if instance_handle.is_null() {
        log::error!("Failed to instantiate Wasm module: {:?}", error_buf);
        let c_str = unsafe { CStr::from_ptr(error_buf.as_ptr()) };
        log::error!("Error: {:?}", c_str);
        return;
    }

    log::info!("Wasm module loaded and instantiated!");

    // run main
    let result = unsafe {
        wasm_application_execute_main(instance_handle, 0, 0usize as _);
    };
    let exception = unsafe { wasm_runtime_get_exception(instance_handle) };

    if !exception.is_null() {
        log::error!("Exception: {:?}", exception);
        let c_str = unsafe { CStr::from_ptr(exception) };
        log::error!("Error: {:?}", c_str.to_string_lossy().into_owned());
        return;
    }

    log::info!("Result: {:?}", result);
    log::info!("Runtime done running!");
}

I am although getting from my esp32s3 device:

I (595) hello_idf: Wasm module loaded!
I (595) hello_idf: Wasm module loaded and instantiated!
E (595) hello_idf: Exception: 0x3fcbadf4
E (595) hello_idf: Error: "Exception: create singleton exec_env failed"

Any thoughts?

@lum1n0us
Copy link
Collaborator

lum1n0us commented Jan 4, 2025

Just some thoughts.

  • check the return value of wasm_runtime_full_init(&mut init_args)
  • compilation options used when building the runtime library

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

2 participants