winsvega
10 years ago
committed by
Dimitry
5 changed files with 205 additions and 55 deletions
@ -0,0 +1,112 @@ |
|||||
|
/*
|
||||
|
This file is part of cpp-ethereum. |
||||
|
|
||||
|
cpp-ethereum 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. |
||||
|
|
||||
|
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
*/ |
||||
|
/** @file fuzzHelper.cpp
|
||||
|
* @author Dimitry Khokhlov <winsvega@mail.ru> |
||||
|
* @date 2015 |
||||
|
*/ |
||||
|
|
||||
|
#include "fuzzHelper.h" |
||||
|
|
||||
|
#include <chrono> |
||||
|
#include <boost/random.hpp> |
||||
|
#include <boost/filesystem/path.hpp> |
||||
|
#include <libevmcore/Instruction.h> |
||||
|
|
||||
|
namespace dev |
||||
|
{ |
||||
|
namespace test |
||||
|
{ |
||||
|
|
||||
|
boost::random::mt19937 RandomCode::gen; |
||||
|
boostIntDistrib RandomCode::opCodeDist = boostIntDistrib (0, 255); |
||||
|
boostIntDistrib RandomCode::opLengDist = boostIntDistrib (1, 32); |
||||
|
boostIntDistrib RandomCode::uniIntDist = boostIntDistrib (0, 0x7fffffff); |
||||
|
|
||||
|
boostIntGenerator RandomCode::randOpCodeGen = boostIntGenerator(gen, opCodeDist); |
||||
|
boostIntGenerator RandomCode::randOpLengGen = boostIntGenerator(gen, opLengDist); |
||||
|
boostIntGenerator RandomCode::randUniIntGen = boostIntGenerator(gen, uniIntDist); |
||||
|
|
||||
|
|
||||
|
std::string RandomCode::rndByteSequence(int length) |
||||
|
{ |
||||
|
refreshSeed(); |
||||
|
std::string hash; |
||||
|
length = std::max(1, length); |
||||
|
for (auto i = 0; i < length; i++) |
||||
|
{ |
||||
|
uint8_t byte = randOpCodeGen(); |
||||
|
hash += toCompactHex(byte); |
||||
|
} |
||||
|
return hash; |
||||
|
} |
||||
|
|
||||
|
std::string RandomCode::fillArguments(int num) |
||||
|
{ |
||||
|
std::string code; |
||||
|
for (auto i = 0; i < num; i++) |
||||
|
{ |
||||
|
int length = randOpLengGen(); |
||||
|
int pushCode = 96 + length - 1; |
||||
|
code += toCompactHex(pushCode) + rndByteSequence(length); |
||||
|
} |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
//generate smart random code
|
||||
|
std::string RandomCode::generate(int maxOpNumber, CodeOptions options) |
||||
|
{ |
||||
|
refreshSeed(); |
||||
|
std::string code; |
||||
|
boostIntDistrib sizeDist (0, maxOpNumber); |
||||
|
boostIntGenerator rndSizeGen(gen, sizeDist); |
||||
|
int size = (int)rndSizeGen(); |
||||
|
for (auto i = 0; i < size; i++) |
||||
|
{ |
||||
|
uint8_t opcode = randOpCodeGen(); |
||||
|
dev::eth::InstructionInfo info = dev::eth::instructionInfo((dev::eth::Instruction) opcode); |
||||
|
|
||||
|
if (info.name.find_first_of("INVALID_INSTRUCTION") > 0) |
||||
|
{ |
||||
|
//Byte code is yet not implemented
|
||||
|
if (options == CodeOptions::DontUseUndefinedOpCodes) |
||||
|
{ |
||||
|
i--; |
||||
|
continue; |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
code += fillArguments(info.args); |
||||
|
code += toCompactHex(opcode); |
||||
|
} |
||||
|
return code; |
||||
|
} |
||||
|
|
||||
|
std::string RandomCode::randomUniInt() |
||||
|
{ |
||||
|
refreshSeed(); |
||||
|
return "0x" + toCompactHex((int)randUniIntGen()); |
||||
|
} |
||||
|
|
||||
|
void RandomCode::refreshSeed() |
||||
|
{ |
||||
|
auto now = std::chrono::steady_clock::now().time_since_epoch(); |
||||
|
auto timeSinceEpoch = std::chrono::duration_cast<std::chrono::nanoseconds>(now).count(); |
||||
|
gen.seed(static_cast<unsigned int>(timeSinceEpoch)); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,75 @@ |
|||||
|
/*
|
||||
|
This file is part of cpp-ethereum. |
||||
|
|
||||
|
cpp-ethereum 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. |
||||
|
|
||||
|
cpp-ethereum 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 cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
*/ |
||||
|
/** @file fuzzHelper.h
|
||||
|
* @author Dimitry Khokhlov <winsvega@mail.ru> |
||||
|
* @date 2015 |
||||
|
*/ |
||||
|
|
||||
|
#include <string> |
||||
|
#include <boost/random.hpp> |
||||
|
#include <boost/filesystem/path.hpp> |
||||
|
|
||||
|
#include <test/TestHelper.h> |
||||
|
#include <libdevcore/CommonIO.h> |
||||
|
#include <libdevcore/CommonData.h> |
||||
|
|
||||
|
#pragma once |
||||
|
|
||||
|
namespace dev |
||||
|
{ |
||||
|
namespace test |
||||
|
{ |
||||
|
|
||||
|
typedef boost::random::uniform_int_distribution<> boostIntDistrib; |
||||
|
typedef boost::random::variate_generator<boost::mt19937&, boostIntDistrib > boostIntGenerator; |
||||
|
|
||||
|
enum class CodeOptions |
||||
|
{ |
||||
|
UseUndefinedOpCodes, |
||||
|
DontUseUndefinedOpCodes |
||||
|
}; |
||||
|
|
||||
|
class RandomCode |
||||
|
{ |
||||
|
public: |
||||
|
/// Generate random vm code
|
||||
|
static std::string generate(int maxOpNumber = 1, CodeOptions options = CodeOptions::DontUseUndefinedOpCodes); |
||||
|
|
||||
|
/// Generate random byte string of a given length
|
||||
|
static std::string rndByteSequence(int length = 1); |
||||
|
|
||||
|
/// Generate random uniForm Int with reasonable value 0..0x7fffffff
|
||||
|
static std::string randomUniInt(); |
||||
|
|
||||
|
private: |
||||
|
static std::string fillArguments(int num); |
||||
|
static void refreshSeed(); |
||||
|
|
||||
|
static boost::random::mt19937 gen; ///< Random generator
|
||||
|
static boostIntDistrib opCodeDist; ///< 0..255 opcodes
|
||||
|
static boostIntDistrib opLengDist; ///< 1..32 byte string
|
||||
|
static boostIntDistrib uniIntDist; ///< 0..0x7fffffff
|
||||
|
|
||||
|
static boostIntGenerator randUniIntGen; ///< Generate random UniformInt from
|
||||
|
static boostIntGenerator randOpCodeGen; ///< Generate random value from opCodeDist
|
||||
|
static boostIntGenerator randOpLengGen; ///< Generate random length from opLengDist
|
||||
|
}; |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
Loading…
Reference in new issue