1b815c7f3Sopenharmony_ci#include <stdint.h> 2b815c7f3Sopenharmony_ci#include <stdio.h> 3b815c7f3Sopenharmony_ci#include <stdlib.h> 4b815c7f3Sopenharmony_ci 5b815c7f3Sopenharmony_ci#include "testinput.h" 6b815c7f3Sopenharmony_ci 7b815c7f3Sopenharmony_ci/** 8b815c7f3Sopenharmony_ci * Main procedure for standalone fuzzing engine. 9b815c7f3Sopenharmony_ci * 10b815c7f3Sopenharmony_ci * Reads filenames from the argument array. For each filename, read the file 11b815c7f3Sopenharmony_ci * into memory and then call the fuzzing interface with the data. 12b815c7f3Sopenharmony_ci */ 13b815c7f3Sopenharmony_ciint main(int argc, char **argv) 14b815c7f3Sopenharmony_ci{ 15b815c7f3Sopenharmony_ci int ii; 16b815c7f3Sopenharmony_ci for(ii = 1; ii < argc; ii++) 17b815c7f3Sopenharmony_ci { 18b815c7f3Sopenharmony_ci FILE *infile; 19b815c7f3Sopenharmony_ci printf("[%s] ", argv[ii]); 20b815c7f3Sopenharmony_ci 21b815c7f3Sopenharmony_ci /* Try and open the file. */ 22b815c7f3Sopenharmony_ci infile = fopen(argv[ii], "rb"); 23b815c7f3Sopenharmony_ci if(infile) 24b815c7f3Sopenharmony_ci { 25b815c7f3Sopenharmony_ci uint8_t *buffer = NULL; 26b815c7f3Sopenharmony_ci size_t buffer_len; 27b815c7f3Sopenharmony_ci 28b815c7f3Sopenharmony_ci printf("Opened.. "); 29b815c7f3Sopenharmony_ci 30b815c7f3Sopenharmony_ci /* Get the length of the file. */ 31b815c7f3Sopenharmony_ci fseek(infile, 0L, SEEK_END); 32b815c7f3Sopenharmony_ci buffer_len = ftell(infile); 33b815c7f3Sopenharmony_ci 34b815c7f3Sopenharmony_ci /* Reset the file indicator to the beginning of the file. */ 35b815c7f3Sopenharmony_ci fseek(infile, 0L, SEEK_SET); 36b815c7f3Sopenharmony_ci 37b815c7f3Sopenharmony_ci /* Allocate a buffer for the file contents. */ 38b815c7f3Sopenharmony_ci buffer = (uint8_t *)calloc(buffer_len, sizeof(uint8_t)); 39b815c7f3Sopenharmony_ci if(buffer) 40b815c7f3Sopenharmony_ci { 41b815c7f3Sopenharmony_ci size_t result; 42b815c7f3Sopenharmony_ci 43b815c7f3Sopenharmony_ci /* Read all the text from the file into the buffer. */ 44b815c7f3Sopenharmony_ci result = fread(buffer, sizeof(uint8_t), buffer_len, infile); 45b815c7f3Sopenharmony_ci 46b815c7f3Sopenharmony_ci if (result == buffer_len) 47b815c7f3Sopenharmony_ci { 48b815c7f3Sopenharmony_ci printf("Read %zu bytes, fuzzing.. ", buffer_len); 49b815c7f3Sopenharmony_ci /* Call the fuzzer with the data. */ 50b815c7f3Sopenharmony_ci LLVMFuzzerTestOneInput(buffer, buffer_len); 51b815c7f3Sopenharmony_ci 52b815c7f3Sopenharmony_ci printf("complete !!"); 53b815c7f3Sopenharmony_ci } 54b815c7f3Sopenharmony_ci else 55b815c7f3Sopenharmony_ci { 56b815c7f3Sopenharmony_ci fprintf(stderr, 57b815c7f3Sopenharmony_ci "Failed to read %zu bytes (result %zu)\n", 58b815c7f3Sopenharmony_ci buffer_len, 59b815c7f3Sopenharmony_ci result); 60b815c7f3Sopenharmony_ci } 61b815c7f3Sopenharmony_ci 62b815c7f3Sopenharmony_ci /* Free the buffer as it's no longer needed. */ 63b815c7f3Sopenharmony_ci free(buffer); 64b815c7f3Sopenharmony_ci buffer = NULL; 65b815c7f3Sopenharmony_ci } 66b815c7f3Sopenharmony_ci else 67b815c7f3Sopenharmony_ci { 68b815c7f3Sopenharmony_ci fprintf(stderr, 69b815c7f3Sopenharmony_ci "[%s] Failed to allocate %zu bytes \n", 70b815c7f3Sopenharmony_ci argv[ii], 71b815c7f3Sopenharmony_ci buffer_len); 72b815c7f3Sopenharmony_ci } 73b815c7f3Sopenharmony_ci 74b815c7f3Sopenharmony_ci /* Close the file as it's no longer needed. */ 75b815c7f3Sopenharmony_ci fclose(infile); 76b815c7f3Sopenharmony_ci infile = NULL; 77b815c7f3Sopenharmony_ci } 78b815c7f3Sopenharmony_ci else 79b815c7f3Sopenharmony_ci { 80b815c7f3Sopenharmony_ci /* Failed to open the file. Maybe wrong name or wrong permissions? */ 81b815c7f3Sopenharmony_ci fprintf(stderr, "[%s] Open failed. \n", argv[ii]); 82b815c7f3Sopenharmony_ci } 83b815c7f3Sopenharmony_ci 84b815c7f3Sopenharmony_ci printf("\n"); 85b815c7f3Sopenharmony_ci } 86b815c7f3Sopenharmony_ci} 87