-
Notifications
You must be signed in to change notification settings - Fork 646
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
Comments
Just some thoughts.
|
I will take a look into this, thank you! |
Tried it out and got a code like this: use std::ffi::CStr;
use esp_idf_sys::wamr::{
wasm_application_execute_main, wasm_runtime_deinstantiate, wasm_runtime_destroy,
wasm_runtime_full_init, wasm_runtime_get_exception, wasm_runtime_instantiate,
wasm_runtime_load, wasm_runtime_unload, RuntimeInitArgs,
};
fn iwasm_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");
let mut init_args = RuntimeInitArgs::default();
init_args.mem_alloc_type = 1;
// init_args.mem_alloc_option.pool.heap_size = 1024 * 1024 * 8; // 8 MB heap size
init_args.mem_alloc_option.allocator.malloc_func = esp_idf_svc::sys::malloc as *mut _;
init_args.mem_alloc_option.allocator.realloc_func = esp_idf_svc::sys::realloc as *mut _;
init_args.mem_alloc_option.allocator.free_func = esp_idf_svc::sys::free as *mut _;
// Initialize the WAMR runtime
let success: bool = unsafe { wasm_runtime_full_init(&mut init_args) };
if !success {
log::error!("Failed to initialize WAMR runtime");
return;
}
let mut wasm_module = include_bytes!("output.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() {
let c_str = unsafe { CStr::from_ptr(error_buf.as_ptr()) };
log::error!("Failed to load Wasm module: {:?}", c_str);
return;
}
log::info!("Wasm module loaded!");
// Instantiate the module
let instance_handle = unsafe {
wasm_runtime_instantiate(
module_handle,
16 * 1024, // stack size
16 * 1024, // heap size
error_buf.as_mut_ptr(),
error_buf.len() as u32,
)
};
if instance_handle.is_null() {
let c_str = unsafe { CStr::from_ptr(error_buf.as_ptr()) };
log::error!("Failed to instantiate Wasm module: {:?}", c_str);
return;
}
log::info!("Wasm module loaded and instantiated!");
// run main
let argv: *mut *mut i8 = std::ptr::null_mut();
let success = unsafe { wasm_application_execute_main(instance_handle, 0, argv) };
if !success {
log::error!("Failed to execute Wasm module");
let exception = unsafe { wasm_runtime_get_exception(instance_handle) };
if !exception.is_null() {
let c_str = unsafe { CStr::from_ptr(exception) };
log::error!(
"Error was found during execution: {:?}",
c_str.to_string_lossy().into_owned()
);
return;
}
}
std::thread::sleep(std::time::Duration::from_secs(1));
// Clean up
log::info!("Cleaning up runtime");
unsafe { wasm_runtime_deinstantiate(instance_handle) };
log::info!("Deinstantiate done!");
unsafe { wasm_runtime_unload(module_handle) };
log::info!("Unload done!");
unsafe { wasm_runtime_destroy() };
log::info!("Runtime done running!");
}
fn main() {
// Test running a thread
let handle = std::thread::spawn(|| {
iwasm_main();
});
if let Err(e) = handle.join() {
log::error!("Failed to run thread: {:?}", e);
}
} Now the problem is that I get "Failed to instantiate Wasm module: "WASM module instantiate failed: allocate linear memory failed"". I heard its some allocator problem, but at a stop for now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi!
I am using ESP32 on Rust via
esp-idf-sys
and WAMR linking component as such:And the code looking something like this for main.rs
I am although getting from my esp32s3 device:
Any thoughts?
The text was updated successfully, but these errors were encountered: