Skip to content

Commit

Permalink
[compiler-rt] update detect_write_exec option for apple devices.
Browse files Browse the repository at this point in the history
Reviewed By: yln, vitalybuka

Differential Revision: https://reviews.llvm.org/D111390
  • Loading branch information
devnexen committed Oct 28, 2021
1 parent 9ed528e commit bb168f3
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions compiler-rt/lib/dfsan/dfsan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
int fd, OFF_T offset) {
if (common_flags()->detect_write_exec)
ReportMmapWriteExec(prot);
ReportMmapWriteExec(prot, flags);
if (!__dfsan::dfsan_inited)
return (void *)internal_mmap(addr, length, prot, flags, fd, offset);
COMMON_INTERCEPTOR_ENTER(mmap, addr, length, prot, flags, fd, offset);
Expand All @@ -171,7 +171,7 @@ INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags,
int fd, OFF64_T offset) {
if (common_flags()->detect_write_exec)
ReportMmapWriteExec(prot);
ReportMmapWriteExec(prot, flags);
if (!__dfsan::dfsan_inited)
return (void *)internal_mmap(addr, length, prot, flags, fd, offset);
COMMON_INTERCEPTOR_ENTER(mmap64, addr, length, prot, flags, fd, offset);
Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/sanitizer_common/sanitizer_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ void ReportErrorSummary(const char *error_type, const AddressInfo &info,
void ReportErrorSummary(const char *error_type, const StackTrace *trace,
const char *alt_tool_name = nullptr);

void ReportMmapWriteExec(int prot);
void ReportMmapWriteExec(int prot, int mflags);

// Math
#if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7418,7 +7418,7 @@ INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
OFF_T off) {
void *ctx;
if (common_flags()->detect_write_exec)
ReportMmapWriteExec(prot);
ReportMmapWriteExec(prot, flags);
if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
Expand All @@ -7428,7 +7428,7 @@ INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
void *ctx;
if (common_flags()->detect_write_exec)
ReportMmapWriteExec(prot);
ReportMmapWriteExec(prot, 0);
if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
return (int)internal_mprotect(addr, sz, prot);
COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
Expand All @@ -7447,7 +7447,7 @@ INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
OFF64_T off) {
void *ctx;
if (common_flags()->detect_write_exec)
ReportMmapWriteExec(prot);
ReportMmapWriteExec(prot, flags);
if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
Expand Down
10 changes: 8 additions & 2 deletions compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,17 @@ void ReportErrorSummary(const char *error_type, const StackTrace *stack,
#endif
}

void ReportMmapWriteExec(int prot) {
void ReportMmapWriteExec(int prot, int flags) {
#if SANITIZER_POSIX && (!SANITIZER_GO && !SANITIZER_ANDROID)
if ((prot & (PROT_WRITE | PROT_EXEC)) != (PROT_WRITE | PROT_EXEC))
int pflags = (PROT_WRITE | PROT_EXEC);
if ((prot & pflags) != pflags)
return;

# if SANITIZER_MAC && defined(MAP_JIT)
if ((flags & MAP_JIT) == MAP_JIT)
return;
# endif

ScopedErrorReportLock l;
SanitizerCommonDecorator d;

Expand Down
2 changes: 1 addition & 1 deletion compiler-rt/lib/tsan/rtl/tsan_platform_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ void InitializePlatformEarly() {
uptr max_vm = GetMaxUserVirtualAddress() + 1;
if (max_vm != HiAppMemEnd()) {
Printf("ThreadSanitizer: unsupported vm address limit %p, expected %p.\n",
max_vm, HiAppMemEnd());
(void *)max_vm, (void *)HiAppMemEnd());
Die();
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// TODO: Fix option on Android, it hangs there for unknown reasons.
// XFAIL: android

#include <pthread.h>
#include <stdio.h>
#include <sys/mman.h>

Expand All @@ -30,6 +31,12 @@ int main(int argc, char **argv) {
MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
(void)mprotect(q, 64, PROT_READ | PROT_EXEC);
// CHECK-NOT: Sanitizer
#if defined(__APPLE__)
pthread_jit_write_protect_np(false);
char *c = (char *)mmap(0, 128, PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_PRIVATE | MAP_JIT, -1, 0);
// CHECK-NOT: Sanitizer
#endif

printf("done\n");
// CHECK-DISABLED-NOT: Sanitizer
Expand Down

0 comments on commit bb168f3

Please sign in to comment.