-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Add aligned_alloc to macOS tsan interceptors #79198
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8a688b6
Initial working version
cjappl f00f129
Refactor, moving WEAK_IMPORT up to common defs
cjappl cc88ee1
Introduce new test file
cjappl 35b549e
Aligned alloc test cleaned up
cjappl 72dbcd2
Clean up for PR
cjappl 84f6f33
Simplify 'HAVE_ALIGNED_ALLOC' preprocessor
cjappl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// RUN: %clang_tsan -O1 %s -o %t -undefined dynamic_lookup | ||
// RUN: %deflake %run %t | FileCheck %s | ||
|
||
#include "test.h" | ||
|
||
#include <stdlib.h> | ||
|
||
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \ | ||
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101500 | ||
# define MACOS_VERSION_AT_LEAST_10_15 1 | ||
#else | ||
# define MACOS_VERSION_AT_LEAST_10_15 0 | ||
#endif | ||
|
||
#if defined(__cplusplus) && (__cplusplus >= 201703L) && \ | ||
(!defined(__apple__) || MACOS_VERSION_AT_LEAST_10_15) | ||
# define HAVE_ALIGNED_ALLOC 1 | ||
#else | ||
# define HAVE_ALIGNED_ALLOC 0 | ||
#endif | ||
|
||
int *mem; | ||
pthread_mutex_t mtx; | ||
|
||
void *Thread1(void *x) { | ||
pthread_mutex_lock(&mtx); | ||
free(mem); | ||
pthread_mutex_unlock(&mtx); | ||
barrier_wait(&barrier); | ||
return NULL; | ||
} | ||
|
||
__attribute__((noinline)) void *Thread2(void *x) { | ||
barrier_wait(&barrier); | ||
pthread_mutex_lock(&mtx); | ||
mem[0] = 42; | ||
pthread_mutex_unlock(&mtx); | ||
return NULL; | ||
} | ||
|
||
int main() { | ||
|
||
barrier_init(&barrier, 2); | ||
#if HAVE_ALIGNED_ALLOC | ||
mem = (int *)aligned_alloc(8, 8); | ||
#else | ||
mem = (int *)malloc(8); | ||
#endif | ||
pthread_mutex_init(&mtx, 0); | ||
pthread_t t; | ||
pthread_create(&t, NULL, Thread1, NULL); | ||
Thread2(0); | ||
pthread_join(t, NULL); | ||
pthread_mutex_destroy(&mtx); | ||
return 0; | ||
} | ||
|
||
// CHECK: WARNING: ThreadSanitizer: heap-use-after-free | ||
// CHECK: Write of size 4 at {{.*}} by main thread{{.*}}: | ||
// CHECK: #0 Thread2 | ||
// CHECK: #1 main | ||
// CHECK: Previous write of size 8 at {{.*}} by thread T1{{.*}}: | ||
// CHECK: #0 free | ||
// CHECK: #{{(1|2)}} Thread1 | ||
// CHECK: SUMMARY: ThreadSanitizer: heap-use-after-free{{.*}}Thread2 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we need to declare SANITIZER_WEAK_IMPORT for !APPLE ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, maybe @yln can help me answer it. This was following the pattern I found for libdispatch, I don't know if it is necessary or not.
llvm-project/compiler-rt/lib/tsan/rtl/tsan_interceptors_libdispatch.cpp
Lines 226 to 247 in f7669ba