From c2bd94fb7a36bb9b09242e013b50b0ad58bc0f84 Mon Sep 17 00:00:00 2001 From: Owanesh <24321561+Owanesh@users.noreply.github.com> Date: Wed, 1 Apr 2026 18:22:07 +0200 Subject: [PATCH] fix(parser): handle single-line functions in parseWast Empty-body functions like (func $f (type $t0) (param $p0 i32))` have balanced brackets on a single line, so `func_bracketNum` drops to 0 immediately. The parser never finalized them, leaking their header into the next function's header list. `opaque_predicate_insertion` then appended `(local $wadelocal i32)` to the wrong line, producing invalid .wat that breaks wat2wasm --- strategies/code_perturbation.py | 4 ++-- wasmParser/parser.py | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/strategies/code_perturbation.py b/strategies/code_perturbation.py index c29ac3f..a2cfd5c 100644 --- a/strategies/code_perturbation.py +++ b/strategies/code_perturbation.py @@ -1314,9 +1314,9 @@ def opaque_predicate_insertion(parsedSection, alpha, beta, ratio): for f in functions: # print(f) function = parsedSection['Function'][f] - if function.body == None: + if function.body is None or len(function.body) == 0: continue - + body = function.body offset_list = list() diff --git a/wasmParser/parser.py b/wasmParser/parser.py index 458d467..fa1b0ac 100755 --- a/wasmParser/parser.py +++ b/wasmParser/parser.py @@ -264,10 +264,27 @@ def parseWast(origin_wast): funcHeaderArea = True ##function header line이 여러개인 경우, payload와 분류하기 위해 funcHeaderLine.append([line, '']) - + parsedSection["Function"][funcID] = ss.FUNCTION(type=funcType) - - + + # Handle single-line functions where brackets balance on one line + # e.g. (func $f44 (type $t0) (param $p0 i32)) + if func_bracketNum == 0: + funcHeaderStr = ''.join([h[0].strip() for h in funcHeaderLine]) + params = parse_param_local_blocks(funcHeaderStr, RE_PARAM) + locals_ = parse_param_local_blocks(funcHeaderStr, RE_LOCAL) + has_result = re.search(RE_RESULT, funcHeaderStr) + result = has_result.group(1) if has_result is not None else None + + parsedSection["Function"][funcID].param = params + parsedSection["Function"][funcID].result = result + parsedSection["Function"][funcID].local = locals_ + parsedSection["Function"][funcID].header = funcHeaderLine + parsedSection["Function"][funcID].body = [] + + funcHeaderArea = False + funcHeaderLine = list() + # print(whichSection) if 1 not in whichSection and (funcHeaderArea==True or funcBodyArea==True):