{"id":"920ed9e2270df33d","repo":"rust-lang/rust","slug":"got-a-non-utf8-data-layout-from-llvm","errorCode":null,"errorMessage":"got a non-UTF8 data-layout from LLVM","messagePattern":"got a non-UTF8 data-layout from LLVM","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_codegen_llvm/src/context.rs","lineNumber":238,"sourceCode":"    }\n    if llvm_version < (23, 0, 0) {\n        if sess.target.arch == Arch::S390x {\n            // LLVM 23 updated the s390x layout to specify the stack alignment: https://github.com/llvm/llvm-project/pull/176041\n            target_data_layout = target_data_layout.replace(\"-S64\", \"\");\n        }\n    }\n\n    // Ensure the data-layout values hardcoded remain the defaults.\n    {\n        let tm = crate::back::write::create_informational_target_machine(sess, false);\n        unsafe {\n            llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm.raw());\n        }\n\n        let llvm_data_layout = unsafe { llvm::LLVMGetDataLayoutStr(llmod) };\n        let llvm_data_layout =\n            str::from_utf8(unsafe { CStr::from_ptr(llvm_data_layout) }.to_bytes())\n                .expect(\"got a non-UTF8 data-layout from LLVM\");\n\n        if target_data_layout != llvm_data_layout {\n            tcx.dcx().emit_err(crate::diagnostics::MismatchedDataLayout {\n                rustc_target: sess.opts.target_triple.to_string().as_str(),\n                rustc_layout: target_data_layout.as_str(),\n                llvm_target: sess.target.llvm_target.borrow(),\n                llvm_layout: llvm_data_layout,\n            });\n        }\n    }\n\n    let data_layout = SmallCStr::new(&target_data_layout);\n    unsafe {\n        llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());\n    }\n\n    let llvm_target = SmallCStr::new(&versioned_llvm_target(sess));\n    unsafe {","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_codegen_llvm/src/context.rs#L220-L256","documentation":"This panic occurs in `context.rs` when rustc reads the target data-layout string back from the LLVM module via `LLVMGetDataLayoutStr(llmod)` and converts the raw `CStr` bytes to a Rust `&str`. `str::from_utf8(...).expect(\"got a non-UTF8 data-layout from LLVM\")` panics if the bytes are not valid UTF-8. The data-layout is fundamental to ABI correctness, so a malformed string is treated as a fatal LLVM/target inconsistency rather than silently propagated.","triggerScenarios":"After `LLVMRustSetDataLayoutFromTargetMachine` writes the layout into the module, rustc reads it back (context.rs:235-238) and asserts UTF-8 validity. The panic fires only if LLVM returns bytes that are not valid UTF-8 — e.g. a corrupted target triple, a target whose data-layout was misconfigured in LLVM, or a buggy/custom LLVM build that emits garbage for the triple.","commonSituations":"Using an unusual or mistyped `--target` triple whose LLVM target has no valid data-layout; a custom LLVM build that returns a non-null-terminated or invalid string; memory corruption from an FFI mismatch; running rustc against an incompatible LLVM shared library version.","solutions":["Verify the `--target` triple is valid and supported by both rustc and the linked LLVM.","Use the rustc toolchain's bundled LLVM rather than a system LLVM of a different version.","Rebuild rustc from source so the `rustc_codegen_llvm` crate matches the LLVM it links against.","Reproduce with `-Zverbose` and report the offending triple/data-layout to the rustc issue tracker if it persists on a stock toolchain."],"exampleFix":"// before\nrustc --target=x86_64-unknown-linux-gn-   # typo in triple\n// after\nrustc --target=x86_64-unknown-linux-gnu","handlingStrategy":"validation","validationCode":"# The data-layout string returned by LLVM is not valid UTF-8, which means the\n# LLVM/rustc linkage is corrupt or ABI-incompatible. Probe before building.\nrustc -vV >/dev/null 2>&1 || { echo 'rustc binary broken'; exit 1; }\n# A self-consistent toolchain can print its target's data layout as UTF-8.\nrustc --print=data-layout 2>&1 | LC_ALL=C grep -Eq '^[A-Za-z0-9-]+$' \\\n  || { echo 'data-layout probe failed; toolchain/LLVM mismatch'; exit 1; }","typeGuard":null,"tryCatchPattern":"out=\"$(cargo build 2>&1)\"; rc=$?\nif [ $rc -ne 0 ]; then\n  case \"$out\" in\n    *\"got a non-UTF8 data-layout from LLVM\"*)\n      echo \"rustc/LLVM ABI mismatch; reinstall the toolchain via rustup\" >&2\n      rustup toolchain uninstall \"$(rustup show active-toolchain | awk '{print $1}')\" 2>/dev/null\n      echo \"then: rustup toolchain install stable\" >&2 ;;\n    *) echo \"$out\" >&2 ;;\n  esac\n  exit $rc\nfi","preventionTips":["Never mix a rustc binary from one manifest with libLLVM from another; always install both via the same rustup release.","Avoid LD_LIBRARY_PATH / DYLD_LIBRARY_PATH overrides that point rustc at a foreign libLLVM.","Reinstall the toolchain (rustup toolchain uninstall && install) if a system upgrade touched LLVM libraries.","Pin the toolchain in rust-toolchain.toml so CI and teammates cannot pick up a drifted LLVM.","If you build rustc yourself, link against the exact LLVM revision listed in src/llvm-project."],"tags":["llvm","target","data-layout","ffi","panic","abi"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}