Debian Codegraph @

crt/aws-c-common/tests/alloc_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/common.h>
7
8 #include <aws/common/array_list.h>
9 #include <aws/common/assert.h>
10 #include <aws/common/thread.h>
11 #include <aws/testing/aws_test_harness.h>
12
13 #ifdef __MACH__
14 # include <CoreFoundation/CoreFoundation.h>
15 #endif
16
17 static void *s_test_alloc_acquire(struct aws_allocator *allocator, size_t size) {
18 (void)allocator;
19 return (size > 0) ? malloc(size) : NULL;
20 }
21
22 static void s_test_alloc_release(struct aws_allocator *allocator, void *ptr) {
23 (void)allocator;
24 free(ptr);
25 }
26
27 static void *s_test_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) {
28 (void)allocator;
29 (void)oldsize;
30 /* Realloc should ensure that newsize is never 0 */
31 AWS_FATAL_ASSERT(newsize != 0);
32 return realloc(ptr, newsize);
33 }
34
35 static void *s_test_calloc(struct aws_allocator *allocator, size_t num, size_t size) {
36 (void)allocator;
37 return (num > 0 && size > 0) ? calloc(num, size) : NULL;
38 }
39
40 /**
41 * Check that we correctly protect against
42 * https://wiki.sei.cmu.edu/confluence/display/c/MEM04-C.+Beware+of+zero-length+allocations
43 * For now, can only test the realloc case, because it returns NULL on error
44 * Test the remaining cases once https://github.com/awslabs/aws-c-common/issues/471 is solved
45 */
46 AWS_TEST_CASE(test_alloc_nothing, s_test_alloc_nothing_fn)
47 static int s_test_alloc_nothing_fn(struct aws_allocator *allocator, void *ctx) {
48 (void)allocator;
49 (void)ctx;
50
51 struct aws_allocator test_allocator = {
52 .mem_acquire = s_test_alloc_acquire,
53 .mem_release = s_test_alloc_release,
54 .mem_realloc = s_test_realloc,
55 .mem_calloc = s_test_calloc,
56 };
57
58 /* realloc should handle the case correctly, return null, and free the memory */
59 void *p = aws_mem_acquire(&test_allocator, 12);
60 ASSERT_SUCCESS(aws_mem_realloc(&test_allocator, &p, 12, 0));
61 ASSERT_NULL(p);
62 return 0;
63 }
64
65 /*
66 * Small Block Allocator tests
67 */
68 static int s_sba_alloc_free_once(struct aws_allocator *allocator, void *ctx) {
69 (void)ctx;
70
71 struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
72 void *mem = aws_mem_acquire(sba, 42);
73 ASSERT_NOT_NULL(mem);
74 const size_t allocated = aws_mem_tracer_bytes(allocator);
75 ASSERT_TRUE(allocated > 0);
76 aws_mem_release(sba, mem);
77 aws_small_block_allocator_destroy(sba);
78
79 return 0;
80 }
81 AWS_TEST_CASE(sba_alloc_free_once, s_sba_alloc_free_once)
82
83 #define NUM_TEST_ALLOCS 10000
84 #define NUM_TEST_THREADS 8
85 static int s_sba_random_allocs_and_frees(struct aws_allocator *allocator, void *ctx) {
86 (void)ctx;
87
88 struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
89 srand(42);
90
91 void *allocs[NUM_TEST_ALLOCS];
92 for (size_t count = 0; count < NUM_TEST_ALLOCS; ++count) {
93 size_t size = aws_max_size(rand() % 512, 1);
94 void *alloc = aws_mem_acquire(sba, size);
95 ASSERT_NOT_NULL(alloc);
96 allocs[count] = alloc;
97 }
98
99 for (size_t count = 0; count < NUM_TEST_ALLOCS; ++count) {
100 void *alloc = allocs[count];
101 aws_mem_release(sba, alloc);
102 }
103
104 aws_small_block_allocator_destroy(sba);
105
106 return 0;
107 }
108 AWS_TEST_CASE(sba_random_allocs_and_frees, s_sba_random_allocs_and_frees)
109
110 static int s_sba_random_reallocs(struct aws_allocator *allocator, void *ctx) {
111 (void)ctx;
112
113 struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
114 srand(128);
115
116 void *alloc = NULL;
117 size_t size = 0;
118 for (size_t count = 0; count < NUM_TEST_ALLOCS; ++count) {
119 size_t old_size = size;
120 size = rand() % 4096;
121 ASSERT_SUCCESS(aws_mem_realloc(sba, &alloc, old_size, size));
122 }
123 ASSERT_SUCCESS(aws_mem_realloc(sba, &alloc, size, 0));
124
125 aws_small_block_allocator_destroy(sba);
126
127 return 0;
128 }
129 AWS_TEST_CASE(sba_random_reallocs, s_sba_random_reallocs)
130
131 struct allocator_thread_test_data {
132 struct aws_allocator *test_allocator;
133 uint32_t thread_idx;
134 };
135
136 static void s_threaded_alloc_worker(void *user_data) {
137 struct aws_allocator *test_allocator = ((struct allocator_thread_test_data *)user_data)->test_allocator;
138
139 void *allocs[NUM_TEST_ALLOCS];
140 for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) {
141 size_t size = aws_max_size(rand() % 512, 1);
142 void *alloc = aws_mem_acquire(test_allocator, size);
143 AWS_FATAL_ASSERT(alloc);
144 allocs[count] = alloc;
145 }
146
147 for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) {
148 void *alloc = allocs[count];
149 aws_mem_release(test_allocator, alloc);
150 }
151 }
152
153 static void s_thread_test(
154 struct aws_allocator *allocator,
155 void (*thread_fn)(void *),
156 struct aws_allocator *test_allocator) {
157 const struct aws_thread_options *thread_options = aws_default_thread_options();
158 struct aws_thread threads[NUM_TEST_THREADS];
159 struct allocator_thread_test_data thread_data[NUM_TEST_THREADS];
160 AWS_ZERO_ARRAY(threads);
161 AWS_ZERO_ARRAY(thread_data);
162 for (size_t thread_idx = 0; thread_idx < AWS_ARRAY_SIZE(threads); ++thread_idx) {
163 struct aws_thread *thread = &threads[thread_idx];
164 aws_thread_init(thread, allocator);
165 struct allocator_thread_test_data *data = &thread_data[thread_idx];
166 data->test_allocator = test_allocator;
167 data->thread_idx = (uint32_t)thread_idx;
168 aws_thread_launch(thread, thread_fn, data, thread_options);
169 }
170
171 for (size_t thread_idx = 0; thread_idx < AWS_ARRAY_SIZE(threads); ++thread_idx) {
172 struct aws_thread *thread = &threads[thread_idx];
173 aws_thread_join(thread);
174 }
175 }
176
177 static int s_sba_threaded_allocs_and_frees(struct aws_allocator *allocator, void *ctx) {
178 (void)ctx;
179 srand(96);
180
181 struct aws_allocator *sba = aws_small_block_allocator_new(allocator, true);
182
183 s_thread_test(allocator, s_threaded_alloc_worker, sba);
184
185 aws_small_block_allocator_destroy(sba);
186
187 return 0;
188 }
189 AWS_TEST_CASE(sba_threaded_allocs_and_frees, s_sba_threaded_allocs_and_frees)
190
191 static void s_threaded_realloc_worker(void *user_data) {
192 struct allocator_thread_test_data *thread_data = user_data;
193 struct aws_allocator *test_allocator = thread_data->test_allocator;
194 void *alloc = NULL;
195 size_t size = 0;
196 for (size_t count = 0; count < NUM_TEST_ALLOCS / NUM_TEST_THREADS; ++count) {
197 size_t old_size = size;
198 size = rand() % 1024;
199 if (old_size) {
200 AWS_FATAL_ASSERT(*(uint8_t *)alloc == (uint8_t)thread_data->thread_idx);
201 }
202 AWS_FATAL_ASSERT(0 == aws_mem_realloc(test_allocator, &alloc, old_size, size));
203 /* If there was a value, make sure it's still there */
204 if (old_size && size) {
205 AWS_FATAL_ASSERT(*(uint8_t *)alloc == (uint8_t)thread_data->thread_idx);
206 }
207 if (size) {
208 memset(alloc, (int)thread_data->thread_idx, size);
209 }
210 }
211 AWS_FATAL_ASSERT(0 == aws_mem_realloc(test_allocator, &alloc, size, 0));
212 }
213
214 static int s_sba_threaded_reallocs(struct aws_allocator *allocator, void *ctx) {
215 (void)ctx;
216 srand(12);
217
218 struct aws_allocator *sba = aws_small_block_allocator_new(allocator, true);
219
220 s_thread_test(allocator, s_threaded_realloc_worker, sba);
221
222 aws_small_block_allocator_destroy(sba);
223
224 return 0;
225 }
226 AWS_TEST_CASE(sba_threaded_reallocs, s_sba_threaded_reallocs)
227
228 static int s_sba_churn(struct aws_allocator *allocator, void *ctx) {
229 (void)ctx;
230 srand(9000);
231
232 struct aws_array_list allocs;
233 aws_array_list_init_dynamic(&allocs, allocator, NUM_TEST_ALLOCS, sizeof(void *));
234
235 struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
236
237 size_t alloc_count = 0;
238 while (alloc_count++ < NUM_TEST_ALLOCS * 10) {
239 size_t size = aws_max_size(rand() % (2 * 4096), 1);
240 void *alloc = aws_mem_acquire(sba, size);
241 aws_array_list_push_back(&allocs, &alloc);
242
243 /* randomly free a previous allocation, simulating the real world a bit */
244 if ((rand() % allocs.length) > (allocs.length / 2)) {
245 size_t idx = rand() % allocs.length;
246 aws_array_list_get_at(&allocs, &alloc, idx);
247 aws_array_list_erase(&allocs, idx);
248 aws_mem_release(sba, alloc);
249 }
250 }
251
252 /* free all remaining allocations */
253 for (size_t idx = 0; idx < allocs.length; ++idx) {
254 void *alloc = NULL;
255 aws_array_list_get_at(&allocs, &alloc, idx);
256 aws_mem_release(sba, alloc);
257 }
258
259 aws_array_list_clean_up(&allocs);
260
261 aws_small_block_allocator_destroy(sba);
262
263 return 0;
264 }
265 AWS_TEST_CASE(sba_churn, s_sba_churn)
266
267 static int s_sba_metrics_test(struct aws_allocator *allocator, void *ctx) {
268 (void)ctx;
269
270 struct aws_allocator *sba = aws_small_block_allocator_new(allocator, false);
271
272 size_t expected_active_size = 0;
273 void *allocs[512] = {0};
274 for (int idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
275 size_t size = idx + 1;
276 size_t bin_size = 0;
277 ASSERT_SUCCESS(aws_round_up_to_power_of_two(size, &bin_size));
278 expected_active_size += bin_size;
279 allocs[idx] = aws_mem_acquire(sba, size);
280
281 ASSERT_TRUE(aws_small_block_allocator_bytes_reserved(sba) > aws_small_block_allocator_bytes_active(sba));
282 ASSERT_TRUE(expected_active_size <= aws_small_block_allocator_bytes_active(sba));
283 }
284
285 /*
286 * There are
287 *
288 * 32 allocations of size < 32 # (bin 0)
289 * 32 allocations of 32 < size <= 64 # (bin 1)
290 * 64 allocations of 64 < size <= 128 # (bin 2)
291 * 128 allocations of 128 < size <= 256 # (bin 3)
292 * 256 allocations of 256 < size <= 512 # (bin 4)
293 *
294 * If we let actual_page_size = allocated_page_size - sizeof(page_header), then we expect to have reserved
295 *
296 * (32 + actual_page_size / 32 - 1) / (actual_page_size / 32) # (bin 0)
297 * (32 + actual_page_size / 64 - 1) / (actual_page_size / 64) # (bin 1)
298 * (64 + actual_page_size / 128 - 1) / (actual_page_size / 128) # (bin 2)
299 * (128 + actual_page_size / 256 - 1) / (actual_page_size / 256) # (bin 3)
300 * (256 + actual_page_size / 512 - 1) / (actual_page_size / 512) # (bin 4)
301 *
302 * total pages during the allocations.
303 */
304 size_t actual_page_size = aws_small_block_allocator_page_size_available(sba);
305
306 size_t bin0_pages = (32 + actual_page_size / 32 - 1) / (actual_page_size / 32);
307 size_t bin1_pages = (32 + actual_page_size / 64 - 1) / (actual_page_size / 64);
308 size_t bin2_pages = (64 + actual_page_size / 128 - 1) / (actual_page_size / 128);
309 size_t bin3_pages = (128 + actual_page_size / 256 - 1) / (actual_page_size / 256);
310 size_t bin4_pages = (256 + actual_page_size / 512 - 1) / (actual_page_size / 512);
311 size_t expected_page_count = bin0_pages + bin1_pages + bin2_pages + bin3_pages + bin4_pages;
312 ASSERT_INT_EQUALS(
313 expected_page_count * aws_small_block_allocator_page_size(sba), aws_small_block_allocator_bytes_reserved(sba));
314
315 for (int idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
316 aws_mem_release(sba, allocs[idx]);
317 }
318
319 ASSERT_INT_EQUALS(0, aws_small_block_allocator_bytes_active(sba));
320
321 /* after freeing everything, we should have relinquished all but one page in each bin */
322 ASSERT_INT_EQUALS(5 * aws_small_block_allocator_page_size(sba), aws_small_block_allocator_bytes_reserved(sba));
323
324 aws_small_block_allocator_destroy(sba);
325 return 0;
326 }
327 AWS_TEST_CASE(sba_metrics, s_sba_metrics_test)
328
329 /*
330 * Default allocator tests.
331 */
332 static int s_default_threaded_reallocs(struct aws_allocator *allocator, void *ctx) {
333 (void)ctx;
334 srand(15);
335
336 s_thread_test(allocator, s_threaded_realloc_worker, allocator);
337
338 return 0;
339 }
340 AWS_TEST_CASE(default_threaded_reallocs, s_default_threaded_reallocs)
341
342 static int s_default_threaded_allocs_and_frees(struct aws_allocator *allocator, void *ctx) {
343 (void)ctx;
344 srand(99);
345
346 s_thread_test(allocator, s_threaded_alloc_worker, allocator);
347
348 return 0;
349 }
350 AWS_TEST_CASE(default_threaded_allocs_and_frees, s_default_threaded_allocs_and_frees)
351
352 /*
353 * aligned allocator tests.
354 */
355 static int s_aligned_threaded_reallocs(struct aws_allocator *allocator, void *ctx) {
356 (void)allocator;
357 (void)ctx;
358 srand(15);
359
360 struct aws_allocator *alloc = aws_mem_tracer_new(aws_aligned_allocator(), NULL, AWS_MEMTRACE_STACKS, 8);
361
362 s_thread_test(alloc, s_threaded_realloc_worker, alloc);
363
364 aws_mem_tracer_destroy(alloc);
365
366 return 0;
367 }
368 AWS_TEST_CASE(aligned_threaded_reallocs, s_aligned_threaded_reallocs)
369
370 static int s_aligned_threaded_allocs_and_frees(struct aws_allocator *allocator, void *ctx) {
371 (void)allocator;
372 (void)ctx;
373 srand(99);
374
375 struct aws_allocator *alloc = aws_mem_tracer_new(aws_aligned_allocator(), NULL, AWS_MEMTRACE_STACKS, 8);
376
377 s_thread_test(alloc, s_threaded_alloc_worker, alloc);
378
379 aws_mem_tracer_destroy(alloc);
380
381 return 0;
382 }
383 AWS_TEST_CASE(aligned_threaded_allocs_and_frees, s_aligned_threaded_allocs_and_frees)
384
385 static int s_explicit_aligned_sanitize(struct aws_allocator *allocator, void *ctx) {
386 (void)allocator;
387 (void)ctx;
388
389 struct aws_allocator *aligned_alloc = aws_explicit_aligned_allocator_new(1);
390 ASSERT_NULL(aligned_alloc);
391 ASSERT_UINT_EQUALS(aws_last_error(), AWS_ERROR_INVALID_ARGUMENT);
392
393 aligned_alloc = aws_explicit_aligned_allocator_new(3 * sizeof(void *));
394 ASSERT_NULL(aligned_alloc);
395 ASSERT_UINT_EQUALS(aws_last_error(), AWS_ERROR_INVALID_ARGUMENT);
396
397 aligned_alloc = aws_explicit_aligned_allocator_new(0);
398 ASSERT_NULL(aligned_alloc);
399 ASSERT_UINT_EQUALS(aws_last_error(), AWS_ERROR_INVALID_ARGUMENT);
400
401 size_t aligned_size = 1024;
402 aligned_alloc = aws_explicit_aligned_allocator_new(aligned_size);
403 ASSERT_NOT_NULL(aligned_alloc);
404 void *test = aws_mem_acquire(aligned_alloc, sizeof(void *));
405 ASSERT_TRUE((uintptr_t)test % aligned_size == 0);
406 aws_mem_release(aligned_alloc, test);
407
408 aws_explicit_aligned_allocator_destroy(aligned_alloc);
409
410 return 0;
411 }
412 AWS_TEST_CASE(explicit_aligned_sanitize, s_explicit_aligned_sanitize)
413
414 static int s_explicit_aligned_threaded_reallocs(struct aws_allocator *allocator, void *ctx) {
415 (void)allocator;
416 (void)ctx;
417 srand(15);
418 struct aws_allocator *aligned_alloc = aws_explicit_aligned_allocator_new(512);
419
420 struct aws_allocator *alloc = aws_mem_tracer_new(aligned_alloc, NULL, AWS_MEMTRACE_STACKS, 8);
421
422 s_thread_test(alloc, s_threaded_realloc_worker, alloc);
423
424 aws_mem_tracer_destroy(alloc);
425 aws_explicit_aligned_allocator_destroy(aligned_alloc);
426
427 return 0;
428 }
429 AWS_TEST_CASE(explicit_aligned_threaded_reallocs, s_explicit_aligned_threaded_reallocs)
430
431 static int s_explicit_aligned_threaded_allocs_and_frees(struct aws_allocator *allocator, void *ctx) {
432 (void)allocator;
433 (void)ctx;
434 srand(99);
435 struct aws_allocator *aligned_alloc = aws_explicit_aligned_allocator_new(512);
436
437 struct aws_allocator *alloc = aws_mem_tracer_new(aligned_alloc, NULL, AWS_MEMTRACE_STACKS, 8);
438
439 s_thread_test(alloc, s_threaded_alloc_worker, alloc);
440
441 aws_mem_tracer_destroy(alloc);
442 aws_explicit_aligned_allocator_destroy(aligned_alloc);
443
444 return 0;
445 }
446 AWS_TEST_CASE(explicit_aligned_threaded_allocs_and_frees, s_explicit_aligned_threaded_allocs_and_frees)