Debian Codegraph @

crt/aws-c-common/tests/realloc_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/common/common.h>
7
8 #include <aws/testing/aws_test_harness.h>
9
10 #ifdef __MACH__
11 # include <CoreFoundation/CoreFoundation.h>
12 #endif
13
14 static size_t s_alloc_counter, s_alloc_total_size, s_call_ct_malloc, s_call_ct_free, s_call_ct_realloc;
15
16 static void *s_test_alloc_acquire(struct aws_allocator *allocator, size_t size) {
17 (void)allocator;
18
19 s_alloc_counter++;
20 s_call_ct_malloc++;
21 s_alloc_total_size += size;
22
23 uint8_t *buf = malloc(size + 16);
24 *(size_t *)buf = size;
25 buf += 16;
26 return buf;
27 }
28
29 static void s_test_alloc_release(struct aws_allocator *allocator, void *ptr) {
30 (void)allocator;
31
32 uint8_t *buf = ptr;
33 s_call_ct_free++;
34
35 buf -= 16;
36 size_t old_size = *(size_t *)buf;
37 s_alloc_counter--;
38 s_alloc_total_size -= old_size;
39
40 free(buf);
41 }
42
43 static size_t s_original_size, s_reported_oldsize;
44
45 static void *s_test_realloc(struct aws_allocator *allocator, void *ptr, size_t oldsize, size_t newsize) {
46 (void)allocator;
47
48 uint8_t *buf = ptr;
49 buf -= 16;
50 s_call_ct_realloc++;
51
52 s_original_size = *(size_t *)buf;
53 s_reported_oldsize = oldsize;
54
55 /* Always pick a new pointer for test purposes */
56 void *newbuf = malloc(newsize);
57 if (!newbuf) {
58 abort();
59 }
60
61 memcpy(newbuf, buf, 16 + (oldsize > newsize ? newsize : oldsize));
62 free(buf);
63 buf = newbuf;
64
65 *(size_t *)buf = newsize;
66 s_alloc_total_size += (newsize - oldsize);
67
68 return buf + 16;
69 }
70
71 static const uint8_t TEST_PATTERN[32] = {0xa5, 0x41, 0xcb, 0xe7, 0x00, 0x19, 0xd9, 0xf3, 0x60, 0x4a, 0x2b,
72 0x68, 0x55, 0x46, 0xb7, 0xe0, 0x74, 0x91, 0x2a, 0xbe, 0x5e, 0x41,
73 0x06, 0x39, 0x02, 0x02, 0xf6, 0x79, 0x1c, 0x4a, 0x08, 0xa9};
74
75 AWS_TEST_CASE(test_realloc_fallback, s_test_realloc_fallback_fn)
76 static int s_test_realloc_fallback_fn(struct aws_allocator *allocator, void *ctx) {
77 (void)allocator;
78 (void)ctx;
79
80 struct aws_allocator test_allocator = {
81 .mem_acquire = s_test_alloc_acquire,
82 .mem_release = s_test_alloc_release,
83 .mem_realloc = NULL,
84 };
85
86 s_call_ct_malloc = s_call_ct_free = s_call_ct_realloc = 0;
87
88 void *buf = aws_mem_acquire(&test_allocator, 32);
89 void *oldbuf = buf;
90 memcpy(buf, TEST_PATTERN, 32);
91 ASSERT_SUCCESS(aws_mem_realloc(&test_allocator, &buf, 32, 64));
92 ASSERT_INT_EQUALS(s_call_ct_malloc, 2);
93 ASSERT_INT_EQUALS(s_call_ct_free, 1);
94 ASSERT_INT_EQUALS(s_alloc_counter, 1);
95 ASSERT_INT_EQUALS(s_alloc_total_size, 64);
96 ASSERT_INT_EQUALS(memcmp(buf, TEST_PATTERN, 32), 0);
97 ASSERT_FALSE(buf == oldbuf);
98
99 aws_mem_release(&test_allocator, buf);
100
101 return 0;
102 }
103
104 AWS_TEST_CASE(test_realloc_passthrough, s_test_realloc_passthrough_fn)
105 static int s_test_realloc_passthrough_fn(struct aws_allocator *allocator, void *ctx) {
106 (void)allocator;
107 (void)ctx;
108
109 struct aws_allocator test_allocator = {
110 .mem_acquire = s_test_alloc_acquire,
111 .mem_release = s_test_alloc_release,
112 .mem_realloc = s_test_realloc,
113 };
114
115 s_call_ct_malloc = s_call_ct_free = s_call_ct_realloc = 0;
116
117 void *buf = aws_mem_acquire(&test_allocator, 32);
118 void *oldbuf = buf;
119 memcpy(buf, TEST_PATTERN, 32);
120
121 ASSERT_SUCCESS(aws_mem_realloc(&test_allocator, &buf, 32, 64));
122 ASSERT_INT_EQUALS(memcmp(buf, TEST_PATTERN, 32), 0);
123 ASSERT_INT_EQUALS(s_reported_oldsize, 32);
124 ASSERT_INT_EQUALS(s_original_size, 32);
125 ASSERT_INT_EQUALS(s_call_ct_malloc, 1);
126 ASSERT_INT_EQUALS(s_call_ct_free, 0);
127 ASSERT_FALSE(buf == oldbuf);
128
129 aws_mem_release(&test_allocator, buf);
130
131 return 0;
132 }
133
134 AWS_TEST_CASE(test_cf_allocator_wrapper, s_test_cf_allocator_wrapper_fn)
135
136 static int s_test_cf_allocator_wrapper_fn(struct aws_allocator *allocator, void *ctx) {
137 (void)allocator;
138 (void)ctx;
139
140 #ifdef __MACH__
141 CFAllocatorRef cf_allocator = aws_wrapped_cf_allocator_new(allocator);
142 ASSERT_NOT_NULL(cf_allocator);
143 char test_prefix[] = "test_string";
144 CFStringRef test_str = CFStringCreateWithCString(cf_allocator, test_prefix, kCFStringEncodingUTF8);
145
146 ASSERT_NOT_NULL(test_str);
147 /* NOLINTNEXTLINE */
148 ASSERT_BIN_ARRAYS_EQUALS(
149 test_prefix,
150 sizeof(test_prefix) - 1,
151 CFStringGetCStringPtr(test_str, kCFStringEncodingUTF8),
152 CFStringGetLength(test_str));
153
154 CFRelease(test_str);
155 aws_wrapped_cf_allocator_destroy(cf_allocator);
156 #endif
157
158 return 0;
159 }
160
161 AWS_TEST_CASE(test_acquire_many, s_test_acquire_many_fn)
162 static int s_test_acquire_many_fn(struct aws_allocator *allocator, void *ctx) {
163 (void)allocator;
164 (void)ctx;
165
166 void *a = NULL;
167 void *b = NULL;
168 void *buffer = aws_mem_acquire_many(allocator, 2, &a, (size_t)64, &b, (size_t)64);
169
170 ASSERT_NOT_NULL(buffer);
171 ASSERT_NOT_NULL(a);
172 ASSERT_NOT_NULL(b);
173
174 ASSERT_UINT_EQUALS((uintptr_t)a, (uintptr_t)buffer);
175 ASSERT_UINT_EQUALS((uintptr_t)b, (uintptr_t)buffer + 64);
176 ASSERT_UINT_EQUALS((uintptr_t)a % sizeof(intmax_t), 0);
177 ASSERT_UINT_EQUALS((uintptr_t)b % sizeof(intmax_t), 0);
178
179 aws_mem_release(allocator, buffer);
180 a = NULL;
181 b = NULL;
182
183 buffer = aws_mem_acquire_many(allocator, 2, &a, (size_t)1, &b, (size_t)1);
184
185 ASSERT_NOT_NULL(buffer);
186 ASSERT_NOT_NULL(a);
187 ASSERT_NOT_NULL(b);
188
189 ASSERT_UINT_EQUALS((uintptr_t)a, (uintptr_t)buffer);
190 ASSERT_UINT_EQUALS((uintptr_t)b, (uintptr_t)buffer + sizeof(intmax_t));
191 ASSERT_UINT_EQUALS((uintptr_t)a % sizeof(intmax_t), 0);
192 ASSERT_UINT_EQUALS((uintptr_t)b % sizeof(intmax_t), 0);
193
194 aws_mem_release(allocator, buffer);
195
196 return 0;
197 }