Skip to content

Commit

Permalink
[cilksan] Fix bugs and compilation warnings when building and running…
Browse files Browse the repository at this point in the history
… Cilksan on MacOSX.
  • Loading branch information
neboat committed Jan 19, 2021
1 parent 6c9e28e commit 35c6b24
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 34 deletions.
6 changes: 5 additions & 1 deletion cilksan/cilksan_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
58 changes: 39 additions & 19 deletions cilksan/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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))
Expand All @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);

Expand All @@ -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);
}
Expand All @@ -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);

Expand All @@ -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);
}
Expand Down
17 changes: 5 additions & 12 deletions cilksan/hook_format.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 *);
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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 { \
Expand All @@ -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; \
} \
} \
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down
18 changes: 16 additions & 2 deletions cilksan/libhooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 35c6b24

Please sign in to comment.