Skip to content

Commit

Permalink
[fold] Responding to Ed PR feedback 1
Browse files Browse the repository at this point in the history
  • Loading branch information
seelabs committed Aug 31, 2017
1 parent 3fe6fd1 commit d259692
Show file tree
Hide file tree
Showing 20 changed files with 298 additions and 252 deletions.
2 changes: 1 addition & 1 deletion bin/crypto_conditions_test_gen/conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2016 Ripple Labs Inc.
Copyright (c) 2017 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand Down
1 change: 1 addition & 0 deletions docs/source.dox
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ INPUT = \
../src/ripple/app/tx/applySteps.h \
../src/ripple/app/tx/impl/InvariantCheck.h \
../src/ripple/app/consensus/RCLValidations.h \
../src/ripple/conditions/impl/Der.h \

INPUT_ENCODING = UTF-8
FILE_PATTERNS =
Expand Down
22 changes: 17 additions & 5 deletions src/fuzzers/Conditions_fuzz_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@
*/
//==============================================================================

/*
Note: This file requires clang 5.0 or greater.
Typical build setup:
cmake -Dfuzzer_conditions=ON -Dsan='fuzzer;address' -Dtarget=clang.debug <path to proj CMakeLists.txt>
Typical run:
./fulfillment_fuzzer <path to corpus> -max_len=5000 -jobs=4
An initial fuzz corpus may be generated with the `conditions.py` script.
*/


#include <ripple/conditions/impl/Der.h>
#include <ripple/conditions/Fulfillment.h>

#include <boost/filesystem.hpp>

#include <fstream>

extern "C" int
Expand All @@ -36,16 +49,15 @@ LLVMFuzzerTestOneInput(uint8_t const* data, size_t size)
Condition c;
decoder >> c >> eos;
#else
#error("Must define either FUZZ_TEST_CONDITION or FUZZ_TEST_FULFILLMENT
#error("Must define either FUZZ_TEST_CONDITION or FUZZ_TEST_FULFILLMENT")
#endif

if (decoder.ec() == Error::logicError)
{
static int fileNum = 0;
static boost::filesystem::path model{"logic_error%%%%.dat"};
auto const fileName = boost::filesystem::unique_path(model);
std::ofstream ofs;
char fileName[32];
sprintf(fileName, "logic_error%d.dat", fileNum++);
ofs.open(fileName, std::ofstream::out | std::ofstream::binary);
ofs.open(fileName.native(), std::ofstream::out | std::ofstream::binary);
ofs.write((char const*) data, size);
ofs.close();
}
Expand Down
252 changes: 112 additions & 140 deletions src/ripple/conditions/impl/Der.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,36 +85,20 @@ tagNumLength(std::uint64_t v)
if (v <= 30)
return 1;

std::size_t n = 1 + 8 * sizeof(v) / 7;

// skip leading zeros
while (n--)
{
auto b = static_cast<std::uint8_t>((v >> (n * 7)) & 0xFF);
if (b)
break;
}

assert(n);
return n + 2;
constexpr std::uint64_t chunkSize = 7;
std::uint64_t const nChunks = 1 + 8 * sizeof(v) / chunkSize;
auto const lz = numLeadingZeroChunks<chunkSize>(v, nChunks);
assert(lz != nChunks);
return nChunks - lz + 1;
}

