Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using sanitizer runtimes on RISC-V #564

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
6 changes: 4 additions & 2 deletions compiler-rt/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ set(MIPS_CHERI_PURECAP mips64c128)
set(PPC32 powerpc)
set(PPC64 powerpc64 powerpc64le)
set(RISCV32 riscv32)
set(RISCV32_PURECAP riscv32c)
set(RISCV64 riscv64)
set(RISCV64_PURECAP riscv64c)
set(S390X s390x)
set(SPARC sparc)
set(SPARCV9 sparcv9)
Expand All @@ -298,7 +300,7 @@ if(APPLE)
set(X86_64 x86_64 x86_64h)
endif()

set(ALL_SANITIZER_COMMON_SUPPORTED_ARCH ${X86} ${X86_64} ${PPC64} ${RISCV64}
set(ALL_SANITIZER_COMMON_SUPPORTED_ARCH ${X86} ${X86_64} ${PPC64} ${RISCV64} ${RISCV64_PURECAP}
${ARM32} ${ARM64} ${MIPS32} ${MIPS64} ${MIPS_CHERI_PURECAP} ${S390X} ${SPARC} ${SPARCV9})
set(ALL_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64}
${MIPS32} ${MIPS64} ${PPC64} ${S390X} ${SPARC} ${SPARCV9})
Expand Down Expand Up @@ -333,7 +335,7 @@ set(ALL_MEMPROF_SUPPORTED_ARCH ${X86_64})
set(ALL_PROFILE_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${PPC32} ${PPC64}
${MIPS32} ${MIPS64} ${S390X} ${SPARC} ${SPARCV9})
set(ALL_TSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${PPC64} ${S390X})
set(ALL_UBSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64}
set(ALL_UBSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${RISCV64} ${RISCV64_PURECAP}
${MIPS32} ${MIPS64} ${MIPS_CHERI_PURECAP} ${PPC64} ${S390X} ${SPARC} ${SPARCV9})
set(ALL_SAFESTACK_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM64} ${MIPS32} ${MIPS64})
set(ALL_CFI_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${MIPS64})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,14 @@ class DeadlockDetector {
int unique_tid;
};

uptr current_epoch_;
usize current_epoch_;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I changed all the indices, etc. in this file to also be usize. The only thing in this file that holds actual pointers is the data_ array.

BV available_nodes_;
BV recycled_nodes_;
BV tmp_bv_;
BVGraph<BV> g_;
uptr data_[BV::kSize];
Edge edges_[BV::kSize * 32];
uptr n_edges_;
usize n_edges_;
};

} // namespace __sanitizer
Expand Down
22 changes: 16 additions & 6 deletions compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ usize internal_readlink(const char *path, char *buf, usize bufsize) {
#endif
}

uptr internal_unlink(const char *path) {
usize internal_unlink(const char *path) {
#if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0);
#else
Expand Down Expand Up @@ -1934,9 +1934,13 @@ SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
return (instr >> 21) & 1 ? WRITE: READ;
#elif defined(__riscv)
#if SANITIZER_FREEBSD
unsigned long pc = ucontext->uc_mcontext.mc_gpregs.gp_sepc;
#ifdef __CHERI_PURE_CAPABILITY__
uptr pc = ucontext->uc_mcontext.mc_capregs.cp_sepcc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sigh yes we currently do this for purecap...

#else
unsigned long pc = ucontext->uc_mcontext.__gregs[REG_PC];
uptr pc = ucontext->uc_mcontext.mc_gpregs.gp_sepc;
#endif
#else
uptr pc = ucontext->uc_mcontext.__gregs[REG_PC];
#endif
unsigned faulty_instruction = *(uint16_t *)pc;

Expand Down Expand Up @@ -2167,12 +2171,18 @@ static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
# endif
*bp = ucontext->uc_mcontext.gregs[11];
*sp = ucontext->uc_mcontext.gregs[15];
#elif defined(__riscv)
ucontext_t *ucontext = (ucontext_t*)context;
# elif defined(__riscv)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep the upstream formatting to reduce diffs?

ucontext_t *ucontext = (ucontext_t *)context;
# if SANITIZER_FREEBSD
# ifdef __CHERI_PURE_CAPABILITY__
*pc = ucontext->uc_mcontext.mc_capregs.cp_sepcc;
*bp = ucontext->uc_mcontext.mc_capregs.cp_cs[0];
*sp = ucontext->uc_mcontext.mc_capregs.cp_csp;
# else
*pc = ucontext->uc_mcontext.mc_gpregs.gp_sepc;
*bp = ucontext->uc_mcontext.mc_gpregs.gp_s[0];
*sp = ucontext->uc_mcontext.mc_gpregs.gp_sp;
# endif
# else
*pc = ucontext->uc_mcontext.__gregs[REG_PC];
*bp = ucontext->uc_mcontext.__gregs[REG_S0];
Expand Down Expand Up @@ -2232,7 +2242,7 @@ void CheckASLR() {
}
#elif SANITIZER_FREEBSD
int aslr_pie;
uptr len = sizeof(aslr_pie);
usize len = sizeof(aslr_pie);
#if SANITIZER_WORDSIZE == 64
if (UNLIKELY(internal_sysctlbyname("kern.elf64.aslr.pie_enable",
&aslr_pie, &len, NULL, 0) == -1)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,12 @@ class StackTraceTextPrinter {
const bool symbolize_ = false;
};

static void CopyStringToBuffer(const InternalScopedString &str, char *out_buf,
uptr out_buf_size) {
static void CopyStringToBuffer(const InternalScopedString &str, char *out_buf, usize out_buf_size) {
if (!out_buf_size)
return;

CHECK_GT(out_buf_size, 0);
uptr copy_size = Min(str.length(), out_buf_size - 1);
usize copy_size = Min(str.length(), out_buf_size - 1);
internal_memcpy(out_buf, str.data(), copy_size);
out_buf[copy_size] = '\0';
}
Expand Down