asan-dg.exp (asan_get_gtest_test_list, [...]): New procedures.

* lib/asan-dg.exp (asan_get_gtest_test_list,
	asan_get_gtest_expect_death_list, asan-gtest): New procedures.
	(proc ${tool}_load): Remember [asan_get_gtest_test_list "$output"]
	and [asan_get_gtest_expect_death_list "$output"] in global vars.
	(asan_symbolize): Sanitize [] characters from key.
	* g++.dg/asan/asan_test_config.h: New file.
	* g++.dg/asan/asan_globals_test.cc: New file.
	* g++.dg/asan/asan_test_utils.h: New file.
	* g++.dg/asan/dejagnu-gtest.h: New file.
	* g++.dg/asan/asan_test.cc: New file.
	* g++.dg/asan/asan_test.C: New test.

From-SVN: r194081
This commit is contained in:
Jakub Jelinek 2012-12-03 12:49:41 +01:00 committed by Jakub Jelinek
parent d9600ae5a3
commit 5c31da80bb
8 changed files with 2612 additions and 2 deletions

View File

@ -1,3 +1,17 @@
2012-12-03 Jakub Jelinek <jakub@redhat.com>
* lib/asan-dg.exp (asan_get_gtest_test_list,
asan_get_gtest_expect_death_list, asan-gtest): New procedures.
(proc ${tool}_load): Remember [asan_get_gtest_test_list "$output"]
and [asan_get_gtest_expect_death_list "$output"] in global vars.
(asan_symbolize): Sanitize [] characters from key.
* g++.dg/asan/asan_test_config.h: New file.
* g++.dg/asan/asan_globals_test.cc: New file.
* g++.dg/asan/asan_test_utils.h: New file.
* g++.dg/asan/dejagnu-gtest.h: New file.
* g++.dg/asan/asan_test.cc: New file.
* g++.dg/asan/asan_test.C: New test.
2012-12-03 Richard Biener <rguenther@suse.de>
* gcc.dg/torture/pr35634.c: Use signed char.

View File

@ -0,0 +1,22 @@
//===-- asan_globals_test.cc ----------------------------------------------===//
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of AddressSanitizer, an address sanity checker.
//
// Some globals in a separate file.
//===----------------------------------------------------------------------===//
extern char glob5[5];
static char static10[10];
int GlobalsTest(int zero) {
static char func_static15[15];
glob5[zero] = 0;
static10[zero] = 0;
func_static15[zero] = 0;
return glob5[1] + func_static15[2];
}

View File

@ -0,0 +1,12 @@
// { dg-do run { target { { i?86-*-linux* x86_64-*-linux* } && sse2_runtime } } }
// { dg-skip-if "" { *-*-* } { "*" } { "-O2" } }
// { dg-skip-if "" { *-*-* } { "-flto" } { "" } }
// { dg-additional-sources "asan_globals_test.cc" }
// { dg-options "-fsanitize=address -fno-builtin -Wall -Wno-format -Werror -g -DASAN_UAR=0 -DASAN_HAS_EXCEPTIONS=1 -DASAN_HAS_BLACKLIST=0 -DASAN_USE_DEJAGNU_GTEST=1 -lpthread -ldl" }
// { dg-additional-options "-DASAN_NEEDS_SEGV=1" { target { ! arm*-*-* } } }
// { dg-additional-options "-DASAN_LOW_MEMORY=1 -DASAN_NEEDS_SEGV=0" { target arm*-*-* } }
// { dg-additional-options "-DASAN_AVOID_EXPENSIVE_TESTS=1" { target { ! run_expensive_tests } } }
// { dg-additional-options "-msse2" { target { i?86-*-linux* x86_64-*-linux* } } }
// { dg-final { asan-gtest } }
#include "asan_test.cc"

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,58 @@
//===-- asan_test_config.h --------------------------------------*- C++ -*-===//
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of AddressSanitizer, an address sanity checker.
//
//===----------------------------------------------------------------------===//
#if !defined(INCLUDED_FROM_ASAN_TEST_UTILS_H)
# error "This file should be included into asan_test_utils.h only"
#endif
#ifndef ASAN_TEST_CONFIG_H
#define ASAN_TEST_CONFIG_H
#include <vector>
#include <string>
#include <map>
#if ASAN_USE_DEJAGNU_GTEST
# include "dejagnu-gtest.h"
#else
# include "gtest/gtest.h"
#endif
using std::string;
using std::vector;
using std::map;
#ifndef ASAN_UAR
# error "please define ASAN_UAR"
#endif
#ifndef ASAN_HAS_EXCEPTIONS
# error "please define ASAN_HAS_EXCEPTIONS"
#endif
#ifndef ASAN_HAS_BLACKLIST
# error "please define ASAN_HAS_BLACKLIST"
#endif
#ifndef ASAN_NEEDS_SEGV
# error "please define ASAN_NEEDS_SEGV"
#endif
#ifndef ASAN_LOW_MEMORY
# define ASAN_LOW_MEMORY 0
#endif
#ifndef ASAN_AVOID_EXPENSIVE_TESTS
# define ASAN_AVOID_EXPENSIVE_TESTS 0
#endif
#define ASAN_PCRE_DOTALL ""
#endif // ASAN_TEST_CONFIG_H

