From 799b36a40f47d21727507f35f0b0c545509db083 Mon Sep 17 00:00:00 2001 From: Denis Khalikov Date: Tue, 19 Dec 2017 12:47:55 +0300 Subject: [ASan] Add tests of interceptor for printf_chk Differential Revision: https://reviews.llvm.org/D40951 Change-Id: I7c32945659da4cf80e995370cb26fb063bb7930c --- gcc/testsuite/gcc.dg/asan/snprintf-fortify-1.c | 29 +++++++++++++++++++ gcc/testsuite/gcc.dg/asan/sprintf-fortify-1.c | 29 +++++++++++++++++++ gcc/testsuite/gcc.dg/asan/vsnprintf-fortify-1.c | 38 +++++++++++++++++++++++++ gcc/testsuite/gcc.dg/asan/vsprintf-fortify-1.c | 37 ++++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/asan/snprintf-fortify-1.c create mode 100644 gcc/testsuite/gcc.dg/asan/sprintf-fortify-1.c create mode 100644 gcc/testsuite/gcc.dg/asan/vsnprintf-fortify-1.c create mode 100644 gcc/testsuite/gcc.dg/asan/vsprintf-fortify-1.c diff --git a/gcc/testsuite/gcc.dg/asan/snprintf-fortify-1.c b/gcc/testsuite/gcc.dg/asan/snprintf-fortify-1.c new file mode 100644 index 00000000000..b5e1a6e5101 --- /dev/null +++ b/gcc/testsuite/gcc.dg/asan/snprintf-fortify-1.c @@ -0,0 +1,29 @@ +/* { dg-do run } */ +/* { dg-options "-D_FORTIFY_SOURCE=1 -w" } */ +/* { dg-skip-if "" { *-*-* } { "*" } { "-O1" } } */ +/* { dg-shouldfail "asan" } */ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +const char * +__asan_default_options () { + return "fast_unwind_on_malloc=false"; +} + +#ifdef __cplusplus +} +#endif + +int main(int argc, char **argv) { + char *short_buffer = (char*)malloc(3); + snprintf (short_buffer, 10, "%s_%s", "hello", "world"); /* BOOM */ + return short_buffer[0]; +} +/* { dg-output " #1 0x\[0-9a-f\]+ +(in _*(interceptor_|wrap_|)__snprintf_chk|\[(\])\[^\n\r]*(\n|\r\n|\r)" } */ + diff --git a/gcc/testsuite/gcc.dg/asan/sprintf-fortify-1.c b/gcc/testsuite/gcc.dg/asan/sprintf-fortify-1.c new file mode 100644 index 00000000000..86fed28e373 --- /dev/null +++ b/gcc/testsuite/gcc.dg/asan/sprintf-fortify-1.c @@ -0,0 +1,29 @@ +/* { dg-do run } */ +/* { dg-options "-D_FORTIFY_SOURCE=1 -w" } */ +/* { dg-skip-if "" { *-*-* } { "*" } { "-O1" } } */ +/* { dg-shouldfail "asan" } */ + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +const char * +__asan_default_options () { + return "fast_unwind_on_malloc=false"; +} + +#ifdef __cplusplus +} +#endif + +int main(int argc, char **argv) { + char *short_buffer = (char*)malloc(3); + sprintf (short_buffer, "%s_%s", "hello", "world"); /* BOOM */ + return short_buffer[0]; +} +/* { dg-output " #1 0x\[0-9a-f\]+ +(in _*(interceptor_|wrap_|)__sprintf_chk|\[(\])\[^\n\r]*(\n|\r\n|\r)" } */ + diff --git a/gcc/testsuite/gcc.dg/asan/vsnprintf-fortify-1.c b/gcc/testsuite/gcc.dg/asan/vsnprintf-fortify-1.c new file mode 100644 index 00000000000..711c31f84e8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/asan/vsnprintf-fortify-1.c @@ -0,0 +1,38 @@ +/* { dg-do run } */ +/* { dg-options "-D_FORTIFY_SOURCE=1 -w" } */ +/* { dg-skip-if "" { *-*-* } { "*" } { "-O1" } } */ +/* { dg-shouldfail "asan" } */ + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +const char * +__asan_default_options () { + return "fast_unwind_on_malloc=false"; +} + +#ifdef __cplusplus +} +#endif + +__attribute__((noinline, noclone)) +char foo (const char *format, ...) { + char *str = (char *) malloc (1); + va_list ap; + va_start (ap, format); + vsnprintf (str, 10, format, ap); + va_end (ap); + return str[1]; +} + +int main(int argc, char **argv) { + return foo ("%s_%s", "hello", "world"); +} +/* { dg-output " #0 0x\[0-9a-f\]+ +(in _*(interceptor_|wrap_|)__vsnprintf_chk|\[(\])\[^\n\r]*(\n|\r\n|\r)" } */ + diff --git a/gcc/testsuite/gcc.dg/asan/vsprintf-fortify-1.c b/gcc/testsuite/gcc.dg/asan/vsprintf-fortify-1.c new file mode 100644 index 00000000000..d14f38fff75 --- /dev/null +++ b/gcc/testsuite/gcc.dg/asan/vsprintf-fortify-1.c @@ -0,0 +1,37 @@ +/* { dg-do run } */ +/* { dg-options "-D_FORTIFY_SOURCE=1 -w" } */ +/* { dg-skip-if "" { *-*-* } { "*" } { "-O1" } } */ +/* { dg-shouldfail "asan" } */ + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +const char * +__asan_default_options () { + return "fast_unwind_on_malloc=false"; +} + +#ifdef __cplusplus +} +#endif + +char foo (const char *format, ...) { + char *str = (char *) malloc (1); + va_list ap; + va_start (ap, format); + vsprintf (str, format, ap); + va_end (ap); + return str[1]; +} + +int main(int argc, char **argv) { + return foo ("%s_%s", "hello", "world"); +} +/* { dg-output " #0 0x\[0-9a-f\]+ +(in _*(interceptor_|wrap_|)__vsprintf_chk|\[(\])\[^\n\r]*(\n|\r\n|\r)" } */ + -- cgit v1.2.3