File: memcheck_test.c

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (343 lines) | stat: -rw-r--r-- 8,658 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * Copyright (c) 2011 The Native Client Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/*
  Test integration of Valgrind and NaCl.
*/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

/* For NaCl we have to use a special verion of valgrind.h */
#include "native_client/src/third_party/valgrind/nacl_valgrind.h"
#include "native_client/src/third_party/valgrind/nacl_memcheck.h"

#define NOINLINE __attribute__((noinline))
#define INLINE __attribute__((always_inline))

#define SHOW_ME do { \
    fprintf(stderr, "========== %s:%d =========\n",\
                             __FUNCTION__, __LINE__);  \
    fflush(stderr); \
  } while(0)

NOINLINE void break_optimization(void) {
  static volatile int z;
  z++;
}

/*----------------------------------------------------------------------
 Tests to see if memcheck finds memory bugs.
*/

/* Use the memory location '*a' in a conditional statement.
   Use macro instead of a function so that no extra stack frame is created. */
#define USE(ptr) do { \
  fprintf(stderr, "USE: %p\n", (void*)(ptr)); \
  if (*(ptr) == 777) { /* Use *a in a conditional statement. */ \
    fprintf(stderr, "777\n");                                   \
  }                                                             \
} while(0)

/* write something to '*a' */
void update(int *a) {
  *a = 1;
}

/* read from heap memory out of bounds */
void oob_read_test(void) {
  int *foo;
  SHOW_ME;
  foo = (int*)malloc(sizeof(int) * 10);
  USE(foo+10);
  USE(foo-1);
  free(foo);
}

/* write to heap memory out of bounds */
void oob_write_test(void) {
  int *foo;
  SHOW_ME;
  foo = (int*)malloc(sizeof(int) * 10);
  update(foo+10);
  free(foo);
}

/* read uninitialized value from heap */
void umr_heap_test(void) {
  int *foo;
  SHOW_ME;
  foo = (int*)malloc(sizeof(int) * 10);
  USE(foo+5);
  free(foo);
}

/* read uninitialized value from stack */
void umr_stack_test(void) {
  int foo[10];
  SHOW_ME;
  USE(foo+5);
}

/* use heap memory after free() */
void use_after_free_test(void) {
  int *foo;
  SHOW_ME;
  foo = (int*)malloc(sizeof(int) * 10);
  foo[5] = 666;
  free(foo);
  USE(foo+5);
}

/* use heap memory after free() and a series of unrelated malloc/free */
void use_after_free_with_reuse_test(void) {
  int i;
  int *foo;
  int *bar[1000];
  SHOW_ME;
  foo = (int*)malloc(sizeof(int) * 10);
  foo[5] = 666;
  free(foo);
  for (i = 0; i < 1000; i++) {
    bar[i] = (int*)malloc(sizeof(int) * 10);
  }
  for (i = 0; i < 1000; i++) {
    free(bar[i]);
  }
  foo[6] = 123;
}


/* memory leak */
void leak_test(void) {
  int *foo;
  SHOW_ME;
  foo = (int*)malloc(sizeof(int) * 10);
  if (!foo) exit(0);
}

/*----------------------------------------------------------------------
 Test valgrind client requests.
 See http://valgrind.org/docs/manual/manual-core-adv.html for more
 details on valgrind client requests.
*/
void test_client_requests(void) {
  /* Pass a NULL callback. Valgrind will simply print the parameters. */
  /* TODO(kcc): add tests for non-trivial client requests (when they start
   * working) */
  long f = 0;
  SHOW_ME;
  VALGRIND_NON_SIMD_CALL1(f, 0xABCDEF);
  break_optimization(); /* to test an unaligned client request call */
  VALGRIND_NON_SIMD_CALL2(f, 0xBCD001, 0xBCD002);
  VALGRIND_NON_SIMD_CALL3(f, 0xCDE001, 0xCDE002, 0xCDE003);
}

/* test how valgrind prints backtraces */
NOINLINE void test_backtrace(void) {
  SHOW_ME;
  VALGRIND_PRINTF_BACKTRACE("BACKTRACE:\n");
  break_optimization();
}
NOINLINE void test_backtrace1(void) { test_backtrace(); break_optimization(); }
NOINLINE void test_backtrace2(void) { test_backtrace1(); break_optimization(); }
NOINLINE void test_backtrace3(void) { test_backtrace2(); break_optimization(); }

NOINLINE void test_printf(void) {
  SHOW_ME;
  VALGRIND_PRINTF("VG PRINTF\n");
  /*
  TODO(kcc): passing vargs from untrusted code to valgrind does not work yet.
  VALGRIND_PRINTF("VG PRINTF with args: %d %d %d\n", 1, 2, 3);
  */
  break_optimization();
}

/*----------------------------------------------------------------------
 Test valgrind wrappers.
 See http://valgrind.org/docs/manual/manual-core-adv.html for more
 details on function wrapping.
*/

/* Functions to wrap. */
NOINLINE int wrap_me_0(void) { SHOW_ME; return 123; }
NOINLINE int wrap_me_1(int a) { SHOW_ME; return a; }
NOINLINE int wrap_me_2(int a, int b) { SHOW_ME; return a+10*b; }
NOINLINE int wrap_me_3(int a, int b, int c) { SHOW_ME; return a+10*b+100*c; }

/* Wrapper functions. */

NOINLINE int I_WRAP_SONAME_FNNAME_ZZ(NaClZuNONE, wrap_me_0)(void) {
  int ret;
  OrigFn fn;
  VALGRIND_GET_ORIG_FN(fn);
  SHOW_ME;
  CALL_FN_W_v(ret, fn);
  SHOW_ME;
  VALGRIND_NON_SIMD_CALL1(0, 0xABCDEF);
  return ret + 777;  /* change the return value */
}

