Issue 240687
Summary [ASan][Clang] ItaniumCXXABI::InitializeArrayCookie and readArrayCookieImpl Check LangOpts.Sanitize Instead of CGF.SanOpts, Emitting ASan Array-Cookie Calls Inside no_sanitize("address") Functions
Labels clang
Assignees
Reporter zjy-dev
    In `ItaniumCXXABI::InitializeArrayCookie` (clang/lib/CodeGen/ItaniumCXXABI.cpp:2587) and `ItaniumCXXABI::readArrayCookieImpl` (line 2616), Clang checks `CGM.getLangOpts().Sanitize.has(SanitizerKind::Address)` (the translation-unit-wide language options) instead of `CGF.SanOpts.has(SanitizerKind::Address)` (the per-function sanitizer mask that accounts for `__attribute__((no_sanitize("address")))` and `-fsanitize-ignorelist`). Consequently, a function explicitly annotated with `__attribute__((no_sanitize("address")))` that executes `new T[n]` or `delete[] p` on a type with a non-trivial destructor still emits calls to `__asan_poison_cxx_array_cookie` and `__asan_load_cxx_array_cookie`.

### Reproducer (`trigger.cpp`)

```cpp
struct S {
  long long v;
 ~S();
};
S::~S() {}

// Element type has a non-trivial destructor => delete[] lowers through
// ItaniumCXXABI::readArrayCookieImpl.
__attribute__((no_sanitize("address")))
void del(S *p) { delete[] p; }

// Replaceable operator new + runtime element count => InitializeArrayCookie.
__attribute__((no_sanitize("address")))
S *make(long n) { return new S[n]; }

int main() {
  S *p = make(4);
 del(p);
  return 0;
}
```

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

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

Observed output on LLVM 23.1.2:

```text
; Function Attrs: mustprogress uwtable (no sanitize_address attribute!)
define dso_local void @_Z3delP1S(ptr noundef %p) local_unnamed_addr #1 {
...
  %1 = tail call i64 @__asan_load_cxx_array_cookie(ptr nonnull %0)
...
}
define dso_local noundef nonnull ptr @_Z4makel(i64 noundef %n) local_unnamed_addr #1 {
...
  tail call void @__asan_poison_cxx_array_cookie(ptr nonnull %call)
 %7 = getelementptr inbounds nuw i8, ptr %call, i64 8
  ret ptr %7
}
```

### Expected behavior

`ItaniumCXXABI::InitializeArrayCookie` and `ItaniumCXXABI::readArrayCookieImpl` must check `CGF.SanOpts.has(SanitizerKind::Address)` instead of `CGM.getLangOpts().Sanitize.has(SanitizerKind::Address)` so that `no_sanitize("address")` functions do not call into the ASan runtime.

### Root cause & impact

- **Source location**: `clang/lib/CodeGen/ItaniumCXXABI.cpp:2587-2597, 2616-2627`
- **Impact**: Violates the `no_sanitize("address")` contract (used in early-boot/custom-allocator/sandbox code where ASan shadow memory is not mapped or must not be touched), causing unexpected ASan shadow memory accesses via `__asan_poison_cxx_array_cookie` / `__asan_load_cxx_array_cookie`.

### Suggested fix

In `clang/lib/CodeGen/ItaniumCXXABI.cpp:2587` and `:2616`, replace `CGM.getLangOpts().Sanitize.has(SanitizerKind::Address)` with `CGF.SanOpts.has(SanitizerKind::Address)`.

Environment: LLVM 23.1.2 (`llvmorg-23.1.2`), affected targets: `x86_64, aarch64, riscv64, loongarch64`.

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

Reply via email to