wasmerio/wasmer · error

`wasm_valtype_kind: argument is a null pointer

Error message

`wasm_valtype_kind: argument is a null pointer

What it means

wasm_valtype_kind is part of the C API and expects a non-null pointer to a wasm_valtype_t. Passing NULL is undefined behavior in the C API contract, so the Rust implementation panics with this message via .expect(). It is a defensive guard against misuse of the C ABI.

Source

Thrown at lib/c-api/src/wasm_c_api/types/value.rs:98

        }
    }
}

#[unsafe(no_mangle)]
pub extern "C" fn wasm_valtype_new(kind: wasm_valkind_t) -> Option<Box<wasm_valtype_t>> {
    let kind_enum = kind.try_into().ok()?;
    let valtype = wasm_valtype_t { valkind: kind_enum };

    Some(Box::new(valtype))
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn wasm_valtype_delete(_valtype: Option<Box<wasm_valtype_t>>) {}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn wasm_valtype_kind(valtype: Option<&wasm_valtype_t>) -> wasm_valkind_t {
    valtype
        .expect("`wasm_valtype_kind: argument is a null pointer")
        .valkind as wasm_valkind_t
}

View on GitHub (pinned to 8c4b9ee9d3)

Solutions

  1. Check the pointer for NULL before calling wasm_valtype_kind
  2. Ensure wasm_valtype_new succeeded and the valtype was not deleted before use
  3. Recreate the valtype if it was consumed or freed
  4. Enable address sanitizer/valgrind to find where the pointer became null

Example fix

// before
wasm_valkind_t k = wasm_valtype_kind(valtype);
// after
if (valtype != NULL) {
    wasm_valkind_t k = wasm_valtype_kind(valtype);
} else {
    // handle missing valtype
}
Defensive patterns

Strategy: type-guard

Validate before calling

// C caller
if (valtype == NULL) { /* handle error */ return 0; }

Type guard

// in Rust bindings
fn is_valid(valtype: Option<&wasm_valtype_t>) -> bool { valtype.is_some() }

Try / catch

// Wrap C-API FFI calls at the binding layer
let kind = std::panic::catch_unwind(|| unsafe { wasm_valtype_kind(valtype) })
    .map_err(|_| anyhow::anyhow!("null valtype passed to wasm_valtype_kind"))?;

Prevention

When it happens

Trigger: Calling wasm_valtype_kind(NULL) from C/C++ code, e.g. after wasm_valtype_new failed or a valtype was already deleted/freed and the pointer set to null.

Common situations: C embedders building Wasm types programmatically; uninitialized or double-freed valtype pointers; binding generators passing optional/null values through.

Related errors


AI-assisted analysis of wasmerio/wasmer@8c4b9ee9d3 (2026-09-01). Data as JSON: /api/errors/98a97d142d26882f. Report an issue: GitHub.