Skip to content

Commit

Permalink
[cilksan] Fix handling of a realloc when the allocation of the given …
Browse files Browse the repository at this point in the history
…old pointer is not instrumented.

Fixes issue OpenCilk/opencilk-project#218
  • Loading branch information
neboat committed Jan 6, 2024
1 parent 08a93c7 commit 15c8cd4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cilksan/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
36 changes: 36 additions & 0 deletions test/cilksan/TestCases/malloc-realloc-free.c
Original file line number Diff line number Diff line change
@@ -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 <stdlib.h>
#include <cilk/cilk.h>

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.

0 comments on commit 15c8cd4

Please sign in to comment.