Fabcoin Core  0.16.2
P2P Digital Currency
fuzzHelper.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
22 #include <string>
23 #include <boost/random.hpp>
24 #include <boost/filesystem/path.hpp>
25 
27 #include <libdevcore/CommonIO.h>
28 #include <libdevcore/CommonData.h>
29 #include <libevmcore/Instruction.h>
30 
31 #pragma once
32 
33 namespace dev
34 {
35 namespace test
36 {
37 
38 typedef boost::random::uniform_int_distribution<> boostIntDistrib;
39 typedef boost::random::discrete_distribution<> boostDescreteDistrib;
40 typedef boost::uniform_int<uint64_t> boostUint64Distrib;
41 
42 typedef boost::random::variate_generator<boost::mt19937&, boostIntDistrib > boostIntGenerator;
43 typedef boost::random::variate_generator<boost::mt19937&, boostDescreteDistrib > boostWeightGenerator;
44 typedef boost::random::variate_generator<boost::mt19937&, boostUint64Distrib > boostUInt64Generator;
45 
47 {
48 public:
50  void setWeight(dev::eth::Instruction _opCode, int _weight);
51  void addAddress(dev::Address const& _address);
53 
56  boostDescreteDistrib opCodeProbability;
57 private:
58  void setWeights();
59  std::map<int, int> mapWeights;
60  std::vector<dev::Address> addressList;
61 };
62 
63 enum class SizeStrictness
64 {
65  Strict,
66  Random
67 };
68 
69 struct RlpDebug
70 {
71  std::string rlp;
73 };
74 
76 {
77 public:
79  static std::string generate(int _maxOpNumber = 1, RandomCodeOptions _options = RandomCodeOptions());
80 
82  static void parseTestWithTypes(std::string& _test, std::map<std::string, std::string> const& _varMap);
83 
85  static std::string rndByteSequence(int _length = 1, SizeStrictness _sizeType = SizeStrictness::Strict);
86 
95  static std::string rndRLPSequence(int _depth, std::string& _debug);
96 
98  static std::string randomUniIntHex(u256 _maxVal = 0);
99  static u256 randomUniInt(u256 _maxVal = 0);
100 
101 private:
102  static std::string fillArguments(dev::eth::Instruction _opcode, RandomCodeOptions const& _options);
103  static std::string getPushCode(int _value);
104  static std::string getPushCode(std::string const& _hex);
105  static int recursiveRLP(std::string& _result, int _depth, std::string& _debug);
106  static void refreshSeed();
107  static std::vector<std::string> getTypes();
108 
109  static boost::random::mt19937 gen;
110  static boostIntDistrib opCodeDist;
111  static boostIntDistrib opLengDist;
112  static boostIntDistrib uniIntDist;
113  static boostUint64Distrib uInt64Dist;
114 
115  static boostIntGenerator randUniIntGen;
116  static boostIntGenerator randOpCodeGen;
117  static boostIntGenerator randOpLengGen;
118  static boostUInt64Generator randUInt64Gen;
119 };
120 
121 }
122 }
static boostIntGenerator randUniIntGen
Generate random UniformInt from uniIntDist.
Definition: fuzzHelper.h:115
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
static boostIntDistrib opCodeDist
0..255 opcodes
Definition: fuzzHelper.h:110
static boostIntGenerator randOpCodeGen
Generate random value from opCodeDist.
Definition: fuzzHelper.h:116
static boostUInt64Generator randUInt64Gen
Generate random uInt64.
Definition: fuzzHelper.h:118
boost::random::variate_generator< boost::mt19937 &, boostDescreteDistrib > boostWeightGenerator
Definition: fuzzHelper.h:43
std::string rlp
Definition: fuzzHelper.h:71
void addAddress(dev::Address const &_address)
Definition: fuzzHelper.cpp:384
void setWeight(dev::eth::Instruction _opCode, int _weight)
Definition: fuzzHelper.cpp:378
static boost::random::mt19937 gen
Random generator.
Definition: fuzzHelper.h:109
boost::random::discrete_distribution boostDescreteDistrib
Definition: fuzzHelper.h:39
UniValue generate(const JSONRPCRequest &request)
Definition: rpcwallet.cpp:3584
Fixed-size raw-byte array container type, with an API optimised for storing hashes.
Definition: FixedHash.h:47
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
Instruction
Virtual machine bytecode instruction.
Definition: Instruction.h:39
dev::Address getRandomAddress() const
Definition: fuzzHelper.cpp:389
boostDescreteDistrib opCodeProbability
Definition: fuzzHelper.h:56
static boostUint64Distrib uInt64Dist
0..2**64
Definition: fuzzHelper.h:113
std::vector< dev::Address > addressList
Definition: fuzzHelper.h:60
static boostIntGenerator randOpLengGen
Generate random length from opLengDist.
Definition: fuzzHelper.h:117
boost::random::uniform_int_distribution boostIntDistrib
Definition: fuzzHelper.h:38
static boostIntDistrib opLengDist
1..32 byte string
Definition: fuzzHelper.h:111
boost::random::variate_generator< boost::mt19937 &, boostUint64Distrib > boostUInt64Generator
Definition: fuzzHelper.h:44
boost::uniform_int< uint64_t > boostUint64Distrib
Definition: fuzzHelper.h:40
boost::random::variate_generator< boost::mt19937 &, boostIntDistrib > boostIntGenerator
Definition: fuzzHelper.h:42
std::map< int, int > mapWeights
Definition: fuzzHelper.h:59
static boostIntDistrib uniIntDist
0..0x7fffffff
Definition: fuzzHelper.h:112
Helper functions to work with json::spirit and test files.