| Issue |
240685
|
| Summary |
[HWASan] HWAddressSanitizer::instrumentGlobal Replaces Zero-Sized Globals with Tagged Aliases While Emitting Zero hwasan_globals Descriptors
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
zjy-dev
|
In HWAddressSanitizer::instrumentGlobal (llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp:1733-1801), a global variable `GV` of size `SizeInBytes == 0` (such as `int zero_arr[0];` in GNU C/C++) has `NewSize = alignTo(0, 16) = 0`. `instrumentGlobal` creates `NewGV` (`@zero_arr.hwasan`) and replaces `GV` with a tagged `GlobalAlias` carrying `Tag` in the top byte (`inttoptr (i64 add (i64 ptrtoint (ptr @zero_arr.hwasan to i64), i64 (Tag << 56)) to ptr)`). It then enters the descriptor emission loop at line 1775: `for (uint64_t DescriptorPos = 0; DescriptorPos < SizeInBytes; DescriptorPos += kMaxDescriptorSize)`. When `SizeInBytes == 0`, this loop executes 0 iterations! As a result, `@zero_arr` is accessed through a non-zero tagged pointer, while the runtime `__hwasan_init_frames` / `__hwasan_register_globals` receives no descriptor for `@zero_arr.hwasan` and never writes `Tag` into shadow memory (or worse, `@zero_arr.hwasan` shares its address with the next symbol in `.bss`/`.data`, whose shadow byte holds a different tag).
### Reproducer (`trigger.c`)
```c
int sink;
int zero_arr[0];
char neighbour[4] = {1, 2, 3, 4};
int *addr_of_zero(void) { return zero_arr; }
int use(void) {
sink += neighbour[0];
return sink;
}
```
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
@zero_arr.hwasan = dso_local global [0 x i32] zeroinitializer, align 16
@nonzero_arr.hwasan = dso_local global { [4 x i32], [12 x i8] } { [4 x i32] [i32 1, i32 2, i32 3, i32 4], [12 x i8] c"\00\00\00\00\00\00\00\00\00\00\00\BE" }, align 16
@hwasan.global.nonzero_arr = private constant { i32, i32 } { i32 trunc (i64 sub (i64 ptrtoint (ptr @nonzero_arr.hwasan to i64), i64 ptrtoint (ptr @hwasan.global.nonzero_arr to i64)) to i32), i32 -1107296240 }, section "hwasan_globals", !associated !0
...
@zero_arr = dso_local alias [0 x i32], inttoptr (i64 add (i64 ptrtoint (ptr @zero_arr.hwasan to i64), i64 3746994889972252672) to ptr)
```
### Expected behavior
`HWAddressSanitizer::shouldInstrumentGlobal` (or `instrumentGlobal`) must either skip zero-sized globals (`if (M.getDataLayout().getTypeAllocSize(GV->getValueType()) == 0) return false;`) or pad them to one 16-byte granule with a valid `hwasan_globals` descriptor.
### Root cause & impact
- **Source location**: `llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp:1733-1801`
- **Impact**: Under `-fsanitize=hwaddress`, zero-sized globals (`int zero_arr[0];`, commonly used as section boundary markers or flexible headers) are assigned a non-zero pointer tag in their symbol alias without any corresponding shadow-tag initialization or granule padding.
### Suggested fix
In `HWAddressSanitizer::shouldInstrumentGlobal` (`HWAddressSanitizer.cpp:1700`), return `false` if `M.getDataLayout().getTypeAllocSize(GV->getValueType()).isZero()`.
Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `aarch64, riscv64, x86_64`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs