From 84316e8fc3448932c4d62d1e749047aaacf02ef2 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 28 Jun 2023 15:19:52 -0700 Subject: Limit printing of Literal[s] in a general way (#5792) Previously we limited printing in a single Literals. But we can have infinitely recursive GC literals, or just huge graphs even without infinite recursion where no single Literals is that big (but we still get exponential blowup). This PR adds a general limit on how much we print once we start to print a Literal or Literals. --- src/wasm/literal.cpp | 65 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/wasm/literal.cpp b/src/wasm/literal.cpp index a0e3d2bf6..075028127 100644 --- a/src/wasm/literal.cpp +++ b/src/wasm/literal.cpp @@ -540,7 +540,36 @@ void Literal::printVec128(std::ostream& o, const std::array& v) { o << std::dec; } +// Printing literals is mainly for debugging purposes, and they can be of +// massive size or even infinitely recursive, so abbreviate past some point. +// We do so by tracking how much we've printed so far, and whether we are the +// "toplevel" call (the entry point from somewhere else), and we reset the +// count when we leave the toplevel call. +namespace { +struct PrintLimiter { + static const size_t PRINT_LIMIT = 100; + static thread_local size_t printed; + + bool isTopLevel; + + PrintLimiter() : isTopLevel(printed == 0) { printed++; } + + ~PrintLimiter() { + if (isTopLevel) { + printed = 0; + } + } + + bool stop() { return printed >= PRINT_LIMIT; } +}; + +thread_local size_t PrintLimiter::printed = 0; + +} // namespace + std::ostream& operator<<(std::ostream& o, Literal literal) { + PrintLimiter limiter; + prepareMinorColor(o); assert(literal.type.isSingle()); if (literal.type.isBasic()) { @@ -618,7 +647,6 @@ std::ostream& operator<<(std::ostream& o, Literal literal) { assert(literal.isData()); auto data = literal.getGCData(); assert(data); - // TODO: infinite recursion is possible here, if the data is cyclic o << "[ref " << data->type << ' ' << data->values << ']'; } } @@ -626,27 +654,32 @@ std::ostream& operator<<(std::ostream& o, Literal literal) { return o; } -// Printing literals is mainly for debugging purposes, and they can be of -// massive size, so abbreviate past some point. -static size_t LITERALS_PRINT_LIMIT = 20; - std::ostream& operator<<(std::ostream& o, wasm::Literals literals) { + PrintLimiter limiter; + + if (limiter.stop()) { + return o << "[..]"; + } + if (literals.size() == 1) { return o << literals[0]; - } else { - o << '('; - if (literals.size() > 0) { - o << literals[0]; + } + + o << '('; + bool first = true; + for (auto& literal : literals) { + if (limiter.stop()) { + o << "[..]"; + break; } - for (size_t i = 1; i < literals.size(); ++i) { - o << ", " << literals[i]; - if (i == LITERALS_PRINT_LIMIT) { - o << "[..]"; - break; - } + if (first) { + first = false; + } else { + o << ", "; } - return o << ')'; + o << literal; } + return o << ')'; } Literal Literal::countLeadingZeroes() const { -- cgit v1.2.3