Browse Source

RLP fuzz: invalid rlp generation

cl-refactor
Dimitry 10 years ago
committed by Dimitry
parent
commit
b7e8c64d13
  1. 94
      test/fuzzTesting/fuzzHelper.cpp
  2. 12
      test/fuzzTesting/fuzzHelper.h

94
test/fuzzTesting/fuzzHelper.cpp

@ -42,68 +42,97 @@ boostIntGenerator RandomCode::randOpLengGen = boostIntGenerator(gen, opLengDist)
boostIntGenerator RandomCode::randUniIntGen = boostIntGenerator(gen, uniIntDist); boostIntGenerator RandomCode::randUniIntGen = boostIntGenerator(gen, uniIntDist);
boostUInt64Generator RandomCode::randUInt64Gen = boostUInt64Generator(gen, uInt64Dist); boostUInt64Generator RandomCode::randUInt64Gen = boostUInt64Generator(gen, uInt64Dist);
int RandomCode::recursiveRLP(std::string &result, int depth) int RandomCode::recursiveRLP(std::string &_result, int _depth, RlpDebug &_debug)
{ {
bool genvalidrlp = true; bool genvalidrlp = false;
if (depth > 1) if (_depth > 1)
{ {
//create rlp blocks //create rlp blocks
int size = 2 + randUniIntGen() % 4; int size = 2 + randUniIntGen() % 4;
for (auto i = 0; i < size; i++) for (auto i = 0; i < size; i++)
{ {
std::string blockstr; std::string blockstr;
recursiveRLP(blockstr, depth - 1); RlpDebug blockDebug;
result += blockstr; recursiveRLP(blockstr, _depth - 1, blockDebug);
_result += blockstr;
_debug.rlp += blockDebug.rlp;
} }
//make rlp header //make rlp header
int length = result.size()/2; int length = _result.size() / 2;
std::string header; std::string header;
int rtype = 0;
int rnd = randUniIntGen() % 100; int rnd = randUniIntGen() % 100;
if (rnd < 50) if (rnd < 10)
{ {
//make header as array //make header as array
if (length <= 55) if (length <= 55)
{
header = toCompactHex(128 + length); header = toCompactHex(128 + length);
rtype = 1;
}
else else
{ {
std::string hexlength = toCompactHex(length); std::string hexlength = toCompactHex(length);
header = toCompactHex(183 + hexlength.size()/2) + hexlength; header = toCompactHex(183 + hexlength.size() / 2) + hexlength;
rtype = 2;
} }
} }
else else
{ {
//make header as list //make header as list
if (length <= 55) if (length <= 55)
{
header = toCompactHex(192 + length); header = toCompactHex(192 + length);
rtype = 3;
}
else else
{ {
std::string hexlength = toCompactHex(length, HexPrefix::DontAdd, 1); std::string hexlength = toCompactHex(length, HexPrefix::DontAdd, 1);
header = toCompactHex(247 + hexlength.size()/2) + hexlength; header = toCompactHex(247 + hexlength.size() / 2) + hexlength;
rtype = 4;
} }
} }
result = header + result; _result = header + _result;
return result.size()/2; _debug.rlp = "[" + header + "(" + toString(length) + "){" + toString(rtype) + "}]" + _debug.rlp;
return _result.size() / 2;
} }
if (depth == 1) if (_depth == 1)
{ {
bool genbug = false;
bool genbug2 = false;
int bugprobability = randUniIntGen() % 100;
if (bugprobability < 150 && !genvalidrlp)
genbug = true;
bugprobability = randUniIntGen() % 100; //more randomness
if (bugprobability < 150 && !genvalidrlp)
genbug2 = true;
std::string emptyZeros = genvalidrlp ? "" : genbug ? "00" : "";
std::string emptyZeros2 = genvalidrlp ? "" : genbug2 ? "00" : "";
int rnd = randUniIntGen() % 5; int rnd = randUniIntGen() % 5;
switch (rnd) switch (rnd)
{ {
case 0: case 0:
{
//single byte [0x00, 0x7f] //single byte [0x00, 0x7f]
result.insert(0, toCompactHex(randUniIntGen() % 128, HexPrefix::DontAdd, 1)); std::string rlp = emptyZeros + toCompactHex(genbug ? randUniIntGen() % 255 : randUniIntGen() % 128, HexPrefix::DontAdd, 1);
_result.insert(0, rlp);
_debug.rlp.insert(0, "[" + rlp + "]");
return 1; return 1;
}
case 1: case 1:
{ {
//string 0-55 [0x80, 0xb7] + string //string 0-55 [0x80, 0xb7] + string
int len = randUniIntGen() % 55; int len = genbug ? randUniIntGen() % 255 : randUniIntGen() % 55;
std::string hex = rndByteSequence(len); std::string hex = rndByteSequence(len);
if (len == 1) if (len == 1)
if (genvalidrlp && fromHex(hex)[0] < 128) if (genvalidrlp && fromHex(hex)[0] < 128)
hex = toCompactHex((u64)128); hex = toCompactHex((u64)128);
result.insert(0, toCompactHex(128 + len) + hex); _result.insert(0, toCompactHex(128 + len) + emptyZeros + hex);
_debug.rlp.insert(0, "[" + toCompactHex(128 + len) + "(" + toString(len) + ")]" + emptyZeros + hex);
return len + 1; return len + 1;
} }
case 2: case 2:
@ -114,17 +143,19 @@ int RandomCode::recursiveRLP(std::string &result, int depth)
len = 56; len = 56;
std::string hex = rndByteSequence(len); std::string hex = rndByteSequence(len);
std::string hexlen = toCompactHex(len, HexPrefix::DontAdd, 1); std::string hexlen = emptyZeros2 + toCompactHex(len, HexPrefix::DontAdd, 1);
std::string rlpblock = toCompactHex(183 + hexlen.size()/2) + hexlen + hex; std::string rlpblock = toCompactHex(183 + hexlen.size() / 2) + hexlen + emptyZeros + hex;
result.insert(0, rlpblock); _debug.rlp.insert(0, "[" + toCompactHex(183 + hexlen.size() / 2) + hexlen + "(" + toString(len) + "){2}]" + emptyZeros + hex);
return rlpblock.size()/2; _result.insert(0, rlpblock);
return rlpblock.size() / 2;
} }
case 3: case 3:
{ {
//list 0-55 [0xc0, 0xf7] + data //list 0-55 [0xc0, 0xf7] + data
int len = randUniIntGen() % 55; int len = genbug ? randUniIntGen() % 255 : randUniIntGen() % 55;
std::string hex = rndByteSequence(len); std::string hex = emptyZeros + rndByteSequence(len);
result.insert(0, toCompactHex(192 + len) + hex); _result.insert(0, toCompactHex(192 + len) + hex);
_debug.rlp.insert(0, "[" + toCompactHex(192 + len) + "(" + toString(len) + "){3}]" + hex);
return len + 1; return len + 1;
} }
case 4: case 4:
@ -133,22 +164,25 @@ int RandomCode::recursiveRLP(std::string &result, int depth)
int len = randUniIntGen() % 100; int len = randUniIntGen() % 100;
if (len < 56 && genvalidrlp) if (len < 56 && genvalidrlp)
len = 56; len = 56;
std::string hexlen = toCompactHex(len, HexPrefix::DontAdd, 1); std::string hexlen = emptyZeros2 + toCompactHex(len, HexPrefix::DontAdd, 1);
std::string rlpblock = toCompactHex(247 + hexlen.size()/2) + hexlen + rndByteSequence(len); std::string rlpblock = toCompactHex(247 + hexlen.size() / 2) + hexlen + emptyZeros + rndByteSequence(len);
result.insert(0, rlpblock); _debug.rlp.insert(0, "[" + toCompactHex(247 + hexlen.size() / 2) + hexlen + "(" + toString(len) + "){4}]" + emptyZeros + rndByteSequence(len));
return rlpblock.size()/2; _result.insert(0, rlpblock);
return rlpblock.size() / 2;
} }
} }
} }
return 0; return 0;
} }
std::string RandomCode::rndRLPSequence(int _length, SizeStrictness _sizeType) std::string RandomCode::rndRLPSequence(int _depth, SizeStrictness _sizeType)
{ {
refreshSeed(); refreshSeed();
std::string hash; std::string hash;
_length = (_sizeType == SizeStrictness::Strict) ? std::max(1, _length) : randomUniInt() % _length; _depth = (_sizeType == SizeStrictness::Strict) ? std::max(1, _depth) : randomUniInt() % _depth;
recursiveRLP(hash, _length); RlpDebug debug;
recursiveRLP(hash, _depth, debug);
cnote << debug.rlp;
return hash; return hash;
} }

