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

llvm-svn: 183391
This commit is contained in:
Alexey Samsonov 2013-06-06 08:30:26 +00:00
parent 7a75e16c27
commit b42b2f5c69
4 changed files with 85 additions and 0 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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'
}