1
2 /* A concatenation of varinfo1 .. varinfo4 in a shared object. This
3 is to check for correct functionality in a non-zero-biased ELF
4 executable. */
5
6 /* Relevant compile flags are:
7
8 -Wall -g -I$prefix/include/valgrind
9
10 eg -Wall -g -I`pwd`/Inst/include/valgrind
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <assert.h>
16 #include "memcheck/memcheck.h"
17
18 /* Cause memcheck to complain about the address "a" and so to print
19 its best guess as to what "a" actually is. a must be
20 addressible. */
21 __attribute__((noinline))
croak(void * aV)22 void croak ( void* aV )
23 {
24 char* a = (char*)aV;
25 char* undefp = malloc(1);
26 char saved = *a;
27 assert(undefp);
28 *a = *undefp;
29 (void) VALGRIND_CHECK_MEM_IS_DEFINED(a, 1);
30 *a = saved;
31 free(undefp);
32 }
33
34 #include <stdio.h>
35
36 /* ------------ varinfo1 ------------ */
37
38 int global_u1;
39
40 int global_i1 = 17;
41
42 char global_u2[10];
43
44 char global_i2[10] = { 1,2,3,4,5,6,7,8,9,10 };
45
46 __attribute__((noinline))
varinfo1_main(void)47 static int varinfo1_main ( void )
48 {
49 int local;
50 char* onheap = malloc(3);
51 assert(onheap);
52 croak(onheap+1);
53 free(onheap);
54
55 croak( &global_u1 );
56 croak( &global_i1 );
57 croak( &global_u2[3] );
58 croak( &global_i2[7] );
59 croak( &local );
60 return 0;
61 }
62
63 /* ------------ varinfo2 ------------ */
64 __attribute__((noinline))
foo2(void)65 static void foo2 ( void )
66 {
67 int var;
68 var = 1;
69 { char var[10];
70 var[6] = 4;
71 croak( &var[7] );
72 { struct { double foo; float bar; } var;
73 croak ( 2 + (char*)&var.bar );
74 }
75 }
76 croak( 1 + (char*)&var );
77 }
78 __attribute__((noinline))
varinfo2_main(void)79 static int varinfo2_main ( void )
80 {
81 foo2();
82 return 0;
83 }
84
85 /* ------------ varinfo3 ------------ */
86
87 static char static_global_def[10] = {0,0,0,0,0, 0,0,0,0,0};
88 char nonstatic_global_def[10] = {0,0,0,0,0, 0,0,0,0,0};
89 static char static_global_undef[10];
90 char nonstatic_global_undef[10];
91 __attribute__((noinline))
bar3(char * p1,char * p2,char * p3,char * p4)92 static void bar3 ( char* p1, char* p2, char* p3, char* p4 )
93 {
94 croak(p1);
95 croak(p2);
96 croak(p3);
97 croak(p4);
98 }
99 __attribute__((noinline))
foo3(void)100 static void foo3 ( void )
101 {
102 static char static_local_def[10] = {0,0,0,0,0, 0,0,0,0,0};
103 char nonstatic_local_def[10] = {0,0,0,0,0, 0,0,0,0,0};
104 static char static_local_undef[10];
105 char nonstatic_local_undef[10];
106 croak ( 1 + (char*)&static_global_def );
107 croak ( 2 + (char*)&nonstatic_global_def );
108 croak ( 3 + (char*)&static_global_undef );
109 croak ( 4 + (char*)&nonstatic_global_undef );
110 bar3( 5 + (char*)&static_local_def,
111 6 + (char*)&nonstatic_local_def,
112 7 + (char*)&static_local_undef,
113 8 + (char*)&nonstatic_local_undef );
114 }
115 __attribute__((noinline))
varinfo3_main(void)116 static int varinfo3_main ( void )
117 {
118 foo3();
119 return 0;
120 }
121
122 /* ------------ varinfo4 ------------ */
123
124 #include <string.h>
125
126 typedef struct { short c1; char* c2[3]; } XX;
127
128 typedef
129 struct _str { int bing; int bong; XX xyzzy[77]; }
130 Str;
131
132 __attribute__((noinline))
blah4(int x,int y)133 static int blah4 ( int x, int y )
134 {
135 Str a[10];
136 memset(a, 0, sizeof(a));
137 croak(1 + (char*)(&a[3].xyzzy[x*y].c1));
138 croak( (char*)(&a[5].bong) );
139 croak( 1 + (char*)(&a[3].xyzzy[x*y].c2[2]) );
140 memset(a, 0, sizeof(a));
141 return a[3].xyzzy[x*y].c1;
142 }
143 __attribute__((noinline))
varinfo4_main(void)144 static int varinfo4_main ( void )
145 {
146 fprintf(stderr, "answer is %d\n", blah4(3,7) );
147 return 0;
148 }
149 static void inlinetest(void);
150 /* ------------ varinfo5 ------------ */
151
varinfo5_main(void)152 void varinfo5_main ( void )
153 {
154 varinfo1_main();
155 varinfo2_main();
156 varinfo3_main();
157 varinfo4_main();
158 inlinetest();
159 }
160
161 #define INLINE inline __attribute__((always_inline))
162
fun_c(int argc)163 INLINE void fun_c(int argc) {
164 croak(&argc);
165 }
166
fun_b(int argb)167 INLINE void fun_b(int argb) {
168 fun_c(argb);
169 }
170
fun_a(int * arga)171 INLINE void fun_a(int *arga) {
172 fun_b(*arga);
173 }
174
inlinetest(void)175 void inlinetest(void)
176 {
177 int i = 1;
178 fun_a(&i);
179 }
180