• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	for (alignment = 0; alignment < sizeof(void *); alignment++) {
21 		assert_d_eq(posix_memalign(&p, alignment, 1), EINVAL,
22 		    "Expected error for invalid alignment %zu",
23 		    alignment);
24 	}
25 
26 	for (alignment = sizeof(size_t); alignment < MAXALIGN;
27 	    alignment <<= 1) {
28 		assert_d_ne(posix_memalign(&p, alignment + 1, 1), 0,
29 		    "Expected error for invalid alignment %zu",
30 		    alignment + 1);
31 	}
32 }
33 TEST_END
34 
TEST_BEGIN(test_oom_errors)35 TEST_BEGIN(test_oom_errors) {
36 	size_t alignment, size;
37 	void *p;
38 
39 #if LG_SIZEOF_PTR == 3
40 	alignment = UINT64_C(0x8000000000000000);
41 	size      = UINT64_C(0x8000000000000000);
42 #else
43 	alignment = 0x80000000LU;
44 	size      = 0x80000000LU;
45 #endif
46 	assert_d_ne(posix_memalign(&p, alignment, size), 0,
47 	    "Expected error for posix_memalign(&p, %zu, %zu)",
48 	    alignment, size);
49 
50 #if LG_SIZEOF_PTR == 3
51 	alignment = UINT64_C(0x4000000000000000);
52 	size      = UINT64_C(0xc000000000000001);
53 #else
54 	alignment = 0x40000000LU;
55 	size      = 0xc0000001LU;
56 #endif
57 	assert_d_ne(posix_memalign(&p, alignment, size), 0,
58 	    "Expected error for posix_memalign(&p, %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 	assert_d_ne(posix_memalign(&p, alignment, size), 0,
68 	    "Expected error for posix_memalign(&p, %zu, %zu)",
69 	    alignment, size);
70 }
71 TEST_END
72 
TEST_BEGIN(test_alignment_and_size)73 TEST_BEGIN(test_alignment_and_size) {
74 #define NITER 4
75 	size_t alignment, size, total;
76 	unsigned i;
77 	int err;
78 	void *ps[NITER];
79 
80 	for (i = 0; i < NITER; i++) {
81 		ps[i] = NULL;
82 	}
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 				err = posix_memalign(&ps[i],
93 				    alignment, size);
94 				if (err) {
95 					char buf[BUFERROR_BUF];
96 
97 					buferror(get_errno(), buf, sizeof(buf));
98 					test_fail(
99 					    "Error for alignment=%zu, "
100 					    "size=%zu (%#zx): %s",
101 					    alignment, size, size, buf);
102 				}
103 				total += malloc_usable_size(ps[i]);
104 				if (total >= (MAXALIGN << 1)) {
105 					break;
106 				}
107 			}
108 			for (i = 0; i < NITER; i++) {
109 				if (ps[i] != NULL) {
110 					free(ps[i]);
111 					ps[i] = NULL;
112 				}
113 			}
114 		}
115 		purge();
116 	}
117 #undef NITER
118 }
119 TEST_END
120 
121 int
main(void)122 main(void) {
123 	return test(
124 	    test_alignment_errors,
125 	    test_oom_errors,
126 	    test_alignment_and_size);
127 }
128