{"record":{"id":"0f95cbc7cca197f9","repo":"swc-project/swc","slug":"should-be-able-to-convert-to-i32","errorCode":null,"errorMessage":"Should be able to convert to i32","messagePattern":"Should be able to convert to i32","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_plugin_runner/src/memory_interop.rs","lineNumber":34,"sourceCode":"        .expect(\"Should able to read memory from given ptr\");\n}\n\n/// Locate a view from given memory, write serialized bytes into.\n#[cfg_attr(debug_assertions, tracing::instrument(level = \"info\", skip_all))]\npub fn write_into_memory_view<F>(\n    view: &mut dyn runtime::Caller<'_>,\n    serialized_bytes: &PluginSerializedBytes,\n    get_allocated_ptr: F,\n) -> (u32, u32)\nwhere\n    F: Fn(&mut dyn runtime::Caller<'_>, usize) -> u32,\n{\n    let serialized_len = serialized_bytes.as_slice().len();\n\n    let ptr_start = get_allocated_ptr(view, serialized_len);\n    let serialized_size = serialized_len\n        .try_into()\n        .expect(\"Should be able to convert to i32\");\n\n    // Note: it's important to get a view from memory _after_ alloc completes\n    view.write_buf(ptr_start, serialized_bytes.as_slice())\n        .expect(\"Should able to write into memory view\");\n\n    (ptr_start, serialized_size)\n}\n\n/// Set `return` value to pass into guest from functions returning values with\n/// non-deterministic size like `Vec<Comment>`. Guest pre-allocates a struct to\n/// contain ptr to the value, host in here allocates guest memory for the actual\n/// value then returns its ptr with length to the preallocated struct.\n#[cfg_attr(debug_assertions, tracing::instrument(level = \"info\", skip_all))]\npub fn allocate_return_values_into_guest(\n    caller: &mut dyn runtime::Caller<'_>,\n    allocated_ret_ptr: u32,\n    serialized_bytes: &PluginSerializedBytes,\n) {","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_plugin_runner/src/memory_interop.rs#L16-L52","documentation":"In write_into_memory_view, the byte length of the serialized AST (usize) is converted with try_into() into the 32-bit size type returned to the guest. The expect fires when the serialized payload exceeds u32::MAX (~4 GiB), i.e. the program handed to a plugin is larger than the wasm pointer-size trampoline can express. It is a host-side panic, not a graceful error.","triggerScenarios":"Calling a plugin transform (or a host import that writes into the guest) with a serialized AST whose rkyv/serialized byte length is greater than 4 GiB on a 64-bit host.","commonSituations":"Feeding a monolithic, machine-generated or concatenated bundle into a single plugin transform instead of per-module transforms; pathological inputs such as a giant generated data module.","solutions":["Split the input: transform per file/module so each serialized AST stays far below 4 GiB (SWC is designed for per-module transforms).","Exclude multi-megabyte generated blobs (data URLs, base64 assets) from plugin processing.","If genuinely required, report an upstream issue - the limit is inherent to the 32-bit wasm pointer ABI."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Guard at the input boundary: serialized ASTs are roughly proportional to\n// source size; keep per-transform inputs far below the 4 GiB wasm limit.\nfn assert_transformable_size(src_len: usize) -> anyhow::Result<()> {\n    const LIMIT: usize = 512 * 1024 * 1024; // conservative safety margin\n    if src_len > LIMIT {\n        anyhow::bail!(\"input of {src_len} bytes is too large for a single plugin transform; split it\");\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Transform per module instead of feeding monolithic bundles through plugins.","Exclude multi-megabyte generated files (base64/data modules) from plugin processing.","Alert on any single source file approaching hundreds of MB in your build pipeline."],"tags":["integer-overflow","plugin","serialization","panic"],"backgroundTag":"integer-conversion-overflow","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}