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

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

mercurial