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))
22void 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
38int global_u1;
39
40int global_i1 = 17;
41
42char global_u2[10];
43
44char global_i2[10] = { 1,2,3,4,5,6,7,8,9,10 };
45
46__attribute__((noinline))
47static 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))
65static 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))
79static int varinfo2_main ( void )
80{
81  foo2();
82  return 0;
83}
84
85/* ------------ varinfo3 ------------ */
86
87static 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};
89static char static_global_undef[10];
90       char nonstatic_global_undef[10];
91__attribute__((noinline))
92static 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))
100static 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))
116static int varinfo3_main ( void )
117{
118  foo3();
119  return 0;
120}
121
122/* ------------ varinfo4 ------------ */
123
124#include <string.h>
125
126typedef struct { short c1; char* c2[3]; } XX;
127
128typedef
129   struct _str { int bing; int bong; XX xyzzy[77]; }
130   Str;
131
132__attribute__((noinline))
133static 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))
144static int varinfo4_main ( void )
145{
146  fprintf(stderr, "answer is %d\n", blah4(3,7) );
147  return 0;
148}
149static void inlinetest(void);
150/* ------------ varinfo5 ------------ */
151
152void 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
163INLINE void fun_c(int argc) {
164   croak(&argc);
165}
166
167INLINE void fun_b(int argb) {
168   fun_c(argb);
169}
170
171INLINE void fun_a(int *arga) {
172   fun_b(*arga);
173}
174
175void inlinetest(void)
176{
177   int i = 1;
178   fun_a(&i);
179}
180