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
63 changes: 63 additions & 0 deletions clang/test/Driver/cheri/freebsd-sanitizer-libnames.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// RUN: %riscv64_cheri_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined %s -### 2>&1 | FileCheck --check-prefixes=UBSAN,UBSAN-RISCV64 %s
// RUN: %riscv64_cheri_purecap_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined %s -### 2>&1 | FileCheck --check-prefixes=UBSAN,UBSAN-PURECAP-RISCV64 %s
// RUN: %riscv32_cheri_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined %s -### 2>&1 | FileCheck --check-prefixes=UBSAN,UBSAN-RISCV32 %s
// RUN: %riscv32_cheri_purecap_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined %s -### 2>&1 | FileCheck --check-prefixes=UBSAN,UBSAN-PURECAP-RISCV32 %s
// RUN: %cheri_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined %s -### 2>&1 | FileCheck --check-prefixes=UBSAN,UBSAN-MIPS64 %s
// RUN: %cheri_purecap_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined %s -### 2>&1 | FileCheck --check-prefixes=UBSAN,UBSAN-PURECAP-MIPS64 %s

// UBSAN: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" "-isysroot"
// UBSAN-SAME: "-fsanitize=alignment,array-bounds,
// UBSAN-SAME: "-fsanitize-recover=alignment,array-bounds,
// UBSAN-NEXT: "/usr/bin/ld"
// UBSAN-RISCV64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_standalone-riscv64.a"
// UBSAN-PURECAP-RISCV64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_standalone-riscv64.a"
// UBSAN-RISCV32-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_standalone-riscv32.a"
// UBSAN-PURECAP-RISCV32-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_standalone-riscv32.a"
// UBSAN-MIPS64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_standalone-mips64.a"
// UBSAN-PURECAP-MIPS64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_standalone-mips64c128.a"

// RUN: %riscv64_cheri_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined -fsanitize-minimal-runtime %s -### 2>&1 | FileCheck --check-prefixes=UBSAN-MINIMAL,UBSAN-MINIMAL-RISCV64 %s
// RUN: %riscv64_cheri_purecap_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined -fsanitize-minimal-runtime %s -### 2>&1 | FileCheck --check-prefixes=UBSAN-MINIMAL,UBSAN-MINIMAL-PURECAP-RISCV64 %s
// RUN: %riscv32_cheri_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined -fsanitize-minimal-runtime %s -### 2>&1 | FileCheck --check-prefixes=UBSAN-MINIMAL,UBSAN-MINIMAL-RISCV32 %s
// RUN: %riscv32_cheri_purecap_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined -fsanitize-minimal-runtime %s -### 2>&1 | FileCheck --check-prefixes=UBSAN-MINIMAL,UBSAN-MINIMAL-PURECAP-RISCV32 %s
// RUN: %cheri_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined -fsanitize-minimal-runtime %s -### 2>&1 | FileCheck --check-prefixes=UBSAN-MINIMAL,UBSAN-MINIMAL-MIPS64 %s
// RUN: %cheri_purecap_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=undefined -fsanitize-minimal-runtime %s -### 2>&1 | FileCheck --check-prefixes=UBSAN-MINIMAL,UBSAN-MINIMAL-PURECAP-MIPS64 %s

// UBSAN-MINIMAL: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" "-isysroot"
// UBSAN-MINIMAL-SAME: "-fsanitize=alignment,array-bounds,
// UBSAN-MINIMAL-SAME: "-fsanitize-recover=alignment,array-bounds,
// UBSAN-MINIMAL-NEXT: "/usr/bin/ld"
// UBSAN-MINIMAL-RISCV64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_minimal-riscv64.a"
// UBSAN-MINIMAL-PURECAP-RISCV64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_minimal-riscv64.a"
// UBSAN-MINIMAL-RISCV32-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_minimal-riscv32.a"
// UBSAN-MINIMAL-PURECAP-RISCV32-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_minimal-riscv32.a"
// UBSAN-MINIMAL-MIPS64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_minimal-mips64.a"
// UBSAN-MINIMAL-PURECAP-MIPS64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_minimal-mips64c128.a"

// FIXME: fsanitize=fuzzer is not accepted for RISC-V
// RUN: %cheri_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=fuzzer %s -### 2>&1 | FileCheck --check-prefixes=FUZZER,FUZZER-MIPS64 %s
// RUN: %cheri_purecap_clang --sysroot=%S/Inputs/basic_cheribsd_libXXc_tree \
// RUN: -fsanitize=fuzzer %s -### 2>&1 | FileCheck --check-prefixes=FUZZER,FUZZER-PURECAP-MIPS64 %s


