Skip to content

Commit dea5797

Browse files
clover2123ksh8281
authored andcommitted
Add assertion for stack operation during parsing
Signed-off-by: HyukWoo Park <[email protected]>
1 parent 4408832 commit dea5797

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/parser/WASMParser.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -535,16 +535,20 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
535535

536536
m_vmStack.push_back(VMStackInfo(*this, type, pos, m_functionStackSizeSoFar, localIndex));
537537
size_t allocSize = Walrus::valueStackAllocatedSize(type);
538-
if (UNLIKELY(m_functionStackSizeSoFar + allocSize > std::numeric_limits<Walrus::ByteCodeStackOffset>::max())) {
539-
throw std::string("too many stack usage. we could not support this(yet).");
540-
}
538+
// FIXME too many stack usage. we could not support this(yet)
539+
ASSERT(m_functionStackSizeSoFar + allocSize <= std::numeric_limits<Walrus::ByteCodeStackOffset>::max());
540+
541541
m_functionStackSizeSoFar += allocSize;
542542
m_currentFunction->m_requiredStackSize = std::max(
543543
m_currentFunction->m_requiredStackSize, m_functionStackSizeSoFar);
544544
}
545545

546546
VMStackInfo popVMStackInfo()
547547
{
548+
// FIXME This error can occur during the parsing process because of invalid wasm instructions
549+
// e.g. a function with no end opcode
550+
ASSERT(m_vmStack.size() > 0);
551+
548552
auto info = m_vmStack.back();
549553
m_functionStackSizeSoFar -= Walrus::valueStackAllocatedSize(info.valueType());
550554
m_vmStack.pop_back();
@@ -567,24 +571,27 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
567571
return info;
568572
}
569573

570-
size_t popVMStack()
574+
VMStackInfo& peekVMStackInfo()
571575
{
572-
return popVMStackInfo().position();
576+
// FIXME This error can occur during the parsing process because of invalid wasm instructions
577+
// e.g. a function with no end opcode
578+
ASSERT(m_vmStack.size() > 0);
579+
return m_vmStack.back();
573580
}
574581

575-
size_t peekVMStack()
582+
size_t popVMStack()
576583
{
577-
return m_vmStack.back().position();
584+
return popVMStackInfo().position();
578585
}
579586

580-
VMStackInfo& peekVMStackInfo()
587+
size_t peekVMStack()
581588
{
582-
return m_vmStack.back();
589+
return peekVMStackInfo().position();
583590
}
584591

585592
Walrus::Value::Type peekVMStackValueType()
586593
{
587-
return m_vmStack.back().valueType();
594+
return peekVMStackInfo().valueType();
588595
}
589596

590597
void beginFunction(Walrus::ModuleFunction* mf, bool inInitExpr)

third_party/wabt/src/walrus/binary-reader-walrus.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,16 +1350,16 @@ std::string ReadWasmBinary(const std::string &filename, const uint8_t *data, siz
13501350
const bool kFailOnCustomSectionError = true;
13511351
ReadBinaryOptions options(getFeatures(), nullptr, kReadDebugNames, kStopOnFirstError, kFailOnCustomSectionError);
13521352
BinaryReaderDelegateWalrus binaryReaderDelegateWalrus(delegate, filename);
1353-
try {
1354-
ReadBinary(data, size, &binaryReaderDelegateWalrus, options);
1355-
} catch(const std::string& err) {
1356-
// error from WASMBinaryReader
1357-
return err;
1358-
}
1353+
Result result = ReadBinary(data, size, &binaryReaderDelegateWalrus, options);
13591354

1360-
if (binaryReaderDelegateWalrus.m_errors.size()) {
1355+
if (WABT_UNLIKELY(binaryReaderDelegateWalrus.m_errors.size())) {
13611356
return std::move(binaryReaderDelegateWalrus.m_errors.begin()->message);
13621357
}
1358+
1359+
if (WABT_UNLIKELY(result != ::wabt::Result::Ok)) {
1360+
return std::string("read wasm error");
1361+
}
1362+
13631363
return std::string();
13641364
}
13651365

tools/run-tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def run_core_tests(engine):
114114

115115

116116
@runner('wasi', default=True)
117-
def run_basic_tests(engine):
117+
def run_wasi_tests(engine):
118118
TEST_DIR = join(PROJECT_SOURCE_DIR, 'test', 'wasi')
119119

120120
print('Running wasi tests:')
@@ -132,7 +132,7 @@ def run_basic_tests(engine):
132132

133133

134134
@runner('jit', default=True)
135-
def run_basic_tests(engine):
135+
def run_jit_tests(engine):
136136
TEST_DIR = join(PROJECT_SOURCE_DIR, 'test', 'jit')
137137

138138
print('Running jit tests:')
@@ -142,7 +142,7 @@ def run_basic_tests(engine):
142142
tests_total = len(xpass)
143143
fail_total = xpass_result
144144
print('TOTAL: %d' % (tests_total))
145-
print('%sPASS : %d%s' % (COLOR_GREEN, tests_total, COLOR_RESET))
145+
print('%sPASS : %d%s' % (COLOR_GREEN, tests_total - fail_total, COLOR_RESET))
146146
print('%sFAIL : %d%s' % (COLOR_RED, fail_total, COLOR_RESET))
147147

148148
if fail_total > 0:

0 commit comments

Comments
 (0)