Add constant optimizer testing to the fuzz tester.

This commit is contained in:
chriseth 2017-04-06 16:32:20 +02:00 committed by Alex Beregszaszi
parent 2c8b770062
commit 99dd28d63e
3 changed files with 87 additions and 3 deletions

View File

@ -182,6 +182,7 @@ AssemblyItems const& CodeCopyMethod::copyRoutine() const
AssemblyItems ComputeMethod::findRepresentation(u256 const& _value)
{
cout << "Looking for " << _value << endl;
if (_value < 0x10000)
// Very small value, not worth computing
return AssemblyItems{_value};

View File

@ -21,4 +21,4 @@ include_directories(BEFORE ..)
target_link_libraries(${EXECUTABLE} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})
add_executable(solfuzzer fuzzer.cpp)
target_link_libraries(solfuzzer soljson)
target_link_libraries(solfuzzer soljson ${Boost_PROGRAM_OPTIONS_LIBRARIES})

View File

@ -16,15 +16,22 @@
*/
/**
* Executable for use with AFL <http://lcamtuf.coredump.cx/afl>.
* Reads a single source from stdin and signals a failure for internal errors.
*/
#include <libevmasm/Assembly.h>
#include <libevmasm/ConstantOptimiser.h>
#include <json/json.h>
#include <boost/program_options.hpp>
#include <string>
#include <iostream>
using namespace std;
using namespace dev;
using namespace dev::eth;
namespace po = boost::program_options;
extern "C"
{
@ -39,8 +46,41 @@ string contains(string const& _haystack, vector<string> const& _needles)
return "";
}
int main()
void testConstantOptimizer()
{
cout << "Testing constant optimizer" << endl;
vector<u256> numbers;
while (!cin.eof())
{
h256 data;
cin.read(reinterpret_cast<char*>(data.data()), 32);
numbers.push_back(u256(data));
}
cout << "Got " << numbers.size() << " inputs:" << endl;
Assembly assembly;
for (u256 const& n: numbers)
{
cout << n << endl;
assembly.append(n);
}
for (bool isCreation: {false, true})
{
for (unsigned runs: {1, 2, 3, 20, 40, 100, 200, 400, 1000})
{
ConstantOptimisationMethod::optimiseConstants(
isCreation,
runs,
assembly,
const_cast<AssemblyItems&>(assembly.items())
);
}
}
}
void testCompiler()
{
cout << "Testing compiler." << endl;
string input;
while (!cin.eof())
{
@ -87,5 +127,48 @@ int main()
cout << "Output JSON has neither \"errors\" nor \"contracts\"." << endl;
abort();
}
}
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."
)
(
"const-opt",
"Only run the constant optimizer. "
"Expects a binary string of up to 32 bytes on stdin."
);
po::variables_map arguments;
try
{
po::command_line_parser cmdLineParser(argc, argv);
cmdLineParser.options(options);
po::store(cmdLineParser.run(), arguments);
}
catch (po::error const& _exception)
{
cerr << _exception.what() << endl;
return false;
}
if (arguments.count("help"))
cout << options;
else if (arguments.count("const-opt"))
testConstantOptimizer();
else
testCompiler();
return 0;
}