From 9e36e2c9030f70473c09cfcd4bb9b8be20902872 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Mon, 10 Oct 2022 12:28:10 +0200 Subject: [PATCH 1/9] Added wasm_function_name to get the function name --- lib/c-api/src/wasm_c_api/types/frame.rs | 45 +++++++++++++++++++++++++ lib/wasi-types-generated/wit-bindgen | 1 - lib/wasi-types/wit-bindgen | 1 + 3 files changed, 46 insertions(+), 1 deletion(-) delete mode 160000 lib/wasi-types-generated/wit-bindgen create mode 160000 lib/wasi-types/wit-bindgen diff --git a/lib/c-api/src/wasm_c_api/types/frame.rs b/lib/c-api/src/wasm_c_api/types/frame.rs index d88767d6882..e0343e096ae 100644 --- a/lib/c-api/src/wasm_c_api/types/frame.rs +++ b/lib/c-api/src/wasm_c_api/types/frame.rs @@ -1,6 +1,15 @@ use super::super::instance::wasm_instance_t; +use libc::c_char; +use std::ffi::CString; use wasmer_api::FrameInfo; +#[repr(C)] +#[allow(non_camel_case_types)] +#[derive(Debug, Clone)] +pub struct wasm_function_name { + pub name: *mut c_char, +} + #[allow(non_camel_case_types)] #[derive(Debug, Clone)] pub struct wasm_frame_t { @@ -47,4 +56,40 @@ pub unsafe extern "C" fn wasm_frame_module_offset(frame: &wasm_frame_t) -> usize frame.info.module_offset() } +#[no_mangle] +pub unsafe extern "C" fn wasm_frame_func_name( + frame: &wasm_frame_t, + out: Option<&mut wasm_function_name>, +) -> u32 { + let out = match out { + Some(s) => s, + None => return 1, + }; + + let func_name = frame + .info + .function_name() + .and_then(|f| Some(CString::new(f).ok()?.into_raw())); + + match func_name { + Some(s) => { + out.name = s; + 0 + } + None => { + out.name = std::ptr::null_mut(); + 1 + } + } +} + +#[no_mangle] +pub unsafe extern "C" fn wasm_function_name_delete(name: Option<&mut wasm_function_name>) { + if let Some(s) = name { + if s.name != std::ptr::null_mut() { + let _ = CString::from_raw(s.name); + } + } +} + wasm_declare_boxed_vec!(frame); diff --git a/lib/wasi-types-generated/wit-bindgen b/lib/wasi-types-generated/wit-bindgen deleted file mode 160000 index 095d295be63..00000000000 --- a/lib/wasi-types-generated/wit-bindgen +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 095d295be6392259924e48488af188d3ed3e4102 diff --git a/lib/wasi-types/wit-bindgen b/lib/wasi-types/wit-bindgen new file mode 160000 index 00000000000..44a2bf81489 --- /dev/null +++ b/lib/wasi-types/wit-bindgen @@ -0,0 +1 @@ +Subproject commit 44a2bf8148932590479bd64b9f90502b14c3b0d3 From b71ab2f1a3f45e0e326b08707a283acabffcc9f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Mon, 10 Oct 2022 12:40:17 +0200 Subject: [PATCH 2/9] Fix "make lint" --- lib/c-api/src/wasm_c_api/types/frame.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/c-api/src/wasm_c_api/types/frame.rs b/lib/c-api/src/wasm_c_api/types/frame.rs index e0343e096ae..8e63c782018 100644 --- a/lib/c-api/src/wasm_c_api/types/frame.rs +++ b/lib/c-api/src/wasm_c_api/types/frame.rs @@ -86,7 +86,7 @@ pub unsafe extern "C" fn wasm_frame_func_name( #[no_mangle] pub unsafe extern "C" fn wasm_function_name_delete(name: Option<&mut wasm_function_name>) { if let Some(s) = name { - if s.name != std::ptr::null_mut() { + if s.name.is_null() { let _ = CString::from_raw(s.name); } } From f62ec23a80d587830862dca0f37ef31ab1b446a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Mon, 10 Oct 2022 12:49:58 +0200 Subject: [PATCH 3/9] Added wasm_frame_get_module_name --- lib/c-api/src/wasm_c_api/types/frame.rs | 55 +++++++++++++++++++++---- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/lib/c-api/src/wasm_c_api/types/frame.rs b/lib/c-api/src/wasm_c_api/types/frame.rs index 8e63c782018..4b2aa0f3c20 100644 --- a/lib/c-api/src/wasm_c_api/types/frame.rs +++ b/lib/c-api/src/wasm_c_api/types/frame.rs @@ -3,13 +3,6 @@ use libc::c_char; use std::ffi::CString; use wasmer_api::FrameInfo; -#[repr(C)] -#[allow(non_camel_case_types)] -#[derive(Debug, Clone)] -pub struct wasm_function_name { - pub name: *mut c_char, -} - #[allow(non_camel_case_types)] #[derive(Debug, Clone)] pub struct wasm_frame_t { @@ -56,6 +49,45 @@ pub unsafe extern "C" fn wasm_frame_module_offset(frame: &wasm_frame_t) -> usize frame.info.module_offset() } +#[repr(C)] +#[allow(non_camel_case_types)] +#[derive(Debug, Clone)] +pub struct wasm_module_name { + pub name: *mut c_char, +} + +#[no_mangle] +pub unsafe extern "C" fn wasm_frame_module_name( + frame: &wasm_frame_t, + out: Option<&mut wasm_module_name>, +) -> u32 { + let out = match out { + Some(s) => s, + None => return 1, + }; + + let module_name = + Some(frame.info.module_name()).and_then(|f| Some(CString::new(f).ok()?.into_raw())); + + match module_name { + Some(s) => { + out.name = s; + 0 + } + None => { + out.name = std::ptr::null_mut(); + 1 + } + } +} + +#[repr(C)] +#[allow(non_camel_case_types)] +#[derive(Debug, Clone)] +pub struct wasm_function_name { + pub name: *mut c_char, +} + #[no_mangle] pub unsafe extern "C" fn wasm_frame_func_name( frame: &wasm_frame_t, @@ -83,6 +115,15 @@ pub unsafe extern "C" fn wasm_frame_func_name( } } +#[no_mangle] +pub unsafe extern "C" fn wasm_module_name_delete(name: Option<&mut wasm_module_name>) { + if let Some(s) = name { + if s.name.is_null() { + let _ = CString::from_raw(s.name); + } + } +} + #[no_mangle] pub unsafe extern "C" fn wasm_function_name_delete(name: Option<&mut wasm_function_name>) { if let Some(s) = name { From 50c2e2861fddaaf224f2827549150a95cd986971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Tue, 11 Oct 2022 16:41:04 +0200 Subject: [PATCH 4/9] Remove wit-bindgen project --- lib/wasi-types/wit-bindgen | 1 - 1 file changed, 1 deletion(-) delete mode 160000 lib/wasi-types/wit-bindgen diff --git a/lib/wasi-types/wit-bindgen b/lib/wasi-types/wit-bindgen deleted file mode 160000 index 44a2bf81489..00000000000 --- a/lib/wasi-types/wit-bindgen +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 44a2bf8148932590479bd64b9f90502b14c3b0d3 From 46d967178f32144f036d743c701ca2dcaf46b85e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Wed, 12 Oct 2022 07:23:58 +0200 Subject: [PATCH 5/9] Fix PR based on review --- lib/c-api/src/wasm_c_api/types/frame.rs | 58 ++++++------------------- 1 file changed, 13 insertions(+), 45 deletions(-) diff --git a/lib/c-api/src/wasm_c_api/types/frame.rs b/lib/c-api/src/wasm_c_api/types/frame.rs index 4b2aa0f3c20..6ffff81b83f 100644 --- a/lib/c-api/src/wasm_c_api/types/frame.rs +++ b/lib/c-api/src/wasm_c_api/types/frame.rs @@ -52,50 +52,33 @@ pub unsafe extern "C" fn wasm_frame_module_offset(frame: &wasm_frame_t) -> usize #[repr(C)] #[allow(non_camel_case_types)] #[derive(Debug, Clone)] -pub struct wasm_module_name { +pub struct wasm_name_t { pub name: *mut c_char, } #[no_mangle] pub unsafe extern "C" fn wasm_frame_module_name( frame: &wasm_frame_t, - out: Option<&mut wasm_module_name>, -) -> u32 { - let out = match out { - Some(s) => s, - None => return 1, +) -> wasm_name_t { + let null = wasm_name_t { + name: core::ptr::null_mut(), }; let module_name = Some(frame.info.module_name()).and_then(|f| Some(CString::new(f).ok()?.into_raw())); match module_name { - Some(s) => { - out.name = s; - 0 - } - None => { - out.name = std::ptr::null_mut(); - 1 - } + Some(s) => wasm_name_t { name: s }, + None => null } } -#[repr(C)] -#[allow(non_camel_case_types)] -#[derive(Debug, Clone)] -pub struct wasm_function_name { - pub name: *mut c_char, -} - #[no_mangle] pub unsafe extern "C" fn wasm_frame_func_name( frame: &wasm_frame_t, - out: Option<&mut wasm_function_name>, -) -> u32 { - let out = match out { - Some(s) => s, - None => return 1, +) -> wasm_name_t { + let null = wasm_name_t { + name: core::ptr::null_mut(), }; let func_name = frame @@ -104,30 +87,15 @@ pub unsafe extern "C" fn wasm_frame_func_name( .and_then(|f| Some(CString::new(f).ok()?.into_raw())); match func_name { - Some(s) => { - out.name = s; - 0 - } - None => { - out.name = std::ptr::null_mut(); - 1 - } - } -} - -#[no_mangle] -pub unsafe extern "C" fn wasm_module_name_delete(name: Option<&mut wasm_module_name>) { - if let Some(s) = name { - if s.name.is_null() { - let _ = CString::from_raw(s.name); - } + Some(s) => wasm_name_t { name: s }, + None => null, } } #[no_mangle] -pub unsafe extern "C" fn wasm_function_name_delete(name: Option<&mut wasm_function_name>) { +pub unsafe extern "C" fn wasm_name_delete(name: Option<&mut wasm_name_t>) { if let Some(s) = name { - if s.name.is_null() { + if !s.name.is_null() { let _ = CString::from_raw(s.name); } } From ed433a6c15b41dec4a4f8ddfa970e0527875a192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Wed, 12 Oct 2022 07:24:31 +0200 Subject: [PATCH 6/9] cargo fmt --- lib/c-api/src/wasm_c_api/types/frame.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/c-api/src/wasm_c_api/types/frame.rs b/lib/c-api/src/wasm_c_api/types/frame.rs index 6ffff81b83f..4e554ad4b98 100644 --- a/lib/c-api/src/wasm_c_api/types/frame.rs +++ b/lib/c-api/src/wasm_c_api/types/frame.rs @@ -57,9 +57,7 @@ pub struct wasm_name_t { } #[no_mangle] -pub unsafe extern "C" fn wasm_frame_module_name( - frame: &wasm_frame_t, -) -> wasm_name_t { +pub unsafe extern "C" fn wasm_frame_module_name(frame: &wasm_frame_t) -> wasm_name_t { let null = wasm_name_t { name: core::ptr::null_mut(), }; @@ -69,14 +67,12 @@ pub unsafe extern "C" fn wasm_frame_module_name( match module_name { Some(s) => wasm_name_t { name: s }, - None => null + None => null, } } #[no_mangle] -pub unsafe extern "C" fn wasm_frame_func_name( - frame: &wasm_frame_t, -) -> wasm_name_t { +pub unsafe extern "C" fn wasm_frame_func_name(frame: &wasm_frame_t) -> wasm_name_t { let null = wasm_name_t { name: core::ptr::null_mut(), }; From 912bb93613469477e972df81f6ccea601d947751 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Mon, 24 Oct 2022 17:45:21 +0200 Subject: [PATCH 7/9] Adress review comments --- lib/c-api/src/wasm_c_api/types/frame.rs | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/c-api/src/wasm_c_api/types/frame.rs b/lib/c-api/src/wasm_c_api/types/frame.rs index 4e554ad4b98..90a8d8e0db7 100644 --- a/lib/c-api/src/wasm_c_api/types/frame.rs +++ b/lib/c-api/src/wasm_c_api/types/frame.rs @@ -58,25 +58,19 @@ pub struct wasm_name_t { #[no_mangle] pub unsafe extern "C" fn wasm_frame_module_name(frame: &wasm_frame_t) -> wasm_name_t { - let null = wasm_name_t { - name: core::ptr::null_mut(), - }; - let module_name = Some(frame.info.module_name()).and_then(|f| Some(CString::new(f).ok()?.into_raw())); match module_name { Some(s) => wasm_name_t { name: s }, - None => null, + None => wasm_name_t { + name: core::ptr::null_mut(), + }, } } #[no_mangle] pub unsafe extern "C" fn wasm_frame_func_name(frame: &wasm_frame_t) -> wasm_name_t { - let null = wasm_name_t { - name: core::ptr::null_mut(), - }; - let func_name = frame .info .function_name() @@ -84,7 +78,9 @@ pub unsafe extern "C" fn wasm_frame_func_name(frame: &wasm_frame_t) -> wasm_name match func_name { Some(s) => wasm_name_t { name: s }, - None => null, + None => wasm_name_t { + name: core::ptr::null_mut(), + }, } } From 0abbf0d1eb4e55b42b1f1bca4aed672533093050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Mon, 24 Oct 2022 17:57:06 +0200 Subject: [PATCH 8/9] Add working unit test --- lib/c-api/src/wasm_c_api/types/frame.rs | 31 ++++++++++++++++++++++ lib/compiler/src/engine/trap/frame_info.rs | 27 +++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lib/c-api/src/wasm_c_api/types/frame.rs b/lib/c-api/src/wasm_c_api/types/frame.rs index 90a8d8e0db7..9cccd95805f 100644 --- a/lib/c-api/src/wasm_c_api/types/frame.rs +++ b/lib/c-api/src/wasm_c_api/types/frame.rs @@ -94,3 +94,34 @@ pub unsafe extern "C" fn wasm_name_delete(name: Option<&mut wasm_name_t>) { } wasm_declare_boxed_vec!(frame); + +#[cfg(test)] +#[test] +fn test_frame_name() { + use std::ffi::CStr; + use wasmer_types::SourceLoc; + + let info = wasm_frame_t { + info: FrameInfo::new( + "module_name".to_string(), + 5, + Some("function_name".to_string()), + SourceLoc::new(10), + SourceLoc::new(20), + ), + }; + + unsafe { + let mut wasm_frame_func_name = wasm_frame_func_name(&info); + let s = CStr::from_ptr(wasm_frame_func_name.name); + assert_eq!(s.to_str().unwrap(), "function_name"); + wasm_name_delete(Some(&mut wasm_frame_func_name)); + + let mut wasm_frame_module_name = wasm_frame_module_name(&info); + let s = CStr::from_ptr(wasm_frame_module_name.name); + assert_eq!(s.to_str().unwrap(), "module_name"); + wasm_name_delete(Some(&mut wasm_frame_module_name)); + } + + println!("{:#?}", info); +} diff --git a/lib/compiler/src/engine/trap/frame_info.rs b/lib/compiler/src/engine/trap/frame_info.rs index 65324014cfd..c43f20f5074 100644 --- a/lib/compiler/src/engine/trap/frame_info.rs +++ b/lib/compiler/src/engine/trap/frame_info.rs @@ -258,7 +258,34 @@ pub struct FrameInfo { instr: SourceLoc, } +#[cfg(test)] +#[derive(Debug, Clone)] +pub struct FrameInfo { + pub module_name: String, + pub func_index: u32, + pub function_name: Option, + pub func_start: SourceLoc, + pub instr: SourceLoc, +} + impl FrameInfo { + /// Creates a new [FrameInfo], useful for testing. + pub fn new( + module_name: String, + func_index: u32, + function_name: Option, + func_start: SourceLoc, + instr: SourceLoc, + ) -> Self { + Self { + module_name, + func_index, + function_name, + func_start, + instr, + } + } + /// Returns the WebAssembly function index for this frame. /// /// This function index is the index in the function index space of the From 9786231b802ea323af06c5aeda668ccccd8350b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sch=C3=BCtt?= Date: Mon, 24 Oct 2022 18:31:48 +0200 Subject: [PATCH 9/9] Remove duplicate FrameInfo --- lib/compiler/src/engine/trap/frame_info.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/lib/compiler/src/engine/trap/frame_info.rs b/lib/compiler/src/engine/trap/frame_info.rs index c43f20f5074..c7bf7034d73 100644 --- a/lib/compiler/src/engine/trap/frame_info.rs +++ b/lib/compiler/src/engine/trap/frame_info.rs @@ -258,16 +258,6 @@ pub struct FrameInfo { instr: SourceLoc, } -#[cfg(test)] -#[derive(Debug, Clone)] -pub struct FrameInfo { - pub module_name: String, - pub func_index: u32, - pub function_name: Option, - pub func_start: SourceLoc, - pub instr: SourceLoc, -} - impl FrameInfo { /// Creates a new [FrameInfo], useful for testing. pub fn new(