Coverage Report

Created: 2025-12-08 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/kea/fuzz/fuzz.cc
Line
Count
Source
1
// Copyright (C) 2024-2025 Internet Systems Consortium, Inc. ("ISC")
2
//
3
// This Source Code Form is subject to the terms of the Mozilla Public
4
// License, v. 2.0. If a copy of the MPL was not distributed with this
5
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7
#include <config.h>
8
9
#include <fuzz.h>
10
11
#include <log/logger_support.h>
12
#include <process/daemon.h>
13
#include <util/filesystem.h>
14
#include <util/encode/encode.h>
15
16
#include <cassert>
17
#include <string>
18
19
using namespace isc::process;
20
using namespace isc::util::encode;
21
using namespace isc::util::file;
22
using namespace std;
23
24
52
string KEA_FUZZ_DIR() {
25
52
    static TemporaryDirectory TEMP_DIR = TemporaryDirectory();
26
52
    return TEMP_DIR.dirName();
27
52
}
28
29
extern "C" {
30
31
bool
32
14
DoInitialization() {
33
14
    LLVMFuzzerTearDown();
34
35
    // Spoof the logger just enough to not get LoggingNotInitialized thrown.
36
    // We explicitly don't want any logging during fuzzing for performance reasons.
37
14
    setenv("KEA_LOCKFILE_DIR", KEA_FUZZ_DIR().c_str(), 0);
38
14
    setenv("KEA_LFC_EXECUTABLE", "/bin/true", 0);
39
14
    if (!getenv("DEBUG")) {
40
14
        setenv("KEA_LOGGER_DESTINATION", "/dev/null", 0);
41
14
    }
42
14
    setenv("KEA_PIDFILE_DIR", KEA_FUZZ_DIR().c_str(), 0);
43
14
    if (!isc::log::isLoggingInitialized()) {
44
14
        isc::log::initLogger("fuzzer");
45
14
        Daemon::loggerInit("fuzzer", /* verbose = */ false);
46
14
        Daemon::setDefaultLoggerName("fuzzer");
47
14
    }
48
49
14
    return true;
50
14
}
51
52
2.49k
void writeToFile(string const& file, string const& content) {
53
    // Create the config file.
54
2.49k
    ofstream out(file, ios::out | ios::trunc);
55
2.49k
    assert(out.is_open());
56
2.49k
    out << content;
57
2.49k
    out.close();
58
2.49k
    assert(!out.is_open());
59
2.49k
}
60
61
696
bool byteStreamToPacketData(uint8_t const* data, size_t size, vector<uint8_t>& byte_stream) {
62
696
    string str(data, data + size);
63
696
    if (!str.empty() && str.at(str.size() - 1) == '\n') {
64
147
        str = str.substr(0, str.size() - 1);
65
147
    }
66
696
    if (str.find_first_not_of("0123456789abcdefABCDEF") != string::npos) {
67
266
        return false;
68
266
    }
69
430
    if (str.size() % 2) {
70
25
        return false;
71
25
    }
72
405
    decodeHex(str, byte_stream);
73
405
    return true;
74
430
}
75
76
}  // extern "C"