1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <assert.h>
5 #include <cmocka.h>
6 /* cmocka < 1.0 didn't support these features we need */
7 #ifndef assert_ptr_equal
8 #define assert_ptr_equal(a, b) \
9     _assert_int_equal(cast_ptr_to_largest_integral_type(a), \
10                       cast_ptr_to_largest_integral_type(b), \
11                       __FILE__, __LINE__)
12 #define CMUnitTest UnitTest
13 #define cmocka_unit_test unit_test
14 #define cmocka_run_group_tests(t, setup, teardown) run_tests(t)
15 #endif
16 
17 
18 extern void mock_assert(const int result, const char* const expression,
19                         const char * const file, const int line);
20 #undef assert
21 #define assert(expression) \
22     mock_assert((int)(expression), #expression, __FILE__, __LINE__);
23 #include "alloc-inl.h"
24 
25 void __wrap_exit(int status);
26 /* remap exit -> assert, then use cmocka's mock_assert
27     (compile with `--wrap=exit`) */
28 extern void exit(int status);
29 extern void __real_exit(int status);
__wrap_exit(int status)30 void __wrap_exit(int status) {
31     (void) status;
32     assert(0);
33 }
34 
35 int __wrap_printf(const char *format, ...);
36 /* ignore all printfs */
37 #undef printf
38 extern int printf(const char *format, ...);
39 //extern int __real_printf(const char *format, ...);
__wrap_printf(const char * format,...)40 int __wrap_printf(const char *format, ...) {
41     (void)format;
42     return 1;
43 }
44 
45 #define VOID_BUF (void **)&buf
46 
create_fake_maybe_grow_of(size_t size)47 static void *create_fake_maybe_grow_of(size_t size) {
48 
49     size += AFL_ALLOC_SIZE_OFFSET;
50 
51     // fake a realloc buf
52 
53     struct afl_alloc_buf *buf = malloc(size);
54     if (!buf) {
55         perror("Could not allocate fake buf");
56         return NULL;
57     }
58     buf->complete_size = size; // The size
59     void *actual_buf = (void *)(buf->buf);
60     return actual_buf;
61 
62 }
63 
64 /*
65 static int setup(void **state) {
66 
67     return 0;
68 
69 }
70 */
71 
test_pow2(void ** state)72 static void test_pow2(void **state) {
73     (void)state;
74 
75     assert_int_equal(next_pow2(64), 64);
76     assert_int_equal(next_pow2(63), 64);
77     assert_int_not_equal(next_pow2(65), 65);
78     assert_int_equal(next_pow2(0x100), 0x100);
79     assert_int_equal(next_pow2(0x180), 0x200);
80     assert_int_equal(next_pow2(108), 0x80);
81     assert_int_equal(next_pow2(0), 0);
82     assert_int_equal(next_pow2(1), 1);
83     assert_int_equal(next_pow2(2), 2);
84     assert_int_equal(next_pow2(3), 4);
85     assert_int_equal(next_pow2(0xFFFFFF), 0x1000000);
86     assert_int_equal(next_pow2(0xFFFFFFF), 0x10000000);
87     assert_int_equal(next_pow2(0xFFFFFF0), 0x10000000);
88     assert_int_equal(next_pow2(SIZE_MAX), 0);
89     assert_int_equal(next_pow2(-1), 0);
90     assert_int_equal(next_pow2(-2), 0);
91 
92 }
93 
test_null_allocs(void ** state)94 static void test_null_allocs(void **state) {
95     (void)state;
96 
97     void *buf = NULL;
98     void *ptr = afl_realloc(VOID_BUF, 100);
99     if (unlikely(!buf)) { PFATAL("alloc"); }
100     size_t size = afl_alloc_bufsize(buf);
101     assert_true(buf == ptr);
102     assert_true(size >= 100);
103     afl_free(ptr);
104 
105 }
106 
test_nonpow2_size(void ** state)107 static void test_nonpow2_size(void **state) {
108     (void)state;
109 
110     char *buf = create_fake_maybe_grow_of(150);
111 
112     buf[140] = '5';
113 
114     char *ptr = afl_realloc(VOID_BUF, 160);
115     if (unlikely(!ptr)) { PFATAL("alloc"); }
116     size_t size = afl_alloc_bufsize(buf);
117     assert_ptr_equal(buf, ptr);
118     assert_true(size >= 160);
119     assert_true(buf[140] == '5');
120     afl_free(ptr);
121 
122 }
123 
test_zero_size(void ** state)124 static void test_zero_size(void **state) {
125     (void)state;
126 
127     char *buf = NULL;
128     size_t size = 0;
129     char *new_buf = afl_realloc(VOID_BUF, 0);
130     assert_non_null(new_buf);
131     assert_ptr_equal(buf, new_buf);
132     afl_free(buf);
133     buf = NULL;
134     size = 0;
135 
136     char *ptr = afl_realloc(VOID_BUF, 100);
137     if (unlikely(!ptr)) { PFATAL("alloc"); }
138     size = afl_alloc_bufsize(buf);
139     assert_non_null(ptr);
140     assert_ptr_equal(buf, ptr);
141     assert_true(size >= 100);
142 
143     afl_free(ptr);
144 
145 }
146 
147 
test_unchanged_size(void ** state)148 static void test_unchanged_size(void **state) {
149     (void)state;
150 
151     // fake a realloc buf
152     void *actual_buf = create_fake_maybe_grow_of(100);
153 
154     void *buf_before = actual_buf;
155     void *buf_after = afl_realloc(&actual_buf, 100);
156     if (unlikely(!buf_after)) { PFATAL("alloc"); }
157     assert_ptr_equal(actual_buf, buf_after);
158     assert_ptr_equal(buf_after, buf_before);
159     afl_free(buf_after);
160 
161 }
162 
test_grow_multiple(void ** state)163 static void test_grow_multiple(void **state) {
164     (void)state;
165 
166     char *buf = NULL;
167     size_t size = 0;
168 
169     char *ptr = afl_realloc(VOID_BUF, 100);
170     if (unlikely(!ptr)) { PFATAL("alloc"); }
171     size = afl_alloc_bufsize(ptr);
172     assert_ptr_equal(ptr, buf);
173     assert_true(size >= 100);
174     assert_int_equal(size, next_pow2(size) - AFL_ALLOC_SIZE_OFFSET);
175     buf[50] = '5';
176 
177     ptr = (char *)afl_realloc(VOID_BUF, 1000);
178     if (unlikely(!ptr)) { PFATAL("alloc"); }
179     size = afl_alloc_bufsize(ptr);
180     assert_ptr_equal(ptr, buf);
181     assert_true(size >= 100);
182     assert_int_equal(size, next_pow2(size) - AFL_ALLOC_SIZE_OFFSET);
183     buf[500] = '5';
184 
185     ptr = (char *)afl_realloc(VOID_BUF, 10000);
186     if (unlikely(!ptr)) { PFATAL("alloc"); }
187     size = afl_alloc_bufsize(ptr);
188     assert_ptr_equal(ptr, buf);
189     assert_true(size >= 10000);
190     assert_int_equal(size, next_pow2(size) - AFL_ALLOC_SIZE_OFFSET);
191     buf[5000] = '5';
192 
193     assert_int_equal(buf[50], '5');
194     assert_int_equal(buf[500], '5');
195     assert_int_equal(buf[5000], '5');
196 
197     afl_free(buf);
198 
199 }
200 
201 /*
202 static int teardown(void **state) {
203 
204     return 0;
205 
206 }
207 */
208 
main(int argc,char ** argv)209 int main(int argc, char **argv) {
210     (void)argc;
211     (void)argv;
212 
213 	const struct CMUnitTest tests[] = {
214 		cmocka_unit_test(test_pow2),
215 		cmocka_unit_test(test_null_allocs),
216 		cmocka_unit_test(test_nonpow2_size),
217 		cmocka_unit_test(test_zero_size),
218         cmocka_unit_test(test_unchanged_size),
219         cmocka_unit_test(test_grow_multiple),
220 	};
221 
222     //return cmocka_run_group_tests (tests, setup, teardown);
223     __real_exit( cmocka_run_group_tests (tests, NULL, NULL) );
224 
225     // fake return for dumb compilers
226     return 0;
227 }
228