{"record":{"id":"f4fe6096577ddabc","repo":"swc-project/swc","slug":"should-able-to-allocate-memory-in-the-plugin","errorCode":null,"errorMessage":"Should able to allocate memory in the plugin","messagePattern":"Should able to allocate memory in the plugin","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/swc_plugin_runner/src/memory_interop.rs","lineNumber":67,"sourceCode":"    caller: &mut dyn runtime::Caller<'_>,\n    allocated_ret_ptr: u32,\n    serialized_bytes: &PluginSerializedBytes,\n) {\n    let serialized_bytes_len: usize = serialized_bytes.as_slice().len();\n    let serialized_bytes_len = serialized_bytes_len\n        .try_into()\n        .expect(\"Should be able to convert size\");\n\n    // In most cases our host-plugin trampoline works in a way that\n    // plugin pre-allocates\n    // memory before calling host imported fn. But in case of\n    // comments return value is Vec<Comments> which\n    // guest cannot predetermine size to allocate, instead\n    // let host allocate by calling guest's alloc via attached\n    // hostenvironment.\n    let guest_memory_ptr = caller\n        .alloc(serialized_bytes_len)\n        .expect(\"Should able to allocate memory in the plugin\");\n\n    let (allocated_ptr, allocated_ptr_len) =\n        write_into_memory_view(caller, serialized_bytes, |_, _| guest_memory_ptr);\n\n    // We cannot use cbor serialization because it is a variable-length encoding.\n    let allocated_fatptr = {\n        let allocated_ptr = allocated_ptr.to_le_bytes();\n        let allocated_ptr_len = allocated_ptr_len.to_le_bytes();\n        [\n            allocated_ptr[0],\n            allocated_ptr[1],\n            allocated_ptr[2],\n            allocated_ptr[3],\n            allocated_ptr_len[0],\n            allocated_ptr_len[1],\n            allocated_ptr_len[2],\n            allocated_ptr_len[3],\n        ]","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_plugin_runner/src/memory_interop.rs#L49-L85","documentation":"For return values the guest cannot pre-size (Vec<Comment> via the comments proxy), the host calls back into the plugin's exported allocator with caller.alloc(len). The expect panics when that guest alloc call traps: the plugin's wasm heap is exhausted (memory.grow failed against wasm memory limits) or the plugin module does not export the allocator ABI the host runtime expects.","triggerScenarios":"allocate_return_values_into_guest with comments proxy enabled and a large comment return; plugin built with a swc_core whose allocator export signature differs from the host swc_plugin_runner; guest already near its 4 GiB wasm ceiling.","commonSituations":"Version mismatch between plugin and host; comment-heavy inputs; plugins that allocate aggressively and fragment the guest heap before returning comments.","solutions":["Rebuild the plugin with the swc_core version matching the host and the official plugin macro (which exports the required allocator).","Lower comment volume or disable the comments proxy for the affected files.","Wrap plugin execution in catch_unwind or a worker process so allocation traps are reported per file instead of aborting the build.","Update both host and plugin together to a matched release line."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Guest alloc traps surface as host panics here; isolate and degrade.\nlet res = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {\n    compiler.process_js_file(&fm, &handler, &config)\n}));\nif res.is_err() {\n    tracing::error!(\"plugin allocator trapped (likely guest OOM); skipping plugins for this file\");\n}","preventionTips":["Ensure plugins are built with the official plugin macro so the allocator export matches the host ABI.","Keep host swc and plugin swc_core on the same release line; upgrade them together.","Limit comment-heavy inputs or disable the comments proxy where possible.","Run each plugin in a subprocess so guest OOM cannot take down the compiler."],"tags":["wasm","plugin","allocation","oom","panic"],"backgroundTag":"wasm-out-of-memory","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}