[compiler-rt] r183391 - [ASan] Add a few tests for use-after-scope mode

Alexey Samsonov samsonov at google.com
Thu Jun 6 01:30:27 PDT 2013


Author: samsonov
Date: Thu Jun  6 03:30:26 2013
New Revision: 183391

URL: http://llvm.org/viewvc/llvm-project?rev=183391&view=rev
Log:
[ASan] Add a few tests for use-after-scope mode

Added:
    compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-dtor-order.cc
    compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-nobug.cc
    compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-temp.cc
    compiler-rt/trunk/lib/asan/lit_tests/use-after-scope.cc

Added: compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-dtor-order.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-dtor-order.cc?rev=183391&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-dtor-order.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-dtor-order.cc Thu Jun  6 03:30:26 2013
@@ -0,0 +1,25 @@
+// RUN: %clangxx_asan -m64 -O0 -fsanitize=use-after-scope %s -o %t && \
+// RUN:     %t 2>&1 | %symbolize | FileCheck %s
+#include <stdio.h>
+
+struct IntHolder {
+  explicit IntHolder(int *val = 0) : val_(val) { }
+  ~IntHolder() {
+    printf("Value: %d\n", *val_);  // BOOM
+    // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
+    // CHECK:  #0 0x{{.*}} in IntHolder::~IntHolder{{.*}}use-after-scope-dtor-order.cc:[[@LINE-2]]
+  }
+  void set(int *val) { val_ = val; }
+  int *get() { return val_; }
+
+  int *val_;
+};
+
+int main(int argc, char *argv[]) {
+  // It is incorrect to use "x" int IntHolder destructor, because "x" is
+  // "destroyed" earlier as it's declared later.
+  IntHolder holder;
+  int x = argc;
+  holder.set(&x);
+  return 0;
+}

Added: compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-nobug.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-nobug.cc?rev=183391&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-nobug.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-nobug.cc Thu Jun  6 03:30:26 2013
@@ -0,0 +1,16 @@
+// RUN: %clangxx_asan -m64 -O0 -fsanitize=use-after-scope %s -o %t && \
+// RUN:     %t 2>&1 | %symbolize | FileCheck %s
+
+#include <stdio.h>
+
+int main() {
+  int *p = 0;
+  // Variable goes in and out of scope.
+  for (int i = 0; i < 3; i++) {
+    int x = 0;
+    p = &x;
+  }
+  printf("PASSED\n");
+  // CHECK: PASSED
+  return 0;
+}

Added: compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-temp.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-temp.cc?rev=183391&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-temp.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/use-after-scope-temp.cc Thu Jun  6 03:30:26 2013
@@ -0,0 +1,29 @@
+// RUN: %clangxx_asan -m64 -O0 -fsanitize=use-after-scope %s -o %t && \
+// RUN:     %t 2>&1 | %symbolize | FileCheck %s
+//
+// Lifetime for temporaries is not emitted yet.
+// XFAIL: *
+
+#include <stdio.h>
+
+struct IntHolder {
+  explicit IntHolder(int val) : val(val) {
+    printf("IntHolder: %d\n", val);
+  }
+  int val;
+};
+
+const IntHolder *saved;
+
+void save(const IntHolder &holder) {
+  saved = &holder;
+}
+
+int main(int argc, char *argv[]) {
+  save(IntHolder(10));
+  int x = saved->val;  // BOOM
+  // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
+  // CHECK:  #0 0x{{.*}} in {{_?}}main {{.*}}use-after-scope-temp.cc:[[@LINE-2]]
+  printf("saved value: %d\n", x);
+  return 0;
+}

Added: compiler-rt/trunk/lib/asan/lit_tests/use-after-scope.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/lit_tests/use-after-scope.cc?rev=183391&view=auto
==============================================================================
--- compiler-rt/trunk/lib/asan/lit_tests/use-after-scope.cc (added)
+++ compiler-rt/trunk/lib/asan/lit_tests/use-after-scope.cc Thu Jun  6 03:30:26 2013
@@ -0,0 +1,15 @@
+// RUN: %clangxx_asan -m64 -O0 -fsanitize=use-after-scope %s -o %t && \
+// RUN:     %t 2>&1 | %symbolize | FileCheck %s
+
+int main() {
+  int *p = 0;
+  {
+    int x = 0;
+    p = &x;
+  }
+  return *p;  // BOOM
+  // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
+  // CHECK:  #0 0x{{.*}} in {{_?}}main {{.*}}use-after-scope.cc:[[@LINE-2]]
+  // CHECK: Address 0x{{.*}} is located in stack of thread T{{.*}} at offset [[OFFSET:[^ ]+]] in frame
+  // {{\[}}[[OFFSET]], {{[0-9]+}}) 'x'
+}





More information about the llvm-commits mailing list