Skip to content

Commit

Permalink
Merge pull request #102 from harveyghq/main
Browse files Browse the repository at this point in the history
Fix 'return' and 'unreachable' testcase failure
  • Loading branch information
HNYuuu authored Jun 28, 2024
2 parents 0edf19c + 18aac8e commit d01dc69
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
env/
.devcontainer/
.pytest_cache/
.DS_Store
__pycache__
.idea
Expand Down
6 changes: 4 additions & 2 deletions seewasm/arch/wasm/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,12 @@ def emulate_basic_block(self, states, instructions, lvar=None):
"""
for instruction in instructions:
if instruction.name == "return":
logging.debug("got 'return' instruction, now return")
stderr_msg = "got 'return' instruction, now return\n"
states[0].file_sys[2]['content'] += [ord(i) for i in stderr_msg]
break
if instruction.name == "unreachable":
logging.debug("got 'unreachable' instruction, now terminate")
stderr_msg = "got 'unreachable' instruction, now terminate\n"
states[0].file_sys[2]['content'] += [ord(i) for i in stderr_msg]
raise ProcFailTermination(ASSERT_FAIL)
next_states = []
for state in states: # TODO: embarassing parallel
Expand Down
29 changes: 16 additions & 13 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import sys
import json
import glob
import os
import pytest
from os import path
import resource
import subprocess
import glob
import sys

# Set a memory limit of 4GB
resource.setrlimit(resource.RLIMIT_AS, (4 * 1024 * 1024 * 1024, -1))

testcase_dir = './test/'

Expand All @@ -11,10 +16,11 @@
('hello_world_go.wasm', '_start'),
('hello_world_rust.wasm', ''),
('test.wasm', ''),
('password.wasm', '')
])

def test_wasm_can_be_analyzed(wasm_path, entry):
wasm_path = path.join(testcase_dir, wasm_path)
wasm_path = os.path.join(testcase_dir, wasm_path)
cmd = [sys.executable, 'launcher.py', '-f', wasm_path, '-s', '-v', 'info']
if entry != "":
cmd.extend(['--entry', entry])
Expand All @@ -31,10 +37,9 @@ def test_return_simulation():
state_path = glob.glob(f'{result_dir}/state*.json')
assert len(state_path) == 1, 'should have only one state output `Exit 0`'

proc = subprocess.run(['jq', '.Solution.proc_exit', state_path[0]], capture_output=True, check=True)
out = proc.stdout.decode('utf-8').strip()
expect = '"\\u0000"'
assert out == expect, f'expect {expect}, got {out}'
with open(state_path[0], 'r') as f:
state = json.load(f)
assert state['Solution']['proc_exit'] == "\u0000", f'exit code should be 0, got {state["Solution"]["proc_exit"]}'

def test_unreachable_simulation():
wasm_path = './test/test_unreachable.wasm'
Expand All @@ -46,8 +51,6 @@ def test_unreachable_simulation():
result_dir = result_dir[0]
state_path = glob.glob(f'{result_dir}/state*.json')
assert len(state_path) == 1, 'should have only one state output `null`'

proc = subprocess.run(['jq', '.Solution.proc_exit', state_path[0]], capture_output=True, check=True)
out = proc.stdout.decode('utf-8').strip()
expect = 'null'
assert out == expect, f'expect {expect}, got {out}'
with open(state_path[0], 'r') as f:
state = json.load(f)
assert state['Solution'] == {}, f'should have no solution, got {state["Solution"]}'

0 comments on commit d01dc69

Please sign in to comment.