From b6c1cc49e142cba6715ac2f7099c3102ffe9273c Mon Sep 17 00:00:00 2001 From: Andreas Date: Fri, 11 Nov 2022 23:47:44 +0100 Subject: [PATCH] fix(es/plugin): Print more details on pointer conversion failures (#6378) --- crates/swc_common/src/plugin/serialized.rs | 4 +-- crates/swc_plugin_macro/src/lib.rs | 24 ++++++++--------- .../src/comments/plugin_comments_proxy.rs | 16 +++++------ .../read_returned_result_from_host.rs | 10 +++---- .../src/metadata/transform_plugin_metadata.rs | 12 ++++----- .../src/source_map/plugin_source_map_proxy.rs | 22 +++++++-------- .../src/imported_fn/comments.rs | 14 +++++----- .../src/imported_fn/diagnostics.rs | 4 +-- .../src/imported_fn/handler.rs | 2 +- .../src/imported_fn/hygiene.rs | 6 ++--- .../src/imported_fn/metadata_context.rs | 14 +++++----- .../src/imported_fn/set_transform_result.rs | 4 +-- .../src/imported_fn/source_map.rs | 14 +++++----- .../swc_plugin_runner/src/memory_interop.rs | 27 ++++++++++--------- .../src/transform_executor.rs | 20 +++++++------- 15 files changed, 98 insertions(+), 95 deletions(-) diff --git a/crates/swc_common/src/plugin/serialized.rs b/crates/swc_common/src/plugin/serialized.rs index 1b279ff2ec9..f61bbc2fe1e 100644 --- a/crates/swc_common/src/plugin/serialized.rs +++ b/crates/swc_common/src/plugin/serialized.rs @@ -123,7 +123,7 @@ impl PluginSerializedBytes { #[tracing::instrument(level = "info", skip_all)] pub unsafe fn deserialize_from_ptr( raw_allocated_ptr: *const u8, - raw_allocated_ptr_len: i32, + raw_allocated_ptr_len: u32, ) -> Result where W: rkyv::Archive, @@ -147,7 +147,7 @@ where #[tracing::instrument(level = "info", skip_all)] pub unsafe fn deserialize_from_ptr_into_fallible( raw_allocated_ptr: *const u8, - raw_allocated_ptr_len: i32, + raw_allocated_ptr_len: u32, ) -> Result where W: rkyv::Archive, diff --git a/crates/swc_plugin_macro/src/lib.rs b/crates/swc_plugin_macro/src/lib.rs index 77364d528e6..cc2ac7b74c0 100644 --- a/crates/swc_plugin_macro/src/lib.rs +++ b/crates/swc_plugin_macro/src/lib.rs @@ -31,9 +31,9 @@ fn handle_func(func: ItemFn) -> TokenStream { // Refer swc_plugin_runner for the actual implementation. #[cfg(target_arch = "wasm32")] // Allow testing extern "C" { - fn __set_transform_result(bytes_ptr: i32, bytes_ptr_len: i32); - fn __set_transform_plugin_core_pkg_diagnostics(bytes_ptr: i32, bytes_ptr_len: i32); - fn __emit_diagnostics(bytes_ptr: i32, bytes_ptr_len: i32); + fn __set_transform_result(bytes_ptr: u32, bytes_ptr_len: u32); + fn __set_transform_plugin_core_pkg_diagnostics(bytes_ptr: u32, bytes_ptr_len: u32); + fn __emit_diagnostics(bytes_ptr: u32, bytes_ptr_len: u32); } /// An emitter for the Diagnostic in plugin's context by borrowing host's @@ -52,7 +52,7 @@ fn handle_func(func: ItemFn) -> TokenStream { #[cfg(target_arch = "wasm32")] // Allow testing unsafe { - __emit_diagnostics(ptr as i32, len as i32); + __emit_diagnostics(ptr as u32, len as u32); } } } @@ -60,7 +60,7 @@ fn handle_func(func: ItemFn) -> TokenStream { /// Call hosts's imported fn to set transform results. /// __set_transform_result is host side imported fn, which read and copies guest's byte into host. - fn send_transform_result_to_host(bytes_ptr: i32, bytes_ptr_len: i32) { + fn send_transform_result_to_host(bytes_ptr: u32, bytes_ptr_len: u32) { #[cfg(target_arch = "wasm32")] // Allow testing unsafe { __set_transform_result(bytes_ptr, bytes_ptr_len); @@ -68,19 +68,19 @@ fn handle_func(func: ItemFn) -> TokenStream { } /// Internal function plugin_macro uses to create ptr to PluginError. - fn construct_error_ptr(plugin_error: swc_core::common::plugin::serialized::PluginError) -> i32 { + fn construct_error_ptr(plugin_error: swc_core::common::plugin::serialized::PluginError) -> u32 { let ret = swc_core::common::plugin::serialized::PluginSerializedBytes::try_serialize(&plugin_error).expect("Should able to serialize PluginError"); let (ptr, len) = ret.as_ptr(); send_transform_result_to_host( ptr as _, - len as i32 + len as u32 ); 1 } #[no_mangle] - pub fn #transform_core_pkg_diag_ident() -> i32 { + pub fn #transform_core_pkg_diag_ident() -> u32 { let schema_version = swc_core::common::plugin::PLUGIN_TRANSFORM_AST_SCHEMA_VERSION; let core_pkg_diag = swc_core::diagnostics::get_core_engine_diagnostics(); @@ -99,7 +99,7 @@ fn handle_func(func: ItemFn) -> TokenStream { #[cfg(target_arch = "wasm32")] // Allow testing unsafe { - __set_transform_plugin_core_pkg_diagnostics(serialized_result_ptr as _, serialized_result_ptr_len as i32); + __set_transform_plugin_core_pkg_diagnostics(serialized_result_ptr as _, serialized_result_ptr_len as u32); } 0 } @@ -110,8 +110,8 @@ fn handle_func(func: ItemFn) -> TokenStream { // serialization of PluginError itself should succeed. #[no_mangle] pub fn #transform_process_impl_ident( - ast_ptr: *const u8, ast_ptr_len: i32, - unresolved_mark: i32, should_enable_comments_proxy: i32) -> i32 { + ast_ptr: *const u8, ast_ptr_len: u32, + unresolved_mark: u32, should_enable_comments_proxy: i32) -> u32 { // Reconstruct `Program` & config string from serialized program // Host (SWC) should allocate memory, copy bytes and pass ptr to plugin. let program = unsafe { swc_core::common::plugin::serialized::deserialize_from_ptr(ast_ptr, ast_ptr_len) }; @@ -160,7 +160,7 @@ fn handle_func(func: ItemFn) -> TokenStream { let serialized_result = serialized_result.expect("Should be a realized transformed program"); let (serialized_result_ptr, serialized_result_ptr_len) = serialized_result.as_ptr(); - send_transform_result_to_host(serialized_result_ptr as _, serialized_result_ptr_len as i32); + send_transform_result_to_host(serialized_result_ptr as _, serialized_result_ptr_len as u32); 0 } }; diff --git a/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs b/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs index 1e52c78505d..b3ee6f31a21 100644 --- a/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs +++ b/crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs @@ -14,19 +14,19 @@ use crate::memory_interop::read_returned_result_from_host; #[cfg(target_arch = "wasm32")] extern "C" { - fn __copy_comment_to_host_env(bytes_ptr: i32, bytes_ptr_len: i32); + fn __copy_comment_to_host_env(bytes_ptr: u32, bytes_ptr_len: u32); fn __add_leading_comment_proxy(byte_pos: u32); fn __add_leading_comments_proxy(byte_pos: u32); - fn __has_leading_comments_proxy(byte_pos: u32) -> i32; + fn __has_leading_comments_proxy(byte_pos: u32) -> u32; fn __move_leading_comments_proxy(from_byte_pos: u32, to_byte_pos: u32); - fn __take_leading_comments_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; - fn __get_leading_comments_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; + fn __take_leading_comments_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32; + fn __get_leading_comments_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32; fn __add_trailing_comment_proxy(byte_pos: u32); fn __add_trailing_comments_proxy(byte_pos: u32); - fn __has_trailing_comments_proxy(byte_pos: u32) -> i32; + fn __has_trailing_comments_proxy(byte_pos: u32) -> u32; fn __move_trailing_comments_proxy(from_byte_pos: u32, to_byte_pos: u32); - fn __take_trailing_comments_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; - fn __get_trailing_comments_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; + fn __take_trailing_comments_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32; + fn __get_trailing_comments_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32; fn __add_pure_comment_proxy(byte_pos: u32); } @@ -65,7 +65,7 @@ impl PluginCommentsProxy { // CommentHostEnvironment's buffer, subsequent proxy call will read & // deserialize it. __copy_comment_to_host_env( - serialized_comment_ptr as i32, + serialized_comment_ptr as u32, serialized_comment_ptr_len .try_into() .expect("Should able to convert ptr length"), diff --git a/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs b/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs index 170c9c78af0..275f69e8b9c 100644 --- a/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs +++ b/crates/swc_plugin_proxy/src/memory_interop/read_returned_result_from_host.rs @@ -11,7 +11,7 @@ use swc_common::plugin::serialized::{ feature = "__rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize) )] -pub struct AllocatedBytesPtr(pub i32, pub i32); +pub struct AllocatedBytesPtr(pub u32, pub u32); #[cfg(not(feature = "__rkyv"))] fn read_returned_result_from_host_inner(f: F) -> Option { @@ -31,7 +31,7 @@ fn read_returned_result_from_host_inner(f: F) -> Option { #[tracing::instrument(level = "info", skip_all)] fn read_returned_result_from_host_inner(f: F) -> Option where - F: FnOnce(i32) -> i32, + F: FnOnce(u32) -> u32, { // Allocate AllocatedBytesPtr to get return value from the host let allocated_bytes_ptr = AllocatedBytesPtr(0, 0); @@ -78,7 +78,7 @@ pub fn read_returned_result_from_host(f: F) -> Option { #[tracing::instrument(level = "info", skip_all)] pub fn read_returned_result_from_host(f: F) -> Option where - F: FnOnce(i32) -> i32, + F: FnOnce(u32) -> u32, R: rkyv::Archive, R::Archived: rkyv::Deserialize, { @@ -110,7 +110,7 @@ pub fn read_returned_result_from_host_fallible(f: F) -> Option { #[tracing::instrument(level = "info", skip_all)] pub fn read_returned_result_from_host_fallible(f: F) -> Option where - F: FnOnce(i32) -> i32, + F: FnOnce(u32) -> u32, R: rkyv::Archive, R::Archived: rkyv::Deserialize, { @@ -138,7 +138,7 @@ where let allocated_returned_value_ptr: AllocatedBytesPtr = unsafe { deserialize_from_ptr( serialized_allocated_bytes_raw_ptr, - serialized_allocated_bytes_raw_ptr_size as i32, + serialized_allocated_bytes_raw_ptr_size as u32, ) .expect("Should able to deserialize AllocatedBytesPtr") }; diff --git a/crates/swc_plugin_proxy/src/metadata/transform_plugin_metadata.rs b/crates/swc_plugin_proxy/src/metadata/transform_plugin_metadata.rs index fb31399c2a5..8bb3e7377b6 100644 --- a/crates/swc_plugin_proxy/src/metadata/transform_plugin_metadata.rs +++ b/crates/swc_plugin_proxy/src/metadata/transform_plugin_metadata.rs @@ -31,11 +31,11 @@ pub struct TransformPluginProgramMetadata { #[cfg(target_arch = "wasm32")] // Allow testing extern "C" { - fn __copy_context_key_to_host_env(bytes_ptr: i32, bytes_ptr_len: i32); - fn __get_transform_plugin_config(allocated_ret_ptr: i32) -> i32; - fn __get_transform_context(key: u32, allocated_ret_ptr: i32) -> i32; - fn __get_experimental_transform_context(allocated_ret_ptr: i32) -> i32; - fn __get_raw_experiemtal_transform_context(allocated_ret_ptr: i32) -> i32; + fn __copy_context_key_to_host_env(bytes_ptr: u32, bytes_ptr_len: u32); + fn __get_transform_plugin_config(allocated_ret_ptr: u32) -> u32; + fn __get_transform_context(key: u32, allocated_ret_ptr: u32) -> u32; + fn __get_experimental_transform_context(allocated_ret_ptr: u32) -> u32; + fn __get_raw_experiemtal_transform_context(allocated_ret_ptr: u32) -> u32; } #[cfg(feature = "__plugin_mode")] @@ -86,7 +86,7 @@ impl TransformPluginProgramMetadata { ) .expect("Should be serializable"); let (key_ptr, key_ptr_len) = serialized.as_ptr(); - __copy_context_key_to_host_env(key_ptr as i32, key_ptr_len as i32); + __copy_context_key_to_host_env(key_ptr as u32, key_ptr_len as u32); __get_experimental_transform_context(serialized_ptr) }); diff --git a/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs b/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs index 1cf44f3061d..d34d2e14879 100644 --- a/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs +++ b/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs @@ -22,8 +22,8 @@ extern "C" { fn __lookup_char_pos_source_map_proxy( byte_pos: u32, should_include_source_file: i32, - allocated_ret_ptr: i32, - ) -> i32; + allocated_ret_ptr: u32, + ) -> u32; fn __doctest_offset_line_proxy(orig: u32) -> u32; fn __merge_spans_proxy( lhs_lo: u32, @@ -32,28 +32,28 @@ extern "C" { rhs_lo: u32, rhs_hi: u32, rhs_ctxt: u32, - allocated_ptr: i32, - ) -> i32; + allocated_ptr: u32, + ) -> u32; fn __span_to_string_proxy( span_lo: u32, span_hi: u32, span_ctxt: u32, - allocated_ret_ptr: i32, - ) -> i32; + allocated_ret_ptr: u32, + ) -> u32; fn __span_to_filename_proxy( span_lo: u32, span_hi: u32, span_ctxt: u32, - allocated_ret_ptr: i32, - ) -> i32; + allocated_ret_ptr: u32, + ) -> u32; fn __span_to_lines_proxy( span_lo: u32, span_hi: u32, span_ctxt: u32, should_request_source_file: i32, - allocated_ret_ptr: i32, - ) -> i32; - fn __lookup_byte_offset_proxy(byte_pos: u32, allocated_ret_ptr: i32) -> i32; + allocated_ret_ptr: u32, + ) -> u32; + fn __lookup_byte_offset_proxy(byte_pos: u32, allocated_ret_ptr: u32) -> u32; } #[cfg(feature = "__plugin_mode")] diff --git a/crates/swc_plugin_runner/src/imported_fn/comments.rs b/crates/swc_plugin_runner/src/imported_fn/comments.rs index c16d4b33d10..578b516c14a 100644 --- a/crates/swc_plugin_runner/src/imported_fn/comments.rs +++ b/crates/swc_plugin_runner/src/imported_fn/comments.rs @@ -20,7 +20,7 @@ pub struct CommentHostEnvironment { /// Attached imported fn `__alloc` to the hostenvironment to allow any other /// imported fn can allocate guest's memory space from host runtime. #[wasmer(export(name = "__alloc"))] - pub alloc_guest_memory: LazyInit>, + pub alloc_guest_memory: LazyInit>, /// A buffer to `Comment`, or `Vec` plugin need to pass to the host /// to perform mutable comment operations like `add_leading, or /// add_leading_comments`. This is vec to serialized bytes, doesn't @@ -42,7 +42,7 @@ impl CommentHostEnvironment { /// Copy given serialized byte into host's comment buffer, subsequent proxy call /// in the host can read it. #[tracing::instrument(level = "info", skip_all)] -pub fn copy_comment_to_host_env(env: &CommentHostEnvironment, bytes_ptr: i32, bytes_ptr_len: i32) { +pub fn copy_comment_to_host_env(env: &CommentHostEnvironment, bytes_ptr: u32, bytes_ptr_len: u32) { if let Some(memory) = env.memory_ref() { (*env.mutable_comment_buffer.lock()) = copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len); @@ -90,7 +90,7 @@ where #[tracing::instrument(level = "info", skip_all)] fn unwrap_comments_storage_with_env(env: &CommentHostEnvironment, f: F, default: R) -> R where - F: FnOnce(&SingleThreadedComments, &Memory, &NativeFunc) -> R, + F: FnOnce(&SingleThreadedComments, &Memory, &NativeFunc) -> R, { if let Some(memory) = env.memory_ref() { if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { @@ -162,7 +162,7 @@ pub fn move_leading_comments_proxy(from_byte_pos: u32, to_byte_pos: u32) { pub fn take_leading_comments_proxy( env: &CommentHostEnvironment, byte_pos: u32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { unwrap_comments_storage_with_env( env, @@ -197,7 +197,7 @@ pub fn take_leading_comments_proxy( pub fn get_leading_comments_proxy( env: &CommentHostEnvironment, byte_pos: u32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { unwrap_comments_storage_with_env( env, @@ -266,7 +266,7 @@ pub fn move_trailing_comments_proxy(from_byte_pos: u32, to_byte_pos: u32) { pub fn take_trailing_comments_proxy( env: &CommentHostEnvironment, byte_pos: u32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { unwrap_comments_storage_with_env( env, @@ -296,7 +296,7 @@ pub fn take_trailing_comments_proxy( pub fn get_trailing_comments_proxy( env: &CommentHostEnvironment, byte_pos: u32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { unwrap_comments_storage_with_env( env, diff --git a/crates/swc_plugin_runner/src/imported_fn/diagnostics.rs b/crates/swc_plugin_runner/src/imported_fn/diagnostics.rs index 26d41193afc..758bf983f89 100644 --- a/crates/swc_plugin_runner/src/imported_fn/diagnostics.rs +++ b/crates/swc_plugin_runner/src/imported_fn/diagnostics.rs @@ -26,8 +26,8 @@ impl DiagnosticContextHostEnvironment { #[tracing::instrument(level = "info", skip_all)] pub fn set_plugin_core_pkg_diagnostics( env: &DiagnosticContextHostEnvironment, - bytes_ptr: i32, - bytes_ptr_len: i32, + bytes_ptr: u32, + bytes_ptr_len: u32, ) { let memory = env.memory_ref().expect("Memory should be initialized"); (*env.core_diag_buffer.lock()) = copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len); diff --git a/crates/swc_plugin_runner/src/imported_fn/handler.rs b/crates/swc_plugin_runner/src/imported_fn/handler.rs index 2476c9727d1..9eae3176df2 100644 --- a/crates/swc_plugin_runner/src/imported_fn/handler.rs +++ b/crates/swc_plugin_runner/src/imported_fn/handler.rs @@ -6,7 +6,7 @@ use swc_common::{ use crate::{host_environment::BaseHostEnvironment, memory_interop::copy_bytes_into_host}; #[tracing::instrument(level = "info", skip_all)] -pub fn emit_diagnostics(env: &BaseHostEnvironment, bytes_ptr: i32, bytes_ptr_len: i32) { +pub fn emit_diagnostics(env: &BaseHostEnvironment, bytes_ptr: u32, bytes_ptr_len: u32) { if let Some(memory) = env.memory_ref() { if HANDLER.is_set() { HANDLER.with(|handler| { diff --git a/crates/swc_plugin_runner/src/imported_fn/hygiene.rs b/crates/swc_plugin_runner/src/imported_fn/hygiene.rs index 667522cfd1f..6f50430ac97 100644 --- a/crates/swc_plugin_runner/src/imported_fn/hygiene.rs +++ b/crates/swc_plugin_runner/src/imported_fn/hygiene.rs @@ -33,7 +33,7 @@ pub fn mark_is_descendant_of_proxy( env: &BaseHostEnvironment, self_mark: u32, ancestor: u32, - allocated_ptr: i32, + allocated_ptr: u32, ) { let self_mark = Mark::from_u32(self_mark); let ancestor = Mark::from_u32(ancestor); @@ -50,7 +50,7 @@ pub fn mark_is_descendant_of_proxy( } #[tracing::instrument(level = "info", skip_all)] -pub fn mark_least_ancestor_proxy(env: &BaseHostEnvironment, a: u32, b: u32, allocated_ptr: i32) { +pub fn mark_least_ancestor_proxy(env: &BaseHostEnvironment, a: u32, b: u32, allocated_ptr: u32) { let a = Mark::from_u32(a); let b = Mark::from_u32(b); @@ -76,7 +76,7 @@ pub fn syntax_context_apply_mark_proxy(self_syntax_context: u32, mark: u32) -> u pub fn syntax_context_remove_mark_proxy( env: &BaseHostEnvironment, self_mark: u32, - allocated_ptr: i32, + allocated_ptr: u32, ) { let mut self_mark = SyntaxContext::from_u32(self_mark); diff --git a/crates/swc_plugin_runner/src/imported_fn/metadata_context.rs b/crates/swc_plugin_runner/src/imported_fn/metadata_context.rs index 80f5792b3a2..fa4af50ca27 100644 --- a/crates/swc_plugin_runner/src/imported_fn/metadata_context.rs +++ b/crates/swc_plugin_runner/src/imported_fn/metadata_context.rs @@ -16,7 +16,7 @@ pub struct MetadataContextHostEnvironment { /// Attached imported fn `__alloc` to the hostenvironment to allow any other /// imported fn can allocate guest's memory space from host runtime. #[wasmer(export(name = "__alloc"))] - pub alloc_guest_memory: LazyInit>, + pub alloc_guest_memory: LazyInit>, pub metadata_context: Arc, pub transform_plugin_config: Option, /// A buffer to string key to the context plugin need to pass to the host. @@ -44,8 +44,8 @@ impl MetadataContextHostEnvironment { #[tracing::instrument(level = "info", skip_all)] pub fn copy_context_key_to_host_env( env: &MetadataContextHostEnvironment, - bytes_ptr: i32, - bytes_ptr_len: i32, + bytes_ptr: u32, + bytes_ptr_len: u32, ) { if let Some(memory) = env.memory_ref() { (*env.mutable_context_key_buffer.lock()) = @@ -56,7 +56,7 @@ pub fn copy_context_key_to_host_env( #[tracing::instrument(level = "info", skip_all)] pub fn get_transform_plugin_config( env: &MetadataContextHostEnvironment, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { @@ -88,7 +88,7 @@ pub fn get_transform_plugin_config( pub fn get_transform_context( env: &MetadataContextHostEnvironment, key: u32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { @@ -117,7 +117,7 @@ pub fn get_transform_context( #[tracing::instrument(level = "info", skip_all)] pub fn get_experimental_transform_context( env: &MetadataContextHostEnvironment, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() { @@ -153,7 +153,7 @@ pub fn get_experimental_transform_context( #[tracing::instrument(level = "info", skip_all)] pub fn get_raw_experiemtal_transform_context( env: &MetadataContextHostEnvironment, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { let experimental_context = &env.metadata_context.experimental; diff --git a/crates/swc_plugin_runner/src/imported_fn/set_transform_result.rs b/crates/swc_plugin_runner/src/imported_fn/set_transform_result.rs index ad4d4e90008..a645b99b3f8 100644 --- a/crates/swc_plugin_runner/src/imported_fn/set_transform_result.rs +++ b/crates/swc_plugin_runner/src/imported_fn/set_transform_result.rs @@ -33,8 +33,8 @@ impl TransformResultHostEnvironment { #[tracing::instrument(level = "info", skip_all)] pub fn set_transform_result( env: &TransformResultHostEnvironment, - bytes_ptr: i32, - bytes_ptr_len: i32, + bytes_ptr: u32, + bytes_ptr_len: u32, ) { if let Some(memory) = env.memory_ref() { (*env.transform_result.lock()) = copy_bytes_into_host(memory, bytes_ptr, bytes_ptr_len); diff --git a/crates/swc_plugin_runner/src/imported_fn/source_map.rs b/crates/swc_plugin_runner/src/imported_fn/source_map.rs index 71316114591..2cb6722f966 100644 --- a/crates/swc_plugin_runner/src/imported_fn/source_map.rs +++ b/crates/swc_plugin_runner/src/imported_fn/source_map.rs @@ -19,7 +19,7 @@ pub struct SourceMapHostEnvironment { /// Attached imported fn `__alloc` to the hostenvironment to allow any other /// imported fn can allocate guest's memory space from host runtime. #[wasmer(export(name = "__alloc"))] - pub alloc_guest_memory: LazyInit>, + pub alloc_guest_memory: LazyInit>, pub source_map: Arc>>, /// A buffer to non-determined size of return value from the host. pub mutable_source_map_buffer: Arc>>, @@ -47,7 +47,7 @@ pub fn lookup_char_pos_proxy( env: &SourceMapHostEnvironment, byte_pos: u32, should_include_source_file: i32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { let original_loc = (env.source_map.lock()).lookup_char_pos(BytePos(byte_pos)); @@ -96,7 +96,7 @@ pub fn merge_spans_proxy( rhs_lo: u32, rhs_hi: u32, rhs_ctxt: u32, - allocated_ptr: i32, + allocated_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { let sp_lhs = Span { @@ -132,7 +132,7 @@ pub fn span_to_lines_proxy( span_hi: u32, span_ctxt: u32, should_request_source_file: i32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { let span = Span { @@ -175,7 +175,7 @@ pub fn span_to_lines_proxy( pub fn lookup_byte_offset_proxy( env: &SourceMapHostEnvironment, byte_pos: u32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { let byte_pos = BytePos(byte_pos); @@ -206,7 +206,7 @@ pub fn span_to_string_proxy( span_lo: u32, span_hi: u32, span_ctxt: u32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { let span = Span { @@ -240,7 +240,7 @@ pub fn span_to_filename_proxy( span_lo: u32, span_hi: u32, span_ctxt: u32, - allocated_ret_ptr: i32, + allocated_ret_ptr: u32, ) -> i32 { if let Some(memory) = env.memory_ref() { let span = Span { diff --git a/crates/swc_plugin_runner/src/memory_interop.rs b/crates/swc_plugin_runner/src/memory_interop.rs index 9365e4a04ed..4c2dc52b4f9 100644 --- a/crates/swc_plugin_runner/src/memory_interop.rs +++ b/crates/swc_plugin_runner/src/memory_interop.rs @@ -3,7 +3,7 @@ use swc_plugin_proxy::AllocatedBytesPtr; use wasmer::{Array, Memory, NativeFunc, WasmPtr}; #[tracing::instrument(level = "info", skip_all)] -pub fn copy_bytes_into_host(memory: &Memory, bytes_ptr: i32, bytes_ptr_len: i32) -> Vec { +pub fn copy_bytes_into_host(memory: &Memory, bytes_ptr: u32, bytes_ptr_len: u32) -> Vec { let ptr: WasmPtr = WasmPtr::new(bytes_ptr as _); // Deref & read through plugin's wasm memory space via returned ptr @@ -25,19 +25,22 @@ pub fn write_into_memory_view( memory: &Memory, serialized_bytes: &PluginSerializedBytes, get_allocated_ptr: F, -) -> (i32, i32) +) -> (u32, u32) where - F: Fn(usize) -> i32, + F: Fn(usize) -> u32, { let serialized_len = serialized_bytes.as_ptr().1; - let ptr_start = get_allocated_ptr(serialized_len); - let ptr_start_size = ptr_start + let ptr_start: u32 = get_allocated_ptr(serialized_len); + let ptr_start_size: u32 = ptr_start .try_into() - .expect("Should be able to convert to usize"); - let serialized_len_size: u32 = serialized_len - .try_into() - .expect("Should be able to convert to u32"); + .unwrap_or_else(|_| panic!("Should be able to convert the value {} to u32", ptr_start)); + let serialized_len_size: u32 = serialized_len.try_into().unwrap_or_else(|_| { + panic!( + "Should be able to convert the value {} to u32", + serialized_len + ) + }); // Note: it's important to get a view from memory _after_ alloc completes let view = memory.view::(); @@ -69,11 +72,11 @@ where #[tracing::instrument(level = "info", skip_all)] pub fn allocate_return_values_into_guest( memory: &Memory, - alloc_guest_memory: &NativeFunc, - allocated_ret_ptr: i32, + alloc_guest_memory: &NativeFunc, + allocated_ret_ptr: u32, serialized_bytes: &PluginSerializedBytes, ) { - let serialized_bytes_len = serialized_bytes.as_ptr().1; + let serialized_bytes_len: usize = serialized_bytes.as_ptr().1; let (allocated_ptr, allocated_ptr_len) = write_into_memory_view(memory, serialized_bytes, |_| { diff --git a/crates/swc_plugin_runner/src/transform_executor.rs b/crates/swc_plugin_runner/src/transform_executor.rs index 1389f3bcb68..564692fd953 100644 --- a/crates/swc_plugin_runner/src/transform_executor.rs +++ b/crates/swc_plugin_runner/src/transform_executor.rs @@ -21,16 +21,16 @@ use crate::memory_interop::write_into_memory_view; /// A struct encapsule executing a plugin's transform interop to its teardown pub struct TransformExecutor { // Main transform interface plugin exports - exported_plugin_transform: wasmer::NativeFunc<(i32, i32, u32, i32), i32>, + exported_plugin_transform: wasmer::NativeFunc<(u32, u32, u32, u32), u32>, // `__free` function automatically exported via swc_plugin sdk to allow deallocation in guest // memory space - exported_plugin_free: wasmer::NativeFunc<(i32, i32), i32>, + exported_plugin_free: wasmer::NativeFunc<(u32, u32), u32>, // `__alloc` function automatically exported via swc_plugin sdk to allow allocation in guest // memory space - exported_plugin_alloc: wasmer::NativeFunc, + exported_plugin_alloc: wasmer::NativeFunc, instance: Instance, // Reference to the pointers successfully allocated which'll be freed by Drop. - allocated_ptr_vec: Vec<(i32, i32)>, + allocated_ptr_vec: Vec<(u32, u32)>, transform_result: Arc>>, // diagnostic metadata for the swc_core plugin binary uses. pub plugin_core_diag: PluginCorePkgDiagnostics, @@ -81,15 +81,15 @@ impl TransformExecutor { let tracker = TransformExecutor { exported_plugin_transform: instance .exports - .get_native_function::<(i32, i32, u32, i32), i32>( + .get_native_function::<(u32, u32, u32, u32), u32>( "__transform_plugin_process_impl", )?, exported_plugin_free: instance .exports - .get_native_function::<(i32, i32), i32>("__free")?, + .get_native_function::<(u32, u32), u32>("__free")?, exported_plugin_alloc: instance .exports - .get_native_function::("__alloc")?, + .get_native_function::("__alloc")?, instance, allocated_ptr_vec: Vec::with_capacity(3), transform_result, @@ -105,7 +105,7 @@ impl TransformExecutor { fn write_bytes_into_guest( &mut self, serialized_bytes: &PluginSerializedBytes, - ) -> Result<(i32, i32), Error> { + ) -> Result<(u32, u32), Error> { let memory = self.instance.exports.get_memory("memory")?; let ptr = write_into_memory_view(memory, serialized_bytes, |serialized_len| { @@ -122,7 +122,7 @@ impl TransformExecutor { /// bytes. fn read_transformed_result_bytes_from_guest( &mut self, - returned_ptr_result: i32, + returned_ptr_result: u32, ) -> Result { let transformed_result = &(*self.transform_result.lock()); let ret = PluginSerializedBytes::from_slice(&transformed_result[..]); @@ -187,7 +187,7 @@ impl TransformExecutor { unresolved_mark: swc_common::Mark, should_enable_comments_proxy: bool, ) -> Result { - let should_enable_comments_proxy = i32::from(should_enable_comments_proxy); + let should_enable_comments_proxy = u32::from(should_enable_comments_proxy); let guest_program_ptr = self.write_bytes_into_guest(program)?; let result = self.exported_plugin_transform.call(