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
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
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ void UnmapFromTo(uptr from, uptr to) {
}
}

uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
uptr MapDynamicShadow(usize shadow_size_bytes, uptr shadow_scale,
uptr min_shadow_base_alignment,
UNUSED uptr &high_mem_end) {
const uptr granularity = GetMmapGranularity();
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ uptr internal_mremap(void *old_address, usize old_size, usize new_size, int flag
return 0;
}

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

Expand Down Expand Up @@ -1225,7 +1225,7 @@ vaddr GetMaxVirtualAddress() {
return GetMaxUserVirtualAddress();
}

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) {
const uptr granularity = GetMmapGranularity();
const uptr alignment =
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/sanitizer_common/sanitizer_netbsd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ uptr internal_mremap(void *old_address, usize old_size, usize new_size, int flag
return 0;
}

int internal_mprotect(void *addr, uptr length, int prot) {
int internal_mprotect(void *addr, usize length, int prot) {
DEFINE__REAL(int, mprotect, void *a, uptr b, int c);
return _REAL(mprotect, addr, length, prot);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ inline void *PersistentAllocator::alloc(usize size) {
s = tryAlloc(size);
if (s) return s;
atomic_store(&region_pos, 0, memory_order_relaxed);
uptr allocsz = 64 * 1024;
usize allocsz = 64 * 1024;
if (allocsz < size) allocsz = size;
uptr mem = (uptr)MmapOrDie(allocsz, "stack depot");
atomic_store(&region_end, mem + allocsz, memory_order_release);
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/sanitizer_common/sanitizer_printf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ int internal_snprintf(char *buffer, usize length, const char *format, ...) {

FORMAT(2, 3)
void InternalScopedString::append(const char *format, ...) {
uptr prev_len = length();
usize prev_len = length();

while (true) {
buffer_.resize(buffer_.capacity());
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/sanitizer_common/sanitizer_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ bool DontDumpShadowMemory(uptr addr, usize length) {
return true;
}

uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
uptr MapDynamicShadow(usize shadow_size_bytes, uptr shadow_scale,
uptr min_shadow_base_alignment,
UNUSED uptr &high_mem_end) {
const uptr granularity = GetMmapGranularity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ void TestReleaseFreeMemoryToOS() {

for (uptr class_id = 1; class_id <= Allocator::SizeClassMapT::kLargestClassID;
class_id++) {
const uptr chunk_size = Allocator::SizeClassMapT::Size(class_id);
const usize chunk_size = Allocator::SizeClassMapT::Size(class_id);
const uptr chunk_size_scaled = chunk_size >> Allocator::kCompactPtrScale;
const uptr max_chunks =
kAllocatedPagesCount * GetPageSizeCached() / chunk_size;
Expand Down