zero.c revision ab8c79fdafd6d1ee722c1277ef32c14c6e0c9dd3
1#include "test/jemalloc_test.h"
2
3#ifdef JEMALLOC_FILL
4const char *malloc_conf =
5    "abort:false,junk:false,zero:true,redzone:false,quarantine:0";
6#endif
7
8static void
9test_zero(size_t sz_min, size_t sz_max)
10{
11	char *s;
12	size_t sz_prev, sz, i;
13
14	sz_prev = 0;
15	s = (char *)mallocx(sz_min, 0);
16	assert_ptr_not_null((void *)s, "Unexpected mallocx() failure");
17
18	for (sz = sallocx(s, 0); sz <= sz_max;
19	    sz_prev = sz, sz = sallocx(s, 0)) {
20		if (sz_prev > 0) {
21			assert_c_eq(s[0], 'a',
22			    "Previously allocated byte %zu/%zu is corrupted",
23			    ZU(0), sz_prev);
24			assert_c_eq(s[sz_prev-1], 'a',
25			    "Previously allocated byte %zu/%zu is corrupted",
26			    sz_prev-1, sz_prev);
27		}
28
29		for (i = sz_prev; i < sz; i++) {
30			assert_c_eq(s[i], 0x0,
31			    "Newly allocated byte %zu/%zu isn't zero-filled",
32			    i, sz);
33			s[i] = 'a';
34		}
35
36		if (xallocx(s, sz+1, 0, 0) == sz) {
37			s = (char *)rallocx(s, sz+1, 0);
38			assert_ptr_not_null((void *)s,
39			    "Unexpected rallocx() failure");
40		}
41	}
42
43	dallocx(s, 0);
44}
45
46TEST_BEGIN(test_zero_small)
47{
48
49	test_skip_if(!config_fill);
50	test_zero(1, SMALL_MAXCLASS-1);
51}
52TEST_END
53
54TEST_BEGIN(test_zero_large)
55{
56
57	test_skip_if(!config_fill);
58	test_zero(SMALL_MAXCLASS+1, arena_maxclass);
59}
60TEST_END
61
62TEST_BEGIN(test_zero_huge)
63{
64
65	test_skip_if(!config_fill);
66	test_zero(arena_maxclass+1, chunksize*2);
67}
68TEST_END
69
70int
71main(void)
72{
73
74	return (test(
75	    test_zero_small,
76	    test_zero_large,
77	    test_zero_huge));
78}
79