swc-project/swc · error

Should be able to convert to i32

Error message

Should be able to convert to i32

What it means

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.

Source

Thrown at crates/swc_plugin_runner/src/memory_interop.rs:34

        .expect("Should able to read memory from given ptr");
}

/// Locate a view from given memory, write serialized bytes into.
#[cfg_attr(debug_assertions, tracing::instrument(level = "info", skip_all))]
pub fn write_into_memory_view<F>(
    view: &mut dyn runtime::Caller<'_>,
    serialized_bytes: &PluginSerializedBytes,
    get_allocated_ptr: F,
) -> (u32, u32)
where
    F: Fn(&mut dyn runtime::Caller<'_>, usize) -> u32,
{
    let serialized_len = serialized_bytes.as_slice().len();

    let ptr_start = get_allocated_ptr(view, serialized_len);
    let serialized_size = serialized_len
        .try_into()
        .expect("Should be able to convert to i32");

    // Note: it's important to get a view from memory _after_ alloc completes
    view.write_buf(ptr_start, serialized_bytes.as_slice())
        .expect("Should able to write into memory view");

    (ptr_start, serialized_size)
}

/// Set `return` value to pass into guest from functions returning values with
/// non-deterministic size like `Vec<Comment>`. Guest pre-allocates a struct to
/// contain ptr to the value, host in here allocates guest memory for the actual
/// value then returns its ptr with length to the preallocated struct.
#[cfg_attr(debug_assertions, tracing::instrument(level = "info", skip_all))]
pub fn allocate_return_values_into_guest(
    caller: &mut dyn runtime::Caller<'_>,
    allocated_ret_ptr: u32,
    serialized_bytes: &PluginSerializedBytes,
) {

View on GitHub (pinned to 5176682b65)

Solutions

  1. Split the input: transform per file/module so each serialized AST stays far below 4 GiB (SWC is designed for per-module transforms).
  2. Exclude multi-megabyte generated blobs (data URLs, base64 assets) from plugin processing.
  3. If genuinely required, report an upstream issue - the limit is inherent to the 32-bit wasm pointer ABI.
Defensive patterns

Strategy: validation

Validate before calling

// Guard at the input boundary: serialized ASTs are roughly proportional to
// source size; keep per-transform inputs far below the 4 GiB wasm limit.
fn assert_transformable_size(src_len: usize) -> anyhow::Result<()> {
    const LIMIT: usize = 512 * 1024 * 1024; // conservative safety margin
    if src_len > LIMIT {
        anyhow::bail!("input of {src_len} bytes is too large for a single plugin transform; split it");
    }
    Ok(())
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/0f95cbc7cca197f9. Report an issue: GitHub.