12
test/fuzzTesting/fuzzHelper.h

@ -66,6 +66,12 @@ enum class SizeStrictness
Random Random
}; };
struct RlpDebug
{
std::string rlp;
int insertions;
};
class RandomCode class RandomCode
{ {
public: public:
@ -75,8 +81,8 @@ public:
/// Generate random byte string of a given length /// Generate random byte string of a given length
static std::string rndByteSequence(int _length = 1, SizeStrictness _sizeType = SizeStrictness::Strict); static std::string rndByteSequence(int _length = 1, SizeStrictness _sizeType = SizeStrictness::Strict);
/// Gemerate random rlp byte sequence of a given length /// Gemerate random rlp byte sequence of a given depth (e.g [[[]],[]])
static std::string rndRLPSequence(int _length = 1, SizeStrictness _sizeType = SizeStrictness::Strict); static std::string rndRLPSequence(int _depth = 1, SizeStrictness _sizeType = SizeStrictness::Strict);
/// Generate random int64 /// Generate random int64
static std::string randomUniIntHex(u256 _maxVal = 0); static std::string randomUniIntHex(u256 _maxVal = 0);
@ -86,7 +92,7 @@ private:
static std::string fillArguments(dev::eth::Instruction _opcode, RandomCodeOptions const& _options); static std::string fillArguments(dev::eth::Instruction _opcode, RandomCodeOptions const& _options);
static std::string getPushCode(int _value); static std::string getPushCode(int _value);
static std::string getPushCode(std::string const& _hex); static std::string getPushCode(std::string const& _hex);
static int recursiveRLP(std::string &result, int depth); static int recursiveRLP(std::string &_result, int _depth, RlpDebug &_debug);
static void refreshSeed(); static void refreshSeed();
static boost::random::mt19937 gen; ///< Random generator static boost::random::mt19937 gen; ///< Random generator

Loading…
Cancel
Save