Browse Source

Random Code

cl-refactor
winsvega 10 years ago
committed by Dimitry
parent
commit
120611e9d4
  1. 7
      test/fuzzTesting/CMakeLists.txt
  2. 57
      test/fuzzTesting/createRandomStateTest.cpp
  3. 112
      test/fuzzTesting/fuzzHelper.cpp
  4. 75
      test/fuzzTesting/fuzzHelper.h
  5. 9
      test/libethereum/blockchain.cpp

7
test/fuzzTesting/CMakeLists.txt

@ -9,10 +9,13 @@ include_directories(${CRYPTOPP_INCLUDE_DIRS})
include_directories(${JSON_RPC_CPP_INCLUDE_DIRS})
add_executable(createRandomVMTest "./createRandomVMTest.cpp" "../libevm/vm.cpp" "../TestHelper.cpp" "../Stats.cpp")
add_executable(createRandomStateTest "./createRandomStateTest.cpp" "../TestHelper.cpp" "../Stats.cpp")
add_executable(checkRandomVMTest "./checkRandomVMTest.cpp" "../libevm/vm.cpp" "../TestHelper.cpp" "../Stats.cpp")
add_executable(createRandomStateTest "./createRandomStateTest.cpp" "../TestHelper.cpp" "../Stats.cpp" "fuzzHelper.cpp")
add_executable(checkRandomVMTest "./checkRandomVMTest.cpp" "../libevm/vm.cpp" "../TestHelper.cpp" "../Stats.cpp" )
add_executable(checkRandomStateTest "./checkRandomStateTest.cpp" "../TestHelper.cpp" "../Stats.cpp")
list(APPEND SRCS "./fuzzHelper.cpp")
add_sources(${SRCS})
target_link_libraries(createRandomVMTest ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})
target_link_libraries(createRandomVMTest ethereum)
target_link_libraries(createRandomVMTest ethcore)

57
test/fuzzTesting/createRandomStateTest.cpp

@ -37,6 +37,7 @@
#include <libevm/VMFactory.h>
#include <test/libevm/vm.h>
#include <test/TestHelper.h>
#include <test/fuzzTesting/fuzzHelper.h>
using namespace std;
using namespace json_spirit;
@ -47,55 +48,7 @@ void doStateTests(json_spirit::mValue& _v);
int main(int argc, char *argv[])
{
g_logVerbosity = 0;
// create random code
boost::random::mt19937 gen;
auto now = chrono::steady_clock::now().time_since_epoch();
auto timeSinceEpoch = chrono::duration_cast<chrono::nanoseconds>(now).count();
gen.seed(static_cast<unsigned int>(timeSinceEpoch));
// set min and max length of the random evm code
boost::random::uniform_int_distribution<> lengthOfCodeDist(8, 24);
boost::random::uniform_int_distribution<> reasonableInputValuesSize(0, 7);
boost::random::uniform_int_distribution<> opcodeDist(0, 255);
boost::random::uniform_int_distribution<> BlockInfoOpcodeDist(0x40, 0x45);
boost::random::uniform_int_distribution<> uniformInt(0, 0x7fffffff);
boost::random::variate_generator<boost::mt19937&, boost::random::uniform_int_distribution<> > randGenInputValue(gen, reasonableInputValuesSize);
boost::random::variate_generator<boost::mt19937&, boost::random::uniform_int_distribution<> > randGenUniformInt(gen, uniformInt);
boost::random::variate_generator<boost::mt19937&, boost::random::uniform_int_distribution<> > randGen(gen, opcodeDist);
boost::random::variate_generator<boost::mt19937&, boost::random::uniform_int_distribution<> > randGenBlockInfoOpcode(gen, BlockInfoOpcodeDist);
std::vector<u256> reasonableInputValues;
reasonableInputValues.push_back(0);
reasonableInputValues.push_back(1);
reasonableInputValues.push_back(50000);
reasonableInputValues.push_back(u256("0x10000000000000000000000000000000000000000"));
reasonableInputValues.push_back(u256("0xffffffffffffffffffffffffffffffffffffffff"));
reasonableInputValues.push_back(u256("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"));
reasonableInputValues.push_back(u256("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
reasonableInputValues.push_back(u256("0x945304eb96065b2a98b57a48a06ae28d285a71b5"));
reasonableInputValues.push_back(randGenUniformInt());
int lengthOfCode = lengthOfCodeDist(gen);
string randomCode;
for (int i = 0; i < lengthOfCode; ++i)
{
// pre-fill stack to avoid that most of the test fail with a stackunderflow
if (i < 8 && (randGen() < 192))
{
randomCode += randGen() < 32 ? toHex(toCompactBigEndian((uint8_t)randGenBlockInfoOpcode())) : "7f" + toHex(reasonableInputValues[randGenInputValue()]);
continue;
}
uint8_t opcode = randGen();
// disregard all invalid commands, except of one (0x0c)
if ((dev::eth::isValidInstruction(dev::eth::Instruction(opcode)) || (randGen() > 250)))
randomCode += toHex(toCompactBigEndian(opcode));
else
i--;
}
string randomCode = dev::test::RandomCode::generate(25);
string const s = R"(
{
@ -147,16 +100,16 @@ int main(int argc, char *argv[])
read_string(s, v);
// insert new random code
v.get_obj().find("randomStatetest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode + (randGen() > 128 ? "55" : "") + (randGen() > 128 ? "60005155" : "");
v.get_obj().find("randomStatetest")->second.get_obj().find("pre")->second.get_obj().begin()->second.get_obj()["code"] = "0x" + randomCode;
// insert new data in tx
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["data"] = "0x" + randomCode;
// insert new value in tx
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["value"] = toString(randGenUniformInt());
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["value"] = dev::test::RandomCode::randomUniInt();
// insert new gasLimit in tx
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["gasLimit"] = "0x" + toHex(toCompactBigEndian((int)randGenUniformInt()));
v.get_obj().find("randomStatetest")->second.get_obj().find("transaction")->second.get_obj()["gasLimit"] = dev::test::RandomCode::randomUniInt();
// fill test
doStateTests(v);

112
test/fuzzTesting/fuzzHelper.cpp

@ -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));
}
}
}

75
test/fuzzTesting/fuzzHelper.h

@ -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
};
}
}

9
test/libethereum/blockchain.cpp

@ -19,7 +19,7 @@
* @date 2015
* block test functions.
*/
#include "test/fuzzTesting/fuzzHelper.h"
#include <boost/filesystem.hpp>
#include <libdevcore/FileSystem.h>
#include <libdevcore/TransientDirectory.h>
@ -811,4 +811,11 @@ BOOST_AUTO_TEST_CASE(userDefinedFile)
dev::test::userDefinedTest(dev::test::doBlockchainTests);
}
BOOST_AUTO_TEST_CASE(rndCode)
{
cerr << "Testing Random Code: ";
std::string code = dev::test::RandomCode::generate(10);
cerr << code;
}
BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save