Skip to content

Commit

Permalink
fix(es/plugin): Print more details on pointer conversion failures (#6378
Browse files Browse the repository at this point in the history
)
  • Loading branch information
stahlbauer committed Nov 11, 2022
1 parent 8d8f058 commit b6c1cc4
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 95 deletions.
4 changes: 2 additions & 2 deletions crates/swc_common/src/plugin/serialized.rs
Expand Up @@ -123,7 +123,7 @@ impl PluginSerializedBytes {
#[tracing::instrument(level = "info", skip_all)]
pub unsafe fn deserialize_from_ptr<W>(
raw_allocated_ptr: *const u8,
raw_allocated_ptr_len: i32,
raw_allocated_ptr_len: u32,
) -> Result<W, Error>
where
W: rkyv::Archive,
Expand All @@ -147,7 +147,7 @@ where
#[tracing::instrument(level = "info", skip_all)]
pub unsafe fn deserialize_from_ptr_into_fallible<W>(
raw_allocated_ptr: *const u8,
raw_allocated_ptr_len: i32,
raw_allocated_ptr_len: u32,
) -> Result<W, Error>
where
W: rkyv::Archive,
Expand Down
24 changes: 12 additions & 12 deletions crates/swc_plugin_macro/src/lib.rs
Expand Up @@ -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
Expand All @@ -52,35 +52,35 @@ 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);
}
}
}


/// 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);
}
}

/// 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();

