{"record":{"id":"0b2131b2d90a4062","repo":"wasmerio/wasmer","slug":"global-is-a-constant","errorCode":null,"errorMessage":"global #{} is a constant","messagePattern":"global #(.+?) is a constant","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/compiler-cranelift/src/translator/code_translator.rs","lineNumber":231,"sourceCode":"                GlobalVariable::Const(val) => val,\n                GlobalVariable::Memory { gv, offset, ty } => {\n                    let addr =\n                        materialize_global_value(&mut builder.cursor(), environ.pointer_type(), gv);\n                    let mut flags = ir::MemFlagsData::trusted();\n                    // Put globals in the \"table\" abstract heap category as well.\n                    set_memflags_alias_region(builder.func, &mut flags, MemoryAliasRegion::Table);\n                    builder.ins().load(ty, flags, addr, offset)\n                }\n                GlobalVariable::Custom => environ.translate_custom_global_get(\n                    builder.cursor(),\n                    GlobalIndex::from_u32(*global_index),\n                )?,\n            };\n            state.push1(val);\n        }\n        Operator::GlobalSet { global_index } => {\n            match state.get_global(builder.func, *global_index, environ)? {\n                GlobalVariable::Const(_) => panic!(\"global #{} is a constant\", *global_index),\n                GlobalVariable::Memory { gv, offset, ty } => {\n                    let addr =\n                        materialize_global_value(&mut builder.cursor(), environ.pointer_type(), gv);\n                    let mut flags = ir::MemFlagsData::trusted();\n                    // Put globals in the \"table\" abstract heap category as well.\n                    set_memflags_alias_region(builder.func, &mut flags, MemoryAliasRegion::Table);\n                    let mut val = state.pop1();\n                    // Ensure SIMD values are cast to their default Cranelift type, I8x16.\n                    if ty.is_vector() {\n                        val = optionally_bitcast_vector(val, I8X16, builder);\n                    }\n                    debug_assert_eq!(ty, builder.func.dfg.value_type(val));\n                    builder.ins().store(flags, val, addr, offset);\n                }\n                GlobalVariable::Custom => {\n                    let val = state.pop1();\n                    environ.translate_custom_global_set(\n                        builder.cursor(),","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/wasmerio/wasmer/blob/8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5/lib/compiler-cranelift/src/translator/code_translator.rs#L213-L249","documentation":"During Wasm-to-Cranelift translation, code_translator handles Operator::GlobalSet. If state.get_global resolves the global index to GlobalVariable::Const, the global is a constant and cannot be assigned, so translation panics. Well-formed Wasm modules mark such globals immutable in their type section, so this usually indicates an invalid or corrupted module.","triggerScenarios":"Translating a function body (via parse_function_body → translate_operator) containing global.set whose index refers to a global recorded as Const — i.e. writing to an immutable global, typically from a module that bypassed validation or from a get_global/state tracking bug.","commonSituations":"Feeding hand-crafted or fuzzed Wasm binaries that skip the validator; toolchain bugs emitting global.set to immutable globals; old/hand-patched modules where the global's mutability flag disagrees with actual writes.","solutions":["Validate the module before compilation (wasm-validate app.wasm or Wasmer's validation pass) — a valid module can never do this.","Fix the producer toolchain so global.set only targets mutable (0x01 mutability) globals.","Change the global's declared mutability byte in the module if it is genuinely meant to be written.","If you believe the module is valid, report a Wasmer bug with the failing module attached."],"exampleFix":"// before (globals section, byte view)\n0x03 0x7F 0x00 0x41 0x00 0x0B  // global #3, i32, MUTABLE=0x00\n// after\n0x03 0x7F 0x01 0x41 0x00 0x0B  // global #3, i32, MUTABLE=0x01 (writable)","handlingStrategy":"validation","validationCode":"// Validate modules before compiling; a valid module can never hit this:\n$ wasm-validate app.wasm   # or via the wasm-tools/wasmparser validator in-pipeline","typeGuard":null,"tryCatchPattern":"// Wrap compilation of untrusted modules\nlet result = std::panic::catch_unwind(|| compiler.compile(module, environ));\nmatch result {\n    Ok(r) => r,\n    Err(_) => return Err(CompileError::InvalidModule(\"global.set on const global\")),\n}","preventionTips":["Run the Wasm validator on every untrusted or hand-built module before compilation.","Ensure codegen tools emit global.set only for mutable globals.","Fuzz your pipeline with the validator enabled to catch producer bugs."],"tags":["rust","panic","cranelift","wasm","validation","immutable-global"],"backgroundTag":"invalid-wasm-module","analyzedSha":"8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5","analyzedAt":"2026-09-01T23:06:31.009Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}