Debian Codegraph @

crt/aws-c-common/tests/memtrace_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/testing/aws_test_harness.h>
7
8 #include <aws/common/allocator.h>
9 #include <aws/common/device_random.h>
10
11 #include "logging/test_logger.h"
12
13 #define NUM_ALLOCS 100
14 static int s_test_memtrace_count(struct aws_allocator *allocator, void *ctx) {
15 (void)ctx;
16
17 struct aws_allocator *tracer = aws_mem_tracer_new(allocator, NULL, AWS_MEMTRACE_BYTES, 0);
18
19 void *allocs[NUM_ALLOCS] = {0};
20 size_t sizes[NUM_ALLOCS] = {0};
21 size_t total = 0;
22
23 for (size_t idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
24 uint32_t size = 0;
25 aws_device_random_u32(&size);
26 size = (size % 1024) + 1; /* not necessary to allocate a gajillion bytes */
27 allocs[idx] = aws_mem_acquire(tracer, size);
28 sizes[idx] = size;
29 total += size;
30 }
31
32 ASSERT_UINT_EQUALS(total, aws_mem_tracer_bytes(tracer));
33 ASSERT_UINT_EQUALS(NUM_ALLOCS, aws_mem_tracer_count(tracer));
34
35 size_t freed = 0;
36 for (size_t idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
37 uint32_t roll = 0;
38 aws_device_random_u32(&roll);
39 if (roll % 3 == 0) {
40 aws_mem_release(tracer, allocs[idx]);
41 allocs[idx] = NULL;
42 total -= sizes[idx];
43 ++freed;
44 }
45 }
46
47 ASSERT_UINT_EQUALS(total, aws_mem_tracer_bytes(tracer));
48 ASSERT_UINT_EQUALS(NUM_ALLOCS - freed, aws_mem_tracer_count(tracer));
49
50 for (size_t idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
51 if (allocs[idx]) {
52 aws_mem_release(tracer, allocs[idx]);
53 }
54 }
55
56 ASSERT_UINT_EQUALS(0, aws_mem_tracer_bytes(tracer));
57 ASSERT_UINT_EQUALS(0, aws_mem_tracer_count(tracer));
58
59 struct aws_allocator *original = aws_mem_tracer_destroy(tracer);
60 ASSERT_PTR_EQUALS(allocator, original);
61
62 return 0;
63 }
64 AWS_TEST_CASE(test_memtrace_count, s_test_memtrace_count)
65
66 /* Disable optimization for tests to make sure those allocation function still exists. */
67 #if defined(__clang__)
68 # pragma clang optimize off
69 #elif defined(__GNUC__) || defined(__GNUG__)
70 # pragma GCC push_options
71 # pragma GCC optimize("O0")
72 #endif
73
74 AWS_NO_INLINE void *s_alloc_1(struct aws_allocator *allocator, size_t size) {
75 return aws_mem_acquire(allocator, size);
76 }
77
78 AWS_NO_INLINE void *s_alloc_2(struct aws_allocator *allocator, size_t size) {
79 return aws_mem_acquire(allocator, size);
80 }
81
82 AWS_NO_INLINE void *s_alloc_3(struct aws_allocator *allocator, size_t size) {
83 return aws_mem_acquire(allocator, size);
84 }
85
86 AWS_NO_INLINE void *s_alloc_4(struct aws_allocator *allocator, size_t size) {
87 return aws_mem_acquire(allocator, size);
88 }
89
90 #if defined(__clang__)
91 # pragma clang optimize on
92 #elif defined(__GNUC__) || defined(__GNUG__)
93 # pragma GCC pop_options
94 #endif
95
96 static struct aws_logger s_test_logger;
97
98 static int s_test_memtrace_stacks(struct aws_allocator *allocator, void *ctx) {
99 (void)ctx;
100
101 /* only bother to run this test if the platform can do a backtrace */
102 void *probe_stack[1];
103 if (!aws_backtrace(probe_stack, 1)) {
104 return AWS_OP_SKIP;
105 }
106
107 test_logger_init(&s_test_logger, allocator, AWS_LL_TRACE, 0);
108 aws_logger_set(&s_test_logger);
109
110 struct aws_allocator *tracer = aws_mem_tracer_new(allocator, NULL, AWS_MEMTRACE_STACKS, 8);
111
112 void *allocs[NUM_ALLOCS] = {0};
113 size_t total = 0;
114
115 for (size_t idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
116 uint32_t size = 0;
117 aws_device_random_u32(&size);
118 size = (size % 1024) + 1; /* not necessary to allocate a gajillion bytes */
119
120 void *(*allocate)(struct aws_allocator *, size_t) = NULL;
121 switch (idx % 4) {
122 case 0:
123 allocate = s_alloc_1;
124 break;
125 case 1:
126 allocate = s_alloc_2;
127 break;
128 case 2:
129 allocate = s_alloc_3;
130 break;
131 case 3:
132 allocate = s_alloc_4;
133 break;
134 }
135
136 allocs[idx] = allocate(tracer, size);
137 total += size;
138 }
139
140 ASSERT_UINT_EQUALS(total, aws_mem_tracer_bytes(tracer));
141 ASSERT_UINT_EQUALS(NUM_ALLOCS, aws_mem_tracer_count(tracer));
142 aws_mem_tracer_dump(tracer);
143
144 /* make sure all of the functions that allocated are found */
145 struct test_logger_impl *test_logger = s_test_logger.p_impl;
146 /* if this is not a debug build, there may not be symbols, so the test cannot
147 * verify if a best effort was made */
148 #if defined(DEBUG_BUILD)
149 fprintf(stderr, "%s\n", test_logger->log_buffer.buffer);
150 char s_alloc_1_addr[32];
151 char s_alloc_2_addr[32];
152 char s_alloc_3_addr[32];
153 char s_alloc_4_addr[32];
154 # if defined(_MSC_VER)
155 # pragma warning(disable : 4054) /* type cast function pointer to data pointer */
156 # endif
157
158 snprintf(s_alloc_1_addr, AWS_ARRAY_SIZE(s_alloc_1_addr), "0x%tx", (uintptr_t)(void *)s_alloc_1);
159 snprintf(s_alloc_2_addr, AWS_ARRAY_SIZE(s_alloc_2_addr), "0x%tx", (uintptr_t)(void *)s_alloc_2);
160 snprintf(s_alloc_3_addr, AWS_ARRAY_SIZE(s_alloc_3_addr), "0x%tx", (uintptr_t)(void *)s_alloc_3);
161 snprintf(s_alloc_4_addr, AWS_ARRAY_SIZE(s_alloc_4_addr), "0x%tx", (uintptr_t)(void *)s_alloc_4);
162
163 const char *log_buffer = (const char *)test_logger->log_buffer.buffer;
164 ASSERT_TRUE(strstr(log_buffer, "s_alloc_1") || strstr(log_buffer, s_alloc_1_addr));
165 ASSERT_TRUE(strstr(log_buffer, "s_alloc_2") || strstr(log_buffer, s_alloc_2_addr));
166 ASSERT_TRUE(strstr(log_buffer, "s_alloc_3") || strstr(log_buffer, s_alloc_3_addr));
167 ASSERT_TRUE(strstr(log_buffer, "s_alloc_4") || strstr(log_buffer, s_alloc_4_addr));
168 #endif /* DEBUG_BUILD */
169
170 /* reset log */
171 aws_byte_buf_reset(&test_logger->log_buffer, true);
172
173 for (size_t idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
174 if (allocs[idx]) {
175 aws_mem_release(tracer, allocs[idx]);
176 }
177 }
178
179 ASSERT_UINT_EQUALS(0, aws_mem_tracer_bytes(tracer));
180 ASSERT_UINT_EQUALS(0, aws_mem_tracer_count(tracer));
181 aws_mem_tracer_dump(tracer);
182
183 /* Make sure no known allocs are left */
184 ASSERT_UINT_EQUALS(0, test_logger->log_buffer.len);
185
186 struct aws_allocator *original = aws_mem_tracer_destroy(tracer);
187 ASSERT_PTR_EQUALS(allocator, original);
188
189 aws_logger_clean_up(&s_test_logger);
190
191 return 0;
192 }
193 AWS_TEST_CASE(test_memtrace_stacks, s_test_memtrace_stacks)
194
195 static int s_test_memtrace_none(struct aws_allocator *allocator, void *ctx) {
196 (void)ctx;
197 struct aws_allocator *tracer = aws_mem_tracer_new(allocator, NULL, AWS_MEMTRACE_NONE, 0);
198
199 void *allocs[NUM_ALLOCS] = {0};
200
201 for (size_t idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
202 uint32_t size = 0;
203 aws_device_random_u32(&size);
204 size = (size % 1024) + 1; /* not necessary to allocate a gajillion bytes */
205 allocs[idx] = aws_mem_acquire(tracer, size);
206 }
207
208 ASSERT_UINT_EQUALS(0, aws_mem_tracer_bytes(tracer));
209
210 for (size_t idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
211 if (allocs[idx]) {
212 aws_mem_release(tracer, allocs[idx]);
213 }
214 }
215
216 ASSERT_UINT_EQUALS(0, aws_mem_tracer_bytes(tracer));
217
218 struct aws_allocator *original = aws_mem_tracer_destroy(tracer);
219 ASSERT_PTR_EQUALS(allocator, original);
220
221 return 0;
222 }
223 AWS_TEST_CASE(test_memtrace_none, s_test_memtrace_none)
224
225 static int s_test_memtrace_midstream(struct aws_allocator *allocator, void *ctx) {
226 (void)ctx;
227
228 void *allocs[NUM_ALLOCS] = {0};
229
230 /* allocate some from the base allocator first */
231 for (size_t idx = 0; idx < AWS_ARRAY_SIZE(allocs) / 4; ++idx) {
232 uint32_t size = 0;
233 aws_device_random_u32(&size);
234 size = (size % 1024) + 1; /* not necessary to allocate a gajillion bytes */
235 allocs[idx] = aws_mem_acquire(allocator, size);
236 }
237
238 struct aws_allocator *tracer = aws_mem_tracer_new(allocator, NULL, AWS_MEMTRACE_BYTES, 0);
239
240 /* Now allocate from the tracer, and make sure everything still works */
241 size_t total = 0;
242 size_t tracked_allocs = 0;
243 for (size_t idx = AWS_ARRAY_SIZE(allocs) / 4 + 1; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
244 uint32_t size = 0;
245 aws_device_random_u32(&size);
246 size = (size % 1024) + 1; /* not necessary to allocate a gajillion bytes */
247 allocs[idx] = aws_mem_acquire(tracer, size);
248 total += size;
249 ++tracked_allocs;
250 }
251
252 ASSERT_UINT_EQUALS(total, aws_mem_tracer_bytes(tracer));
253 ASSERT_UINT_EQUALS(tracked_allocs, aws_mem_tracer_count(tracer));
254
255 for (size_t idx = 0; idx < AWS_ARRAY_SIZE(allocs); ++idx) {
256 if (allocs[idx]) {
257 aws_mem_release(tracer, allocs[idx]);
258 }
259 }
260
261 ASSERT_UINT_EQUALS(0, aws_mem_tracer_bytes(tracer));
262 ASSERT_UINT_EQUALS(0, aws_mem_tracer_count(tracer));
263
264 struct aws_allocator *original = aws_mem_tracer_destroy(tracer);
265 ASSERT_PTR_EQUALS(allocator, original);
266
267 return 0;
268 }
269 AWS_TEST_CASE(test_memtrace_midstream, s_test_memtrace_midstream)