Skip to content
Closed
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
2 changes: 1 addition & 1 deletion include/sonic/dom/dynamicnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class DNode : public GenericNode<DNode<Allocator>> {
size_t cap;
map_type* map;

~MetaNode() {
~MetaNode() __attribute__((noinline)) {
if (map) {
map->~map_type();
Allocator::Free(map);
Expand Down
8 changes: 6 additions & 2 deletions tests/allocator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ namespace {

using namespace sonic_json;

size_t RuntimeHugeAllocationSize() {
volatile size_t size = size_t{1} << 62;
return size;
}

#ifdef SONIC_MEMSTAT
#define MEMSTAT_ISEMPTY() EXPECT_TRUE(MemStat::Instance().stat.empty())
#define MEMSTAT_NOTEMPTY() EXPECT_FALSE(MemStat::Instance().stat.empty())
Expand Down Expand Up @@ -217,8 +222,7 @@ TEST(Stack, ConstructorOomLeavesConsistentState) {
// (absent) buffer. Otherwise Grow()'s guard `top_+cnt >= buf_+cap_` reads
// as `1 >= cap_ + 0` and skips the re-allocation entirely, letting a
// subsequent Push() dereference a null top_.
constexpr size_t kHuge = (size_t{1} << 62);
sonic_json::internal::Stack s(kHuge);
sonic_json::internal::Stack s(RuntimeHugeAllocationSize());

if (s.Begin<char>() == nullptr) {
EXPECT_EQ(0u, s.Capacity())
Expand Down
29 changes: 18 additions & 11 deletions tests/parser_oom_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,22 @@ struct AlwaysOomAllocator {
static constexpr bool kNeedFree = false;
};

static constexpr size_t kJsonHeadroom = 64;

static std::vector<uint8_t> pad_json_bytes(const char* json, size_t len) {
std::vector<uint8_t> buf(len + 64, 0);
std::memcpy(buf.data(), json, len);
buf[len] = 'x';
buf[len + 1] = '"';
buf[len + 2] = 'x';
std::vector<uint8_t> buf(kJsonHeadroom + len + 64, 0);
uint8_t* data = buf.data() + kJsonHeadroom;
std::memcpy(data, json, len);
data[len] = 'x';
data[len + 1] = '"';
data[len + 2] = 'x';
return buf;
}

static uint8_t* padded_json_data(std::vector<uint8_t>& buf) {
return buf.data() + kJsonHeadroom;
}

TEST(Document, OomDoesNotCrashPushBack) {
AlwaysOomAllocator alloc;
DNode<AlwaysOomAllocator> arr;
Expand Down Expand Up @@ -244,7 +251,7 @@ TEST(Document, ParseLazyEscapedKeyOomReportsNoMem) {
Parser<ParseFlags::kParseDefault> p;
const char* json = R"({"\n": 1})";
auto buf = pad_json_bytes(json, std::strlen(json));
auto res = p.ParseLazy(buf.data(), std::strlen(json), sax);
auto res = p.ParseLazy(padded_json_data(buf), std::strlen(json), sax);
EXPECT_EQ(kErrorNoMem, res.Error());
}

Expand Down Expand Up @@ -304,7 +311,7 @@ TEST(Document, ParseLazyFreesEscapedKeyOnKeyFailure) {
Parser<ParseFlags::kParseDefault> p;
const char* json = R"({"\n": 1})";
auto buf = pad_json_bytes(json, std::strlen(json));
p.ParseLazy(buf.data(), std::strlen(json), sax);
p.ParseLazy(padded_json_data(buf), std::strlen(json), sax);

ASSERT_TRUE(sax.key_called);
EXPECT_EQ(0, SentinelTrackingAllocator::balance);
Expand Down Expand Up @@ -560,7 +567,7 @@ TEST(ParseLazy, RawRejectionReportsSaxTermination) {
Parser<ParseFlags::kParseDefault> p;
const char* j = "42";
auto buf = pad_json_bytes(j, 2);
auto r = p.ParseLazy(buf.data(), 2, sax);
auto r = p.ParseLazy(padded_json_data(buf), 2, sax);
EXPECT_EQ(r.Error(), kSaxTermination);
}

Expand All @@ -569,7 +576,7 @@ TEST(ParseLazy, StartArrayRejectionReportsSaxTermination) {
Parser<ParseFlags::kParseDefault> p;
const char* j = "[1,2,3]";
auto buf = pad_json_bytes(j, 7);
auto r = p.ParseLazy(buf.data(), 7, sax);
auto r = p.ParseLazy(padded_json_data(buf), 7, sax);
EXPECT_EQ(r.Error(), kSaxTermination);
}

Expand All @@ -578,7 +585,7 @@ TEST(ParseLazy, StartObjectRejectionReportsSaxTermination) {
Parser<ParseFlags::kParseDefault> p;
const char* j = R"({"k":1})";
auto buf = pad_json_bytes(j, 7);
auto r = p.ParseLazy(buf.data(), 7, sax);
auto r = p.ParseLazy(padded_json_data(buf), 7, sax);
EXPECT_EQ(r.Error(), kSaxTermination);
}

Expand All @@ -599,7 +606,7 @@ TEST(ParseLazy, AcceptAllStillCompletesCleanly) {
Parser<ParseFlags::kParseDefault> p;
const char* j = R"({"a":1,"b":[2,3]})";
auto buf = pad_json_bytes(j, std::strlen(j));
auto r = p.ParseLazy(buf.data(), std::strlen(j), sax);
auto r = p.ParseLazy(padded_json_data(buf), std::strlen(j), sax);
EXPECT_EQ(r.Error(), kErrorNone);
}

Expand Down
Loading