forked from newrelic/newrelic-php-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_memory.c
431 lines (353 loc) · 11.8 KB
/
test_memory.c
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include "nr_axiom.h"
#include <errno.h>
#include <limits.h>
#include "util_memory.h"
#include "util_strings.h"
#include "tlib_main.h"
/*
* nr_malloc (0) must return a valid pointer.
*/
static void test_malloc_valid(void) {
char* rp;
rp = (char*)nr_malloc(0);
tlib_pass_if_true("nr_malloc (0) returns a pointer", 0 != rp, "rp=%p", rp);
nr_free(rp);
}
/*
* nr_calloc(0,x) or nr_calloc (x,0) returns a valid pointer.
*/
static void test_calloc_0_valid(void) {
char* rp;
rp = (char*)nr_calloc(0, 10);
tlib_pass_if_true("nr_calloc (0, 10) returns a pointer", 0 != rp, "rp=%p",
rp);
nr_free(rp);
rp = (char*)nr_calloc(10, 0);
tlib_pass_if_true("nr_calloc (10, 0) returns a pointer", 0 != rp, "rp=%p",
rp);
nr_free(rp);
}
/*
* nr_realloc (NULL, x) returns a valid pointer, as does
* nr_realloc (ptr, 0) and nr_realloc (0, 0).
*/
static void test_calloc_null_valid(void) {
char* rp;
rp = (char*)nr_realloc(0, 10);
tlib_pass_if_true("nr_realloc (0, 10) returns a pointer", 0 != rp, "rp=%p",
rp);
nr_free(rp);
rp = (char*)nr_malloc(10);
tlib_pass_if_true("nr_malloc (10) for nr_realloc returns a pointer", 0 != rp,
"rp=%p", rp);
rp = (char*)nr_realloc(rp, 0);
tlib_pass_if_true("nr_realloc (ptr, 0) returns a pointer", 0 != rp, "rp=%p",
rp);
nr_free(rp);
rp = (char*)nr_realloc(0, 0);
tlib_pass_if_true("nr_realloc (0, 0) returns a pointer", 0 != rp, "rp=%p",
rp);
nr_free(rp);
}
/*
* So that valgrind will report invalid reads and writes, these functions will
* do something to every byte in the given memory block.
*/
static void read_all_bytes(void* ptr, size_t size) {
uint8_t* buf = (uint8_t*)ptr;
size_t i;
for (i = 0; i < size; i++) {
uint8_t expected = i % UINT8_MAX;
tlib_pass_if_uint8_t_equal("read check", expected, buf[i]);
}
}
static void poke_all_bytes(void* ptr, size_t size) {
uint8_t* buf = (uint8_t*)ptr;
size_t i;
for (i = 0; i < size; i++) {
buf[i] = i % UINT8_MAX;
}
read_all_bytes(ptr, size);
}
static void test_reallocarray(void) {
char* op;
char* rp;
/*
* reallocarray(NULL, nmemb, size) is equivalent to calloc(nmemb, size) for
* all values of nmemb and size.
*/
op = (char*)nr_reallocarray(NULL, 0, 0);
tlib_pass_if_not_null("nr_reallocarray(NULL, 0, 0)", op);
nr_free(op);
op = (char*)nr_reallocarray(NULL, 10, 10);
tlib_pass_if_not_null("nr_reallocarray(NULL, 10, 10)", op);
poke_all_bytes(op, 10 * 10);
nr_free(op);
/*
* Let's check our overflow tests. Note that gcc is actually pretty good at
* the static compile time assertions controlled by the NRMALLOC and
* NRCALLOCSZ macros, so we have to use variables to provide the values.
*/
{
size_t size_max = SIZE_MAX;
tlib_pass_if_null("overflow nmemb", nr_reallocarray(NULL, size_max, 2));
tlib_pass_if_int_equal("overflow nmemb errno", ENOMEM, errno);
errno = 0;
tlib_pass_if_null("overflow size", nr_reallocarray(NULL, 2, size_max));
tlib_pass_if_int_equal("overflow nmemb errno", ENOMEM, errno);
errno = 0;
}
/*
* Now try actually reallocating to a larger size. Firstly, we'll try an
* initially zero length allocation.
*/
op = (char*)nr_reallocarray(NULL, 0, 0);
tlib_pass_if_not_null("nr_reallocarray(NULL, 0, 0)", op);
rp = (char*)nr_reallocarray(op, 10, 10);
tlib_pass_if_not_null("nr_reallocarray(op, 10, 10)", rp);
poke_all_bytes(rp, 10 * 10);
nr_free(rp);
/*
* Now a non-zero allocation.
*/
op = (char*)nr_reallocarray(NULL, 5, 5);
tlib_pass_if_not_null("nr_reallocarray(NULL, 5, 5)", op);
poke_all_bytes(op, 5 * 5);
rp = (char*)nr_reallocarray(op, 10, 10);
tlib_pass_if_not_null("nr_reallocarray(op, 10, 10)", rp);
read_all_bytes(rp, 5 * 5);
poke_all_bytes(rp, 10 * 10);
nr_free(rp);
/*
* Now we'll reallocate to a smaller size.
*/
op = (char*)nr_reallocarray(NULL, 10, 10);
tlib_pass_if_not_null("nr_reallocarray(NULL, 10, 10)", op);
poke_all_bytes(op, 10 * 10);
rp = (char*)nr_reallocarray(op, 5, 5);
tlib_pass_if_not_null("nr_reallocarray(op, 5, 5)", rp);
read_all_bytes(rp, 5 * 5);
poke_all_bytes(rp, 5 * 5);
nr_free(rp);
/*
* Finally, reallocarray(ptr, 0, 0) is equivalent to free(ptr), so let's do
* that without a corresponding free() and ensure that Valgrind doesn't
* complain.
*/
op = (char*)nr_reallocarray(NULL, 10, 10);
tlib_pass_if_not_null("nr_reallocarray(NULL, 10, 10)", op);
rp = (char*)nr_reallocarray(op, 0, 0);
tlib_pass_if_null("nr_reallocarray(op, 0, 0)", rp);
}
/*
* test that free also sets the pointer to NULL, and that
* calling free on a NULL pointer just returns.
*/
static void test_free_side_effect(void) {
char* rp;
rp = (char*)nr_malloc(16);
tlib_pass_if_true("nr_malloc (16) for free tests", 0 != rp, "rp=%p", rp);
nr_free(rp);
tlib_pass_if_true("freed pointer is NULL", 0 == rp, "rp=%p", rp);
rp = 0;
nr_free(rp);
tlib_pass_if_true("nr_free (0) does not crash", 0 == rp, "rp=%p", rp);
}
/*
* test that string duplication works.
*/
static void test_strdup(void) {
char* rp;
rp = nr_strdup("abc");
tlib_pass_if_true("simple nr_strdup",
(0 != rp) && (0 == nr_strcmp(rp, "abc")), "rp=%p rp='%s'",
rp, rp ? rp : "");
nr_free(rp);
rp = nr_strdup("");
tlib_pass_if_true("nr_strdup of empty string",
(0 != rp) && (0 == nr_strcmp(rp, "")), "rp=%p rp='%s'", rp,
rp ? rp : "");
nr_free(rp);
rp = nr_strdup(0);
tlib_pass_if_true("nr_strdup of NULL string",
(0 != rp) && (0 == nr_strcmp(rp, "")), "rp=%p rp='%s'", rp,
rp ? rp : "");
nr_free(rp);
}
/*
* test that string duplication with default fallback works.
*/
static void test_strdup_or(void) {
char* rp;
rp = nr_strdup_or("abc", "default");
tlib_pass_if_true("simple nr_strdup_or",
(0 != rp) && (0 == nr_strcmp(rp, "abc")), "rp=%p rp='%s'",
rp, rp ? rp : "");
nr_free(rp);
rp = nr_strdup_or("", "default");
tlib_pass_if_true("nr_strdup_or of empty string",
(0 != rp) && (0 == nr_strcmp(rp, "")), "rp=%p rp='%s'", rp,
rp ? rp : "");
nr_free(rp);
rp = nr_strdup_or(0, "default");
tlib_pass_if_true("nr_strdup_or of NULL string",
(0 != rp) && (0 == nr_strcmp(rp, "default")),
"rp=%p rp='%s'", rp, rp ? rp : "");
nr_free(rp);
rp = nr_strdup_or(0, 0);
tlib_pass_if_true("nr_strdup_or of NULL string with NULL backup",
(0 != rp) && (0 == nr_strcmp(rp, "")), "rp=%p rp='%s'", rp,
rp ? rp : "");
nr_free(rp);
rp = nr_strdup_or("abc", 0);
tlib_pass_if_true("nr_strdup_or of string with NULL backup",
(0 != rp) && (0 == nr_strcmp(rp, "abc")), "rp=%p rp='%s'",
rp, rp ? rp : "");
nr_free(rp);
}
/*
* test that nr_strndup() works.
*/
static void test_strndup(void) {
char* rp;
rp = nr_strndup("abc", 3);
tlib_pass_if_true("simple nr_strndup",
(0 != rp) && (0 == nr_strcmp(rp, "abc")), "rp=%p rp='%s'",
rp, rp ? rp : "");
nr_free(rp);
rp = nr_strndup("", 16);
tlib_pass_if_true("nr_strndup of empty string",
(0 != rp) && (0 == nr_strcmp(rp, "")), "rp=%p rp='%s'", rp,
rp ? rp : rp);
nr_free(rp);
rp = nr_strndup(0, 16);
tlib_pass_if_true("nr_strndup of NULL string",
(0 != rp) && (0 == nr_strcmp(rp, "")), "rp=%p rp='%s'", rp,
rp ? rp : "");
nr_free(rp);
rp = nr_strndup("abcdef", 4);
tlib_pass_if_true("nr_strndup of longer string",
(0 != rp) && (0 == nr_strcmp(rp, "abcd")), "rp=%p rp='%s'",
rp, rp ? rp : "");
nr_free(rp);
}
static void test_memcpy(void) {
void* as;
void* bs;
void* dest;
void* retval;
int len;
len = 64;
as = nr_malloc(len);
bs = nr_malloc(len);
dest = nr_malloc(len);
nr_memset(as, 0xaa, len);
nr_memset(bs, 0xbb, len);
tlib_pass_if_null("memcpy to NULL dest", nr_memcpy(NULL, as, len));
retval = nr_memcpy(dest, as, len);
tlib_pass_if_ptr_equal("memcpy returns dest", dest, retval);
tlib_pass_if_bytes_equal("memcpy copies src", dest, len, as, len);
retval = nr_memcpy(dest, NULL, len);
tlib_pass_if_ptr_equal("memcpy from NULL src", dest, retval);
tlib_pass_if_bytes_equal("memcpy copies src", dest, len, as, len);
retval = nr_memcpy(dest, bs, 0);
tlib_pass_if_ptr_equal("memcpy from NULL src", dest, retval);
tlib_pass_if_bytes_equal("memcpy zero size doesn't modify dest", dest, len,
as, len);
nr_free(as);
nr_free(bs);
nr_free(dest);
}
static void test_memmove(void) {
void* as;
void* bs;
void* dest;
void* retval;
int len;
len = 64;
as = nr_malloc(len);
bs = nr_malloc(len);
dest = nr_malloc(len);
nr_memset(as, 0xaa, len);
nr_memset(bs, 0xbb, len);
tlib_pass_if_null("memmove to NULL dest", nr_memmove(NULL, as, len));
retval = nr_memmove(dest, as, len);
tlib_pass_if_ptr_equal("memmove returns dest", dest, retval);
tlib_pass_if_bytes_equal("memmove copies src", dest, len, as, len);
retval = nr_memmove(dest, NULL, len);
tlib_pass_if_ptr_equal("memmove from NULL src", dest, retval);
tlib_pass_if_bytes_equal("memmove copies src", dest, len, as, len);
retval = nr_memmove(dest, bs, 0);
tlib_pass_if_ptr_equal("memmove from NULL src", dest, retval);
tlib_pass_if_bytes_equal("memmove zero size doesn't modify dest", dest, len,
as, len);
nr_free(as);
nr_free(bs);
nr_free(dest);
}
static void test_memcmp(void) {
int rv;
/*
* Test zero-length comparisons.
*/
rv = nr_memcmp(NULL, NULL, 0);
tlib_pass_if_int_equal("nr_memcmp(NULL, NULL, 0)", 0, rv);
rv = nr_memcmp("", NULL, 0);
tlib_pass_if_int_equal("nr_memcmp(\"\", NULL, 0)", 0, rv);
rv = nr_memcmp(NULL, "", 0);
tlib_pass_if_int_equal("nr_memcmp(NULL, \"\", 0)", 0, rv);
rv = nr_memcmp("", "", 0);
tlib_pass_if_int_equal("nr_memcmp(\"\", \"\", 0)", 0, rv);
rv = nr_memcmp("a", "b", 0);
tlib_pass_if_int_equal("nr_memcmp(\"aB\", \"ab\", 0)", 0, rv);
/*
* Test positive lengths.
*/
rv = nr_memcmp(NULL, NULL, 1);
tlib_pass_if_int_equal("nr_memcmp(NULL, NULL, 1)", 0, rv);
rv = nr_memcmp("", NULL, 1);
tlib_pass_if_true("nr_memcmp(\"\", NULL, 1)", rv > 0, "rv=%d", rv);
rv = nr_memcmp(NULL, "", 1);
tlib_pass_if_true("nr_memcmp(NULL, \"\", 1)", rv < 0, "rv=%d", rv);
rv = nr_memcmp("a", "a", 1);
tlib_pass_if_int_equal("nr_memcmp(\"a\", \"a\", 1)", 0, rv);
rv = nr_memcmp("a", "b", 1);
tlib_pass_if_true("nr_memcmp(\"a\", \"b\", 2)", rv < 0, "rv=%d", rv);
rv = nr_memcmp("b", "a", 1);
tlib_pass_if_true("nr_memcmp(\"b\", \"a\", 2)", rv > 0, "rv=%d", rv);
}
static void test_memchr(void) {
char buf[] = "abc";
void* rv;
rv = nr_memchr(NULL, 'a', 0);
tlib_pass_if_null("null buffer", rv);
rv = nr_memchr(buf, 'd', 0);
tlib_pass_if_null("zero length and value not present", rv);
rv = nr_memchr(buf, 'a', 0);
tlib_pass_if_null("zero length and value present", rv);
rv = nr_memchr(buf, 'b', 1);
tlib_pass_if_null("value not present", rv);
rv = nr_memchr(buf, 'a', 1);
tlib_pass_if_ptr_equal("value present at buf[0]", buf, rv);
rv = nr_memchr(buf, 'c', 4);
tlib_pass_if_ptr_equal("value present at buf[2]", &buf[2], rv);
}
void test_main(void* p NRUNUSED) {
test_malloc_valid();
test_calloc_0_valid();
test_calloc_null_valid();
test_reallocarray();
test_free_side_effect();
test_strdup();
test_strdup_or();
test_strndup();
test_memcpy();
test_memmove();
test_memcmp();
test_memchr();
}
tlib_parallel_info_t parallel_info = {.suggested_nthreads = 4, .state_size = 0};