{"id":"eeea581f847005fe","repo":"rust-lang/rust","slug":"const-alloc-to-gcc-uncached-could-not-read-reloca","errorCode":null,"errorMessage":"const_alloc_to_gcc_uncached: could not read relocation pointer","messagePattern":"const_alloc_to_gcc_uncached: could not read relocation pointer","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_codegen_gcc/src/consts.rs","lineNumber":347,"sourceCode":"        let offset = offset as usize;\n        if offset > next_offset {\n            // This `inspect` is okay since we have checked that it is not within a pointer with provenance, it\n            // is within the bounds of the allocation, and it doesn't affect interpreter execution\n            // (we inspect the result after interpreter execution). Any undef byte is replaced with\n            // some arbitrary byte value.\n            //\n            // FIXME: relay undef bytes to codegen as undef const bytes\n            let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(next_offset..offset);\n            llvals.push(cx.const_bytes(bytes));\n        }\n        let ptr_offset = read_target_uint(\n            dl.endian,\n            // This `inspect` is okay since it is within the bounds of the allocation, it doesn't\n            // affect interpreter execution (we inspect the result after interpreter execution),\n            // and we properly interpret the provenance as a relocation pointer offset.\n            alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size)),\n        )\n        .expect(\"const_alloc_to_gcc_uncached: could not read relocation pointer\")\n            as u64;\n\n        let address_space = cx.tcx.global_alloc(alloc_id).address_space(cx);\n\n        llvals.push(cx.scalar_to_backend(\n            InterpScalar::from_pointer(\n                interpret::Pointer::new(prov, Size::from_bytes(ptr_offset)),\n                &cx.tcx,\n            ),\n            abi::Scalar::Initialized {\n                value: Primitive::Pointer(address_space),\n                valid_range: WrappingRange::full(dl.pointer_size()),\n            },\n            cx.type_i8p_ext(address_space),\n        ));\n        next_offset = offset + pointer_size;\n    }\n    if alloc.len() >= next_offset {","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_codegen_gcc/src/consts.rs#L329-L365","documentation":"`const_alloc_to_gcc_uncached` (consts.rs:315-348) walks an allocation's provenance pointers and, for each, reads `pointer_size` bytes as a target-endian integer via `read_target_uint(dl.endian, ...).expect(\"const_alloc_to_gcc_uncached: could not read relocation pointer\")` at line 347. The panic means those bytes could not be decoded into a `u64` relocation offset. This is an interpreter/codegen contract violation: the provenance range must contain exactly a pointer-sized, endian-correct integer.","triggerScenarios":"Reached when an allocation has provenance (`alloc.provenance().ptrs()` non-empty) and `read_target_uint` overflows or otherwise fails on the bytes at `[offset, offset+pointer_size)`. Concretely: pointer-size mismatch (e.g. the data layout's `pointer_size` differs from what the interpreter wrote), a truncated/corrupt allocation buffer, or an unsupported relocation encoding for the target's endianness.","commonSituations":"Cross-compiling between pointer-width targets (32 vs 64-bit) with a misconfigured target spec / data layout; using a rustc version whose const-eval writes provenance in a shape this backend doesn't expect; corrupted incremental-cache artifacts; custom targets with non-standard pointer sizes.","solutions":["Clean incremental artifacts (`cargo clean` / remove `target/`) and rebuild — a stale corrupt allocation cache is a cheap first check.","Verify the target triple and data layout match the pointer width the code was compiled for; a 64-bit target with a 32-bit-pointer data layout (or vice versa) is the usual cause.","Reproduce against the rustc version this rustc_codegen_gcc checkout is pinned to; provenance encoding has changed across rustc releases.","If you control the target spec, confirm `data_layout` pointer size and endianness are correct; dump the bytes at the failing offset and compare to the expected relocation value."],"exampleFix":"// before (consts.rs:340-348)\nlet ptr_offset = read_target_uint(\n    dl.endian,\n    alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size)),\n)\n.expect(\"const_alloc_to_gcc_uncached: could not read relocation pointer\") as u64;\n\n// after (diagnose pointer-size / endianness mismatch instead of opaque panic)\nlet reloc_bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(offset..(offset + pointer_size));\nlet ptr_offset = read_target_uint(dl.endian, reloc_bytes).unwrap_or_else(|e| {\n    panic!(\"relocation read failed at offset {offset}: pointer_size={pointer_size}, endian={:?}, bytes={reloc_bytes:?}, err={e:?}\", dl.endian);\n}) as u64;","handlingStrategy":"validation","validationCode":"// consts.rs:347 -> read_target_uint over the relocation slot panics if the\n// byte slice is shorter than pointer_size or the offset is out of bounds.\n// Validate the allocation window before const lowering:\nfn reloc_slot_ok(alloc_bytes: &[u8], offset: usize, ptr_size: usize) -> bool {\n    match offset.checked_add(ptr_size) {\n        Some(end) => end <= alloc_bytes.len(),\n        None => false,\n    }\n}\n// Also confirm dl.endian and pointer_size match the target data layout.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat this panic as a malformed const allocation: the relocation offset points past the alloc, or pointer_size is wrong for the target.","Verify the target pointer size and endianness match the allocation's data layout before lowering a const with relocations.","Reproduce the same const under miri or LLVM first; if they accept it, file an upstream bug with the allocation dump."],"tags":["rustc-codegen-gcc","consts","allocation","provenance","relocation","panic"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}