diff options
author | Ben Cheng <bccheng@google.com> | 2014-03-25 22:37:19 -0700 |
---|---|---|
committer | Ben Cheng <bccheng@google.com> | 2014-03-25 22:37:19 -0700 |
commit | 1bc5aee63eb72b341f506ad058502cd0361f0d10 (patch) | |
tree | c607e8252f3405424ff15bc2d00aa38dadbb2518 /gcc-4.9/gcc/testsuite/c-c++-common/ubsan | |
parent | 283a0bf58fcf333c58a2a92c3ebbc41fb9eb1fdb (diff) | |
download | toolchain_gcc-1bc5aee63eb72b341f506ad058502cd0361f0d10.tar.gz toolchain_gcc-1bc5aee63eb72b341f506ad058502cd0361f0d10.tar.bz2 toolchain_gcc-1bc5aee63eb72b341f506ad058502cd0361f0d10.zip |
Initial checkin of GCC 4.9.0 from trunk (r208799).
Change-Id: I48a3c08bb98542aa215912a75f03c0890e497dba
Diffstat (limited to 'gcc-4.9/gcc/testsuite/c-c++-common/ubsan')
59 files changed, 1855 insertions, 0 deletions
diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/attrib-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/attrib-1.c new file mode 100644 index 000000000..2e9141ca0 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/attrib-1.c @@ -0,0 +1,33 @@ +/* PR sanitizer/58411 */ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=undefined -w" } */ + +__attribute__((no_sanitize_undefined)) int +f1 (int i) +{ + return 16 << i; +} + +int f2 (int i); +int f2 (int i) __attribute__((no_sanitize_undefined)); +int f2 (int i) __attribute__((no_sanitize_undefined)); +int f2 (int i); + +int +f2 (int i) +{ + return 1 / i; +} + +void f3 (void); +__typeof (f3) f3 __attribute__((__no_sanitize_undefined__)); + +void +f3 (void) +{ + __builtin_unreachable (); +} + +/* { dg-final { scan-assembler-not "__ubsan_handle_shift_out_of_bounds" } } */ +/* { dg-final { scan-assembler-not "__ubsan_handle_divrem_overflow" } } */ +/* { dg-final { scan-assembler-not "__ubsan_handle_builtin_unreachable" } } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/const-char-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/const-char-1.c new file mode 100644 index 000000000..6c2c3f8c3 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/const-char-1.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=shift" } */ + +void +foo (void) +{ + int y = 1 << 2; + __builtin_printf ("%d\n", y); +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/const-expr-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/const-expr-1.c new file mode 100644 index 000000000..f474ec64e --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/const-expr-1.c @@ -0,0 +1,22 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=shift -w" } */ + +enum e { A = 1 << 1, B, }; +const int arr[] = { + 1 << 2, + 1 << 3, +}; + +int +bar (int a, int b) +{ + return a >> b; +} + +int +foo (void) +{ + int i = 1; + int vla[B << 3]; + return bar (A, (i <<= 6, i + 2)); +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-1.c new file mode 100644 index 000000000..ec391e40b --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-1.c @@ -0,0 +1,24 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=integer-divide-by-zero -Wno-div-by-zero" } */ + +int +main (void) +{ + volatile int a = 0; + volatile long long int b = 0; + volatile unsigned int c = 1; + + a / b; + 0 / 0; + a / 0; + 0 / b; + 2 / --c; + + return 0; +} + +/* { dg-output "division by zero(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-2.c new file mode 100644 index 000000000..c8820fa94 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-2.c @@ -0,0 +1,23 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=integer-divide-by-zero -Wno-div-by-zero" } */ + +int +main (void) +{ + volatile const unsigned long int o = 1UL; + int zero = 0; + + o / 0; + 1UL / 0; + 1UL / zero; + o / zero; + o / (++zero - 1); + + return 0; +} + +/* { dg-output "division by zero(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division by zero" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-3.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-3.c new file mode 100644 index 000000000..399071ee7 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-3.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=integer-divide-by-zero -Wno-overflow" } */ + +#include <limits.h> + +int +main (void) +{ + volatile int min = INT_MIN; + volatile int zero = 0; + + INT_MIN / -1; + min / -1; + min / (10 * zero - (2 - 1)); + + return 0; +} + +/* { dg-output "division of -2147483648 by -1 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division of -2147483648 by -1 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*division of -2147483648 by -1 cannot be represented in type 'int'" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-4.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-4.c new file mode 100644 index 000000000..02162e139 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-4.c @@ -0,0 +1,11 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=integer-divide-by-zero -Wno-overflow" } */ + +#define INT_MIN (-__INT_MAX__ - 1) + +int +main (void) +{ + /* This should not fail. */ + return (unsigned int) INT_MIN / -1; +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-5.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-5.c new file mode 100644 index 000000000..7a28bacd1 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/div-by-zero-5.c @@ -0,0 +1,8 @@ +/* { dg-do compile} */ +/* { dg-options "-fsanitize=integer-divide-by-zero" } */ + +void +foo (void) +{ + int A[-2 / -1] = {}; +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/load-bool-enum.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/load-bool-enum.c new file mode 100644 index 000000000..96f7984f8 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/load-bool-enum.c @@ -0,0 +1,29 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=bool,enum" } */ + +#ifndef __cplusplus +#define bool _Bool +#endif +enum A { B = -3, C = 2 } a; +bool b; + +__attribute__((noinline, noclone)) enum A +foo (bool *p) +{ + *p = b; /* { dg-output "load-bool-enum.c:13:\[^\n\r]*runtime error: load of value 4, which is not a valid value for type '(_B|b)ool'(\n|\r\n|\r)*" } */ + return a; /* { dg-output "\[^\n\r]*load-bool-enum.c:14:\[^\n\r]*runtime error: load of value 9, which is not a valid value for type 'A'(\n|\r\n|\r)*" { target c++ } } */ +} + +int +main () +{ + char c = 4; + int d = 9; + if (sizeof (int) != sizeof (a) || sizeof (b) != 1) + return 0; + __builtin_memcpy (&a, &d, sizeof (int)); + __builtin_memcpy (&b, &c, 1); + bool e; + foo (&e); + return 0; +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-1.c new file mode 100644 index 000000000..08d547f3a --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-1.c @@ -0,0 +1,12 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +int +main (void) +{ + int *p = 0; + return *p; +} + +/* { dg-output "load of null pointer of type 'int'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-10.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-10.c new file mode 100644 index 000000000..c3d61a01d --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-10.c @@ -0,0 +1,13 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +int +main (void) +{ + short *p = 0, *u; + *(u + *p) = 23; + return 0; +} + +/* { dg-output "load of null pointer of type 'short int'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-11.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-11.c new file mode 100644 index 000000000..6645f2acf --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-11.c @@ -0,0 +1,16 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +struct S { + int i; +}; + +int +main (void) +{ + struct S **s = 0; + return (*s)->i; +} + +/* { dg-output "load of null pointer of type 'struct S \\*'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-2.c new file mode 100644 index 000000000..cb3907fa5 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-2.c @@ -0,0 +1,12 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +int +main (void) +{ + int ***ppp = 0; + return ***ppp; +} + +/* { dg-output "load of null pointer of type 'int \\*\\*'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-3.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-3.c new file mode 100644 index 000000000..f58562c63 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-3.c @@ -0,0 +1,18 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +int +foo (int *p) +{ + return *p; +} + +int +main (void) +{ + int **p = 0; + return foo (*p); +} + +/* { dg-output "load of null pointer of type 'int \\*'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-4.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-4.c new file mode 100644 index 000000000..18506afb3 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-4.c @@ -0,0 +1,14 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +int +main (void) +{ + _Complex double *p = 0; + if (p[0]) + return 42; + return 0; +} + +/* { dg-output "load of null pointer of type 'complex double'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-5.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-5.c new file mode 100644 index 000000000..c3c45b7c4 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-5.c @@ -0,0 +1,16 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +typedef volatile const _Complex float *T; + +int +main (void) +{ + T t = 0; + if (*t) + return 42; + return 0; +} + +/* { dg-output "load of null pointer of type 'volatile const complex float'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-6.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-6.c new file mode 100644 index 000000000..63fade5a6 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-6.c @@ -0,0 +1,13 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +int +main (void) +{ + unsigned long int *p = 0; + *p = 42; + return 0; +} + +/* { dg-output "store to null pointer of type 'long unsigned int'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-7.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-7.c new file mode 100644 index 000000000..bf30a0b9b --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-7.c @@ -0,0 +1,17 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +int * +gao (void) +{ + return 0; +} + +int +main (void) +{ + return *gao (); +} + +/* { dg-output "load of null pointer of type 'int'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-8.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-8.c new file mode 100644 index 000000000..170377ee4 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-8.c @@ -0,0 +1,16 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +struct S { + int i; +}; + +int +main (void) +{ + struct S *s = 0; + return s->i; +} + +/* { dg-output "member access within null pointer of type 'struct S'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-9.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-9.c new file mode 100644 index 000000000..ab43d22a3 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/null-9.c @@ -0,0 +1,16 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=null -w" } */ +/* { dg-shouldfail "ubsan" } */ + +union U { + int i; +}; + +int +main (void) +{ + union U *u = 0; + return u->i; +} + +/* { dg-output "member access within null pointer of type 'union U'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-1.c new file mode 100644 index 000000000..68199551b --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-1.c @@ -0,0 +1,269 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow" } */ + +#include <stdio.h> + +#ifndef ASM1 +# define ASM1(a) /* Nothing */ +#endif +#ifndef ASM2 +# define ASM2(a, b) /* Nothing */ +#endif + +#define CHECK(A, B) ({ if ((A) != (B)) __builtin_abort (); }) + +#define FN1(T1, T2, OP) \ + ({ \ + T1 a = 14; \ + T2 b = 9; \ + ASM2 (a, b); \ + a OP b; \ + }) + +#define FN2(T, OP) \ + ({ \ + T a = 14; \ + ASM1 (a); \ + a OP 7; \ + }) + +#define FN3(T1, T2, OP) \ + ({ \ + T1 a = 4; \ + T2 b = 1; \ + ASM2 (a, b); \ + ~a OP b; \ + }) + +#define FN4(T1, T2, OP) \ + ({ \ + T1 a = 4; \ + T2 b = 1; \ + ASM2 (a, b); \ + a OP ~b; \ + }) + +#define FN5(T) \ + ({ \ + T a = 77; \ + ASM1 (a); \ + -a; \ + }) + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + CHECK (FN1 (char, char, +), 23); + CHECK (FN1 (char, char, -), 5); + CHECK (FN1 (char, char, *), 126); + CHECK (FN1 (unsigned char, unsigned char, +), 23); + CHECK (FN1 (unsigned char, unsigned char, -), 5); + CHECK (FN1 (unsigned char, unsigned char, *), 126); + CHECK (FN1 (short, short, +), 23); + CHECK (FN1 (short, short, -), 5); + CHECK (FN1 (short, short, *), 126); + CHECK (FN1 (unsigned short, unsigned short, +), 23); + CHECK (FN1 (unsigned short, unsigned short, -), 5); + CHECK (FN1 (unsigned short, unsigned short, *), 126); + CHECK (FN1 (int, int, +), 23); + CHECK (FN1 (int, int, -), 5); + CHECK (FN1 (int, int, *), 126); + CHECK (FN1 (unsigned int, unsigned int, +), 23); + CHECK (FN1 (unsigned int, unsigned int, -), 5); + CHECK (FN1 (unsigned int, unsigned int, *), 126); + CHECK (FN1 (long int, long int, +), 23); + CHECK (FN1 (long int, long int, -), 5); + CHECK (FN1 (long int, long int, *), 126); + CHECK (FN1 (unsigned long int, unsigned long int, +), 23); + CHECK (FN1 (unsigned long int, unsigned long int, -), 5); + CHECK (FN1 (unsigned long int, unsigned long int, *), 126); + CHECK (FN1 (long long int, long int, +), 23); + CHECK (FN1 (long long int, long int, -), 5); + CHECK (FN1 (long long int, long int, *), 126); + CHECK (FN1 (unsigned long long int, unsigned long long int, +), 23); + CHECK (FN1 (unsigned long long int, unsigned long long int, -), 5); + CHECK (FN1 (unsigned long long int, unsigned long long int, *), 126); + CHECK (FN1 (int, unsigned char, +), 23); + CHECK (FN1 (int, unsigned char, -), 5); + CHECK (FN1 (int, unsigned char, *), 126); + CHECK (FN1 (unsigned char, int, +), 23); + CHECK (FN1 (unsigned char, int, -), 5); + CHECK (FN1 (unsigned char, int, *), 126); + CHECK (FN1 (int, long int, +), 23); + CHECK (FN1 (int, long int, -), 5); + CHECK (FN1 (int, long int, *), 126); + CHECK (FN1 (long int, int, +), 23); + CHECK (FN1 (long int, int, -), 5); + CHECK (FN1 (long int, int, *), 126); + CHECK (FN1 (unsigned int, int, +), 23); + CHECK (FN1 (unsigned int, int, -), 5); + CHECK (FN1 (unsigned int, int, *), 126); + CHECK (FN1 (int, unsigned int, +), 23); + CHECK (FN1 (int, unsigned int, -), 5); + CHECK (FN1 (int, unsigned int, *), 126); + CHECK (FN1 (unsigned long int, int, +), 23); + CHECK (FN1 (unsigned long int, int, -), 5); + CHECK (FN1 (unsigned long int, int, *), 126); + CHECK (FN1 (int, unsigned long int, +), 23); + CHECK (FN1 (int, unsigned long int, -), 5); + CHECK (FN1 (int, unsigned long int, *), 126); + + CHECK (FN2 (char, +), 21); + CHECK (FN2 (char, -), 7); + CHECK (FN2 (char, *), 98); + CHECK (FN2 (unsigned char, +), 21); + CHECK (FN2 (unsigned char, -), 7); + CHECK (FN2 (unsigned char, *), 98); + CHECK (FN2 (short, +), 21); + CHECK (FN2 (short, -), 7); + CHECK (FN2 (short, *), 98); + CHECK (FN2 (unsigned short, +), 21); + CHECK (FN2 (unsigned short, -), 7); + CHECK (FN2 (unsigned short, *), 98); + CHECK (FN2 (int, +), 21); + CHECK (FN2 (int, -), 7); + CHECK (FN2 (int, *), 98); + CHECK (FN2 (unsigned int, +), 21); + CHECK (FN2 (unsigned int, -), 7); + CHECK (FN2 (unsigned int, *), 98); + CHECK (FN2 (long int, +), 21); + CHECK (FN2 (long int, -), 7); + CHECK (FN2 (long int, *), 98); + CHECK (FN2 (unsigned long int, +), 21); + CHECK (FN2 (unsigned long int, -), 7); + CHECK (FN2 (unsigned long int, *), 98); + CHECK (FN2 (long long int, +), 21); + CHECK (FN2 (long long int, -), 7); + CHECK (FN2 (long long int, *), 98); + CHECK (FN2 (unsigned long long int, +), 21); + CHECK (FN2 (unsigned long long int, -), 7); + CHECK (FN2 (unsigned long long int, *), 98); + + CHECK (FN3 (char, char, +), -4); + CHECK (FN3 (char, char, -), -6); + CHECK (FN3 (char, char, *), -5); + CHECK (FN3 (unsigned char, unsigned char, +), -4); + CHECK (FN3 (unsigned char, unsigned char, -), -6); + CHECK (FN3 (unsigned char, unsigned char, *), -5); + CHECK (FN3 (short, short, +), -4); + CHECK (FN3 (short, short, -), -6); + CHECK (FN3 (short, short, *), -5); + CHECK (FN3 (unsigned short, unsigned short, +), -4); + CHECK (FN3 (unsigned short, unsigned short, -), -6); + CHECK (FN3 (unsigned short, unsigned short, *), -5); + CHECK (FN3 (int, int, +), -4); + CHECK (FN3 (int, int, -), -6); + CHECK (FN3 (int, int, *), -5); + CHECK (FN3 (unsigned int, unsigned int, +), -4); + CHECK (FN3 (unsigned int, unsigned int, -), -6); + CHECK (FN3 (unsigned int, unsigned int, *), -5); + CHECK (FN3 (long int, long int, +), -4); + CHECK (FN3 (long int, long int, -), -6); + CHECK (FN3 (long int, long int, *), -5); + CHECK (FN3 (unsigned long int, unsigned long int, +), -4); + CHECK (FN3 (unsigned long int, unsigned long int, -), -6); + CHECK (FN3 (unsigned long int, unsigned long int, *), -5); + CHECK (FN3 (long long int, long int, +), -4); + CHECK (FN3 (long long int, long int, -), -6); + CHECK (FN3 (long long int, long int, *), -5); + CHECK (FN3 (unsigned long long int, unsigned long long int, +), -4); + CHECK (FN3 (unsigned long long int, unsigned long long int, -), -6); + CHECK (FN3 (unsigned long long int, unsigned long long int, *), -5); + CHECK (FN3 (int, unsigned char, +), -4); + CHECK (FN3 (int, unsigned char, -), -6); + CHECK (FN3 (int, unsigned char, *), -5); + CHECK (FN3 (unsigned char, int, +), -4); + CHECK (FN3 (unsigned char, int, -), -6); + CHECK (FN3 (unsigned char, int, *), -5); + CHECK (FN3 (int, long int, +), -4); + CHECK (FN3 (int, long int, -), -6); + CHECK (FN3 (int, long int, *), -5); + CHECK (FN3 (long int, int, +), -4); + CHECK (FN3 (long int, int, -), -6); + CHECK (FN3 (long int, int, *), -5); + CHECK (FN3 (unsigned int, int, +), -4); + CHECK (FN3 (unsigned int, int, -), -6); + CHECK (FN3 (unsigned int, int, *), -5); + CHECK (FN3 (int, unsigned int, +), -4); + CHECK (FN3 (int, unsigned int, -), -6); + CHECK (FN3 (int, unsigned int, *), -5); + CHECK (FN3 (unsigned long int, int, +), -4); + CHECK (FN3 (unsigned long int, int, -), -6); + CHECK (FN3 (unsigned long int, int, *), -5); + CHECK (FN3 (int, unsigned long int, +), -4); + CHECK (FN3 (int, unsigned long int, -), -6); + CHECK (FN3 (int, unsigned long int, *), -5); + + CHECK (FN4 (char, char, +), 2); + CHECK (FN4 (char, char, -), 6); + CHECK (FN4 (char, char, *), -8); + CHECK (FN4 (unsigned char, unsigned char, +), 2); + CHECK (FN4 (unsigned char, unsigned char, -), 6); + CHECK (FN4 (unsigned char, unsigned char, *), -8); + CHECK (FN4 (short, short, +), 2); + CHECK (FN4 (short, short, -), 6); + CHECK (FN4 (short, short, *), -8); + CHECK (FN4 (unsigned short, unsigned short, +), 2); + CHECK (FN4 (unsigned short, unsigned short, -), 6); + CHECK (FN4 (unsigned short, unsigned short, *), -8); + CHECK (FN4 (int, int, +), 2); + CHECK (FN4 (int, int, -), 6); + CHECK (FN4 (int, int, *), -8); + CHECK (FN4 (unsigned int, unsigned int, +), 2); + CHECK (FN4 (unsigned int, unsigned int, -), 6); + CHECK (FN4 (unsigned int, unsigned int, *), -8); + CHECK (FN4 (long int, long int, +), 2); + CHECK (FN4 (long int, long int, -), 6); + CHECK (FN4 (long int, long int, *), -8); + CHECK (FN4 (unsigned long int, unsigned long int, +), 2); + CHECK (FN4 (unsigned long int, unsigned long int, -), 6); + CHECK (FN4 (unsigned long int, unsigned long int, *), -8); + CHECK (FN4 (long long int, long int, +), 2); + CHECK (FN4 (long long int, long int, -), 6); + CHECK (FN4 (long long int, long int, *), -8); + CHECK (FN4 (unsigned long long int, unsigned long long int, +), 2); + CHECK (FN4 (unsigned long long int, unsigned long long int, -), 6); + CHECK (FN4 (unsigned long long int, unsigned long long int, *), -8); + CHECK (FN4 (int, unsigned char, +), 2); + CHECK (FN4 (int, unsigned char, -), 6); + CHECK (FN4 (int, unsigned char, *), -8); + CHECK (FN4 (unsigned char, int, +), 2); + CHECK (FN4 (unsigned char, int, -), 6); + CHECK (FN4 (unsigned char, int, *), -8); + CHECK (FN4 (int, long int, +), 2); + CHECK (FN4 (int, long int, -), 6); + CHECK (FN4 (int, long int, *), -8); + CHECK (FN4 (long int, int, +), 2); + CHECK (FN4 (long int, int, -), 6); + CHECK (FN4 (long int, int, *), -8); + CHECK (FN4 (unsigned int, int, +), 2); + CHECK (FN4 (unsigned int, int, -), 6); + CHECK (FN4 (unsigned int, int, *), -8); + CHECK (FN4 (int, unsigned int, +), 2); + CHECK (FN4 (int, unsigned int, -), 6); + CHECK (FN4 (int, unsigned int, *), -8); + CHECK (FN4 (unsigned long int, int, +), 2); + CHECK (FN4 (unsigned long int, int, -), 6); + CHECK (FN4 (unsigned long int, int, *), -8); + CHECK (FN4 (int, unsigned long int, +), 2); + CHECK (FN4 (int, unsigned long int, -), 6); + CHECK (FN4 (int, unsigned long int, *), -8); + + CHECK (FN5 (char), -77); + CHECK (FN5 (unsigned char), -77); + CHECK (FN5 (short), -77); + CHECK (FN5 (unsigned short), -77); + CHECK (FN5 (int), -77); + CHECK (FN5 (unsigned int), -77); + CHECK (FN5 (long int), -77); + CHECK (FN5 (unsigned long int), -77); + CHECK (FN5 (long long int), -77); + CHECK (FN5 (unsigned long long int), -77); + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-2.c new file mode 100644 index 000000000..aeca04d34 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-2.c @@ -0,0 +1,7 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow" } */ + +#define ASM1(a) asm volatile ("" : "+g" (a)) +#define ASM2(a, b) asm volatile ("" : "+g" (a), "+g" (b)) + +#include "overflow-1.c" diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c new file mode 100644 index 000000000..fd6c6d325 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-1.c @@ -0,0 +1,67 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */ + +#include <stdio.h> + +#define SCHAR_MAX __SCHAR_MAX__ +#define SHRT_MAX __SHRT_MAX__ +#define INT_MAX __INT_MAX__ +#define INT_MIN (-__INT_MAX__ - 1) + +void __attribute__((noinline,noclone)) +check (int i, int j) +{ + if (i != j) + __builtin_abort (); +} + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + +#if __INT_MAX__ == 2147483647 + /* Here, nothing should fail. */ + volatile int j = INT_MAX; + volatile int i = -1; + volatile int k = j + i; + check (k, 2147483646); + k = i + j; + check (k, 2147483646); + j--; + check (j, 2147483646); + + i = 1; + j = INT_MIN; + k = i + j; + check (k, -2147483647); + k = j + i; + check (k, -2147483647); + j++; + check (j, -2147483647); +#endif + + /* Test integer promotion. */ +#if __SCHAR_MAX__ == 127 + volatile signed char a = SCHAR_MAX; + volatile signed char b = 1; + volatile signed char c = a + b; + check (c, -128); + a++; + check (a, -128); +#endif + +#if __SHRT_MAX__ == 32767 + volatile short d = SHRT_MAX; + volatile short e = 1; + volatile short f = d + e; + check (f, -32768); + d++; + check (d, -32768); +#endif + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-2.c new file mode 100644 index 000000000..85499d86c --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-add-2.c @@ -0,0 +1,60 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */ + +#define INT_MAX __INT_MAX__ +#define INT_MIN (-__INT_MAX__ - 1) +#define LONG_MAX __LONG_MAX__ +#define LONG_MIN (-__LONG_MAX__ - 1L) +#define LLONG_MAX __LONG_LONG_MAX__ +#define LLONG_MIN (-__LONG_LONG_MAX__ - 1L) + +int +main (void) +{ + volatile int j = INT_MAX; + volatile int i = 1; + volatile int k = j + i; + k = i + j; + j++; + j = INT_MAX - 100; + j += (1 << 10); + + j = INT_MIN; + i = -1; + k = i + j; + k = j + i; + j = INT_MIN + 100; + j += -(1 << 10); + + volatile long int m = LONG_MAX; + volatile long int n = 1; + volatile long int o = m + n; + o = n + m; + m++; + m = LONG_MAX - 100; + m += (1 << 10); + + m = LONG_MIN; + n = -1; + o = m + n; + o = n + m; + m = LONG_MIN + 100; + m += -(1 << 10); + + return 0; +} + +/* { dg-output "signed integer overflow: 2147483647 \\+ 1 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 1 \\+ 2147483647 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 2147483647 \\+ 1 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 2147483547 \\+ 1024 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -1 \\+ -2147483648 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 \\+ -1 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -2147483548 \\+ -1024 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: \[^\n\r]* \\+ 1 cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 1 \\+ \[^\n\r]* cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: \[^\n\r]* \\+ 1 cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: \[^\n\r]* \\+ 1024 cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1 cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -1 \\+ -\[^\n\r]* cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1024 cannot be represented in type 'long int'" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-int128.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-int128.c new file mode 100644 index 000000000..9a850243d --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-int128.c @@ -0,0 +1,47 @@ +/* { dg-do run } */ +/* { dg-require-effective-target int128 } */ +/* { dg-options "-fsanitize=signed-integer-overflow" } */ + +/* 2^127 - 1 */ +#define INT128_MAX (__int128) (((unsigned __int128) 1 << ((__SIZEOF_INT128__ * __CHAR_BIT__) - 1)) - 1) +#define INT128_MIN (-INT128_MAX - 1) + +int +main (void) +{ + volatile __int128 i = INT128_MAX; + volatile __int128 j = 1; + volatile __int128 k = i + j; + k = j + i; + i++; + j = INT128_MAX - 100; + j += (1 << 10); + + j = INT128_MIN; + i = -1; + k = i + j; + k = j + i; + j--; + j = INT128_MIN + 100; + j += -(1 << 10); + + i = INT128_MAX; + j = 2; + k = i * j; + + i = INT128_MIN; + i = -i; + + return 0; +} + +/* { dg-output "signed integer overflow: 0x7fffffffffffffffffffffffffffffff \\+ 1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 1 \\+ 0x7fffffffffffffffffffffffffffffff cannot be represented in type '__int128'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffffff \\+ 1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffff9b \\+ 1024 cannot be represented in type '__int128'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -1 \\+ 0x80000000000000000000000000000000 cannot be represented in type '__int128'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 \\+ -1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 \\+ -1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000064 \\+ -1024 cannot be represented in type '__int128'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffffff \\* 2 cannot be represented in type '__int128'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of 0x80000000000000000000000000000000 cannot be represented in type '__int128'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-1.c new file mode 100644 index 000000000..afb1a2596 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-1.c @@ -0,0 +1,53 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */ + +#include <stdio.h> + +#define SCHAR_MAX __SCHAR_MAX__ +#define SHRT_MAX __SHRT_MAX__ +#define INT_MAX __INT_MAX__ +#define INT_MIN (-__INT_MAX__ - 1) + +void __attribute__((noinline,noclone)) +check (int i, int j) +{ + if (i != j) + __builtin_abort (); +} + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + /* Test integer promotion. */ +#if __SCHAR_MAX__ == 127 + volatile signed char a = -2; + volatile signed char b = SCHAR_MAX; + volatile signed char c = a * b; + check (c, 2); +#endif + +#if __SHRT_MAX__ == 32767 + volatile short d = SHRT_MAX; + volatile short e = 2; + volatile short f = d * e; + check (f, -2); +#endif + +#if __INT_MAX__ == 2147483647 + volatile int m = INT_MAX; + volatile int n = 1; + volatile int o = m * n; + check (o, INT_MAX); + + m = INT_MIN; + o = m * n; + check (o, INT_MIN); +#endif + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-2.c new file mode 100644 index 000000000..ece25a354 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-2.c @@ -0,0 +1,26 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */ + +#define INT_MAX __INT_MAX__ +#define LONG_MAX __LONG_MAX__ + +int +main (void) +{ + volatile int j = INT_MAX; + volatile int i = 2; + volatile int k = j * i; + k = i * j; + + volatile long int m = LONG_MAX; + volatile long int n = 2; + volatile long int o = m * n; + o = n * m; + + return 0; +} + +/* { dg-output "signed integer overflow: 2147483647 \\* 2 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 2 \\* 2147483647 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: \[^\n\r]* \\* 2 cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 2 \\* \[^\n\r]* cannot be represented in type 'long int'" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-3.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-3.c new file mode 100644 index 000000000..037609be0 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-3.c @@ -0,0 +1,46 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow" } */ + +#include <stdio.h> + +__attribute__((noinline, noclone)) long long +mul (long long x, long long y) +{ + return x * y; +} + +long long tab[] = { + 0x7fffffffLL, 0x7fffffffLL, 0x3fffffff00000001LL, + -0x80000000LL, -0x80000000LL, 0x4000000000000000LL, + 0x7fffffffLL, -0x80000000LL, -0x3fffffff80000000LL, + -0x80000000LL, 0x7fffffffLL, -0x3fffffff80000000LL, + 3LL, 5LL, 15LL, + -3LL, -9LL, 27LL, + 6LL, -7LL, -42LL, + -12LL, 13LL, -156LL, + 0x1555555555555555LL, 6LL, 0x7ffffffffffffffeLL, + -0x1555555555555555LL, -6LL, 0x7ffffffffffffffeLL, + 0x1555555555555555LL, -6LL, -0x7ffffffffffffffeLL, + -0x1555555555555555LL, 6LL, -0x7ffffffffffffffeLL, + 0x81234567LL, 0xfdbe971fLL, 0x7fffffffbea72879LL, + -0x81234567LL, -0xfdbe971fLL, 0x7fffffffbea72879LL, + 0x81234567LL, -0xfdbe971fLL, -0x7fffffffbea72879LL, + -0x81234567LL, 0xfdbe971fLL, -0x7fffffffbea72879LL +}; + +int +main () +{ + fputs ("UBSAN TEST START\n", stderr); + + unsigned int i; + for (i = 0; i < sizeof (tab) / sizeof (long long); i += 3) + if (mul (tab[i], tab[i + 1]) != tab[i + 2] + || mul (tab[i + 1], tab[i]) != tab[i + 2]) + __builtin_abort (); + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-4.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-4.c new file mode 100644 index 000000000..82e114001 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-mul-4.c @@ -0,0 +1,86 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow" } */ + + +int +main () +{ +#define MUL_WITH_CHECK(xv, yv, zv) \ + do { \ + long long x = xv; \ + long long y = yv; \ + long long z; \ + asm ("" : "+g" (x)); \ + asm ("" : "+g" (y)); \ + z = x * y; \ + asm ("" : "+g" (z)); \ + if (z != zv) \ + __builtin_abort (); \ + } while (0) + MUL_WITH_CHECK (0x1555555555555556LL, 6LL, -0x7ffffffffffffffcLL); + MUL_WITH_CHECK (-0x1555555555555556LL, -6LL, -0x7ffffffffffffffcLL); + MUL_WITH_CHECK (0x1555555555555556LL, -6LL, 0x7ffffffffffffffcLL); + MUL_WITH_CHECK (-0x1555555555555556LL, 6LL, 0x7ffffffffffffffcLL); + MUL_WITH_CHECK (0x81234568LL, 0xfdbe971fLL, -0x7fffffff439a4068LL); + MUL_WITH_CHECK (-0x81234568LL, -0xfdbe971fLL, -0x7fffffff439a4068LL); + MUL_WITH_CHECK (0x81234568LL, -0xfdbe971fLL, 0x7fffffff439a4068LL); + MUL_WITH_CHECK (-0x81234568LL, 0xfdbe971fLL, 0x7fffffff439a4068LL); + MUL_WITH_CHECK (0x1555555555555555LL, 7LL, -0x6aaaaaaaaaaaaaadLL); + MUL_WITH_CHECK (-0x1555555555555555LL, -7LL, -0x6aaaaaaaaaaaaaadLL); + MUL_WITH_CHECK (0x1555555555555555LL, -7LL, 0x6aaaaaaaaaaaaaadLL); + MUL_WITH_CHECK (-0x1555555555555555LL, 7LL, 0x6aaaaaaaaaaaaaadLL); + MUL_WITH_CHECK (0x81234567LL, 0xfdbe9720LL, -0x7fffffffc0359220LL); + MUL_WITH_CHECK (-0x81234567LL, -0xfdbe9720LL, -0x7fffffffc0359220LL); + MUL_WITH_CHECK (0x81234567LL, -0xfdbe9720LL, 0x7fffffffc0359220LL); + MUL_WITH_CHECK (-0x81234567LL, 0xfdbe9720LL, 0x7fffffffc0359220LL); + MUL_WITH_CHECK (6LL, 0x1555555555555556LL, -0x7ffffffffffffffcLL); + MUL_WITH_CHECK (-6LL, -0x1555555555555556LL, -0x7ffffffffffffffcLL); + MUL_WITH_CHECK (-6LL, 0x1555555555555556LL, 0x7ffffffffffffffcLL); + MUL_WITH_CHECK (6LL, -0x1555555555555556LL, 0x7ffffffffffffffcLL); + MUL_WITH_CHECK (0xfdbe971fLL, 0x81234568LL, -0x7fffffff439a4068LL); + MUL_WITH_CHECK (-0xfdbe971fLL, -0x81234568LL, -0x7fffffff439a4068LL); + MUL_WITH_CHECK (-0xfdbe971fLL, 0x81234568LL, 0x7fffffff439a4068LL); + MUL_WITH_CHECK (0xfdbe971fLL, -0x81234568LL, 0x7fffffff439a4068LL); + MUL_WITH_CHECK (7LL, 0x1555555555555555LL, -0x6aaaaaaaaaaaaaadLL); + MUL_WITH_CHECK (-7LL, -0x1555555555555555LL, -0x6aaaaaaaaaaaaaadLL); + MUL_WITH_CHECK (-7LL, 0x1555555555555555LL, 0x6aaaaaaaaaaaaaadLL); + MUL_WITH_CHECK (7LL, -0x1555555555555555LL, 0x6aaaaaaaaaaaaaadLL); + MUL_WITH_CHECK (0xfdbe9720LL, 0x81234567LL, -0x7fffffffc0359220LL); + MUL_WITH_CHECK (-0xfdbe9720LL, -0x81234567LL, -0x7fffffffc0359220LL); + MUL_WITH_CHECK (-0xfdbe9720LL, 0x81234567LL, 0x7fffffffc0359220LL); + MUL_WITH_CHECK (0xfdbe9720LL, -0x81234567LL, 0x7fffffffc0359220LL); + return 0; +} + +/* { dg-output "overflow-mul-4.c:20:\[^\n\r]*signed integer overflow: 1537228672809129302 \\* 6 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:21:\[^\n\r]*signed integer overflow: -1537228672809129302 \\* -6 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:22:\[^\n\r]*signed integer overflow: 1537228672809129302 \\* -6 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:23:\[^\n\r]*signed integer overflow: -1537228672809129302 \\* 6 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:24:\[^\n\r]*signed integer overflow: 2166572392 \\* 4257126175 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:25:\[^\n\r]*signed integer overflow: -2166572392 \\* -4257126175 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:26:\[^\n\r]*signed integer overflow: 2166572392 \\* -4257126175 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:27:\[^\n\r]*signed integer overflow: -2166572392 \\* 4257126175 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:28:\[^\n\r]*signed integer overflow: 1537228672809129301 \\* 7 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:29:\[^\n\r]*signed integer overflow: -1537228672809129301 \\* -7 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:30:\[^\n\r]*signed integer overflow: 1537228672809129301 \\* -7 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:31:\[^\n\r]*signed integer overflow: -1537228672809129301 \\* 7 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:32:\[^\n\r]*signed integer overflow: 2166572391 \\* 4257126176 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:33:\[^\n\r]*signed integer overflow: -2166572391 \\* -4257126176 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:34:\[^\n\r]*signed integer overflow: 2166572391 \\* -4257126176 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:35:\[^\n\r]*signed integer overflow: -2166572391 \\* 4257126176 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:36:\[^\n\r]*signed integer overflow: 6 \\* 1537228672809129302 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:37:\[^\n\r]*signed integer overflow: -6 \\* -1537228672809129302 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:38:\[^\n\r]*signed integer overflow: -6 \\* 1537228672809129302 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:39:\[^\n\r]*signed integer overflow: 6 \\* -1537228672809129302 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:40:\[^\n\r]*signed integer overflow: 4257126175 \\* 2166572392 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:41:\[^\n\r]*signed integer overflow: -4257126175 \\* -2166572392 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:42:\[^\n\r]*signed integer overflow: -4257126175 \\* 2166572392 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:43:\[^\n\r]*signed integer overflow: 4257126175 \\* -2166572392 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:44:\[^\n\r]*signed integer overflow: 7 \\* 1537228672809129301 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:45:\[^\n\r]*signed integer overflow: -7 \\* -1537228672809129301 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:46:\[^\n\r]*signed integer overflow: -7 \\* 1537228672809129301 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:47:\[^\n\r]*signed integer overflow: 7 \\* -1537228672809129301 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:48:\[^\n\r]*signed integer overflow: 4257126176 \\* 2166572391 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:49:\[^\n\r]*signed integer overflow: -4257126176 \\* -2166572391 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:50:\[^\n\r]*signed integer overflow: -4257126176 \\* 2166572391 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*overflow-mul-4.c:51:\[^\n\r]*signed integer overflow: 4257126176 \\* -2166572391 cannot be represented in type 'long long int'" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-negate-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-negate-1.c new file mode 100644 index 000000000..85f81d8b5 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-negate-1.c @@ -0,0 +1,38 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */ + +#define INT_MIN (-__INT_MAX__ - 1) +#define LONG_MIN (-__LONG_MAX__ - 1L) +#define LLONG_MIN (-__LONG_LONG_MAX__ - 1LL) + +int +main (void) +{ + int e = 1, f = -1; + volatile int i = INT_MIN; + volatile int i2 = i & (((((((-i) + 1) - 1) + 1) - 1) + 1) - 1); + i2 = -(i + e + f); + i = -i; + + volatile long int li = LONG_MIN; + volatile long int li2 = li & (((((((-li) + 1) - 1) + 1) - 1) + 1) - 1); + li2 = -(li + e + f); + li = -li; + + volatile long long int lli = LLONG_MIN; + volatile long long int lli2 = lli & (((((((-lli) + 1) - 1) + 1) - 1) + 1) - 1); + lli2 = -(lli + e + f); + lli = -lli; + + return 0; +} + +/* { dg-output "negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -\[^\n\r]* cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -\[^\n\r]* cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -\[^\n\r]* cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -9223372036854775808 cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -9223372036854775808 cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*negation of -9223372036854775808 cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-negate-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-negate-2.c new file mode 100644 index 000000000..6a4f288bb --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-negate-2.c @@ -0,0 +1,45 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */ + +#include <stdio.h> + +#define SCHAR_MIN (-__SCHAR_MAX__ - 1) +#define SHRT_MIN (-__SHRT_MAX__ - 1) +#define INT_MIN (-__INT_MAX__ - 1) +#define LONG_MIN (-__LONG_MAX__ - 1L) +#define LLONG_MIN (-__LONG_LONG_MAX__ - 1LL) + +#define CHECK(A, B) ({ if ((A) != (B)) __builtin_abort (); }) + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + volatile signed char c = -SCHAR_MIN; + CHECK (c, -128); + + volatile short s = -SHRT_MIN; + CHECK (s, -32768); + + volatile int i = INT_MIN; + i = -(unsigned) i; + CHECK (i, -0x80000000); + + volatile long int li = LONG_MIN; + li = -(unsigned long) li; +#if __LONG_MAX__ == 2147483647L + CHECK (li, -0x80000000L); +#elif __LONG_MAX__ == 9223372036854775807L + CHECK (li, -0x8000000000000000L); +#endif + + volatile long long lli = LLONG_MIN; + lli = -(unsigned long long) lli; + CHECK (lli, -0x8000000000000000L); + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-sub-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-sub-1.c new file mode 100644 index 000000000..15f04455e --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-sub-1.c @@ -0,0 +1,69 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */ + +#include <stdio.h> + +#define SCHAR_MAX __SCHAR_MAX__ +#define SCHAR_MIN (-__SCHAR_MAX__ - 1) +#define SHRT_MAX __SHRT_MAX__ +#define SHRT_MIN (-__SHRT_MAX__ - 1) +#define INT_MAX __INT_MAX__ +#define INT_MIN (-__INT_MAX__ - 1) + +void __attribute__((noinline,noclone)) +check (int i, int j) +{ + if (i != j) + __builtin_abort (); +} + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + +#if __INT_MAX__ == 2147483647 + /* Here, nothing should fail. */ + volatile int i = -1; + volatile int j = INT_MIN; + volatile int k = j - i; + check (k, -2147483647); + k = i - j; + check (k, 2147483647); + j++; + check (j, -2147483647); + + i = 1; + j = INT_MAX; + k = i - j; + check (k, -2147483646); + k = j - i; + check (k, 2147483646); + j--; + check (k, 2147483646); +#endif + + /* Test integer promotion. */ +#if __SCHAR_MAX__ == 127 + volatile signed char a = SCHAR_MIN; + volatile signed char b = 1; + volatile signed char c = a - b; + check (c, 127); + a--; + check (a, 127); +#endif + +#if __SHRT_MAX__ == 32767 + volatile short d = SHRT_MIN; + volatile short e = 1; + volatile short f = d - e; + check (f, 32767); + d--; + check (d, 32767); +#endif + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-sub-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-sub-2.c new file mode 100644 index 000000000..6476b65d2 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/overflow-sub-2.c @@ -0,0 +1,54 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow -Wno-unused-variable" } */ + +#define INT_MAX __INT_MAX__ +#define INT_MIN (-__INT_MAX__ - 1) +#define LONG_MAX __LONG_MAX__ +#define LONG_MIN (-__LONG_MAX__ - 1L) +#define LLONG_MAX __LONG_LONG_MAX__ +#define LLONG_MIN (-__LONG_LONG_MAX__ - 1L) + +int +main (void) +{ + volatile int j = INT_MIN; + volatile int i = 1; + volatile int k = j - i; + j--; + j = INT_MIN + 100; + j -= (1 << 10); + + j = INT_MIN; + i = -1; + k = j - -i; + + i = INT_MIN + 1000; + i -= (1 << 20); + + volatile long int l = LONG_MIN; + volatile long int m = 1; + volatile long int n = l - m; + l--; + l = LONG_MIN + 100; + l -= (1 << 10); + + l = LONG_MIN; + m = -1; + n = l - -m; + + m = LONG_MIN + 1000; + m -= (1 << 20); + + return 0; +} + +/* { dg-output "signed integer overflow: -2147483648 - 1 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 \\+ -1 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -2147483548 \\+ -1024 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 \\+ -1 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -2147482648 \\+ -1048576 cannot be represented in type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* - 1 cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1 cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1024 cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1 cannot be represented in type 'long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1048576 cannot be represented in type 'long int'" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr58443-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr58443-1.c new file mode 100644 index 000000000..76f1dda07 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr58443-1.c @@ -0,0 +1,11 @@ +/* PR sanitizer/58443 */ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=shift,unreachable -w" } */ + +int +foo (int u, int o) +{ + return u / o; +} + +/* { dg-final { scan-assembler-not "__ubsan_handle_divrem_overflow" } } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr58443-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr58443-2.c new file mode 100644 index 000000000..a135758a8 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr58443-2.c @@ -0,0 +1,11 @@ +/* PR sanitizer/58443 */ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=unreachable,integer-divide-by-zero -w" } */ + +int +foo (int u, int o) +{ + return u >> o; +} + +/* { dg-final { scan-assembler-not "__ubsan_handle_shift_out_of_bounds" } } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr58443-3.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr58443-3.c new file mode 100644 index 000000000..5696a62df --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr58443-3.c @@ -0,0 +1,18 @@ +/* PR sanitizer/58443 */ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=undefined -w" } */ + +int +foo (int u, int o) +{ + return u >> o; +} + +int +bar (int u, int o) +{ + return u / o; +} + +/* { dg-final { scan-assembler "__ubsan_handle_divrem_overflow" } } */ +/* { dg-final { scan-assembler "__ubsan_handle_shift_out_of_bounds" } } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59333.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59333.c new file mode 100644 index 000000000..b68775702 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59333.c @@ -0,0 +1,18 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=undefined" } */ + +long long int __attribute__ ((noinline, noclone)) +foo (long long int i, long long int j) +{ + asm (""); + return i + j; +} + +int +main (void) +{ + foo (2LL, __LONG_LONG_MAX__); + return 0; +} + +/* { dg-output "signed integer overflow: 2 \\+ 9223372036854775807 cannot be represented in type 'long long int'" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59397.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59397.c new file mode 100644 index 000000000..0de025835 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59397.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=signed-integer-overflow" } */ + +typedef enum E { A = -1 } e; +int +foo (void) +{ + e e = A; + return e + 1; +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59503.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59503.c new file mode 100644 index 000000000..1637f01b5 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59503.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=signed-integer-overflow" } */ + +#include <stdio.h> + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + long long int a = 14; + long int b = 9; + asm volatile ("" : "+r" (a), "+r" (b)); + if ((a - b) != 5) + __builtin_abort (); + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59667.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59667.c new file mode 100644 index 000000000..7fad70299 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr59667.c @@ -0,0 +1,14 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=undefined" } */ +/* { dg-shouldfail "ubsan" } */ + +int +main (void) +{ + unsigned int len = 1; + float (*P)[len][len] = 0; + (*P)[0][0] = 1; + return 0; +} + +/* { dg-output "store to null pointer of type 'float'(\n|\r\n|\r)" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr60613-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr60613-1.c new file mode 100644 index 000000000..6794532a4 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr60613-1.c @@ -0,0 +1,41 @@ +/* PR sanitizer/60613 */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=undefined" } */ + +#include <stdio.h> + +long long y; + +__attribute__((noinline, noclone)) long long +foo (long long x) +{ + asm (""); + if (x >= 0 || x < -2040) + return 23; + x += 2040; + return x - y; +} + +__attribute__((noinline, noclone)) long long +bar (long long x) +{ + asm (""); + return 8LL - x; +} + +int +main () +{ + fputs ("UBSAN TEST START\n", stderr); + + y = 1; + if (foo (8 - 2040) != 8 - 1) + __builtin_abort (); + if (bar (1) != 8 - 1) + __builtin_abort (); + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr60613-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr60613-2.c new file mode 100644 index 000000000..92c2de81e --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/pr60613-2.c @@ -0,0 +1,36 @@ +/* PR sanitizer/60613 */ +/* { dg-do run } */ +/* { dg-options "-fsanitize=undefined" } */ + +long long y; + +__attribute__((noinline, noclone)) long long +foo (long long x) +{ + asm (""); + if (x >= 0 || x < -2040) + return 23; + x += 2040; + return x - y; +} + +__attribute__((noinline, noclone)) long long +bar (long long x) +{ + asm (""); + return 8LL - x; +} + +int +main () +{ + y = -__LONG_LONG_MAX__ + 6; + if (foo (8 - 2040) != -__LONG_LONG_MAX__) + __builtin_abort (); + if (bar (-__LONG_LONG_MAX__ + 5) != -__LONG_LONG_MAX__ + 1) + __builtin_abort (); + return 0; +} + +/* { dg-output "signed integer overflow: 8 \\- -9223372036854775801 cannot be represented in type 'long long int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*signed integer overflow: 8 \\- -9223372036854775802 cannot be represented in type 'long long int'" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-1.c new file mode 100644 index 000000000..19b1eb08b --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-1.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=shift -Wall -Werror -O" } */ + +#include <stdio.h> + +static int x; +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + int o = 1; + int y = x << o; + + fputs ("UBSAN TEST END\n", stderr); + return y; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-2.c new file mode 100644 index 000000000..14ac17def --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-2.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=shift -Wall -Werror -O" } */ + +int +foo (int i, unsigned int u) +{ + return u / i; +} + +int +bar (int i, unsigned int u) +{ + return u % i; +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-3.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-3.c new file mode 100644 index 000000000..dd2903bd6 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-3.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=shift -Wall -Werror -O" } */ + +int x; + +int +foo (int i, int u) +{ + return (i << u) << x; +} + +int +bar (int i, int u) +{ + return (i >> u) >> x; +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-4.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-4.c new file mode 100644 index 000000000..aa34a70ed --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/save-expr-4.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=shift -Wall -Werror -O" } */ + +int x; + +int +foo (int i, unsigned int u) +{ + return (i % u) << (x / u); +} + +int +bar (int i, unsigned int u) +{ + return (((x % u) << (u / i)) >> x); +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-1.c new file mode 100644 index 000000000..d2538802a --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-1.c @@ -0,0 +1,31 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=shift -w" } */ + +typedef const unsigned long long int CULLI; +typedef volatile int VI; +struct s { signed long int a; }; + +int +main (void) +{ + int a = 1; + struct s s = { .a = 400 }; + CULLI culli = 42; + VI vi = 370; + volatile int shiftcount = 153; + + a <<= 152; + 1 << shiftcount; + 1 << 154; + culli << 524; + 1 << vi++; + (long) 1 << (s.a + 2); + + return 0; +} +/* { dg-output "shift exponent 152 is too large for \[^\n\r]*-bit type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent 153 is too large for \[^\n\r]*-bit type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent 154 is too large for \[^\n\r]*-bit type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent 524 is too large for \[^\n\r]*-bit type 'long long unsigned int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent 370 is too large for \[^\n\r]*-bit type 'int'(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent 402 is too large for \[^\n\r]*-bit type 'long int'" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-2.c new file mode 100644 index 000000000..aaaeb6fcc --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-2.c @@ -0,0 +1,23 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=shift -w" } */ + +int +main (void) +{ + int a = 1; + volatile int b = -5; + long long int c = -6; + + a << -3; + 1 << -4; + 1 << b; + a << c; + a << (b + c); + + return 0; +} +/* { dg-output "shift exponent -3 is negative(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent -4 is negative(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent -5 is negative(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent -6 is negative(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*shift exponent -11 is negative" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-3.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-3.c new file mode 100644 index 000000000..65ee5d882 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-3.c @@ -0,0 +1,19 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=shift -w" } */ + +#include <stdio.h> + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + unsigned int a = 1; + a <<= 31; + a <<= 1; + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-4.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-4.c new file mode 100644 index 000000000..5f095b61a --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-4.c @@ -0,0 +1,14 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=shift -w" } */ + +struct S { unsigned long long int b:40; } s; + +int +main () +{ + s.b = 2; + s.b <<= 120; + return 0; +} + +/* { dg-output "shift exponent 120 is too large\[^\n\r]*" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-5.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-5.c new file mode 100644 index 000000000..6f9c52a72 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-5.c @@ -0,0 +1,33 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=shift -w" } */ +/* { dg-shouldfail "ubsan" } */ + +int x; +int +foo (void) +{ + /* None of the following should pass. */ + switch (x) + { + case 1 >> -1: +/* { dg-error "case label does not reduce to an integer constant" "" {target c } 12 } */ +/* { dg-error "is not a constant expression" "" { target c++ } 12 } */ + case -1 >> -1: +/* { dg-error "case label does not reduce to an integer constant" "" {target c } 15 } */ +/* { dg-error "is not a constant expression" "" { target c++ } 15 } */ + case 1 << -1: +/* { dg-error "case label does not reduce to an integer constant" "" {target c } 18 } */ +/* { dg-error "is not a constant expression" "" { target c++ } 18 } */ + case -1 << -1: +/* { dg-error "case label does not reduce to an integer constant" "" {target c } 21 } */ +/* { dg-error "is not a constant expression" "" { target c++ } 21 } */ + case -1 >> 200: +/* { dg-error "case label does not reduce to an integer constant" "" {target c } 24 } */ +/* { dg-error "is not a constant expression" "" { target c++ } 24 } */ + case 1 << 200: +/* { dg-error "case label does not reduce to an integer constant" "" {target c } 27 } */ +/* { dg-error "is not a constant expression" "" { target c++ } 27 } */ + return 1; + } + return 0; +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-6.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-6.c new file mode 100644 index 000000000..d88a42910 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/shift-6.c @@ -0,0 +1,38 @@ +/* PR sanitizer/58413 */ +/* { dg-do run { target int32plus } } */ +/* { dg-options "-fsanitize=shift -w" } */ + +#include <stdio.h> + +int x = 7; +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + /* All of the following should pass. */ + int A[128 >> 5] = {}; + int B[128 << 5] = {}; + + static int e = + ((int) + (0x00000000 | ((31 & ((1 << (4)) - 1)) << (((15) + 6) + 4)) | + ((0) << ((15) + 6)) | ((0) << (15)))); + + if (e != 503316480) + __builtin_abort (); + + switch (x) + { + case 1 >> 4: + case 1 << 4: + case 128 << (4 + 1): + case 128 >> (4 + 1): + return 1; + } + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/typedef-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/typedef-1.c new file mode 100644 index 000000000..8dcf451c3 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/typedef-1.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-fsanitize=undefined" } */ + +typedef int V; +int +foo (void) +{ + V v = 9; + int a = 3; + v += v % a; + return v / 3; +} diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/undefined-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/undefined-1.c new file mode 100644 index 000000000..d1b9ce787 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/undefined-1.c @@ -0,0 +1,33 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=undefined" } */ + +#include <stdio.h> + +int +foo (int x, int y) +{ + const int z = 2; + if (z & 1) + return x << y; + return 0; +} + +int +bar (int x, int y) +{ + return x + y; +} + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + foo (3, 2); + bar (12, 42); + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/unreachable-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/unreachable-1.c new file mode 100644 index 000000000..336240c96 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/unreachable-1.c @@ -0,0 +1,10 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=unreachable" } */ +/* { dg-shouldfail "ubsan" } */ + +int +main (void) +{ + __builtin_unreachable (); +} + /* { dg-output "execution reached a __builtin_unreachable\\(\\) call" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-1.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-1.c new file mode 100644 index 000000000..0fecfa2a3 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-1.c @@ -0,0 +1,119 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=vla-bound -Wall -Wno-unused-variable" } */ + +typedef long int V; +int x = -1; +double di = -3.2; +V v = -6; + +static int __attribute__ ((noinline, noclone)) +bar (void) +{ + return -4; +} + +static void __attribute__ ((noinline, noclone)) +fn1 (void) +{ + int a[x]; +} + +static void __attribute__ ((noinline, noclone)) +fn2 (void) +{ + int a[x][x]; +} + +static void __attribute__ ((noinline, noclone)) +fn3 (void) +{ + int a[x][x][x]; +} + +static void __attribute__ ((noinline, noclone)) +fn4 (void) +{ + int b[x - 4]; +} + +static void __attribute__ ((noinline, noclone)) +fn5 (void) +{ + int c[(int) di]; +} + +static void __attribute__ ((noinline, noclone)) +fn6 (void) +{ + int d[1 + x]; +} + +static void __attribute__ ((noinline, noclone)) +fn7 (void) +{ + int e[1 ? x : -1]; +} + +static void __attribute__ ((noinline, noclone)) +fn8 (void) +{ + int f[++x]; +} + +static void __attribute__ ((noinline, noclone)) +fn9 (void) +{ + int g[(signed char) --x]; +} + +static void __attribute__ ((noinline, noclone)) +fn10 (void) +{ + int h[(++x, --x, x)]; +} + +static void __attribute__ ((noinline, noclone)) +fn11 (void) +{ + int i[v]; +} + +static void __attribute__ ((noinline, noclone)) +fn12 (void) +{ + int j[bar ()]; +} + +int +main (void) +{ + fn1 (); + fn2 (); + fn3 (); + fn4 (); + fn5 (); + fn6 (); + fn7 (); + fn8 (); + fn9 (); + fn10 (); + fn11 (); + fn12 (); + return 0; +} + +/* { dg-output "variable length array bound evaluates to non-positive value -1(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -1(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -1(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -1(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -1(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -1(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -5(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -3(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value 0(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -1(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value 0(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -1(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -1(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -6(\n|\r\n|\r)" } */ +/* { dg-output "\[^\n\r]*variable length array bound evaluates to non-positive value -4" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-2.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-2.c new file mode 100644 index 000000000..a11e4e722 --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-2.c @@ -0,0 +1,22 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=vla-bound -Wall -Wno-unused-variable" } */ + +#include <stdio.h> + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + const int t = 0; + struct s { + int x; + /* Don't instrument this one. */ + int g[t]; + }; + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-3.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-3.c new file mode 100644 index 000000000..7772857fd --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-3.c @@ -0,0 +1,25 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=vla-bound" } */ + +#include <stdio.h> + +/* Don't instrument the arrays here. */ +int +foo (int n, int a[]) +{ + return a[n - 1]; +} + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + int a[6] = { }; + int ret = foo (3, a); + + fputs ("UBSAN TEST END\n", stderr); + return ret; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ diff --git a/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-4.c b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-4.c new file mode 100644 index 000000000..8a7bbac9a --- /dev/null +++ b/gcc-4.9/gcc/testsuite/c-c++-common/ubsan/vla-4.c @@ -0,0 +1,21 @@ +/* { dg-do run } */ +/* { dg-options "-fsanitize=vla-bound" } */ + +#include <stdio.h> + +int +main (void) +{ + fputs ("UBSAN TEST START\n", stderr); + + int x = 1; + /* Check that the size of an array is evaluated only once. */ + int a[++x]; + if (x != 2) + __builtin_abort (); + + fputs ("UBSAN TEST END\n", stderr); + return 0; +} + +/* { dg-output "UBSAN TEST START(\n|\r\n|\r)UBSAN TEST END" } */ |