View File

@ -0,0 +1,73 @@
//===-- asan_test_utils.h ---------------------------------------*- C++ -*-===//
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of AddressSanitizer, an address sanity checker.
//
//===----------------------------------------------------------------------===//
#ifndef ASAN_TEST_UTILS_H
#define ASAN_TEST_UTILS_H
#if !defined(ASAN_EXTERNAL_TEST_CONFIG)
# define INCLUDED_FROM_ASAN_TEST_UTILS_H
# include "asan_test_config.h"
# undef INCLUDED_FROM_ASAN_TEST_UTILS_H
#endif
#if defined(_WIN32)
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
# define NOINLINE __declspec(noinline)
# define USED
#else // defined(_WIN32)
# define NOINLINE __attribute__((noinline))
# define USED __attribute__((used))
#endif // defined(_WIN32)
#if !defined(__has_feature)
#define __has_feature(x) 0
#endif
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
# define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS \
__attribute__((no_address_safety_analysis))
#else
# define ATTRIBUTE_NO_ADDRESS_SAFETY_ANALYSIS
#endif
#if __LP64__ || defined(_WIN64)
# define SANITIZER_WORDSIZE 64
#else
# define SANITIZER_WORDSIZE 32
#endif
// Make the compiler thinks that something is going on there.
inline void break_optimization(void *arg) {
__asm__ __volatile__ ("" : : "r" (arg) : "memory");
}
// This function returns its parameter but in such a way that compiler
// can not prove it.
template<class T>
NOINLINE
static T Ident(T t) {
T ret = t;
break_optimization(&ret);
return ret;
}
// Check that pthread_create/pthread_join return success.
#define PTHREAD_CREATE(a, b, c, d) ASSERT_EQ(0, pthread_create(a, b, c, d))
#define PTHREAD_JOIN(a, b) ASSERT_EQ(0, pthread_join(a, b))
#endif // ASAN_TEST_UTILS_H

View File

