{"record":{"id":"a37663ff4ae955a4","repo":"wasmerio/wasmer","slug":"cannot-load-reference-type","errorCode":null,"errorMessage":"Cannot load reference type","messagePattern":"Cannot load reference type","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"lib/wasix/src/syscalls/wasix/closure_prepare.rs","lineNumber":113,"sourceCode":"                memory_index,\n            }),\n            Self::F32 => sink.f32_load(MemArg {\n                offset,\n                align: 0,\n                memory_index,\n            }),\n            Self::F64 => sink.f64_load(MemArg {\n                offset,\n                align: 0,\n                memory_index,\n            }),\n            Self::V128 => sink.v128_load(MemArg {\n                offset,\n                align: 0,\n                memory_index,\n            }),\n            // Not supported in closures\n            Self::Ref(_) => panic!(\"Cannot load reference type\"),\n        };\n    }\n}\n\n/// Build a dynamically linkable WASM module for the given closure.\nfn build_closure_wasm_bytes(\n    module_name: &str,\n    closure: u32,\n    backing_function: u32,\n    environment_offset: u64,\n    argument_types: &[ValType],\n    result_types: &[ValType],\n) -> Vec<u8> {\n    let mut wasm_module = wasm_encoder::Module::new();\n\n    // Add dylink section\n    let dylink = CustomSection {\n        name: Cow::Borrowed(\"dylink.0\"),","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/wasmerio/wasmer/blob/8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5/lib/wasix/src/syscalls/wasix/closure_prepare.rs#L95-L131","documentation":"During closure_prepare, the WASIX runtime synthesizes a trampoline WASM module whose instructions load/store closure environment fields of a given value type. The ValTypeOps::load helper panics with \"Cannot load reference type\" because reference types (funcref/externref) cannot be loaded from linear memory. It is an internal invariant panic, not a recoverable error.","triggerScenarios":"Calling closure_prepare (via the wasix closure_prepare syscall) with a closure whose declared argument or result types include a reference type (Ref/Funcref/Externref) that was not rejected earlier by ValTypeOps::from_u8.","commonSituations":"Passing funcref or externref parameters when registering a closure with closure_allocate/closure_prepare; using a toolchain that emits reference-typed closure signatures; newer WasmValueType encodings slipping past the from_u8 validation path.","solutions":["Change the closure signature so all argument and result types are I32, I64, F32, F64, or V128 only.","If a reference must be passed, store it in an externref table and pass the table index as an i32 instead.","If you control the runtime, extend ValTypeOps::load/store to handle Ref types via table.get/table.set instead of panicking.","Recompile the guest code that declares the closure with reference types removed from the signature."],"exampleFix":"// before (guest side)\nclosure_allocate(&sig_with_funcref_param);\n// after\nclosure_allocate(&sig_with_i32_table_index_param); // pass table index instead of funcref","handlingStrategy":"validation","validationCode":"fn closure_types_supported(arg_types: &[wasm_encoder::ValType], res_types: &[wasm_encoder::ValType]) -> bool {\n    let ok = |t: &wasm_encoder::ValType| !matches!(t, wasm_encoder::ValType::Ref(_));\n    arg_types.iter().all(ok) && res_types.iter().all(ok)\n}","typeGuard":"fn is_reference_type(t: &wasm_encoder::ValType) -> bool {\n    matches!(t, wasm_encoder::ValType::Ref(_))\n}","tryCatchPattern":null,"preventionTips":["Declare closures using only numeric and vector value types (i32/i64/f32/f64/v128).","Pass references via an externref/funcref table index instead of by value.","Check the WasmValueType rejected by from_u8 (Errno::Inval) before preparing the closure."],"tags":["wasix","panic","closures","wasm","reference-types"],"backgroundTag":"unsupported-wasm-reference-type","analyzedSha":"8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5","analyzedAt":"2026-09-01T23:06:31.009Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}