Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 58 additions & 34 deletions lib/decoder/PayloadDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ MAT_NS_END
#else
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <limits>
#include <new>
Expand Down Expand Up @@ -391,7 +393,9 @@ namespace clienttelemetry {
/// <param name="destLen"></param>
/// <param name="prependSize"></param>
/// <returns></returns>
bool Compress(const char* source, size_t sourceLen, char** dest, size_t& destLen, bool prependSize)
// Successful custom allocations must be compatible with delete[].
bool Compress(const char* source, size_t sourceLen, char** dest, size_t& destLen, bool prependSize,
char* (*allocate)(size_t))
{
if ((!source) || (!sourceLen))
{
Expand All @@ -401,38 +405,48 @@ namespace clienttelemetry {
*dest = NULL;
destLen = 0;

// Compressing variables
uLong compSize = compressBound((uLong)sourceLen);
const auto inputSize = static_cast<std::uintmax_t>(sourceLen);
if (inputSize > static_cast<std::uintmax_t>(std::numeric_limits<uLong>::max()) ||
(prependSize && inputSize > static_cast<std::uintmax_t>(std::numeric_limits<uint32_t>::max())))
{
return false;
}

// Allocate memory for the new compressed buffer
size_t reserved = ((unsigned)prependSize * sizeof(uint32_t));
char* compBody = new char[std::max(compSize, ((uLong)sourceLen)) + reserved];
if (compBody != NULL)
uLong compSize = compressBound(static_cast<uLong>(sourceLen));
const size_t reserved = prependSize ? sizeof(uint32_t) : 0;
const size_t maxAllocation = static_cast<size_t>(std::numeric_limits<ptrdiff_t>::max());
if (compSize == std::numeric_limits<uLong>::max() || compSize < sourceLen ||
sourceLen > maxAllocation - reserved || compSize > maxAllocation - reserved)
{
if (prependSize)
{
// Remember source uncompressed size if requested
uint32_t *s = (uint32_t*)(compBody);
(*s) = (uint32_t)sourceLen; // truncate this to 32-bit, we do not support 3+ TB blobs
}
// Deflate
int res = compress2((Bytef *)(compBody + reserved), &compSize, (Bytef *)source, (uLong)sourceLen, Z_BEST_SPEED);
if (res != Z_OK)
{
TEST_LOG_ERROR("Compression failed, error=%u", res);
delete[] compBody;
compBody = NULL;
return false;
}
else
{
*dest = compBody;
destLen = compSize + reserved;
return true;
}
return false;
}
// OOM
return false;

char* compBody = allocate(std::max(static_cast<size_t>(compSize), sourceLen) + reserved);
if (compBody == nullptr)
{
return false;
}
if (prependSize)
{
const uint32_t originalSize = static_cast<uint32_t>(sourceLen);
std::memcpy(compBody, &originalSize, sizeof(originalSize));
}
int res = compress2((Bytef *)(compBody + reserved), &compSize, (Bytef *)source, (uLong)sourceLen, Z_BEST_SPEED);
if (res != Z_OK)
{
TEST_LOG_ERROR("Compression failed, error=%u", res);
delete[] compBody;
return false;
}
*dest = compBody;
destLen = compSize + reserved;
return true;
}

bool Compress(const char* source, size_t sourceLen, char** dest, size_t& destLen, bool prependSize)
{
return Compress(source, sourceLen, dest, destLen, prependSize,
[](size_t size) { return new (std::nothrow) char[size]; });
}

/// <summary>
Expand Down Expand Up @@ -460,13 +474,23 @@ namespace clienttelemetry {
// Get uncompressed size at zero offset.
if (sizeAtZeroIndex)
{
uint32_t s32 = *((uint32_t*)(source));
uint64_t s64 = (sourceLen >= sizeof(uint64_t)) ? *((uint64_t*)(source)) : 0;
if (sourceLen < sizeof(uint32_t))
{
destLen = 0;
return false;
}
uint32_t s32 = 0;
std::memcpy(&s32, source, sizeof(s32));
// If we are reading 64-bit generated legacy DB, step 32-bit forward to
// skip zero-padding in most-significant DWORD on Intel architecture
if ((s64 - s32) == 0)
if (sourceLen >= sizeof(uint64_t))
{
reserved += sizeof(uint32_t);
uint64_t s64 = 0;
std::memcpy(&s64, source, sizeof(s64));
if (s64 == s32)
{
reserved += sizeof(uint32_t);
}
}
destLen = s32;
}
Expand Down
52 changes: 51 additions & 1 deletion tests/no-exceptions/no-exceptions-smoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include "http/HttpResponseDecoder.hpp"
#include "utils/ZlibUtils.hpp"

#include <atomic>
#include <cstdio>
#include <cstdlib>
#include <limits>
#include <new>

#if HAVE_EXCEPTIONS
#error This regression executable must be compiled without C++ exceptions.
Expand All @@ -19,9 +21,25 @@
#error This regression executable requires JSON and zlib support.
#endif

namespace
{
std::atomic<bool> failNextNothrowArrayAllocation {false};
}

void* operator new[](std::size_t size, const std::nothrow_t&) noexcept
{
if (failNextNothrowArrayAllocation.exchange(false))
{
return nullptr;
}
return ::operator new[](size);
}

namespace clienttelemetry {
namespace data {
namespace v3 {
bool Compress(const char*, size_t, char**, size_t&, bool);
bool Compress(const char*, size_t, char**, size_t&, bool, char* (*)(size_t));
bool Expand(const char*, size_t, char**, size_t&, bool);
bool Expand(const char*, size_t, char**, size_t&, bool, char* (*)(size_t));
}
Expand Down Expand Up @@ -151,6 +169,14 @@ int main()
"oversized expansion must fail");
check(output == nullptr && outputSize == 0, "failed expansion must clear outputs");
char previousOutput = 0;
for (size_t length = 1; length < sizeof(uint32_t); ++length)
{
output = &previousOutput;
outputSize = 32;
check(!clienttelemetry::data::v3::Expand(invalidCompressed, length, &output, outputSize, true),
"short size prefix must fail");
check(output == nullptr && outputSize == 0, "short size prefix must clear outputs");
}
output = &previousOutput;
outputSize = 32;
static size_t requestedSize = 0;
Expand All @@ -161,6 +187,30 @@ int main()
}), "representable allocation failure must return false");
check(requestedSize == 32, "failure injection must reach the allocator");
check(output == nullptr && outputSize == 0, "allocation failure must clear an existing output and its length");
std::puts("Passed no-exceptions configuration, response, profile, numeric-limit, and expansion smoke checks.");
output = &previousOutput;
outputSize = 32;
requestedSize = 0;
check(!clienttelemetry::data::v3::Compress(invalidCompressed, sizeof(invalidCompressed), &output, outputSize, false,
[](size_t size) -> char* {
requestedSize = size;
return nullptr;
}), "compression allocation failure must return false");
check(requestedSize > sizeof(invalidCompressed), "compression failure injection must reach the allocator");
check(output == nullptr && outputSize == 0, "failed compression must clear output and its length");
output = &previousOutput;
outputSize = 32;
failNextNothrowArrayAllocation = true;
const bool compressed = clienttelemetry::data::v3::Compress(
invalidCompressed, sizeof(invalidCompressed), &output, outputSize, false);
check(!failNextNothrowArrayAllocation, "default compression must use the nothrow array allocator");
check(!compressed && output == nullptr && outputSize == 0,
"default compression must report allocation failure without exceptions");
output = &previousOutput;
outputSize = 32;
check(!clienttelemetry::data::v3::Compress(invalidCompressed, std::numeric_limits<size_t>::max(),
&output, outputSize, true),
"unrepresentable compression size must fail");
check(output == nullptr && outputSize == 0, "oversized compression must clear output and its length");
std::puts("Passed no-exceptions configuration, response, profile, numeric-limit, and compression smoke checks.");
return EXIT_SUCCESS;
}
Loading
Loading