Debian Codegraph @

crt/aws-c-s3/tests/fuzz/fuzz_optimal_range_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_util.h>
10 #include <aws/testing/aws_test_harness.h>
11
12 #include <inttypes.h>
13 #include <stdint.h>
14
15 /**
16 * Combined fuzz test for optimal range size calculation functions:
17 * 1. aws_s3_calculate_client_optimal_range_size
18 * 2. aws_s3_calculate_request_optimal_range_size
19 *
20 * This tests the complete flow of calculating optimal range sizes for S3 operations.
21 */
22
23 AWS_EXTERN_C_BEGIN
24
25 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
26 /* Setup allocator */
27 struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0);
28
29 /* Init S3 library once (static to avoid repeated initialization) */
30 static bool s3_library_initialized = false;
31 if (!s3_library_initialized) {
32 aws_s3_library_init(aws_default_allocator());
33 s3_library_initialized = true;
34 }
35
36 /* Need at least 21 bytes of input:
37 * - 8 bytes for memory_limit_in_bytes (uint64_t)
38 * - 4 bytes for max_connections (uint32_t)
39 * - 8 bytes for estimated_object_stored_part_size (uint64_t)
40 * - 1 byte for is_express (bool)
41 */
42 if (size < 21) {
43 goto cleanup;
44 }
45
46 struct aws_byte_cursor input = aws_byte_cursor_from_array(data, size);
47
48 /* Extract inputs for aws_s3_calculate_client_optimal_range_size */
49 uint64_t memory_limit_in_bytes = 0;
50 aws_byte_cursor_read_be64(&input, &memory_limit_in_bytes);
51
52 uint32_t max_connections = 0;
53 aws_byte_cursor_read_be32(&input, &max_connections);
54
55 /* Extract inputs for aws_s3_calculate_request_optimal_range_size */
56 uint64_t estimated_object_stored_part_size = 0;
57 aws_byte_cursor_read_be64(&input, &estimated_object_stored_part_size);
58
59 uint8_t is_express_byte = 0;
60 aws_byte_cursor_read_u8(&input, &is_express_byte);
61 bool is_express = (is_express_byte % 2) == 1;
62
63 /* Test 1: aws_s3_calculate_client_optimal_range_size */
64 uint64_t client_optimal_range_size = 0;
65 int result1 =
66 aws_s3_calculate_client_optimal_range_size(memory_limit_in_bytes, max_connections, &client_optimal_range_size);
67
68 if (memory_limit_in_bytes == 0 || max_connections == 0) {
69 AWS_FATAL_ASSERT(result1 == AWS_OP_ERR);
70 AWS_FATAL_ASSERT(aws_last_error() == AWS_ERROR_INVALID_ARGUMENT);
71 goto cleanup; /* Can't proceed to test 2 without valid client_optimal_range_size */
72 }
73
74 AWS_FATAL_ASSERT(result1 == AWS_OP_SUCCESS);
75 AWS_FATAL_ASSERT(client_optimal_range_size > 0);
76
77 const uint64_t max_part_size = g_default_max_part_size; /* 5 GiB */
78 const uint64_t min_part_size = g_default_part_size_fallback; /* 8 MiB */
79
80 AWS_FATAL_ASSERT(client_optimal_range_size >= min_part_size);
81 AWS_FATAL_ASSERT(client_optimal_range_size <= max_part_size);
82 /* Note: client_optimal_range_size may exceed memory_limit_in_bytes when the minimum
83 * constraint (8 MiB) is applied. This is intentional to maintain S3's minimum part size. */
84
85 /* Verify the calculation formula */
86 uint64_t memory_constrained_size = memory_limit_in_bytes / max_connections / 3;
87 if (memory_constrained_size < min_part_size) {
88 AWS_FATAL_ASSERT(client_optimal_range_size == min_part_size);
89 } else if (memory_constrained_size > max_part_size) {
90 AWS_FATAL_ASSERT(client_optimal_range_size == max_part_size);
91 } else {
92 AWS_FATAL_ASSERT(client_optimal_range_size == memory_constrained_size);
93 }
94
95 /* Test 2: aws_s3_calculate_request_optimal_range_size */
96 uint64_t request_optimal_range_size = 0;
97 int result2 = aws_s3_calculate_request_optimal_range_size(
98 client_optimal_range_size, estimated_object_stored_part_size, is_express, &request_optimal_range_size);
99
100 AWS_FATAL_ASSERT(result2 == AWS_OP_SUCCESS);
101 AWS_FATAL_ASSERT(request_optimal_range_size > 0);
102 AWS_FATAL_ASSERT(request_optimal_range_size <= max_part_size);
103
104 /* Verify S3 Express constraints */
105 if (is_express) {
106 const uint64_t express_max = 134217728ULL; /* 128 MiB */
107 AWS_FATAL_ASSERT(request_optimal_range_size <= express_max);
108 } else {
109 const uint64_t standard_max = 2147483648ULL; /* 2 GiB */
110 AWS_FATAL_ASSERT(request_optimal_range_size <= standard_max);
111 }
112
113 /* Verify relationship between client and request optimal sizes */
114 const uint64_t default_part_size_fallback = g_default_part_size_fallback; /* 8 MiB */
115 if (estimated_object_stored_part_size > 0 && estimated_object_stored_part_size < client_optimal_range_size &&
116 client_optimal_range_size >= default_part_size_fallback) {
117 /* When client size is >= fallback, output should be <= client_optimal_range_size */
118 AWS_FATAL_ASSERT(request_optimal_range_size <= client_optimal_range_size);
119 }
120
121 cleanup:
122 /* Check for memory leaks */
123 AWS_FATAL_ASSERT(aws_mem_tracer_count(allocator) == 0);
124 allocator = aws_mem_tracer_destroy(allocator);
125
126 return 0;
127 }
128
129 AWS_EXTERN_C_END