Issue 237909
Summary [AArch64][HWASan] classifyGlobalReference returns MO_TAGGED under -mcmodel=large / tiny, causing invalid movk/adr fixups
Labels new issue
Assignees
Reporter zjy-dev
    On AArch64 with `-fsanitize=hwaddress`, `AArch64Subtarget::classifyGlobalReference` returns `AArch64II::MO_TAGGED`
without checking the active code model. Only the `small` code model (`getAddr`) handles `MO_TAGGED`: under
`-mcmodel=large`, `getAddrLarge` ORs `MO_TAGGED` (`5`) with `MO_G3` (`3`), corrupting the 4-bit fragment enum into
`MO_GOT_PAGE` (`7`) and emitting `movk x0, #:gotpage_lo15:g_var`, which the integrated assembler rejects. Under
`-mcmodel=tiny`, `getAddrTiny` passes `MO_TAGGED` to `ADR`, emitting invalid `adr x0, :prel_g3:g_var`.

```c
static int g_var = 42;
int *get_tagged_global(void) {
  return &g_var;
}
```

Compile flag: `clang --target=aarch64-unknown-linux-gnu -O2 -fno-pic -fsanitize=hwaddress -mcmodel=large -c trigger.c`

```text
$ clang --target=aarch64-unknown-linux-gnu -O2 -fno-pic -fsanitize=hwaddress -mcmodel=large -c trigger.c
trigger.c:2:5: error: invalid fixup for movz/movk instruction

$ clang --target=aarch64-unknown-linux-gnu -O2 -fno-pic -fsanitize=hwaddress -mcmodel=large -S trigger.c -o -
get_tagged_global:
	movz	x0, #:abs_g0_nc:_ZL5g_var
	movk	x0, #:abs_g1_nc:_ZL5g_var
	movk	x0, #:abs_g2_nc:_ZL5g_var
	movk	x0, #:gotpage_lo15:_ZL5g_var    // MO_TAGGED (5) | MO_G3 (3) = MO_GOT_PAGE (7)
	ret
```

**Root cause.** `AArch64Subtarget::classifyGlobalReference` (`llvm/lib/Target/AArch64/AArch64Subtarget.cpp:467-473`)
returns `AArch64II::MO_TAGGED` whenever `allowTaggedGlobals() && !isa<FunctionType>(GV->getValueType())`. In
`llvm/lib/Target/AArch64/AArch64ISelLowering.cpp`, `getAddrLarge` (line 8560) bitwise-ORs `Flags` (`MO_TAGGED`) with
`AArch64II::MO_G3` inside the 4-bit `MO_Fragment` field, and `getAddrTiny` (line 8640) attaches `MO_TAGGED` to `ADR`.

**Suggested fix.** Gate `MO_TAGGED` in `classifyGlobalReference` (or `allowTaggedGlobals`) on `CodeModel::Small`, or
handle `MO_TAGGED` explicitly in `getAddrLarge` and `getAddrTiny`.

Environment: clang 23.1.2 (`85ac560262434c9ccfc0c183ec22d4138ed647fb`), `aarch64-unknown-linux-gnu`.

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

Reply via email to