Issue 237863
Summary [ASan] "Instrument the same temp just once" dedup ignores access size, dropping checks of wider loads / byval reads
Labels new issue
Assignees
Reporter zjy-dev
    `AddressSanitizer::instrumentFunction` skips any memory operand whose pointer was already
instrumented earlier in the same basic block (`ClOptSameTemp`, default on). The key is only the
pointer value, not the access size, so a narrow access followed by a wider access through the same
pointer leaves the wider access with no shadow check. With default `-fsanitize=address`, an 8-byte
store followed by a 64-byte `byval` read, or a 1-byte store followed by an 8-byte load, checks only
the first, narrow access.

```c
struct S { long a[8]; };
void sink(struct S s);
void byval_after_store(struct S *s) { s->a[0] = 0; sink(*s); }
unsigned long load_after_store(char *p) {
  p[0] = 1; unsigned long v; __builtin_memcpy(&v, p, 8); return v;
}
void byval_only(struct S *s) { sink(*s); }          /* control */
```

Compile flag: `clang --target=x86_64-linux-gnu -O2 -fsanitize=address -S -emit-llvm`

```llvm
define void @byval_after_store(ptr %s)
  call void @__asan_report_store8(i64 %0)            ; 8-byte store checked
  store i64 0, ptr %s, align 8
  tail call void @sink(ptr byval(%struct.S) align 8 %s) ; 64-byte read: NO check
define i64 @load_after_store(ptr %p)
  call void @__asan_report_store1(i64 %0)            ; 1-byte store checked
  store i8 1, ptr %p, align 1
  %v.0.copyload = load i64, ptr %p, align 1          ; 8-byte load: NO check
define void @byval_only(ptr %s)
  call void @__asan_report_load_n(i64 %3, i64 64)    ; control: 64-byte read checked
```

With `-mllvm -asan-opt-same-temp=0` both dropped accesses get `__asan_report_load_n(..., 64)` and
`__asan_report_load_n(..., 8)` respectively. Bytes 8..63 of `*s` and 1..7 of `*p` are never
checked, so an overflow past a small object is missed. The same IR-level behaviour applies to every
target; on AArch64, the `load_after_store` case reproduces.

**Root cause.** `llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp:3142-3152`:
`if (!TempsToInstrument.insert(Ptr).second) continue;`. `TempsToInstrument` is a
`SmallPtrSet<Value *>` (line 3120), so an earlier access of any size suppresses later ones.
GCC's equivalent (`has_mem_ref_been_instrumented`, gcc/asan.cc:659-665) compares
`saved_ref->access_size >= access_size`.

**Suggested fix.** Track the largest instrumented size per pointer (e.g.
`DenseMap<Value *, TypeSize>`) and skip an operand only if a previous check covered at least
its `TypeStoreSize`. A variable-size operand should never be skipped.

Environment: clang 23.1.2 (llvmorg-23.1.2). Godbolt clang trunk (2026-11-01) has the same output.

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

Reply via email to