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