fuzzHelper.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file fuzzHelper.h
  15. * @author Dimitry Khokhlov <winsvega@mail.ru>
  16. * @date 2015
  17. */
  18. #include <string>
  19. #include <boost/random.hpp>
  20. #include <boost/filesystem/path.hpp>
  21. #include <test/TestHelper.h>
  22. #include <libdevcore/CommonIO.h>
  23. #include <libdevcore/CommonData.h>
  24. #include <libevmcore/Instruction.h>
  25. #pragma once
  26. namespace dev
  27. {
  28. namespace test
  29. {
  30. typedef boost::random::uniform_int_distribution<> boostIntDistrib;
  31. typedef boost::random::discrete_distribution<> boostDescreteDistrib;
  32. typedef boost::uniform_int<uint64_t> boostUint64Distrib;
  33. typedef boost::random::variate_generator<boost::mt19937&, boostIntDistrib > boostIntGenerator;
  34. typedef boost::random::variate_generator<boost::mt19937&, boostDescreteDistrib > boostWeightGenerator;
  35. typedef boost::random::variate_generator<boost::mt19937&, boostUint64Distrib > boostUInt64Generator;
  36. struct RandomCodeOptions
  37. {
  38. public:
  39. RandomCodeOptions();
  40. void setWeight(dev::eth::Instruction _opCode, int _weight);
  41. void addAddress(dev::Address const& _address);
  42. dev::Address getRandomAddress() const;
  43. bool useUndefinedOpCodes;
  44. int smartCodeProbability;
  45. boostDescreteDistrib opCodeProbability;
  46. private:
  47. void setWeights();
  48. std::map<int, int> mapWeights;
  49. std::vector<dev::Address> addressList;
  50. };
  51. enum class SizeStrictness
  52. {
  53. Strict,
  54. Random
  55. };
  56. struct RlpDebug
  57. {
  58. std::string rlp;
  59. int insertions;
  60. };
  61. class RandomCode
  62. {
  63. public:
  64. /// Generate random vm code
  65. static std::string generate(int _maxOpNumber = 1, RandomCodeOptions _options = RandomCodeOptions());
  66. /// Replace keywords in given string with values
  67. static void parseTestWithTypes(std::string& _test, std::map<std::string, std::string> const& _varMap);
  68. /// Generate random byte string of a given length
  69. static std::string rndByteSequence(int _length = 1, SizeStrictness _sizeType = SizeStrictness::Strict);
  70. /// Generate random rlp byte sequence of a given depth (e.g [[[]],[]]). max depth level = 20.
  71. /// The _debug string contains returned rlp string with analysed sections
  72. /// [] - length section/ or single byte rlp encoding
  73. /// () - decimal representation of length
  74. /// {1} - Array
  75. /// {2} - Array more than 55
  76. /// {3} - List
  77. /// {4} - List more than 55
  78. static std::string rndRLPSequence(int _depth, std::string& _debug);
  79. /// Generate random int64
  80. static std::string randomUniIntHex(u256 _maxVal = 0);
  81. static u256 randomUniInt(u256 _maxVal = 0);
  82. private:
  83. static std::string fillArguments(dev::eth::Instruction _opcode, RandomCodeOptions const& _options);
  84. static std::string getPushCode(int _value);
  85. static std::string getPushCode(std::string const& _hex);
  86. static int recursiveRLP(std::string& _result, int _depth, std::string& _debug);
  87. static void refreshSeed();
  88. static std::vector<std::string> getTypes();
  89. static boost::random::mt19937 gen; ///< Random generator
  90. static boostIntDistrib opCodeDist; ///< 0..255 opcodes
  91. static boostIntDistrib opLengDist; ///< 1..32 byte string
  92. static boostIntDistrib uniIntDist; ///< 0..0x7fffffff
  93. static boostUint64Distrib uInt64Dist; ///< 0..2**64
  94. static boostIntGenerator randUniIntGen; ///< Generate random UniformInt from uniIntDist
  95. static boostIntGenerator randOpCodeGen; ///< Generate random value from opCodeDist
  96. static boostIntGenerator randOpLengGen; ///< Generate random length from opLengDist
  97. static boostUInt64Generator randUInt64Gen; ///< Generate random uInt64
  98. };
  99. }
  100. }