void
encodeTagNum(MutableSlice& dst, std::uint64_t v, std::error_code& ec)
{
assert(v > 30);
std::size_t n = 1 + 8 * sizeof(v) / 7;

// skip leading zeros
while (n--)
{
auto b = static_cast<std::uint8_t>((v >> (n * 7)) & 0xFF);
if (b)
break;
}
auto n = tagNumLength(v) - 1;

assert(n);
++n;
if (dst.size() != n)
{
ec = make_error_code(Error::logicError);
Expand All @@ -133,116 +117,7 @@ encodeTagNum(MutableSlice& dst, std::uint64_t v, std::error_code& ec)
}

void
encodeContentLength(MutableSlice& dst, std::uint64_t v, std::error_code& ec)
{
if (v <= 127)
{
if (dst.size() != 1)
{
ec = make_error_code(Error::logicError);
return;
}
dst.push_back(static_cast<char>(v));
return;
}

std::size_t n = sizeof(v);

// skip leading zeros
while (n--)
{
auto b = static_cast<std::uint8_t>((v >> (n * 8)) & 0xFF);
if (b)
break;
}

++n;
assert(n);

if (dst.size() != n + 1)
{
ec = make_error_code(Error::logicError);
return;
}

dst.push_back(static_cast<char>(n) | (1 << 7));

while (n--)
dst.push_back(static_cast<char>((v >> (n * 8)) & 0xFF));
}

std::uint64_t
contentLengthLength(std::uint64_t v)
{
if (v <= 127)
{
return 1;
}

std::size_t n = sizeof(v);

// skip leading zeros
while (n--)
{
auto b = static_cast<std::uint8_t>((v >> (n * 8)) & 0xFF);
if (b)
break;
}

return n + 2;
}

std::uint64_t
tagLength(Tag t)
{
if (t.tagNum <= 30)
return 1;

auto v = t.tagNum;
std::size_t n = 1 + 8 * sizeof(v) / 7;

// skip leading zeros
while (n--)
{
auto b = static_cast<std::uint8_t>((v >> (n * 7)) & 0xFF);
if (b)
break;
}

return 2+n;
}

void
encodePreamble(MutableSlice& dst, Preamble const& p, std::error_code& ec)
{
if (dst.size() <= 1)
{
ec = make_error_code(Error::logicError);
return;
}

char d = (static_cast<uint8_t>(p.tag_.classId) << 6);
if (!p.tag_.primitive)
d |= 1 << 5;

if (p.tag_.tagNum <= 30)
{
d |= p.tag_.tagNum;
dst.push_back(d);
}
else
{
d |= 0x1f;
dst.push_back(d);
encodeTagNum(dst, p.tag_.tagNum, ec);
if (ec)
return;
}
encodeContentLength(dst, p.contentLength_, ec);
}

void
decodePreamble(Slice& slice, Preamble& p, std::error_code& ec)
decodeTag(Slice& slice, Tag& tag, std::error_code& ec)
{
auto popFront = [&]() -> std::uint8_t {
if (slice.empty())
Expand All @@ -259,18 +134,17 @@ decodePreamble(Slice& slice, Preamble& p, std::error_code& ec)
if (ec)
return;

p.tag_.classId = static_cast<ClassId>(curByte >> 6);
p.tag_.primitive = !(curByte & (1 << 5));
tag.classId = static_cast<ClassId>(curByte >> 6);
tag.primitive = !(curByte & (1 << 5));

// decode the tag
if ((curByte & 0x1f) != 0x1f)
{
p.tag_.tagNum = curByte & 0x1f;
tag.tagNum = curByte & 0x1f;
}
else
{
std::uint64_t tagNum = 0;

do
{
curByte = popFront();
Expand All @@ -297,7 +171,7 @@ decodePreamble(Slice& slice, Preamble& p, std::error_code& ec)

} while (curByte & (1 << 7));

p.tag_.tagNum = tagNum;
tag.tagNum = tagNum;
if (tagNum <= 30)
{
// tag was encoded with the long form, but should have been short
Expand All @@ -306,12 +180,66 @@ decodePreamble(Slice& slice, Preamble& p, std::error_code& ec)
return;
}
}
}

std::uint64_t
contentLengthLength(std::uint64_t v)
{
if (v <= 127)
return 1;

constexpr std::uint64_t chunkSize = 8;
constexpr std::uint64_t nChunks = sizeof(v);
auto const lz = numLeadingZeroChunks<chunkSize>(v, nChunks);
return nChunks - lz + 1;
}

void
encodeContentLength(MutableSlice& dst, std::uint64_t v, std::error_code& ec)
{
if (v <= 127)
{
if (dst.size() != 1)
{
ec = make_error_code(Error::logicError);
return;
}
dst.push_back(static_cast<char>(v));
return;
}

auto n = contentLengthLength(v);

if (dst.size() != n)
{
ec = make_error_code(Error::logicError);
return;
}

--n;
dst.push_back(static_cast<char>(n) | (1 << 7));

while (n--)
dst.push_back(static_cast<char>((v >> (n * 8)) & 0xFF));
}

void
decodeContentLength(Slice& slice, std::uint64_t& contentLength, std::error_code& ec)
{
auto popFront = [&]() -> std::uint8_t {
if (slice.empty())
{
ec = make_error_code(Error::shortGroup);
return 0;
}
auto const r = slice[0];
slice += 1;
return r;
};

// decode the content length
std::uint64_t& contentLength = p.contentLength_;
contentLength = 0;

curByte = popFront();
std::uint8_t curByte = popFront();
if (ec)
return;
if (curByte <= 127)
Expand All @@ -336,6 +264,50 @@ decodePreamble(Slice& slice, Preamble& p, std::error_code& ec)
}
}

std::uint64_t
tagLength(Tag t)
{
return tagNumLength(t.tagNum);
}

void
encodePreamble(MutableSlice& dst, Preamble const& p, std::error_code& ec)
{
if (dst.size() <= 1)
{
ec = make_error_code(Error::logicError);
return;
}

char d = (static_cast<uint8_t>(p.tag_.classId) << 6);
if (!p.tag_.primitive)
d |= 1 << 5;

if (p.tag_.tagNum <= 30)
{
d |= p.tag_.tagNum;
dst.push_back(d);
}
else
{
d |= 0x1f;
dst.push_back(d);
encodeTagNum(dst, p.tag_.tagNum, ec);
if (ec)
return;
}
encodeContentLength(dst, p.contentLength_, ec);
}

void
decodePreamble(Slice& slice, Preamble& p, std::error_code& ec)
{
decodeTag(slice, p.tag_, ec);
if (ec)
return;
decodeContentLength(slice, p.contentLength_, ec);
}

//------------------------------------------------------------------------------

Group::Group(
Expand Down
Loading

0 comments on commit d259692

Please sign in to comment.