From 35c6b24b75b284f9dcd52c580f8b751497cebb1b Mon Sep 17 00:00:00 2001 From: TB Schardl Date: Tue, 19 Jan 2021 10:41:03 -0500 Subject: [PATCH] [cilksan] Fix bugs and compilation warnings when building and running Cilksan on MacOSX. --- cilksan/cilksan_internal.h | 6 +++- cilksan/driver.cpp | 58 +++++++++++++++++++++++++------------- cilksan/hook_format.inc | 17 ++++------- cilksan/libhooks.cpp | 18 ++++++++++-- 4 files changed, 65 insertions(+), 34 deletions(-) diff --git a/cilksan/cilksan_internal.h b/cilksan/cilksan_internal.h index f29709e..9728c1f 100644 --- a/cilksan/cilksan_internal.h +++ b/cilksan/cilksan_internal.h @@ -15,13 +15,17 @@ #include "shadow_mem_allocator.h" #include "stack.h" +extern bool TOOL_INITIALIZED; + // Forward declarations class SimpleShadowMem; // Top-level class implementing the tool. class CilkSanImpl_t { public: - CilkSanImpl_t() : color_report(ColorizeReports()) {} + CilkSanImpl_t() : color_report(ColorizeReports()) { + TOOL_INITIALIZED = true; + } ~CilkSanImpl_t(); MALineAllocator &getMALineAllocator(unsigned Idx) { diff --git a/cilksan/driver.cpp b/cilksan/driver.cpp index c6ae5a7..f9b954d 100644 --- a/cilksan/driver.cpp +++ b/cilksan/driver.cpp @@ -212,7 +212,6 @@ CILKSAN_API void __csan_init() { // to shutdown and cleanup the tool at program termination. init_internal(); - TOOL_INITIALIZED = true; } // Helper function to grow a map from CSI ID to program counter (PC). @@ -274,6 +273,9 @@ static inline void handle_stack_switch(uintptr_t bp, uintptr_t sp) { CILKSAN_API void __csan_func_entry(const csi_id_t func_id, const void *bp, const void *sp, const func_prop_t prop) { + if (!TOOL_INITIALIZED) + return; + { // Handle tool initialization as a special case. static bool first_call = true; if (first_call) { @@ -311,7 +313,6 @@ CILKSAN_API void __csan_func_entry(const csi_id_t func_id, srcloc->name, srcloc->filename, srcloc->line_number); }); - cilksan_assert(TOOL_INITIALIZED); // Propagate the parallel-execution state to the child. uint8_t current_pe = parallel_execution.back(); @@ -340,10 +341,12 @@ CILKSAN_API void __csan_func_entry(const csi_id_t func_id, CILKSAN_API void __csan_func_exit(const csi_id_t func_exit_id, const csi_id_t func_id, const func_exit_prop_t prop) { + if (!TOOL_INITIALIZED) + return; + if (!should_check()) return; - cilksan_assert(TOOL_INITIALIZED); #if CILKSAN_DEBUG const csan_source_loc_t *srcloc = __csan_get_func_exit_source_loc(func_exit_id); #endif @@ -597,10 +600,11 @@ CILKSAN_API void __csan_detach_continue(const csi_id_t detach_continue_id, // Hook called at a sync CILKSAN_API void __csan_sync(csi_id_t sync_id, const unsigned sync_reg) { - if (!should_check()) + if (!TOOL_INITIALIZED) return; - cilksan_assert(TOOL_INITIALIZED); + if (!should_check()) + return; // Because this is a serial tool, we can safely perform all operations related // to a sync. @@ -623,7 +627,9 @@ CILKSAN_API void __csan_sync(csi_id_t sync_id, const unsigned sync_reg) { CILKSAN_API void __csan_load(csi_id_t load_id, const void *addr, int32_t size, load_prop_t prop) { - cilksan_assert(TOOL_INITIALIZED); + if (!TOOL_INITIALIZED) + return; + if (!should_check()) { DBG_TRACE(DEBUG_MEMORY, "SKIP %s read (%p, %ld)\n", __FUNCTION__, addr, size); @@ -657,7 +663,9 @@ void __csan_load(csi_id_t load_id, const void *addr, int32_t size, CILKSAN_API void __csan_large_load(csi_id_t load_id, const void *addr, size_t size, load_prop_t prop) { - cilksan_assert(TOOL_INITIALIZED); + if (!TOOL_INITIALIZED) + return; + if (!should_check()) { DBG_TRACE(DEBUG_MEMORY, "SKIP %s read (%p, %ld)\n", __FUNCTION__, addr, size); @@ -691,7 +699,9 @@ void __csan_large_load(csi_id_t load_id, const void *addr, size_t size, CILKSAN_API void __csan_store(csi_id_t store_id, const void *addr, int32_t size, store_prop_t prop) { - cilksan_assert(TOOL_INITIALIZED); + if (!TOOL_INITIALIZED) + return; + if (!should_check()) { DBG_TRACE(DEBUG_MEMORY, "SKIP %s wrote (%p, %ld)\n", __FUNCTION__, addr, size); @@ -726,7 +736,9 @@ void __csan_store(csi_id_t store_id, const void *addr, int32_t size, CILKSAN_API void __csan_large_store(csi_id_t store_id, const void *addr, size_t size, store_prop_t prop) { - cilksan_assert(TOOL_INITIALIZED); + if (!TOOL_INITIALIZED) + return; + if (!should_check()) { DBG_TRACE(DEBUG_MEMORY, "SKIP %s wrote (%p, %ld)\n", __FUNCTION__, addr, size); @@ -764,7 +776,9 @@ void __csan_large_store(csi_id_t store_id, const void *addr, size_t size, CILKSAN_API void __csi_after_alloca(const csi_id_t alloca_id, const void *addr, size_t size, const alloca_prop_t prop) { - cilksan_assert(TOOL_INITIALIZED); + if (!TOOL_INITIALIZED) + return; + if (!should_check()) return; @@ -791,7 +805,9 @@ CILKSAN_API void __csan_after_allocfn(const csi_id_t allocfn_id, const void *addr, size_t size, size_t num, size_t alignment, const void *oldaddr, const allocfn_prop_t prop) { - cilksan_assert(TOOL_INITIALIZED); + if (!TOOL_INITIALIZED) + return; + if (!should_check()) return; @@ -879,7 +895,9 @@ CILKSAN_API void __csan_alloc_posix_memalign(const csi_id_t allocfn_id, const allocfn_prop_t prop, int result, void **ptr, size_t alignment, size_t size) { - cilksan_assert(TOOL_INITIALIZED); + if (!TOOL_INITIALIZED) + return; + if (!should_check()) return; if (__builtin_expect(!allocfn_pc[allocfn_id], false)) @@ -900,7 +918,9 @@ CILKSAN_API void __csan_alloc_posix_memalign(const csi_id_t allocfn_id, CILKSAN_API void __csan_after_free(const csi_id_t free_id, const void *ptr, const free_prop_t prop) { - cilksan_assert(TOOL_INITIALIZED); + if (!TOOL_INITIALIZED) + return; + if (!should_check()) return; @@ -1274,7 +1294,7 @@ typedef cilkred_map *(*merge_two_rmaps_t)(__cilkrts_worker *, cilkred_map *, cilkred_map *); static merge_two_rmaps_t dl___cilkrts_internal_merge_two_rmaps = NULL; -CILKSAN_API __attribute__((weak)) cilkred_map * +CILKSAN_API cilkred_map * __cilkrts_internal_merge_two_rmaps(__cilkrts_worker *ws, cilkred_map *left, cilkred_map *right) { START_DL_INTERPOSER(__cilkrts_internal_merge_two_rmaps, merge_two_rmaps_t); @@ -1288,7 +1308,7 @@ __cilkrts_internal_merge_two_rmaps(__cilkrts_worker *ws, cilkred_map *left, /// Wrapped __cilkrts_internal_merge_two_rmaps method for link-time /// interpositioning. -CILKSAN_API __attribute__((weak)) cilkred_map * +CILKSAN_API cilkred_map * __real___cilkrts_internal_merge_two_rmaps(__cilkrts_worker *ws, cilkred_map *left, cilkred_map *right) { @@ -1309,7 +1329,7 @@ cilkred_map *__wrap___cilkrts_internal_merge_two_rmaps(__cilkrts_worker *ws, typedef void *(*hyper_alloc_t)(void *, size_t); static hyper_alloc_t dl___cilkrts_hyper_alloc = NULL; -CILKSAN_API __attribute__((weak)) void* +CILKSAN_API void* __cilkrts_hyper_alloc(void *ignored, size_t bytes) { START_DL_INTERPOSER(__cilkrts_hyper_alloc, hyper_alloc_t); @@ -1321,7 +1341,7 @@ __cilkrts_hyper_alloc(void *ignored, size_t bytes) { } /// Wrapped __cilkrts_hyper_alloc method for link-time interpositioning. -CILKSAN_API __attribute__((weak)) void* +CILKSAN_API void* __real___cilkrts_hyper_alloc(void *ignored, size_t bytes) { return __cilkrts_hyper_alloc(ignored, bytes); } @@ -1339,7 +1359,7 @@ void *__wrap___cilkrts_hyper_alloc(void *ignored, size_t bytes) { typedef void (*hyper_dealloc_t)(void *, void *); static hyper_dealloc_t dl___cilkrts_hyper_dealloc = NULL; -CILKSAN_API __attribute__((weak)) void +CILKSAN_API void __cilkrts_hyper_dealloc(void *ignored, void *view) { START_DL_INTERPOSER(__cilkrts_hyper_dealloc, hyper_dealloc_t); @@ -1353,7 +1373,7 @@ __cilkrts_hyper_dealloc(void *ignored, void *view) { } /// Wrapped __cilkrts_hyper_alloc method for link-time interpositioning. -CILKSAN_API __attribute__((weak)) void +CILKSAN_API void __real___cilkrts_hyper_dealloc(void *ignored, void *view) { __cilkrts_hyper_dealloc(ignored, view); } diff --git a/cilksan/hook_format.inc b/cilksan/hook_format.inc index 4b472e4..eac8afc 100644 --- a/cilksan/hook_format.inc +++ b/cilksan/hook_format.inc @@ -328,7 +328,6 @@ static void scanf_common(csi_id_t call_id, unsigned &MAAP_count, int n_inputs, CHECK_GT(n_inputs, 0); const char *p = format; - // COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1); CHECK_READ_RANGE(format, strlen(format) + 1); while (*p) { @@ -351,8 +350,8 @@ static void scanf_common(csi_id_t call_id, unsigned &MAAP_count, int n_inputs, int size = scanf_get_value_size(&dir); if (size == FSS_INVALID) { fprintf(err_io, - "Cilksan Warning: Unexpected format specifier in scanf hook: ", - "%.*s\n", dir.end - dir.begin, dir.begin); + "Cilksan Warning: Unexpected format specifier in scanf hook: " + "%.*s\n", (int)(dir.end - dir.begin), dir.begin); break; } void *argp = va_arg(aq, void *); @@ -366,7 +365,6 @@ static void scanf_common(csi_id_t call_id, unsigned &MAAP_count, int n_inputs, // FIXME: actually use wcslen() to calculate it. size = 0; } - // COMMON_INTERCEPTOR_WRITE_RANGE(ctx, argp, size); CHECK_WRITE_RANGE(argp, size); } } @@ -491,7 +489,7 @@ static int printf_get_value_size(PrintfDirective *dir) { fprintf(err_io, \ "Cilksan Warning: Unexpected floating-point arg size" \ " in printf hook: %d\n", \ - size); \ + (int)size); \ return; \ } \ } else { \ @@ -508,7 +506,7 @@ static int printf_get_value_size(PrintfDirective *dir) { fprintf(err_io, \ "Cilksan Warning: Unexpected arg size" \ " in printf hook: %d\n", \ - size); \ + (int)size); \ return; \ } \ } \ @@ -522,8 +520,6 @@ static int printf_get_value_size(PrintfDirective *dir) { // Process format string and va_list, and report all load ranges. static void printf_common(csi_id_t call_id, unsigned &MAAP_count, const char *format, va_list aq) { - // COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1); - // check_read_bytes(call_id, MAAPVal, format, strlen(format) + 1); CHECK_READ_RANGE(format, strlen(format) + 1); const char *p = format; @@ -561,12 +557,11 @@ static void printf_common(csi_id_t call_id, unsigned &MAAP_count, fprintf(err_io, "Cilksan Warning: Unexpected format specifier in printf " "hook: %.*s (reported once per process)\n", - dir.end - dir.begin, dir.begin); + (int)(dir.end - dir.begin), dir.begin); break; } if (dir.convSpecifier == 'n') { void *argp = va_arg(aq, void *); - // COMMON_INTERCEPTOR_WRITE_RANGE(ctx, argp, size); CHECK_WRITE_RANGE(argp, size); continue; } else if (size == FSS_STRLEN) { @@ -583,14 +578,12 @@ static void printf_common(csi_id_t call_id, unsigned &MAAP_count, // Whole string will be accessed. size = strlen((const char *)argp) + 1; } - // COMMON_INTERCEPTOR_READ_RANGE(ctx, argp, size); CHECK_READ_RANGE(argp, size); } } else if (size == FSS_WCSLEN) { if (void *argp = va_arg(aq, void *)) { // FIXME: Properly support wide-character strings (via wcsrtombs). size = 0; - // COMMON_INTERCEPTOR_READ_RANGE(ctx, argp, size); CHECK_READ_RANGE(argp, size); } } else { diff --git a/cilksan/libhooks.cpp b/cilksan/libhooks.cpp index 80c5cf7..942524a 100644 --- a/cilksan/libhooks.cpp +++ b/cilksan/libhooks.cpp @@ -9,8 +9,7 @@ #include "driver.h" #define START_HOOK(call_id) \ - cilksan_assert(TOOL_INITIALIZED); \ - if (!should_check()) \ + if (!TOOL_INITIALIZED || !should_check()) \ return; \ if (__builtin_expect(!call_pc[call_id], false)) \ call_pc[call_id] = CALLERPC; \ @@ -67,6 +66,9 @@ static inline void check_write_bytes(csi_id_t call_id, MAAP_t MAAPVal, CILKSAN_API void __csan_default_libhook(const csi_id_t call_id, const csi_id_t func_id, unsigned MAAP_count) { + if (!TOOL_INITIALIZED) + return; + cilksan_assert(TOOL_INITIALIZED); if (!should_check()) return; @@ -245,6 +247,9 @@ CILKSAN_API void __csan_llvm_stacksave(const csi_id_t call_id, const csi_id_t func_id, unsigned MAAP_count, const call_prop_t prop, void *sp) { + if (!TOOL_INITIALIZED) + return; + cilksan_assert(TOOL_INITIALIZED); if (!should_check()) return; @@ -278,6 +283,9 @@ CILKSAN_API void __csan_llvm_va_start(const csi_id_t call_id, const csi_id_t func_id, unsigned MAAP_count, const call_prop_t prop, va_list ap) { + if (!TOOL_INITIALIZED) + return; + cilksan_assert(TOOL_INITIALIZED); if (!should_check()) return; @@ -289,6 +297,9 @@ CILKSAN_API void __csan_llvm_va_start(const csi_id_t call_id, CILKSAN_API void __csan_llvm_va_end(const csi_id_t call_id, const csi_id_t func_id, unsigned MAAP_count, const call_prop_t prop, va_list ap) { + if (!TOOL_INITIALIZED) + return; + cilksan_assert(TOOL_INITIALIZED); if (!should_check()) return; @@ -339,6 +350,9 @@ CILKSAN_API void __csan___cxa_atexit(const csi_id_t call_id, const csi_id_t func_id, unsigned MAAP_count, const call_prop_t prop, int result, void (*func)(void *), void *arg, void *dso_handle) { + if (!TOOL_INITIALIZED) + return; + cilksan_assert(TOOL_INITIALIZED); for (unsigned i = 0; i < MAAP_count; ++i)