From d2f951ce190c39b711e2563d84b9510b71eecc89 Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Fri, 22 Apr 2016 00:10:23 +0000 Subject: Enable stack-use-after-scope tests. Fix and enable working stack-use-after-scope tests. Add more failing tests for the feature, for fix later. PR27453. Patch by Vitaly Buka. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@267084 91177308-0d34-0410-b5e6-96231b3b80d8 --- test/asan/TestCases/use-after-scope-capture.cc | 13 ++++++++----- test/asan/TestCases/use-after-scope-dtor-order.cc | 6 +++--- test/asan/TestCases/use-after-scope-if.cc | 15 +++++++++++++++ test/asan/TestCases/use-after-scope-inlined.cc | 4 ++-- test/asan/TestCases/use-after-scope-loop-bug.cc | 16 ++++++++++++++++ test/asan/TestCases/use-after-scope-loop-removed.cc | 19 +++++++++++++++++++ test/asan/TestCases/use-after-scope-loop.cc | 14 ++++++++++++++ test/asan/TestCases/use-after-scope-nobug.cc | 11 ++++++----- test/asan/TestCases/use-after-scope-temp.cc | 18 ++++++------------ test/asan/TestCases/use-after-scope.cc | 9 ++++----- 10 files changed, 93 insertions(+), 32 deletions(-) create mode 100644 test/asan/TestCases/use-after-scope-if.cc create mode 100644 test/asan/TestCases/use-after-scope-loop-bug.cc create mode 100644 test/asan/TestCases/use-after-scope-loop-removed.cc create mode 100644 test/asan/TestCases/use-after-scope-loop.cc (limited to 'test') diff --git a/test/asan/TestCases/use-after-scope-capture.cc b/test/asan/TestCases/use-after-scope-capture.cc index 28c99b446..4ea16af99 100644 --- a/test/asan/TestCases/use-after-scope-capture.cc +++ b/test/asan/TestCases/use-after-scope-capture.cc @@ -1,14 +1,17 @@ -// 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 int main() { std::function 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 } diff --git a/test/asan/TestCases/use-after-scope-dtor-order.cc b/test/asan/TestCases/use-after-scope-dtor-order.cc index 7896dd30c..4b16df727 100644 --- a/test/asan/TestCases/use-after-scope-dtor-order.cc +++ b/test/asan/TestCases/use-after-scope-dtor-order.cc @@ -1,6 +1,6 @@ -// 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 struct IntHolder { @@ -8,7 +8,7 @@ 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_; } diff --git a/test/asan/TestCases/use-after-scope-if.cc b/test/asan/TestCases/use-after-scope-if.cc new file mode 100644 index 000000000..8779161ee --- /dev/null +++ b/test/asan/TestCases/use-after-scope-if.cc @@ -0,0 +1,15 @@ +// 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]] +} diff --git a/test/asan/TestCases/use-after-scope-inlined.cc b/test/asan/TestCases/use-after-scope-inlined.cc index a0a0d9461..7146d900c 100644 --- a/test/asan/TestCases/use-after-scope-inlined.cc +++ b/test/asan/TestCases/use-after-scope-inlined.cc @@ -2,8 +2,8 @@ // 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; diff --git a/test/asan/TestCases/use-after-scope-loop-bug.cc b/test/asan/TestCases/use-after-scope-loop-bug.cc new file mode 100644 index 000000000..6d8e88870 --- /dev/null +++ b/test/asan/TestCases/use-after-scope-loop-bug.cc @@ -0,0 +1,16 @@ +// 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 +} diff --git a/test/asan/TestCases/use-after-scope-loop-removed.cc b/test/asan/TestCases/use-after-scope-loop-removed.cc new file mode 100644 index 000000000..a9ea45051 --- /dev/null +++ b/test/asan/TestCases/use-after-scope-loop-removed.cc @@ -0,0 +1,19 @@ +// 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 + +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 +} diff --git a/test/asan/TestCases/use-after-scope-loop.cc b/test/asan/TestCases/use-after-scope-loop.cc new file mode 100644 index 000000000..dc96239e8 --- /dev/null +++ b/test/asan/TestCases/use-after-scope-loop.cc @@ -0,0 +1,14 @@ +// 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]] +} diff --git a/test/asan/TestCases/use-after-scope-nobug.cc b/test/asan/TestCases/use-after-scope-nobug.cc index 21b085c96..b7bf8ca92 100644 --- a/test/asan/TestCases/use-after-scope-nobug.cc +++ b/test/asan/TestCases/use-after-scope-nobug.cc @@ -1,14 +1,15 @@ -// 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 +#include + +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; diff --git a/test/asan/TestCases/use-after-scope-temp.cc b/test/asan/TestCases/use-after-scope-temp.cc index f9bd779ac..b238d8547 100644 --- a/test/asan/TestCases/use-after-scope-temp.cc +++ b/test/asan/TestCases/use-after-scope-temp.cc @@ -1,15 +1,10 @@ -// 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 - struct IntHolder { - explicit IntHolder(int val) : val(val) { - printf("IntHolder: %d\n", val); - } int val; }; @@ -20,10 +15,9 @@ void save(const IntHolder &holder) { } 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; } diff --git a/test/asan/TestCases/use-after-scope.cc b/test/asan/TestCases/use-after-scope.cc index 59a0e0cd6..e1f7302f9 100644 --- a/test/asan/TestCases/use-after-scope.cc +++ b/test/asan/TestCases/use-after-scope.cc @@ -1,10 +1,9 @@ -// 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; -- cgit v1.2.3