tests/util_allocator.c

Mon, 11 Nov 2024 21:42:14 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 11 Nov 2024 21:42:14 +0100
changeset 986
38fa7e41194c
parent 985
68754c7de906
permissions
-rw-r--r--

simplify cx_array_copy() - fixes #474

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

mercurial