// FUZZER: "-resource-dir" "[[RESOURCE_DIR:[^"]+]]" "-isysroot"
// FUZZER-SAME: "-fsanitize-coverage
// FUZZER-SAME: "-fsanitize=fuzzer,fuzzer-no-link"
// FUZZER: "/usr/bin/ld"
// FUZZER-MIPS64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.fuzzer-mips64.a"
// FUZZER-MIPS64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_standalone-mips64.a"
// FUZZER-PURECAP-MIPS64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.fuzzer-mips64c128.a"
// FUZZER-PURECAP-MIPS64-SAME: "[[RESOURCE_DIR]]/lib/freebsd/libclang_rt.ubsan_standalone-mips64c128.a"
3 changes: 3 additions & 0 deletions compiler-rt/cmake/Modules/AddCompilerRT.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ endfunction()

macro(set_output_name output name arch)
if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
if (COMPILER_RT_IS_CHERI_PURECAP)
message(FATAL_ERROR "LLVM_ENABLE_PER_TARGET_RUNTIME_DIR is incompatible with purecap runtimes")
endif()
set(${output} ${name})
else()
if(ANDROID AND ${arch} STREQUAL "i386")
Expand Down
33 changes: 30 additions & 3 deletions compiler-rt/cmake/Modules/CompilerRTUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ macro(test_target_arch arch def)
endmacro()

macro(detect_target_arch)
check_symbol_exists(__CHERI_PURE_CAPABILITY__ "" __CHERI_PURECAP)
check_symbol_exists(__arm__ "" __ARM)
check_symbol_exists(__aarch64__ "" __AARCH64)
check_symbol_exists(__x86_64__ "" __X86_64)
Expand Down Expand Up @@ -183,7 +184,11 @@ macro(detect_target_arch)
elseif(__I386)
add_default_target_arch(i386)
elseif(__MIPS64) # must be checked before __MIPS
add_default_target_arch(mips64)
if (__CHERI_PURECAP)
add_default_target_arch(mips64c128)
else()
add_default_target_arch(mips64)
endif()
elseif(__MIPS)
add_default_target_arch(mips)
elseif(__PPC64) # must be checked before __PPC
Expand All @@ -194,9 +199,17 @@ macro(detect_target_arch)
add_default_target_arch(powerpc)
elseif(__RISCV)
if(CMAKE_SIZEOF_VOID_P EQUAL "4")
add_default_target_arch(riscv32)
if (__CHERI_PURECAP)
add_default_target_arch(riscv32c)
else()
add_default_target_arch(riscv32)
endif()
elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")
add_default_target_arch(riscv64)
if (__CHERI_PURECAP)
add_default_target_arch(riscv64c)
else()
add_default_target_arch(riscv64)
endif()
else()
message(FATAL_ERROR "Unsupport XLEN for RISC-V")
endif()
Expand Down Expand Up @@ -425,9 +438,12 @@ macro(construct_compiler_rt_default_triple)
message(FATAL_ERROR "COMPILER_RT_DEFAULT_TARGET_TRIPLE isn't supported when building for default target only")
endif()
set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${CMAKE_C_COMPILER_TARGET})
check_symbol_exists(__CHERI_PURE_CAPABILITY__ "" COMPILER_RT_DEFAULT_TARGET_TRIPLE_IS_PURECAP)
else()
set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${TARGET_TRIPLE} CACHE STRING
"Default triple for which compiler-rt runtimes will be built.")
set(COMPILER_RT_DEFAULT_TARGET_TRIPLE_IS_PURECAP OFF CACHE BOOL
"Whether the default target triple is purecap.")
endif()

if(DEFINED COMPILER_RT_TEST_TARGET_TRIPLE)
Expand All @@ -438,13 +454,24 @@ macro(construct_compiler_rt_default_triple)

string(REPLACE "-" ";" TARGET_TRIPLE_LIST ${COMPILER_RT_DEFAULT_TARGET_TRIPLE})
list(GET TARGET_TRIPLE_LIST 0 COMPILER_RT_DEFAULT_TARGET_ARCH)
message(STATUS "COMPILER_RT_DEFAULT_TARGET_ARCH inferred as ${COMPILER_RT_DEFAULT_TARGET_ARCH}")

