Issue 240317
Summary [HWASan] HWAddressSanitizer::getInterestingMemoryOperands Drops Alignment on AtomicRMWInst and AtomicCmpXchgInst, Bypassing Unaligned Granule-Straddling Checks
Labels new issue
Assignees
Reporter zjy-dev
    In HWAddressSanitizer::getInterestingMemoryOperands (llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp:924-934), `LoadInst` and `StoreInst` pass `LI->getAlign()` and `SI->getAlign()` into `InterestingMemoryOperand`, whereas `AtomicRMWInst` (line 928) and `AtomicCmpXchgInst` (line 934) pass `std::nullopt` instead of `RMW->getAlign()` and `XCHG->getAlign()`. Later, in `HWAddressSanitizer::instrumentMemAccess` (lines 1198-1200), the condition `if (UseFastPath && (!O.Alignment || *O.Alignment >= Mapping.getObjectAlignment() || *O.Alignment >= O.TypeStoreSize / 8))` treats `!O.Alignment` (`std::nullopt`) as naturally aligned! Consequently, an underaligned `atomicrmw` or `cmpxchg` (such as `__atomic_fetch_add` or `__atomic_compare_exchange_n` on a `#pragma pack(1)` struct member with `align 1`) takes the single-granule `@llvm.hwasan.check.memaccess` fast path (checking only `ptr`) instead of `@__hwasan_storeN` (which checks both `ptr` and `ptr + size - 1`).

### Reproducer (`trigger.cpp`)

```cpp
struct __attribute__((packed)) P { char c; long long v; };

typedef struct __attribute__((packed)) { char c; long long v; } Q;

long long rmw_packed(struct P *p) {
  return __atomic_fetch_add(&p->v, 1, __ATOMIC_SEQ_CST);
}

long long cmpxchg_packed(Q *q, long long expected, long long desired) {
  long long old = expected;
 __atomic_compare_exchange_n(&q->v, &old, desired, 0,
 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
  return old;
}

long long load_packed(P *p) {
  return __atomic_load_n(&p->v, __ATOMIC_SEQ_CST);
}
```

Compile / reproduction command (`['--target=aarch64-unknown-linux-gnu', '-fsanitize=hwaddress', '-O1', '-S', '-emit-llvm']`):

```bash
podman exec -i -e TMPDIR=/dev/shm defuzz-toolchain-daemon /opt/llvm23/bin/clang++ --target=aarch64-unknown-linux-gnu -fsanitize=hwaddress -O1 -x c++ - -S -emit-llvm -o -
```

Observed output on LLVM 23.1.2:

```text
define dso_local noundef i64 @_Z10rmw_packedP1P(ptr nofree noundef captures(none) %0) local_unnamed_addr #0 {
...
  call void @llvm.hwasan.check.memaccess.shortgranules(ptr %5, ptr %6, i32 19)
  br label %16
16:
  %17 = atomicrmw add ptr %6, i64 1 seq_cst, align 1
  ret i64 %17
}
...
define dso_local noundef i64 @_Z11load_packedP1P(ptr nofree noundef captures(none) %0) local_unnamed_addr #0 {
...
  call void @__hwasan_loadN(i64 %7, i64 8)
  %8 = load atomic i64, ptr %6 seq_cst, align 1
  ret i64 %8
}
```

### Expected behavior

HWAddressSanitizer::getInterestingMemoryOperands must pass `RMW->getAlign()` and `XCHG->getAlign()` to `InterestingMemoryOperand` so that underaligned atomic RMW and cmpxchg instructions are instrumented with `@__hwasan_storeN` to check both endpoints across 16-byte tag-granule boundaries.

### Root cause & impact

- **Source location**: `llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp:924-934, 1198-1205`
- **Impact**: False negative in HWASan: an unaligned `atomicrmw` or `cmpxchg` that straddles a 16-byte tag granule boundary (where the first byte is in-bounds and the trailing bytes overflow into an adjacent heap/stack object or redzone) only checks the starting granule's tag and misses the out-of-bounds write.

### Suggested fix

In `llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp:928` and `:934`, replace `std::nullopt` with `RMW->getAlign()` and `XCHG->getAlign()`.

Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `aarch64, riscv64, x86_64`.
Also reproduced with clang trunk (Compiler Explorer, 2026-11-09); `atomicrmw` still uses `align 1` and only the shortgranules check.

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

Reply via email to