From 31fa85a64846dd8c447b021b92d32eae9b94b981 Mon Sep 17 00:00:00 2001 From: palkeo Date: Thu, 14 May 2020 22:21:02 +1200 Subject: [PATCH] Check that decompilation.json is actually serializable. --- pano/decompiler.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pano/decompiler.py b/pano/decompiler.py index f5bbb53f..c74b298c 100644 --- a/pano/decompiler.py +++ b/pano/decompiler.py @@ -1,6 +1,8 @@ import dataclasses import io +import json import logging +import os import sys from contextlib import redirect_stdout @@ -25,7 +27,6 @@ class Decompilation: json: dict = dataclasses.field(default_factory=dict) - def decompile_bytecode(code: str, only_func_name=None) -> Decompilation: loader = Loader() loader.load_binary(code) # Code is actually hex. @@ -140,7 +141,7 @@ def _decompile_with_loader(loader, only_func_name=None) -> Decompilation: if target > 1 and loader.lines[target][1] == "jumpdest": target += 1 - @timeout_decorator.timeout(60*3, use_signals=True) + @timeout_decorator.timeout(60 * 3, use_signals=True) def dec(): trace = VM(loader).run(target, stack=stack, timeout=60) explain("Initial decompiled trace", trace[1:]) @@ -177,10 +178,7 @@ def dec(): """ - contract = Contract( - problems=problems, - functions=functions, - ) + contract = Contract(problems=problems, functions=functions,) contract.postprocess() @@ -191,8 +189,12 @@ def dec(): try: decompilation.json = contract.json() + # This would raise a TypeError if it's not serializable, which is an + # important assumption people can make. + json.dump(decompilation.json, open(os.devnull, "w")) except Exception: - logger.exception("failed json dump") + logger.exception("Failed json serialization.") + decompilation.json = {} text_output = io.StringIO() with redirect_stdout(text_output):