Debian Codegraph @

crt/aws-c-s3/tests/fuzz/fuzz_buffer_pool_special_size.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/allocator.h>
7 #include <aws/common/byte_buf.h>
8 #include <aws/common/logging.h>
9 #include <aws/s3/private/s3_default_buffer_pool.h>
10 #include <aws/s3/private/s3_util.h>
11 #include <aws/testing/aws_test_harness.h>
12
13 #include <inttypes.h>
14 #include <stdint.h>
15
16 /**
17 * Fuzz test for buffer pool with special sizes.
18 * Tests the complete flow of:
19 * 1. Adding special sizes to the buffer pool
20 * 2. Reserving buffers with a mix of special sizes and random sizes
21 * 3. Verifying usage statistics match expectations
22 * 4. Acquiring buffers from tickets
23 * 5. Releasing buffers and verifying cleanup
24 */
25
26 #define MAX_SPECIAL_SIZES 5
27 #define MAX_RESERVATIONS 10000
28
29 AWS_EXTERN_C_BEGIN
30
31 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32 /* Setup allocator */
33 struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0);
34
35 /* Init S3 library once (static to avoid repeated initialization) */
36 static bool s3_library_initialized = false;
37 if (!s3_library_initialized) {
38 aws_s3_library_init(aws_default_allocator());
39 s3_library_initialized = true;
40 }
41
42 /* Need at least 3 bytes of input:
43 * - 1 byte for num_special_sizes
44 * - 1 byte for num_reservations
45 * - 1 byte for at least one size value
46 */
47 if (size < 3) {
48 goto cleanup;
49 }
50
51 struct aws_byte_cursor input = aws_byte_cursor_from_array(data, size);
52
53 /* Extract number of special sizes (0-5) */
54 uint8_t num_special_sizes_raw = 0;
55 aws_byte_cursor_read_u8(&input, &num_special_sizes_raw);
56 size_t num_special_sizes = (num_special_sizes_raw % (MAX_SPECIAL_SIZES + 1));
57
58 /* Extract number of reservations (1-20) */
59 uint8_t num_reservations_raw = 0;
60 aws_byte_cursor_read_u8(&input, &num_reservations_raw);
61 size_t num_reservations = 1 + (num_reservations_raw % MAX_RESERVATIONS);
62
63 /* Scale down counts based on available data for better fuzzing coverage */
64 size_t available_for_special = input.len / 4;
65 size_t available_for_reservations = input.len / 5;
66
67 if (num_special_sizes > available_for_special) {
68 num_special_sizes = available_for_special;
69 }
70 if (num_reservations > available_for_reservations) {
71 num_reservations = available_for_reservations;
72 }
73
74 /* Create buffer pool with balanced memory limit to avoid OOM during fuzzing */
75 size_t part_size = MB_TO_BYTES(8);
76 size_t memory_limit = GB_TO_BYTES(1); /* 1GB - balanced between functionality and avoiding OOM */
77 struct aws_s3_buffer_pool *buffer_pool = aws_s3_default_buffer_pool_new(
78 allocator, (struct aws_s3_buffer_pool_config){.part_size = part_size, .memory_limit = memory_limit});
79
80 AWS_FATAL_ASSERT(buffer_pool);
81 struct aws_s3_default_buffer_pool *pool = buffer_pool->impl;
82 /* Hack to keep the special blocks alive from the trim, so that we can be sure tracking the stats correctly. */
83 pool->force_keeping_special_blocks = true;
84 /* Get initial stats */
85 struct aws_s3_default_buffer_pool_usage_stats initial_stats = aws_s3_default_buffer_pool_get_usage(buffer_pool);
86 size_t primary_cutoff = initial_stats.primary_cutoff;
87
88 /* Add special sizes - consume as much input as available */
89 size_t special_sizes[MAX_SPECIAL_SIZES] = {0};
90 size_t valid_special_sizes = 0;
91 size_t unique_special_sizes_added = 0;
92
93 for (size_t i = 0; i < num_special_sizes && input.len >= 4; ++i) {
94 uint32_t size_raw = 0;
95 if (!aws_byte_cursor_read_be32(&input, &size_raw)) {
96 break; /* Not enough bytes left */
97 }
98
99 /* Generate special size between primary_cutoff and 256MB */
100 size_t special_size = primary_cutoff + 1 + (size_raw % MB_TO_BYTES(256));
101
102 /* Check if this is a duplicate size */
103 bool is_duplicate = false;
104 for (size_t j = 0; j < valid_special_sizes; ++j) {
105 if (special_sizes[j] == special_size) {
106 is_duplicate = true;
107 break;
108 }
109 }
110
111 int result = aws_s3_buffer_pool_add_special_size(buffer_pool, special_size);
112 AWS_FATAL_ASSERT(result == AWS_OP_SUCCESS);
113
114 /* Track unique sizes (add_special_size is idempotent, so duplicates don't create new lists) */
115 if (!is_duplicate) {
116 unique_special_sizes_added++;
117 special_sizes[valid_special_sizes++] = special_size;
118 }
119 }
120
121 /* Skip test if no special sizes were added - this test is specifically for testing special sizes */
122 if (valid_special_sizes == 0) {
123 aws_s3_default_buffer_pool_destroy(buffer_pool);
124 goto cleanup;
125 }
126
127 /* Verify special_blocks_num matches the number of unique special sizes we added */
128 struct aws_s3_default_buffer_pool_usage_stats after_add_stats = aws_s3_default_buffer_pool_get_usage(buffer_pool);
129 AWS_FATAL_ASSERT(after_add_stats.special_blocks_num == unique_special_sizes_added);
130
131 /* Track reservations - some will be kept, some released immediately within the loop */
132 struct aws_future_s3_buffer_ticket *futures[MAX_RESERVATIONS] = {0};
133 struct aws_s3_buffer_ticket *tickets[MAX_RESERVATIONS] = {0};
134 size_t num_kept = 0; /* Track how many we're keeping for later release */
135
136 /* Track expected usage for validation */
137 size_t expected_primary_reserved = 0;
138 size_t expected_primary_used = 0;
139 size_t expected_secondary_reserved = 0;
140 size_t expected_secondary_used = 0;
141 size_t expected_special_reserved = 0;
142 size_t expected_special_used = 0;
143
144 /*
145 * Stress test: reserve/acquire/release cycle within loop
146 * Pattern: reserve → acquire → maybe release immediately (50% chance)
147 * This creates heavy churn and tests state management
148 */
149 for (size_t i = 0; i < num_reservations && input.len >= 6; ++i) {
150 /* Read 6 bytes: 1 for type, 4 for size, 1 for release decision */
151 uint8_t reservation_type = 0;
152 if (!aws_byte_cursor_read_u8(&input, &reservation_type)) {
153 break;
154 }
155
156 uint32_t size_value = 0;
157 if (!aws_byte_cursor_read_be32(&input, &size_value)) {
158 break;
159 }
160
161 uint8_t release_immediately = 0;
162 if (!aws_byte_cursor_read_u8(&input, &release_immediately)) {
163 break;
164 }
165
166 /* Determine reservation size and type BEFORE reserving */
167 size_t reservation_size = 0;
168 bool is_special = false;
169 bool is_primary = false;
170 bool is_secondary = false;
171
172 uint8_t size_type = reservation_type % 3;
173
174 if (size_type == 0) {
175 /* Special size allocation - explicitly choose a special size */
176 size_t special_idx = size_value % valid_special_sizes;
177 reservation_size = special_sizes[special_idx];
178 is_special = true;
179 } else if (size_type == 1) {
180 /* Primary storage allocation (below primary_cutoff) */
181 reservation_size = 1024 + (size_value % (primary_cutoff - 1024));
182 } else {
183 /* Secondary storage allocation (above primary_cutoff, below smallest special size) */
184 size_t secondary_range = special_sizes[0] - primary_cutoff - 1;
185 if (secondary_range > 0) {
186 reservation_size = primary_cutoff + 1 + (size_value % secondary_range);
187 } else {
188 /* Not enough space for secondary, use primary instead */
189 reservation_size = 1024 + (size_value % (primary_cutoff - 1024));
190 }
191 }
192
193 /*
194 * CRITICAL: Check if randomly generated size accidentally matches a special size!
195 * If so, the buffer pool will treat it as special, not secondary/primary.
196 * We must update our flags to match what the pool will actually do.
197 */
198 if (!is_special) {
199 for (size_t j = 0; j < valid_special_sizes; ++j) {
200 if (reservation_size == special_sizes[j]) {
201 /* Randomly hit a special size - update flags to match buffer pool behavior */
202 is_special = true;
203 is_primary = false;
204 is_secondary = false;
205 break;
206 }
207 }
208 }
209
210 /* Reserve */
211 struct aws_future_s3_buffer_ticket *future = aws_s3_default_buffer_pool_reserve(
212 buffer_pool, (struct aws_s3_buffer_pool_reserve_meta){.size = reservation_size});
213
214 AWS_FATAL_ASSERT(future);
215
216 /* Check if reservation completed */
217 if (!aws_future_s3_buffer_ticket_is_done(future)) {
218 /* Hit memory limit - release the pending future to keep exact tracking */
219 aws_future_s3_buffer_ticket_release(future);
220 break;
221 }
222
223 /* Check for errors */
224 int error = aws_future_s3_buffer_ticket_get_error(future);
225 AWS_FATAL_ASSERT(error == AWS_OP_SUCCESS);
226
227 /* Acquire the ticket */
228 struct aws_s3_buffer_ticket *ticket = aws_future_s3_buffer_ticket_get_result_by_move(future);
229 AWS_FATAL_ASSERT(ticket != NULL);
230
231 enum aws_s3_default_buffer_pool_reserved_from reserved_from =
232 aws_s3_default_buffer_pool_get_ticket_reserved_from(ticket->impl);
233
234 if (reserved_from == AWS_S3_BUFFER_POOL_RESERVED_FROM_SPECIAL) {
235 AWS_FATAL_ASSERT(is_special); /* we know in advance all special blocks that must be reserved.*/
236 is_special = true;
237 } else if (reserved_from == AWS_S3_BUFFER_POOL_RESERVED_FROM_PRIMARY) {
238 is_primary = true; /* for primary and secondary there is a lot more rules so rely on pool for where its
239 allocated from*/
240 } else if (reserved_from == AWS_S3_BUFFER_POOL_RESERVED_FROM_SECONDARY) {
241 is_secondary = true;
242 } else {
243 AWS_FATAL_ASSERT(false);
244 }
245
246 /* Update expected reserved based on type we determined earlier (including accidental special size check) */
247 if (is_special) {
248 expected_special_reserved += reservation_size;
249 } else if (is_primary) {
250 expected_primary_reserved += reservation_size;
251 } else if (is_secondary) {
252 expected_secondary_reserved += reservation_size;
253 }
254
255 /* Check #1: Verify stats after reserve match expectations */
256 struct aws_s3_default_buffer_pool_usage_stats after_reserve_stats =
257 aws_s3_default_buffer_pool_get_usage(buffer_pool);
258
259 if (is_special) {
260 AWS_FATAL_ASSERT(after_reserve_stats.special_blocks_reserved == expected_special_reserved);
261 } else if (is_primary) {
262 AWS_FATAL_ASSERT(after_reserve_stats.primary_reserved == expected_primary_reserved);
263 } else if (is_secondary) {
264 AWS_FATAL_ASSERT(after_reserve_stats.secondary_reserved == expected_secondary_reserved);
265 }
266
267 /* Claim the buffer to verify it works */
268 struct aws_byte_buf buf = aws_s3_buffer_ticket_claim(ticket);
269 AWS_FATAL_ASSERT(buf.buffer != NULL);
270 AWS_FATAL_ASSERT(buf.capacity >= reservation_size);
271 aws_future_s3_buffer_ticket_release(future);
272 /* Update expected used and reserved after acquire */
273 if (is_special) {
274 expected_special_used += reservation_size;
275 expected_special_reserved -= reservation_size;
276 } else if (is_primary) {
277 expected_primary_used += reservation_size;
278 expected_primary_reserved -= reservation_size;
279 } else if (is_secondary) {
280 expected_secondary_used += reservation_size;
281 expected_secondary_reserved -= reservation_size;
282 }
283
284 /* Check #2: Verify stats after acquire match expectations */
285 struct aws_s3_default_buffer_pool_usage_stats after_acquire_stats =
286 aws_s3_default_buffer_pool_get_usage(buffer_pool);
287
288 if (is_special) {
289 AWS_FATAL_ASSERT(after_acquire_stats.special_blocks_used >= expected_special_used);
290 AWS_FATAL_ASSERT(after_acquire_stats.special_blocks_reserved >= expected_special_reserved);
291 } else if (is_primary) {
292 AWS_FATAL_ASSERT(after_acquire_stats.primary_used >= expected_primary_used);
293 AWS_FATAL_ASSERT(after_acquire_stats.primary_reserved >= expected_primary_reserved);
294 } else if (is_secondary) {
295 AWS_FATAL_ASSERT(after_acquire_stats.secondary_used >= expected_secondary_used);
296 AWS_FATAL_ASSERT(after_acquire_stats.secondary_reserved >= expected_secondary_reserved);
297 }
298
299 /* Write to buffer to ensure it's valid */
300 if (buf.capacity > 0) {
301 buf.buffer[0] = (uint8_t)i;
302 if (buf.capacity > 1) {
303 buf.buffer[buf.capacity - 1] = (uint8_t)i;
304 }
305 }
306
307 /* Decision: release immediately (50% chance) or keep for later */
308 if ((release_immediately % 2) == 0) {
309 /* Release immediately - creates churn */
310 aws_s3_buffer_ticket_release(ticket);
311
312 /* Update expected used after release */
313 if (is_special) {
314 expected_special_used -= reservation_size;
315 } else if (is_primary) {
316 expected_primary_used -= reservation_size;
317 } else if (is_secondary) {
318 expected_secondary_used -= reservation_size;
319 }
320
321 /* Check #3: Verify stats after release match expectations */
322 struct aws_s3_default_buffer_pool_usage_stats after_release_stats =
323 aws_s3_default_buffer_pool_get_usage(buffer_pool);
324
325 if (is_special) {
326 AWS_FATAL_ASSERT(after_release_stats.special_blocks_used >= expected_special_used);
327 } else if (is_primary) {
328 AWS_FATAL_ASSERT(after_release_stats.primary_used >= expected_primary_used);
329 } else if (is_secondary) {
330 AWS_FATAL_ASSERT(after_release_stats.secondary_used >= expected_secondary_used);
331 }
332 } else {
333 /* Keep for later release */
334 tickets[num_kept++] = ticket;
335 }
336 }
337
338 /* Release all tickets and futures */
339 for (size_t i = 0; i < MAX_RESERVATIONS; ++i) {
340 if (tickets[i] != NULL) {
341 aws_s3_buffer_ticket_release(tickets[i]);
342 tickets[i] = NULL;
343 }
344 if (futures[i] != NULL) {
345 aws_future_s3_buffer_ticket_release(futures[i]);
346 futures[i] = NULL;
347 }
348 }
349
350 for (size_t j = 0; j < valid_special_sizes; ++j) {
351 aws_s3_buffer_pool_release_special_size(buffer_pool, special_sizes[j]);
352 }
353 /* Force trim */
354 aws_s3_buffer_pool_trim(buffer_pool);
355
356 /* Check final stats after release */
357 struct aws_s3_default_buffer_pool_usage_stats final_stats = aws_s3_default_buffer_pool_get_usage(buffer_pool);
358
359 /* Verify everything is released */
360 AWS_FATAL_ASSERT(final_stats.primary_allocated == 0);
361 AWS_FATAL_ASSERT(final_stats.primary_used == 0);
362 AWS_FATAL_ASSERT(final_stats.primary_reserved == 0);
363 AWS_FATAL_ASSERT(final_stats.secondary_used == 0);
364 AWS_FATAL_ASSERT(final_stats.secondary_reserved == 0);
365 AWS_FATAL_ASSERT(final_stats.special_blocks_allocated == 0);
366 AWS_FATAL_ASSERT(final_stats.special_blocks_used == 0);
367 AWS_FATAL_ASSERT(final_stats.special_blocks_reserved == 0);
368 AWS_FATAL_ASSERT(final_stats.forced_used == 0);
369
370 /*
371 * Note: We don't assert special_blocks_allocated > 0 because the loop might not have
372 * allocated any special sizes depending on the fuzzer input pattern
373 */
374
375 /* Clean up buffer pool */
376 aws_s3_default_buffer_pool_destroy(buffer_pool);
377
378 cleanup:
379 /* Check for memory leaks */
380 AWS_FATAL_ASSERT(aws_mem_tracer_count(allocator) == 0);
381 allocator = aws_mem_tracer_destroy(allocator);
382 return 0;
383 }
384
385 AWS_EXTERN_C_END