|
| 1 | +// Copyright 2023 P1umer |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <signal.h> |
| 16 | +#include <execinfo.h> |
| 17 | +#include <stdio.h> |
| 18 | +#include <stdlib.h> |
| 19 | +#include <unistd.h> |
| 20 | + |
| 21 | +#include "extractor.h" |
| 22 | + |
| 23 | +// libFuzzer interface is thin, so we don't include any libFuzzer headers. |
| 24 | +__attribute__((weak)) int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); |
| 25 | +__attribute__((weak)) int LLVMFuzzerInitialize(int *argc, char ***argv){ |
| 26 | + // Default implementation does nothing |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +#define INITIAL_SIZE (100) |
| 31 | +#define MAX_LENGTH(x, y) ( ((x) > (y)) ? (x) : y ) |
| 32 | + |
| 33 | +// #define kMaxAflInputSize (1 * 1024 * 1024) |
| 34 | +// static uint8_t AflInputBuf[kMaxAflInputSize]; |
| 35 | +static mutate_helper_t* afl_mutate_helper; |
| 36 | + |
| 37 | +void fake_init() { |
| 38 | + int fake_argc = 3; |
| 39 | + |
| 40 | + char *fake_argv_arr[] = { |
| 41 | + "fake1", |
| 42 | + "fake2", |
| 43 | + "fake3" |
| 44 | + }; |
| 45 | + |
| 46 | + char **fake_argv = malloc(fake_argc * sizeof(char *)); |
| 47 | + if (!fake_argv) { |
| 48 | + perror("Failed to allocate memory for fake_argv"); |
| 49 | + // return 1; |
| 50 | + } |
| 51 | + |
| 52 | + for (int i = 0; i < fake_argc; i++) { |
| 53 | + fake_argv[i] = fake_argv_arr[i]; |
| 54 | + } |
| 55 | + if (LLVMFuzzerInitialize) { |
| 56 | + LLVMFuzzerInitialize(&fake_argc, &fake_argv); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +void mutate_helper_init() { |
| 61 | + afl_mutate_helper = (mutate_helper_t *)calloc(1, sizeof(mutate_helper_t)); |
| 62 | + afl_mutate_helper->len = INITIAL_SIZE; |
| 63 | + afl_mutate_helper->buf = (uint8_t *)calloc(1, afl_mutate_helper->len); |
| 64 | + if (!afl_mutate_helper->buf) perror("mutate_helper_init"); |
| 65 | + fake_init(); |
| 66 | +} |
| 67 | + |
| 68 | +size_t mutate_helper_generate(unsigned char *buf, size_t buf_size) { |
| 69 | + return LLVMFuzzerTestOneInput(buf, buf_size); |
| 70 | +} |
| 71 | + |
| 72 | +void mutate_helper_realloc_buf(size_t length) { |
| 73 | + |
| 74 | + if (afl_mutate_helper->len <= length) { |
| 75 | + // double length |
| 76 | + size_t old_length = afl_mutate_helper->len; |
| 77 | + afl_mutate_helper->len = length * 2; |
| 78 | + afl_mutate_helper->buf = (uint8_t *)realloc(afl_mutate_helper->buf, afl_mutate_helper->len); |
| 79 | + if (!afl_mutate_helper->buf) { |
| 80 | + perror("mutate_helper_realloc"); |
| 81 | + return; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + memset(afl_mutate_helper->buf, 0, afl_mutate_helper->len); |
| 86 | +} |
| 87 | + |
| 88 | +void mutate_helper_buffer_copy(unsigned char *buf, size_t buf_size){ |
| 89 | + mutate_helper_realloc_buf(buf_size); |
| 90 | + // Clear only the newly allocated portion of the buffer |
| 91 | + memcpy(afl_mutate_helper->buf, buf, buf_size); |
| 92 | + |
| 93 | +} |
| 94 | + |
| 95 | +uint8_t* mutate_helper_buffer(){ |
| 96 | + return afl_mutate_helper->buf; |
| 97 | +} |
| 98 | + |
| 99 | +size_t mutate_helper_buffer_size(){ |
| 100 | + return afl_mutate_helper->len; |
| 101 | +} |
| 102 | + |
| 103 | +#undef MAX_LENGTH |
| 104 | +#undef INITIAL_SIZE |
| 105 | + |
0 commit comments