1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tests/util_allocator.c Wed Dec 27 16:04:38 2023 +0100 1.3 @@ -0,0 +1,253 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + */ 1.31 + 1.32 +#include "util_allocator.h" 1.33 +#include "cx/test.h" 1.34 + 1.35 +static void cx_testing_allocator_track(CxTestingAllocator *alloc, void *ptr) { 1.36 + for (size_t i = 0; i < alloc->tracked_count; i++) { 1.37 + if (alloc->tracked[i] == ptr) return; // is already tracked 1.38 + } 1.39 + 1.40 + if (alloc->tracked_count == alloc->tracked_capacity) { 1.41 + size_t newcapa = alloc->tracked_capacity + 64; 1.42 + void *newarr = realloc(alloc->tracked, newcapa * sizeof(void *)); 1.43 + if (newarr == NULL) abort(); 1.44 + alloc->tracked = newarr; 1.45 + alloc->tracked_capacity = newcapa; 1.46 + } 1.47 + 1.48 + alloc->tracked[alloc->tracked_count] = ptr; 1.49 + alloc->tracked_count++; 1.50 +} 1.51 + 1.52 +static bool cx_testing_allocator_untrack(CxTestingAllocator *alloc, void *ptr) { 1.53 + for (size_t i = 0; i < alloc->tracked_count; i++) { 1.54 + if (alloc->tracked[i] == ptr) { 1.55 + size_t last = alloc->tracked_count - 1; 1.56 + if (i < last) { 1.57 + alloc->tracked[i] = alloc->tracked[last]; 1.58 + } 1.59 + alloc->tracked_count--; 1.60 + return true; 1.61 + } 1.62 + } 1.63 + return false; 1.64 +} 1.65 + 1.66 +static void *cx_malloc_testing(void *d, size_t n) { 1.67 + CxTestingAllocator *data = d; 1.68 + void *ptr = malloc(n); 1.69 + data->alloc_total++; 1.70 + if (ptr == NULL) { 1.71 + data->alloc_failed++; 1.72 + } else { 1.73 + cx_testing_allocator_track(data, ptr); 1.74 + } 1.75 + return ptr; 1.76 +} 1.77 + 1.78 +static void *cx_realloc_testing(void *d, void *mem, size_t n) { 1.79 + CxTestingAllocator *data = d; 1.80 + void *ptr = realloc(mem, n); 1.81 + if (ptr == mem) { 1.82 + return ptr; 1.83 + } else { 1.84 + data->alloc_total++; 1.85 + if (ptr == NULL) { 1.86 + data->alloc_failed++; 1.87 + } else { 1.88 + data->free_total++; 1.89 + if (!cx_testing_allocator_untrack(data, mem)) { 1.90 + data->free_failed++; 1.91 + } 1.92 + cx_testing_allocator_track(data, ptr); 1.93 + } 1.94 + return ptr; 1.95 + } 1.96 +} 1.97 + 1.98 +static void *cx_calloc_testing(void *d, size_t nelem, size_t n) { 1.99 + CxTestingAllocator *data = d; 1.100 + void *ptr = calloc(nelem, n); 1.101 + data->alloc_total++; 1.102 + if (ptr == NULL) { 1.103 + data->alloc_failed++; 1.104 + } else { 1.105 + cx_testing_allocator_track(data, ptr); 1.106 + } 1.107 + return ptr; 1.108 +} 1.109 + 1.110 +static void cx_free_testing(void *d, void *mem) { 1.111 + CxTestingAllocator *data = d; 1.112 + data->free_total++; 1.113 + if (cx_testing_allocator_untrack(data, mem)) { 1.114 + free(mem); 1.115 + } else { 1.116 + data->free_failed++; 1.117 + // do not even attempt to free mem, because it is likely to segfault 1.118 + } 1.119 +} 1.120 + 1.121 +cx_allocator_class cx_testing_allocator_class = { 1.122 + cx_malloc_testing, 1.123 + cx_realloc_testing, 1.124 + cx_calloc_testing, 1.125 + cx_free_testing 1.126 +}; 1.127 + 1.128 + 1.129 +void cx_testing_allocator_init(CxTestingAllocator *alloc) { 1.130 + alloc->base.cl = &cx_testing_allocator_class; 1.131 + alloc->base.data = alloc; 1.132 + alloc->alloc_failed = 0; 1.133 + alloc->alloc_total = 0; 1.134 + alloc->free_failed = 0; 1.135 + alloc->free_total = 0; 1.136 + size_t initial_capa = 16; 1.137 + alloc->tracked_capacity = initial_capa; 1.138 + alloc->tracked_count = 0; 1.139 + alloc->tracked = calloc(sizeof(void *), initial_capa); 1.140 +} 1.141 + 1.142 +void cx_testing_allocator_destroy(CxTestingAllocator *alloc) { 1.143 + free(alloc->tracked); 1.144 +} 1.145 + 1.146 +bool cx_testing_allocator_used(CxTestingAllocator const *alloc) { 1.147 + return alloc->alloc_total > 0; 1.148 +} 1.149 + 1.150 +bool cx_testing_allocator_verify(CxTestingAllocator const *alloc) { 1.151 + return alloc->tracked_count == 0 && alloc->alloc_failed == 0 && alloc->free_failed == 0 1.152 + && alloc->alloc_total == alloc->free_total; 1.153 +} 1.154 + 1.155 +// SELF-TEST 1.156 + 1.157 +CX_TEST(test_util_allocator_expect_free) { 1.158 + CxTestingAllocator talloc; 1.159 + cx_testing_allocator_init(&talloc); 1.160 + CxAllocator *alloc = &talloc.base; 1.161 + CX_TEST_DO { 1.162 + CX_TEST_ASSERTM(cx_testing_allocator_verify(&talloc), 1.163 + "Fresh testing allocator fails to verify."); 1.164 + CX_TEST_ASSERTM(!cx_testing_allocator_used(&talloc), 1.165 + "Fresh testing allocator already used."); 1.166 + void *ptr = cxMalloc(alloc, 16); 1.167 + CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc), 1.168 + "Testing allocator verifies with unfreed memory."); 1.169 + CX_TEST_ASSERT(cx_testing_allocator_used(&talloc)); 1.170 + CX_TEST_ASSERT(ptr != NULL); 1.171 + 1.172 + cxFree(alloc, ptr); 1.173 + CX_TEST_ASSERTM(cx_testing_allocator_verify(&talloc), 1.174 + "Testing allocator fails to verify after everything freed."); 1.175 + } 1.176 + cx_testing_allocator_destroy(&talloc); 1.177 +} 1.178 + 1.179 +CX_TEST(test_util_allocator_detect_double_free) { 1.180 + CxTestingAllocator talloc; 1.181 + cx_testing_allocator_init(&talloc); 1.182 + CxAllocator *alloc = &talloc.base; 1.183 + CX_TEST_DO { 1.184 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.185 + void *ptr = cxMalloc(alloc, 16); 1.186 + CX_TEST_ASSERT(ptr != NULL); 1.187 + cxFree(alloc, ptr); 1.188 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.189 + cxFree(alloc, ptr); 1.190 + CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc), 1.191 + "Testing allocator does not detect double-free."); 1.192 + } 1.193 + cx_testing_allocator_destroy(&talloc); 1.194 +} 1.195 + 1.196 +CX_TEST(test_util_allocator_free_untracked) { 1.197 + CxTestingAllocator talloc; 1.198 + cx_testing_allocator_init(&talloc); 1.199 + CxAllocator *alloc = &talloc.base; 1.200 + void *ptr = malloc(16); 1.201 + CX_TEST_DO { 1.202 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.203 + cxFree(alloc, ptr); 1.204 + CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc), 1.205 + "Testing allocator does not detect free of untracked memory."); 1.206 + } 1.207 + free(ptr); 1.208 + cx_testing_allocator_destroy(&talloc); 1.209 +} 1.210 + 1.211 +CX_TEST(test_util_allocator_full_lifecycle_with_realloc) { 1.212 + CxTestingAllocator talloc; 1.213 + cx_testing_allocator_init(&talloc); 1.214 + CxAllocator *alloc = &talloc.base; 1.215 + CX_TEST_DO { 1.216 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.217 + void *ptr = cxMalloc(alloc, 16); 1.218 + CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1.219 + CX_TEST_ASSERT(ptr != NULL); 1.220 + CX_TEST_ASSERT(talloc.tracked_count == 1); 1.221 + ptr = cxRealloc(alloc, ptr, 256); 1.222 + CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc)); 1.223 + CX_TEST_ASSERT(ptr != NULL); 1.224 + CX_TEST_ASSERT(talloc.tracked_count == 1); 1.225 + cxFree(alloc, ptr); 1.226 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.227 + } 1.228 + cx_testing_allocator_destroy(&talloc); 1.229 +} 1.230 + 1.231 +CX_TEST(test_util_allocator_calloc_initializes) { 1.232 + CxTestingAllocator talloc; 1.233 + cx_testing_allocator_init(&talloc); 1.234 + CxAllocator *alloc = &talloc.base; 1.235 + CX_TEST_DO { 1.236 + const char zeros[16] = {0}; 1.237 + void *ptr = cxCalloc(alloc, 16, 1); 1.238 + CX_TEST_ASSERT(memcmp(ptr, zeros, 16) == 0); 1.239 + cxFree(alloc, ptr); 1.240 + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1.241 + } 1.242 + cx_testing_allocator_destroy(&talloc); 1.243 +} 1.244 + 1.245 +CxTestSuite *cx_test_suite_testing_allocator(void) { 1.246 + CxTestSuite *suite = cx_test_suite_new("testing allocator self-test"); 1.247 + 1.248 + cx_test_register(suite, test_util_allocator_expect_free); 1.249 + cx_test_register(suite, test_util_allocator_detect_double_free); 1.250 + cx_test_register(suite, test_util_allocator_free_untracked); 1.251 + cx_test_register(suite, test_util_allocator_full_lifecycle_with_realloc); 1.252 + cx_test_register(suite, test_util_allocator_calloc_initializes); 1.253 + 1.254 + return suite; 1.255 +} 1.256 +