llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

AddressSanitizer CHECK failed with RISC-V program

Open cardigan1008 opened this issue 1 year ago • 2 comments

I compiled this program with asan on:

#include <stdio.h>
#include <riscv_vector.h>
#include <stddef.h>
#include <stdint.h>

int lego(size_t n) {
    size_t vl = __riscv_vsetvl_e8m1(n);
    __rvv_bool2_t mask = __riscv_vmset_m_b2(vl);
    size_t count = __riscv_vcpop_m_b2(mask, vl);
    return count;
}

int main() {
    long long ret = lego(50);
    printf("ret=%lld\n", ret);
    return 0;
}

Compile command:

clang --target=riscv64-unknown-linux-gnu -march=rv64gcv -mabi=lp64d -menable-experiment
al-extensions -I/data/yunboni/utils/compilers/llvm_latest_rvv/lib/clang/22/include/riscv_vector.h -fsanitize=address test.c

Then run it:

qemu-riscv64 -L /usr/riscv64-linux-gnu ./a.out

It got errors:

AddressSanitizer: CHECK failed: asan_globals.cpp:276 "((AddrIsInMem(g->beg))) != (0)" (0x0, 0x0) (tid=3675936)
AddressSanitizer:DEADLYSIGNAL
=================================================================
==3675936==ERROR: AddressSanitizer: SEGV on unknown address 0x355556b83828 (pc 0x55555559d102 bp 0x2aaaab2a7a70 sp 0x2aaaab2a7210 T0)
==3675936==The signal is caused by a READ memory access.
    <empty stack>

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV
==3675936==ABORTING

My clang version:

clang version 22.0.0git (https://github.com/llvm/llvm-project.git 3defab36b73d60e616f5d6fe0e88e435c3dfc0dc)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /data/yunboni/utils/compilers/llvm_latest_rvv/bin
Build config: +assertions
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/13
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/13
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64

qemu version:

qemu-riscv64 version 8.2.2 (Debian 1:8.2.2+ds-0ubuntu1.10)
Copyright (c) 2003-2023 Fabrice Bellard and the QEMU Project developers

cardigan1008 avatar Oct 23 '25 11:10 cardigan1008

Can I give it a try?

Michael-Chen-NJU avatar Nov 06 '25 10:11 Michael-Chen-NJU

I can reproduce a very similar failure when running RISC‑V ASan-instrumented binaries under qemu-riscv64 (linux-user mode). I did a few controlled experiments to isolate where things go wrong:

#include <stdio.h>
int main() {
  printf("ASan Init Check.\n"); // Crashes before this prints.
  return 0;
}

Build and run:

clang --target=riscv64-unknown-linux-gnu -march=rv64gcv -mabi=lp64d -menable-experimental-extensions -fsanitize=address -fuse-ld=lld test_reduced.c -o test_reduced.out
qemu-riscv64 -L /usr/riscv64-linux-gnu ./test_reduced.out

It got error too:

AddressSanitizer: CHECK failed: asan_globals.cpp:276 "((AddrIsInMem(g->beg))) != (0)" (0x0, 0x0) (tid=3675936)
AddressSanitizer:DEADLYSIGNAL
=================================================================
==3547825==ERROR: AddressSanitizer: SEGV on unknown address 0x355556b0d5c0 (pc 0x5555555db88c bp 0x2aaaab2a8ee0 sp 0x2aaaab2a8680 T0)
==3547825==The signal is caused by a READ memory access.
    <empty stack>

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV
==3547825==ABORTING

I also tried if asan could work on some other simple test.

  • A known failing ASan test (heap-use-after-free) does report the error, then trips a nested failure Source: compiler-rt/test/asan/TestCases/Linux/abort_on_error.cpp (unmodified, i.e. triggers UAF)
#include <stdlib.h>
int main() {
  char *x = (char*)malloc(10 * sizeof(char));
  free(x);
  return x[5];
  // CHECK: {{.*ERROR: AddressSanitizer: heap-use-after-free on address}}
}

Same toolchain flags as above - Run result:

=================================================================
==3546767==ERROR: AddressSanitizer: heap-use-after-free on address 0x32abac070015 at pc 0x55555567fae8 bp 0x2aaaab2ab130 sp 0x2aaaab2ab118
READ of size 1 at 0x32abac070015 thread T0
AddressSanitizer:DEADLYSIGNAL
AddressSanitizer: nested bug in the same thread, aborting.

which is what we expected.

  • If I modify the test to not trigger any bug (malloc/free then exit), LeakSanitizer crashes immediately Source: same file but changed to just malloc/free and return 0 Run result:
==...==LeakSanitizer has encountered a fatal error.
HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)

Likely root cause

  • ASan’s initialization checks that registered globals (e.g., string literals) lie within mapped memory. In qemu linux-user mode, the guest’s virtual address space but do not match the guest program’s expectations, so AddrIsInMem(...) fails and ASan hits a CHECK in asan_globals.cpp during init.
  • The fact that the UAF test first reports an error and then hits a nested failure suggests some parts of ASan work under qemu-user, but other runtime interactions (memory mapping layout) remain incompatible.

Michael-Chen-NJU avatar Nov 09 '25 09:11 Michael-Chen-NJU