Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cilksan] Fix handling of a realloc when the allocation of the given … #41

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.