Expand All @@ -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
}
Expand All @@ -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) };
Expand Down Expand Up @@ -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
}
};
Expand Down
16 changes: 8 additions & 8 deletions crates/swc_plugin_proxy/src/comments/plugin_comments_proxy.rs
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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"),
Expand Down
Expand Up @@ -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: F) -> Option<AllocatedBytesPtr> {
Expand All @@ -31,7 +31,7 @@ fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr> {
#[tracing::instrument(level = "info", skip_all)]
fn read_returned_result_from_host_inner<F>(f: F) -> Option<AllocatedBytesPtr>
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);
Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn read_returned_result_from_host<F, R>(f: F) -> Option<R> {
#[tracing::instrument(level = "info", skip_all)]
pub fn read_returned_result_from_host<F, R>(f: F) -> Option<R>
where
F: FnOnce(i32) -> i32,
F: FnOnce(u32) -> u32,
R: rkyv::Archive,
R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>,
{
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn read_returned_result_from_host_fallible<F, R>(f: F) -> Option<R> {
#[tracing::instrument(level = "info", skip_all)]
pub fn read_returned_result_from_host_fallible<F, R>(f: F) -> Option<R>
where
F: FnOnce(i32) -> i32,
F: FnOnce(u32) -> u32,
R: rkyv::Archive,
R::Archived: rkyv::Deserialize<R, rkyv::de::deserializers::SharedDeserializeMap>,
{
Expand Down Expand Up @@ -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")
};
Expand Down
Expand Up @@ -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")]
Expand Down Expand Up @@ -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)
});
Expand Down
22 changes: 11 additions & 11 deletions crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs
Expand Up @@ -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,
Expand All @@ -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")]
Expand Down
14 changes: 7 additions & 7 deletions crates/swc_plugin_runner/src/imported_fn/comments.rs
Expand Up @@ -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<NativeFunc<u32, i32>>,
pub alloc_guest_memory: LazyInit<NativeFunc<u32, u32>>,
/// A buffer to `Comment`, or `Vec<Comment>` 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
Expand All @@ -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);
Expand Down Expand Up @@ -90,7 +90,7 @@ where
#[tracing::instrument(level = "info", skip_all)]
fn unwrap_comments_storage_with_env<F, R>(env: &CommentHostEnvironment, f: F, default: R) -> R
where
F: FnOnce(&SingleThreadedComments, &Memory, &NativeFunc<u32, i32>) -> R,
F: FnOnce(&SingleThreadedComments, &Memory, &NativeFunc<u32, u32>) -> R,
{
if let Some(memory) = env.memory_ref() {
if let Some(alloc_guest_memory) = env.alloc_guest_memory_ref() {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_plugin_runner/src/imported_fn/diagnostics.rs
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_plugin_runner/src/imported_fn/handler.rs
Expand Up @@ -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| {
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_plugin_runner/src/imported_fn/hygiene.rs
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand Down

1 comment on commit b6c1cc4

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: b6c1cc4 Previous: a923e52 Ratio
es/full/bugs-1 340507 ns/iter (± 17907) 339760 ns/iter (± 29667) 1.00
es/full/minify/libraries/antd 1863655862 ns/iter (± 56003689) 1862972713 ns/iter (± 118800862) 1.00
es/full/minify/libraries/d3 411384034 ns/iter (± 25531057) 402893150 ns/iter (± 9900855) 1.02
es/full/minify/libraries/echarts 1562424844 ns/iter (± 34045401) 1546061253 ns/iter (± 47313197) 1.01
es/full/minify/libraries/jquery 101189933 ns/iter (± 4517917) 113429340 ns/iter (± 6650594) 0.89
es/full/minify/libraries/lodash 117242678 ns/iter (± 4563856) 126340803 ns/iter (± 8244402) 0.93
es/full/minify/libraries/moment 57826477 ns/iter (± 4100557) 56536394 ns/iter (± 2372862) 1.02
es/full/minify/libraries/react 20069939 ns/iter (± 761860) 20891668 ns/iter (± 2308788) 0.96
es/full/minify/libraries/terser 304844979 ns/iter (± 15749843) 298594477 ns/iter (± 14043327) 1.02
es/full/minify/libraries/three 546624050 ns/iter (± 20459129) 534832770 ns/iter (± 7461951) 1.02
es/full/minify/libraries/typescript 3405926372 ns/iter (± 88625957) 3249927880 ns/iter (± 31259965) 1.05
es/full/minify/libraries/victory 836456936 ns/iter (± 43463794) 790459378 ns/iter (± 5084067) 1.06
es/full/minify/libraries/vue 159843024 ns/iter (± 9416817) 147892896 ns/iter (± 3984353) 1.08
es/full/codegen/es3 33417 ns/iter (± 772) 32342 ns/iter (± 4966) 1.03
es/full/codegen/es5 33125 ns/iter (± 1316) 32034 ns/iter (± 983) 1.03
es/full/codegen/es2015 33019 ns/iter (± 882) 31792 ns/iter (± 830) 1.04
es/full/codegen/es2016 33530 ns/iter (± 1236) 31843 ns/iter (± 568) 1.05
es/full/codegen/es2017 34081 ns/iter (± 4069) 32512 ns/iter (± 605) 1.05
es/full/codegen/es2018 33831 ns/iter (± 1627) 32445 ns/iter (± 737) 1.04
es/full/codegen/es2019 33212 ns/iter (± 1183) 32450 ns/iter (± 957) 1.02
es/full/codegen/es2020 33209 ns/iter (± 1433) 32533 ns/iter (± 4204) 1.02
es/full/all/es3 236646930 ns/iter (± 23890504) 192048262 ns/iter (± 4749812) 1.23
es/full/all/es5 222577057 ns/iter (± 18968585) 180742751 ns/iter (± 5640613) 1.23
es/full/all/es2015 180844961 ns/iter (± 23312187) 146059597 ns/iter (± 5082392) 1.24
es/full/all/es2016 179507832 ns/iter (± 22610410) 143688339 ns/iter (± 5966048) 1.25
es/full/all/es2017 180154149 ns/iter (± 25590067) 143697361 ns/iter (± 4233556) 1.25
es/full/all/es2018 180560493 ns/iter (± 20713056) 140383340 ns/iter (± 7355881) 1.29
es/full/all/es2019 170735608 ns/iter (± 20879977) 141239262 ns/iter (± 4895576) 1.21
es/full/all/es2020 172122850 ns/iter (± 19877185) 141377290 ns/iter (± 7092235) 1.22
es/full/parser 759710 ns/iter (± 153223) 722484 ns/iter (± 33752) 1.05
es/full/base/fixer 28068 ns/iter (± 1367) 26488 ns/iter (± 1332) 1.06
es/full/base/resolver_and_hygiene 97094 ns/iter (± 23236) 91343 ns/iter (± 1856) 1.06
serialization of ast node 218 ns/iter (± 10) 213 ns/iter (± 2) 1.02
serialization of serde 219 ns/iter (± 11) 213 ns/iter (± 1) 1.03

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.