]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-alloc-util.c
Merge pull request #16271 from yuwata/network-cleanups-around-link-get
[thirdparty/systemd.git] / src / test / test-alloc-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <malloc.h>
4 #include <stdint.h>
5
6 #include "alloc-util.h"
7 #include "macro.h"
8 #include "memory-util.h"
9 #include "random-util.h"
10 #include "tests.h"
11
12 static void test_alloca(void) {
13 static const uint8_t zero[997] = { };
14 char *t;
15
16 t = alloca_align(17, 512);
17 assert_se(!((uintptr_t)t & 0xff));
18 memzero(t, 17);
19
20 t = alloca0_align(997, 1024);
21 assert_se(!((uintptr_t)t & 0x1ff));
22 assert_se(!memcmp(t, zero, 997));
23 }
24
25 static void test_GREEDY_REALLOC(void) {
26 _cleanup_free_ int *a = NULL, *b = NULL;
27 size_t n_allocated = 0, i, j;
28
29 /* Give valgrind a chance to verify our realloc() operations */
30
31 for (i = 0; i < 20480; i++) {
32 assert_se(GREEDY_REALLOC(a, n_allocated, i + 1));
33 assert_se(n_allocated >= i + 1);
34 assert_se(malloc_usable_size(a) >= (i + 1) * sizeof(int));
35 a[i] = (int) i;
36 assert_se(GREEDY_REALLOC(a, n_allocated, i / 2));
37 assert_se(n_allocated >= i / 2);
38 assert_se(malloc_usable_size(a) >= (i / 2) * sizeof(int));
39 }
40
41 for (j = 0; j < i / 2; j++)
42 assert_se(a[j] == (int) j);
43
44 for (i = 30, n_allocated = 0; i < 20480; i += 7) {
45 assert_se(GREEDY_REALLOC(b, n_allocated, i + 1));
46 assert_se(n_allocated >= i + 1);
47 assert_se(malloc_usable_size(b) >= (i + 1) * sizeof(int));
48 b[i] = (int) i;
49 assert_se(GREEDY_REALLOC(b, n_allocated, i / 2));
50 assert_se(n_allocated >= i / 2);
51 assert_se(malloc_usable_size(b) >= (i / 2) * sizeof(int));
52 }
53
54 for (j = 30; j < i / 2; j += 7)
55 assert_se(b[j] == (int) j);
56 }
57
58 static void test_memdup_multiply_and_greedy_realloc(void) {
59 static const int org[] = { 1, 2, 3 };
60 _cleanup_free_ int *dup;
61 int *p;
62 size_t i, allocated = 3;
63
64 dup = memdup_suffix0_multiply(org, sizeof(int), 3);
65 assert_se(dup);
66 assert_se(dup[0] == 1);
67 assert_se(dup[1] == 2);
68 assert_se(dup[2] == 3);
69 assert_se(((uint8_t*) dup)[sizeof(int) * 3] == 0);
70 free(dup);
71
72 dup = memdup_multiply(org, sizeof(int), 3);
73 assert_se(dup);
74 assert_se(dup[0] == 1);
75 assert_se(dup[1] == 2);
76 assert_se(dup[2] == 3);
77
78 p = dup;
79 assert_se(greedy_realloc0((void**) &dup, &allocated, 2, sizeof(int)) == p);
80
81 p = (int *) greedy_realloc0((void**) &dup, &allocated, 10, sizeof(int));
82 assert_se(p == dup);
83 assert_se(allocated >= 10);
84 assert_se(p[0] == 1);
85 assert_se(p[1] == 2);
86 assert_se(p[2] == 3);
87 for (i = 3; i < allocated; i++)
88 assert_se(p[i] == 0);
89 }
90
91 static void test_bool_assign(void) {
92 bool b, c, *cp = &c, d, e, f, g, h;
93
94 b = 123;
95 *cp = -11;
96 d = 0xF & 0xFF;
97 e = b & d;
98 f = 0x0;
99 g = cp; /* cast from pointer */
100 h = NULL; /* cast from pointer */
101
102 assert_se(b);
103 assert_se(c);
104 assert_se(d);
105 assert_se(e);
106 assert_se(!f);
107 assert_se(g);
108 assert_se(!h);
109 }
110
111 static int cleanup_counter = 0;
112
113 static void cleanup1(void *a) {
114 log_info("%s(%p)", __func__, a);
115 assert_se(++cleanup_counter == *(int*) a);
116 }
117 static void cleanup2(void *a) {
118 log_info("%s(%p)", __func__, a);
119 assert_se(++cleanup_counter == *(int*) a);
120 }
121 static void cleanup3(void *a) {
122 log_info("%s(%p)", __func__, a);
123 assert_se(++cleanup_counter == *(int*) a);
124 }
125
126 static void test_cleanup_order(void) {
127 _cleanup_(cleanup1) int x1 = 4, x2 = 3;
128 _cleanup_(cleanup3) int z = 2;
129 _cleanup_(cleanup2) int y = 1;
130 log_debug("x1: %p", &x1);
131 log_debug("x2: %p", &x2);
132 log_debug("y: %p", &y);
133 log_debug("z: %p", &z);
134 }
135
136 static void test_auto_erase_memory(void) {
137 _cleanup_(erase_and_freep) uint8_t *p1, *p2;
138
139 assert_se(p1 = new(uint8_t, 1024));
140 assert_se(p2 = new(uint8_t, 1024));
141
142 assert_se(genuine_random_bytes(p1, 1024, RANDOM_BLOCK) == 0);
143
144 /* before we exit the scope, do something with this data, so that the compiler won't optimize this away */
145 memcpy(p2, p1, 1024);
146 for (size_t i = 0; i < 1024; i++)
147 assert_se(p1[i] == p2[i]);
148 }
149
150 int main(int argc, char *argv[]) {
151 test_setup_logging(LOG_DEBUG);
152
153 test_alloca();
154 test_GREEDY_REALLOC();
155 test_memdup_multiply_and_greedy_realloc();
156 test_bool_assign();
157 test_cleanup_order();
158 test_auto_erase_memory();
159
160 return 0;
161 }