2020-08-27 17:07:11 +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/>.
|
|
|
|
*/
|
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
|
|
|
|
|
|
|
#include <test/tools/ossfuzz/SolidityGenerator.h>
|
|
|
|
|
|
|
|
#include <libsolutil/Whiskers.h>
|
2021-01-27 13:55:30 +00:00
|
|
|
#include <libsolutil/Visitor.h>
|
2020-08-27 17:07:11 +00:00
|
|
|
|
|
|
|
using namespace solidity::test::fuzzer;
|
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace std;
|
2021-01-21 16:53:22 +00:00
|
|
|
|
|
|
|
GeneratorBase::GeneratorBase(std::shared_ptr<SolidityGenerator> _mutator)
|
|
|
|
{
|
|
|
|
mutator = std::move(_mutator);
|
2021-01-27 13:55:30 +00:00
|
|
|
state = mutator->testState();
|
2021-01-27 13:55:30 +00:00
|
|
|
uRandDist = mutator->uniformRandomDist();
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string GeneratorBase::visitChildren()
|
|
|
|
{
|
|
|
|
ostringstream os;
|
|
|
|
// Randomise visit order
|
|
|
|
vector<GeneratorPtr> randomisedChildren;
|
2021-01-27 13:55:30 +00:00
|
|
|
for (auto const& child: generators)
|
2021-01-21 16:53:22 +00:00
|
|
|
randomisedChildren.push_back(child);
|
2021-01-27 13:55:30 +00:00
|
|
|
shuffle(randomisedChildren.begin(), randomisedChildren.end(), *uRandDist->randomEngine);
|
2021-01-21 16:53:22 +00:00
|
|
|
for (auto child: randomisedChildren)
|
2021-01-27 13:55:30 +00:00
|
|
|
os << std::visit(GenericVisitor{
|
|
|
|
[&](auto const& _item) { return _item->generate(); }
|
|
|
|
}, child);
|
2021-01-21 16:53:22 +00:00
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2021-01-27 13:55:30 +00:00
|
|
|
string TestState::randomPath(set<string> const& _sourceUnitPaths) const
|
|
|
|
{
|
|
|
|
auto it = _sourceUnitPaths.begin();
|
|
|
|
/// Advance iterator by n where 0 <= n <= sourceUnitPaths.size() - 1
|
2021-01-27 13:55:30 +00:00
|
|
|
size_t increment = uRandDist->distributionOneToN(_sourceUnitPaths.size()) - 1;
|
2021-01-27 13:55:30 +00:00
|
|
|
solAssert(
|
|
|
|
increment >= 0 && increment < _sourceUnitPaths.size(),
|
|
|
|
"Solc custom mutator: Invalid increment"
|
|
|
|
);
|
|
|
|
advance(it, increment);
|
|
|
|
return *it;
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestState::randomPath() const
|
|
|
|
{
|
|
|
|
solAssert(!empty(), "Solc custom mutator: Null test state");
|
|
|
|
return randomPath(sourceUnitPaths);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestState::print(std::ostream& _os) const
|
|
|
|
{
|
|
|
|
_os << "Printing test state" << std::endl;
|
|
|
|
for (auto const& item: sourceUnitPaths)
|
|
|
|
_os << "Source path: " << item << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
string TestState::randomNonCurrentPath() const
|
|
|
|
{
|
|
|
|
/// To obtain a source path that is not the currently visited
|
|
|
|
/// source unit itself, we require at least one other source
|
|
|
|
/// unit to be previously visited.
|
|
|
|
solAssert(size() >= 2, "Solc custom mutator: Invalid test state");
|
|
|
|
|
|
|
|
set<string> filteredSourcePaths;
|
|
|
|
string currentPath = currentSourceUnitPath;
|
|
|
|
copy_if(
|
|
|
|
sourceUnitPaths.begin(),
|
|
|
|
sourceUnitPaths.end(),
|
|
|
|
inserter(filteredSourcePaths, filteredSourcePaths.begin()),
|
|
|
|
[currentPath](string const& _item) {
|
|
|
|
return _item != currentPath;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return randomPath(filteredSourcePaths);
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:53:22 +00:00
|
|
|
void TestCaseGenerator::setup()
|
|
|
|
{
|
2021-01-25 13:01:43 +00:00
|
|
|
addGenerators({
|
|
|
|
mutator->generator<SourceUnitGenerator>()
|
|
|
|
});
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string TestCaseGenerator::visit()
|
|
|
|
{
|
|
|
|
ostringstream os;
|
2021-01-27 13:55:30 +00:00
|
|
|
for (unsigned i = 0; i < uRandDist->distributionOneToN(s_maxSourceUnits); i++)
|
2021-01-21 16:53:22 +00:00
|
|
|
{
|
|
|
|
string sourcePath = path();
|
|
|
|
os << "\n"
|
|
|
|
<< "==== Source: "
|
|
|
|
<< sourcePath
|
|
|
|
<< " ===="
|
|
|
|
<< "\n";
|
2021-01-27 13:55:30 +00:00
|
|
|
updateSourcePath(sourcePath);
|
2021-01-21 16:53:22 +00:00
|
|
|
m_numSourceUnits++;
|
|
|
|
os << visitChildren();
|
|
|
|
}
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceUnitGenerator::setup()
|
|
|
|
{
|
2021-01-25 13:01:43 +00:00
|
|
|
addGenerators({
|
2021-01-27 13:55:30 +00:00
|
|
|
mutator->generator<ImportGenerator>(),
|
|
|
|
mutator->generator<PragmaGenerator>()
|
2021-01-25 13:01:43 +00:00
|
|
|
});
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string SourceUnitGenerator::visit()
|
|
|
|
{
|
2021-01-26 15:11:18 +00:00
|
|
|
return visitChildren();
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string PragmaGenerator::visit()
|
|
|
|
{
|
|
|
|
static constexpr const char* preamble = R"(
|
|
|
|
pragma solidity >= 0.0.0;
|
|
|
|
pragma experimental SMTChecker;
|
|
|
|
)";
|
|
|
|
// Choose equally at random from coder v1 and v2
|
|
|
|
string abiPragma = "pragma abicoder v" +
|
2021-01-27 13:55:30 +00:00
|
|
|
to_string(uRandDist->distributionOneToN(2)) +
|
2021-01-21 16:53:22 +00:00
|
|
|
";\n";
|
|
|
|
return preamble + abiPragma;
|
|
|
|
}
|
|
|
|
|
2021-01-27 13:55:30 +00:00
|
|
|
string ImportGenerator::visit()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Case 1: No source units defined
|
|
|
|
* Case 2: One source unit defined
|
|
|
|
* Case 3: At least two source units defined
|
|
|
|
*/
|
|
|
|
ostringstream os;
|
|
|
|
// Self import with a small probability only if
|
|
|
|
// there is one source unit present in test.
|
|
|
|
if (state->size() == 1)
|
|
|
|
{
|
2021-01-27 13:55:30 +00:00
|
|
|
if (uRandDist->probable(s_selfImportInvProb))
|
2021-01-27 13:55:30 +00:00
|
|
|
os << "import "
|
|
|
|
<< "\""
|
|
|
|
<< state->randomPath()
|
|
|
|
<< "\";";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Import a different source unit if at least
|
|
|
|
// two source units available.
|
|
|
|
os << "import "
|
|
|
|
<< "\""
|
|
|
|
<< state->randomNonCurrentPath()
|
|
|
|
<< "\";";
|
|
|
|
}
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2021-01-21 16:53:22 +00:00
|
|
|
template <typename T>
|
|
|
|
shared_ptr<T> SolidityGenerator::generator()
|
|
|
|
{
|
|
|
|
for (auto& g: m_generators)
|
|
|
|
if (holds_alternative<shared_ptr<T>>(g))
|
|
|
|
return get<shared_ptr<T>>(g);
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
SolidityGenerator::SolidityGenerator(unsigned _seed)
|
|
|
|
{
|
|
|
|
m_generators = {};
|
2021-01-27 13:55:30 +00:00
|
|
|
m_urd = make_shared<UniformRandomDistribution>(make_unique<RandomEngine>(_seed));
|
|
|
|
m_state = make_shared<TestState>(m_urd);
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t I>
|
|
|
|
void SolidityGenerator::createGenerators()
|
|
|
|
{
|
|
|
|
if constexpr (I < std::variant_size_v<Generator>)
|
|
|
|
{
|
|
|
|
createGenerator<std::variant_alternative_t<I, Generator>>();
|
|
|
|
createGenerators<I + 1>();
|
|
|
|
}
|
|
|
|
}
|
2020-08-27 17:07:11 +00:00
|
|
|
|
|
|
|
string SolidityGenerator::generateTestProgram()
|
|
|
|
{
|
2021-01-21 16:53:22 +00:00
|
|
|
createGenerators();
|
|
|
|
for (auto& g: m_generators)
|
2021-01-27 13:55:30 +00:00
|
|
|
std::visit(GenericVisitor{
|
|
|
|
[&](auto const& _item) { return _item->setup(); }
|
|
|
|
}, g);
|
2021-01-26 15:11:18 +00:00
|
|
|
string program = generator<TestCaseGenerator>()->generate();
|
2021-01-21 16:53:22 +00:00
|
|
|
destroyGenerators();
|
|
|
|
return program;
|
2020-12-08 20:06:10 +00:00
|
|
|
}
|