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
|
|
|
|
/**
|
|
|
|
* Implements generators for synthesizing mostly syntactically valid
|
|
|
|
* Solidity test programs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-01-21 16:53:22 +00:00
|
|
|
#include <test/tools/ossfuzz/Generators.h>
|
|
|
|
|
|
|
|
#include <liblangutil/Exceptions.h>
|
|
|
|
|
|
|
|
#include <memory>
|
2020-08-27 17:07:11 +00:00
|
|
|
#include <random>
|
2021-01-21 16:53:22 +00:00
|
|
|
#include <set>
|
|
|
|
#include <variant>
|
2020-08-27 17:07:11 +00:00
|
|
|
|
2021-02-02 14:23:02 +00:00
|
|
|
namespace solidity::test::fuzzer::mutator
|
2020-08-27 17:07:11 +00:00
|
|
|
{
|
2021-01-21 16:53:22 +00:00
|
|
|
/// Forward declarations
|
|
|
|
class SolidityGenerator;
|
|
|
|
|
|
|
|
/// Type declarations
|
|
|
|
#define SEMICOLON() ;
|
|
|
|
#define FORWARDDECLAREGENERATORS(G) class G
|
|
|
|
GENERATORLIST(FORWARDDECLAREGENERATORS, SEMICOLON(), SEMICOLON())
|
|
|
|
#undef FORWARDDECLAREGENERATORS
|
|
|
|
#undef SEMICOLON
|
|
|
|
|
|
|
|
#define COMMA() ,
|
|
|
|
using GeneratorPtr = std::variant<
|
|
|
|
#define VARIANTOFSHARED(G) std::shared_ptr<G>
|
|
|
|
GENERATORLIST(VARIANTOFSHARED, COMMA(), )
|
|
|
|
>;
|
|
|
|
#undef VARIANTOFSHARED
|
|
|
|
using Generator = std::variant<
|
|
|
|
#define VARIANTOFGENERATOR(G) G
|
|
|
|
GENERATORLIST(VARIANTOFGENERATOR, COMMA(), )
|
|
|
|
>;
|
|
|
|
#undef VARIANTOFGENERATOR
|
|
|
|
#undef COMMA
|
2020-08-27 17:07:11 +00:00
|
|
|
using RandomEngine = std::mt19937_64;
|
2021-01-21 16:53:22 +00:00
|
|
|
using Distribution = std::uniform_int_distribution<size_t>;
|
|
|
|
|
2021-01-27 13:55:30 +00:00
|
|
|
struct UniformRandomDistribution
|
2021-01-21 16:53:22 +00:00
|
|
|
{
|
2021-01-27 13:55:30 +00:00
|
|
|
explicit UniformRandomDistribution(std::unique_ptr<RandomEngine> _randomEngine):
|
|
|
|
randomEngine(std::move(_randomEngine))
|
|
|
|
{}
|
|
|
|
|
2021-01-25 13:01:43 +00:00
|
|
|
/// @returns an unsigned integer in the range [1, @param _n] chosen
|
|
|
|
/// uniformly at random.
|
2021-01-27 13:55:30 +00:00
|
|
|
[[nodiscard]] size_t distributionOneToN(size_t _n) const
|
2021-01-21 16:53:22 +00:00
|
|
|
{
|
2021-01-27 13:55:30 +00:00
|
|
|
return Distribution(1, _n)(*randomEngine);
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
2021-01-27 13:55:30 +00:00
|
|
|
/// @returns true with a probability of 1/(@param _n), false otherwise.
|
|
|
|
/// @param _n must be non zero.
|
2021-01-27 13:55:30 +00:00
|
|
|
[[nodiscard]] bool probable(size_t _n) const
|
2021-01-21 16:53:22 +00:00
|
|
|
{
|
2021-01-27 13:55:30 +00:00
|
|
|
solAssert(_n > 0, "");
|
2021-01-27 13:55:30 +00:00
|
|
|
return distributionOneToN(_n) == 1;
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
2021-01-27 13:55:30 +00:00
|
|
|
std::unique_ptr<RandomEngine> randomEngine;
|
2021-01-21 16:53:22 +00:00
|
|
|
};
|
2020-08-27 17:07:11 +00:00
|
|
|
|
2021-01-27 13:55:30 +00:00
|
|
|
struct TestState
|
2021-01-21 16:53:22 +00:00
|
|
|
{
|
2021-01-27 13:55:30 +00:00
|
|
|
explicit TestState(std::shared_ptr<UniformRandomDistribution> _urd):
|
2021-01-27 13:55:30 +00:00
|
|
|
sourceUnitPaths({}),
|
|
|
|
currentSourceUnitPath({}),
|
2021-01-27 13:55:30 +00:00
|
|
|
uRandDist(std::move(_urd))
|
2021-01-27 13:55:30 +00:00
|
|
|
{}
|
|
|
|
/// Adds @param _path to @name sourceUnitPaths updates
|
|
|
|
/// @name currentSourceUnitPath.
|
|
|
|
void addSourceUnit(std::string const& _path)
|
2021-01-21 16:53:22 +00:00
|
|
|
{
|
2021-01-27 13:55:30 +00:00
|
|
|
sourceUnitPaths.insert(_path);
|
|
|
|
currentSourceUnitPath = _path;
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns true if @name sourceUnitPaths is empty,
|
2021-01-27 13:55:30 +00:00
|
|
|
/// false otherwise.
|
|
|
|
[[nodiscard]] bool empty() const
|
|
|
|
{
|
|
|
|
return sourceUnitPaths.empty();
|
|
|
|
}
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns the number of items in @name sourceUnitPaths.
|
2021-01-27 13:55:30 +00:00
|
|
|
[[nodiscard]] size_t size() const
|
|
|
|
{
|
|
|
|
return sourceUnitPaths.size();
|
|
|
|
}
|
|
|
|
/// Prints test state to @param _os.
|
|
|
|
void print(std::ostream& _os) const;
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns a randomly chosen path from @param _sourceUnitPaths.
|
2021-01-27 13:55:30 +00:00
|
|
|
[[nodiscard]] std::string randomPath(std::set<std::string> const& _sourceUnitPaths) const;
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns a randomly chosen path from @name sourceUnitPaths.
|
2021-01-27 13:55:30 +00:00
|
|
|
[[nodiscard]] std::string randomPath() const;
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns a randomly chosen non current source unit path.
|
2021-01-27 13:55:30 +00:00
|
|
|
[[nodiscard]] std::string randomNonCurrentPath() const;
|
|
|
|
/// List of source paths in test input.
|
|
|
|
std::set<std::string> sourceUnitPaths;
|
|
|
|
/// Source path being currently visited.
|
|
|
|
std::string currentSourceUnitPath;
|
2021-01-27 13:55:30 +00:00
|
|
|
/// Uniform random distribution.
|
|
|
|
std::shared_ptr<UniformRandomDistribution> uRandDist;
|
2021-01-21 16:53:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct GeneratorBase
|
|
|
|
{
|
2021-01-27 13:53:46 +00:00
|
|
|
explicit GeneratorBase(std::shared_ptr<SolidityGenerator> _mutator);
|
2021-01-21 16:53:22 +00:00
|
|
|
template <typename T>
|
|
|
|
std::shared_ptr<T> generator()
|
|
|
|
{
|
|
|
|
for (auto& g: generators)
|
|
|
|
if (std::holds_alternative<std::shared_ptr<T>>(g))
|
|
|
|
return std::get<std::shared_ptr<T>>(g);
|
|
|
|
solAssert(false, "");
|
|
|
|
}
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns test fragment created by this generator.
|
2021-01-26 15:11:18 +00:00
|
|
|
std::string generate()
|
|
|
|
{
|
|
|
|
std::string generatedCode = visit();
|
|
|
|
endVisit();
|
|
|
|
return generatedCode;
|
|
|
|
}
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns a string representing the generation of
|
|
|
|
/// the Solidity grammar element.
|
2021-01-21 16:53:22 +00:00
|
|
|
virtual std::string visit() = 0;
|
2021-01-26 15:11:18 +00:00
|
|
|
/// Method called after visiting this generator. Used
|
|
|
|
/// for clearing state if necessary.
|
|
|
|
virtual void endVisit() {}
|
2021-01-25 13:01:43 +00:00
|
|
|
/// Visitor that invokes child grammar elements of
|
|
|
|
/// this grammar element returning their string
|
|
|
|
/// representations.
|
2021-01-21 16:53:22 +00:00
|
|
|
std::string visitChildren();
|
2021-01-25 13:01:43 +00:00
|
|
|
/// Adds generators for child grammar elements of
|
|
|
|
/// this grammar element.
|
2021-01-21 16:53:22 +00:00
|
|
|
void addGenerators(std::set<GeneratorPtr> _generators)
|
|
|
|
{
|
2021-01-25 13:01:43 +00:00
|
|
|
generators += _generators;
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
2021-01-25 13:01:43 +00:00
|
|
|
/// Virtual method to obtain string name of generator.
|
2021-01-21 16:53:22 +00:00
|
|
|
virtual std::string name() = 0;
|
2021-01-25 13:01:43 +00:00
|
|
|
/// Virtual method to add generators that this grammar
|
2021-01-26 15:11:18 +00:00
|
|
|
/// element depends on. If not overridden, there are
|
|
|
|
/// no dependencies.
|
|
|
|
virtual void setup() {}
|
2021-01-21 16:53:22 +00:00
|
|
|
virtual ~GeneratorBase()
|
|
|
|
{
|
|
|
|
generators.clear();
|
|
|
|
}
|
2021-01-25 13:01:43 +00:00
|
|
|
/// Shared pointer to the mutator instance
|
2021-01-21 16:53:22 +00:00
|
|
|
std::shared_ptr<SolidityGenerator> mutator;
|
2021-01-25 13:01:43 +00:00
|
|
|
/// Set of generators used by this generator.
|
2021-01-21 16:53:22 +00:00
|
|
|
std::set<GeneratorPtr> generators;
|
2021-01-27 13:55:30 +00:00
|
|
|
/// Shared ptr to global test state.
|
|
|
|
std::shared_ptr<TestState> state;
|
2021-01-27 13:55:30 +00:00
|
|
|
/// Uniform random distribution
|
|
|
|
std::shared_ptr<UniformRandomDistribution> uRandDist;
|
2021-01-21 16:53:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class TestCaseGenerator: public GeneratorBase
|
2020-08-27 17:07:11 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-01-27 13:53:46 +00:00
|
|
|
explicit TestCaseGenerator(std::shared_ptr<SolidityGenerator> _mutator):
|
2021-01-21 16:53:22 +00:00
|
|
|
GeneratorBase(std::move(_mutator)),
|
|
|
|
m_numSourceUnits(0)
|
2020-08-27 17:07:11 +00:00
|
|
|
{}
|
2021-01-21 16:53:22 +00:00
|
|
|
void setup() override;
|
|
|
|
std::string visit() override;
|
|
|
|
std::string name() override
|
|
|
|
{
|
|
|
|
return "Test case generator";
|
|
|
|
}
|
|
|
|
private:
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns a new source path name that is formed by concatenating
|
2021-01-27 13:53:46 +00:00
|
|
|
/// a static prefix @name m_sourceUnitNamePrefix, a monotonically
|
|
|
|
/// increasing counter starting from 0 and the postfix (extension)
|
|
|
|
/// ".sol".
|
|
|
|
[[nodiscard]] std::string path() const
|
2021-01-21 16:53:22 +00:00
|
|
|
{
|
|
|
|
return m_sourceUnitNamePrefix + std::to_string(m_numSourceUnits) + ".sol";
|
|
|
|
}
|
2021-01-27 13:55:30 +00:00
|
|
|
/// Adds @param _path to list of source paths in global test
|
|
|
|
/// state and increments @name m_numSourceUnits.
|
|
|
|
void updateSourcePath(std::string const& _path)
|
|
|
|
{
|
|
|
|
state->addSourceUnit(_path);
|
|
|
|
m_numSourceUnits++;
|
|
|
|
}
|
2021-01-25 13:01:43 +00:00
|
|
|
/// Number of source units in test input
|
2021-01-21 16:53:22 +00:00
|
|
|
size_t m_numSourceUnits;
|
2021-01-25 13:01:43 +00:00
|
|
|
/// String prefix of source unit names
|
2021-01-21 16:53:22 +00:00
|
|
|
std::string const m_sourceUnitNamePrefix = "su";
|
2021-01-25 13:01:43 +00:00
|
|
|
/// Maximum number of source units per test input
|
2021-01-21 16:53:22 +00:00
|
|
|
static constexpr unsigned s_maxSourceUnits = 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SourceUnitGenerator: public GeneratorBase
|
|
|
|
{
|
|
|
|
public:
|
2021-01-27 13:53:46 +00:00
|
|
|
explicit SourceUnitGenerator(std::shared_ptr<SolidityGenerator> _mutator):
|
2021-01-21 16:53:22 +00:00
|
|
|
GeneratorBase(std::move(_mutator))
|
|
|
|
{}
|
|
|
|
void setup() override;
|
|
|
|
std::string visit() override;
|
|
|
|
std::string name() override { return "Source unit generator"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class PragmaGenerator: public GeneratorBase
|
|
|
|
{
|
|
|
|
public:
|
2021-01-27 13:53:46 +00:00
|
|
|
explicit PragmaGenerator(std::shared_ptr<SolidityGenerator> _mutator):
|
2021-01-21 16:53:22 +00:00
|
|
|
GeneratorBase(std::move(_mutator))
|
|
|
|
{}
|
|
|
|
std::string visit() override;
|
|
|
|
std::string name() override { return "Pragma generator"; }
|
|
|
|
};
|
|
|
|
|
2021-01-27 13:55:30 +00:00
|
|
|
class ImportGenerator: public GeneratorBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit ImportGenerator(std::shared_ptr<SolidityGenerator> _mutator):
|
|
|
|
GeneratorBase(std::move(_mutator))
|
|
|
|
{}
|
|
|
|
std::string visit() override;
|
|
|
|
std::string name() override { return "Import generator"; }
|
|
|
|
private:
|
|
|
|
/// Inverse probability with which a source unit
|
|
|
|
/// imports itself. Keeping this at 17 seems to
|
|
|
|
/// produce self imported source units with a
|
|
|
|
/// frequency small enough so that it does not
|
|
|
|
/// consume too many fuzzing cycles but large
|
|
|
|
/// enough so that the fuzzer generates self
|
|
|
|
/// import statements every once in a while.
|
|
|
|
static constexpr size_t s_selfImportInvProb = 17;
|
|
|
|
};
|
|
|
|
|
2021-01-21 16:53:22 +00:00
|
|
|
class SolidityGenerator: public std::enable_shared_from_this<SolidityGenerator>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit SolidityGenerator(unsigned _seed);
|
|
|
|
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns the generator of type @param T.
|
2021-01-21 16:53:22 +00:00
|
|
|
template <typename T>
|
|
|
|
std::shared_ptr<T> generator();
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns a shared ptr to underlying random
|
2021-01-27 13:55:30 +00:00
|
|
|
/// number distribution.
|
|
|
|
std::shared_ptr<UniformRandomDistribution> uniformRandomDist()
|
2021-01-21 16:53:22 +00:00
|
|
|
{
|
2021-01-27 13:55:30 +00:00
|
|
|
return m_urd;
|
2021-01-21 16:53:22 +00:00
|
|
|
}
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns a pseudo randomly generated test case.
|
2020-08-27 17:07:11 +00:00
|
|
|
std::string generateTestProgram();
|
2021-02-09 11:51:36 +00:00
|
|
|
/// @returns shared ptr to global test state.
|
2021-01-27 13:55:30 +00:00
|
|
|
std::shared_ptr<TestState> testState()
|
|
|
|
{
|
|
|
|
return m_state;
|
|
|
|
}
|
2020-08-27 17:07:11 +00:00
|
|
|
private:
|
2021-01-21 16:53:22 +00:00
|
|
|
template <typename T>
|
|
|
|
void createGenerator()
|
|
|
|
{
|
|
|
|
m_generators.insert(
|
|
|
|
std::make_shared<T>(shared_from_this())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template <std::size_t I = 0>
|
|
|
|
void createGenerators();
|
|
|
|
void destroyGenerators()
|
|
|
|
{
|
|
|
|
m_generators.clear();
|
|
|
|
}
|
|
|
|
/// Sub generators
|
|
|
|
std::set<GeneratorPtr> m_generators;
|
2021-01-27 13:55:30 +00:00
|
|
|
/// Shared global test state
|
|
|
|
std::shared_ptr<TestState> m_state;
|
2021-01-27 13:55:30 +00:00
|
|
|
/// Uniform random distribution
|
|
|
|
std::shared_ptr<UniformRandomDistribution> m_urd;
|
2020-08-27 17:07:11 +00:00
|
|
|
};
|
2020-12-08 20:06:10 +00:00
|
|
|
}
|