@ -0,0 +1,115 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#ifdef __cplusplus
#include <string>
#endif
struct dejagnu_gtest_test
{
const char *name;
void (*fn) (void);
struct dejagnu_gtest_test *next;
};
struct dejagnu_gtest_test *dejagnu_gtest_test_first, *dejagnu_gtest_test_last;
int dejagnu_gtest_test_death_num, dejagnu_gtest_test_death_cur_num;
#define TEST(cond, name) \
static void cond##_##name##_fn (void); \
static struct dejagnu_gtest_test cond##_##name##_struct \
= { #cond "_" #name, cond##_##name##_fn, NULL }; \
static __attribute__((__constructor__)) void \
cond##_##name##_ctor (void) \
{ \
if (strncmp (#name, "DISABLED_", 9) == 0) \
return; \
if (dejagnu_gtest_test_first == NULL) \
dejagnu_gtest_test_first = &cond##_##name##_struct; \
else \
dejagnu_gtest_test_last->next = &cond##_##name##_struct; \
dejagnu_gtest_test_last = &cond##_##name##_struct; \
} \
static void \
cond##_##name##_fn (void)
#ifndef __cplusplus
# define DEJAGNU_GTEST_TOCSTR(regex) (regex)
#else
static inline const char *DEJAGNU_GTEST_TOCSTR(const char *x) { return x; }
static inline const char *DEJAGNU_GTEST_TOCSTR(const std::string &x) { return x.c_str (); }
#endif
#define EXPECT_DEATH(statement, regex) \
do \
{ \
++dejagnu_gtest_test_death_cur_num; \
if (dejagnu_gtest_test_death_num == 0) \
{ \
fprintf (stderr, "DEJAGNU_GTEST_EXPECT_DEATH%d %s " \
"DEJAGNU_GTEST_EXPECT_DEATH%d %s " \
"DEJAGNU_GTEST_EXPECT_DEATH%d\n", \
dejagnu_gtest_test_death_cur_num, #statement, \
dejagnu_gtest_test_death_cur_num, \
DEJAGNU_GTEST_TOCSTR (regex), \
dejagnu_gtest_test_death_cur_num); \
} \
else if (dejagnu_gtest_test_death_cur_num \
== dejagnu_gtest_test_death_num) \
{ \
statement; \
} \
} \
while (0)
#define EXPECT_TRUE(condition) \
if (!(condition)) \
{ \
fprintf (stderr, "EXPECT_TRUE failed: " #condition "\n"); \
exit (1); \
}
#define EXPECT_FALSE(condition) EXPECT_TRUE (!condition)
#define EXPECT_EQ(expected, actual) EXPECT_TRUE ((expected) == (actual))
#define EXPECT_NE(expected, actual) EXPECT_TRUE ((expected) != (actual))
#define EXPECT_LT(expected, actual) EXPECT_TRUE ((expected) < (actual))
#define EXPECT_LE(expected, actual) EXPECT_TRUE ((expected) <= (actual))
#define EXPECT_GT(expected, actual) EXPECT_TRUE ((expected) > (actual))
#define EXPECT_GE(expected, actual) EXPECT_TRUE ((expected) >= (actual))
#define ASSERT_DEATH(statement, regex) EXPECT_DEATH (statement, regex)
#define ASSERT_TRUE(condition) EXPECT_TRUE (condition)
#define ASSERT_FALSE(condition) EXPECT_FALSE (condition)
#define ASSERT_EQ(expected, actual) EXPECT_EQ (expected, actual)
#define ASSERT_NE(expected, actual) EXPECT_NE (expected, actual)
#define ASSERT_LT(expected, actual) EXPECT_LT (expected, actual)
#define ASSERT_LE(expected, actual) EXPECT_LE (expected, actual)
#define ASSERT_GT(expected, actual) EXPECT_GT (expected, actual)
#define ASSERT_GE(expected, actual) EXPECT_GE (expected, actual)
int
main (int argc, const char **argv)
{
const char *test = NULL;
struct dejagnu_gtest_test *t;
if (argc > 1)
test = argv[1];
else
test = getenv ("DEJAGNU_GTEST_ARG");
if (test == NULL)
for (t = dejagnu_gtest_test_first; t; t = t->next)
fprintf (stderr, "DEJAGNU_GTEST_TEST %s\n", t->name);
else
{
const char *p = strchr (test, ':');
if (p != NULL)
dejagnu_gtest_test_death_num = atoi (p + 1);
for (t = dejagnu_gtest_test_first; t; t = t->next)
if (p != NULL
? (strncmp (test, t->name, p - test) == 0
&& t->name[p - test] == '\0')
: (strcmp (test, t->name) == 0))
break;
EXPECT_TRUE (t != NULL);
t->fn ();
}
return 0;
}

View File

@ -118,14 +118,14 @@ proc asan_symbolize { output } {
set addr2line_name [find_binutils_prog addr2line]
set idx 1
while { $idx < [llength $addresses] } {
set key [lindex $addresses $idx]
set key [regsub -all "\[\]\[\]" [lindex $addresses $idx] "\\\\&"]
set val [lindex $addresses [expr $idx + 1]]
lappend arr($key) $val
set idx [expr $idx + 3]
}
foreach key [array names arr] {
set args "-f -e $key $arr($key)"
set status [remote_exec host "$addr2line_name" $args]
set status [remote_exec host "$addr2line_name" "$args"]
if { [lindex $status 0] > 0 } continue
regsub -all "\r\n" [lindex $status 1] "\n" addr2line_output
regsub -all "\[\n\r\]BFD: \[^\n\r\]*" $addr2line_output "" addr2line_output
@ -164,6 +164,45 @@ proc asan_symbolize { output } {
return "$output"
}
# Return a list of gtest tests, printed in the form
# DEJAGNU_GTEST_TEST AddressSanitizer_SimpleDeathTest
# DEJAGNU_GTEST_TEST AddressSanitizer_VariousMallocsTest
proc asan_get_gtest_test_list { output } {
set idx 0
set ret ""
while {[regexp -start $idx -indices "DEJAGNU_GTEST_TEST (\[^\n\r\]*)(\r\n|\n|\r)" "$output" -> testname] > 0} {
set low [lindex $testname 0]
set high [lindex $testname 1]
set val [string range "$output" $low $high]
lappend ret $val
set idx [expr $high + 1]
}
return $ret
}
# Return a list of gtest EXPECT_DEATH tests, printed in the form
# DEJAGNU_GTEST_EXPECT_DEATH1 statement DEJAGNU_GTEST_EXPECT_DEATH1 regexp DEJAGNU_GTEST_EXPECT_DEATH1
# DEJAGNU_GTEST_EXPECT_DEATH2 other statement DEJAGNU_GTEST_EXPECT_DEATH2 other regexp DEJAGNU_GTEST_EXPECT_DEATH2
proc asan_get_gtest_expect_death_list { output } {
set idx 0
set ret ""
while {[regexp -start $idx -indices "DEJAGNU_GTEST_EXPECT_DEATH(\[0-9\]*)" "$output" -> id ] > 0} {
set low [lindex $id 0]
set high [lindex $id 1]
set val_id [string range "$output" $low $high]
if {[regexp -start $low -indices "$val_id (.*) DEJAGNU_GTEST_EXPECT_DEATH$val_id (.*) DEJAGNU_GTEST_EXPECT_DEATH$val_id\[\n\r\]" "$output" whole statement regexpr ] == 0} { break }
set low [lindex $statement 0]
set high [lindex $statement 1]
set val_statement [string range "$output" $low $high]
set low [lindex $regexpr 0]
set high [lindex $regexpr 1]
set val_regexpr [string range "$output" $low $high]
lappend ret [list "$val_id" "$val_statement" "$val_regexpr"]
set idx [lindex $whole 1]
}
return $ret
}
# Replace ${tool}_load with a wrapper so that we can symbolize the output.
if { [info procs ${tool}_load] != [list] \
&& [info procs saved_asan_${tool}_load] == [list] } {
@ -171,10 +210,94 @@ if { [info procs ${tool}_load] != [list] \
proc ${tool}_load { program args } {
global tool
global asan_last_gtest_test_list
global asan_last_gtest_expect_death_list
set result [eval [list saved_asan_${tool}_load $program] $args]
set output [lindex $result 1]
set symbolized_output [asan_symbolize "$output"]
set asan_last_gtest_test_list [asan_get_gtest_test_list "$output"]
set asan_last_gtest_expect_death_list [asan_get_gtest_expect_death_list "$output"]
set result [list [lindex $result 0] $symbolized_output]
return $result
}
}
# Utility for running gtest asan emulation under dejagnu, invoked via dg-final.
# Call pass if variable has the desired value, otherwise fail.
#
# Argument 0 handles expected failures and the like
proc asan-gtest { args } {
global tool
global asan_last_gtest_test_list
global asan_last_gtest_expect_death_list
if { ![info exists asan_last_gtest_test_list] } { return }
if { [llength $asan_last_gtest_test_list] == 0 } { return }
if { ![isnative] || [is_remote target] } { return }
set gtest_test_list $asan_last_gtest_test_list
unset asan_last_gtest_test_list
if { [llength $args] >= 1 } {
switch [dg-process-target [lindex $args 0]] {
"S" { }
"N" { return }
"F" { setup_xfail "*-*-*" }
"P" { }
}
}
# This assumes that we are three frames down from dg-test, and that
# it still stores the filename of the testcase in a local variable "name".
# A cleaner solution would require a new DejaGnu release.
upvar 2 name testcase
upvar 2 prog prog
set output_file "[file rootname [file tail $prog]].exe"
foreach gtest $gtest_test_list {
set testname "$testcase $gtest"
set status -1
setenv DEJAGNU_GTEST_ARG "$gtest"
set result [${tool}_load ./$output_file $gtest]
unsetenv DEJAGNU_GTEST_ARG
set status [lindex $result 0]
set output [lindex $result 1]
if { "$status" == "pass" } {
pass "$testname execution test"
if { [info exists asan_last_gtest_expect_death_list] } {
set gtest_expect_death_list $asan_last_gtest_expect_death_list
foreach gtest_death $gtest_expect_death_list {
set id [lindex $gtest_death 0]
set testname "$testcase $gtest [lindex $gtest_death 1]"
set regexpr [lindex $gtest_death 2]
set status -1
setenv DEJAGNU_GTEST_ARG "$gtest:$id"
set result [${tool}_load ./$output_file "$gtest:$id"]
unsetenv DEJAGNU_GTEST_ARG
set status [lindex $result 0]
set output [lindex $result 1]
if { "$status" == "fail" } {
pass "$testname execution test"
if { ![regexp $regexpr ${output}] } {
fail "$testname output pattern test, should match $regexpr"
} else {
pass "$testname output pattern test, $regexpr"
}
} elseif { "$status" == "pass" } {
fail "$testname execution test"
} else {
$status "$testname execution test"
}
}
}
} else {
$status "$testname execution test"
}
unset asan_last_gtest_expect_death_list
}
return
}