From 136fad7e01bd485d7c63e4a571d04b38d9ed6f7f Mon Sep 17 00:00:00 2001 From: Fabian Date: Thu, 21 Dec 2017 11:36:58 -0600 Subject: [PATCH] Include capstone.js and libwabt.js for debugging --- Makefile | 6 +++++ loader.js | 3 +++ src/debug.js | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/externs.js | 16 ++++++++++++ 4 files changed, 93 insertions(+) diff --git a/Makefile b/Makefile index 27f443ee..fb2f6726 100644 --- a/Makefile +++ b/Makefile @@ -249,3 +249,9 @@ node_modules/.bin/jshint: jshint: node_modules/.bin/jshint ./node_modules/.bin/jshint --config=./.jshint.json src tests gen + +build/capstone-x86.min.js: + wget -P build https://github.com/AlexAltea/capstone.js/releases/download/v3.0.5-rc1/capstone-x86.min.js + +build/libwabt.js: + wget -P build https://raw.githubusercontent.com/WebAssembly/wabt/master/demo/libwabt.js diff --git a/loader.js b/loader.js index 7b850bd1..de22e425 100644 --- a/loader.js +++ b/loader.js @@ -17,11 +17,14 @@ // jor1k stuff LIB_FILES += " jor1k.js 9p.js filesystem.js marshall.js utf8.js"; + var BUILD_FILES = "capstone-x86.min.js libwabt.js"; + var to_load = []; load_scripts(CORE_FILES, "src/"); load_scripts(BROWSER_FILES, "src/browser/"); load_scripts(LIB_FILES, "lib/"); + load_scripts(BUILD_FILES, "build/"); function load_scripts(resp, path) { diff --git a/src/debug.js b/src/debug.js index e2c02f49..73761f9b 100644 --- a/src/debug.js +++ b/src/debug.js @@ -711,4 +711,72 @@ CPU.prototype.debug_init = function() // this.debug.dump_regs_short(); //} }; + + let capstone_decoder; + + debug.dump_code = function(is_32, buffer, start) + { + if(!capstone_decoder) + { + if(typeof cs === "undefined") + { + dbg_log("Warning: Missing capstone library, disassembly not available"); + return; + } + + capstone_decoder = [ + new cs.Capstone(cs.ARCH_X86, cs.MODE_16), + new cs.Capstone(cs.ARCH_X86, cs.MODE_32), + ]; + } + + try + { + const instructions = capstone_decoder[is_32].disasm(buffer, start); + + instructions.forEach(function (instr) { + dbg_log(h(instr.address) + ": " + + v86util.pads(instr.bytes.map(x => h(x, 2).slice(-2)).join(" "), 20) + " " + + instr.mnemonic + " " + instr.op_str); + }); + dbg_log(""); + } + catch(e) + { + dbg_log("Could not disassemble: " + Array.from(buffer).map(x => h(x, 2)).join(" ")); + } + }; + + debug.dump_wasm = function(buffer) + { + if(typeof wabt === "undefined") + { + dbg_log("Warning: Missing libwabt, wasm dump not available"); + return; + } + + // Need to make a small copy otherwise libwabt goes nuts trying to copy + // the whole underlying buffer + buffer = buffer.slice(); + + try + { + var module = wabt.readWasm(buffer, { readDebugNames: false }); + module.generateNames(); + module.applyNames(); + const result = module.toText({ foldExprs: true, inlineExport: true }); + dbg_log(result); + } + catch(e) + { + console.log(e.toString()); + } + finally + { + if(module) + { + module.destroy(); + } + } + }; }; diff --git a/src/externs.js b/src/externs.js index ef1f2cf9..4b2676f6 100644 --- a/src/externs.js +++ b/src/externs.js @@ -1,3 +1,5 @@ +"use strict"; + var performance = {}; @@ -16,3 +18,17 @@ var module = {}; var WebAssembly = { Memory() {}, Table() {}, instantiate() {}, compile() {}, Instance() {}, Module() {} }; WebAssembly.Module.customSections = function(module, section) {}; + +var wabt = { + readWasm: function(buf, opt) {}, + generateNames: function() {}, + applyNames: function() {}, + toText: function() {}, +}; +var cs = { + Capstone: function() {}, + ARCH_X86: 0, + MODE_16: 0, + MODE_32: 0, + disasm: { bytes: "", mnemonic: "", op_str: "", }, +};