Fix and enable working stack-use-after-scope tests.
Add more failing tests for the feature, for fix later.
PR27453.
Patch by Vitaly Buka.
llvm-svn: 267084
-// RUN: %clangxx_asan -O0 -fsanitize=use-after-scope %s -o %t && %run %t
-// XFAIL: *
+// RUN: %clangxx_asan -std=c++11 -O1 -mllvm -asan-use-after-scope=1 %s -o %t && \
+// RUN: not %run %t 2>&1 | FileCheck %s
+
+#include <functional>
int main() {
std::function<int()> f;
{
int x = 0;
f = [&x]() {
- return x;
- }
+ return x; // BOOM
+ // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
+ // CHECK: #0 0x{{.*}} in {{.*}}::operator()(){{.*}}.cc:[[@LINE-2]]
+ };
}
return f(); // BOOM
- // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
}
-// RUN: %clangxx_asan -O0 -fsanitize=use-after-scope %s -o %t && \
+// RUN: %clangxx_asan -O1 -mllvm -asan-use-after-scope=1 %s -o %t && \
// RUN: not %run %t 2>&1 | FileCheck %s
-// XFAIL: *
+
#include <stdio.h>
struct IntHolder {
~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]]
+ // CHECK: #0 0x{{.*}} in IntHolder::~IntHolder{{.*}}.cc:[[@LINE-2]]
}
void set(int *val) { val_ = val; }
int *get() { return val_; }
--- /dev/null
+// RUN: %clangxx_asan -O1 -mllvm -asan-use-after-scope=1 %s -o %t && \
+// RUN: not %run %t 2>&1 | FileCheck %s
+
+int *p;
+bool b = true;
+
+int main() {
+ if (b) {
+ int x[5];
+ p = x+1;
+ }
+ return *p; // BOOM
+ // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
+ // CHECK: #0 0x{{.*}} in main {{.*}}.cc:[[@LINE-2]]
+}
// happens. "always_inline" is not enough, as Clang doesn't emit
// llvm.lifetime intrinsics at -O0.
//
-// RUN: %clangxx_asan -O2 -fsanitize=use-after-scope %s -o %t && not %run %t 2>&1 | FileCheck %s
-// XFAIL: *
+// RUN: %clangxx_asan -O2 -mllvm -asan-use-after-scope=1 %s -o %t && \
+// RUN: not %run %t 2>&1 | FileCheck %s
int *arr;
--- /dev/null
+// RUN: %clangxx_asan -O1 -mllvm -asan-use-after-scope=1 %s -o %t && \
+// RUN: not %run %t 2>&1 | FileCheck %s
+//
+// FIXME: @llvm.lifetime.* are not emitted for x.
+// XFAIL: *
+
+int *p;
+
+int main() {
+ // Variable goes in and out of scope.
+ for (int i = 0; i < 3; ++i) {
+ int x[3] = {i, i, i};
+ p = x + i;
+ }
+ return *p; // BOOM
+}
--- /dev/null
+// RUN: %clangxx_asan -O1 -mllvm -asan-use-after-scope=1 %s -o %t && \
+// RUN: not %run %t 2>&1 | FileCheck %s
+//
+// FIXME: Compiler removes for-loop but keeps x variable. For unknown reason
+// @llvm.lifetime.* are not emitted for x.
+// XFAIL: *
+
+#include <stdlib.h>
+
+int *p;
+
+int main() {
+ for (int i = 0; i < 3; i++) {
+ int x;
+ p = &x;
+ }
+ return **p; // BOOM
+ // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
+}
--- /dev/null
+// RUN: %clangxx_asan -O1 -mllvm -asan-use-after-scope=1 %s -o %t && \
+// RUN: not %run %t 2>&1 | FileCheck %s
+
+int *p[3];
+
+int main() {
+ for (int i = 0; i < 3; i++) {
+ int x;
+ p[i] = &x;
+ }
+ return **p; // BOOM
+ // CHECK: ERROR: AddressSanitizer: stack-use-after-scope
+ // CHECK: #0 0x{{.*}} in main {{.*}}.cc:[[@LINE-2]]
+}
-// RUN: %clangxx_asan -O0 -fsanitize=use-after-scope %s -o %t && %run %t
-// XFAIL: *
+// RUN: %clangxx_asan -O1 -mllvm -asan-use-after-scope=1 %s -o %t && %run %t
#include <stdio.h>
+#include <stdlib.h>
+
+int *p[3];
int main() {
- int *p = 0;
// Variable goes in and out of scope.
for (int i = 0; i < 3; i++) {
- int x = 0;
- p = &x;
+ int x;
+ p[i] = &x;
}
printf("PASSED\n");
return 0;
-// RUN: %clangxx_asan -O0 -fsanitize=use-after-scope %s -o %t && \
-// RUN: %run %t 2>&1 | FileCheck %s
+// RUN: %clangxx_asan -O1 -mllvm -asan-use-after-scope=1 %s -o %t && \
+// RUN: not %run %t 2>&1 | 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;
};
}
int main(int argc, char *argv[]) {
- save(IntHolder(10));
+ save({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;
+// CHECK: ERROR: AddressSanitizer: stack-use-after-scope
+// CHECK: #0 0x{{.*}} in main {{.*}}use-after-scope-temp.cc:[[@LINE-2]]
+ return x;
}
-// RUN: %clangxx_asan -O0 -fsanitize=use-after-scope %s -o %t && \
-// RUN: not %run %t 2>&1 | FileCheck %s
-// RUN: %env_asan_opts=detect_stack_use_after_return=1 not %run %t 2>&1 | FileCheck %s
-// XFAIL: *
+// RUN: %clangxx_asan -O1 -mllvm -asan-use-after-scope=1 %s -o %t && \
+// RUN: not %run %t 2>&1 | FileCheck %s
+
+int *p = 0;
int main() {
- int *p = 0;
{
int x = 0;
p = &x;