-
Notifications
You must be signed in to change notification settings - Fork 6k
/
Copy pathafl_fuzzer.cpp
144 lines (122 loc) · 3.59 KB
/
afl_fuzzer.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
* Executable for use with AFL <http://lcamtuf.coredump.cx/afl>.
*/
#include <test/tools/fuzzer_common.h>
#include <libsolutil/CommonIO.h>
#include <boost/program_options.hpp>
#include <string>
#include <iostream>
using namespace solidity;
using namespace solidity::util;
namespace po = boost::program_options;
int main(int argc, char** argv)
{
po::options_description options(
R"(solfuzzer, fuzz-testing binary for use with AFL.
Usage: solfuzzer [Options] < input
Reads a single source from stdin, compiles it and signals a failure for internal errors.
Allowed options)",
po::options_description::m_default_line_length,
po::options_description::m_default_line_length - 23);
options.add_options()
("help", "Show this help screen.")
("quiet", "Only output errors.")
(
"standard-json",
"Test via the standard-json interface, i.e. "
"input is expected to be JSON-encoded instead of "
"plain source file."
)
(
"const-opt",
"Run the constant optimizer instead of compiling. "
"Expects a binary string of up to 32 bytes on stdin."
)
(
"input-file",
po::value<std::string>(),
"input file"
)(
"input-files",
po::value<std::vector<std::string>>()->multitoken(),
"input files"
)
(
"without-optimizer",
"Run without optimizations. Cannot be used together with standard-json."
);
// All positional options should be interpreted as input files
po::positional_options_description filesPositions;
filesPositions.add("input-file", 1);
bool quiet = false;
po::variables_map arguments;
try
{
po::command_line_parser cmdLineParser(argc, argv);
cmdLineParser.options(options).positional(filesPositions);
po::store(cmdLineParser.run(), arguments);
}
catch (po::error const& _exception)
{
std::cerr << _exception.what() << std::endl;
return 1;
}
if (arguments.count("quiet"))
quiet = true;
if (arguments.count("help"))
{
std::cout << options;
return 0;
}
std::vector<std::string> inputs;
if (arguments.count("input-file"))
inputs.push_back(arguments["input-file"].as<std::string>());
else if (arguments.count("input-files"))
inputs = arguments["input-files"].as<std::vector<std::string>>();
else
inputs.emplace_back("");
bool optimize = !arguments.count("without-optimizer");
int retResult = 0;
for (std::string const& inputFile: inputs)
{
std::string input;
if (inputFile.size() == 0)
input = readUntilEnd(std::cin);
else
input = readFileAsString(inputFile);
try
{
if (arguments.count("const-opt"))
FuzzerUtil::testConstantOptimizer(input, quiet);
else if (arguments.count("standard-json"))
FuzzerUtil::testStandardCompiler(input, quiet);
else
FuzzerUtil::testCompilerJsonInterface(input, optimize, quiet);
}
catch (...)
{
retResult = 1;
if (inputFile.size() == 0)
throw;
std::cerr << "Fuzzer "
<< (optimize ? "" : "(without optimizer) ")
<< "failed on "
<< inputFile;
}
}
return retResult;
}