Skip to content

Commit 6b5327c

Browse files
authored
[NFC] Avoid huge output in validator messages (#7334)
Nested expressions that all fail to validate can lead to quadratic output and OOMs. Instead, put a limit on the size we print, and after it only emit truncated error messages of fixed size. Also make some of the types more precise in this code, which makes it easier to read. Fixes #7333
1 parent 4596b98 commit 6b5327c

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/wasm/wasm-validator.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,28 @@ template<typename T,
4242
Expression,
4343
typename std::remove_pointer<T>::type>::value>::type* = nullptr>
4444
inline std::ostream&
45-
printModuleComponent(T curr, std::ostream& stream, Module& wasm) {
45+
printModuleComponent(T curr, std::ostringstream& stream, Module& wasm) {
4646
stream << curr << std::endl;
4747
return stream;
4848
}
4949

5050
// Extra overload for Expressions, to print their contents.
51-
inline std::ostream&
52-
printModuleComponent(Expression* curr, std::ostream& stream, Module& wasm) {
51+
inline std::ostream& printModuleComponent(Expression* curr,
52+
std::ostringstream& stream,
53+
Module& wasm) {
5354
if (curr) {
54-
stream << ModuleExpression(wasm, curr) << '\n';
55+
// Print the full expression if we can, but avoid doing so if the output is
56+
// already very large. This avoids quadratic output in some cases (e.g. if
57+
// we have many nested expressions in each other, all of which fail to
58+
// validate).
59+
const std::ostringstream::pos_type MAX_OUTPUT = 16 * 1024;
60+
if (stream.tellp() < MAX_OUTPUT) {
61+
stream << ModuleExpression(wasm, curr) << '\n';
62+
} else {
63+
// Print something, at least.
64+
stream << "[not printing " << getExpressionName(curr)
65+
<< " because output is already very large]\n";
66+
}
5567
}
5668
return stream;
5769
}
@@ -98,7 +110,7 @@ struct ValidationInfo {
98110
return printModuleComponent(curr, ret, wasm);
99111
}
100112

101-
std::ostream& printFailureHeader(Function* func) {
113+
std::ostringstream& printFailureHeader(Function* func) {
102114
auto& stream = getStream(func);
103115
if (quiet) {
104116
return stream;

0 commit comments

Comments
 (0)