Changeset View
Changeset View
Standalone View
Standalone View
src/test/fuzz/fuzz.cpp
// Copyright (c) 2009-2019 The Bitcoin Core developers | // Copyright (c) 2009-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <test/fuzz/fuzz.h> | #include <test/fuzz/fuzz.h> | ||||
#include <pubkey.h> | #include <cstdint> | ||||
#include <memory> | |||||
#include <unistd.h> | #include <unistd.h> | ||||
#include <vector> | |||||
static bool read_stdin(std::vector<uint8_t> &data) { | static bool read_stdin(std::vector<uint8_t> &data) { | ||||
uint8_t buffer[1024]; | uint8_t buffer[1024]; | ||||
ssize_t length = 0; | ssize_t length = 0; | ||||
while ((length = read(STDIN_FILENO, buffer, 1024)) > 0) { | while ((length = read(STDIN_FILENO, buffer, 1024)) > 0) { | ||||
data.insert(data.end(), buffer, buffer + length); | data.insert(data.end(), buffer, buffer + length); | ||||
if (data.size() > (1 << 20)) { | if (data.size() > (1 << 20)) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
return length == 0; | return length == 0; | ||||
} | } | ||||
// Default initialization: Override using a non-weak initialize(). | // Default initialization: Override using a non-weak initialize(). | ||||
__attribute__((weak)) void initialize() { | __attribute__((weak)) void initialize() {} | ||||
const static auto verify_handle = std::make_unique<ECCVerifyHandle>(); | |||||
} | |||||
// This function is used by libFuzzer | // This function is used by libFuzzer | ||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { | ||||
const std::vector<uint8_t> input(data, data + size); | const std::vector<uint8_t> input(data, data + size); | ||||
test_one_input(input); | test_one_input(input); | ||||
return 0; | return 0; | ||||
} | } | ||||
// This function is used by libFuzzer | // This function is used by libFuzzer | ||||
extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { | extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) { | ||||
initialize(); | initialize(); | ||||
return 0; | return 0; | ||||
} | } | ||||
// Disabled under WIN32 due to clash with Cygwin's WinMain. | |||||
#ifndef WIN32 | |||||
// Declare main(...) "weak" to allow for libFuzzer linking. libFuzzer provides | // Declare main(...) "weak" to allow for libFuzzer linking. libFuzzer provides | ||||
// the main(...) function. | // the main(...) function. | ||||
__attribute__((weak)) | __attribute__((weak)) int main(int argc, char **argv) { | ||||
#endif | |||||
int main(int argc, char **argv) { | |||||
initialize(); | initialize(); | ||||
#ifdef __AFL_INIT | #ifdef __AFL_INIT | ||||
// Enable AFL deferred forkserver mode. Requires compilation using | // Enable AFL deferred forkserver mode. Requires compilation using | ||||
// afl-clang-fast++. See fuzzing.md for details. | // afl-clang-fast++. See fuzzing.md for details. | ||||
__AFL_INIT(); | __AFL_INIT(); | ||||
#endif | #endif | ||||
#ifdef __AFL_LOOP | #ifdef __AFL_LOOP | ||||
Show All 18 Lines |