| 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_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 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 33 |
struct aws_allocator *allocator = aws_mem_tracer_new(aws_default_allocator(), NULL, AWS_MEMTRACE_BYTES, 0); |
| 34 |
|
| 35 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 75 |
size_t part_size = MB_TO_BYTES(8); |
| 76 |
size_t memory_limit = GB_TO_BYTES(1); |
| 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 |
|
| 83 |
pool->force_keeping_special_blocks = true; |
| 84 |
|
| 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 |
|
| 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; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
size_t special_size = primary_cutoff + 1 + (size_raw % MB_TO_BYTES(256)); |
| 101 |
|
| 102 |
|
| 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 |
|
| 115 |
if (!is_duplicate) { |
| 116 |
unique_special_sizes_added++; |
| 117 |
special_sizes[valid_special_sizes++] = special_size; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
if (valid_special_sizes == 0) { |
| 123 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 124 |
goto cleanup; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 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 |
|
| 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; |
| 135 |
|
| 136 |
|
| 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 |
|
| 146 |
nce) |
| 147 |
|
| 148 |
|
| 149 |
for (size_t i = 0; i < num_reservations && input.len >= 6; ++i) { |
| 150 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 181 |
reservation_size = 1024 + (size_value % (primary_cutoff - 1024)); |
| 182 |
} else { |
| 183 |
|
| 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 |
|
| 189 |
reservation_size = 1024 + (size_value % (primary_cutoff - 1024)); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
if (!is_special) { |
| 199 |
for (size_t j = 0; j < valid_special_sizes; ++j) { |
| 200 |
if (reservation_size == special_sizes[j]) { |
| 201 |
|
| 202 |
is_special = true; |
| 203 |
is_primary = false; |
| 204 |
is_secondary = false; |
| 205 |
break; |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 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 |
|
| 217 |
if (!aws_future_s3_buffer_ticket_is_done(future)) { |
| 218 |
|
| 219 |
aws_future_s3_buffer_ticket_release(future); |
| 220 |
break; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
int error = aws_future_s3_buffer_ticket_get_error(future); |
| 225 |
AWS_FATAL_ASSERT(error == AWS_OP_SUCCESS); |
| 226 |
|
| 227 |
|
| 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); |
| 236 |
is_special = true; |
| 237 |
} else if (reserved_from == AWS_S3_BUFFER_POOL_RESERVED_FROM_PRIMARY) { |
| 238 |
is_primary = true; |
| 239 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 308 |
if ((release_immediately % 2) == 0) { |
| 309 |
|
| 310 |
aws_s3_buffer_ticket_release(ticket); |
| 311 |
|
| 312 |
|
| 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 |
|
| 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 |
|
| 334 |
tickets[num_kept++] = ticket; |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 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 |
|
| 354 |
aws_s3_buffer_pool_trim(buffer_pool); |
| 355 |
|
| 356 |
|
| 357 |
struct aws_s3_default_buffer_pool_usage_stats final_stats = aws_s3_default_buffer_pool_get_usage(buffer_pool); |
| 358 |
|
| 359 |
|
| 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 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
aws_s3_default_buffer_pool_destroy(buffer_pool); |
| 377 |
|
| 378 |
cleanup: |
| 379 |
|
| 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 |