Skip to content

Commit

Permalink
[asan][windows] Make tests more flexable (llvm#85274)
Browse files Browse the repository at this point in the history
Contains test changes from
llvm#81677 that are simple test
changes. For the most part just makes the tests allow more flexibility
in the callstacks produced by asan.

Note: this PR has the exact changes from
llvm#81677 in addition to a revert
commit to remove the ones that require other things from that PR. This
will be squashed, ofc. I just left things unsquashed for reviewing
pleasure :).

This is a non-functional-change
  • Loading branch information
barcharcraz authored Mar 15, 2024
1 parent b43965a commit 0360f32
Show file tree
Hide file tree
Showing 25 changed files with 143 additions and 241 deletions.
14 changes: 7 additions & 7 deletions compiler-rt/test/asan/TestCases/Windows/bitfield_uaf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ void make_access(S *s) {
int main(void) {
S *s = (S*)malloc(sizeof(S));
free(s);
// CHECK: [[ADDR]] is located 0 bytes inside of 4-byte region
// CHECK-LABEL: freed by thread T0 here:
// CHECK: {{#0 .* free }}
// CHECK: {{#1 .* main .*bitfield_uaf.cpp}}:[[@LINE-4]]
// CHECK-LABEL: previously allocated by thread T0 here:
// CHECK: {{#0 .* malloc }}
// CHECK: {{#1 .* main .*bitfield_uaf.cpp}}:[[@LINE-8]]
// CHECK: [[ADDR]] is located 0 bytes inside of 4-byte region
// CHECK-LABEL: freed by thread T0 here:
// CHECK: {{#0 .* free }}
// CHECK: {{ #[1-2] .* main .*bitfield_uaf.cpp}}:[[@LINE-4]]
// CHECK-LABEL: previously allocated by thread T0 here:
// CHECK: {{#0 .* malloc }}
// CHECK: {{ #[1-2] .* main .*bitfield_uaf.cpp}}:[[@LINE-8]]
make_access(s);
return 0;
}
Expand Down
14 changes: 7 additions & 7 deletions compiler-rt/test/asan/TestCases/Windows/calloc_left_oob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
int main() {
int *buffer = (int*)calloc(42, sizeof(int));
buffer[-1] = 42;
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 4 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*calloc_left_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 4 bytes before 168-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* calloc }}
// CHECK-NEXT: {{#1 .* main .*calloc_left_oob.cpp}}:[[@LINE-8]]
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 4 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*calloc_left_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 4 bytes before 168-byte region
// CHECK: allocated by thread T0 here:
// CHECK: {{#0 .* calloc }}
// CHECK: {{ #[1-2] .* main .*calloc_left_oob.cpp}}:[[@LINE-8]]
free(buffer);
}
14 changes: 7 additions & 7 deletions compiler-rt/test/asan/TestCases/Windows/calloc_right_oob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
int main() {
int *buffer = (int*)calloc(42, sizeof(int));
buffer[42] = 42;
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 4 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*calloc_right_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes after 168-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* calloc }}
// CHECK-NEXT: {{#1 .* main .*calloc_right_oob.cpp}}:[[@LINE-8]]
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 4 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*calloc_right_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes after 168-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* calloc }}
// CHECK: {{ #[1-2] .* main .*calloc_right_oob.cpp}}:[[@LINE-8]]
free(buffer);
}
20 changes: 10 additions & 10 deletions compiler-rt/test/asan/TestCases/Windows/calloc_uaf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ int main() {
int *buffer = (int*)calloc(42, sizeof(int));
free(buffer);
buffer[0] = 42;
// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 4 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*calloc_uaf.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes inside of 168-byte region
// CHECK: freed by thread T0 here:
// CHECK-NEXT: {{#0 .* free }}
// CHECK-NEXT: {{#1 .* main .*calloc_uaf.cpp}}:[[@LINE-8]]
// CHECK: previously allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* calloc }}
// CHECK-NEXT: {{#1 .* main .*calloc_uaf.cpp}}:[[@LINE-12]]
// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 4 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*calloc_uaf.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes inside of 168-byte region
// CHECK: freed by thread T0 here:
// CHECK-NEXT: {{#0 .* free }}
// CHECK: {{ #[1-2] .* main .*calloc_uaf.cpp}}:[[@LINE-8]]
// CHECK: previously allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* calloc }}
// CHECK: {{ #[1-2] .* main .*calloc_uaf.cpp}}:[[@LINE-12]]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %clang_cl %LD %s %Fe%t.dll -DHEAP_LIBRARY %MD \
// RUN: %if target={{.*-windows-gnu}} %{ -Wl,--out-implib,%t.lib %}
// RUN: %clang_cl %s %t.lib %Fe%t -fsanitize=address %MT
// RUN: %clang_cl_asan %s %t.lib %Fe%t
// RUN: %run %t 2>&1 | FileCheck %s

// Check that ASan does not fail when releasing allocations that occurred within
Expand Down
42 changes: 0 additions & 42 deletions compiler-rt/test/asan/TestCases/Windows/dll_host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,6 @@
// Just make sure we can compile this.
// The actual compile&run sequence is to be done by the DLL tests.
// RUN: %clang_cl_asan -Od %s -Fe%t
//
// Get the list of ASan wrappers exported by the main module RTL:
// note: The mangling decoration (i.e. @4 )is removed because calling convention
// differ from 32-bit and 64-bit.
// RUN: dumpbin /EXPORTS %t | grep -o "__asan_wrap[^ ]*" | sed -e s/@.*// > %t.exports1
//
// The exception handlers differ in 32-bit and 64-bit, so we ignore them:
// RUN: grep '[E]XPORT:' %s | sed -e 's/.*[E]XPORT: //' > %t.exports2
// EXPORT: __asan_wrap__except_handler3
// EXPORT: __asan_wrap__except_handler4
// EXPORT: __asan_wrap___C_specific_handler
//
// Get the list of ASan wrappers imported by the DLL RTL:
// [BEWARE: be really careful with the sed commands, as this test can be run
// from different environments with different shells and seds]
// RUN: grep INTERCEPT_LIBRARY_FUNCTION %p/../../../../lib/asan/asan_win_dll_thunk.cpp \
// RUN: | grep -v define | sed -e s/.*(/__asan_wrap_/ -e s/).*// \
// RUN: > %t.imports1
//
// Add functions interecepted in asan_malloc.win.cpp and asan_win.cpp.
// RUN: grep '[I]MPORT:' %s | sed -e 's/.*[I]MPORT: //' > %t.imports2
// IMPORT: __asan_wrap_HeapAlloc
// IMPORT: __asan_wrap_HeapFree
// IMPORT: __asan_wrap_HeapReAlloc
// IMPORT: __asan_wrap_HeapSize
// IMPORT: __asan_wrap_CreateThread
// IMPORT: __asan_wrap_RaiseException
// IMPORT: __asan_wrap_RtlRaiseException
// IMPORT: __asan_wrap_SetUnhandledExceptionFilter
// IMPORT: __asan_wrap_RtlSizeHeap
// IMPORT: __asan_wrap_RtlAllocateHeap
// IMPORT: __asan_wrap_RtlReAllocateHeap
// IMPORT: __asan_wrap_RtlFreeHeap
//
// RUN: cat %t.imports1 %t.imports2 | sort | uniq > %t.imports-sorted
// RUN: cat %t.exports1 %t.exports2 | sort | uniq > %t.exports-sorted
//
// Now make sure the DLL thunk imports everything:
// RUN: echo
// RUN: echo "=== NOTE === If you see a mismatch below, please update asan_win_dll_thunk.cpp"
// RUN: diff %t.imports-sorted %t.exports-sorted
// REQUIRES: asan-static-runtime

#include <stdio.h>
#include <windows.h>
Expand Down
22 changes: 11 additions & 11 deletions compiler-rt/test/asan/TestCases/Windows/dll_malloc_left_oob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ extern "C" __declspec(dllexport)
int test_function() {
char *buffer = (char*)malloc(42);
buffer[-1] = 42;
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: test_function {{.*}}dll_malloc_left_oob.cpp:[[@LINE-3]]
// CHECK-NEXT: main {{.*}}dll_host.cpp
//
// CHECK: [[ADDR]] is located 1 bytes before 42-byte region
// CHECK-LABEL: allocated by thread T0 here:
// CHECK-NEXT: malloc
// CHECK-NEXT: test_function {{.*}}dll_malloc_left_oob.cpp:[[@LINE-10]]
// CHECK-NEXT: main {{.*}}dll_host.cpp
// CHECK-LABEL: SUMMARY
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: test_function {{.*}}dll_malloc_left_oob.cpp:[[@LINE-3]]
// CHECK-NEXT: main {{.*}}dll_host.cpp
//
// CHECK: [[ADDR]] is located 1 bytes before 42-byte region
// CHECK-LABEL: allocated by thread T0 here:
// CHECK-NEXT: malloc
// CHECK: test_function {{.*}}dll_malloc_left_oob.cpp:[[@LINE-10]]
// CHECK-NEXT: main {{.*}}dll_host.cpp
// CHECK-LABEL: SUMMARY
free(buffer);
return 0;
}
30 changes: 15 additions & 15 deletions compiler-rt/test/asan/TestCases/Windows/dll_malloc_uaf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ int test_function() {
int *buffer = (int*)malloc(42);
free(buffer);
buffer[0] = 42;
// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 4 at [[ADDR]] thread T0
// CHECK-NEXT: test_function {{.*}}dll_malloc_uaf.cpp:[[@LINE-3]]
// CHECK-NEXT: main {{.*}}dll_host
//
// CHECK: [[ADDR]] is located 0 bytes inside of 42-byte region
// CHECK-LABEL: freed by thread T0 here:
// CHECK-NEXT: free
// CHECK-NEXT: test_function {{.*}}dll_malloc_uaf.cpp:[[@LINE-10]]
// CHECK-NEXT: main {{.*}}dll_host
//
// CHECK-LABEL: previously allocated by thread T0 here:
// CHECK-NEXT: malloc
// CHECK-NEXT: test_function {{.*}}dll_malloc_uaf.cpp:[[@LINE-16]]
// CHECK-NEXT: main {{.*}}dll_host
// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 4 at [[ADDR]] thread T0
// CHECK-NEXT: test_function {{.*}}dll_malloc_uaf.cpp:[[@LINE-3]]
// CHECK-NEXT: main {{.*}}dll_host
//
// CHECK: [[ADDR]] is located 0 bytes inside of 42-byte region
// CHECK-LABEL: freed by thread T0 here:
// CHECK-NEXT: free
// CHECK: test_function {{.*}}dll_malloc_uaf.cpp:[[@LINE-10]]
// CHECK-NEXT: main {{.*}}dll_host
//
// CHECK-LABEL: previously allocated by thread T0 here:
// CHECK-NEXT: malloc
// CHECK: test_function {{.*}}dll_malloc_uaf.cpp:[[@LINE-16]]
// CHECK-NEXT: main {{.*}}dll_host
return 0;
}
20 changes: 10 additions & 10 deletions compiler-rt/test/asan/TestCases/Windows/double_free.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ int main() {
int *x = (int*)malloc(42 * sizeof(int));
free(x);
free(x);
// CHECK: AddressSanitizer: attempting double-free on [[ADDR:0x[0-9a-f]+]]
// CHECK-NEXT: {{#0 .* free }}
// CHECK-NEXT: {{#1 .* main .*double_free.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes inside of 168-byte region
// CHECK-LABEL: freed by thread T0 here:
// CHECK-NEXT: {{#0 .* free }}
// CHECK-NEXT: {{#1 .* main .*double_free.cpp}}:[[@LINE-8]]
// CHECK-LABEL: previously allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* malloc }}
// CHECK-NEXT: {{#1 .* main .*double_free.cpp}}:[[@LINE-12]]
// CHECK: AddressSanitizer: attempting double-free on [[ADDR:0x[0-9a-f]+]]
// CHECK-NEXT: {{#0 .* free }}
// CHECK: {{ #[1-2] .* main .*double_free.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes inside of 168-byte region
// CHECK-LABEL: freed by thread T0 here:
// CHECK-NEXT: {{#0 .* free }}
// CHECK: {{ #[1-2] .* main .*double_free.cpp}}:[[@LINE-8]]
// CHECK-LABEL: previously allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* malloc }}
// CHECK: {{ #[1-2] .* main .*double_free.cpp}}:[[@LINE-12]]
return 0;
}

This file was deleted.

14 changes: 7 additions & 7 deletions compiler-rt/test/asan/TestCases/Windows/malloc_left_oob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
int main() {
char *buffer = (char*)malloc(42);
buffer[-1] = 42;
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*malloc_left_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 1 bytes before 42-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* malloc }}
// CHECK-NEXT: {{#1 .* main .*malloc_left_oob.cpp}}:[[@LINE-8]]
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*malloc_left_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 1 bytes before 42-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* malloc }}
// CHECK: {{ #[1-2] .* main .*malloc_left_oob.cpp}}:[[@LINE-8]]
free(buffer);
}
14 changes: 7 additions & 7 deletions compiler-rt/test/asan/TestCases/Windows/malloc_right_oob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
int main() {
char *buffer = (char*)malloc(42);
buffer[42] = 42;
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*malloc_right_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes after 42-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* malloc }}
// CHECK-NEXT: {{#1 .* main .*malloc_right_oob.cpp}}:[[@LINE-8]]
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*malloc_right_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes after 42-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* malloc }}
// CHECK: {{ #[1-2] .* main .*malloc_right_oob.cpp}}:[[@LINE-8]]
free(buffer);
}
20 changes: 10 additions & 10 deletions compiler-rt/test/asan/TestCases/Windows/malloc_uaf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ int main() {
char *buffer = (char*)malloc(42);
free(buffer);
buffer[0] = 42;
// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*malloc_uaf.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes inside of 42-byte region
// CHECK: freed by thread T0 here:
// CHECK-NEXT: {{#0 .* free }}
// CHECK-NEXT: {{#1 .* main .*malloc_uaf.cpp}}:[[@LINE-8]]
// CHECK: previously allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* malloc }}
// CHECK-NEXT: {{#1 .* main .*malloc_uaf.cpp}}:[[@LINE-12]]
// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*malloc_uaf.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes inside of 42-byte region
// CHECK: freed by thread T0 here:
// CHECK-NEXT: {{#0 .* free }}
// CHECK: {{ #[1-2] .* main .*malloc_uaf.cpp}}:[[@LINE-8]]
// CHECK: previously allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* malloc }}
// CHECK: {{ #[1-2] .* main .*malloc_uaf.cpp}}:[[@LINE-12]]
}
14 changes: 7 additions & 7 deletions compiler-rt/test/asan/TestCases/Windows/realloc_left_oob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
int main() {
char *buffer = (char*)realloc(0, 42);
buffer[-1] = 42;
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*realloc_left_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 1 bytes before 42-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* realloc }}
// CHECK-NEXT: {{#1 .* main .*realloc_left_oob.cpp}}:[[@LINE-8]]
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*realloc_left_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 1 bytes before 42-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* realloc }}
// CHECK: {{ #[1-2] .* main .*realloc_left_oob.cpp}}:[[@LINE-8]]
free(buffer);
}
14 changes: 7 additions & 7 deletions compiler-rt/test/asan/TestCases/Windows/realloc_right_oob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
int main() {
char *buffer = (char*)realloc(0, 42);
buffer[42] = 42;
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*realloc_right_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes after 42-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* realloc }}
// CHECK-NEXT: {{#1 .* main .*realloc_right_oob.cpp}}:[[@LINE-8]]
// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
// CHECK: WRITE of size 1 at [[ADDR]] thread T0
// CHECK-NEXT: {{#0 .* main .*realloc_right_oob.cpp}}:[[@LINE-3]]
// CHECK: [[ADDR]] is located 0 bytes after 42-byte region
// CHECK: allocated by thread T0 here:
// CHECK-NEXT: {{#0 .* realloc }}
// CHECK: {{ #[1-2] .* main .*realloc_right_oob.cpp}}:[[@LINE-8]]
free(buffer);
}
Loading

0 comments on commit 0360f32

Please sign in to comment.