diff --git a/runtime/src/plugin.rs b/runtime/src/plugin.rs index aebe306..dd1d9f1 100644 --- a/runtime/src/plugin.rs +++ b/runtime/src/plugin.rs @@ -81,6 +81,8 @@ pub struct Plugin { pub(crate) store_needs_reset: bool, pub(crate) debug_options: DebugOptions, + + pub(crate) error_msg: Option>, } unsafe impl Send for Plugin {} @@ -361,6 +363,7 @@ impl Plugin { store_needs_reset: false, debug_options, _functions: imports, + error_msg: None, }; plugin.current_plugin_mut().store = &mut plugin.store; diff --git a/runtime/src/sdk.rs b/runtime/src/sdk.rs index e33c0c6..61a360d 100644 --- a/runtime/src/sdk.rs +++ b/runtime/src/sdk.rs @@ -505,6 +505,8 @@ pub unsafe extern "C" fn extism_plugin_call_with_host_context( let lock = plugin.instance.clone(); let mut lock = lock.lock().unwrap(); + plugin.error_msg = None; + // Get function name let name = std::ffi::CStr::from_ptr(func_name); let name = match name.to_str() { @@ -551,10 +553,19 @@ pub unsafe extern "C" fn extism_plugin_error(plugin: *mut Plugin) -> *const c_ch return std::ptr::null(); } - plugin + let offs = plugin.output.error_offset; + + let ptr = plugin.current_plugin_mut().memory_ptr().add(offs as usize) as *const _; + + let len = plugin .current_plugin_mut() - .memory_ptr() - .add(plugin.output.error_offset as usize) as *const _ + .memory_length(offs) + .unwrap_or_default(); + + let mut data = std::slice::from_raw_parts(ptr, len as usize).to_vec(); + data.push(0); + plugin.error_msg = Some(data); + plugin.error_msg.as_ref().unwrap().as_ptr() as *const _ } /// Get the length of a plugin's output data