"Fossies" - the Fresh Open Source Software Archive

Member "honggfuzz-2.2/libhfuzz/persistent.c" (23 Apr 2020, 3951 Bytes) of package /linux/privat/honggfuzz-2.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "persistent.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.1_vs_2.2.

    1 #include <errno.h>
    2 #include <fcntl.h>
    3 #include <inttypes.h>
    4 #include <limits.h>
    5 #include <signal.h>
    6 #include <stdbool.h>
    7 #include <stdint.h>
    8 #include <stdio.h>
    9 #include <stdlib.h>
   10 #include <string.h>
   11 #include <sys/mman.h>
   12 #include <sys/stat.h>
   13 #include <sys/types.h>
   14 #include <unistd.h>
   15 
   16 #include "honggfuzz.h"
   17 #include "libhfcommon/common.h"
   18 #include "libhfcommon/files.h"
   19 #include "libhfcommon/log.h"
   20 #include "libhfuzz/fetch.h"
   21 #include "libhfuzz/instrument.h"
   22 #include "libhfuzz/libhfuzz.h"
   23 #include "libhfuzz/performance.h"
   24 
   25 __attribute__((weak)) int LLVMFuzzerInitialize(
   26     int* argc HF_ATTR_UNUSED, char*** argv HF_ATTR_UNUSED) {
   27     return 1;
   28 }
   29 
   30 __attribute__((weak)) size_t LLVMFuzzerMutate(
   31     uint8_t* Data HF_ATTR_UNUSED, size_t Size HF_ATTR_UNUSED, size_t MaxSize HF_ATTR_UNUSED) {
   32     LOG_F("LLVMFuzzerMutate() is not supported in honggfuzz yet");
   33     return 0;
   34 }
   35 
   36 __attribute__((weak)) int LLVMFuzzerTestOneInput(
   37     const uint8_t* buf HF_ATTR_UNUSED, size_t len HF_ATTR_UNUSED) {
   38     LOG_F("Define 'int LLVMFuzzerTestOneInput(uint8_t * buf, size_t len)' in your "
   39           "code to make it work");
   40     return 0;
   41 }
   42 
   43 static const uint8_t* inputFile = NULL;
   44 __attribute__((constructor)) static void initializePersistent(void) {
   45     if (fcntl(_HF_INPUT_FD, F_GETFD) == -1 && errno == EBADF) {
   46         return;
   47     }
   48     int mflags = files_getTmpMapFlags(MAP_SHARED, /* nocore= */ false);
   49     if ((inputFile = mmap(NULL, _HF_INPUT_MAX_SIZE, PROT_READ, mflags, _HF_INPUT_FD, 0)) ==
   50         MAP_FAILED) {
   51         PLOG_F("mmap(fd=%d, size=%zu) of the input file failed", _HF_INPUT_FD,
   52             (size_t)_HF_INPUT_MAX_SIZE);
   53     }
   54 }
   55 
   56 void HF_ITER(const uint8_t** buf_ptr, size_t* len_ptr) {
   57     HonggfuzzFetchData(buf_ptr, len_ptr);
   58 }
   59 
   60 extern const char* const LIBHFUZZ_module_memorycmp;
   61 extern const char* const LIBHFUZZ_module_instrument;
   62 static void HonggfuzzRunOneInput(const uint8_t* buf, size_t len) {
   63     instrumentResetLocalCovFeedback();
   64     int ret = LLVMFuzzerTestOneInput(buf, len);
   65     if (ret != 0) {
   66         LOG_D("Dereferenced: %s, %s", LIBHFUZZ_module_memorycmp, LIBHFUZZ_module_instrument);
   67         LOG_F("LLVMFuzzerTestOneInput() returned '%d' instead of '0'", ret);
   68     }
   69     instrument8BitCountersCount();
   70 }
   71 
   72 static void HonggfuzzPersistentLoop(void) {
   73     for (;;) {
   74         size_t len;
   75         const uint8_t* buf;
   76 
   77         /* Check whether the current benchmark is fast enough, or maybe we should restart it */
   78         performanceCheck();
   79 
   80         HonggfuzzFetchData(&buf, &len);
   81         HonggfuzzRunOneInput(buf, len);
   82     }
   83 }
   84 
   85 static int HonggfuzzRunFromFile(int argc, char** argv) {
   86     int in_fd = STDIN_FILENO;
   87     const char* fname = "[STDIN]";
   88     if (argc > 1) {
   89         fname = argv[argc - 1];
   90         if ((in_fd = TEMP_FAILURE_RETRY(open(argv[argc - 1], O_RDONLY))) == -1) {
   91             PLOG_W("Cannot open '%s' as input, using stdin", argv[argc - 1]);
   92             in_fd = STDIN_FILENO;
   93             fname = "[STDIN]";
   94         }
   95     }
   96 
   97     LOG_I("Accepting input from '%s'", fname);
   98     LOG_I("Usage for fuzzing: honggfuzz -P [flags] -- %s", argv[0]);
   99 
  100     uint8_t* buf = (uint8_t*)util_Calloc(_HF_INPUT_MAX_SIZE);
  101     ssize_t len = files_readFromFd(in_fd, buf, _HF_INPUT_MAX_SIZE);
  102     if (len < 0) {
  103         LOG_E("Couldn't read data from stdin: %s", strerror(errno));
  104         free(buf);
  105         return -1;
  106     }
  107 
  108     HonggfuzzRunOneInput(buf, len);
  109     free(buf);
  110     return 0;
  111 }
  112 
  113 int HonggfuzzMain(int argc, char** argv) {
  114     LLVMFuzzerInitialize(&argc, &argv);
  115     instrumentClearNewCov();
  116 
  117     if (!fetchIsInputAvailable()) {
  118         return HonggfuzzRunFromFile(argc, argv);
  119     }
  120 
  121     HonggfuzzPersistentLoop();
  122     return 0;
  123 }
  124 
  125 /*
  126  * Declare it 'weak', so it can be safely linked with regular binaries which
  127  * implement their own main()
  128  */
  129 #if !defined(__CYGWIN__)
  130 __attribute__((weak))
  131 #endif /* !defined(__CYGWIN__) */
  132 int main(int argc, char** argv) {
  133     return HonggfuzzMain(argc, argv);
  134 }