2019-01-17 10:19:54 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/tools/fuzzer_common.h>
|
|
|
|
#include <libdevcore/CommonData.h>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace dev;
|
|
|
|
using namespace dev::eth;
|
|
|
|
|
2019-01-29 16:08:25 +00:00
|
|
|
void FuzzerUtil::runCompiler(string _input, bool _quiet)
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
2019-01-29 16:08:25 +00:00
|
|
|
if (!_quiet)
|
|
|
|
cout << "Input JSON: " << _input << endl;
|
2019-01-17 10:19:54 +00:00
|
|
|
string outputString(solidity_compile(_input.c_str(), nullptr));
|
2019-01-29 16:08:25 +00:00
|
|
|
if (!_quiet)
|
|
|
|
cout << "Output JSON: " << outputString << endl;
|
2019-01-17 10:19:54 +00:00
|
|
|
Json::Value output;
|
|
|
|
if (!jsonParseStrict(outputString, output))
|
|
|
|
{
|
|
|
|
cout << "Compiler produced invalid JSON output." << endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if (output.isMember("errors"))
|
|
|
|
for (auto const& error: output["errors"])
|
|
|
|
{
|
|
|
|
string invalid = findAnyOf(error["type"].asString(), vector<string>{
|
|
|
|
"Exception",
|
|
|
|
"InternalCompilerError"
|
|
|
|
});
|
|
|
|
if (!invalid.empty())
|
|
|
|
{
|
|
|
|
cout << "Invalid error: \"" << error["type"].asString() << "\"" << endl;
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FuzzerUtil::testCompiler(string const& _input, bool _optimize, bool _quiet)
|
|
|
|
{
|
|
|
|
if (!_quiet)
|
|
|
|
cout << "Testing compiler " << (_optimize ? "with" : "without") << " optimizer." << endl;
|
|
|
|
|
|
|
|
Json::Value config = Json::objectValue;
|
|
|
|
config["language"] = "Solidity";
|
|
|
|
config["sources"] = Json::objectValue;
|
|
|
|
config["sources"][""] = Json::objectValue;
|
|
|
|
config["sources"][""]["content"] = _input;
|
|
|
|
config["settings"] = Json::objectValue;
|
|
|
|
config["settings"]["optimizer"] = Json::objectValue;
|
|
|
|
config["settings"]["optimizer"]["enabled"] = _optimize;
|
|
|
|
config["settings"]["optimizer"]["runs"] = 200;
|
|
|
|
|
|
|
|
// Enable all SourceUnit-level outputs.
|
|
|
|
config["settings"]["outputSelection"]["*"][""][0] = "*";
|
|
|
|
// Enable all Contract-level outputs.
|
|
|
|
config["settings"]["outputSelection"]["*"]["*"][0] = "*";
|
|
|
|
|
2019-01-29 16:08:25 +00:00
|
|
|
runCompiler(jsonCompactPrint(config), _quiet);
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FuzzerUtil::testConstantOptimizer(string const& _input, bool _quiet)
|
|
|
|
{
|
|
|
|
if (!_quiet)
|
|
|
|
cout << "Testing constant optimizer" << endl;
|
|
|
|
vector<u256> numbers;
|
|
|
|
stringstream sin(_input);
|
|
|
|
|
|
|
|
while (!sin.eof())
|
|
|
|
{
|
|
|
|
h256 data;
|
|
|
|
sin.read(reinterpret_cast<char *>(data.data()), 32);
|
|
|
|
numbers.push_back(u256(data));
|
|
|
|
}
|
|
|
|
if (!_quiet)
|
|
|
|
cout << "Got " << numbers.size() << " inputs:" << endl;
|
|
|
|
|
|
|
|
Assembly assembly;
|
|
|
|
for (u256 const& n: numbers)
|
|
|
|
{
|
|
|
|
if (!_quiet)
|
|
|
|
cout << n << endl;
|
|
|
|
assembly.append(n);
|
|
|
|
}
|
|
|
|
for (bool isCreation: {false, true})
|
|
|
|
for (unsigned runs: {1, 2, 3, 20, 40, 100, 200, 400, 1000})
|
|
|
|
{
|
2019-01-28 10:40:33 +00:00
|
|
|
// Make a copy here so that each time we start with the original state.
|
|
|
|
Assembly tmp = assembly;
|
2019-01-17 10:19:54 +00:00
|
|
|
ConstantOptimisationMethod::optimiseConstants(
|
|
|
|
isCreation,
|
|
|
|
runs,
|
|
|
|
EVMVersion{},
|
2019-01-28 16:18:49 +00:00
|
|
|
tmp,
|
2019-01-28 10:40:33 +00:00
|
|
|
const_cast<AssemblyItems &>(tmp.items())
|
2019-01-17 10:19:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FuzzerUtil::testStandardCompiler(string const& _input, bool _quiet)
|
|
|
|
{
|
|
|
|
if (!_quiet)
|
|
|
|
cout << "Testing compiler via JSON interface." << endl;
|
|
|
|
|
2019-01-29 16:08:25 +00:00
|
|
|
runCompiler(_input, _quiet);
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|