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 alignment = 0;
25 set_errno(0);
26 p = aligned_alloc(alignment, 1);
27 assert_false(p != NULL || get_errno() != EINVAL,
28 "Expected error for invalid alignment %zu", alignment);
29
30 for (alignment = sizeof(size_t); alignment < MAXALIGN;
31 alignment <<= 1) {
32 set_errno(0);
33 p = aligned_alloc(alignment + 1, 1);
34 assert_false(p != NULL || get_errno() != EINVAL,
35 "Expected error for invalid alignment %zu",
36 alignment + 1);
37 }
38 }
39 TEST_END
40
TEST_BEGIN(test_oom_errors)41 TEST_BEGIN(test_oom_errors)
42 {
43 size_t alignment, size;
44 void *p;
45
46 #if LG_SIZEOF_PTR == 3
47 alignment = UINT64_C(0x8000000000000000);
48 size = UINT64_C(0x8000000000000000);
49 #else
50 alignment = 0x80000000LU;
51 size = 0x80000000LU;
52 #endif
53 set_errno(0);
54 p = aligned_alloc(alignment, size);
55 assert_false(p != NULL || get_errno() != ENOMEM,
56 "Expected error for aligned_alloc(%zu, %zu)",
57 alignment, size);
58
59 #if LG_SIZEOF_PTR == 3
60 alignment = UINT64_C(0x4000000000000000);
61 size = UINT64_C(0xc000000000000001);
62 #else
63 alignment = 0x40000000LU;
64 size = 0xc0000001LU;
65 #endif
66 set_errno(0);
67 p = aligned_alloc(alignment, size);
68 assert_false(p != NULL || get_errno() != ENOMEM,
69 "Expected error for aligned_alloc(%zu, %zu)",
70 alignment, size);
71
72 alignment = 0x10LU;
73 #if LG_SIZEOF_PTR == 3
74 size = UINT64_C(0xfffffffffffffff0);
75 #else
76 size = 0xfffffff0LU;
77 #endif
78 set_errno(0);
79 p = aligned_alloc(alignment, size);
80 assert_false(p != NULL || get_errno() != ENOMEM,
81 "Expected error for aligned_alloc(&p, %zu, %zu)",
82 alignment, size);
83 }
84 TEST_END
85
TEST_BEGIN(test_alignment_and_size)86 TEST_BEGIN(test_alignment_and_size)
87 {
88 #define NITER 4
89 size_t alignment, size, total;
90 unsigned i;
91 void *ps[NITER];
92
93 for (i = 0; i < NITER; i++)
94 ps[i] = NULL;
95
96 for (alignment = 8;
97 alignment <= MAXALIGN;
98 alignment <<= 1) {
99 total = 0;
100 for (size = 1;
101 size < 3 * alignment && size < (1U << 31);
102 size += (alignment >> (LG_SIZEOF_PTR-1)) - 1) {
103 for (i = 0; i < NITER; i++) {
104 ps[i] = aligned_alloc(alignment, size);
105 if (ps[i] == NULL) {
106 char buf[BUFERROR_BUF];
107
108 buferror(get_errno(), buf, sizeof(buf));
109 test_fail(
110 "Error for alignment=%zu, "
111 "size=%zu (%#zx): %s",
112 alignment, size, size, buf);
113 }
114 total += malloc_usable_size(ps[i]);
115 if (total >= (MAXALIGN << 1))
116 break;
117 }
118 for (i = 0; i < NITER; i++) {
119 if (ps[i] != NULL) {
120 free(ps[i]);
121 ps[i] = NULL;
122 }
123 }
124 }
125 purge();
126 }
127 #undef NITER
128 }
129 TEST_END
130
131 int
main(void)132 main(void)
133 {
134
135 return (test(
136 test_alignment_errors,
137 test_oom_errors,
138 test_alignment_and_size));
139 }
140