# Map various forms of the architecture names to the canonical forms
# (as they are used by clang, see getArchNameForCompilerRTLib).
if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "^i.86$")
# Android uses i686, but that's remapped at a later stage.
set(COMPILER_RT_DEFAULT_TARGET_ARCH "i386")
endif()
if (COMPILER_RT_DEFAULT_TARGET_TRIPLE_IS_PURECAP)
message(STATUS "Updating default target arch (${COMPILER_RT_DEFAULT_TARGET_ARCH}) for purecap...")
if (COMPILER_RT_DEFAULT_TARGET_ARCH MATCHES "^mips")
math(EXPR _cap_size "${CMAKE_SIZEOF_VOID_P} * 8" OUTPUT_FORMAT DECIMAL)
set(COMPILER_RT_DEFAULT_TARGET_ARCH "${COMPILER_RT_DEFAULT_TARGET_ARCH}c${_cap_size}")
else()
set(COMPILER_RT_DEFAULT_TARGET_ARCH "${COMPILER_RT_DEFAULT_TARGET_ARCH}c")
endif()
message(STATUS "Updated default target arch is ${COMPILER_RT_DEFAULT_TARGET_ARCH}")
endif()

# Determine if test target triple is specified explicitly, and doesn't match the
# default.
Expand Down
3 changes: 3 additions & 0 deletions compiler-rt/cmake/base-config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ macro(test_targets)
elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "mips")
test_target_arch(mips "" "-mips32r2" "-mabi=32" "-D_LARGEFILE_SOURCE" "-D_FILE_OFFSET_BITS=64")
test_target_arch(mips64 "" "-mips64r2" "-mabi=64")
test_target_arch(mips64c128 "__CHERI_PURE_CAPABILITY__" "-mcpu=beri" "-mabi=purecap" "-cheri=128")
elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "arm")
if(WIN32)
test_target_arch(arm "" "" "")
Expand All @@ -241,8 +242,10 @@ macro(test_targets)
test_target_arch(aarch64 "" "-march=armv8-a")
elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv32")
test_target_arch(riscv32 "" "")
test_target_arch(riscv32c "__CHERI_PURE_CAPABILITY__" "-mabi=il32pc64")
elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "riscv64")
test_target_arch(riscv64 "" "")
test_target_arch(riscv64c "__CHERI_PURE_CAPABILITY__" "-mabi=l64pc128")
elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm32")
test_target_arch(wasm32 "" "--target=wasm32-unknown-unknown")
elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "wasm64")
Expand Down
6 changes: 4 additions & 2 deletions compiler-rt/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ check_cxx_compiler_flag(/wd4800 COMPILER_RT_HAS_WD4800_FLAG)

# Symbols.
check_symbol_exists(__func__ "" COMPILER_RT_HAS_FUNC_SYMBOL)
check_symbol_exists(__CHERI_PURE_CAPABILITY__ "" COMPILER_RT_IS_CHERI_PURECAP)

