1 #include "test/jemalloc_test.h"
2
3 #define CHUNK 0x400000
4 #define MAXALIGN (((size_t)1) << 23)
5
6 /*
7 * On systems which can't merge extents, tests that call this function generate
8 * a lot of dirty memory very quickly. Purging between cycles mitigates
9 * potential OOM on e.g. 32-bit Windows.
10 */
11 static void
purge(void)12 purge(void)
13 {
14
15 assert_d_eq(mallctl("arena.0.purge", NULL, NULL, NULL, 0), 0,
16 "Unexpected mallctl error");
17 }
18
TEST_BEGIN(test_alignment_errors)19 TEST_BEGIN(test_alignment_errors)
20 {
21 size_t alignment;
22 void *p;
23
24 for (alignment = 0; alignment < sizeof(void *); alignment++) {
25 assert_d_eq(posix_memalign(&p, alignment, 1), EINVAL,
26 "Expected error for invalid alignment %zu",
27 alignment);
28 }
29
30 for (alignment = sizeof(size_t); alignment < MAXALIGN;
31 alignment <<= 1) {
32 assert_d_ne(posix_memalign(&p, alignment + 1, 1), 0,
33 "Expected error for invalid alignment %zu",
34 alignment + 1);
35 }
36 }
37 TEST_END
38
TEST_BEGIN(test_oom_errors)39 TEST_BEGIN(test_oom_errors)
40 {
41 size_t alignment, size;
42 void *p;
43
44 #if LG_SIZEOF_PTR == 3
45 alignment = UINT64_C(0x8000000000000000);
46 size = UINT64_C(0x8000000000000000);
47 #else
48 alignment = 0x80000000LU;
49 size = 0x80000000LU;
50 #endif
51 assert_d_ne(posix_memalign(&p, alignment, size), 0,
52 "Expected error for posix_memalign(&p, %zu, %zu)",
53 alignment, size);
54
55 #if LG_SIZEOF_PTR == 3
56 alignment = UINT64_C(0x4000000000000000);
57 size = UINT64_C(0xc000000000000001);
58 #else
59 alignment = 0x40000000LU;
60 size = 0xc0000001LU;
61 #endif
62 assert_d_ne(posix_memalign(&p, alignment, size), 0,
63 "Expected error for posix_memalign(&p, %zu, %zu)",
64 alignment, size);
65
66 alignment = 0x10LU;
67 #if LG_SIZEOF_PTR == 3
68 size = UINT64_C(0xfffffffffffffff0);
69 #else
70 size = 0xfffffff0LU;
71 #endif
72 assert_d_ne(posix_memalign(&p, alignment, size), 0,
73 "Expected error for posix_memalign(&p, %zu, %zu)",
74 alignment, size);
75 }
76 TEST_END
77
TEST_BEGIN(test_alignment_and_size)78 TEST_BEGIN(test_alignment_and_size)
79 {
80 #define NITER 4
81 size_t alignment, size, total;
82 unsigned i;
83 int err;
84 void *ps[NITER];
85
86 for (i = 0; i < NITER; i++)
87 ps[i] = NULL;
88
89 for (alignment = 8;
90 alignment <= MAXALIGN;
91 alignment <<= 1) {
92 total = 0;
93 for (size = 1;
94 size < 3 * alignment && size < (1U << 31);
95 size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
96 for (i = 0; i < NITER; i++) {
97 err = posix_memalign(&ps[i],
98 alignment, size);
99 if (err) {
100 char buf[BUFERROR_BUF];
101
102 buferror(get_errno(), buf, sizeof(buf));
103 test_fail(
104 "Error for alignment=%zu, "
105 "size=%zu (%#zx): %s",
106 alignment, size, size, buf);
107 }
108 total += malloc_usable_size(ps[i]);
109 if (total >= (MAXALIGN << 1))
110 break;
111 }
112 for (i = 0; i < NITER; i++) {
113 if (ps[i] != NULL) {
114 free(ps[i]);
115 ps[i] = NULL;
116 }
117 }
118 }
119 purge();
120 }
121 #undef NITER
122 }
123 TEST_END
124
125 int
main(void)126 main(void)
127 {
128
129 return (test(
130 test_alignment_errors,
131 test_oom_errors,
132 test_alignment_and_size));
133 }
134