From 93ac0bb50a2343493a4f1e518e026d79a03e6a0a Mon Sep 17 00:00:00 2001 From: TB Schardl Date: Tue, 21 Nov 2023 14:36:16 +0000 Subject: [PATCH] [cilksan] Fix handling of a realloc when the allocation of the given old pointer is not instrumented. Fixes issue OpenCilk/opencilk-project#218 --- cilksan/driver.cpp | 6 ++++ test/cilksan/TestCases/malloc-realloc-free.c | 36 ++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 test/cilksan/TestCases/malloc-realloc-free.c diff --git a/cilksan/driver.cpp b/cilksan/driver.cpp index d49fd61..468f024 100644 --- a/cilksan/driver.cpp +++ b/cilksan/driver.cpp @@ -877,6 +877,12 @@ void __csan_after_allocfn(const csi_id_t allocfn_id, } CilkSanImpl.record_alloc((size_t)addr, new_size, 2 * allocfn_id + 1); CilkSanImpl.malloc_sizes.remove((uintptr_t)addr); + } else { + // If we don't have a recorded size for this realloc, simply treat it as + // a malloc. This situation can occur if the previous malloc was not + // instrumented for some reason. + CilkSanImpl.record_alloc((size_t)addr, new_size, 2 * allocfn_id + 1); + CilkSanImpl.clear_shadow_memory((size_t)addr, new_size); } CilkSanImpl.malloc_sizes.insert((uintptr_t)addr, new_size); } diff --git a/test/cilksan/TestCases/malloc-realloc-free.c b/test/cilksan/TestCases/malloc-realloc-free.c new file mode 100644 index 0000000..95cf08c --- /dev/null +++ b/test/cilksan/TestCases/malloc-realloc-free.c @@ -0,0 +1,36 @@ +// Check that Cilksan correctly verifies that frees of realloc'd +// pointers do not race. +// +// Thanks to bababuck for providing the original source code for this +// issue. +// +// RUN: %clang_cilksan -fopencilk -O3 %s -o %t +// RUN: %run %t 2>&1 | FileCheck %s + +#include +#include + +void test(int *arr, int count) { + if (count == 0) { + free(arr); + return ; + } + + cilk_scope { + for (int i = 0; i < 15; ++i) { + int *new_arr = malloc(10 * sizeof(int)); + new_arr = realloc(new_arr, 10 * sizeof(int)); + cilk_spawn test(new_arr, count - 1); + } + } + return ; +} + +int main(int argc, char *argv[]) { + test(NULL, 1); +} + +// CHECK-NOT: Race detected on location + +// CHECK: Cilksan detected 0 distinct races. +// CHECK-NEXT: Cilksan suppressed 0 duplicate race reports.