# Includes.
check_cxx_compiler_flag(-nostdinc++ COMPILER_RT_HAS_NOSTDINCXX_FLAG)
Expand Down Expand Up @@ -192,8 +193,9 @@ file(WRITE ${SIMPLE_SOURCE} "#include <stdlib.h>\n#include <stdio.h>\nint main()
# Detect whether the current target platform is 32-bit or 64-bit, and setup
# the correct commandline flags needed to attempt to target 32-bit and 64-bit.
if (NOT CMAKE_SIZEOF_VOID_P EQUAL 4 AND
NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
message(WARNING "Please use architecture with 4 or 8 byte pointers.")
NOT CMAKE_SIZEOF_VOID_P EQUAL 8 AND
NOT COMPILER_RT_IS_CHERI_PURECAP)
message(FATAL_ERROR "Please use architecture with 4 or 8 byte pointers.")
endif()

test_targets()
Expand Down
4 changes: 4 additions & 0 deletions compiler-rt/lib/builtins/emutls.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ static pthread_key_t emutls_pthread_key;
static bool emutls_key_created = false;

typedef unsigned int gcc_word __attribute__((mode(word)));
#ifdef __CHERI_PURE_CAPABILITY__
typedef uintptr_t gcc_pointer;
#else
typedef unsigned int gcc_pointer __attribute__((mode(pointer)));
#endif

// Default is not to use posix_memalign, so systems like Android
// can use thread local data without heavier POSIX memory allocators.
Expand Down
6 changes: 3 additions & 3 deletions compiler-rt/lib/sanitizer_common/sanitizer_allocator_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ inline bool CheckAlignedAllocAlignmentAndSize(usize alignment, usize size) {

// Checks posix_memalign() parameters, verifies that alignment is a power of two
// and a multiple of sizeof(void *).
inline bool CheckPosixMemalignAlignment(uptr alignment) {
inline bool CheckPosixMemalignAlignment(usize alignment) {
return alignment != 0 && IsPowerOfTwo(alignment) &&
(alignment % sizeof(void *)) == 0;
}

// Returns true if calloc(size, n) call overflows on size*n calculation.
inline bool CheckForCallocOverflow(uptr size, uptr n) {
inline bool CheckForCallocOverflow(usize size, usize n) {
if (!size)
return false;
usize max = (usize)-1L;
Expand All @@ -67,7 +67,7 @@ inline bool CheckForCallocOverflow(uptr size, uptr n) {

// Returns true if the size passed to pvalloc overflows when rounded to the next
// multiple of page_size.
inline bool CheckForPvallocOverflow(uptr size, uptr page_size) {
inline bool CheckForPvallocOverflow(usize size, usize page_size) {
return RoundUpTo(size, page_size) < size;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ class SizeClassAllocator64 {
void MaybeReleaseToOS(MemoryMapperT *memory_mapper, uptr class_id,
bool force) {
RegionInfo *region = GetRegionInfo(class_id);
const uptr chunk_size = ClassIdToSize(class_id);
const usize chunk_size = ClassIdToSize(class_id);
const uptr page_size = GetPageSizeCached();

uptr n = region->num_freed_chunks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class LargeMmapAllocatorPtrArrayDynamic {
static const int kMaxNumChunks = 1 << 20;
static const int kChunksBlockCount = 1 << 14;
ReservedAddressRange address_range_;
uptr n_reserved_;
usize n_reserved_;
};

#if SANITIZER_WORDSIZE == 32
Expand Down
14 changes: 5 additions & 9 deletions compiler-rt/lib/sanitizer_common/sanitizer_atomic_clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,12 @@ inline typename T::Type atomic_fetch_sub(volatile T *a,
return __sync_fetch_and_add(&a->val_dont_use, -v);
}

template<typename T>
inline typename T::Type atomic_exchange(volatile T *a,
typename T::Type v, memory_order mo) {
template <typename T>
ALWAYS_INLINE typename T::Type atomic_exchange(volatile T *a,
typename T::Type v,
memory_order mo) {
DCHECK(!((vaddr)a % sizeof(*a)));
if (mo & (memory_order_release | memory_order_acq_rel | memory_order_seq_cst))
__sync_synchronize();
v = __sync_lock_test_and_set(&a->val_dont_use, v);
if (mo == memory_order_seq_cst)
__sync_synchronize();
return v;
return __atomic_exchange_n(&a->val_dont_use, v, mo);
}

template <typename T>
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/sanitizer_common/sanitizer_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ usize ReadBinaryNameCached(/*out*/char *buf, usize buf_len) {
return name_len;
}

uptr ReadBinaryDir(/*out*/ char *buf, uptr buf_len) {
usize ReadBinaryDir(/*out*/ char *buf, usize buf_len) {
ReadBinaryNameCached(buf, buf_len);
const char *exec_name_pos = StripModuleName(buf);
uptr name_len = exec_name_pos - buf;
Expand Down
12 changes: 6 additions & 6 deletions compiler-rt/lib/sanitizer_common/sanitizer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void UnmapFromTo(uptr from, uptr to);
// have max(2^min_shadow_base_alignment, mmap granularity) on the left, and
// shadow_size_bytes bytes on the right, which on linux is mapped no access.
// The high_mem_end may be updated if the original shadow size doesn't fit.
uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
uptr MapDynamicShadow(usize shadow_size_bytes, uptr shadow_scale,
uptr min_shadow_base_alignment, uptr &high_mem_end);

// Let S = max(shadow_size, num_aliases * alias_size, ring_buffer_size).
Expand Down Expand Up @@ -403,7 +403,7 @@ inline usize MostSignificantSetBitIndex(usize x) {
}
#ifdef __CHERI_PURE_CAPABILITY__
usize MostSignificantSetBitIndex(uptr x) = delete;
INLINE usize MostSignificantSetBitIndex(u64 x) {
inline usize MostSignificantSetBitIndex(u64 x) {
return MostSignificantSetBitIndex((usize)x);
}
#endif
Expand All @@ -426,7 +426,7 @@ inline usize LeastSignificantSetBitIndex(usize x) {
}
#ifdef __CHERI_PURE_CAPABILITY__
usize LeastSignificantSetBitIndex(uptr x) = delete;
INLINE usize LeastSignificantSetBitIndex(u64 x) {
inline usize LeastSignificantSetBitIndex(u64 x) {
return LeastSignificantSetBitIndex((usize)x);
}
#endif
Expand All @@ -436,7 +436,7 @@ inline bool IsPowerOfTwo(u64 x) {
}
#ifdef __CHERI_PURE_CAPABILITY__
bool IsPowerOfTwo(uptr x) = delete;
INLINE bool IsPowerOfTwo(usize x) {
inline bool IsPowerOfTwo(usize x) {
return IsPowerOfTwo((u64)x);
}
#endif
Expand All @@ -452,7 +452,7 @@ inline u64 RoundUpToPowerOfTwo(u64 size) {
}
#ifdef __CHERI_PURE_CAPABILITY__
uptr RoundUpToPowerOfTwo(uptr size) = delete;
INLINE usize RoundUpToPowerOfTwo(usize x) {
inline usize RoundUpToPowerOfTwo(usize x) {
return (usize)RoundUpToPowerOfTwo((u64)x);
}
#endif
Expand Down Expand Up @@ -510,7 +510,7 @@ inline u64 Log2(u64 x) {
}
#ifdef __CHERI_PURE_CAPABILITY__
uptr Log2(uptr x) = delete;
INLINE usize Log2(usize x) {
inline usize Log2(usize x) {
return (usize)Log2((u64)x);
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void SetSandboxingCallback(void (*f)()) {
sandboxing_callback = f;
}

uptr ReservedAddressRange::InitAligned(uptr size, uptr align,
uptr ReservedAddressRange::InitAligned(usize size, usize align,
const char *name) {
CHECK(IsPowerOfTwo(align));
if (align <= GetPageSizeCached())
Expand Down
16 changes: 13 additions & 3 deletions compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ namespace __sanitizer {
#if !SANITIZER_S390
uptr internal_mmap(void *addr, usize length, int prot, int flags, int fd,
u64 offset) {
#ifdef __CHERI_PURE_CAPABILITY__
#if SANITIZER_FREEBSD && defined(__CHERI_PURE_CAPABILITY__)
return (uptr)mmap(addr, length, prot, flags, fd, offset);
Copy link
Member

Choose a reason for hiding this comment

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

What's wrong with internal_syscall? It returns uptr.

Copy link
Member Author

Choose a reason for hiding this comment

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

For some reason that didn't work, I really can't remember why. Will need to try again.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe it predates making length a usize, so the varargs nature of syscall(2) broke it?

#elif SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd,
Expand All @@ -193,11 +193,11 @@ uptr internal_mremap(void *old_address, usize old_size, usize new_size, int flag
}
#endif

int internal_mprotect(void *addr, uptr length, int prot) {
int internal_mprotect(void *addr, usize length, int prot) {
return internal_syscall(SYSCALL(mprotect), (uptr)addr, length, prot);
}

int internal_madvise(uptr addr, uptr length, int advice) {
int internal_madvise(uptr addr, usize length, int advice) {
return internal_syscall(SYSCALL(madvise), addr, length, advice);
}

Expand Down Expand Up @@ -1854,7 +1854,11 @@ SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
uint32_t op_code;

# if SANITIZER_FREEBSD
# ifdef __CHERI_PURE_CAPABILITY__
exception_source = (uint32_t *)ucontext->uc_mcontext.mc_cheriframe.cf_pcc;
Copy link
Member

Choose a reason for hiding this comment

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

Grr split register files

# else
exception_source = (uint32_t *)(uptr)ucontext->uc_mcontext.mc_pc;
# endif
# else
exception_source = (uint32_t *)ucontext->uc_mcontext.pc;
# endif
Expand Down Expand Up @@ -2133,9 +2137,15 @@ static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
#elif defined(__mips__)
# if SANITIZER_FREEBSD
ucontext_t *ucontext = (ucontext_t*)context;
# ifdef __CHERI_PURE_CAPABILITY__
*pc = (uptr)ucontext->uc_mcontext.mc_cheriframe.cf_pcc;
*bp = (uptr)ucontext->uc_mcontext.mc_cheriframe.cf_c24;
*sp = (uptr)ucontext->uc_mcontext.mc_cheriframe.cf_csp;
# else
*pc = ucontext->uc_mcontext.mc_pc;
*bp = ucontext->uc_mcontext.mc_regs[30];
*sp = ucontext->uc_mcontext.mc_regs[29];
# endif
# else
ucontext_t *ucontext = (ucontext_t*)context;
*pc = ucontext->uc_mcontext.pc;
Expand Down
Loading