tests/util_allocator.c

Wed, 27 Dec 2023 17:28:33 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Dec 2023 17:28:33 +0100
changeset 775
d3f451440eef
parent 770
ed710122af44
child 814
5f9e07d3dd6c
permissions
-rw-r--r--

ignore the intentional use-after-free

universe@422 1 /*
universe@422 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@422 3 *
universe@422 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@422 5 *
universe@422 6 * Redistribution and use in source and binary forms, with or without
universe@422 7 * modification, are permitted provided that the following conditions are met:
universe@422 8 *
universe@422 9 * 1. Redistributions of source code must retain the above copyright
universe@422 10 * notice, this list of conditions and the following disclaimer.
universe@422 11 *
universe@422 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@422 13 * notice, this list of conditions and the following disclaimer in the
universe@422 14 * documentation and/or other materials provided with the distribution.
universe@422 15 *
universe@422 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@422 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@422 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@422 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@422 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@422 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@422 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@422 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@422 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@422 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@422 26 * POSSIBILITY OF SUCH DAMAGE.
universe@422 27 */
universe@422 28
universe@422 29 #include "util_allocator.h"
universe@770 30 #include "cx/test.h"
universe@422 31
universe@770 32 static void cx_testing_allocator_track(CxTestingAllocator *alloc, void *ptr) {
universe@770 33 for (size_t i = 0; i < alloc->tracked_count; i++) {
universe@770 34 if (alloc->tracked[i] == ptr) return; // is already tracked
universe@770 35 }
universe@770 36
universe@770 37 if (alloc->tracked_count == alloc->tracked_capacity) {
universe@770 38 size_t newcapa = alloc->tracked_capacity + 64;
universe@770 39 void *newarr = realloc(alloc->tracked, newcapa * sizeof(void *));
universe@770 40 if (newarr == NULL) abort();
universe@770 41 alloc->tracked = newarr;
universe@770 42 alloc->tracked_capacity = newcapa;
universe@770 43 }
universe@770 44
universe@770 45 alloc->tracked[alloc->tracked_count] = ptr;
universe@770 46 alloc->tracked_count++;
universe@770 47 }
universe@770 48
universe@770 49 static bool cx_testing_allocator_untrack(CxTestingAllocator *alloc, void *ptr) {
universe@770 50 for (size_t i = 0; i < alloc->tracked_count; i++) {
universe@770 51 if (alloc->tracked[i] == ptr) {
universe@770 52 size_t last = alloc->tracked_count - 1;
universe@770 53 if (i < last) {
universe@770 54 alloc->tracked[i] = alloc->tracked[last];
universe@770 55 }
universe@770 56 alloc->tracked_count--;
universe@770 57 return true;
universe@770 58 }
universe@770 59 }
universe@770 60 return false;
universe@770 61 }
universe@770 62
universe@770 63 static void *cx_malloc_testing(void *d, size_t n) {
universe@770 64 CxTestingAllocator *data = d;
universe@422 65 void *ptr = malloc(n);
universe@422 66 data->alloc_total++;
universe@770 67 if (ptr == NULL) {
universe@422 68 data->alloc_failed++;
universe@422 69 } else {
universe@770 70 cx_testing_allocator_track(data, ptr);
universe@422 71 }
universe@422 72 return ptr;
universe@422 73 }
universe@422 74
universe@770 75 static void *cx_realloc_testing(void *d, void *mem, size_t n) {
universe@770 76 CxTestingAllocator *data = d;
universe@422 77 void *ptr = realloc(mem, n);
universe@422 78 if (ptr == mem) {
universe@422 79 return ptr;
universe@422 80 } else {
universe@422 81 data->alloc_total++;
universe@770 82 if (ptr == NULL) {
universe@422 83 data->alloc_failed++;
universe@422 84 } else {
universe@422 85 data->free_total++;
universe@775 86 #pragma GCC diagnostic push
universe@775 87 #pragma GCC diagnostic ignored "-Wuse-after-free"
universe@770 88 if (!cx_testing_allocator_untrack(data, mem)) {
universe@422 89 data->free_failed++;
universe@422 90 }
universe@775 91 #pragma GCC diagnostic pop
universe@770 92 cx_testing_allocator_track(data, ptr);
universe@422 93 }
universe@422 94 return ptr;
universe@422 95 }
universe@422 96 }
universe@422 97
universe@770 98 static void *cx_calloc_testing(void *d, size_t nelem, size_t n) {
universe@770 99 CxTestingAllocator *data = d;
universe@422 100 void *ptr = calloc(nelem, n);
universe@422 101 data->alloc_total++;
universe@770 102 if (ptr == NULL) {
universe@422 103 data->alloc_failed++;
universe@422 104 } else {
universe@770 105 cx_testing_allocator_track(data, ptr);
universe@422 106 }
universe@422 107 return ptr;
universe@422 108 }
universe@422 109
universe@770 110 static void cx_free_testing(void *d, void *mem) {
universe@770 111 CxTestingAllocator *data = d;
universe@422 112 data->free_total++;
universe@770 113 if (cx_testing_allocator_untrack(data, mem)) {
universe@770 114 free(mem);
universe@770 115 } else {
universe@422 116 data->free_failed++;
universe@422 117 // do not even attempt to free mem, because it is likely to segfault
universe@422 118 }
universe@422 119 }
universe@422 120
universe@422 121 cx_allocator_class cx_testing_allocator_class = {
universe@422 122 cx_malloc_testing,
universe@422 123 cx_realloc_testing,
universe@422 124 cx_calloc_testing,
universe@422 125 cx_free_testing
universe@422 126 };
universe@422 127
universe@770 128
universe@770 129 void cx_testing_allocator_init(CxTestingAllocator *alloc) {
universe@770 130 alloc->base.cl = &cx_testing_allocator_class;
universe@770 131 alloc->base.data = alloc;
universe@770 132 alloc->alloc_failed = 0;
universe@770 133 alloc->alloc_total = 0;
universe@770 134 alloc->free_failed = 0;
universe@770 135 alloc->free_total = 0;
universe@770 136 size_t initial_capa = 16;
universe@770 137 alloc->tracked_capacity = initial_capa;
universe@770 138 alloc->tracked_count = 0;
universe@770 139 alloc->tracked = calloc(sizeof(void *), initial_capa);
universe@422 140 }
universe@422 141
universe@770 142 void cx_testing_allocator_destroy(CxTestingAllocator *alloc) {
universe@770 143 free(alloc->tracked);
universe@571 144 }
universe@571 145
universe@770 146 bool cx_testing_allocator_used(CxTestingAllocator const *alloc) {
universe@770 147 return alloc->alloc_total > 0;
universe@770 148 }
universe@770 149
universe@770 150 bool cx_testing_allocator_verify(CxTestingAllocator const *alloc) {
universe@770 151 return alloc->tracked_count == 0 && alloc->alloc_failed == 0 && alloc->free_failed == 0
universe@770 152 && alloc->alloc_total == alloc->free_total;
universe@422 153 }
universe@518 154
universe@518 155 // SELF-TEST
universe@518 156
universe@770 157 CX_TEST(test_util_allocator_expect_free) {
universe@770 158 CxTestingAllocator talloc;
universe@770 159 cx_testing_allocator_init(&talloc);
universe@770 160 CxAllocator *alloc = &talloc.base;
universe@770 161 CX_TEST_DO {
universe@770 162 CX_TEST_ASSERTM(cx_testing_allocator_verify(&talloc),
universe@770 163 "Fresh testing allocator fails to verify.");
universe@770 164 CX_TEST_ASSERTM(!cx_testing_allocator_used(&talloc),
universe@770 165 "Fresh testing allocator already used.");
universe@770 166 void *ptr = cxMalloc(alloc, 16);
universe@770 167 CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc),
universe@770 168 "Testing allocator verifies with unfreed memory.");
universe@770 169 CX_TEST_ASSERT(cx_testing_allocator_used(&talloc));
universe@770 170 CX_TEST_ASSERT(ptr != NULL);
universe@518 171
universe@770 172 cxFree(alloc, ptr);
universe@770 173 CX_TEST_ASSERTM(cx_testing_allocator_verify(&talloc),
universe@770 174 "Testing allocator fails to verify after everything freed.");
universe@770 175 }
universe@770 176 cx_testing_allocator_destroy(&talloc);
universe@518 177 }
universe@518 178
universe@770 179 CX_TEST(test_util_allocator_detect_double_free) {
universe@770 180 CxTestingAllocator talloc;
universe@770 181 cx_testing_allocator_init(&talloc);
universe@770 182 CxAllocator *alloc = &talloc.base;
universe@770 183 CX_TEST_DO {
universe@770 184 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 185 void *ptr = cxMalloc(alloc, 16);
universe@770 186 CX_TEST_ASSERT(ptr != NULL);
universe@770 187 cxFree(alloc, ptr);
universe@770 188 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 189 cxFree(alloc, ptr);
universe@770 190 CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc),
universe@770 191 "Testing allocator does not detect double-free.");
universe@770 192 }
universe@770 193 cx_testing_allocator_destroy(&talloc);
universe@518 194 }
universe@518 195
universe@770 196 CX_TEST(test_util_allocator_free_untracked) {
universe@770 197 CxTestingAllocator talloc;
universe@770 198 cx_testing_allocator_init(&talloc);
universe@770 199 CxAllocator *alloc = &talloc.base;
universe@770 200 void *ptr = malloc(16);
universe@770 201 CX_TEST_DO {
universe@770 202 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 203 cxFree(alloc, ptr);
universe@770 204 CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc),
universe@770 205 "Testing allocator does not detect free of untracked memory.");
universe@770 206 }
universe@770 207 free(ptr);
universe@770 208 cx_testing_allocator_destroy(&talloc);
universe@518 209 }
universe@518 210
universe@770 211 CX_TEST(test_util_allocator_full_lifecycle_with_realloc) {
universe@770 212 CxTestingAllocator talloc;
universe@770 213 cx_testing_allocator_init(&talloc);
universe@770 214 CxAllocator *alloc = &talloc.base;
universe@770 215 CX_TEST_DO {
universe@770 216 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 217 void *ptr = cxMalloc(alloc, 16);
universe@770 218 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
universe@770 219 CX_TEST_ASSERT(ptr != NULL);
universe@770 220 CX_TEST_ASSERT(talloc.tracked_count == 1);
universe@770 221 ptr = cxRealloc(alloc, ptr, 256);
universe@770 222 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
universe@770 223 CX_TEST_ASSERT(ptr != NULL);
universe@770 224 CX_TEST_ASSERT(talloc.tracked_count == 1);
universe@770 225 cxFree(alloc, ptr);
universe@770 226 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 227 }
universe@770 228 cx_testing_allocator_destroy(&talloc);
universe@518 229 }
universe@518 230
universe@770 231 CX_TEST(test_util_allocator_calloc_initializes) {
universe@770 232 CxTestingAllocator talloc;
universe@770 233 cx_testing_allocator_init(&talloc);
universe@770 234 CxAllocator *alloc = &talloc.base;
universe@770 235 CX_TEST_DO {
universe@770 236 const char zeros[16] = {0};
universe@770 237 void *ptr = cxCalloc(alloc, 16, 1);
universe@770 238 CX_TEST_ASSERT(memcmp(ptr, zeros, 16) == 0);
universe@770 239 cxFree(alloc, ptr);
universe@770 240 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 241 }
universe@770 242 cx_testing_allocator_destroy(&talloc);
universe@518 243 }
universe@770 244
universe@770 245 CxTestSuite *cx_test_suite_testing_allocator(void) {
universe@770 246 CxTestSuite *suite = cx_test_suite_new("testing allocator self-test");
universe@770 247
universe@770 248 cx_test_register(suite, test_util_allocator_expect_free);
universe@770 249 cx_test_register(suite, test_util_allocator_detect_double_free);
universe@770 250 cx_test_register(suite, test_util_allocator_free_untracked);
universe@770 251 cx_test_register(suite, test_util_allocator_full_lifecycle_with_realloc);
universe@770 252 cx_test_register(suite, test_util_allocator_calloc_initializes);
universe@770 253
universe@770 254 return suite;
universe@770 255 }
universe@770 256

mercurial