1/* { dg-do run } */ 2/* { dg-options "-fsanitize=bounds -Wall -Wextra -Wno-unused -Wno-array-bounds" } */ 3 4/* Test runtime errors. */ 5 6struct S { int a[10]; }; 7struct T { int a[5]; int s[2]; }; 8 9int 10foo_5 (void) 11{ 12 return 5; 13} 14 15__attribute__ ((noinline, noclone)) 16void 17fn_p (int p) 18{ 19 (void) p; 20} 21 22static void __attribute__ ((noinline, noclone)) 23fn1 (void) 24{ 25 volatile int a[5]; 26 asm ("" : : "r" (&a) : "memory"); 27 a[2] = a[5]; 28} 29 30static void __attribute__ ((noinline, noclone)) 31fn2 (void) 32{ 33 volatile int a[5]; 34 volatile int j; 35 int i = 5; 36 int *p = &i; 37 asm ("" : : "r" (&a) : "memory"); 38 j = a[*p]; 39} 40 41static void __attribute__ ((noinline, noclone)) 42fn3 (void) 43{ 44 volatile int a[5]; 45 fn_p (a[5]); 46} 47 48static void __attribute__ ((noinline, noclone)) 49fn4 (void) 50{ 51 struct T t; 52 asm ("" : : "r" (&t.a) : "memory"); 53 t.a[foo_5 ()] = 1; 54} 55 56static void __attribute__ ((noinline, noclone)) 57fn5 (void) 58{ 59 int i = 5; 60 volatile int a[i]; 61 asm ("" : : "r" (&a) : "memory"); 62 a[2] = a[i]; 63} 64 65static void __attribute__ ((noinline, noclone)) 66fn6 (void) 67{ 68 int i = 5; 69 volatile int a[i]; 70 volatile int j; 71 fn_p (a[i]); 72 asm ("" : : "r" (&a) : "memory"); 73 j = a[foo_5 ()]; 74} 75 76static void __attribute__ ((noinline, noclone)) 77fn7 (void) 78{ 79 int n = 5; 80 volatile int i; 81 volatile int c[n][n][n]; 82 asm ("" : : "r" (&c[5]) : "memory"); 83 i = c[5][2][2]; 84 asm ("" : : "r" (&c[2]) : "memory"); 85 i = c[2][5][2]; 86 asm ("" : : "r" (&c[2]) : "memory"); 87 i = c[2][2][5]; 88} 89 90static void __attribute__ ((noinline, noclone)) 91fn8 (void) 92{ 93 volatile int i; 94 volatile struct S s; 95 asm ("" : : "r" (&s.a) : "memory"); 96 i = s.a[10]; 97} 98 99static void __attribute__ ((noinline, noclone)) 100fn9 (void) 101{ 102 long int *volatile d[10][5]; 103 asm ("" : : "r" (&d[10]) : "memory"); 104 d[8][3] = d[10][0]; 105} 106 107static void __attribute__ ((noinline, noclone)) 108fn10 (void) 109{ 110 /* Beware of side-effects. */ 111 volatile int x = 10; 112 volatile int e[20]; 113 e[x++] = 3; 114 if (x != 11) 115 __builtin_abort (); 116 e[x--] = 3; 117 if (x != 10) 118 __builtin_abort (); 119} 120 121static void __attribute__ ((noinline, noclone)) 122fn11 (void) 123{ 124 char ***volatile f[5]; 125 asm ("" : : "r" (&f) : "memory"); 126 f[2] = f[5]; 127} 128 129static void __attribute__ ((noinline, noclone)) 130fn12 (int i) 131{ 132 volatile int a[5] = { }; 133 int k = i ? a[i] : i; 134} 135 136int 137main (void) 138{ 139 fn1 (); 140 fn2 (); 141 fn3 (); 142 fn4 (); 143 fn5 (); 144 fn6 (); 145 fn7 (); 146 fn8 (); 147 fn9 (); 148 fn10 (); 149 fn11 (); 150 fn12 (5); 151 return 0; 152} 153 154/* { dg-output "index 5 out of bounds for type 'int \\\[5\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 155/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[5\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 156/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[5\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 157/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[5\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 158/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 159/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 160/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 161/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 162/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[\\\*\\\]\\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 163/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[\\\*\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 164/* { dg-output "\[^\n\r]*index 10 out of bounds for type 'int \\\[10\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 165/* { dg-output "\[^\n\r]*index 10 out of bounds for type 'long int \\\*\\\[10\\\]\\\[5\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 166/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'char \\\*\\\*\\\*\\\[5\\\]'\[^\n\r]*(\n|\r\n|\r)" } */ 167/* { dg-output "\[^\n\r]*index 5 out of bounds for type 'int \\\[5\\\]'" } */ 168