1
2#include <stdio.h>
3#include <assert.h>
4#include <dlfcn.h>
5
6/* see comments in preen_invar_so.c for explanation of this */
7
8
9int main ( void )
10{
11  int i, r, sum = 0;
12  char* im_a_global_array;
13  void* hdl = dlopen("./preen_invars_so.so", RTLD_NOW);
14  assert(hdl);
15  im_a_global_array = dlsym(hdl, "im_a_global_array");
16  assert(im_a_global_array);
17  /* printf("%p %p\n", im_a_global_array, me_too_me_too); */
18
19  /* poke around in the global array, so as to cause exp-ptrcheck
20     to generate an Inv_Global invar for it. */
21  for (i = 10/*ERROR*/; i >= 0; i--) {
22     sum += im_a_global_array[i];
23  }
24  /* iterating 10 .. 0 causes an Unknown->Global transition at i = 9.
25     We do it this way in order that at the end of a loop, there is a
26     Global invar in place for the memory read in the loop, so that
27     the subsequent dlclose (hence munmap) causes it to get preened.
28
29     Unfortunately there's nothing to show that the preen was
30     successful or happened at all.  The only way to see is from the
31     -v output:
32
33     --686--  sg_:  251 Invars preened, of which 1 changed
34
35     It's the "1 changed" bit which is significant.
36  */
37
38  /* let's hope gcc is not clever enough to optimise this away, since
39     if it does, then it will also nuke the preceding loop, and
40     thereby render this test program useless. */
41
42  if (sum & 1) printf("%s bar %d\n", "foo", sum & 1); else
43               printf("foo %s %d\n", "bar", 1 - (sum & 1));
44
45  /* Now close (== unmap) the array, so that exp-ptrcheck has to check
46     its collection of Inv_Global invars, and remove this one from
47     it. */
48  r = dlclose(hdl);
49  assert(r == 0);
50
51  return 0;
52}
53