Issue 233456
Summary [lldb] Rust enum `$discr$` gets its byte offset as a bit-field width, so niche-encoded `None` is shown as `Some`
Labels new issue
Assignees
Reporter SiddharthPant
    When LLDB builds the Clang type for a DWARF `DW_TAG_variant_part`, it adds the discriminant field like this:

```cpp
m_ast.AddFieldToRecordType(field_type, "$discr$",
 discriminant_type->GetFullCompilerType(),
 variants.discriminant().byte_offset);
```

https://github.com/llvm/llvm-project/blob/3546bc68b3b37d6554e13a84540845444ff5774f/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp#L3855-L3857

The last parameter of `AddFieldToRecordType` is `uint32_t bitfield_bit_size`:

https://github.com/llvm/llvm-project/blob/3546bc68b3b37d6554e13a84540845444ff5774f/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h#L962-L965

So the discriminant's **byte offset becomes its bit-field width**, and the field is laid out at offset 0 instead of at its real offset. When the discriminant isn't at offset 0, LLDB reads the wrong bits. Rust's formatters compare `$discr$` with the `$variant$N` names to pick a variant, so they choose the wrong one. For a niche-optimised `Option`, that means `None` is displayed as `Some(...)`, built from whatever bytes are left in the unused payload. Enums whose discriminant sits at offset 0 (such as `Option<&T>` and `Option<Box<T>>`) aren't affected, which is probably why this has gone unnoticed.

The `value` field just below passes `offset * 8` into the same parameter, so `value` also becomes a bit-field (see `:32` in the output below):

https://github.com/llvm/llvm-project/blob/3546bc68b3b37d6554e13a84540845444ff5774f/lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp#L3863-L3864

### Reproducer

```rust
use std::hint::black_box;

fn main() {
    let none: Option<(u64, char)> = black_box(None);
    let some: Option<(u64, char)> = black_box(Some((7, 'x')));
    println!("{none:?} {some:?}"); // prints "None Some((7, 'x'))"
}
```

`cargo build` (debug), then in LLDB break on the `println!` line.

DWARF that rustc emits for the type (`dwarfdump --debug-info`), showing the discriminant is a `u32` at offset 8 and `None` is `0xffffffff`:

```
DW_AT_name ("Option<(u64, char)>")
DW_TAG_variant_part
  DW_AT_discr (-> member below)
  DW_TAG_member
    DW_AT_type              ("u32")
 DW_AT_data_member_location (0x08)
  DW_TAG_variant
    DW_AT_discr_value (0xffffffff)      // None
  DW_TAG_variant // Some (default)
```

LLDB without formatters reads an 8-bit `$discr$` at offset 0:

```
(lldb) frame variable --raw-output --show-types --location none
0x000000016fdfe000: (core::option::Option<(u64, char)>) none = {
0x000000016fdfe000:   (...$Inner) $variants$ = {
0x000000016fdfe000: (...$Inner::None$Variant) $variant$4294967295 = {
0x000000016fdfe000: (unsigned int:8) $discr$ = 228
0x000000016fdfe000: (core::option::Option<(u64, char)>::None<(u64, char)>:32) value = {}
    }
 ...
(lldb) memory read --size 4 --format x --count 4 &none
0x16fdfe000: 0x00000be4 0x00000001 0xffffffff 0x00000001
```

The real discriminant at offset 8 is `0xffffffff` (`None`), but `$discr$` is the low byte of offset 0 (`0xe4` = 228, leftover stack data). With the Rust formatters loaded (`rust-lldb`):

```
(lldb) frame variable none some
(core::option::Option<(u64, char)>) none = Some((4294970340, 0xffffffff U'\U0000fffd'))
(core::option::Option<(u64, char)>) some = Some((7, U+0078 U'x'))
```

**Expected:** `$discr$` is a 32-bit field at offset 8 reading `0xffffffff`, which matches `$variant$4294967295`, so `none` displays as `None`.

### Impact

Once the wrong variant is picked, the formatters summarise uninitialised payload memory. In a real program, a `&str` inside such a payload had a garbage length of about 6.2 GB, and the debugger hung and ran out of memory. (I'm reporting the formatter side to rust-lang/rust separately.) Related: #177812 / #188487 (discriminant width, separate fix), and rust-lang/rust#152743 (`None` shown as `Some`, open with no repro so far).

### Versions

- Apple LLDB `lldb-2103.0.34.103` (Xcode) and LLVM LLDB 22.1.8 (bundled with CodeLLDB 1.12.3): both reproduce
- The code is unchanged on `main` at 3546bc68b3b3
- rustc 1.99.1 (58cf8f9c3 2026-10-13), aarch64-apple-darwin, macOS 26.5

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to