NOINLINE int I_WRAP_SONAME_FNNAME_ZZ(NaClZuNONE, wrap_me_1)(int a) {
  int ret;
  OrigFn fn;
  VALGRIND_GET_ORIG_FN(fn);
  SHOW_ME;
  CALL_FN_W_W(ret, fn, a);
  return ret * 7;  /* change the return value */
}

NOINLINE int I_WRAP_SONAME_FNNAME_ZZ(NaClZuNONE, wrap_me_2)(int a, int b) {
  int ret;
  OrigFn fn;
  VALGRIND_GET_ORIG_FN(fn);
  SHOW_ME;
  CALL_FN_W_WW(ret, fn, a, b);
  return ret * 7;  /* change the return value */
}

NOINLINE int I_WRAP_SONAME_FNNAME_ZZ(NaClZuNONE, wrap_me_3)(int a, int b,
    int c) {
  int ret;
  OrigFn fn;
  VALGRIND_GET_ORIG_FN(fn);
  SHOW_ME;
  CALL_FN_W_WWW(ret, fn, a, b, c);
  return ret * 7;  /* change the return value */
}

NOINLINE
void function_wrapping_test(void) {
  SHOW_ME;
  /* The wrappers change the return value, so if the wrappers did not work
   these asserts will fail. */
  assert(wrap_me_0() == 123 + 777);
  assert(wrap_me_1(1) == 7 * 1);
  assert(wrap_me_2(1, 2) == 7 * 21);
  assert(wrap_me_3(1, 2, 3) == 7 * 321);
}

/* test that interceptors with a large number of arguments work and do not mess
   up stack traces */
NOINLINE
int zz_wrap_me(int a1, int a2, int a3, int a4, int a5, int a6, int a7) {
  fprintf(stderr, "wrapped: %d, %d, %d, %d, %d, %d, %d\n", a1, a2, a3, a4, a5,
      a6, a7);
  VALGRIND_PRINTF_BACKTRACE("many_args BACKTRACE:\n");
  return a1 + a2 + a3 + a4 + a5 + a6 + a7;
}

NOINLINE
int I_WRAP_SONAME_FNNAME_ZZ(NaClZuNONE, zz_wrap_me)(int a1, int a2, int a3,
    int a4, int a5, int a6, int a7) {
  int ret;
  OrigFn fn;
  VALGRIND_GET_ORIG_FN(fn);
  SHOW_ME;
  CALL_FN_W_7W(ret, fn, a1, a2, a3, a4, a5, a6, a7);
  return ret * 7;  /* change the return value */
}

NOINLINE
int zz_wrap_me0(int a1, int a2, int a3, int a4, int a5, int a6, int a7) {
  int ret = zz_wrap_me(a1, a2, a3, a4, a5, a6, a7);
  break_optimization();
  return ret;
}

NOINLINE
void many_args_wrapping_test(void) {
  SHOW_ME;
  assert(zz_wrap_me0(1, 2, 3, 4, 5, 6, 7) == 7 * 28);
}

/*----------------------------------------------------------------------
 Test that wrapped functions are called with correct stack alignment.
*/

struct AlignedType {
  int blah;
} __attribute__((aligned(16)));

void check_alignment(void* p) {
  assert(((size_t)p) % 16 == 0);
}

NOINLINE void wrap_me_check_alignment(void) {
  struct AlignedType var;
  check_alignment(&var);
}

NOINLINE void I_WRAP_SONAME_FNNAME_ZZ(NaClZuNONE, wrap_me_check_alignment)(void) {
  OrigFn fn;
  VALGRIND_GET_ORIG_FN(fn);
  CALL_FN_v_v(fn);
}

NOINLINE
void function_alignment_wrapping_test(void) {
  SHOW_ME;
  wrap_me_check_alignment();
}


NOINLINE
void strcmp_test(void) {
  SHOW_ME;
  const int SZ = 15;
  char* s1 = malloc(SZ);
  memset(s1, 'a', SZ);
  s1[SZ - 1] = 0;
  char* s2 = malloc(SZ);
  memset(s2, 'a', SZ);
  s2[SZ - 1] = 0;
  fprintf(stderr, "strcmp: %d\n", strcmp(s1, s2));
  s2[10] = 'b';
  fprintf(stderr, "strcmp: %d\n", strcmp(s1, s2));
  s2[8] = 0;
  fprintf(stderr, "strcmp: %d\n", strcmp(s1, s2));
  free(s1);
  free(s2);
}

NOINLINE
void calloc_realloc_test(void) {
  char* p = calloc(1, 50);
  p[10] = 10;
  free(p);
  p = calloc(1, 60);
  p = realloc(p, 70);
  p[65] = 65;
  free(p);
}

/* run all tests */
int main(void) {
  if (!RUNNING_ON_VALGRIND) {
    /* Don't run this test w/o valgrind. It would fail otherwise. */
    return 0;
  }
  /* Use poor man's gtest_filter. */
  if (1) leak_test();
  if (1) oob_read_test();
  if (1) oob_write_test();
  if (1) umr_heap_test();
  if (1) umr_stack_test();
  if (1) use_after_free_test();
  if (1) use_after_free_with_reuse_test();
  if (1) test_client_requests();
  if (1) test_backtrace3();
  if (1) test_printf();
  if (1) function_wrapping_test();
  if (1) many_args_wrapping_test();
  if (1) function_alignment_wrapping_test();
  if (1) strcmp_test();
  if (1) calloc_realloc_test();
  SHOW_ME;
  return 0;
}