{"record":{"id":"a903647adbe924e1","repo":"wasmerio/wasmer","slug":"global-index-must-be-valid","errorCode":null,"errorMessage":"Global index must be valid","messagePattern":"Global index must be valid","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/compiler/src/translator/sections.rs","lineNumber":296,"sourceCode":"\nfn parse_serialized_init_expr(\n    expr: &wasmparser::ConstExpr<'_>,\n    section_name: &str,\n    module: &ModuleInfo,\n) -> WasmResult<InitExpr> {\n    let mut reader = expr.get_operators_reader();\n    let mut ops = Vec::new();\n    loop {\n        let op = reader.read().map_err(from_binaryreadererror_wasmerror)?;\n        match op {\n            Operator::End => break,\n            Operator::I32Const { value } => ops.push(InitExprOp::I32Const(value)),\n            Operator::I64Const { value } => ops.push(InitExprOp::I64Const(value)),\n            Operator::GlobalGet { global_index } => {\n                let global_index = GlobalIndex::from_u32(global_index);\n                let global_type = module\n                    .global_type(global_index)\n                    .expect(\"Global index must be valid\");\n\n                match global_type.ty {\n                    Type::I32 => ops.push(InitExprOp::GlobalGetI32(global_index)),\n                    Type::I64 => ops.push(InitExprOp::GlobalGetI64(global_index)),\n                    other => {\n                        return Err(wasm_unsupported!(\n                            \"unsupported init expr in {section_name}: global.get type must be i32 or i64, got {other:?}\",\n                        ));\n                    }\n                }\n            }\n            Operator::I32Add => ops.push(InitExprOp::I32Add),\n            Operator::I32Sub => ops.push(InitExprOp::I32Sub),\n            Operator::I32Mul => ops.push(InitExprOp::I32Mul),\n            Operator::I64Add => ops.push(InitExprOp::I64Add),\n            Operator::I64Sub => ops.push(InitExprOp::I64Sub),\n            Operator::I64Mul => ops.push(InitExprOp::I64Mul),\n            other => {","sourceCodeStart":278,"sourceCodeEnd":314,"githubUrl":"https://github.com/wasmerio/wasmer/blob/8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5/lib/compiler/src/translator/sections.rs#L278-L314","documentation":"When parsing a serialized init expression (global initializer, element or data offset), a global.get operand must reference an existing imported or defined global. The translator .expect()s module.global_type(idx) to succeed; an out-of-range index means the module (or its serialization) is malformed, and the translator panics instead of returning a validation error.","triggerScenarios":"Compiling a module whose global.get in an init expression (global initializer, element offset, or data offset) refers to a global index beyond the declared globals — i.e. an invalid or corrupted Wasm binary, or a deserialized artifact whose module metadata is out of sync.","commonSituations":"Loading corrupted/truncated .wasm files or hand-patched modules; loading compiled artifacts (serde-serialized modules) produced by a different wasmer version where indices shifted.","solutions":["Validate the module first with wasm-validate / wasm-tools validate to catch the bad global index","Re-obtain the module from a trusted source; the binary is likely corrupted or maliciously malformed","Regenerate compiled artifacts with the same wasmer version (don't load serialized modules across versions)","Upgrade wasmer so malformed init expressions become a proper validation error instead of a panic"],"exampleFix":"// before: compile unvalidated bytes\nlet module = Module::new(&store, wasm_bytes)?;\n// after\nlet _ = wasmparser::Validator::new().validate_all(&wasm_bytes)?;\nlet module = Module::new(&store, wasm_bytes)?;","handlingStrategy":"validation","validationCode":"// Validate the module before compiling; invalid global.get indices fail validation\nwasmparser::Validator::new()\n    .validate_all(&wasm_bytes)\n    .map_err(|e| anyhow!(\"invalid module: {e}\"))?;","typeGuard":"fn module_is_valid(bytes: &[u8]) -> bool {\n    wasmparser::Validator::new().validate_all(bytes).is_ok()\n}","tryCatchPattern":"std::panic::catch_unwind(|| Module::deserialize(&store, &artifact_bytes))\n    .map_err(|_| anyhow::anyhow!(\"artifact/module has malformed init expressions\"))?","preventionTips":["Run wasm-validate on every untrusted Wasm binary before compilation","Never load serialized artifacts across different wasmer versions","Checksum compiled artifacts to detect corruption in storage/transfer","Reject hand-patched binaries; recompile from source instead"],"tags":["panic","compiler","wasm","validation","init-expr"],"backgroundTag":"invalid-global-index","analyzedSha":"8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5","analyzedAt":"2026-09-01T23:06:31.009Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}