From 18aac8e7315f199e02d04edd9925b98b4dcc01ad Mon Sep 17 00:00:00 2001 From: Hanqin Guan <53598282+harveyghq@users.noreply.github.com> Date: Thu, 27 Jun 2024 07:37:38 +0000 Subject: [PATCH] Better messages for 'return' and 'unreachable' instructions --- .gitignore | 3 +++ seewasm/arch/wasm/emulator.py | 6 ++++-- test.py | 29 ++++++++++++++++------------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 8437178c..9140856b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +env/ +.devcontainer/ +.pytest_cache/ .DS_Store __pycache__ .idea diff --git a/seewasm/arch/wasm/emulator.py b/seewasm/arch/wasm/emulator.py index 219f8b63..3fe23fca 100644 --- a/seewasm/arch/wasm/emulator.py +++ b/seewasm/arch/wasm/emulator.py @@ -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 diff --git a/test.py b/test.py index 67f6ec9c..c9eb4f6f 100644 --- a/test.py +++ b/test.py @@ -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/' @@ -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]) @@ -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' @@ -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"]}'