-
Notifications
You must be signed in to change notification settings - Fork 12
/
driver.cpp
96 lines (86 loc) · 3.28 KB
/
driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <cassert>
#include <set>
#include <algorithm>
#include <unistd.h>
#include <string>
#include <span>
#include <fuzzer/FuzzedDataProvider.h>
#include "driver.h"
#include <bitcoinfuzz/basemodule.h>
namespace bitcoinfuzz
{
void Driver::LoadModule(std::shared_ptr<BaseModule> module)
{
modules[module->name] = module;
}
void Driver::ScriptTarget(std::span<const uint8_t> buffer) const
{
std::optional<bool> last_response{std::nullopt};
for (auto& module : modules)
{
std::optional<bool> res{module.second->script_parse(buffer)};
if (!res.has_value()) continue;
if (last_response.has_value()) assert(*res == *last_response);
last_response = *res;
}
}
void Driver::BlockDeserializationTarget(std::span<const uint8_t> buffer) const
{
std::optional<std::vector<bool>> last_response{std::nullopt};
for (auto& module : modules)
{
std::optional<std::vector<bool>> res{module.second->deserialize_block(buffer)};
if (!res.has_value() || res->empty()) continue;
if (last_response.has_value()) assert(*res == *last_response);
last_response = res.value();
}
}
void Driver::ScriptEvalTarget(std::span<const uint8_t> buffer) const
{
FuzzedDataProvider provider(buffer.data(), buffer.size());
std::vector<uint8_t> input_data = provider.ConsumeBytes<uint8_t>(
provider.ConsumeIntegralInRange<size_t>(0, 1024)
);
auto flags = provider.ConsumeIntegral<unsigned int>();
auto version = provider.PickValueInArray({0,1});
std::optional<bool> last_response{std::nullopt};
for (auto& module : modules)
{
std::optional<bool> res{module.second->script_eval(input_data, flags, version)};
if (!res.has_value()) continue;
if (last_response.has_value()) assert(*res == *last_response);
last_response = *res;
}
}
void Driver::DescriptorParseTarget(std::span<const uint8_t> buffer) const
{
FuzzedDataProvider provider(buffer.data(), buffer.size());
std::string desc{provider.ConsumeRemainingBytesAsString()};
std::optional<bool> last_response{std::nullopt};
for (auto& module : modules)
{
std::optional<bool> res{module.second->descriptor_parse(desc)};
if (!res.has_value()) continue;
std::cout << module.first << std::endl;
std::cout << *res << std::endl;
if (last_response.has_value()) assert(*res == *last_response);
last_response = *res;
}
}
void Driver::Run(const uint8_t *data, const size_t size, const std::string &target) const
{
std::span<const uint8_t> buffer{data, size};
if (target == "script") {
this->ScriptTarget(buffer);
} else if (target == "block_deserialization") {
this->BlockDeserializationTarget(buffer);
} else if (target == "script_eval") {
this->ScriptEvalTarget(buffer);
} else if (target == "descriptor_parse") {
this->DescriptorParseTarget(buffer);
} else {
std::cout << "Target not defined!" << std::endl;
assert(false);
}
};
}