Skip to content

Commit

Permalink
Add assertion for stack operation during parsing
Browse files Browse the repository at this point in the history
Signed-off-by: HyukWoo Park <hyukwoo.park@samsung.com>
  • Loading branch information
clover2123 authored and ksh8281 committed Apr 18, 2024
1 parent 4408832 commit dea5797
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
27 changes: 17 additions & 10 deletions src/parser/WASMParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,16 +535,20 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {

m_vmStack.push_back(VMStackInfo(*this, type, pos, m_functionStackSizeSoFar, localIndex));
size_t allocSize = Walrus::valueStackAllocatedSize(type);
if (UNLIKELY(m_functionStackSizeSoFar + allocSize > std::numeric_limits<Walrus::ByteCodeStackOffset>::max())) {
throw std::string("too many stack usage. we could not support this(yet).");
}
// FIXME too many stack usage. we could not support this(yet)
ASSERT(m_functionStackSizeSoFar + allocSize <= std::numeric_limits<Walrus::ByteCodeStackOffset>::max());

m_functionStackSizeSoFar += allocSize;
m_currentFunction->m_requiredStackSize = std::max(
m_currentFunction->m_requiredStackSize, m_functionStackSizeSoFar);
}

VMStackInfo popVMStackInfo()
{
// FIXME This error can occur during the parsing process because of invalid wasm instructions
// e.g. a function with no end opcode
ASSERT(m_vmStack.size() > 0);

auto info = m_vmStack.back();
m_functionStackSizeSoFar -= Walrus::valueStackAllocatedSize(info.valueType());
m_vmStack.pop_back();
Expand All @@ -567,24 +571,27 @@ class WASMBinaryReader : public wabt::WASMBinaryReaderDelegate {
return info;
}

size_t popVMStack()
VMStackInfo& peekVMStackInfo()
{
return popVMStackInfo().position();
// FIXME This error can occur during the parsing process because of invalid wasm instructions
// e.g. a function with no end opcode
ASSERT(m_vmStack.size() > 0);
return m_vmStack.back();
}

size_t peekVMStack()
size_t popVMStack()
{
return m_vmStack.back().position();
return popVMStackInfo().position();
}

VMStackInfo& peekVMStackInfo()
size_t peekVMStack()
{
return m_vmStack.back();
return peekVMStackInfo().position();
}

Walrus::Value::Type peekVMStackValueType()
{
return m_vmStack.back().valueType();
return peekVMStackInfo().valueType();
}

void beginFunction(Walrus::ModuleFunction* mf, bool inInitExpr)
Expand Down
14 changes: 7 additions & 7 deletions third_party/wabt/src/walrus/binary-reader-walrus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1350,16 +1350,16 @@ std::string ReadWasmBinary(const std::string &filename, const uint8_t *data, siz
const bool kFailOnCustomSectionError = true;
ReadBinaryOptions options(getFeatures(), nullptr, kReadDebugNames, kStopOnFirstError, kFailOnCustomSectionError);
BinaryReaderDelegateWalrus binaryReaderDelegateWalrus(delegate, filename);
try {
ReadBinary(data, size, &binaryReaderDelegateWalrus, options);
} catch(const std::string& err) {
// error from WASMBinaryReader
return err;
}
Result result = ReadBinary(data, size, &binaryReaderDelegateWalrus, options);

if (binaryReaderDelegateWalrus.m_errors.size()) {
if (WABT_UNLIKELY(binaryReaderDelegateWalrus.m_errors.size())) {
return std::move(binaryReaderDelegateWalrus.m_errors.begin()->message);
}

if (WABT_UNLIKELY(result != ::wabt::Result::Ok)) {
return std::string("read wasm error");
}

return std::string();
}

Expand Down
6 changes: 3 additions & 3 deletions tools/run-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def run_core_tests(engine):


@runner('wasi', default=True)
def run_basic_tests(engine):
def run_wasi_tests(engine):
TEST_DIR = join(PROJECT_SOURCE_DIR, 'test', 'wasi')

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


@runner('jit', default=True)
def run_basic_tests(engine):
def run_jit_tests(engine):
TEST_DIR = join(PROJECT_SOURCE_DIR, 'test', 'jit')

print('Running jit tests:')
Expand All @@ -142,7 +142,7 @@ def run_basic_tests(engine):
tests_total = len(xpass)
fail_total = xpass_result
print('TOTAL: %d' % (tests_total))
print('%sPASS : %d%s' % (COLOR_GREEN, tests_total, COLOR_RESET))
print('%sPASS : %d%s' % (COLOR_GREEN, tests_total - fail_total, COLOR_RESET))
print('%sFAIL : %d%s' % (COLOR_RED, fail_total, COLOR_RESET))

if fail_total > 0:
Expand Down

0 comments on commit dea5797

Please sign in to comment.