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