| Issue |
237910
|
| Summary |
[ASan][StackSafety] Masked load/store on a stack alloca is treated as "safe" and loses its ASan check
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
zjy-dev
|
`StackSafetyLocalAnalysis::analyzeAllUses` never marks an `llvm.masked.*` intrinsic call as an
unsafe access. It treats the call as an ordinary call to a declared function and only records
`US.Calls`. `StackSafetyGlobalInfo::stackAccessIsSafe(I)` returns true for every instruction absent
from `UnsafeAccesses`, so `AddressSanitizer::ignoreAccess` drops the shadow check of a masked load or
store whose pointer is a stack alloca. This happens even when the access is 4x larger than the alloca.
```llvm
define <16 x i32> @masked_oob(<16 x i1> %mask) sanitize_address {
%buf = alloca [4 x i32], align 16 ; 16 bytes
call void @escape(ptr %buf)
%v = call <16 x i32> @llvm.masked.load.v16i32.p0(ptr align 16 %buf, <16 x i1> %mask, <16 x i32> poison)
ret <16 x i32> %v ; reads up to 64 bytes
}
define <16 x i32> @plain_oob() sanitize_address { ; control
%buf = alloca [4 x i32], align 16
call void @escape(ptr %buf)
%v = load <16 x i32>, ptr %buf, align 16
ret <16 x i32> %v
}
declare void @escape(ptr)
declare <16 x i32> @llvm.masked.load.v16i32.p0(ptr, <16 x i1>, <16 x i32>)
```
Command: `opt -passes=asan -S trigger.ll` (target x86_64-unknown-linux-gnu)
```llvm
define <16 x i32> @masked_oob(<16 x i1> %mask)
%v = call <16 x i32> @llvm.masked.load.v16i32.p0(ptr align 16 %12, ...) ; NO check
define <16 x i32> @plain_oob()
call void @__asan_report_load_n(i64 %23, i64 64) ; checked
%v = load <16 x i32>, ptr %12, align 16
; with -asan-use-stack-safety=0: @masked_oob gets 16 per-lane __asan_report_load4 checks
```
This also happens from C: `clang -O2 -mavx512f -fsanitize=address` on a tail-folded loop
(`#pragma clang loop vectorize_predicate(enable)`) reading `int buf[16]` with `i < n` emits an
unchecked `llvm.masked.load` from `buf`, so `n > 16` is not reported (see `trigger_c.c`).
**Root cause.** In `llvm/lib/Analysis/StackSafetyAnalysis.cpp:505-533`, a masked intrinsic is not a
`MemIntrinsic` and not byval, and its callee is a `Function` declaration, so the use is only
recorded in `US.Calls`. That later widens the alloca's `Range` (affecting `isSafe(AI)`), but
`addRange(I, ..., /*IsSafe=*/false)` is never called, so `I` never enters `UnsafeAccesses`. `stackAccessIsSafe` (line 1001-1004) is "not in `UnsafeAccesses`", and
`AddressSanitizer::ignoreAccess` (AddressSanitizer.cpp:1509-1511) trusts it for masked
load/store/gather/scatter/expandload/compressstore (lines 1544-1558, 1560-1577).
**Suggested fix.** In `analyzeAllUses`, handle memory intrinsics that are not `MemIntrinsic`s:
`addRange(I, UnknownRange, /*IsSafe=*/false)` for any `IntrinsicInst` that may access memory
through `UI`. Alternatively, ASan could skip the `SSGI->stackAccessIsSafe` shortcut for masked operands.
Environment: LLVM 23.1.2 (llvmorg-23.1.2). Godbolt `opt` trunk (2026-11-01) gives the same output.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs