-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[ASan] [HWASan] Add __sanitizer_ignore_free_hook() #96749
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
Merged
Merged
Changes from 2 commits
Commits
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
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
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
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
119 changes: 119 additions & 0 deletions
119
compiler-rt/test/asan/TestCases/Posix/ignore_free_hook.cpp
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,119 @@ | ||
// Check that free hook doesn't conflict with Realloc. | ||
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=basic_hook_works && not %run %t \ | ||
// RUN: |& FileCheck %s -check-prefix=CHECK-BASIC | ||
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=ignore && %run %t \ | ||
hctim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// RUN: |& FileCheck %s -check-prefix=CHECK-IGNORE | ||
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=ignore_twice && not %run %t \ | ||
// RUN: |& FileCheck %s -check-prefix=CHECK-IGNORE-2 | ||
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=mismatch && not %run %t \ | ||
// RUN: |& FileCheck %s -check-prefix=CHECK-MISMATCH | ||
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=ignore_mismatch && %run %t \ | ||
// RUN: |& FileCheck %s -check-prefix=CHECK-IGNORE-MISMATCH | ||
// RUN: %clangxx_asan -O2 %s -o %t -DTEST=double_delete && not %run %t \ | ||
// RUN: |& FileCheck %s -check-prefix=CHECK-DOUBLE-DELETE | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static char *volatile glob_ptr; | ||
bool ignore_free = false; | ||
|
||
#if (__APPLE__) | ||
// Required for dyld macOS 12.0+ | ||
# define WEAK_ON_APPLE __attribute__((weak)) | ||
#else // !(__APPLE__) | ||
# define WEAK_ON_APPLE | ||
#endif // (__APPLE__) | ||
|
||
extern "C" { | ||
WEAK_ON_APPLE void __sanitizer_free_hook(const volatile void *ptr) { | ||
if (ptr == glob_ptr) | ||
fprintf(stderr, "Free Hook\n"); | ||
} | ||
|
||
WEAK_ON_APPLE int __sanitizer_ignore_free_hook(const volatile void *ptr) { | ||
if (ptr != glob_ptr) | ||
return 0; | ||
fprintf(stderr, ignore_free ? "Free Ignored\n" : "Free Respected\n"); | ||
return ignore_free; | ||
} | ||
} // extern "C" | ||
|
||
void allocate() { glob_ptr = reinterpret_cast<char *volatile>(malloc(100)); } | ||
void deallocate() { free(reinterpret_cast<void *>(glob_ptr)); } | ||
|
||
void basic_hook_works() { | ||
allocate(); | ||
deallocate(); // CHECK-BASIC-NOT: Free Ignored | ||
// CHECK-BASIC: Free Respected | ||
// CHECK-BASIC: Free Hook | ||
*glob_ptr = 0; // CHECK-BASIC: AddressSanitizer: heap-use-after-free | ||
} | ||
|
||
void ignore() { | ||
allocate(); | ||
ignore_free = true; | ||
deallocate(); | ||
// CHECK-IGNORE: Free Ignored | ||
// CHECK-IGNORE-NOT: Free Respected | ||
// CHECK-IGNORE-NOT: Free Hook | ||
// CHECK-IGNORE-NOT: AddressSanitizer | ||
*glob_ptr = 0; | ||
} | ||
|
||
void ignore_twice() { | ||
allocate(); | ||
ignore_free = true; | ||
deallocate(); // CHECK-IGNORE-2: Free Ignored | ||
*glob_ptr = 0; | ||
ignore_free = false; | ||
deallocate(); // CHECK-IGNORE-2-NOT: Free Ignored | ||
// CHECK-IGNORE-2: Free Respected | ||
// CHECK-IGNORE-2: Free Hook | ||
*glob_ptr = 0; // CHECK-IGNORE-2: AddressSanitizer: heap-use-after-free | ||
} | ||
|
||
void ignore_a_lot() { | ||
allocate(); | ||
ignore_free = true; | ||
for (int i = 0; i < 10000; ++i) { | ||
deallocate(); // CHECK-IGNORE-3: Free Ignored | ||
*glob_ptr = 0; | ||
} | ||
ignore_free = false; | ||
deallocate(); // CHECK-IGNORE-3: Free Respected | ||
// CHECK-IGNORE-3: Free Hook | ||
*glob_ptr = 0; // CHECK-IGNORE-3: AddressSanitizer: heap-use-after-free | ||
} | ||
|
||
void mismatch() { | ||
glob_ptr = new char; | ||
deallocate(); // CHECK-MISMATCH: AddressSanitizer: alloc-dealloc-mismatch | ||
} | ||
|
||
void ignore_mismatch() { | ||
glob_ptr = new char; | ||
ignore_free = true; | ||
// Mismatch isn't detected when the free() is ignored. | ||
deallocate(); | ||
deallocate(); | ||
ignore_free = false; | ||
// And also isn't detected when the memory is free()-d for real. | ||
deallocate(); // CHECK-IGNORE-MISMATCH-NOT: AddressSanitizer: alloc-dealloc-mismatch | ||
} | ||
|
||
void double_delete() { | ||
allocate(); | ||
ignore_free = true; | ||
deallocate(); // CHECK-DOUBLE-DELETE: Free Ignored | ||
deallocate(); // CHECK-DOUBLE-DELETE: Free Ignored | ||
ignore_free = false; | ||
deallocate(); // CHECK-DOUBLE-DELETE: Free Respected | ||
// CHECK-DOUBLE-DELETE: Free Hook | ||
deallocate(); // CHECK-DOUBLE-DELETE: AddressSanitizer: attempting double-free | ||
} | ||
|
||
int main() { | ||
TEST(); | ||
return 0; | ||
} |
101 changes: 101 additions & 0 deletions
101
compiler-rt/test/hwasan/TestCases/Posix/ignore_free_hook.cpp
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,101 @@ | ||
// Check that free hook doesn't conflict with Realloc. | ||
// RUN: %clangxx_hwasan -O2 %s -o %t -DTEST=basic_hook_works && not %run %t \ | ||
// RUN: |& FileCheck %s -check-prefix=CHECK-BASIC | ||
// RUN: %clangxx_hwasan -O2 %s -o %t -DTEST=ignore && %run %t \ | ||
// RUN: |& FileCheck %s -check-prefix=CHECK-IGNORE | ||
// RUN: %clangxx_hwasan -O2 %s -o %t -DTEST=ignore_twice && not %run %t \ | ||
// RUN: |& FileCheck %s -check-prefix=CHECK-IGNORE-2 | ||
// RUN: %clangxx_hwasan -O2 %s -o %t -DTEST=double_delete && not %run %t \ | ||
// RUN: |& FileCheck %s -check-prefix=CHECK-DOUBLE-DELETE | ||
|
||
#include <sanitizer/hwasan_interface.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
static char *volatile glob_ptr; | ||
bool ignore_free = false; | ||
|
||
#if (__APPLE__) | ||
// Required for dyld macOS 12.0+ | ||
# define WEAK_ON_APPLE __attribute__((weak)) | ||
#else // !(__APPLE__) | ||
# define WEAK_ON_APPLE | ||
#endif // (__APPLE__) | ||
|
||
extern "C" { | ||
WEAK_ON_APPLE void __sanitizer_free_hook(const volatile void *ptr) { | ||
if (ptr == glob_ptr) | ||
fprintf(stderr, "Free Hook\n"); | ||
} | ||
|
||
WEAK_ON_APPLE int __sanitizer_ignore_free_hook(const volatile void *ptr) { | ||
if (ptr != glob_ptr) | ||
return 0; | ||
fprintf(stderr, ignore_free ? "Free Ignored\n" : "Free Respected\n"); | ||
return ignore_free; | ||
} | ||
} // extern "C" | ||
|
||
void allocate() { glob_ptr = reinterpret_cast<char *volatile>(malloc(100)); } | ||
void deallocate() { free(reinterpret_cast<void *>(glob_ptr)); } | ||
|
||
void basic_hook_works() { | ||
allocate(); | ||
deallocate(); // CHECK-BASIC-NOT: Free Ignored | ||
// CHECK-BASIC: Free Respected | ||
// CHECK-BASIC: Free Hook | ||
*glob_ptr = 0; // CHECK-BASIC: HWAddressSanitizer: tag-mismatch | ||
} | ||
|
||
void ignore() { | ||
allocate(); | ||
ignore_free = true; | ||
deallocate(); | ||
// CHECK-IGNORE: Free Ignored | ||
// CHECK-IGNORE-NOT: Free Respected | ||
// CHECK-IGNORE-NOT: Free Hook | ||
// CHECK-IGNORE-NOT: HWAddressSanitizer | ||
*glob_ptr = 0; | ||
} | ||
|
||
void ignore_twice() { | ||
allocate(); | ||
ignore_free = true; | ||
deallocate(); // CHECK-IGNORE-2: Free Ignored | ||
*glob_ptr = 0; | ||
ignore_free = false; | ||
deallocate(); // CHECK-IGNORE-2-NOT: Free Ignored | ||
// CHECK-IGNORE-2: Free Respected | ||
// CHECK-IGNORE-2: Free Hook | ||
*glob_ptr = 0; // CHECK-IGNORE-2: HWAddressSanitizer: tag-mismatch | ||
} | ||
|
||
void ignore_a_lot() { | ||
allocate(); | ||
ignore_free = true; | ||
for (int i = 0; i < 10000; ++i) { | ||
deallocate(); // CHECK-IGNORE-3: Free Ignored | ||
*glob_ptr = 0; | ||
} | ||
ignore_free = false; | ||
deallocate(); // CHECK-IGNORE-3: Free Respected | ||
// CHECK-IGNORE-3: Free Hook | ||
*glob_ptr = 0; // CHECK-IGNORE-3: HWAddressSanitizer: tag-mismatch | ||
} | ||
|
||
void double_delete() { | ||
allocate(); | ||
ignore_free = true; | ||
deallocate(); // CHECK-DOUBLE-DELETE: Free Ignored | ||
deallocate(); // CHECK-DOUBLE-DELETE: Free Ignored | ||
ignore_free = false; | ||
deallocate(); // CHECK-DOUBLE-DELETE: Free Respected | ||
// CHECK-DOUBLE-DELETE: Free Hook | ||
deallocate(); // CHECK-DOUBLE-DELETE: HWAddressSanitizer: invalid-free | ||
} | ||
|
||
int main() { | ||
__hwasan_enable_allocator_tagging(); | ||
TEST(); | ||
return 0; | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.