From b970ea3b2ed9504c45a840656b0ac21c0f6b72f0 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Mon, 21 Oct 2019 20:04:42 +0330 Subject: listened to lgtm... --- README.md | 3 +++ dwasm.py | 5 +---- execute.py | 55 +++++++++++++++++++++++++++---------------------------- init.py | 26 +++++++++++++------------- parse.py | 12 ++---------- utils.py | 18 +++++++++--------- 6 files changed, 55 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 42084b8..87df82a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ + +[![Total alerts](https://img.shields.io/lgtm/alerts/g/bloodstalker/dwasm.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/bloodstalker/dwasm/alerts/) + # dwasm dwasm is a WASM dump tool.
diff --git a/dwasm.py b/dwasm.py index e404dca..ce5646f 100755 --- a/dwasm.py +++ b/dwasm.py @@ -1,17 +1,14 @@ #!/usr/bin/python3 -import argparse import code -import readline import signal import sys from parse import Argparser, premain, SigHandler_SIGINT,PythonInterpreter -from utils import ParseFlags def getWASMModule(): module_path = sys.argv[1] interpreter = PythonInterpreter() - module = interpreter.parse(module_path) + #module = interpreter.parse(module_path) def main(): signal.signal(signal.SIGINT, SigHandler_SIGINT) diff --git a/execute.py b/execute.py index 40b9042..0633629 100644 --- a/execute.py +++ b/execute.py @@ -30,7 +30,7 @@ class Execute(): # pragma: no cover return self.op_gas def chargeGasMem(self, mem_size_page): - factor = 64 + #factor = 64 self.op_gas += 64 * mem_size_page def chargeGas(self, opcodeint): @@ -38,7 +38,6 @@ class Execute(): # pragma: no cover self.op_gas += 1 else: chargeGasMem() - pass def getInstruction(self, opcodeint, immediates): self.opcodeint = opcodeint @@ -56,7 +55,7 @@ class Execute(): # pragma: no cover except IndexError: # trap print(Colors.red + 'bad stack access.' + Colors.ENDC) - val2 = self.machinestate.Stack_Omni.pop() + #val2 = self.machinestate.Stack_Omni.pop() def instructionUnwinder(self, opcodeint, immediates, machinestate): @@ -296,8 +295,8 @@ class Execute(): # pragma: no cover if len(self.machinestate.Stack_Omni) < 1: print(Colors.red + "the value stack does not have enough values." + Colors.ENDC) # exit 1 - val = self.machinestate.Stack_Omni.pop() - label = self.machinestate.Stack_Label.pop() + #val = self.machinestate.Stack_Omni.pop() + #label = self.machinestate.Stack_Label.pop() def run_br_if(self, opcodeint, immediates): val = self.machinestate.Stack_Omni.pop() @@ -360,40 +359,40 @@ class Execute(): # pragma: no cover elif opcodeint == 44: temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1])]) temp2 = (temp & 0x0000007f) | ((temp & 0x80) << 24) - self.machinestate.append(np.int32(tmep2)) + self.machinestate.append(np.int32(temp2)) elif opcodeint == 45: temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1])]) temp2 = temp & 0x000000ff - self.machinestate.append(np.uint32(tmep2)) + self.machinestate.append(np.uint32(temp2)) elif opcodeint == 46: temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates[1] + 2)]) temp2 = (temp & 0x00007fff) | ((temp & 0x8000) << 16) - self.machinestate.append(np.int32(tmep2)) + self.machinestate.append(np.int32(temp2)) elif opcodeint == 47: temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates[1] + 2)]) temp2 = temp & 0x0000ffff - self.machinestate.append(np.uint32(tmep2)) + self.machinestate.append(np.uint32(temp2)) elif opcodeint == 48: temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1])]) temp2 = (temp & 0x000000000000007f) | ((temp & 0x80) << 56) - self.machinestate.append(np.int64(tmep2)) + self.machinestate.append(np.int64(temp2)) elif opcodeint == 49: temp = np.uint8(self.machinestate.Linear_Memory[0][int(immediates[1])]) - self.machinestate.append(np.uint64(tmep)) + self.machinestate.append(np.uint64(temp)) elif opcodeint == 50: temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates[1] + 2)]) temp2 = (temp & 0x0000000000007fff) | ((temp & 0x8000) << 48) - self.machinestate.append(np.int64(tmep2)) + self.machinestate.append(np.int64(temp2)) elif opcodeint == 51: temp = np.uint8(self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates[1] + 2)]) - self.machinestate.append(np.uint64(tmep)) + self.machinestate.append(np.uint64(temp)) elif opcodeint == 52: temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates[1] + 4)]) temp2 = (temp & 0x000000007fffffff) | ((temp & 0x80000000) << 32) - self.machinestate.append(np.int64(tmep2)) + self.machinestate.append(np.int64(temp2)) elif opcodeint == 53: temp = np.uint8(self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates[1] + 4)]) - self.machinestate.append(np.uint64(tmep)) + self.machinestate.append(np.uint64(temp)) else: raise Exception(Colors.red + 'invalid load instruction.' + Colors.ENDC) @@ -623,8 +622,8 @@ class Execute(): # pragma: no cover raise Exception(Colors.red + 'invalid ge_u instruction' + Colors.ENDC) def run_lt(self, opcodeint, immediates): - val2 = self.machinestate.Stack_Omni.pop() - val1 = self.machinestate.Stack_Omni.pop() + v2 = self.machinestate.Stack_Omni.pop() + v1 = self.machinestate.Stack_Omni.pop() if opcodeint == 93: if np.float32(v1) < np.float32(v2): self.machinestate.Stack_Omni.append(1) @@ -639,8 +638,8 @@ class Execute(): # pragma: no cover raise Exception(Colors.red + 'invalid lt instruction' + Colors.ENDC) def run_gt(self, opcodeint, immediates): - val2 = self.machinestate.Stack_Omni.pop() - val1 = self.machinestate.Stack_Omni.pop() + v2 = self.machinestate.Stack_Omni.pop() + v1 = self.machinestate.Stack_Omni.pop() if opcodeint == 94: if np.float32(v1) > np.float32(v2): self.machinestate.Stack_Omni.append(1) @@ -655,8 +654,8 @@ class Execute(): # pragma: no cover raise Exception(Colors.red + 'invalid gt instruction' + Colors.ENDC) def run_le(self, opcodeint, immediates): - val2 = self.machinestate.Stack_Omni.pop() - val1 = self.machinestate.Stack_Omni.pop() + v2 = self.machinestate.Stack_Omni.pop() + v1 = self.machinestate.Stack_Omni.pop() if opcodeint == 95: if np.float32(v1) <= np.float32(v2): self.machinestate.Stack_Omni.append(1) @@ -671,8 +670,8 @@ class Execute(): # pragma: no cover raise Exception(Colors.red + 'invalid le instruction' + Colors.ENDC) def run_ge(self, opcodeint, immediates): - val2 = self.machinestate.Stack_Omni.pop() - val1 = self.machinestate.Stack_Omni.pop() + v2 = self.machinestate.Stack_Omni.pop() + v1 = self.machinestate.Stack_Omni.pop() if opcodeint == 96: if np.float32(v1) >= np.float32(v2): self.machinestate.Stack_Omni.append(1) @@ -687,7 +686,7 @@ class Execute(): # pragma: no cover raise Exception(Colors.red + 'invalid ge instruction' + Colors.ENDC) def run_clz(self, opcodeint, immediates): - val1 = self.machinestate.Stack_Omni.pop() + val = self.machinestate.Stack_Omni.pop() if opcodeint == 103: self.machinestate.Stack_Omni.append(clz(val, 'uint32')) elif opcodeint == 121: @@ -696,7 +695,7 @@ class Execute(): # pragma: no cover raise Exception(Colors.red + 'invalid clz instruction' + Colors.ENDC) def run_ctz(self, opcodeint, immediates): - val1 = self.machinestate.Stack_Omni.pop() + val = self.machinestate.Stack_Omni.pop() if opcodeint == 104: self.machinestate.Stack_Omni.append(ctz(val, 'uint32')) elif opcodeint == 122: @@ -705,7 +704,7 @@ class Execute(): # pragma: no cover raise Exception(Colors.red + 'invalid ctz instruction' + Colors.ENDC) def run_popcnt(self, opcodeint, immediates): - val1 = self.machinestate.Stack_Omni.pop() + val = self.machinestate.Stack_Omni.pop() if opcodeint == 105: self.machinestate.Stack_Omni.append(pop_cnt(val, 'uint32')) elif opcodeint == 123: @@ -924,8 +923,8 @@ class Execute(): # pragma: no cover raise Exception(Colors.red + 'invalid sqrt instruction' + Colors.ENDC) def run_div(self, opcodeint, immediates): - val2 = self.machinestate.Stack_Omni.pop() - val1 = self.machinestate.Stack_Omni.pop() + v2 = self.machinestate.Stack_Omni.pop() + v1 = self.machinestate.Stack_Omni.pop() if opcodeint == 149: self.machinestate.Stack_Omni.append(v1 / v2) else: diff --git a/init.py b/init.py index f3ac986..1d5f204 100644 --- a/init.py +++ b/init.py @@ -1,8 +1,7 @@ -from utils import Colors, init_interpret, ParseFlags +from utils import Colors, init_interpret from opcodes import WASM_OP_Code -from section_structs import Code_Section, Func_Body, WASM_Ins, Resizable_Limits, Memory_Section +from section_structs import Func_Body, WASM_Ins, Resizable_Limits, Memory_Section from execute import * -import datetime as dti import os import sys import signal @@ -196,7 +195,7 @@ class TBInit(): self.module.memory_section = Memory_Section() self.module.memory_section.memory_types = [rsz_limits] self.module.memory_section.count = 1 - for iter in self.module.memory_section.memory_types: + for _ in self.module.memory_section.memory_types: self.machinestate.Linear_Memory.append(bytearray( WASM_OP_Code.PAGE_SIZE)) if self.module.data_section is not None: @@ -214,13 +213,14 @@ class TBInit(): # WIP-holds the run-rime data structures for a wasm machine class RTE(): def __init__(self): - Stack_Control_Flow = list() - Stack_Value = list() - Vector_Locals = list() - Current_Position = int() - Local_Stacks = list() + #Stack_Control_Flow = list() + #Stack_Value = list() + #Vector_Locals = list() + #Current_Position = int() + #Local_Stacks = list() + pass - def genFuncLocalStack(func_body): + def genFuncLocalStack(self, func_body): pass @@ -374,12 +374,12 @@ class VM(): # a wrapper class for VM. it timeouts instructions that take too long to # execute. class Judicator(): - def __int__(self, op_time_table, module): + def __init__(self, op_time_table, module): self.op_time_table = op_time_table self.vm = VM(modules) self.vm.getStartFunctionBody() - def overseer(): + def overseer(self): # @DEVI- forking introduces a new source of non-determinism pid = os.fork() # child process @@ -403,7 +403,7 @@ class Judicator(): signal.signal(signal.SIGALRM, self.to_sighandler) def set_alarm(t): - signal.alaram(t) + signal.alarm(t) def to_sighandler(signum, frame): print(Colors.red + "execution time out..." + Colors.ENDC) diff --git a/parse.py b/parse.py index 2ef2a95..3cf919d 100755 --- a/parse.py +++ b/parse.py @@ -3,15 +3,12 @@ from __future__ import print_function import argparse import sys -import re from section_structs import * from utils import * from opcodes import * from copy import deepcopy from init import * -import readline import code -import signal _DBG_ = True @@ -224,8 +221,6 @@ class ObjReader(object): temp_wasm_ins.opcodeint = int(byte, 16) #temp_wasm_ins.operands = instruction temp_wasm_ins.operands = operands - instruction = str() - operands = [] break read_bytes += read_bytes_temp @@ -719,24 +714,22 @@ class ObjReader(object): return(SS) def ReadRelocationSection(self): - offset = 0 section_exists = False RS = Relocation_Section() for whatever in self.parsedstruct.section_list: if whatever[0] == 0 and whatever[1] == "reloc": - reloc_section = whatever.copy() + #reloc_section = whatever.copy() section_exists = True if not section_exists: return None return(RS) def ReadNameSection(self): - offset = 0 section_exists = False NS = Name_Section() for whatever in self.parsedstruct.section_list: if whatever[0] == 0 and whatever[1] == "name": - name_section = whatever.copy() + #name_section = whatever.copy() section_exists = True if not section_exists: return None @@ -998,7 +991,6 @@ def premain(argparser): interpreter.dump_sections(module, argparser.args.dbgsection) if interpreter.runValidations(): print(Colors.red + "validations are not implemented yet" + Colors.ENDC) - pass else: print(Colors.red + 'failed validation tests' + Colors.ENDC) vm = VM(interpreter.getmodules()) diff --git a/utils.py b/utils.py index 48a86f7..be6dbf0 100644 --- a/utils.py +++ b/utils.py @@ -141,14 +141,14 @@ def Read(section_byte, offset, kind): break return_list = LEB128UnsignedDecode(operand) - operand = [] + #operand = [] elif kind == 'uint8' or kind == 'uint16' or kind == 'uint32' or kind == 'uint64': byte = section_byte[offset: offset + TypeDic[kind]] read_bytes += TypeDic[kind] offset += TypeDic[kind] operand.append(byte) return_list = int.from_bytes(operand[0], byteorder='little', signed=False) - operand = [] + #operand = [] elif kind == 'varint1' or kind == 'varint7' or kind == 'varint32' or kind == 'varint64': while True: byte = int(section_byte[offset]) @@ -164,7 +164,7 @@ def Read(section_byte, offset, kind): # we have read the lasy byte of the operand break return_list = LEB128SignedDecode(operand) - operand = [] + #operand = [] return return_list, offset, read_bytes def ror(val, type_length, rot_size): @@ -193,7 +193,7 @@ def reinterpreti64tof64(val): def clz(val, _type): cnt = int() if _type == 'uint32': - bits = np.uint32(val) + #bits = np.uint32(val) power = 31 while power > -1: if val & 2**power == 0: @@ -202,7 +202,7 @@ def clz(val, _type): break power -= 1 elif _type == 'uint64': - bits = bin(np.uint64(val)) + #bits = bin(np.uint64(val)) power = 63 while power > -1: if val & 2**power == 0: @@ -220,7 +220,7 @@ def ctz(val, _type): cnt = int() power = int() if _type == 'uint32': - bits = np.uint32(val) + #bits = np.uint32(val) while power < 32: if val & 2**power == 0: cnt += 1 @@ -228,7 +228,7 @@ def ctz(val, _type): break power += 1 elif _type == 'uint64': - bits = bin(np.uint64(val)) + #bits = bin(np.uint64(val)) while power < 64: if val & 2**power == 0: cnt += 1 @@ -244,13 +244,13 @@ def pop_cnt(val, _type): cnt = int() power = int() if _type == 'uint32': - bits = np.uint32(val) + #bits = np.uint32(val) while power < 32: if val & 2**power != 0: cnt += 1 power += 1 elif _type == 'uint64': - bits = bin(np.uint64(val)) + #bits = bin(np.uint64(val)) while power < 64: if val & 2**power != 0: cnt += 1 -- cgit v1.2.3