5 #include "helgrind/helgrind.h"
8 static unsigned char shadow
[MAX
];
11 #define V(cond, testline) \
13 fprintf (stderr, "Test at line %d Failed verif at line %d: " #cond "\n", \
14 testline, __LINE__); } \
17 #define CHK(a1,a2,a3,a4) check(__LINE__,a1,a2,a3,a4)
18 /* Check that [p, p+len[ has access access.
19 If heap, check that one byte before and after is unaccessible */
20 static void check (int testline
, void *p
, int len
, unsigned char access
, int heap
)
25 assert (len
< 1000000); // Do not exceed the shadow array
27 if (len
== 0 && p
== NULL
)
29 // malloc(0) can return a ptr or NULL.
30 // Let's not check NULL
32 r
= VALGRIND_HG_GET_ABITS (p
, shadow
, len
);
33 V (r
== VALGRIND_HG_GET_ABITS (p
, NULL
, len
), testline
);
34 V (access
== 0xff ? r
== len
: r
== 0, testline
);
35 for (i
= 0; i
< len
; i
++)
36 V(shadow
[i
] == access
, testline
);
38 /* Check the range starting 1 byte before. */
39 r
= VALGRIND_HG_GET_ABITS (p
-1, shadow
, len
+1);
40 V (r
== VALGRIND_HG_GET_ABITS (p
-1, NULL
, len
+1), testline
);
41 V (access
== 0xff ? r
== len
: r
== 0, testline
);
42 V (shadow
[0] == 0x00, testline
);
43 for (i
= 1; i
< len
+1; i
++)
44 V (shadow
[i
] == access
, testline
);
45 /* Same but one byte after. We need special cases for
47 r
= VALGRIND_HG_GET_ABITS (p
+1, shadow
, len
);
48 V (r
== VALGRIND_HG_GET_ABITS (p
+1, NULL
, len
), testline
);
52 V (access
== 0xff ? r
== len
-1 : r
== 0, testline
);
53 for (i
= 0; i
< len
-1; i
++)
54 V(shadow
[i
] == access
, testline
);
56 V(shadow
[len
-1] == 0x00, testline
);
60 /* return an address on the stack, with big var on the stack,
61 to ensure it is really unaddressable when calling check. */
62 static void* popped_stack_address(void)
65 memcpy(s
, shadow
, MAX
);
77 /* Basic test for an heap object */
78 fprintf(stderr
, "basic heap test\n");
80 CHK (p
, 100, 0xff, 1);
82 CHK (p
, 100, 0x00, 1);
84 /* Basic test for some code : verify 50 bytes of check function code
86 fprintf(stderr
, "code test\n");
87 CHK (check
, 50, 0xff, 0);
89 /* Check something on the stack */
90 fprintf(stderr
, "stack test\n");
91 CHK (&p
, sizeof(p
), 0xff, 0);
94 /* Now shake the heap, to verify various sizes */
95 fprintf(stderr
, "doing many heap blocks\n");
102 int f
[X
][Y
]; // already freed or not ?
103 for (i
= 0; i
< X
; i
++) {
104 for (j
= 0; j
< Y
; j
++) {
106 // A SecMap represents 8Kb. We test the boundaries
107 // around such secmap (X/2 bytes before and after)
108 // We test with blocks covering from 0 till Y-1 secmaps
109 sz
[i
][j
] = j
* 8192 - (j
== 0 ? 0 : X
/2) + i
;
110 ptr
[i
][j
] = malloc(sz
[i
][j
]);
111 CHK(ptr
[i
][j
],sz
[i
][j
], 0xff, 1);
114 /* Shake and check when doing random free */
115 fprintf(stderr
, "random heap free and checks\n");
116 for (i
= 0; i
< X
*Y
/10; i
++) {
120 CHK(ptr
[x
][y
],sz
[x
][y
], 0xff, 1);
124 CHK(ptr
[x
][y
],sz
[x
][y
], 0x00, 1);
128 /* Check that a use after return gives unaddressable. */
129 CHK (popped_stack_address(), 1, 0x00, 0);
130 /* Well well, it seems helgrind keeps the stack accessible */
132 (void) popped_stack_address();