| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
AWS_EXTERN_C_BEGIN |
| 24 |
|
| 25 |
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
| 26 |
|
| 27 |
struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0); |
| 28 |
|
| 29 |
|
| 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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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; |
| 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; |
| 78 |
const uint64_t min_part_size = g_default_part_size_fallback; |
| 79 |
|
| 80 |
AWS_FATAL_ASSERT(client_optimal_range_size >= min_part_size); |
| 81 |
AWS_FATAL_ASSERT(client_optimal_range_size <= max_part_size); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 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 |
|
| 105 |
if (is_express) { |
| 106 |
const uint64_t express_max = 134217728ULL; |
| 107 |
AWS_FATAL_ASSERT(request_optimal_range_size <= express_max); |
| 108 |
} else { |
| 109 |
const uint64_t standard_max = 2147483648ULL; |
| 110 |
AWS_FATAL_ASSERT(request_optimal_range_size <= standard_max); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
const uint64_t default_part_size_fallback = g_default_part_size_fallback; |
| 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 |
|
| 118 |
AWS_FATAL_ASSERT(request_optimal_range_size <= client_optimal_range_size); |
| 119 |
} |
| 120 |
|
| 121 |
cleanup: |
| 122 |
|
| 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 |