| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <malloc.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "memory-util.h" |
| 7 |
#include "random-util.h" |
| 8 |
#include "tests.h" |
| 9 |
|
| 10 |
TEST(alloca) { |
| 11 |
static const uint8_t zero[997] = { }; |
| 12 |
char *t; |
| 13 |
|
| 14 |
t = alloca_align(17, 512); |
| 15 |
assert_se(!((uintptr_t)t & 0xff)); |
| 16 |
memzero(t, 17); |
| 17 |
|
| 18 |
t = alloca0_align(997, 1024); |
| 19 |
assert_se(!((uintptr_t)t & 0x1ff)); |
| 20 |
assert_se(!memcmp(t, zero, 997)); |
| 21 |
} |
| 22 |
|
| 23 |
TEST(GREEDY_REALLOC) { |
| 24 |
_cleanup_free_ int *a = NULL, *b = NULL, *c = NULL; |
| 25 |
size_t i, j, n_c = 0; |
| 26 |
|
| 27 |
/* Give valgrind a chance to verify our realloc() operations */ |
| 28 |
|
| 29 |
for (i = 0; i < 20480; i++) { |
| 30 |
assert_se(GREEDY_REALLOC(a, i + 1)); |
| 31 |
assert_se(MALLOC_ELEMENTSOF(a) >= i + 1); |
| 32 |
assert_se(MALLOC_SIZEOF_SAFE(a) >= (i + 1) * sizeof(int)); |
| 33 |
a[i] = (int) i; |
| 34 |
assert_se(GREEDY_REALLOC(a, i / 2)); |
| 35 |
assert_se(MALLOC_ELEMENTSOF(a) >= i / 2); |
| 36 |
assert_se(MALLOC_SIZEOF_SAFE(a) >= (i / 2) * sizeof(int)); |
| 37 |
} |
| 38 |
|
| 39 |
for (j = 0; j < i / 2; j++) |
| 40 |
assert_se(a[j] == (int) j); |
| 41 |
|
| 42 |
for (i = 30; i < 20480; i += 7) { |
| 43 |
assert_se(GREEDY_REALLOC(b, i + 1)); |
| 44 |
assert_se(MALLOC_ELEMENTSOF(b) >= i + 1); |
| 45 |
assert_se(MALLOC_SIZEOF_SAFE(b) >= (i + 1) * sizeof(int)); |
| 46 |
b[i] = (int) i; |
| 47 |
assert_se(GREEDY_REALLOC(b, i / 2)); |
| 48 |
assert_se(MALLOC_ELEMENTSOF(b) >= i / 2); |
| 49 |
assert_se(MALLOC_SIZEOF_SAFE(b) >= (i / 2) * sizeof(int)); |
| 50 |
} |
| 51 |
|
| 52 |
for (j = 30; j < i / 2; j += 7) |
| 53 |
assert_se(b[j] == (int) j); |
| 54 |
|
| 55 |
size_t n_from = 10; |
| 56 |
int from[n_from]; |
| 57 |
for (i = 0; i < 2048; i++) { |
| 58 |
for (j = 0; j < n_from; j++) |
| 59 |
from[j] = n_from * i + j; |
| 60 |
|
| 61 |
_cleanup_free_ int *before = NULL; |
| 62 |
size_t n_before = 0; |
| 63 |
assert_se(GREEDY_REALLOC_APPEND(before, n_before, c, n_c)); |
| 64 |
assert_se(before); |
| 65 |
assert_se(n_before == n_c); |
| 66 |
assert_se(memcmp_safe(c, before, n_c) == 0); |
| 67 |
|
| 68 |
assert_se(GREEDY_REALLOC_APPEND(c, n_c, from, n_from)); |
| 69 |
assert_se(n_c == n_before + n_from); |
| 70 |
assert_se(MALLOC_ELEMENTSOF(c) >= n_c); |
| 71 |
assert_se(MALLOC_SIZEOF_SAFE(c) >= n_c * sizeof(int)); |
| 72 |
assert_se(memcmp_safe(c, before, n_before) == 0); |
| 73 |
assert_se(memcmp_safe(&c[n_before], from, n_from) == 0); |
| 74 |
|
| 75 |
before = mfree(before); |
| 76 |
assert_se(!before); |
| 77 |
n_before = 0; |
| 78 |
assert_se(GREEDY_REALLOC_APPEND(before, n_before, c, n_c)); |
| 79 |
assert_se(before); |
| 80 |
assert_se(n_before == n_c); |
| 81 |
assert_se(memcmp_safe(c, before, n_c) == 0); |
| 82 |
|
| 83 |
assert_se(GREEDY_REALLOC_APPEND(c, n_c, NULL, 0)); |
| 84 |
assert_se(c); |
| 85 |
assert_se(n_c == n_before); |
| 86 |
assert_se(MALLOC_ELEMENTSOF(c) >= n_c); |
| 87 |
assert_se(MALLOC_SIZEOF_SAFE(c) >= n_c * sizeof(int)); |
| 88 |
assert_se(memcmp_safe(c, before, n_c) == 0); |
| 89 |
} |
| 90 |
|
| 91 |
for (j = 0; j < i * n_from; j++) |
| 92 |
assert_se(c[j] == (int) j); |
| 93 |
} |
| 94 |
|
| 95 |
TEST(memdup_multiply_and_greedy_realloc) { |
| 96 |
static const int org[] = { 1, 2, 3 }; |
| 97 |
_cleanup_free_ int *dup; |
| 98 |
size_t i; |
| 99 |
int *p; |
| 100 |
|
| 101 |
dup = memdup_suffix0_multiply(org, 3, sizeof(int)); |
| 102 |
assert_se(dup); |
| 103 |
assert_se(dup[0] == 1); |
| 104 |
assert_se(dup[1] == 2); |
| 105 |
assert_se(dup[2] == 3); |
| 106 |
assert_se(((uint8_t*) dup)[sizeof(int) * 3] == 0); |
| 107 |
free(dup); |
| 108 |
|
| 109 |
dup = memdup_multiply(org, 3, sizeof(int)); |
| 110 |
assert_se(dup); |
| 111 |
assert_se(dup[0] == 1); |
| 112 |
assert_se(dup[1] == 2); |
| 113 |
assert_se(dup[2] == 3); |
| 114 |
|
| 115 |
memzero(dup + 3, malloc_usable_size(dup) - sizeof(int) * 3); |
| 116 |
|
| 117 |
p = dup; |
| 118 |
assert_se(GREEDY_REALLOC0(dup, 2) == p); |
| 119 |
|
| 120 |
p = GREEDY_REALLOC0(dup, 10); |
| 121 |
assert_se(p == dup); |
| 122 |
assert_se(MALLOC_ELEMENTSOF(p) >= 10); |
| 123 |
assert_se(p[0] == 1); |
| 124 |
assert_se(p[1] == 2); |
| 125 |
assert_se(p[2] == 3); |
| 126 |
for (i = 3; i < MALLOC_ELEMENTSOF(p); i++) |
| 127 |
assert_se(p[i] == 0); |
| 128 |
} |
| 129 |
|
| 130 |
TEST(bool_assign) { |
| 131 |
bool b, c, *cp = &c, d, e, f, g, h; |
| 132 |
|
| 133 |
b = 123; |
| 134 |
*cp = -11; |
| 135 |
d = 0xF & 0xFF; |
| 136 |
e = b & d; |
| 137 |
f = 0x0; |
| 138 |
g = cp; /* cast from pointer */ |
| 139 |
h = NULL; /* cast from pointer */ |
| 140 |
|
| 141 |
assert_se(b); |
| 142 |
assert_se(c); |
| 143 |
assert_se(d); |
| 144 |
assert_se(e); |
| 145 |
assert_se(!f); |
| 146 |
assert_se(g); |
| 147 |
assert_se(!h); |
| 148 |
} |
| 149 |
|
| 150 |
static int cleanup_counter = 0; |
| 151 |
|
| 152 |
static void cleanup1(void *a) { |
| 153 |
log_info("%s(%p)", __func__, a); |
| 154 |
assert_se(++cleanup_counter == *(int*) a); |
| 155 |
} |
| 156 |
static void cleanup2(void *a) { |
| 157 |
log_info("%s(%p)", __func__, a); |
| 158 |
assert_se(++cleanup_counter == *(int*) a); |
| 159 |
} |
| 160 |
static void cleanup3(void *a) { |
| 161 |
log_info("%s(%p)", __func__, a); |
| 162 |
assert_se(++cleanup_counter == *(int*) a); |
| 163 |
} |
| 164 |
|
| 165 |
TEST(cleanup_order) { |
| 166 |
_cleanup_(cleanup1) int x1 = 4, x2 = 3; |
| 167 |
_cleanup_(cleanup3) int z = 2; |
| 168 |
_cleanup_(cleanup2) int y = 1; |
| 169 |
log_debug("x1: %p", &x1); |
| 170 |
log_debug("x2: %p", &x2); |
| 171 |
log_debug("y: %p", &y); |
| 172 |
log_debug("z: %p", &z); |
| 173 |
} |
| 174 |
|
| 175 |
TEST(auto_erase_memory) { |
| 176 |
_cleanup_(erase_and_freep) uint8_t *p1, *p2; |
| 177 |
|
| 178 |
/* print address of p2, else e.g. clang-11 will optimize it out */ |
| 179 |
log_debug("p1: %p p2: %p", &p1, &p2); |
| 180 |
|
| 181 |
assert_se(p1 = new(uint8_t, 4703)); /* use prime size, to ensure that there will be free space at the |
| 182 |
* end of the allocation, since malloc() enforces alignment */ |
| 183 |
assert_se(p2 = new(uint8_t, 4703)); |
| 184 |
|
| 185 |
assert_se(crypto_random_bytes(p1, 4703) == 0); |
| 186 |
|
| 187 |
/* before we exit the scope, do something with this data, so that the compiler won't optimize this away */ |
| 188 |
memcpy(p2, p1, 4703); |
| 189 |
for (size_t i = 0; i < 4703; i++) |
| 190 |
assert_se(p1[i] == p2[i]); |
| 191 |
} |
| 192 |
|
| 193 |
#define TEST_SIZES(f, n) \ |
| 194 |
do { \ |
| 195 |
log_debug("requested=%zu vs. malloc_size=%zu vs. gcc_size=%zu", \ |
| 196 |
n * sizeof(*f), \ |
| 197 |
malloc_usable_size(f), \ |
| 198 |
__builtin_object_size(f, 0)); \ |
| 199 |
assert_se(MALLOC_ELEMENTSOF(f) >= n); \ |
| 200 |
assert_se(MALLOC_SIZEOF_SAFE(f) >= sizeof(*f) * n); \ |
| 201 |
assert_se(malloc_usable_size(f) >= sizeof(*f) * n); \ |
| 202 |
assert_se(__builtin_object_size(f, 0) >= sizeof(*f) * n); \ |
| 203 |
} while (false) |
| 204 |
|
| 205 |
TEST(malloc_size_safe) { |
| 206 |
_cleanup_free_ uint32_t *f = NULL; |
| 207 |
size_t n = 4711; |
| 208 |
|
| 209 |
/* Let's check the macros and built-ins work on NULL and return the expected values */ |
| 210 |
assert_se(MALLOC_ELEMENTSOF((float*) NULL) == 0); |
| 211 |
assert_se(MALLOC_SIZEOF_SAFE((float*) NULL) == 0); |
| 212 |
assert_se(malloc_usable_size(NULL) == 0); /* as per man page, this is safe and defined */ |
| 213 |
assert_se(__builtin_object_size(NULL, 0) == SIZE_MAX); /* as per docs SIZE_MAX is returned for pointers where the size isn't known */ |
| 214 |
|
| 215 |
/* Then, let's try these macros once with constant size values, so that __builtin_object_size() |
| 216 |
* definitely can work (as long as -O2 is used when compiling) */ |
| 217 |
assert_se(f = new(uint32_t, n)); |
| 218 |
TEST_SIZES(f, n); |
| 219 |
|
| 220 |
/* Finally, let's use some dynamically sized allocations, to make sure this doesn't deteriorate */ |
| 221 |
for (unsigned i = 0; i < 50; i++) { |
| 222 |
_cleanup_free_ uint64_t *g = NULL; |
| 223 |
size_t m; |
| 224 |
|
| 225 |
m = random_u64_range(16*1024); |
| 226 |
assert_se(g = new(uint64_t, m)); |
| 227 |
TEST_SIZES(g, m); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
DEFINE_TEST_MAIN(LOG_DEBUG); |