[compiler-rt] r208859 - [ASan/Win tests] Add tests for aligned mallocs, bitfields and global strings
Timur Iskhodzhanov
timurrrr at google.com
Thu May 15 03:58:36 PDT 2014
Author: timurrrr
Date: Thu May 15 05:58:35 2014
New Revision: 208859
URL: http://llvm.org/viewvc/llvm-project?rev=208859&view=rev
Log:
[ASan/Win tests] Add tests for aligned mallocs, bitfields and global strings
Added:
compiler-rt/trunk/test/asan/TestCases/Windows/aligned_mallocs.cc
compiler-rt/trunk/test/asan/TestCases/Windows/bitfield.cc
compiler-rt/trunk/test/asan/TestCases/Windows/bitfield_uaf.cc
compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string.cc
compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string_oob.cc
Added: compiler-rt/trunk/test/asan/TestCases/Windows/aligned_mallocs.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/aligned_mallocs.cc?rev=208859&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/aligned_mallocs.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/aligned_mallocs.cc Thu May 15 05:58:35 2014
@@ -0,0 +1,29 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// RUN: %run %t
+
+#include <windows.h>
+
+#define CHECK_ALIGNED(ptr,alignment) \
+ do { \
+ if (((uintptr_t)(ptr) % (alignment)) != 0) \
+ return __LINE__; \
+ } \
+ while(0)
+
+int main(void) {
+ int *p = (int*)_aligned_malloc(1024 * sizeof(int), 32);
+ CHECK_ALIGNED(p, 32);
+ p[512] = 0;
+ _aligned_free(p);
+
+ p = (int*)_aligned_malloc(128, 128);
+ CHECK_ALIGNED(p, 128);
+ p = (int*)_aligned_realloc(p, 2048 * sizeof(int), 128);
+ CHECK_ALIGNED(p, 128);
+ p[1024] = 0;
+ if (_aligned_msize(p, 128, 0) != 2048 * sizeof(int))
+ return __LINE__;
+ _aligned_free(p);
+
+ return 0;
+}
Added: compiler-rt/trunk/test/asan/TestCases/Windows/bitfield.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/bitfield.cc?rev=208859&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/bitfield.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/bitfield.cc Thu May 15 05:58:35 2014
@@ -0,0 +1,21 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// RUN: %run %t
+
+#include <windows.h>
+
+typedef struct _S {
+ unsigned int bf1:1;
+ unsigned int bf2:2;
+ unsigned int bf3:3;
+ unsigned int bf4:4;
+} S;
+
+int main(void) {
+ S *s = (S*)malloc(sizeof(S));
+ s->bf1 = 1;
+ s->bf2 = 2;
+ s->bf3 = 3;
+ s->bf4 = 4;
+ free(s);
+ return 0;
+}
Added: compiler-rt/trunk/test/asan/TestCases/Windows/bitfield_uaf.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/bitfield_uaf.cc?rev=208859&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/bitfield_uaf.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/bitfield_uaf.cc Thu May 15 05:58:35 2014
@@ -0,0 +1,35 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <windows.h>
+
+typedef struct _S {
+ unsigned int bf1:1;
+ unsigned int bf2:2;
+ unsigned int bf3:3;
+ unsigned int bf4:4;
+} S;
+
+void make_access(S *s) {
+ s->bf2 = 2;
+// CHECK: AddressSanitizer: heap-use-after-free on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: READ of size {{[124]}} at [[ADDR]]
+// CHECK: {{#0 .* make_access .*bitfield_uaf.cc}}:[[@LINE-3]]
+// CHECK: {{#1 .* main}}
+}
+
+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.cc}}:[[@LINE-4]]
+// CHECK-LABEL: previously allocated by thread T0 here:
+// CHECK: {{#0 .* malloc }}
+// CHECK: {{#1 .* main .*bitfield_uaf.cc}}:[[@LINE-8]]
+ make_access(s);
+ return 0;
+}
+
Added: compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string.cc?rev=208859&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string.cc Thu May 15 05:58:35 2014
@@ -0,0 +1,12 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// RUN: %run %t | FileCheck %s
+
+#include <windows.h>
+#include <stdio.h>
+
+int main(void) {
+ static const char *foo = "foobarspam";
+ printf("Global string is `%s`\n", foo);
+// CHECK: Global string is `foobarspam`
+ return 0;
+}
Added: compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string_oob.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string_oob.cc?rev=208859&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string_oob.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/global_const_string_oob.cc Thu May 15 05:58:35 2014
@@ -0,0 +1,21 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+#include <windows.h>
+#include <stdio.h>
+
+extern "C" const char *foo = "foobarspam";
+
+int main(void) {
+ if (foo[16])
+ printf("Boo\n");
+// CHECK-NOT: Boo
+// CHECK: AddressSanitizer: global-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: READ of size 1 at [[ADDR]] thread T0
+// CHECK-NEXT: {{#0 .* main .*global_const_string_oob.cc:}}[[@LINE-5]]
+// CHECK: [[ADDR]] is located 5 bytes to the right of global variable [[STR:.*]] from {{'.*global_const_string_oob.cc' .*}} of size 11
+// CHECK: [[STR]] is ascii string 'foobarspam'
+ return 0;
+}
+
More information about the llvm-commits
mailing list