ntopng/fuzz/onefile.cpp
Riccardo Mori 151a047760
Add first fuzzing harness (#7430)
* First sketch of fuzzing

* Add m4 script

The script is used in the fuzzing build

* Add stub sources in the makefile

* [Fuzz] Add RedisStub

* Add gitignore for fuzz dir

* Remove definition of non-implemented method

* [Fuzz] Refactoring code

* [Fuzz] Separate the protobuf support in the makefile

* Clean fuzzing related object files

* [Fuzz] Fix makefile

* Change gitignore

* [Fuzz] Separate headers

* [Fuzz] Add README.md

* [Fuzz] Change make target

* [Fuzz] Update README.md

* Add compatibilty with autoconf < 2.71

* Optionally disable hiredis integration

* Include hiredis only for production build

* [Fuzz] Disable period activities

* Remove unused dependencies for librrd

* Optionally use static linking for zmq library

* Add debug output regarding the linking of libzmq

* [Fuzz] Do not run on empty input

* Optionally use static linking for libjson-c

* Optionally use static linking for libmaxminddb

* Make mysqlclient dependency as optional

* Change gitignore

* [Fuzz] Add corpus for fuzz_dissect_packet

* [Fuzz] Refactor fuzz_dissect_packet

* Change gitignore

* [Fuzz] Use correct naming for corpus

* [Fuzz] Add dictionary

* [Fuzz] Fix declaration of LLVMFuzzerInitialize

* [Fuzz] Refactor onefile

* [Fuzz] Fix the initialization memory leaks

* [Fuzz] Fix invocation of LLVMFuzzerInitialize

* Remove double githooks folder

* [Fuzz] Set interface pcap_data_link

* Change gitignore

* Use pkg-config for detecting protobuf libraries

* Add license

* Improve error message
2023-05-02 16:06:52 +02:00

61 lines
No EOL
1.2 KiB
C++

#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
__attribute__((weak)) extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv);
#ifdef IS_AFL
__AFL_FUZZ_INIT();
int main(int argc, char *argv[]) {
#ifdef __AFL_HAVE_MANUAL_CONTROL
__AFL_INIT();
#endif
uint8_t *buf = __AFL_FUZZ_TESTCASE_BUF;
if (LLVMFuzzerInitialize)
LLVMFuzzerInitialize(&argc, &argv);
while (__AFL_LOOP(10000)) {
int len = __AFL_FUZZ_TESTCASE_LEN;
LLVMFuzzerTestOneInput(buf, len);
}
return 0;
}
#else
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Error! Must specificy a input file\n");
return 1;
}
if (LLVMFuzzerInitialize)
LLVMFuzzerInitialize(&argc, &argv);
FILE *f = fopen(argv[1], "r");
if (!f) return 1;
fseek(f, 0, SEEK_END);
size_t fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
uint8_t *string = (uint8_t *)malloc(fsize + 1);
fread(string, fsize, 1, f);
fclose(f);
LLVMFuzzerTestOneInput(string, fsize);
free(string);
return 0;
}
#endif