[compiler-rt] r208865 - [ASan/Win tests] Add tests for downcast-related overflows, as well as CRT initiazliers

Timur Iskhodzhanov timurrrr at google.com
Thu May 15 04:14:00 PDT 2014


Author: timurrrr
Date: Thu May 15 06:14:00 2014
New Revision: 208865

URL: http://llvm.org/viewvc/llvm-project?rev=208865&view=rev
Log:
[ASan/Win tests] Add tests for downcast-related overflows, as well as CRT initiazliers

Added:
    compiler-rt/trunk/test/asan/TestCases/Windows/crt_initializers.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/hello_world.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_heap.cc
    compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_stack.cc

Added: compiler-rt/trunk/test/asan/TestCases/Windows/crt_initializers.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/crt_initializers.cc?rev=208865&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/crt_initializers.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/crt_initializers.cc Thu May 15 06:14:00 2014
@@ -0,0 +1,31 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// RUN: %run %t | FileCheck %s
+
+// This is a test for http://code.google.com/p/address-sanitizer/issues/detail?id=305
+
+#include <stdio.h>
+
+typedef void (*FPTR)();
+
+// __xi_a and __xi_z are defined in VC/crt/src/crt0dat.c
+// and are located in .CRT$XIA and .CRT$XIZ respectively.
+extern "C" FPTR __xi_a, __xi_z;
+
+int main() {
+  unsigned count = 0;
+
+  // Iterate through CRT initializers.
+  for (FPTR* it = &__xi_a; it < &__xi_z; ++it) {
+    if (*it)
+      count++;
+  }
+
+  printf("Number of nonzero CRT initializers: %u\n", count);
+// CHECK: Number of nonzero CRT initializers
+}
+
+void call_me_maybe() {}
+
+#pragma data_seg(".CRT$XIB")
+// Add an initializer that shouldn't get its own redzone.
+FPTR run_on_startup = call_me_maybe;

Added: compiler-rt/trunk/test/asan/TestCases/Windows/hello_world.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/hello_world.cc?rev=208865&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/hello_world.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/hello_world.cc Thu May 15 06:14:00 2014
@@ -0,0 +1,9 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// RUN: %run %t | FileCheck %s
+
+#include <stdio.h>
+
+int main() {
+  printf("Hello, world!\n");
+// CHECK: Hello, world!
+}

Added: compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_heap.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_heap.cc?rev=208865&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_heap.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_heap.cc Thu May 15 06:14:00 2014
@@ -0,0 +1,27 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+class Parent {
+ public:
+  int field;
+};
+
+class Child : public Parent {
+ public:
+  int extra_field;
+};
+
+int main(void) {
+  Parent *p = new Parent;
+  Child *c = (Child*)p;  // Intentional error here!
+  c->extra_field = 42;
+// CHECK: AddressSanitizer: heap-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]] thread T0
+// CHECK:   {{#0 0x[0-9a-f]* in main .*wrong_downcast_on_heap.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located 0 bytes to the right of 4-byte region
+// CHECK: allocated by thread T0 here:
+// CHECK:   #0 {{.*}} operator new
+  return 0;
+}
+

Added: compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_stack.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_stack.cc?rev=208865&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_stack.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/wrong_downcast_on_stack.cc Thu May 15 06:14:00 2014
@@ -0,0 +1,27 @@
+// RUN: %clangxx_asan -O0 %s -Fe%t
+// FIXME: 'cat' is needed due to PR19744.
+// RUN: not %run %t 2>&1 | cat | FileCheck %s
+
+class Parent {
+ public:
+  int field;
+};
+
+class Child : public Parent {
+ public:
+  int extra_field;
+};
+
+int main(void) {
+  Parent p;
+  Child *c = (Child*)&p;  // Intentional error here!
+  c->extra_field = 42;
+// CHECK: AddressSanitizer: stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]]
+// CHECK: WRITE of size 4 at [[ADDR]] thread T0
+// CHECK-NEXT:  {{#0 0x[0-9a-f]* in main .*wrong_downcast_on_stack.cc}}:[[@LINE-3]]
+// CHECK: [[ADDR]] is located in stack of thread T0 at offset [[OFFSET:[0-9]+]] in frame
+// CHECK-NEXT:  {{#0 0x[0-9a-f]* in main }}
+// CHECK:  'p' <== Memory access at offset [[OFFSET]] overflows this variable
+  return 0;
+}
+





More information about the llvm-commits mailing list