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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2019-01-17 10:19:54 +00:00
|
|
|
|
|
|
|
#include <test/tools/fuzzer_common.h>
|
2019-01-29 16:14:28 +00:00
|
|
|
|
2021-06-17 15:19:27 +00:00
|
|
|
#include <libsolidity/interface/OptimiserSettings.h>
|
2020-02-28 11:48:59 +00:00
|
|
|
#include <libsolidity/interface/CompilerStack.h>
|
2021-07-02 12:42:53 +00:00
|
|
|
#include <libsolidity/formal/ModelCheckerSettings.h>
|
2020-02-28 11:48:59 +00:00
|
|
|
|
2020-01-06 10:52:23 +00:00
|
|
|
#include <libsolutil/JSON.h>
|
2020-02-28 11:48:59 +00:00
|
|
|
|
2019-01-29 16:14:28 +00:00
|
|
|
#include <libevmasm/Assembly.h>
|
|
|
|
#include <libevmasm/ConstantOptimiser.h>
|
2020-02-28 11:48:59 +00:00
|
|
|
|
2019-01-29 16:14:28 +00:00
|
|
|
#include <libsolc/libsolc.h>
|
|
|
|
|
2020-02-28 11:48:59 +00:00
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
|
2019-01-29 16:14:28 +00:00
|
|
|
#include <sstream>
|
2019-01-17 10:19:54 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::evmasm;
|
2021-06-17 15:19:27 +00:00
|
|
|
using namespace solidity::frontend;
|
2020-02-28 11:48:59 +00:00
|
|
|
using namespace solidity::langutil;
|
2021-06-17 15:19:27 +00:00
|
|
|
using namespace solidity::util;
|
2019-01-17 10:19:54 +00:00
|
|
|
|
2020-02-28 11:48:59 +00:00
|
|
|
static vector<EVMVersion> s_evmVersions = {
|
|
|
|
EVMVersion::homestead(),
|
|
|
|
EVMVersion::tangerineWhistle(),
|
|
|
|
EVMVersion::spuriousDragon(),
|
|
|
|
EVMVersion::byzantium(),
|
|
|
|
EVMVersion::constantinople(),
|
|
|
|
EVMVersion::petersburg(),
|
|
|
|
EVMVersion::istanbul(),
|
2022-11-09 11:12:18 +00:00
|
|
|
EVMVersion::berlin(),
|
|
|
|
EVMVersion::london(),
|
|
|
|
EVMVersion::paris()
|
2019-09-17 11:43:22 +00:00
|
|
|
};
|
|
|
|
|
2020-02-28 11:48:59 +00:00
|
|
|
void FuzzerUtil::testCompilerJsonInterface(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;
|
2021-06-17 15:19:27 +00:00
|
|
|
config["settings"]["optimizer"]["runs"] = static_cast<int>(OptimiserSettings{}.expectedExecutionsPerDeployment);
|
2020-02-28 11:48:59 +00:00
|
|
|
config["settings"]["evmVersion"] = "berlin";
|
|
|
|
|
|
|
|
// Enable all SourceUnit-level outputs.
|
|
|
|
config["settings"]["outputSelection"]["*"][""][0] = "*";
|
|
|
|
// Enable all Contract-level outputs.
|
|
|
|
config["settings"]["outputSelection"]["*"]["*"][0] = "*";
|
|
|
|
|
|
|
|
runCompiler(jsonCompactPrint(config), _quiet);
|
|
|
|
}
|
|
|
|
|
2020-12-21 09:33:02 +00:00
|
|
|
void FuzzerUtil::forceSMT(StringMap& _input)
|
|
|
|
{
|
|
|
|
// Add SMT checker pragma if not already present in source
|
2021-08-14 18:29:59 +00:00
|
|
|
static auto constexpr smtPragma = "pragma experimental SMTChecker;";
|
2020-12-21 09:33:02 +00:00
|
|
|
for (auto &sourceUnit: _input)
|
|
|
|
if (sourceUnit.second.find(smtPragma) == string::npos)
|
|
|
|
sourceUnit.second += smtPragma;
|
|
|
|
}
|
|
|
|
|
2021-02-05 11:29:52 +00:00
|
|
|
void FuzzerUtil::testCompiler(
|
|
|
|
StringMap& _input,
|
|
|
|
bool _optimize,
|
|
|
|
unsigned _rand,
|
|
|
|
bool _forceSMT,
|
|
|
|
bool _compileViaYul
|
|
|
|
)
|
2020-02-28 11:48:59 +00:00
|
|
|
{
|
|
|
|
frontend::CompilerStack compiler;
|
2020-07-06 17:32:48 +00:00
|
|
|
EVMVersion evmVersion = s_evmVersions[_rand % s_evmVersions.size()];
|
2020-02-28 11:48:59 +00:00
|
|
|
frontend::OptimiserSettings optimiserSettings;
|
|
|
|
if (_optimize)
|
|
|
|
optimiserSettings = frontend::OptimiserSettings::standard();
|
|
|
|
else
|
|
|
|
optimiserSettings = frontend::OptimiserSettings::minimal();
|
2020-12-21 09:33:02 +00:00
|
|
|
if (_forceSMT)
|
|
|
|
{
|
|
|
|
forceSMT(_input);
|
2021-04-15 10:31:45 +00:00
|
|
|
compiler.setModelCheckerSettings({
|
2023-02-23 18:22:46 +00:00
|
|
|
/*bmcLoopIterations*/1,
|
2021-04-15 10:31:45 +00:00
|
|
|
frontend::ModelCheckerContracts::Default(),
|
2021-07-15 15:39:01 +00:00
|
|
|
/*divModWithSlacks*/true,
|
2021-04-15 10:31:45 +00:00
|
|
|
frontend::ModelCheckerEngine::All(),
|
2021-10-12 09:12:18 +00:00
|
|
|
frontend::ModelCheckerExtCalls{},
|
2021-10-06 09:52:44 +00:00
|
|
|
frontend::ModelCheckerInvariants::All(),
|
2023-02-09 16:07:13 +00:00
|
|
|
/*showProvedSafe=*/false,
|
2021-07-02 12:42:53 +00:00
|
|
|
/*showUnproved=*/false,
|
2023-03-06 13:19:58 +00:00
|
|
|
/*showUnsupported=*/false,
|
2021-05-19 15:35:19 +00:00
|
|
|
smtutil::SMTSolverChoice::All(),
|
2021-04-15 10:31:45 +00:00
|
|
|
frontend::ModelCheckerTargets::Default(),
|
|
|
|
/*timeout=*/1
|
|
|
|
});
|
2020-12-21 09:33:02 +00:00
|
|
|
}
|
2020-07-06 17:32:48 +00:00
|
|
|
compiler.setSources(_input);
|
2020-02-28 11:48:59 +00:00
|
|
|
compiler.setEVMVersion(evmVersion);
|
|
|
|
compiler.setOptimiserSettings(optimiserSettings);
|
2023-05-05 09:39:56 +00:00
|
|
|
compiler.setViaIR(_compileViaYul);
|
2020-02-28 11:48:59 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
compiler.compile();
|
|
|
|
}
|
|
|
|
catch (Error const&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (FatalError const&)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
catch (UnimplementedFeatureError const&)
|
|
|
|
{
|
|
|
|
}
|
2020-07-01 07:46:06 +00:00
|
|
|
catch (StackTooDeepError const&)
|
|
|
|
{
|
2023-05-05 09:39:56 +00:00
|
|
|
if (_optimize && _compileViaYul)
|
|
|
|
throw;
|
2020-07-01 07:46:06 +00:00
|
|
|
}
|
2020-02-28 11:48:59 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:09:09 +00:00
|
|
|
void FuzzerUtil::runCompiler(string const& _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-10-29 19:00:58 +00:00
|
|
|
string outputString(solidity_compile(_input.c_str(), nullptr, nullptr));
|
2019-01-29 16:08:25 +00:00
|
|
|
if (!_quiet)
|
|
|
|
cout << "Output JSON: " << outputString << endl;
|
2019-01-22 11:41:21 +00:00
|
|
|
|
|
|
|
// This should be safe given the above copies the output.
|
2019-12-04 15:36:56 +00:00
|
|
|
solidity_reset();
|
2019-01-22 11:41:21 +00:00
|
|
|
|
2019-01-17 10:19:54 +00:00
|
|
|
Json::Value output;
|
|
|
|
if (!jsonParseStrict(outputString, output))
|
|
|
|
{
|
2019-04-04 12:03:32 +00:00
|
|
|
string msg{"Compiler produced invalid JSON output."};
|
|
|
|
cout << msg << endl;
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error(std::move(msg)));
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
if (output.isMember("errors"))
|
|
|
|
for (auto const& error: output["errors"])
|
|
|
|
{
|
|
|
|
string invalid = findAnyOf(error["type"].asString(), vector<string>{
|
|
|
|
"Exception",
|
|
|
|
"InternalCompilerError"
|
|
|
|
});
|
|
|
|
if (!invalid.empty())
|
|
|
|
{
|
2019-04-04 12:03:32 +00:00
|
|
|
string msg = "Invalid error: \"" + error["type"].asString() + "\"";
|
|
|
|
cout << msg << endl;
|
2021-02-18 23:49:34 +00:00
|
|
|
BOOST_THROW_EXCEPTION(std::runtime_error(std::move(msg)));
|
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;
|
|
|
|
|
|
|
|
for (bool isCreation: {false, true})
|
2022-03-07 11:49:23 +00:00
|
|
|
{
|
2022-11-23 10:51:34 +00:00
|
|
|
Assembly assembly{langutil::EVMVersion{}, isCreation, {}};
|
2022-03-07 11:49:23 +00:00
|
|
|
for (u256 const& n: numbers)
|
|
|
|
{
|
|
|
|
if (!_quiet)
|
|
|
|
cout << n << endl;
|
|
|
|
assembly.append(n);
|
|
|
|
}
|
2020-06-03 08:27:23 +00:00
|
|
|
for (unsigned runs: {1u, 2u, 3u, 20u, 40u, 100u, 200u, 400u, 1000u})
|
2019-01-17 10:19:54 +00:00
|
|
|
{
|
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(
|
2022-03-07 11:49:23 +00:00
|
|
|
isCreation,
|
|
|
|
runs,
|
|
|
|
langutil::EVMVersion{},
|
|
|
|
tmp
|
2019-01-17 10:19:54 +00:00
|
|
|
);
|
|
|
|
}
|
2022-03-07 11:49:23 +00:00
|
|
|
}
|
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
|
|
|
}
|