diff options
Diffstat (limited to '')
-rw-r--r-- | dwasm-cpp/dwasm.hpp | 475 | ||||
-rw-r--r-- | dwasm-cpp/misc.hpp | 11 | ||||
-rw-r--r-- | dwasm-cpp/wasm.h | 337 | ||||
-rwxr-xr-x | dwasm.py | 10 | ||||
-rw-r--r-- | execute.py | 746 | ||||
-rw-r--r-- | init.py | 190 | ||||
-rw-r--r-- | opcodes.py | 691 | ||||
-rwxr-xr-x | parse.py | 1000 | ||||
-rw-r--r-- | section_structs.py | 149 | ||||
-rw-r--r-- | utils.py | 389 |
10 files changed, 2670 insertions, 1328 deletions
diff --git a/dwasm-cpp/dwasm.hpp b/dwasm-cpp/dwasm.hpp index 3c9c4a5..3b41e81 100644 --- a/dwasm-cpp/dwasm.hpp +++ b/dwasm-cpp/dwasm.hpp @@ -16,7 +16,8 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +USA.*/ /**********************************************************************************************************************/ #include <algorithm> #include <map> @@ -26,21 +27,68 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* #define _WASM_HPP #define MAGIC_NUMBER 0x6d736100 #define PAGE_SIZE 65536 -enum class section_id_t {custom, type, import, func, table, memory, global, exp, start, element, code, data, unknown}; -enum class relocation_type_t {R_WEBASSEMBLY_FUNCTION_INDEX_LEB, R_WEBASSEMBLY_TABLE_INDEX_SLEB, - R_WEBASSEMBLY_TABLE_INDEX_I32, R_WEBASSEMBLY_MEMORY_ADDR_LEB, R_WEBASSEMBLY_MEMORY_ADDR_SLEB, - R_WEBASSEMBLY_MEMORY_ADDR_I32, R_WEBASSEMBLY_TYPE_INDEX_LEB, R_WEBASSEMBLY_GLOBAL_INDEX_LEB, - R_WEBASSEMPLY_FUNCTION_OFFSET_I32, R_WEBASSEMBLY_SECTION_OFFSET_I32}; -enum class linking_subsection_t {WASM_SEGMENT_INFO, WASM_INIT_FUNCS, WASM_COMDAT_INFO, WASM_SYMBOL_TABLE}; -enum class syminfo_kind_t {SYMTAB_FUNCTION, SYMTAB_DATA, SYMTAB_GLOBAL, SYMTAB_SECTION}; -enum class w_type_t {uint8, uint16, uint32, uint64, - varuint1, varuint7, varuint32, varuint64, varint1, - varint7, varint32, varint64, floatt, doublet}; +enum class section_id_t { + custom, + type, + import, + func, + table, + memory, + global, + exp, + start, + element, + code, + data, + unknown +}; +enum class relocation_type_t { + R_WEBASSEMBLY_FUNCTION_INDEX_LEB, + R_WEBASSEMBLY_TABLE_INDEX_SLEB, + R_WEBASSEMBLY_TABLE_INDEX_I32, + R_WEBASSEMBLY_MEMORY_ADDR_LEB, + R_WEBASSEMBLY_MEMORY_ADDR_SLEB, + R_WEBASSEMBLY_MEMORY_ADDR_I32, + R_WEBASSEMBLY_TYPE_INDEX_LEB, + R_WEBASSEMBLY_GLOBAL_INDEX_LEB, + R_WEBASSEMPLY_FUNCTION_OFFSET_I32, + R_WEBASSEMBLY_SECTION_OFFSET_I32 +}; +enum class linking_subsection_t { + WASM_SEGMENT_INFO, + WASM_INIT_FUNCS, + WASM_COMDAT_INFO, + WASM_SYMBOL_TABLE +}; +enum class syminfo_kind_t { + SYMTAB_FUNCTION, + SYMTAB_DATA, + SYMTAB_GLOBAL, + SYMTAB_SECTION +}; +enum class w_type_t { + uint8, + uint16, + uint32, + uint64, + varuint1, + varuint7, + varuint32, + varuint64, + varint1, + varint7, + varint32, + varint64, + floatt, + doublet +}; struct wasm_op_t { - wasm_op_t(std::string _mnemonic, bool _has_opnd, std::vector<w_type_t> _opnd_types) { + wasm_op_t(std::string _mnemonic, bool _has_opnd, + std::vector<w_type_t> _opnd_types) { mnemonic = _mnemonic; has_opnd = _has_opnd; - std::for_each(_opnd_types.begin(), _opnd_types.end(), [this](w_type_t __t){this->opnd_types.push_back(__t);}); + std::for_each(_opnd_types.begin(), _opnd_types.end(), + [this](w_type_t __t) { this->opnd_types.push_back(__t); }); } std::string mnemonic; bool has_opnd; @@ -48,187 +96,226 @@ struct wasm_op_t { }; std::map<uint8_t, wasm_op_t> wasm_opcodes = { - //{0x7f, wasm_op_t{"i32", false, {}}}, - //{0x7e, wasm_op_t{"i64", false, {}}}, - //{0x7d, wasm_op_t{"f32", false, {}}}, - //{0x7c, wasm_op_t{"f64", false, {}}}, - //{0x7b, wasm_op_t{"anyfunc", false, {}}}, - //{0x60, wasm_op_t{"func", false, {}}}, - //{0x40, wasm_op_t{"empty_block_type", false, {}}}, - {0x00, wasm_op_t{"unreachable", false, {}}}, - {0x01, wasm_op_t{"nop", false, {}}}, - {0x02, wasm_op_t{"block", true, {w_type_t::varuint7}}}, - {0x03, wasm_op_t{"loop", true, {w_type_t::varuint7}}}, - {0x04, wasm_op_t{"if", true, {w_type_t::varuint7}}}, - {0x05, wasm_op_t{"else", false, {}}}, - {0x0b, wasm_op_t{"end", false, {}}}, - {0x0c, wasm_op_t{"br", true, {w_type_t::varuint32}}}, - {0x0d, wasm_op_t{"br_if", true, {w_type_t::varuint32}}}, - {0x0e, wasm_op_t{"br_table", true, {w_type_t::varuint32, w_type_t::varuint32, w_type_t::varuint32}}}, - {0x0f, wasm_op_t{"return", false, {}}}, - {0x10, wasm_op_t{"call", true, {w_type_t::varuint32}}}, - {0x11, wasm_op_t{"call_indirect", true, {w_type_t::varuint32, w_type_t::varuint1}}}, - {0x1a, wasm_op_t{"drop", false, {}}}, - {0x1b, wasm_op_t{"select", false, {}}}, - {0x20, wasm_op_t{"get_local", true, {w_type_t::varuint32}}}, - {0x21, wasm_op_t{"set_local", true, {w_type_t::varuint32}}}, - {0x22, wasm_op_t{"tee_local", true, {w_type_t::varuint32}}}, - {0x23, wasm_op_t{"get_global", true, {w_type_t::varuint32}}}, - {0x24, wasm_op_t{"set_global", true, {w_type_t::varuint32}}}, - {0x28, wasm_op_t{"i32.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x29, wasm_op_t{"i64.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2a, wasm_op_t{"f32.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2b, wasm_op_t{"f64.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2c, wasm_op_t{"i32.load8_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2d, wasm_op_t{"i32.load8_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2e, wasm_op_t{"i32.load16_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x2f, wasm_op_t{"i32.load16_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x30, wasm_op_t{"i64.load8_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x31, wasm_op_t{"i64.load8_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x32, wasm_op_t{"i64.load16_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x33, wasm_op_t{"i64.load16_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x34, wasm_op_t{"i64.load32_s", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x35, wasm_op_t{"i64.load32_u", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x36, wasm_op_t{"i32.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x37, wasm_op_t{"i64.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x38, wasm_op_t{"f32.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x39, wasm_op_t{"f64.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3a, wasm_op_t{"i32.store8", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3b, wasm_op_t{"i32.store16", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3c, wasm_op_t{"i64.store8", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3d, wasm_op_t{"i64.store16", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3e, wasm_op_t{"i64.store32", true, {w_type_t::varuint32, w_type_t::varuint32}}}, - {0x3f, wasm_op_t{"current_memory", true, {w_type_t::varuint1}}}, - {0x40, wasm_op_t{"grow_memory", true, {w_type_t::varuint1}}}, - {0x41, wasm_op_t{"i32.const", true, {w_type_t::varint32}}}, - {0x42, wasm_op_t{"i64.const", true, {w_type_t::varint64}}}, - {0x43, wasm_op_t{"f32.const", true, {w_type_t::uint32}}}, - {0x44, wasm_op_t{"f64.const", true, {w_type_t::uint64}}}, - {0x45, wasm_op_t{"i32.eqz", false, {}}}, - {0x46, wasm_op_t{"i32.eq", false, {}}}, - {0x47, wasm_op_t{"i32.ne", false, {}}}, - {0x48, wasm_op_t{"i32.lt_s", false, {}}}, - {0x49, wasm_op_t{"i32.lt_u", false, {}}}, - {0x4a, wasm_op_t{"i32.gt_s", false, {}}}, - {0x4b, wasm_op_t{"i32.gt_u", false, {}}}, - {0x4c, wasm_op_t{"i32.le_s", false, {}}}, - {0x4d, wasm_op_t{"i32.le_u", false, {}}}, - {0x4e, wasm_op_t{"i32.ge_s", false, {}}}, - {0x4f, wasm_op_t{"i32.ge_u", false, {}}}, - {0x50, wasm_op_t{"i64.eqz", false, {}}}, - {0x51, wasm_op_t{"i64.eq", false, {}}}, - {0x52, wasm_op_t{"i64.ne", false, {}}}, - {0x53, wasm_op_t{"i64.lt_s", false, {}}}, - {0x54, wasm_op_t{"i64.lt_u", false, {}}}, - {0x55, wasm_op_t{"i64.gt_s", false, {}}}, - {0x56, wasm_op_t{"i64.gt_u", false, {}}}, - {0x57, wasm_op_t{"i64.le_s", false, {}}}, - {0x58, wasm_op_t{"i64.le_u", false, {}}}, - {0x59, wasm_op_t{"i64.ge_s", false, {}}}, - {0x5a, wasm_op_t{"i64.ge_u", false, {}}}, - {0x5b, wasm_op_t{"f32.eq", false, {}}}, - {0x5c, wasm_op_t{"f32.ne", false, {}}}, - {0x5d, wasm_op_t{"f32.lt", false, {}}}, - {0x5e, wasm_op_t{"f32.gt", false, {}}}, - {0x5f, wasm_op_t{"f32.le", false, {}}}, - {0x60, wasm_op_t{"f32.ge", false, {}}}, - {0x61, wasm_op_t{"f64.eq", false, {}}}, - {0x62, wasm_op_t{"f64.ne", false, {}}}, - {0x63, wasm_op_t{"f64.lt", false, {}}}, - {0x64, wasm_op_t{"f64.gt", false, {}}}, - {0x65, wasm_op_t{"f64.le", false, {}}}, - {0x66, wasm_op_t{"f64.ge", false, {}}}, - {0x67, wasm_op_t{"i32.clz", false, {}}}, - {0x68, wasm_op_t{"i32.ctz", false, {}}}, - {0x69, wasm_op_t{"i32.popcnt", false, {}}}, - {0x6a, wasm_op_t{"i32.add", false, {}}}, - {0x6b, wasm_op_t{"i32.sub", false, {}}}, - {0x6c, wasm_op_t{"i32.mul", false, {}}}, - {0x6d, wasm_op_t{"i32.div_s", false, {}}}, - {0x6e, wasm_op_t{"i32.div_u", false, {}}}, - {0x6f, wasm_op_t{"i32.rem_s", false, {}}}, - {0x70, wasm_op_t{"i32.rem_u", false, {}}}, - {0x71, wasm_op_t{"i32.and", false, {}}}, - {0x72, wasm_op_t{"i32.or", false, {}}}, - {0x73, wasm_op_t{"i32.xor", false, {}}}, - {0x74, wasm_op_t{"i32.shl", false, {}}}, - {0x75, wasm_op_t{"i32.shr_s", false, {}}}, - {0x76, wasm_op_t{"i32.shr_u", false, {}}}, - {0x77, wasm_op_t{"i32.rotl", false, {}}}, - {0x78, wasm_op_t{"i32.rotr", false, {}}}, - {0x79, wasm_op_t{"i64.clz", false, {}}}, - {0x7a, wasm_op_t{"i64.ctz", false, {}}}, - {0x7b, wasm_op_t{"i64.popcnt", false, {}}}, - {0x7c, wasm_op_t{"i64.add", false, {}}}, - {0x7d, wasm_op_t{"i64.sub", false, {}}}, - {0x7e, wasm_op_t{"i64.mul", false, {}}}, - {0x7f, wasm_op_t{"i64.div_s", false, {}}}, - {0x80, wasm_op_t{"i64.div_u", false, {}}}, - {0x81, wasm_op_t{"i64.rem_s", false, {}}}, - {0x82, wasm_op_t{"i64.rem_u", false, {}}}, - {0x83, wasm_op_t{"i64.and", false, {}}}, - {0x84, wasm_op_t{"i64.or", false, {}}}, - {0x85, wasm_op_t{"i64.xor", false, {}}}, - {0x86, wasm_op_t{"i64.shl", false, {}}}, - {0x87, wasm_op_t{"i64.shr_s", false, {}}}, - {0x88, wasm_op_t{"i64.shr_u", false, {}}}, - {0x89, wasm_op_t{"i64.rotl", false, {}}}, - {0x8a, wasm_op_t{"i63.rotr", false, {}}}, - {0x8b, wasm_op_t{"f32.abs", false, {}}}, - {0x8c, wasm_op_t{"f32.neg", false, {}}}, - {0x8d, wasm_op_t{"f32.ceil", false, {}}}, - {0x8e, wasm_op_t{"f32.floor", false, {}}}, - {0x8f, wasm_op_t{"f32.trunc", false, {}}}, - {0x90, wasm_op_t{"f32.nearest", false, {}}}, - {0x91, wasm_op_t{"f32.sqrt", false, {}}}, - {0x92, wasm_op_t{"f32.add", false, {}}}, - {0x93, wasm_op_t{"f32.sub", false, {}}}, - {0x94, wasm_op_t{"f32.mul", false, {}}}, - {0x95, wasm_op_t{"f32.div", false, {}}}, - {0x96, wasm_op_t{"f32.min", false, {}}}, - {0x97, wasm_op_t{"f32.max", false, {}}}, - {0x98, wasm_op_t{"f32.copysign", false, {}}}, - {0x99, wasm_op_t{"f64.abs", false, {}}}, - {0x9a, wasm_op_t{"f64.neg", false, {}}}, - {0x9b, wasm_op_t{"f64.ceil", false, {}}}, - {0x9c, wasm_op_t{"f64.floor", false, {}}}, - {0x9d, wasm_op_t{"f64.trunc", false, {}}}, - {0x9e, wasm_op_t{"f64.nearest", false, {}}}, - {0x9f, wasm_op_t{"f64.sqrt", false, {}}}, - {0xa0, wasm_op_t{"f64.add", false, {}}}, - {0xa1, wasm_op_t{"f64.sub", false, {}}}, - {0xa2, wasm_op_t{"f64.mul", false, {}}}, - {0xa3, wasm_op_t{"f64.div", false, {}}}, - {0xa4, wasm_op_t{"f64.min", false, {}}}, - {0xa5, wasm_op_t{"f64.max", false, {}}}, - {0xa6, wasm_op_t{"f64.copysign", false, {}}}, - {0xa7, wasm_op_t{"i32.wrap/i64", false, {}}}, - {0xa8, wasm_op_t{"i32.trunc_s/f32", false, {}}}, - {0xa9, wasm_op_t{"i32.trunc_u/f32", false, {}}}, - {0xaa, wasm_op_t{"i32.trunc_s/f64", false, {}}}, - {0xab, wasm_op_t{"i32.trunc_u/f64", false, {}}}, - {0xac, wasm_op_t{"i64.extend_s/i32", false, {}}}, - {0xad, wasm_op_t{"i64.extend_u/i32", false, {}}}, - {0xae, wasm_op_t{"i64.trunc_s/f32", false, {}}}, - {0xaf, wasm_op_t{"i64.trunc_u/f32", false, {}}}, - {0xb0, wasm_op_t{"i64.trunc_s/f64", false, {}}}, - {0xb1, wasm_op_t{"i64.trunc_u/f64", false, {}}}, - {0xb2, wasm_op_t{"f32.convert_s/i32", false, {}}}, - {0xb3, wasm_op_t{"f32.convert_u/i32", false, {}}}, - {0xb4, wasm_op_t{"f32.convert_s/i64", false, {}}}, - {0xb5, wasm_op_t{"f32.convert_u/i64", false, {}}}, - {0xb6, wasm_op_t{"f32.demote/f64", false, {}}}, - {0xb7, wasm_op_t{"f64.convert_s/i32", false, {}}}, - {0xb8, wasm_op_t{"f64.convert_u/i32", false, {}}}, - {0xb9, wasm_op_t{"f64.convert_s/i64", false, {}}}, - {0xba, wasm_op_t{"f64.convert_u/i64", false, {}}}, - {0xbb, wasm_op_t{"f64.promote/f32", false, {}}}, - {0xbc, wasm_op_t{"i32.reinterpret/f32", false, {}}}, - {0xbd, wasm_op_t{"i64.reinterpret/f64", false, {}}}, - {0xbe, wasm_op_t{"f32.reinterpret/i32", false, {}}}, - {0xbf, wasm_op_t{"f64.reinterpret/i64", false, {}}} -}; + //{0x7f, wasm_op_t{"i32", false, {}}}, + //{0x7e, wasm_op_t{"i64", false, {}}}, + //{0x7d, wasm_op_t{"f32", false, {}}}, + //{0x7c, wasm_op_t{"f64", false, {}}}, + //{0x7b, wasm_op_t{"anyfunc", false, {}}}, + //{0x60, wasm_op_t{"func", false, {}}}, + //{0x40, wasm_op_t{"empty_block_type", false, {}}}, + {0x00, wasm_op_t{"unreachable", false, {}}}, + {0x01, wasm_op_t{"nop", false, {}}}, + {0x02, wasm_op_t{"block", true, {w_type_t::varuint7}}}, + {0x03, wasm_op_t{"loop", true, {w_type_t::varuint7}}}, + {0x04, wasm_op_t{"if", true, {w_type_t::varuint7}}}, + {0x05, wasm_op_t{"else", false, {}}}, + {0x0b, wasm_op_t{"end", false, {}}}, + {0x0c, wasm_op_t{"br", true, {w_type_t::varuint32}}}, + {0x0d, wasm_op_t{"br_if", true, {w_type_t::varuint32}}}, + {0x0e, wasm_op_t{"br_table", + true, + {w_type_t::varuint32, w_type_t::varuint32, + w_type_t::varuint32}}}, + {0x0f, wasm_op_t{"return", false, {}}}, + {0x10, wasm_op_t{"call", true, {w_type_t::varuint32}}}, + {0x11, wasm_op_t{"call_indirect", + true, + {w_type_t::varuint32, w_type_t::varuint1}}}, + {0x1a, wasm_op_t{"drop", false, {}}}, + {0x1b, wasm_op_t{"select", false, {}}}, + {0x20, wasm_op_t{"get_local", true, {w_type_t::varuint32}}}, + {0x21, wasm_op_t{"set_local", true, {w_type_t::varuint32}}}, + {0x22, wasm_op_t{"tee_local", true, {w_type_t::varuint32}}}, + {0x23, wasm_op_t{"get_global", true, {w_type_t::varuint32}}}, + {0x24, wasm_op_t{"set_global", true, {w_type_t::varuint32}}}, + {0x28, + wasm_op_t{"i32.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x29, + wasm_op_t{"i64.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2a, + wasm_op_t{"f32.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2b, + wasm_op_t{"f64.load", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2c, wasm_op_t{"i32.load8_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2d, wasm_op_t{"i32.load8_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2e, wasm_op_t{"i32.load16_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x2f, wasm_op_t{"i32.load16_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x30, wasm_op_t{"i64.load8_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x31, wasm_op_t{"i64.load8_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x32, wasm_op_t{"i64.load16_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x33, wasm_op_t{"i64.load16_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x34, wasm_op_t{"i64.load32_s", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x35, wasm_op_t{"i64.load32_u", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x36, + wasm_op_t{"i32.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x37, + wasm_op_t{"i64.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x38, + wasm_op_t{"f32.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x39, + wasm_op_t{"f64.store", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3a, + wasm_op_t{"i32.store8", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3b, wasm_op_t{"i32.store16", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3c, + wasm_op_t{"i64.store8", true, {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3d, wasm_op_t{"i64.store16", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3e, wasm_op_t{"i64.store32", + true, + {w_type_t::varuint32, w_type_t::varuint32}}}, + {0x3f, wasm_op_t{"current_memory", true, {w_type_t::varuint1}}}, + {0x40, wasm_op_t{"grow_memory", true, {w_type_t::varuint1}}}, + {0x41, wasm_op_t{"i32.const", true, {w_type_t::varint32}}}, + {0x42, wasm_op_t{"i64.const", true, {w_type_t::varint64}}}, + {0x43, wasm_op_t{"f32.const", true, {w_type_t::uint32}}}, + {0x44, wasm_op_t{"f64.const", true, {w_type_t::uint64}}}, + {0x45, wasm_op_t{"i32.eqz", false, {}}}, + {0x46, wasm_op_t{"i32.eq", false, {}}}, + {0x47, wasm_op_t{"i32.ne", false, {}}}, + {0x48, wasm_op_t{"i32.lt_s", false, {}}}, + {0x49, wasm_op_t{"i32.lt_u", false, {}}}, + {0x4a, wasm_op_t{"i32.gt_s", false, {}}}, + {0x4b, wasm_op_t{"i32.gt_u", false, {}}}, + {0x4c, wasm_op_t{"i32.le_s", false, {}}}, + {0x4d, wasm_op_t{"i32.le_u", false, {}}}, + {0x4e, wasm_op_t{"i32.ge_s", false, {}}}, + {0x4f, wasm_op_t{"i32.ge_u", false, {}}}, + {0x50, wasm_op_t{"i64.eqz", false, {}}}, + {0x51, wasm_op_t{"i64.eq", false, {}}}, + {0x52, wasm_op_t{"i64.ne", false, {}}}, + {0x53, wasm_op_t{"i64.lt_s", false, {}}}, + {0x54, wasm_op_t{"i64.lt_u", false, {}}}, + {0x55, wasm_op_t{"i64.gt_s", false, {}}}, + {0x56, wasm_op_t{"i64.gt_u", false, {}}}, + {0x57, wasm_op_t{"i64.le_s", false, {}}}, + {0x58, wasm_op_t{"i64.le_u", false, {}}}, + {0x59, wasm_op_t{"i64.ge_s", false, {}}}, + {0x5a, wasm_op_t{"i64.ge_u", false, {}}}, + {0x5b, wasm_op_t{"f32.eq", false, {}}}, + {0x5c, wasm_op_t{"f32.ne", false, {}}}, + {0x5d, wasm_op_t{"f32.lt", false, {}}}, + {0x5e, wasm_op_t{"f32.gt", false, {}}}, + {0x5f, wasm_op_t{"f32.le", false, {}}}, + {0x60, wasm_op_t{"f32.ge", false, {}}}, + {0x61, wasm_op_t{"f64.eq", false, {}}}, + {0x62, wasm_op_t{"f64.ne", false, {}}}, + {0x63, wasm_op_t{"f64.lt", false, {}}}, + {0x64, wasm_op_t{"f64.gt", false, {}}}, + {0x65, wasm_op_t{"f64.le", false, {}}}, + {0x66, wasm_op_t{"f64.ge", false, {}}}, + {0x67, wasm_op_t{"i32.clz", false, {}}}, + {0x68, wasm_op_t{"i32.ctz", false, {}}}, + {0x69, wasm_op_t{"i32.popcnt", false, {}}}, + {0x6a, wasm_op_t{"i32.add", false, {}}}, + {0x6b, wasm_op_t{"i32.sub", false, {}}}, + {0x6c, wasm_op_t{"i32.mul", false, {}}}, + {0x6d, wasm_op_t{"i32.div_s", false, {}}}, + {0x6e, wasm_op_t{"i32.div_u", false, {}}}, + {0x6f, wasm_op_t{"i32.rem_s", false, {}}}, + {0x70, wasm_op_t{"i32.rem_u", false, {}}}, + {0x71, wasm_op_t{"i32.and", false, {}}}, + {0x72, wasm_op_t{"i32.or", false, {}}}, + {0x73, wasm_op_t{"i32.xor", false, {}}}, + {0x74, wasm_op_t{"i32.shl", false, {}}}, + {0x75, wasm_op_t{"i32.shr_s", false, {}}}, + {0x76, wasm_op_t{"i32.shr_u", false, {}}}, + {0x77, wasm_op_t{"i32.rotl", false, {}}}, + {0x78, wasm_op_t{"i32.rotr", false, {}}}, + {0x79, wasm_op_t{"i64.clz", false, {}}}, + {0x7a, wasm_op_t{"i64.ctz", false, {}}}, + {0x7b, wasm_op_t{"i64.popcnt", false, {}}}, + {0x7c, wasm_op_t{"i64.add", false, {}}}, + {0x7d, wasm_op_t{"i64.sub", false, {}}}, + {0x7e, wasm_op_t{"i64.mul", false, {}}}, + {0x7f, wasm_op_t{"i64.div_s", false, {}}}, + {0x80, wasm_op_t{"i64.div_u", false, {}}}, + {0x81, wasm_op_t{"i64.rem_s", false, {}}}, + {0x82, wasm_op_t{"i64.rem_u", false, {}}}, + {0x83, wasm_op_t{"i64.and", false, {}}}, + {0x84, wasm_op_t{"i64.or", false, {}}}, + {0x85, wasm_op_t{"i64.xor", false, {}}}, + {0x86, wasm_op_t{"i64.shl", false, {}}}, + {0x87, wasm_op_t{"i64.shr_s", false, {}}}, + {0x88, wasm_op_t{"i64.shr_u", false, {}}}, + {0x89, wasm_op_t{"i64.rotl", false, {}}}, + {0x8a, wasm_op_t{"i63.rotr", false, {}}}, + {0x8b, wasm_op_t{"f32.abs", false, {}}}, + {0x8c, wasm_op_t{"f32.neg", false, {}}}, + {0x8d, wasm_op_t{"f32.ceil", false, {}}}, + {0x8e, wasm_op_t{"f32.floor", false, {}}}, + {0x8f, wasm_op_t{"f32.trunc", false, {}}}, + {0x90, wasm_op_t{"f32.nearest", false, {}}}, + {0x91, wasm_op_t{"f32.sqrt", false, {}}}, + {0x92, wasm_op_t{"f32.add", false, {}}}, + {0x93, wasm_op_t{"f32.sub", false, {}}}, + {0x94, wasm_op_t{"f32.mul", false, {}}}, + {0x95, wasm_op_t{"f32.div", false, {}}}, + {0x96, wasm_op_t{"f32.min", false, {}}}, + {0x97, wasm_op_t{"f32.max", false, {}}}, + {0x98, wasm_op_t{"f32.copysign", false, {}}}, + {0x99, wasm_op_t{"f64.abs", false, {}}}, + {0x9a, wasm_op_t{"f64.neg", false, {}}}, + {0x9b, wasm_op_t{"f64.ceil", false, {}}}, + {0x9c, wasm_op_t{"f64.floor", false, {}}}, + {0x9d, wasm_op_t{"f64.trunc", false, {}}}, + {0x9e, wasm_op_t{"f64.nearest", false, {}}}, + {0x9f, wasm_op_t{"f64.sqrt", false, {}}}, + {0xa0, wasm_op_t{"f64.add", false, {}}}, + {0xa1, wasm_op_t{"f64.sub", false, {}}}, + {0xa2, wasm_op_t{"f64.mul", false, {}}}, + {0xa3, wasm_op_t{"f64.div", false, {}}}, + {0xa4, wasm_op_t{"f64.min", false, {}}}, + {0xa5, wasm_op_t{"f64.max", false, {}}}, + {0xa6, wasm_op_t{"f64.copysign", false, {}}}, + {0xa7, wasm_op_t{"i32.wrap/i64", false, {}}}, + {0xa8, wasm_op_t{"i32.trunc_s/f32", false, {}}}, + {0xa9, wasm_op_t{"i32.trunc_u/f32", false, {}}}, + {0xaa, wasm_op_t{"i32.trunc_s/f64", false, {}}}, + {0xab, wasm_op_t{"i32.trunc_u/f64", false, {}}}, + {0xac, wasm_op_t{"i64.extend_s/i32", false, {}}}, + {0xad, wasm_op_t{"i64.extend_u/i32", false, {}}}, + {0xae, wasm_op_t{"i64.trunc_s/f32", false, {}}}, + {0xaf, wasm_op_t{"i64.trunc_u/f32", false, {}}}, + {0xb0, wasm_op_t{"i64.trunc_s/f64", false, {}}}, + {0xb1, wasm_op_t{"i64.trunc_u/f64", false, {}}}, + {0xb2, wasm_op_t{"f32.convert_s/i32", false, {}}}, + {0xb3, wasm_op_t{"f32.convert_u/i32", false, {}}}, + {0xb4, wasm_op_t{"f32.convert_s/i64", false, {}}}, + {0xb5, wasm_op_t{"f32.convert_u/i64", false, {}}}, + {0xb6, wasm_op_t{"f32.demote/f64", false, {}}}, + {0xb7, wasm_op_t{"f64.convert_s/i32", false, {}}}, + {0xb8, wasm_op_t{"f64.convert_u/i32", false, {}}}, + {0xb9, wasm_op_t{"f64.convert_s/i64", false, {}}}, + {0xba, wasm_op_t{"f64.convert_u/i64", false, {}}}, + {0xbb, wasm_op_t{"f64.promote/f32", false, {}}}, + {0xbc, wasm_op_t{"i32.reinterpret/f32", false, {}}}, + {0xbd, wasm_op_t{"i64.reinterpret/f64", false, {}}}, + {0xbe, wasm_op_t{"f32.reinterpret/i32", false, {}}}, + {0xbf, wasm_op_t{"f64.reinterpret/i64", false, {}}}}; #endif // header guard end /**********************************************************************************************************************/ /*last line intentionally left blank.*/ - diff --git a/dwasm-cpp/misc.hpp b/dwasm-cpp/misc.hpp index 74f61ad..964c380 100644 --- a/dwasm-cpp/misc.hpp +++ b/dwasm-cpp/misc.hpp @@ -16,21 +16,23 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +USA.*/ /**********************************************************************************************************************/ #include <cstddef> #include <vector> /**********************************************************************************************************************/ -int read_header(std::ifstream& wasm) { +int read_header(std::ifstream &wasm) { char head[4]; wasm.read(head, sizeof(uint32_t)); std::cout << head[0] << head[1] << head[2] << head[3] << "\n"; wasm.read(head, sizeof(uint32_t)); - std::cout << (int)(head[0]) << (int)(head[1]) << (int)(head[2]) << (int)(head[3]) << "\n"; + std::cout << (int)(head[0]) << (int)(head[1]) << (int)(head[2]) + << (int)(head[3]) << "\n"; return 0; } -int dump_file(std::ifstream& wasm) { +int dump_file(std::ifstream &wasm) { while (wasm) { std::ios::pos_type before = wasm.tellg(); uint8_t x; @@ -47,4 +49,3 @@ std::vector<unsigned char> leb128_u_e() {} std::vector<unsigned char> leb128_s_e() {} /**********************************************************************************************************************/ /*last line intentionally left blank.*/ - diff --git a/dwasm-cpp/wasm.h b/dwasm-cpp/wasm.h index a7b916f..6687c03 100644 --- a/dwasm-cpp/wasm.h +++ b/dwasm-cpp/wasm.h @@ -16,7 +16,8 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software -Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/ +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, +USA.*/ /**********************************************************************************************************************/ #ifndef WASM_H #define WASM_H @@ -34,147 +35,156 @@ typedef int8_t varuint1; typedef int8_t varuint7; typedef int32_t varuint32; - enum value_type_t {f64_vt = -4, f32_vt, i64_vt, i32_vt}; - enum external_kind_t {Function, Table, Memory, Global}; - enum type_ctor_t {i32_ctor = -1, i64_ctor = -2, f32_ctor = -3, f64_ctor = -4, anyfunc_ctor = -16, func_ctor = -32, block_type_ctor = -64}; - - typedef struct { - varuint32 size; - char* code; - }init_expr_t; - - typedef struct { - varuint1 flags; - varuint32 initial; - varuint32 maximum; - } resizable_limit_t; - - typedef struct { - enum value_type_t value_type; - varuint1 mutability; - }global_type_t; - - typedef struct { - varint7 element_type; - resizable_limit_t* resizable_limit; - }table_type_t; - - typedef struct { - resizable_limit_t* resizable_limit; - }memory_type_t; - - // func_type - typedef struct { - varint7 form; - varuint32 param_count; - varint7* param_types; - varuint1 return_count; - varint7 * return_types; - }W_Type_Section_Entry; - - typedef struct { - varuint32 count; - W_Type_Section_Entry** entries; - }W_Type_Section; - - typedef struct { - varuint32 module_length; - char* module_str; - varuint32 field_len; - char* field_str; - enum external_kind_t kind; - // based on external_kind it can be 4 different types. thats why im casting to void*. - void* type; - }W_Import_Section_Entry; - - typedef struct { - varuint32 count; - W_Import_Section_Entry** entries; - }W_Import_Section; - - typedef struct { - varuint32 count; - // indexes into the type section - varuint32* types; - }W_Function_Section; - - typedef struct W_Table_Section { - varuint32 count; - table_type_t** entries; - }W_Table_Section; - - typedef struct { - varuint32 count; - memory_type_t** entries; - }W_Memory_Section; - - typedef struct { - global_type_t* type; - init_expr_t* init; - }W_Global_Entry; - - typedef struct { - varuint32 count; - W_Global_Entry** globals; - }W_Global_Section; - - typedef struct { - varuint32 field_len; - char* field_str; - enum external_kind_t kind; - varuint32 index; - }W_Export_Entry; - - typedef struct { - int count; - W_Export_Entry** entries; - }W_Export_Section; - - typedef struct { - varuint32 index; - }W_Start_Section; - - typedef struct { - varuint32 index; - init_expr_t* offset; - varuint32 num_length; - varuint32* elems; - }W_Elem_Segment; - - typedef struct { - varuint32 count; - W_Elem_Segment** entries; - }W_Element_Section; - - typedef struct { - varuint32 count; - enum value_type_t type; - }W_Local_Entry; - - typedef struct W_Function_Body { - varuint32 body_size; - varuint32 local_count; - W_Local_Entry** locals; - char* code; - //char end = 0x0b; - }W_Function_Body; - - typedef struct { - varuint32 count; - W_Function_Body** bodies; - }W_Code_Section; - - typedef struct { - varuint32 index; - init_expr_t* offset; - varuint32 size; - char* data; - }W_Data_Segment; - - typedef struct { - varuint32 count; - W_Data_Segment** entries; - }W_Data_Section; +enum value_type_t { f64_vt = -4, f32_vt, i64_vt, i32_vt }; +enum external_kind_t { Function, Table, Memory, Global }; +enum type_ctor_t { + i32_ctor = -1, + i64_ctor = -2, + f32_ctor = -3, + f64_ctor = -4, + anyfunc_ctor = -16, + func_ctor = -32, + block_type_ctor = -64 +}; + +typedef struct { + varuint32 size; + char *code; +} init_expr_t; + +typedef struct { + varuint1 flags; + varuint32 initial; + varuint32 maximum; +} resizable_limit_t; + +typedef struct { + enum value_type_t value_type; + varuint1 mutability; +} global_type_t; + +typedef struct { + varint7 element_type; + resizable_limit_t *resizable_limit; +} table_type_t; + +typedef struct { + resizable_limit_t *resizable_limit; +} memory_type_t; + +// func_type +typedef struct { + varint7 form; + varuint32 param_count; + varint7 *param_types; + varuint1 return_count; + varint7 *return_types; +} W_Type_Section_Entry; + +typedef struct { + varuint32 count; + W_Type_Section_Entry **entries; +} W_Type_Section; + +typedef struct { + varuint32 module_length; + char *module_str; + varuint32 field_len; + char *field_str; + enum external_kind_t kind; + // based on external_kind it can be 4 different types. thats why im casting to + // void*. + void *type; +} W_Import_Section_Entry; + +typedef struct { + varuint32 count; + W_Import_Section_Entry **entries; +} W_Import_Section; + +typedef struct { + varuint32 count; + // indexes into the type section + varuint32 *types; +} W_Function_Section; + +typedef struct W_Table_Section { + varuint32 count; + table_type_t **entries; +} W_Table_Section; + +typedef struct { + varuint32 count; + memory_type_t **entries; +} W_Memory_Section; + +typedef struct { + global_type_t *type; + init_expr_t *init; +} W_Global_Entry; + +typedef struct { + varuint32 count; + W_Global_Entry **globals; +} W_Global_Section; + +typedef struct { + varuint32 field_len; + char *field_str; + enum external_kind_t kind; + varuint32 index; +} W_Export_Entry; + +typedef struct { + int count; + W_Export_Entry **entries; +} W_Export_Section; + +typedef struct { + varuint32 index; +} W_Start_Section; + +typedef struct { + varuint32 index; + init_expr_t *offset; + varuint32 num_length; + varuint32 *elems; +} W_Elem_Segment; + +typedef struct { + varuint32 count; + W_Elem_Segment **entries; +} W_Element_Section; + +typedef struct { + varuint32 count; + enum value_type_t type; +} W_Local_Entry; + +typedef struct W_Function_Body { + varuint32 body_size; + varuint32 local_count; + W_Local_Entry **locals; + char *code; + // char end = 0x0b; +} W_Function_Body; + +typedef struct { + varuint32 count; + W_Function_Body **bodies; +} W_Code_Section; + +typedef struct { + varuint32 index; + init_expr_t *offset; + varuint32 size; + char *data; +} W_Data_Segment; + +typedef struct { + varuint32 count; + W_Data_Segment **entries; +} W_Data_Section; #if 0 typedef struct W_Custom_Section {}; @@ -182,27 +192,27 @@ typedef int32_t varuint32; typedef struct W_Relocation_Section {}; #endif - typedef struct Wasm_Module { - W_Type_Section* type_section; - W_Import_Section* import_section; - W_Function_Section* function_section; - W_Table_Section* table_section; - W_Memory_Section* memory_section; - W_Global_Section* global_section; - W_Export_Section* export_section; - W_Start_Section* start_section; - W_Element_Section* element_section; - W_Code_Section* code_section; - W_Data_Section* data_section; - void** W_Custom_Sections; - char* name; - }Wasm_Module; - - // get the raw binary of the wasm module - // char* getWRaw(); - - // get wasm section raw binary by name - // char* get_W_Section_Raw(const char* section_name); +typedef struct Wasm_Module { + W_Type_Section *type_section; + W_Import_Section *import_section; + W_Function_Section *function_section; + W_Table_Section *table_section; + W_Memory_Section *memory_section; + W_Global_Section *global_section; + W_Export_Section *export_section; + W_Start_Section *start_section; + W_Element_Section *element_section; + W_Code_Section *code_section; + W_Data_Section *data_section; + void **W_Custom_Sections; + char *name; +} Wasm_Module; + +// get the raw binary of the wasm module +// char* getWRaw(); + +// get wasm section raw binary by name +// char* get_W_Section_Raw(const char* section_name); #ifdef __cplusplus } @@ -210,4 +220,3 @@ typedef int32_t varuint32; #endif // end of header guard /**********************************************************************************************************************/ /*last line intentionally left blank.*/ - @@ -3,12 +3,14 @@ import code import signal import sys -from parse import Argparser, premain, SigHandler_SIGINT,PythonInterpreter +from parse import Argparser, premain, SigHandler_SIGINT, PythonInterpreter + 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) @@ -18,7 +20,8 @@ def main(): premain(argparser) except Exception as e: print(e.__doc__) - if e.message: print(e.message) + if e.message: + print(e.message) variables = globals().copy() variables.update(locals()) shell = code.InteractiveConsole(variables) @@ -26,5 +29,6 @@ def main(): else: premain(argparser) + if __name__ == "__main__": main() @@ -4,13 +4,13 @@ import numpy as np import math -class Label(): +class Label: def __init__(self, arity, name): self.arity = arity self.name = name -class Frame(): +class Frame: def __init__(self, arity, local_indices, self_ref): self.arity = arity self.local_indices = local_indices @@ -18,10 +18,10 @@ class Frame(): # takes the machinestate, opcode and operand to run. updates the machinestate -class Execute(): # pragma: no cover +class Execute: # pragma: no cover def __init__(self, machinestate): self.machinestate = machinestate - self.opcodeint = '' + self.opcodeint = "" self.immediates = [] self.op_gas = int() self.stack_top = [] @@ -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): @@ -42,216 +42,242 @@ class Execute(): # pragma: no cover def getInstruction(self, opcodeint, immediates): self.opcodeint = opcodeint dummy = [] - #FIXME-why is it being cast to int? + # FIXME-why is it being cast to int? for i in immediates: dummy.append(int(i)) self.immediates = dummy def callExecuteMethod(self): - runmethod = self.instructionUnwinder(self.opcodeint, self.immediates, self.machinestate) - #print (repr(hex(self.opcodeint)) + ' ' + repr(self.immediates)) + runmethod = self.instructionUnwinder( + self.opcodeint, self.immediates, self.machinestate + ) + # print (repr(hex(self.opcodeint)) + ' ' + repr(self.immediates)) try: runmethod(self.opcodeint, self.immediates) except IndexError: # trap - print(Colors.red + 'bad stack access.' + Colors.ENDC) - #val2 = self.machinestate.Stack_Omni.pop() - + print(Colors.red + "bad stack access." + Colors.ENDC) + # val2 = self.machinestate.Stack_Omni.pop() def instructionUnwinder(self, opcodeint, immediates, machinestate): self.chargeGas(opcodeint) if opcodeint == 0: - return(self.run_unreachable) + return self.run_unreachable elif opcodeint == 1: - return(self.run_nop) + return self.run_nop elif opcodeint == 2: - return(self.run_block) + return self.run_block elif opcodeint == 3: - return(self.run_loop) + return self.run_loop elif opcodeint == 4: - return(self.run_if) + return self.run_if elif opcodeint == 5: - return(self.run_else) + return self.run_else elif opcodeint == 11: - return(self.run_end) + return self.run_end elif opcodeint == 12: - return(self.run_br) + return self.run_br elif opcodeint == 13: - return(self.run_br_if) + return self.run_br_if elif opcodeint == 14: - return(self.run_br_table) + return self.run_br_table elif opcodeint == 15: - return(self.run_return) + return self.run_return elif opcodeint == 16: - return(self.run_call) + return self.run_call elif opcodeint == 17: - return(self.run_call_indirect) + return self.run_call_indirect elif opcodeint == 26: - return(self.run_drop) + return self.run_drop elif opcodeint == 27: - return(self.run_select) + return self.run_select elif opcodeint == 32: - return(self.run_getlocal) + return self.run_getlocal elif opcodeint == 33: - return(self.run_setlocal) + return self.run_setlocal elif opcodeint == 34: - return(self.run_teelocal) + return self.run_teelocal elif opcodeint == 35: - return(self.run_getglobal) + return self.run_getglobal elif opcodeint == 36: - return(self.run_setglobal) + return self.run_setglobal elif opcodeint >= 40 and opcodeint <= 53: - return(self.run_load) + return self.run_load elif opcodeint >= 54 and opcodeint <= 62: - return(self.run_store) + return self.run_store elif opcodeint == 63: - return(self.run_current_memory) + return self.run_current_memory elif opcodeint == 64: self.chargeGasMem(immediates[0]) - return(self.run_grow_memory) + return self.run_grow_memory elif opcodeint >= 65 and opcodeint <= 68: - return(self.run_const) + return self.run_const elif opcodeint == 69 or opcodeint == 80: - return(self.run_eqz) - elif opcodeint == 70 or opcodeint == 81 or opcodeint == 91 or opcodeint == 97: - return(self.run_eq) - elif opcodeint == 71 or opcodeint == 82 or opcodeint == 92 or opcodeint == 98: - return(self.run_ne) + return self.run_eqz + elif ( + opcodeint == 70 + or opcodeint == 81 + or opcodeint == 91 + or opcodeint == 97 + ): + return self.run_eq + elif ( + opcodeint == 71 + or opcodeint == 82 + or opcodeint == 92 + or opcodeint == 98 + ): + return self.run_ne elif opcodeint == 72 or opcodeint == 83: - return(self.run_lt_s) + return self.run_lt_s elif opcodeint == 73 or opcodeint == 84: - return(self.run_lt_u) + return self.run_lt_u elif opcodeint == 74 or opcodeint == 85: - return(self.run_gt_s) + return self.run_gt_s elif opcodeint == 75 or opcodeint == 86: - return(self.run_gt_u) + return self.run_gt_u elif opcodeint == 76 or opcodeint == 87: - return(self.run_le_s) + return self.run_le_s elif opcodeint == 77 or opcodeint == 88: - return(self.run_le_u) + return self.run_le_u elif opcodeint == 78 or opcodeint == 89: - return(self.run_ge_s) + return self.run_ge_s elif opcodeint == 79 or opcodeint == 90: - return(self.run_ge_u) + return self.run_ge_u elif opcodeint == 93 or opcodeint == 99: - return(self.run_lt) + return self.run_lt elif opcodeint == 94 or opcodeint == 100: - return(self.run_gt) + return self.run_gt elif opcodeint == 95 or opcodeint == 101: - return(self.run_le) + return self.run_le elif opcodeint == 96 or opcodeint == 102: - return(self.run_ge) + return self.run_ge elif opcodeint == 103 or opcodeint == 121: - return(self.run_clz) + return self.run_clz elif opcodeint == 104 or opcodeint == 122: - return(self.run_ctz) + return self.run_ctz elif opcodeint == 105 or opcodeint == 123: - return(self.run_popcnt) - elif opcodeint == 106 or opcodeint == 124 or opcodeint == 146 or opcodeint == 160: - return(self.run_add) - elif opcodeint == 107 or opcodeint == 125 or opcodeint == 147 or opcodeint == 161: - return(self.run_sub) - elif opcodeint == 108 or opcodeint == 126 or opcodeint == 148 or opcodeint == 162: - return(self.run_mul) + return self.run_popcnt + elif ( + opcodeint == 106 + or opcodeint == 124 + or opcodeint == 146 + or opcodeint == 160 + ): + return self.run_add + elif ( + opcodeint == 107 + or opcodeint == 125 + or opcodeint == 147 + or opcodeint == 161 + ): + return self.run_sub + elif ( + opcodeint == 108 + or opcodeint == 126 + or opcodeint == 148 + or opcodeint == 162 + ): + return self.run_mul elif opcodeint == 109 or opcodeint == 127: - return(self.run_div_s) + return self.run_div_s elif opcodeint == 110 or opcodeint == 128: - return(self.run_div_u) + return self.run_div_u elif opcodeint == 111 or opcodeint == 129: - return(self.run_rem_s) + return self.run_rem_s elif opcodeint == 112 or opcodeint == 130: - return(self.run_rem_u) + return self.run_rem_u elif opcodeint == 113 or opcodeint == 131: - return(self.run_and) + return self.run_and elif opcodeint == 114 or opcodeint == 132: - return(self.run_or) + return self.run_or elif opcodeint == 115 or opcodeint == 133: - return(self.run_xor) + return self.run_xor elif opcodeint == 116 or opcodeint == 134: - return(self.run_shl) + return self.run_shl elif opcodeint == 117 or opcodeint == 135: - return(self.run_shr_s) + return self.run_shr_s elif opcodeint == 118 or opcodeint == 136: - return(self.run_shr_u) + return self.run_shr_u elif opcodeint == 119 or opcodeint == 137: - return(self.run_rotl) + return self.run_rotl elif opcodeint == 120 or opcodeint == 138: - return(self.run_rotr) + return self.run_rotr elif opcodeint == 139 or opcodeint == 153: - return(self.run_abs) + return self.run_abs elif opcodeint == 140 or opcodeint == 154: - return(self.run_neg) + return self.run_neg elif opcodeint == 141 or opcodeint == 155: - return(self.run_ceil) + return self.run_ceil elif opcodeint == 142 or opcodeint == 156: - return(self.run_floor) + return self.run_floor elif opcodeint == 143 or opcodeint == 157: - return(self.run_trunc) + return self.run_trunc elif opcodeint == 144 or opcodeint == 158: - return(self.run_nearest) + return self.run_nearest elif opcodeint == 145 or opcodeint == 159: - return(self.run_sqrt) + return self.run_sqrt elif opcodeint == 149 or opcodeint == 163: - return(self.run_div) + return self.run_div elif opcodeint == 150 or opcodeint == 164: - return(self.run_min) + return self.run_min elif opcodeint == 151 or opcodeint == 165: - return(self.run_max) + return self.run_max elif opcodeint == 152 or opcodeint == 166: - return(self.run_copysign) + return self.run_copysign elif opcodeint == 167: - return(self.run_i32wrapi64) + return self.run_i32wrapi64 elif opcodeint == 168: - return(self.run_i32trunc_sf32) + return self.run_i32trunc_sf32 elif opcodeint == 169: - return(self.run_i32trunc_uf32) + return self.run_i32trunc_uf32 elif opcodeint == 170: - return(self.run_i32trunc_sf64) + return self.run_i32trunc_sf64 elif opcodeint == 171: - return(self.run_i32trunc_uf64) + return self.run_i32trunc_uf64 elif opcodeint == 172: - return(self.run_i64extend_si32) + return self.run_i64extend_si32 elif opcodeint == 173: - return(self.run_i64extend_ui3o) + return self.run_i64extend_ui3o elif opcodeint == 174: - return(self.run_i64trunc_sf32) + return self.run_i64trunc_sf32 elif opcodeint == 175: - return(self.run_i64trunc_uf32) + return self.run_i64trunc_uf32 elif opcodeint == 176: - return(self.run_i64trunc_sf64) + return self.run_i64trunc_sf64 elif opcodeint == 177: - return(self.run_i64trunc_uf64) + return self.run_i64trunc_uf64 elif opcodeint == 178: - return(self.run_f32convert_si32) + return self.run_f32convert_si32 elif opcodeint == 179: - return(self.run_f32convert_ui32) + return self.run_f32convert_ui32 elif opcodeint == 180: - return(self.run_f32convert_si64) + return self.run_f32convert_si64 elif opcodeint == 181: - return(self.run_f32convert_ui64) + return self.run_f32convert_ui64 elif opcodeint == 182: - return(self.run_f32demotef64) + return self.run_f32demotef64 elif opcodeint == 183: - return(self.run_f64convert_si32) + return self.run_f64convert_si32 elif opcodeint == 184: - return(self.run_f64convert_ui32) + return self.run_f64convert_ui32 elif opcodeint == 185: - return(self.run_f64convert_si64) + return self.run_f64convert_si64 elif opcodeint == 186: - return(self.run_f64convert_ui64) + return self.run_f64convert_ui64 elif opcodeint == 187: - return(self.run_f64promotef32) + return self.run_f64promotef32 elif opcodeint == 188: - return(self.run_i32reinterpretf32) + return self.run_i32reinterpretf32 elif opcodeint == 189: - return(self.run_i64reinterpretf64) + return self.run_i64reinterpretf64 elif opcodeint == 190: - return(self.run_f32reinterpreti32) + return self.run_f32reinterpreti32 elif opcodeint == 191: - return(self.run_f64reinterpreti64) + return self.run_f64reinterpreti64 else: - raise Exception(Colors.red + 'unknown opcode' + Colors.ENDC) + raise Exception(Colors.red + "unknown opcode" + Colors.ENDC) def run_unreachable(self, opcodeint, immediates): # trap @@ -262,7 +288,9 @@ class Execute(): # pragma: no cover pass def run_block(self, opcodeint, immediates): - self.machinestate.Stack_Label.append(self.machinestate.Stack_Label_Height) + self.machinestate.Stack_Label.append( + self.machinestate.Stack_Label_Height + ) self.machinestate.Stack_Label_Height += 1 def run_loop(self, opcodeint, immediates): @@ -270,7 +298,9 @@ class Execute(): # pragma: no cover if not self.machinestate.Stack_Omni: print(Colors.red + "entered a loop. stack is empty." + Colors.ENDC) # exit 1 - self.machinestate.Stack_Label.append(self.machinestate.Stack_Label_Height) + self.machinestate.Stack_Label.append( + self.machinestate.Stack_Label_Height + ) self.machinestate.Stack_Label_Height += 1 val = self.machinestate.Stack_Omni.pop() if val != 0: @@ -285,18 +315,26 @@ class Execute(): # pragma: no cover pass def run_end(self, opcodeint, immediates): - #self.machinestate.Stack_Label.pop() + # self.machinestate.Stack_Label.pop() pass def run_br(self, opcodeint, immediates): if self.machinestate.Stack_Label_Height >= immediates[0] + 1: - print(Colors.red + "label stack does not have enough labels." + Colors.ENDC) + print( + Colors.red + + "label stack does not have enough labels." + + Colors.ENDC + ) # exit 1 if len(self.machinestate.Stack_Omni) < 1: - print(Colors.red + "the value stack does not have enough values." + Colors.ENDC) + 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() @@ -328,11 +366,15 @@ class Execute(): # pragma: no cover self.machinestate.Stack_Omni.append(local) def run_setlocal(self, opcodeint, immediates): - self.machinestate.Index_Space_Locals[int(immediates[0])] = self.machinestate.Stack_Omni.pop() + self.machinestate.Index_Space_Locals[ + int(immediates[0]) + ] = self.machinestate.Stack_Omni.pop() def run_teelocal(self, dummy, immediates): # @DEVI-we dont pop and push - self.machinestate.Index_Space_Locals[int(immediates[0])] = self.machinestate.Stack_Omni[-1] + self.machinestate.Index_Space_Locals[ + int(immediates[0]) + ] = self.machinestate.Stack_Omni[-1] def run_getglobal(self, opcodeint, immediates): val = self.machinestate.Index_Space_Global[immediates[0]] @@ -345,75 +387,141 @@ class Execute(): # pragma: no cover # currently only one linear memory is allowed so thats the default. def run_load(self, opcodeint, immediates): if opcodeint == 40: - bytes = self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates) + 4] + bytes = self.machinestate.Linear_Memory[0][ + int(immediates[1]) : int(immediates) + 4 + ] self.machinestate.Stack_Omni.append(np.int32(bytes)) elif opcodeint == 41: - bytes = self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates) + 8] + bytes = self.machinestate.Linear_Memory[0][ + int(immediates[1]) : int(immediates) + 8 + ] self.machinestate.Stack_Omni.append(np.int64(bytes)) elif opcodeint == 42: - bytes = self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates) + 4] + bytes = self.machinestate.Linear_Memory[0][ + int(immediates[1]) : int(immediates) + 4 + ] self.machinestate.Stack_Omni.append(np.float32(bytes)) elif opcodeint == 43: - bytes = self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates) + 8] + bytes = self.machinestate.Linear_Memory[0][ + int(immediates[1]) : int(immediates) + 8 + ] self.machinestate.Stack_Omni.append(np.float64(bytes)) elif opcodeint == 44: - temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1])]) - temp2 = (temp & 0x0000007f) | ((temp & 0x80) << 24) + temp = np.int8( + self.machinestate.Linear_Memory[0][int(immediates[1])] + ) + temp2 = (temp & 0x0000007F) | ((temp & 0x80) << 24) self.machinestate.append(np.int32(temp2)) elif opcodeint == 45: - temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1])]) - temp2 = temp & 0x000000ff + temp = np.int8( + self.machinestate.Linear_Memory[0][int(immediates[1])] + ) + temp2 = temp & 0x000000FF 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) + 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(temp2)) elif opcodeint == 47: - temp = np.int8(self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates[1] + 2)]) - temp2 = temp & 0x0000ffff + temp = np.int8( + self.machinestate.Linear_Memory[0][ + int(immediates[1]) : int(immediates[1] + 2) + ] + ) + temp2 = temp & 0x0000FFFF 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) + temp = np.int8( + self.machinestate.Linear_Memory[0][int(immediates[1])] + ) + temp2 = (temp & 0x000000000000007F) | ((temp & 0x80) << 56) self.machinestate.append(np.int64(temp2)) elif opcodeint == 49: - temp = np.uint8(self.machinestate.Linear_Memory[0][int(immediates[1])]) + temp = np.uint8( + self.machinestate.Linear_Memory[0][int(immediates[1])] + ) 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) + 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(temp2)) elif opcodeint == 51: - temp = np.uint8(self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates[1] + 2)]) + temp = np.uint8( + self.machinestate.Linear_Memory[0][ + int(immediates[1]) : int(immediates[1] + 2) + ] + ) 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) + 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(temp2)) elif opcodeint == 53: - temp = np.uint8(self.machinestate.Linear_Memory[0][int(immediates[1]):int(immediates[1] + 4)]) + temp = np.uint8( + self.machinestate.Linear_Memory[0][ + int(immediates[1]) : int(immediates[1] + 4) + ] + ) self.machinestate.append(np.uint64(temp)) else: - raise Exception(Colors.red + 'invalid load instruction.' + Colors.ENDC) + raise Exception( + Colors.red + "invalid load instruction." + Colors.ENDC + ) # currently only one linear memory is allowed so thats the default. def run_store(self, opcodeint, immediates): if opcodeint == 54: val = self.machinestate.Stack_Omni.pop() - self.machinestate.Linear_Memory[0][int(immediates[1]) + 0] = val & 0x000000ff - self.machinestate.Linear_Memory[0][int(immediates[1]) + 1] = val & 0x0000ff00 >> 8 - self.machinestate.Linear_Memory[0][int(immediates[1]) + 2] = val & 0x00ff0000 >> 16 - self.machinestate.Linear_Memory[0][int(immediates[1]) + 3] = val & 0xff000000 >> 24 + self.machinestate.Linear_Memory[0][int(immediates[1]) + 0] = ( + val & 0x000000FF + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 1] = ( + val & 0x0000FF00 >> 8 + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 2] = ( + val & 0x00FF0000 >> 16 + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 3] = ( + val & 0xFF000000 >> 24 + ) elif opcodeint == 55: val = self.machinestate.Stack_Omni.pop() - self.machinestate.Linear_Memory[0][int(immediates[1]) + 0] = val & 0x00000000000000ff - self.machinestate.Linear_Memory[0][int(immediates[1]) + 1] = val & 0x000000000000ff00 >> 8 - self.machinestate.Linear_Memory[0][int(immediates[1]) + 2] = val & 0x0000000000ff0000 >> 16 - self.machinestate.Linear_Memory[0][int(immediates[1]) + 3] = val & 0x00000000ff000000 >> 24 - self.machinestate.Linear_Memory[0][int(immediates[1]) + 4] = val & 0x000000ff00000000 >> 32 - self.machinestate.Linear_Memory[0][int(immediates[1]) + 5] = val & 0x0000ff0000000000 >> 40 - self.machinestate.Linear_Memory[0][int(immediates[1]) + 6] = val & 0x00ff000000000000 >> 48 - self.machinestate.Linear_Memory[0][int(immediates[1]) + 7] = val & 0xff00000000000000 >> 56 + self.machinestate.Linear_Memory[0][int(immediates[1]) + 0] = ( + val & 0x00000000000000FF + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 1] = ( + val & 0x000000000000FF00 >> 8 + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 2] = ( + val & 0x0000000000FF0000 >> 16 + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 3] = ( + val & 0x00000000FF000000 >> 24 + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 4] = ( + val & 0x000000FF00000000 >> 32 + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 5] = ( + val & 0x0000FF0000000000 >> 40 + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 6] = ( + val & 0x00FF000000000000 >> 48 + ) + self.machinestate.Linear_Memory[0][int(immediates[1]) + 7] = ( + val & 0xFF00000000000000 >> 56 + ) # @DEVI-FIXME-needs reinterpret cast elif opcodeint == 56: pass @@ -422,26 +530,48 @@ class Execute(): # pragma: no cover pass elif opcodeint == 58: val = self.machinestate.Stack_Omni.pop() - self.machinestate.Linear_Memory[0][int(immediates[1])] = np.in8(val & 0x000000ff) + self.machinestate.Linear_Memory[0][int(immediates[1])] = np.in8( + val & 0x000000FF + ) elif opcodeint == 59: val = self.machinestate.Stack_Omni.pop() - self.machinestate.Linear_Memory[0][int(immediates[1]) + 0] = np.in8(val & 0x000000ff) - self.machinestate.Linear_Memory[0][int(immediates[1]) + 1] = np.in8(val & 0x0000ff00 >> 8) + self.machinestate.Linear_Memory[0][ + int(immediates[1]) + 0 + ] = np.in8(val & 0x000000FF) + self.machinestate.Linear_Memory[0][ + int(immediates[1]) + 1 + ] = np.in8(val & 0x0000FF00 >> 8) elif opcodeint == 60: val = self.machinestate.Stack_Omni.pop() - self.machinestate.Linear_Memory[0][int(immediates[1])] = np.in8(val & 0x00000000000000ff) + self.machinestate.Linear_Memory[0][int(immediates[1])] = np.in8( + val & 0x00000000000000FF + ) elif opcodeint == 61: val = self.machinestate.Stack_Omni.pop() - self.machinestate.Linear_Memory[0][int(immediates[1]) + 0] = np.in8(val & 0x00000000000000ff) - self.machinestate.Linear_Memory[0][int(immediates[1]) + 1] = np.in8(val & 0x000000000000ff00 >> 8) + self.machinestate.Linear_Memory[0][ + int(immediates[1]) + 0 + ] = np.in8(val & 0x00000000000000FF) + self.machinestate.Linear_Memory[0][ + int(immediates[1]) + 1 + ] = np.in8(val & 0x000000000000FF00 >> 8) elif opcodeint == 62: val = self.machinestate.Stack_Omni.pop() - self.machinestate.Linear_Memory[0][int(immediates[1]) + 0] = np.in8(val & 0x00000000000000ff) - self.machinestate.Linear_Memory[0][int(immediates[1]) + 1] = np.in8(val & 0x000000000000ff00 >> 8) - self.machinestate.Linear_Memory[0][int(immediates[1]) + 2] = np.in8(val & 0x0000000000ff0000 >> 16) - self.machinestate.Linear_Memory[0][int(immediates[1]) + 3] = np.in8(val & 0x00000000ff000000 >> 24) + self.machinestate.Linear_Memory[0][ + int(immediates[1]) + 0 + ] = np.in8(val & 0x00000000000000FF) + self.machinestate.Linear_Memory[0][ + int(immediates[1]) + 1 + ] = np.in8(val & 0x000000000000FF00 >> 8) + self.machinestate.Linear_Memory[0][ + int(immediates[1]) + 2 + ] = np.in8(val & 0x0000000000FF0000 >> 16) + self.machinestate.Linear_Memory[0][ + int(immediates[1]) + 3 + ] = np.in8(val & 0x00000000FF000000 >> 24) else: - raise Exception(Colors.red + 'invalid store instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid store instruction" + Colors.ENDC + ) def run_current_memory(self, opcodeint, immediates): pass @@ -459,7 +589,9 @@ class Execute(): # pragma: no cover elif opcodeint == 68: self.machinestate.Stack_Omni.append(immediates[0]) else: - raise Exception(Colors.red + 'invalid const instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid const instruction" + Colors.ENDC + ) def run_eqz(self, opcodeint, immediates): if opcodeint == 69 or opcodeint == 80: @@ -469,10 +601,17 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid eqz instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid eqz instruction" + Colors.ENDC + ) def run_eq(self, opcodeint, immediates): - if opcodeint == 70 or opcodeint == 81 or opcodeint == 91 or opcodeint == 97: + if ( + opcodeint == 70 + or opcodeint == 81 + or opcodeint == 91 + or opcodeint == 97 + ): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if val1 == val2: @@ -480,10 +619,17 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid eq instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid eq instruction" + Colors.ENDC + ) def run_ne(self, opcodeint, immediates): - if opcodeint == 71 or opcodeint == 82 or opcodeint == 92 or opcodeint == 98: + if ( + opcodeint == 71 + or opcodeint == 82 + or opcodeint == 92 + or opcodeint == 98 + ): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if val1 != val2: @@ -491,7 +637,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid ne instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid ne instruction" + Colors.ENDC + ) def run_lt_s(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -507,7 +655,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid lt_s instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid lt_s instruction" + Colors.ENDC + ) def run_lt_u(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -523,7 +673,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid lt_u instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid lt_u instruction" + Colors.ENDC + ) def run_gt_s(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -539,7 +691,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid gt_s instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid gt_s instruction" + Colors.ENDC + ) def run_gt_u(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -555,7 +709,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid gt_u instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid gt_u instruction" + Colors.ENDC + ) def run_le_s(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -571,7 +727,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid le_s instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid le_s instruction" + Colors.ENDC + ) def run_le_u(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -587,7 +745,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid le_u instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid le_u instruction" + Colors.ENDC + ) def run_ge_s(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -603,7 +763,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid ge_s instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid ge_s instruction" + Colors.ENDC + ) def run_ge_u(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -619,7 +781,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid ge_u instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid ge_u instruction" + Colors.ENDC + ) def run_lt(self, opcodeint, immediates): v2 = self.machinestate.Stack_Omni.pop() @@ -635,7 +799,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid lt instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid lt instruction" + Colors.ENDC + ) def run_gt(self, opcodeint, immediates): v2 = self.machinestate.Stack_Omni.pop() @@ -651,7 +817,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid gt instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid gt instruction" + Colors.ENDC + ) def run_le(self, opcodeint, immediates): v2 = self.machinestate.Stack_Omni.pop() @@ -667,7 +835,9 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid le instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid le instruction" + Colors.ENDC + ) def run_ge(self, opcodeint, immediates): v2 = self.machinestate.Stack_Omni.pop() @@ -683,34 +853,42 @@ class Execute(): # pragma: no cover else: self.machinestate.Stack_Omni.append(0) else: - raise Exception(Colors.red + 'invalid ge instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid ge instruction" + Colors.ENDC + ) def run_clz(self, opcodeint, immediates): val = self.machinestate.Stack_Omni.pop() if opcodeint == 103: - self.machinestate.Stack_Omni.append(clz(val, 'uint32')) + self.machinestate.Stack_Omni.append(clz(val, "uint32")) elif opcodeint == 121: - self.machinestate.Stack_Omni.append(clz(val, 'uint64')) + self.machinestate.Stack_Omni.append(clz(val, "uint64")) else: - raise Exception(Colors.red + 'invalid clz instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid clz instruction" + Colors.ENDC + ) def run_ctz(self, opcodeint, immediates): val = self.machinestate.Stack_Omni.pop() if opcodeint == 104: - self.machinestate.Stack_Omni.append(ctz(val, 'uint32')) + self.machinestate.Stack_Omni.append(ctz(val, "uint32")) elif opcodeint == 122: - self.machinestate.Stack_Omni.append(ctz(val, 'uint64')) + self.machinestate.Stack_Omni.append(ctz(val, "uint64")) else: - raise Exception(Colors.red + 'invalid ctz instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid ctz instruction" + Colors.ENDC + ) def run_popcnt(self, opcodeint, immediates): val = self.machinestate.Stack_Omni.pop() if opcodeint == 105: - self.machinestate.Stack_Omni.append(pop_cnt(val, 'uint32')) + self.machinestate.Stack_Omni.append(pop_cnt(val, "uint32")) elif opcodeint == 123: - self.machinestate.Stack_Omni.append(pop_cnt(val, 'uint64')) + self.machinestate.Stack_Omni.append(pop_cnt(val, "uint64")) else: - raise Exception(Colors.red + 'invalid popcnt instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid popcnt instruction" + Colors.ENDC + ) def run_add(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -724,7 +902,9 @@ class Execute(): # pragma: no cover elif opcodeint == 160: self.machinestate.Stack_Omni.append(np.float64(val1 + val2)) else: - raise Exception(Colors.red + 'invalid add instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid add instruction" + Colors.ENDC + ) def run_sub(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -738,7 +918,9 @@ class Execute(): # pragma: no cover elif opcodeint == 161: self.machinestate.Stack_Omni.append(np.float64(val1 - val2)) else: - raise Exception(Colors.red + 'invalid sub instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid sub instruction" + Colors.ENDC + ) def run_mul(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -752,107 +934,169 @@ class Execute(): # pragma: no cover elif opcodeint == 162: self.machinestate.Stack_Omni.append(np.float64(val1 * val2)) else: - raise Exception(Colors.red + 'invalid mul instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid mul instruction" + Colors.ENDC + ) def run_div_s(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 109: - self.machinestate.Stack_Omni.append(np.int32(np.int32(val1) / np.int32(val2))) + self.machinestate.Stack_Omni.append( + np.int32(np.int32(val1) / np.int32(val2)) + ) elif opcodeint == 127: - self.machinestate.Stack_Omni.append(np.int64(np.int64(val1) / np.int64(val2))) + self.machinestate.Stack_Omni.append( + np.int64(np.int64(val1) / np.int64(val2)) + ) else: - raise Exception(Colors.red + 'invalid div_s instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid div_s instruction" + Colors.ENDC + ) def run_div_u(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 110: - self.machinestate.Stack_Omni.append(np.uint32(np.uint32(val1) / np.uint32(val2))) + self.machinestate.Stack_Omni.append( + np.uint32(np.uint32(val1) / np.uint32(val2)) + ) elif opcodeint == 128: - self.machinestate.Stack_Omni.append(np.uint64(np.uint64(val1) / np.uint64(val2))) + self.machinestate.Stack_Omni.append( + np.uint64(np.uint64(val1) / np.uint64(val2)) + ) else: - raise Exception(Colors.red + 'invalid div_u instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid div_u instruction" + Colors.ENDC + ) def run_rem_s(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 111: - self.machinestate.Stack_Omni.append(np.int32(np.int32(val1) % np.int32(val2))) + self.machinestate.Stack_Omni.append( + np.int32(np.int32(val1) % np.int32(val2)) + ) elif opcodeint == 129: - self.machinestate.Stack_Omni.append(np.int64(np.int64(val1) % np.int64(val2))) + self.machinestate.Stack_Omni.append( + np.int64(np.int64(val1) % np.int64(val2)) + ) else: - raise Exception(Colors.red + 'invalid rem_s instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid rem_s instruction" + Colors.ENDC + ) def run_rem_u(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 112: - self.machinestate.Stack_Omni.append(np.uint32(np.uint32(val1) % np.uint32(val2))) + self.machinestate.Stack_Omni.append( + np.uint32(np.uint32(val1) % np.uint32(val2)) + ) elif opcodeint == 130: - self.machinestate.Stack_Omni.append(np.uint64(np.uint64(val1) % np.uint64(val2))) + self.machinestate.Stack_Omni.append( + np.uint64(np.uint64(val1) % np.uint64(val2)) + ) else: - raise Exception(Colors.red + 'invalid rem_u instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid rem_u instruction" + Colors.ENDC + ) def run_and(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 113: - self.machinestate.Stack_Omni.append(np.uint32(np.uint32(val1) & np.uint32(val2))) + self.machinestate.Stack_Omni.append( + np.uint32(np.uint32(val1) & np.uint32(val2)) + ) elif opcodeint == 131: - self.machinestate.Stack_Omni.append(np.uint64(np.uint64(val1) & np.uint64(val2))) + self.machinestate.Stack_Omni.append( + np.uint64(np.uint64(val1) & np.uint64(val2)) + ) else: - raise Exception(Colors.red + 'invalid and instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid and instruction" + Colors.ENDC + ) def run_or(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 114: - self.machinestate.Stack_Omni.append(np.uint32(np.uint32(val1) | np.uint32(val2))) + self.machinestate.Stack_Omni.append( + np.uint32(np.uint32(val1) | np.uint32(val2)) + ) elif opcodeint == 132: - self.machinestate.Stack_Omni.append(np.uint64(np.uint64(val1) | np.uint64(val2))) + self.machinestate.Stack_Omni.append( + np.uint64(np.uint64(val1) | np.uint64(val2)) + ) else: - raise Exception(Colors.red + 'invalid or instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid or instruction" + Colors.ENDC + ) def run_xor(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 115: - self.machinestate.Stack_Omni.append(np.uint32(np.uint32(val1) ^ np.uint32(val2))) + self.machinestate.Stack_Omni.append( + np.uint32(np.uint32(val1) ^ np.uint32(val2)) + ) elif opcodeint == 133: - self.machinestate.Stack_Omni.append(np.uint64(np.uint64(val1) ^ np.uint64(val2))) + self.machinestate.Stack_Omni.append( + np.uint64(np.uint64(val1) ^ np.uint64(val2)) + ) else: - raise Exception(Colors.red + 'invalid xor instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid xor instruction" + Colors.ENDC + ) def run_shl(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 116: - self.machinestate.Stack_Omni.append(np.uint32(np.uint32(val1) << (np.uint32(val2)))) + self.machinestate.Stack_Omni.append( + np.uint32(np.uint32(val1) << (np.uint32(val2))) + ) elif opcodeint == 134: - self.machinestate.Stack_Omni.append(np.uint64(np.uint64(val1) << (np.uint64(val2)))) + self.machinestate.Stack_Omni.append( + np.uint64(np.uint64(val1) << (np.uint64(val2))) + ) else: - raise Exception(Colors.red + 'invalid shl instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid shl instruction" + Colors.ENDC + ) def run_shr_s(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 117: - self.machinestate.Stack_Omni.append(np.int32(np.int32(val1) >> (np.int32(val2)))) + self.machinestate.Stack_Omni.append( + np.int32(np.int32(val1) >> (np.int32(val2))) + ) elif opcodeint == 135: - self.machinestate.Stack_Omni.append(np.int64(np.int64(val1) >> (np.int64(val2)))) + self.machinestate.Stack_Omni.append( + np.int64(np.int64(val1) >> (np.int64(val2))) + ) else: - raise Exception(Colors.red + 'invalid shr_s instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid shr_s instruction" + Colors.ENDC + ) def run_shr_u(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 118: - self.machinestate.Stack_Omni.append(np.uint32(np.uint32(val1) >> (np.uint32(val2)))) + self.machinestate.Stack_Omni.append( + np.uint32(np.uint32(val1) >> (np.uint32(val2))) + ) elif opcodeint == 136: - self.machinestate.Stack_Omni.append(np.uint64(np.uint64(val1) >> (np.uint64(val2)))) + self.machinestate.Stack_Omni.append( + np.uint64(np.uint64(val1) >> (np.uint64(val2))) + ) else: - raise Exception(Colors.red + 'invalid shr_u instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid shr_u instruction" + Colors.ENDC + ) def run_rotl(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -862,7 +1106,9 @@ class Execute(): # pragma: no cover elif opcodeint == 137: self.machinestate.Stack_Omni.append(rol(val1, 64, val2)) else: - raise Exception(Colors.red + 'invalid rotl instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid rotl instruction" + Colors.ENDC + ) def run_rotr(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -872,55 +1118,71 @@ class Execute(): # pragma: no cover elif opcodeint == 138: self.machinestate.Stack_Omni.append(ror(val1, 32, val2)) else: - raise Exception(Colors.red + 'invalid rotl instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid rotl instruction" + Colors.ENDC + ) def run_abs(self, opcodeint, immediates): val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 139 or opcodeint == 153: self.machinestate.Stack_Omni.append(abs(val1)) else: - raise Exception(Colors.red + 'invalid abs instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid abs instruction" + Colors.ENDC + ) def run_neg(self, opcodeint, immediates): val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 140 or opcodeint == 154: self.machinestate.Stack_Omni.append(-val1) else: - raise Exception(Colors.red + 'invalid neg instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid neg instruction" + Colors.ENDC + ) def run_ceil(self, opcodeint, immediates): val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 141 or opcodeint == 155: self.machinestate.Stack_Omni.append(math.ceil(val1)) else: - raise Exception(Colors.red + 'invalid ceil instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid ceil instruction" + Colors.ENDC + ) def run_floor(self, opcodeint, immediates): if opcodeint == 142 or opcodeint == 156: self.machinestate.Stack_Omni.append(math.floor(val1)) else: - raise Exception(Colors.red + 'invalid floor instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid floor instruction" + Colors.ENDC + ) def run_trunc(self, opcodeint, immediates): val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 143 or opcodeint == 157: self.machinestate.Stack_Omni.append(math.trunc(val1)) else: - raise Exception(Colors.red + 'invalid trunc instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid trunc instruction" + Colors.ENDC + ) def run_nearest(self, opcodeint, immediates): val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 144 or opcodeint == 158: self.machinestate.Stack_Omni.append(round(val1)) else: - raise Exception(Colors.red + 'invalid nearest instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid nearest instruction" + Colors.ENDC + ) def run_sqrt(self, opcodeint, immediates): val1 = self.machinestate.Stack_Omni.pop() if opcodeint == 145 or opcodeint == 159: self.machinestate.Stack_Omni.append(math.sqrt(val1)) else: - raise Exception(Colors.red + 'invalid sqrt instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid sqrt instruction" + Colors.ENDC + ) def run_div(self, opcodeint, immediates): v2 = self.machinestate.Stack_Omni.pop() @@ -928,7 +1190,9 @@ class Execute(): # pragma: no cover if opcodeint == 149: self.machinestate.Stack_Omni.append(v1 / v2) else: - raise Exception(Colors.red + 'invalid float div instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid float div instruction" + Colors.ENDC + ) def run_min(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -936,7 +1200,9 @@ class Execute(): # pragma: no cover if opcodeint == 150 or opcodeint == 164: self.machinestate.Stack_Omni.append(min(val1, val2)) else: - raise Exception(Colors.red + 'invalid min instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid min instruction" + Colors.ENDC + ) def run_max(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -944,7 +1210,9 @@ class Execute(): # pragma: no cover if opcodeint == 151 or opcodeint == 165: self.machinestate.Stack_Omni.append(max(val1, val2)) else: - raise Exception(Colors.red + 'invalid max instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid max instruction" + Colors.ENDC + ) def run_copysign(self, opcodeint, immediates): val2 = self.machinestate.Stack_Omni.pop() @@ -952,7 +1220,9 @@ class Execute(): # pragma: no cover if opcodeint == 152 or opcodeint == 166: self.machinestate.Stack_Omni.append(math.copysign(val1, val2)) else: - raise Exception(Colors.red + 'invalid max instruction' + Colors.ENDC) + raise Exception( + Colors.red + "invalid max instruction" + Colors.ENDC + ) def run_i32wrapi64(self, opcodeint, immediates): val1 = self.machinestate.Stack_Omni.pop() @@ -1,6 +1,11 @@ from utils import Colors, init_interpret from opcodes import WASM_OP_Code -from section_structs import Func_Body, WASM_Ins, Resizable_Limits, Memory_Section +from section_structs import ( + Func_Body, + WASM_Ins, + Resizable_Limits, + Memory_Section, +) from execute import * import os import sys @@ -8,7 +13,7 @@ import signal # McCabe cyclomatic complexity metric -class Metric(): +class Metric: def __init__(self, code_section): self.code_section = code_section self.metric = [] @@ -21,9 +26,14 @@ class Metric(): for funcs in self.code_section.func_bodies: for ins in funcs.code: soc += 1 - #print(repr(ins.opcodeint)) - if ins.opcodeint == 4 or ins.opcodeint == 5 or ins.opcodeint == 12 \ - or ins.opcodeint == 13 or ins.opcodeint == 14: + # print(repr(ins.opcodeint)) + if ( + ins.opcodeint == 4 + or ins.opcodeint == 5 + or ins.opcodeint == 12 + or ins.opcodeint == 13 + or ins.opcodeint == 14 + ): Nodes += 2 Edges += 4 elif ins.opcode == 3: @@ -51,26 +61,39 @@ def DumpLinearMems(linear_memories, threshold): strrep = [] linmem_cnt = int() for lin_mem in linear_memories: - print('-----------------------------------------') - print(Colors.blue + Colors.BOLD + 'Linear Memory '+ repr(linmem_cnt)+ ' :' + Colors.ENDC) + print("-----------------------------------------") + print( + Colors.blue + + Colors.BOLD + + "Linear Memory " + + repr(linmem_cnt) + + " :" + + Colors.ENDC + ) for byte in lin_mem: if count >= threshold: break - if count%16 == 0: + if count % 16 == 0: for ch in strrep: # @DEVI-line feed messes the pretty format up if ord(ch) != 10: - print(Colors.green + ' ' + ch + Colors.ENDC, end = '') + print(Colors.green + " " + ch + Colors.ENDC, end="") else: pass print() strrep = [] - print(Colors.cyan + hex(count), ':\t' + Colors.ENDC, end='') + print(Colors.cyan + hex(count), ":\t" + Colors.ENDC, end="") strrep.append(str(chr(byte))) - print(Colors.blue + format(byte, '02x') + ' ' + Colors.ENDC, end='') + print( + Colors.blue + format(byte, "02x") + " " + Colors.ENDC, + end="", + ) else: strrep += str(chr(byte)) - print(Colors.blue + format(byte, '02x') + ' ' + Colors.ENDC, end='') + print( + Colors.blue + format(byte, "02x") + " " + Colors.ENDC, + end="", + ) count += 1 count = 0 print() @@ -78,30 +101,30 @@ def DumpLinearMems(linear_memories, threshold): # handles the debug options --idxspc. dumps the index spaces. def DumpIndexSpaces(machinestate): - print('-----------------------------------------') - print(Colors.green + 'Function Index Space: ' + Colors.ENDC) + print("-----------------------------------------") + print(Colors.green + "Function Index Space: " + Colors.ENDC) for iter in machinestate.Index_Space_Function: print(Colors.blue + repr(iter) + Colors.ENDC) - print('-----------------------------------------') - print(Colors.green + 'Globa Index Space: ' + Colors.ENDC) + print("-----------------------------------------") + print(Colors.green + "Globa Index Space: " + Colors.ENDC) for iter in machinestate.Index_Space_Global: print(Colors.blue + repr(iter) + Colors.ENDC) - print('-----------------------------------------') - print(Colors.green + 'Linear Memory Index Space: ' + Colors.ENDC) + print("-----------------------------------------") + print(Colors.green + "Linear Memory Index Space: " + Colors.ENDC) for iter in machinestate.Index_Space_Linear: print(Colors.blue + repr(iter) + Colors.ENDC) - print('-----------------------------------------') - print(Colors.green + 'Table Index Space: ' + Colors.ENDC) + print("-----------------------------------------") + print(Colors.green + "Table Index Space: " + Colors.ENDC) for iter in machinestate.Index_Space_Table: print(Colors.blue + repr(iter) + Colors.ENDC) - print('-----------------------------------------') + print("-----------------------------------------") # WIP-the Truebit Machine class -class TBMachine(): +class TBMachine: def __init__(self): # bytearray of size PAGE_SIZE self.Linear_Memory = [] @@ -121,7 +144,7 @@ class TBMachine(): # handles the initialization of the WASM machine -class TBInit(): +class TBInit: def __init__(self, module, machinestate): self.module = module self.machinestate = machinestate @@ -196,28 +219,31 @@ class TBInit(): self.module.memory_section.memory_types = [rsz_limits] self.module.memory_section.count = 1 for _ in self.module.memory_section.memory_types: - self.machinestate.Linear_Memory.append(bytearray( - WASM_OP_Code.PAGE_SIZE)) + self.machinestate.Linear_Memory.append( + bytearray(WASM_OP_Code.PAGE_SIZE) + ) if self.module.data_section is not None: for iter in self.module.data_section.data_segments: count = int() for byte in iter.data: - self.machinestate.Linear_Memory[iter.index][init_interpret(iter.offset) + count] = byte + self.machinestate.Linear_Memory[iter.index][ + init_interpret(iter.offset) + count + ] = byte count += 1 # returns the machinestate def getInits(self): - return(self.machinestate) + return self.machinestate # WIP-holds the run-rime data structures for a wasm machine -class RTE(): +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(self, func_body): @@ -225,7 +251,7 @@ class RTE(): # palceholder for the class that holds the validation functions -class ModuleValidation(): +class ModuleValidation: def __init__(self, module): self.module = module @@ -279,12 +305,12 @@ class ModuleValidation(): self.DataSection() self.TBCustom() - return(True) + return True # a convinience class that handles the initialization of the wasm machine and # interpretation of the code. -class VM(): +class VM: def __init__(self, modules): self.modules = modules self.machinestate = TBMachine() @@ -303,7 +329,7 @@ class VM(): self.parseflags = parseflags def getState(self): - return(self.machinestate) + return self.machinestate def initLocalIndexSpace(self, local_count): for i in range(0, local_count): @@ -312,33 +338,64 @@ class VM(): def getStartFunctionIndex(self): if self.modules[0].start_section is None: if self.parseflags.entry is None: - raise Exception(Colors.red + "module does not have a start section. no function index was provided with the --entry option.quitting..." + Colors.ENDC) + raise Exception( + Colors.red + + "module does not have a start section. no function index was provided with the --entry option.quitting..." + + Colors.ENDC + ) else: start_index = int(self.parseflags.entry) else: - print(Colors.green + "found start section: " + Colors.ENDC, end = '') + print(Colors.green + "found start section: " + Colors.ENDC, end="") start_index = self.modules[0].start_section.function_section_index - print(Colors.blue + Colors.BOLD + "running function at index " + repr(start_index) + Colors.ENDC) - if (start_index > len(self.modules[0].code_section.func_bodies) - 1): - raise Exception(Colors.red + "invalid function index: the function index does not exist." + Colors.ENDC) - return(start_index) + print( + Colors.blue + + Colors.BOLD + + "running function at index " + + repr(start_index) + + Colors.ENDC + ) + if start_index > len(self.modules[0].code_section.func_bodies) - 1: + raise Exception( + Colors.red + + "invalid function index: the function index does not exist." + + Colors.ENDC + ) + return start_index def getStartFunctionBody(self): start_index = self.getStartFunctionIndex() if isinstance(start_index, int): - self.start_function = self.modules[0].code_section.func_bodies[start_index] + self.start_function = self.modules[0].code_section.func_bodies[ + start_index + ] elif isinstance(start_index, str): # we have to import the function from another module/library. we # assume sys calls are not present.:w pass else: - raise Exception(Colors.red + "invalid entry for start function index" + Colors.ENDC) + raise Exception( + Colors.red + + "invalid entry for start function index" + + Colors.ENDC + ) def execute(self): - print(Colors.blue + Colors.BOLD + 'running module with code: ' + Colors.ENDC) + print( + Colors.blue + + Colors.BOLD + + "running module with code: " + + Colors.ENDC + ) for ins in self.start_function.code: - print(Colors.purple + repr(ins.opcode) + ' ' + repr(ins.operands) + Colors.ENDC) + print( + Colors.purple + + repr(ins.opcode) + + " " + + repr(ins.operands) + + Colors.ENDC + ) for ins in self.start_function.code: self.executewasm.getInstruction(ins.opcodeint, ins.operands) self.executewasm.callExecuteMethod() @@ -351,16 +408,33 @@ class VM(): self.executewasm.chargeGasMem(mem.initial) self.metric.mccabe() - print(Colors.red + "mccabe: " + repr(self.metric.getMcCabe()) + Colors.ENDC) - print(Colors.red + "soc: " + repr(self.metric.getSOC()) + Colors.ENDC) + print( + Colors.red + + "mccabe: " + + repr(self.metric.getMcCabe()) + + Colors.ENDC + ) + print( + Colors.red + "soc: " + repr(self.metric.getSOC()) + Colors.ENDC + ) # post-execution hook def endHook(self): if self.parseflags.gas: self.totGas = self.executewasm.getOPGas() - print(Colors.red + "total gas cost: " + repr(self.totGas) + Colors.ENDC) + print( + Colors.red + + "total gas cost: " + + repr(self.totGas) + + Colors.ENDC + ) if self.machinestate.Stack_Omni: - print(Colors.green + "stack top: " + repr(self.machinestate.Stack_Omni.pop()) + Colors.ENDC) + print( + Colors.green + + "stack top: " + + repr(self.machinestate.Stack_Omni.pop()) + + Colors.ENDC + ) # a convinience method def run(self): @@ -373,7 +447,7 @@ class VM(): # a wrapper class for VM. it timeouts instructions that take too long to # execute. -class Judicator(): +class Judicator: def __init__(self, op_time_table, module): self.op_time_table = op_time_table self.vm = VM(modules) @@ -384,20 +458,22 @@ class Judicator(): pid = os.fork() # child process if pid == 0: - sys.stdout = open('./jstdout', 'w') - sys.stderr = open('./jstderr', 'w') + sys.stdout = open("./jstdout", "w") + sys.stderr = open("./jstderr", "w") self.vm.execute() sys.exit() # parent process if pid > 0: cpid, status = os.waitpid(pid, 0) if status == 0: - print('overseer child exited successfully.') + print("overseer child exited successfully.") else: - print('overseer child exited with non-zero.') + print("overseer child exited with non-zero.") # pid < 0 else: - raise Exception(Colors.red + 'could not fork judicator overseer.' + Colors.ENDC) + raise Exception( + Colors.red + "could not fork judicator overseer." + Colors.ENDC + ) def setup(self): signal.signal(signal.SIGALRM, self.to_sighandler) @@ -1,6 +1,21 @@ from enum import Enum -SectionID = {0:"custom", 1:"type", 2:"import", 3:"function", 4:"table", 5:"memory", 6:"global", 7:"export", 8:"start", 9:"element", 10:"code", 11:"data", 63:"unknown"} +SectionID = { + 0: "custom", + 1: "type", + 2: "import", + 3: "function", + 4: "table", + 5: "memory", + 6: "global", + 7: "export", + 8: "start", + 9: "element", + 10: "code", + 11: "data", + 63: "unknown", +} + class RelocType(Enum): R_WEBASSEMBLY_FUNCTION_INDEX_LEB = 0 @@ -14,12 +29,14 @@ class RelocType(Enum): R_WEBASSEMPLY_FUNCTION_OFFSET_I32 = 8 R_WEBASSEMBLY_SECTION_OFFSET_I32 = 9 + class LinkingSubsection(Enum): WASM_SEGMENT_INFO = 5 WASM_INIT_FUNCS = 6 WASM_COMDAT_INFO = 7 WASM_SYMBOL_TABLE = 8 + class TypeType(Enum): none = 1 lebu = 2 @@ -27,27 +44,49 @@ class TypeType(Enum): flot = 4 dobl = 5 -class Syminfo_Kind(): + +class Syminfo_Kind: SYMTAB_FUNCTION = 0 SYMTAB_DATA = 1 SYMTAB_GLOBAL = 2 SYMTAB_SECTION = 3 -TypeKS = [['uint8', 8, TypeType.none], ['uint16', 16, TypeType.none], - ['uint32', 32, TypeType.none], ['uint64', 64, TypeType.none], - ['varuint1', 1, TypeType.lebu], ['varuint7', 7, TypeType.lebu], - ['varuint32', 32, TypeType.lebu], ['varuint64', 64, TypeType.lebu], - ['varint1', 1, TypeType.lebs], ['varint7', 7, TypeType.lebs], - ['varint32', 32, TypeType.lebs], ['varint64', 64, TypeType.lebs]] -TypeDic = {'uint8': 1, 'uint16': 2, 'uint32': 4, 'uint64': 8, - 'varuint1': 1, 'varuint7': 1, 'varuint32': 4, 'varuint64': 8, - 'varint1': 1, 'varint7': 1, 'varint32': 4, 'varint64': 8} +TypeKS = [ + ["uint8", 8, TypeType.none], + ["uint16", 16, TypeType.none], + ["uint32", 32, TypeType.none], + ["uint64", 64, TypeType.none], + ["varuint1", 1, TypeType.lebu], + ["varuint7", 7, TypeType.lebu], + ["varuint32", 32, TypeType.lebu], + ["varuint64", 64, TypeType.lebu], + ["varint1", 1, TypeType.lebs], + ["varint7", 7, TypeType.lebs], + ["varint32", 32, TypeType.lebs], + ["varint64", 64, TypeType.lebs], +] + +TypeDic = { + "uint8": 1, + "uint16": 2, + "uint32": 4, + "uint64": 8, + "varuint1": 1, + "varuint7": 1, + "varuint32": 4, + "varuint64": 8, + "varint1": 1, + "varint7": 1, + "varint32": 4, + "varint64": 8, +} + # holds the version 1.0 wasm opcodes and immediates class WASM_OP_Code: version_number = 0x01 - magic_number = 0x6d736100 + magic_number = 0x6D736100 PAGE_SIZE = 65536 uint8 = 1 uint16 = 2 @@ -64,269 +103,433 @@ class WASM_OP_Code: floatt = 4 doublet = 8 - all_ops = [('i32', '7f', False), ('i64', '7e', False), ('f32', '7d', False), - ('f64', '7c', False), ('anyfunc', '7b', False), - ('func', '60', False), ('empty_block_type', '40', False), - ('unreachable', '00', False), ('nop', '01', False), - ('block', '02', True, ('varuint7')), - ('loop', '03', True, ('varuint7')), - ('if', '04', True, ('varuint7')), ('else', '05', False), - ('end', '0b', False), ('br', '0c', True, ('varuint32')), - ('br_if', '0d', True, ('varuint32')), - ('br_table', '0e', True, ('varuint32', 'varuint32', 'varuint32')), - ('return', '0f', False), ('call', '10', True, ('varuint32')), - ('call_indirect', '11', True, ('varuint32', 'varuint1')), - ('drop', '1a', False), ('select', '1b', False), - ('get_local', '20', True, ('varuint32')), - ('set_local', '21', True, ('varuint32')), - ('tee_local', '22', True, ('varuint32')), - ('get_global', '23', True, ('varuint32')), - ('set_global', '24', True, ('varuint32')), - ('i32.load', '28', True, ('varuint32', 'varuint32')), - ('i64.load', '29', True, ('varuint32', 'varuint32')), - ('f32.load', '2a', True, ('varuint32', 'varuint32')), - ('f64.load', '2b', True, ('varuint32', 'varuint32')), - ('i32.load8_s', '2c', True, ('varuint32', 'varuint32')), - ('i32.load8_u', '2d', True, ('varuint32', 'varuint32')), - ('i32.load16_s', '2e', True, ('varuint32', 'varuint32')), - ('i32.load16_u', '2f', True, ('varuint32', 'varuint32')), - ('i64.load8_s', '30', True, ('varuint32', 'varuint32')), - ('i64.load8_u', '31', True, ('varuint32', 'varuint32')), - ('i64.load16_s', '32', True, ('varuint32', 'varuint32')), - ('i64.load16_u', '33', True, ('varuint32', 'varuint32')), - ('i64.load32_s', '34', True, ('varuint32', 'varuint32')), - ('i64.load32_u', '35', True, ('varuint32', 'varuint32')), - ('i32.store', '36', True, ('varuint32', 'varuint32')), - ('i64.store', '37', True, ('varuint32', 'varuint32')), - ('f32.store', '38', True, ('varuint32', 'varuint32')), - ('f64.store', '39', True, ('varuint32', 'varuint32')), - ('i32.store8', '3a', True, ('varuint32', 'varuint32')), - ('i32.store16', '3b', True, ('varuint32', 'varuint32')), - ('i64.store8', '3c', True, ('varuint32', 'varuint32')), - ('i64.store16', '3d', True, ('varuint32', 'varuint32')), - ('i64.store32', '3e', True, ('varuint32', 'varuint32')), - ('current_memory', '3f', True, ('varuint1')), - ('grow_memory', '40', True, ('varuint1')), - ('i32.const', '41', True, ('varint32')), - ('i64.const', '42', True, ('varint64')), - ('f32.const', '43', True, ('uint32')), - ('f64.const', '44', True, ('uint64')), - ('i32.eqz', '45', False), ('i32.eq', '46', False), - ('i32.ne', '47', False), ('i32.lt_s', '48', False), - ('i32.lt_u', '49', False), ('i32.gt_s', '4a', False), - ('i32.gt_u', '4b', False), ('i32.le_s', '4c', False), - ('i32.le_u', '4d', False), ('i32.ge_s', '4e', False), - ('i32.ge_u', '4f', False), ('i64.eqz', '50', False), - ('i64.eq', '51', False), ('i64.ne', '52', False), - ('i64.lt_s', '53', False), ('i64.lt_u', '54', False), - ('i64.gt_s', '55', False), ('i64.gt_u', '56', False), - ('i64.le_s', '57', False), ('i64.le_u', '58', False), - ('i64.ge_s', '59', False), ('i64.ge_u', '5a', False), - ('f32.eq', '5b', False), ('f32.ne', '5c', False), - ('f32.lt', '5d', False), ('f32.gt', '5e', False), - ('f32.le', '5f', False), ('f32.ge', '60', False), - ('f64.eq', '61', False), ('f64.ne', '62', False), - ('f64.lt', '63', False), ('f64.gt', '64', False), - ('f64.le', '65', False), ('f64.ge', '66', False), - ('i32.clz', '67', False), ('i32.ctz', '68', False), - ('i32.popcnt', '69', False), ('i32.add', '6a', False), - ('i32.sub', '6b', False), ('i32.mul', '6c', False), - ('i32.div_s', '6d', False), ('i32.div_u', '6e', False), - ('i32.rem_s', '6f', False), ('i32.rem_u', '70', False), - ('i32.and', '71', False), ('i32.or', '72', False), - ('i32.xor', '73', False), ('i32.shl', '74', False), - ('i32.shr_s', '75', False), ('i32.shr_u', '76', False), - ('i32.rotl', '77', False), ('i32.rotr', '78', False), - ('i64.clz', '79', False), ('i64.ctz', '7a', False), - ('i64.popcnt', '7b', False), ('i64.add', '7c', False), - ('i64.sub', '7d', False), ('i64.mul', '7e', False), - ('i64.div_s', '7f', False), ('i64.div_u', '80', False), - ('i64.rem_s', '81', False), ('i64.rem_u', '82', False), - ('i64.and', '83', False), ('i64.or', '84', False), - ('i64.xor', '85', False), ('i64.shl', '86', False), - ('i64.shr_s', '87', False), ('i64.shr_u', '88', False), - ('i64.rotl', '89', False), ('i63.rotr', '8a', False), - ('f32.abs', '8b', False), ('f32.neg', '8c', False), - ('f32.ceil', '8d', False), ('f32.floor', '8e', False), - ('f32.trunc', '8f', False), ('f32.nearest', '90', False), - ('f32.sqrt', '91', False), ('f32.add', '92', False), - ('f32.sub', '93', False), ('f32.mul', '94', False), - ('f32.div', '95', False), ('f32.min', '96', False), - ('f32.max', '97', False), ('f32.copysign', '98', False), - ('f64.abs', '99', False), ('f64.neg', '9a', False), - ('f64.ceil', '9b', False), ('f64.floor', '9c', False), - ('f64.trunc', '9d', False), ('f64.nearest', '9e', False), - ('f64.sqrt', '9f', False), ('f64.add', 'a0', False), - ('f64.sub', 'a1', False), ('f64.mul', 'a2', False), - ('f64.div', 'a3', False), ('f64.min', 'a4', False), - ('f64.max', 'a5', False), ('f64.copysign', 'a6', False), - ('i32.wrap/i64', 'a7', False), ('i32.trunc_s/f32', 'a8', False), - ('i32.trunc_u/f32', 'a9', False), - ('i32.trunc_s/f64', 'aa', False), - ('i32.trunc_u/f64', 'ab', False), - ('i64.extend_s/i32', 'ac', False), - ('i64.extend_u/i32', 'ad', False), - ('i64.trunc_s/f32', 'ae', False), - ('i64.trunc_u/f32', 'af', False), - ('i64.trunc_s/f64', 'b0', False), - ('i64.trunc_u/f64', 'b1', False), - ('f32.convert_s/i32', 'b2', False), - ('f32.convert_u/i32', 'b3', False), - ('f32.convert_s/i64', 'b4', False), - ('f32.convert_u/i64', 'b5', False), - ('f32.demote/f64', 'b6', False), - ('f64.convert_s/i32', 'b7', False), - ('f64.convert_u/i32', 'b8', False), - ('f64.convert_s/i64', 'b9', False), - ('f64.convert_u/i64', 'ba', False), - ('f64.promote/f32', 'bb', False), - ('i32.reinterpret/f32', 'bc', False), - ('i64.reinterpret/f64', 'bd', False), - ('f32.reinterpret/i32', 'be', False), - ('f64.reinterpret/i64', 'bf', False)] + all_ops = [ + ("i32", "7f", False), + ("i64", "7e", False), + ("f32", "7d", False), + ("f64", "7c", False), + ("anyfunc", "7b", False), + ("func", "60", False), + ("empty_block_type", "40", False), + ("unreachable", "00", False), + ("nop", "01", False), + ("block", "02", True, ("varuint7")), + ("loop", "03", True, ("varuint7")), + ("if", "04", True, ("varuint7")), + ("else", "05", False), + ("end", "0b", False), + ("br", "0c", True, ("varuint32")), + ("br_if", "0d", True, ("varuint32")), + ("br_table", "0e", True, ("varuint32", "varuint32", "varuint32")), + ("return", "0f", False), + ("call", "10", True, ("varuint32")), + ("call_indirect", "11", True, ("varuint32", "varuint1")), + ("drop", "1a", False), + ("select", "1b", False), + ("get_local", "20", True, ("varuint32")), + ("set_local", "21", True, ("varuint32")), + ("tee_local", "22", True, ("varuint32")), + ("get_global", "23", True, ("varuint32")), + ("set_global", "24", True, ("varuint32")), + ("i32.load", "28", True, ("varuint32", "varuint32")), + ("i64.load", "29", True, ("varuint32", "varuint32")), + ("f32.load", "2a", True, ("varuint32", "varuint32")), + ("f64.load", "2b", True, ("varuint32", "varuint32")), + ("i32.load8_s", "2c", True, ("varuint32", "varuint32")), + ("i32.load8_u", "2d", True, ("varuint32", "varuint32")), + ("i32.load16_s", "2e", True, ("varuint32", "varuint32")), + ("i32.load16_u", "2f", True, ("varuint32", "varuint32")), + ("i64.load8_s", "30", True, ("varuint32", "varuint32")), + ("i64.load8_u", "31", True, ("varuint32", "varuint32")), + ("i64.load16_s", "32", True, ("varuint32", "varuint32")), + ("i64.load16_u", "33", True, ("varuint32", "varuint32")), + ("i64.load32_s", "34", True, ("varuint32", "varuint32")), + ("i64.load32_u", "35", True, ("varuint32", "varuint32")), + ("i32.store", "36", True, ("varuint32", "varuint32")), + ("i64.store", "37", True, ("varuint32", "varuint32")), + ("f32.store", "38", True, ("varuint32", "varuint32")), + ("f64.store", "39", True, ("varuint32", "varuint32")), + ("i32.store8", "3a", True, ("varuint32", "varuint32")), + ("i32.store16", "3b", True, ("varuint32", "varuint32")), + ("i64.store8", "3c", True, ("varuint32", "varuint32")), + ("i64.store16", "3d", True, ("varuint32", "varuint32")), + ("i64.store32", "3e", True, ("varuint32", "varuint32")), + ("current_memory", "3f", True, ("varuint1")), + ("grow_memory", "40", True, ("varuint1")), + ("i32.const", "41", True, ("varint32")), + ("i64.const", "42", True, ("varint64")), + ("f32.const", "43", True, ("uint32")), + ("f64.const", "44", True, ("uint64")), + ("i32.eqz", "45", False), + ("i32.eq", "46", False), + ("i32.ne", "47", False), + ("i32.lt_s", "48", False), + ("i32.lt_u", "49", False), + ("i32.gt_s", "4a", False), + ("i32.gt_u", "4b", False), + ("i32.le_s", "4c", False), + ("i32.le_u", "4d", False), + ("i32.ge_s", "4e", False), + ("i32.ge_u", "4f", False), + ("i64.eqz", "50", False), + ("i64.eq", "51", False), + ("i64.ne", "52", False), + ("i64.lt_s", "53", False), + ("i64.lt_u", "54", False), + ("i64.gt_s", "55", False), + ("i64.gt_u", "56", False), + ("i64.le_s", "57", False), + ("i64.le_u", "58", False), + ("i64.ge_s", "59", False), + ("i64.ge_u", "5a", False), + ("f32.eq", "5b", False), + ("f32.ne", "5c", False), + ("f32.lt", "5d", False), + ("f32.gt", "5e", False), + ("f32.le", "5f", False), + ("f32.ge", "60", False), + ("f64.eq", "61", False), + ("f64.ne", "62", False), + ("f64.lt", "63", False), + ("f64.gt", "64", False), + ("f64.le", "65", False), + ("f64.ge", "66", False), + ("i32.clz", "67", False), + ("i32.ctz", "68", False), + ("i32.popcnt", "69", False), + ("i32.add", "6a", False), + ("i32.sub", "6b", False), + ("i32.mul", "6c", False), + ("i32.div_s", "6d", False), + ("i32.div_u", "6e", False), + ("i32.rem_s", "6f", False), + ("i32.rem_u", "70", False), + ("i32.and", "71", False), + ("i32.or", "72", False), + ("i32.xor", "73", False), + ("i32.shl", "74", False), + ("i32.shr_s", "75", False), + ("i32.shr_u", "76", False), + ("i32.rotl", "77", False), + ("i32.rotr", "78", False), + ("i64.clz", "79", False), + ("i64.ctz", "7a", False), + ("i64.popcnt", "7b", False), + ("i64.add", "7c", False), + ("i64.sub", "7d", False), + ("i64.mul", "7e", False), + ("i64.div_s", "7f", False), + ("i64.div_u", "80", False), + ("i64.rem_s", "81", False), + ("i64.rem_u", "82", False), + ("i64.and", "83", False), + ("i64.or", "84", False), + ("i64.xor", "85", False), + ("i64.shl", "86", False), + ("i64.shr_s", "87", False), + ("i64.shr_u", "88", False), + ("i64.rotl", "89", False), + ("i63.rotr", "8a", False), + ("f32.abs", "8b", False), + ("f32.neg", "8c", False), + ("f32.ceil", "8d", False), + ("f32.floor", "8e", False), + ("f32.trunc", "8f", False), + ("f32.nearest", "90", False), + ("f32.sqrt", "91", False), + ("f32.add", "92", False), + ("f32.sub", "93", False), + ("f32.mul", "94", False), + ("f32.div", "95", False), + ("f32.min", "96", False), + ("f32.max", "97", False), + ("f32.copysign", "98", False), + ("f64.abs", "99", False), + ("f64.neg", "9a", False), + ("f64.ceil", "9b", False), + ("f64.floor", "9c", False), + ("f64.trunc", "9d", False), + ("f64.nearest", "9e", False), + ("f64.sqrt", "9f", False), + ("f64.add", "a0", False), + ("f64.sub", "a1", False), + ("f64.mul", "a2", False), + ("f64.div", "a3", False), + ("f64.min", "a4", False), + ("f64.max", "a5", False), + ("f64.copysign", "a6", False), + ("i32.wrap/i64", "a7", False), + ("i32.trunc_s/f32", "a8", False), + ("i32.trunc_u/f32", "a9", False), + ("i32.trunc_s/f64", "aa", False), + ("i32.trunc_u/f64", "ab", False), + ("i64.extend_s/i32", "ac", False), + ("i64.extend_u/i32", "ad", False), + ("i64.trunc_s/f32", "ae", False), + ("i64.trunc_u/f32", "af", False), + ("i64.trunc_s/f64", "b0", False), + ("i64.trunc_u/f64", "b1", False), + ("f32.convert_s/i32", "b2", False), + ("f32.convert_u/i32", "b3", False), + ("f32.convert_s/i64", "b4", False), + ("f32.convert_u/i64", "b5", False), + ("f32.demote/f64", "b6", False), + ("f64.convert_s/i32", "b7", False), + ("f64.convert_u/i32", "b8", False), + ("f64.convert_s/i64", "b9", False), + ("f64.convert_u/i64", "ba", False), + ("f64.promote/f32", "bb", False), + ("i32.reinterpret/f32", "bc", False), + ("i64.reinterpret/f64", "bd", False), + ("f32.reinterpret/i32", "be", False), + ("f64.reinterpret/i64", "bf", False), + ] - type_ops = [('i32', '7f'), ('i64', '7e'), ('f32', '7d'), - ('f64', '7c'), ('anyfunc', '7b'), ('func', '60'), - ('empty_block_type', '40')] + type_ops = [ + ("i32", "7f"), + ("i64", "7e"), + ("f32", "7d"), + ("f64", "7c"), + ("anyfunc", "7b"), + ("func", "60"), + ("empty_block_type", "40"), + ] type_ops_dict = dict(type_ops) type_ops_dict_rev = {v: k for k, v in type_ops_dict.items()} - control_flow_ops = [('unreachable', '00'), ('nop', '01'), - ('block', '02'), ('loop', '03'), - ('if', '04'), ('else', '05'), - ('end', '0b'), ('br', '0c'), - ('br_if', '0d'), ('br_table', '0e'), - ('return', '0f')] + control_flow_ops = [ + ("unreachable", "00"), + ("nop", "01"), + ("block", "02"), + ("loop", "03"), + ("if", "04"), + ("else", "05"), + ("end", "0b"), + ("br", "0c"), + ("br_if", "0d"), + ("br_table", "0e"), + ("return", "0f"), + ] control_flow_ops_dict = dict(control_flow_ops) - control_flow_ops_dict_rev = {v: k for k, v in control_flow_ops_dict.items()} + control_flow_ops_dict_rev = { + v: k for k, v in control_flow_ops_dict.items() + } - call_ops = [('call', '10'), ('call_indirect', '11')] + call_ops = [("call", "10"), ("call_indirect", "11")] call_ops_dict = dict(call_ops) call_ops_dict_rev = {v: k for k, v in call_ops_dict.items()} - param_ops = [('drop', '1a'), ('select', '1b')] + param_ops = [("drop", "1a"), ("select", "1b")] param_ops_dict = dict(param_ops) param_ops_dict_rev = {v: k for k, v in param_ops_dict.items()} - var_access = [('get_local', '20'), ('set_local', '21'), - ('tee_local', '22'), ('get_global', '23'), - ('set_global', '24')] + var_access = [ + ("get_local", "20"), + ("set_local", "21"), + ("tee_local", "22"), + ("get_global", "23"), + ("set_global", "24"), + ] var_access_dict = dict(var_access) var_access_dict_rev = {v: k for k, v in var_access_dict.items()} - mem_ops = [('i32.load', '28'), ('i64.load', '29'), - ('f32.load', '2a'), ('f64.load', '2b'), - ('i32.load8_s', '2c'), ('i32.load8_u', '2d'), - ('i32.load16_s', '2e'), ('i32.load16_u', '2f'), - ('i64.load8_s', '30'), ('i64.load8_u', '31'), - ('i64.load16_s', '32'), ('i64.load16_u', '33'), - ('i64.load32_s', '34'), ('i64.load32_u', '35'), - ('i32.store', '36'), ('i64.store', '37'), - ('f32.store', '38'), ('f64.store', '39'), - ('i32.store8', '3a'), ('i32.store16', '3b'), - ('i64.store8', '3c'), ('i64.store16', '3d'), - ('i64.store32', '3e'), ('current_memory', '3f'), - ('grow_memory', '40')] + mem_ops = [ + ("i32.load", "28"), + ("i64.load", "29"), + ("f32.load", "2a"), + ("f64.load", "2b"), + ("i32.load8_s", "2c"), + ("i32.load8_u", "2d"), + ("i32.load16_s", "2e"), + ("i32.load16_u", "2f"), + ("i64.load8_s", "30"), + ("i64.load8_u", "31"), + ("i64.load16_s", "32"), + ("i64.load16_u", "33"), + ("i64.load32_s", "34"), + ("i64.load32_u", "35"), + ("i32.store", "36"), + ("i64.store", "37"), + ("f32.store", "38"), + ("f64.store", "39"), + ("i32.store8", "3a"), + ("i32.store16", "3b"), + ("i64.store8", "3c"), + ("i64.store16", "3d"), + ("i64.store32", "3e"), + ("current_memory", "3f"), + ("grow_memory", "40"), + ] mem_ops_dict = dict(mem_ops) mem_ops_dict_rev = {v: k for k, v in mem_ops_dict.items()} - consts = [('i32.const', '41'), ('i64.const', '42'), - ('f32.const', '43'), ('f64', '44')] + consts = [ + ("i32.const", "41"), + ("i64.const", "42"), + ("f32.const", "43"), + ("f64", "44"), + ] consts_dict = dict(consts) consts_dict_rev = {v: k for k, v in consts_dict.items()} - comp_ops = [('i32.eqz', '45'), ('i32.eq', '46'), ('i32.ne', '47'), - ('i32.lt_s', '48'), ('i32.lt_u', '49'), - ('i32.gt_s', '4a'), ('i32.gt_u', '4b'), - ('i32.le_s', '4c'), ('i32.le_u', '4d'), - ('i32.ge_s', '4e'), ('i32.ge_u', '4f'), - ('i64.eqz', '50'), ('i64.eq', '51'), - ('i64.ne', '52'), ('i64.lt_s', '53'), - ('i64.lt_u', '54'), ('i64.gt_s', '55'), - ('i64.gt_u', '56'), ('i64.le_s', '57'), - ('i64.le_u', '58'), ('i64.ge_s', '59'), - ('i64.ge_u', '5a'), ('f32.eq', '5b'), - ('f32.ne', '5c'), ('f32.lt', '5d'), - ('f32.gt', '5e'), ('f32.le', '5f'), - ('f32.ge', '60'), ('f64.eq', '61'), - ('f64.ne', '62'), ('f64.lt', '63'), - ('f64.gt', '64'), ('f64.le', '65'), - ('f64.ge', '66')] + comp_ops = [ + ("i32.eqz", "45"), + ("i32.eq", "46"), + ("i32.ne", "47"), + ("i32.lt_s", "48"), + ("i32.lt_u", "49"), + ("i32.gt_s", "4a"), + ("i32.gt_u", "4b"), + ("i32.le_s", "4c"), + ("i32.le_u", "4d"), + ("i32.ge_s", "4e"), + ("i32.ge_u", "4f"), + ("i64.eqz", "50"), + ("i64.eq", "51"), + ("i64.ne", "52"), + ("i64.lt_s", "53"), + ("i64.lt_u", "54"), + ("i64.gt_s", "55"), + ("i64.gt_u", "56"), + ("i64.le_s", "57"), + ("i64.le_u", "58"), + ("i64.ge_s", "59"), + ("i64.ge_u", "5a"), + ("f32.eq", "5b"), + ("f32.ne", "5c"), + ("f32.lt", "5d"), + ("f32.gt", "5e"), + ("f32.le", "5f"), + ("f32.ge", "60"), + ("f64.eq", "61"), + ("f64.ne", "62"), + ("f64.lt", "63"), + ("f64.gt", "64"), + ("f64.le", "65"), + ("f64.ge", "66"), + ] comp_ops_dict = dict(comp_ops) comp_ops_dict_rev = {v: k for k, v in comp_ops_dict.items()} - num_ops = [('i32.clz', '67'), ('i32.ctz', '68'), - ('i32.popcnt', '69'), ('i32.add', '6a'), - ('i32.sub', '6b'), ('i32.mul', '6c'), - ('i32.div_s', '6d'), ('i32.div_u', '6e'), - ('i32.rem_s', '6e'), ('i32.rem_u', '70'), - ('i32.and', '71'), ('i32.or', '72'), - ('i32.xor', '73'), ('i32.shl', '74'), - ('i32.shr_s', '75'), ('i32.shr_u', '76'), - ('i32.rotl', '77'), ('i32.rotr', '78'), - ('i64.clz', '79'), ('i64.ctz', '7a'), - ('i64.popcnt', '7b'), ('i64.add', '7c'), - ('i64.sub', '7d'), ('i64.mul', '7e'), - ('i64.div_s', '7f'), ('i64.div_u', '80'), - ('i64.rem_s', '81'), ('i64.rem_u', '82'), - ('i64.and', '83'), ('i64.or', '84'), - ('i64.xor', '85'), ('i64.shl', '86'), - ('i64.shr_s', '87'), ('i64.shr_u', '88'), - ('i64.rotl', '89'), ('i63.rotr', '8a'), - ('f32.abs', '8b'), ('f32.neg', '8c'), - ('f32.ceil', '8d'), ('f32.floor', '8e'), - ('f32.trunc', '8f'), ('f32.nearest', '90'), - ('f32.sqrt', '91'), ('f32.add', '92'), - ('f32.sub', '93'), ('f32.mul', '94'), - ('f32.div', '95'), ('f32.min', '96'), - ('f32.max', '97'), ('f32.copysign', '98'), - ('f64.abs', '99'), ('f64.neg', '9a'), - ('f64.ceil', '9b'), ('f64.floor', '9c'), - ('f64.trunc', '9d'), ('f64.nearest', '9e'), - ('f64.sqrt', '9f'), ('f64.add', 'a0'), - ('f64.sub', 'a1'), ('f64.mul', 'a2'), - ('f64.div', 'a3'), ('f64.min', 'a4'), - ('f64.max', 'a5'), ('f64.copysign', 'a6')] + num_ops = [ + ("i32.clz", "67"), + ("i32.ctz", "68"), + ("i32.popcnt", "69"), + ("i32.add", "6a"), + ("i32.sub", "6b"), + ("i32.mul", "6c"), + ("i32.div_s", "6d"), + ("i32.div_u", "6e"), + ("i32.rem_s", "6e"), + ("i32.rem_u", "70"), + ("i32.and", "71"), + ("i32.or", "72"), + ("i32.xor", "73"), + ("i32.shl", "74"), + ("i32.shr_s", "75"), + ("i32.shr_u", "76"), + ("i32.rotl", "77"), + ("i32.rotr", "78"), + ("i64.clz", "79"), + ("i64.ctz", "7a"), + ("i64.popcnt", "7b"), + ("i64.add", "7c"), + ("i64.sub", "7d"), + ("i64.mul", "7e"), + ("i64.div_s", "7f"), + ("i64.div_u", "80"), + ("i64.rem_s", "81"), + ("i64.rem_u", "82"), + ("i64.and", "83"), + ("i64.or", "84"), + ("i64.xor", "85"), + ("i64.shl", "86"), + ("i64.shr_s", "87"), + ("i64.shr_u", "88"), + ("i64.rotl", "89"), + ("i63.rotr", "8a"), + ("f32.abs", "8b"), + ("f32.neg", "8c"), + ("f32.ceil", "8d"), + ("f32.floor", "8e"), + ("f32.trunc", "8f"), + ("f32.nearest", "90"), + ("f32.sqrt", "91"), + ("f32.add", "92"), + ("f32.sub", "93"), + ("f32.mul", "94"), + ("f32.div", "95"), + ("f32.min", "96"), + ("f32.max", "97"), + ("f32.copysign", "98"), + ("f64.abs", "99"), + ("f64.neg", "9a"), + ("f64.ceil", "9b"), + ("f64.floor", "9c"), + ("f64.trunc", "9d"), + ("f64.nearest", "9e"), + ("f64.sqrt", "9f"), + ("f64.add", "a0"), + ("f64.sub", "a1"), + ("f64.mul", "a2"), + ("f64.div", "a3"), + ("f64.min", "a4"), + ("f64.max", "a5"), + ("f64.copysign", "a6"), + ] num_ops_dict = dict(num_ops) num_ops_dict_rev = {v: k for k, v in num_ops_dict.items()} - conversion = [('i32.wrap/i64', 'a7'), - ('i32.trunc_s/f32', 'a8'), - ('i32.trunc_u/f32', 'a9'), - ('i32.trunc_s/f64', 'aa'), - ('i32.trunc_u/f64', 'ab'), - ('i64.extend_s/i32', 'ac'), - ('i64.extend_u/i32', 'ad'), - ('i64.trunc_s/f32', 'ae'), - ('i64.trunc_u/f32', 'af'), - ('i64.trunc_s/f64', 'b0'), - ('i64.trunc_u/f64', 'b1'), - ('f32.convert_s/i32', 'b2'), - ('f32.convert_u/i32', 'b3'), - ('f32.convert_s/i64', 'b4'), - ('f32.convert_u/i64', 'b5'), - ('f32.demote/f64', 'b6'), - ('f64.convert_s/i32', 'b7'), - ('f64.convert_u/i32', 'b8'), - ('f64.convert_s/i64', 'b9'), - ('f64.convert_u/i64', 'ba'), - ('f64.promote/f32', 'bb')] + conversion = [ + ("i32.wrap/i64", "a7"), + ("i32.trunc_s/f32", "a8"), + ("i32.trunc_u/f32", "a9"), + ("i32.trunc_s/f64", "aa"), + ("i32.trunc_u/f64", "ab"), + ("i64.extend_s/i32", "ac"), + ("i64.extend_u/i32", "ad"), + ("i64.trunc_s/f32", "ae"), + ("i64.trunc_u/f32", "af"), + ("i64.trunc_s/f64", "b0"), + ("i64.trunc_u/f64", "b1"), + ("f32.convert_s/i32", "b2"), + ("f32.convert_u/i32", "b3"), + ("f32.convert_s/i64", "b4"), + ("f32.convert_u/i64", "b5"), + ("f32.demote/f64", "b6"), + ("f64.convert_s/i32", "b7"), + ("f64.convert_u/i32", "b8"), + ("f64.convert_s/i64", "b9"), + ("f64.convert_u/i64", "ba"), + ("f64.promote/f32", "bb"), + ] conversion_dict = dict(conversion) conversion_dict_rev = {v: k for k, v in conversion_dict.items()} - reinterpretations = [('i32.reinterpret/f32', 'bc'), - ('i64.reinterpret/f64', 'bd'), - ('f32.reinterpret/i32', 'be'), - ('f64.reinterpret/i64', 'bf')] + reinterpretations = [ + ("i32.reinterpret/f32", "bc"), + ("i64.reinterpret/f64", "bd"), + ("f32.reinterpret/i32", "be"), + ("f64.reinterpret/i64", "bf"), + ] reinterpretations_dict = dict(reinterpretations) - reinterpretations_dict_rev = {v: k for k, - v in reinterpretations_dict.items()} + reinterpretations_dict_rev = { + v: k for k, v in reinterpretations_dict.items() + } - section_code = [('type', '01'), ('import', '02'), - ('function', '03'), ('table', '04'), - ('memory', '05'), ('global', '06'), - ('export', '07'), ('start', '08'), - ('element', '09'), ('code', '0a'), - ('data', '0b'), ('custom', '00')] + section_code = [ + ("type", "01"), + ("import", "02"), + ("function", "03"), + ("table", "04"), + ("memory", "05"), + ("global", "06"), + ("export", "07"), + ("start", "08"), + ("element", "09"), + ("code", "0a"), + ("data", "0b"), + ("custom", "00"), + ] section_code_dict = dict(section_code) section_code_dict_rev = {v: k for k, v in section_code_dict.items()} @@ -12,10 +12,12 @@ import code _DBG_ = True + def SigHandler_SIGINT(signum, frame): print() sys.exit(0) + # we first read the object file and put all the sections in this class class ParsedStruct: def __init__(self): @@ -38,48 +40,124 @@ def Conver2Int(little_endian, size, bytelist): if little_endian: for bit in reversed(bytelist): if bit != 0x80: - sum += bit*(pow(16, modifier)) + sum += bit * (pow(16, modifier)) modifier -= 1 - return(sum) + return sum else: for bit in reversed(bytelist): if bit != 0x80: - sum += bit*(pow(16, modifier)) + sum += bit * (pow(16, modifier)) modifier -= 1 - return(sum) + return sum + # the argparser class Argparser(object): def __init__(self): parser = argparse.ArgumentParser() - parser.add_argument("--wast", type=str, help="path to the wasm text file") - parser.add_argument("--wasm", type=str, nargs='+', help="path to the wasm object file") - parser.add_argument("--asb", type=str, help="path to the wast file to assemble") - parser.add_argument("--dis", type=str, help="path to the wasm file to disassemble") + parser.add_argument( + "--wast", type=str, help="path to the wasm text file" + ) + parser.add_argument( + "--wasm", type=str, nargs="+", help="path to the wasm object file" + ) + parser.add_argument( + "--asb", type=str, help="path to the wast file to assemble" + ) + parser.add_argument( + "--dis", type=str, help="path to the wasm file to disassemble" + ) parser.add_argument("-o", type=str, help="the path to the output file") - parser.add_argument("--dbg", action='store_true', help="print debug info", default=False) - parser.add_argument("--unval", action='store_true', help="skips validation tests", default=False) - parser.add_argument("--memdump", type=int, help="dumps the linear memory") - parser.add_argument("--idxspc", action='store_true', help="print index space data", default=False) - parser.add_argument("--run", action='store_true', help="runs the start function", default=False) - parser.add_argument("--metric", action='store_true', help="print metrics", default=False) - parser.add_argument("--gas", action='store_true', help="print gas usage", default=False) - parser.add_argument("--entry", type=str, help="name of the function that will act as the entry point into execution") - parser.add_argument("--link", type=str, nargs="+", help="link the following wasm modules") - parser.add_argument("--sectiondump", type=str, help="dumps the section provided") + parser.add_argument( + "--dbg", + action="store_true", + help="print debug info", + default=False, + ) + parser.add_argument( + "--unval", + action="store_true", + help="skips validation tests", + default=False, + ) + parser.add_argument( + "--memdump", type=int, help="dumps the linear memory" + ) + parser.add_argument( + "--idxspc", + action="store_true", + help="print index space data", + default=False, + ) + parser.add_argument( + "--run", + action="store_true", + help="runs the start function", + default=False, + ) + parser.add_argument( + "--metric", + action="store_true", + help="print metrics", + default=False, + ) + parser.add_argument( + "--gas", action="store_true", help="print gas usage", default=False + ) + parser.add_argument( + "--entry", + type=str, + help="name of the function that will act as the entry point into execution", + ) + parser.add_argument( + "--link", + type=str, + nargs="+", + help="link the following wasm modules", + ) + parser.add_argument( + "--sectiondump", type=str, help="dumps the section provided" + ) parser.add_argument("--hexdump", type=int, help="dumps all sections") - parser.add_argument("--dbgsection", type=str, help="dumps the parsed section provided", default="") - parser.add_argument("--interactive", action='store_true', help="open in cli mode", default=False) - parser.add_argument("--rawdump", type=int, nargs=2, help="dumps all sections") + parser.add_argument( + "--dbgsection", + type=str, + help="dumps the parsed section provided", + default="", + ) + parser.add_argument( + "--interactive", + action="store_true", + help="open in cli mode", + default=False, + ) + parser.add_argument( + "--rawdump", type=int, nargs=2, help="dumps all sections" + ) self.args = parser.parse_args() if self.args.wasm is not None and self.args.wast is not None: - raise Exception("the --wast option and the --wasm option cannot\ - be set at the same time. you need to choose one.") + raise Exception( + "the --wast option and the --wasm option cannot\ + be set at the same time. you need to choose one." + ) def getParseFlags(self): - return(ParseFlags(self.args.wast, self.args.wasm, self.args.asb, self.args.dis, - self.args.o, self.args.dbg, self.args.unval, self.args.memdump, - self.args.idxspc, self.args.run, self.args.metric, self.args.gas, self.args.entry)) + return ParseFlags( + self.args.wast, + self.args.wasm, + self.args.asb, + self.args.dis, + self.args.o, + self.args.dbg, + self.args.unval, + self.args.memdump, + self.args.idxspc, + self.args.run, + self.args.metric, + self.args.gas, + self.args.entry, + ) + # reads a wasm-obj file, returns a parsedstruct that holds all the sections' # bytecode, their section type and their length @@ -89,20 +167,26 @@ def ReadWASM(file_path, endianness, is_extended_isa, dbg): parsedstruct = ParsedStruct() # read the magic cookie byte = wasm_file.read(WASM_OP_Code.uint32) - if byte != WASM_OP_Code.magic_number.to_bytes(WASM_OP_Code.uint32, byteorder=endianness, signed=False): + if byte != WASM_OP_Code.magic_number.to_bytes( + WASM_OP_Code.uint32, byteorder=endianness, signed=False + ): raise Exception("bad magic cookie") # read the version number byte = wasm_file.read(WASM_OP_Code.uint32) - if byte != WASM_OP_Code.version_number.to_bytes(WASM_OP_Code.uint32, byteorder=endianness, signed=False): + if byte != WASM_OP_Code.version_number.to_bytes( + WASM_OP_Code.uint32, byteorder=endianness, signed=False + ): raise Exception("bad version number") else: parsedstruct.version_number = byte while True: byte = wasm_file.read(1) - if byte != b'': - temp_obj_file.append(int.from_bytes(byte, byteorder='big', signed=False)) + if byte != b"": + temp_obj_file.append( + int.from_bytes(byte, byteorder="big", signed=False) + ) else: break @@ -111,18 +195,22 @@ def ReadWASM(file_path, endianness, is_extended_isa, dbg): while loop: try: # section_id, offset, dummy = Read(temp_obj_file, offset, 'varuint7') - section_id, offset, dummy = Read(temp_obj_file, offset, 'varuint32') + section_id, offset, dummy = Read( + temp_obj_file, offset, "varuint32" + ) except IndexError: break - payload_length, offset, dummy = Read(temp_obj_file, offset, 'varuint32') + payload_length, offset, dummy = Read( + temp_obj_file, offset, "varuint32" + ) if section_id == 0: is_custom_section = True - name_len, offset, dummy = Read(temp_obj_file, offset, 'varuint32') + name_len, offset, dummy = Read(temp_obj_file, offset, "varuint32") name = temp_obj_file[offset : offset + name_len] offset += name_len - ''' + """ if type(name) == list: print(''.join([chr(elem) for elem in name])) name = ''.join([chr(elem) for elem in name]) @@ -134,26 +222,39 @@ def ReadWASM(file_path, endianness, is_extended_isa, dbg): for i in range(0, reloc_entry_count): reloc_entry, offset, dummy = Read(tmp_obj, offset, 'varuint32') reloc_entries.append(reloc_entry) - ''' + """ else: is_custom_section = False name_len = 0 - name = '' + name = "" dummy = 0 - payload_data = temp_obj_file[offset:offset + payload_length - name_len - dummy] + payload_data = temp_obj_file[ + offset : offset + payload_length - name_len - dummy + ] offset += payload_length - name_len - dummy # @DEVI-the second field is for general use. it is unused right # now so we are filling it with jojo. - parsedstruct.section_list.append([section_id, 'jojo', payload_length, is_custom_section, name_len, name, payload_data]) + parsedstruct.section_list.append( + [ + section_id, + "jojo", + payload_length, + is_custom_section, + name_len, + name, + payload_data, + ] + ) # prints out the sections in the wasm object for section in parsedstruct.section_list: pass - #print(section) + # print(section) wasm_file.close() - return(parsedstruct) + return parsedstruct + # Receives a parsedstruct returned from ReadWASM, parses all the sections and # fills up a module class. the parse method, then can return the module. @@ -177,7 +278,7 @@ class ObjReader(object): # @DEVI-FIXME-for v1.0 opcodes. needs to get fixed for extended # op-codes. ideally the mosule should hold its version number so we can # check it here. - byte = format(section_byte[6][offset], '02x') + byte = format(section_byte[6][offset], "02x") offset += 1 read_bytes += 1 @@ -190,36 +291,46 @@ class ObjReader(object): # we read(at least two ticks per opcode). I could have the # br_table opcode done separately but kinda hurts the codes # uniformity. anyways. - if op_code[1] == '0e': + if op_code[1] == "0e": matched = True - temp, offset, read_bytes_temp_iter = Read(section_byte[6], offset, op_code[3][0]) - instruction += repr(temp) + ' ' + temp, offset, read_bytes_temp_iter = Read( + section_byte[6], offset, op_code[3][0] + ) + instruction += repr(temp) + " " operands.append(repr(temp)) read_bytes_temp += read_bytes_temp_iter for target_table in range(0, temp): - temp, offset, read_bytes_temp_iter = Read(section_byte[6], offset, op_code[3][1]) + temp, offset, read_bytes_temp_iter = Read( + section_byte[6], offset, op_code[3][1] + ) read_bytes_temp += read_bytes_temp_iter - instruction += repr(temp) + ' ' + instruction += repr(temp) + " " operands.append(repr(temp)) - temp, offset, read_bytes_temp_iter = Read(section_byte[6], offset, op_code[3][2]) - instruction += repr(temp) + ' ' + temp, offset, read_bytes_temp_iter = Read( + section_byte[6], offset, op_code[3][2] + ) + instruction += repr(temp) + " " operands.append(repr(temp)) read_bytes_temp += read_bytes_temp_iter elif op_code[2]: if isinstance(op_code[3], tuple): - for i in range(0, len(op_code [3])): - temp, offset, read_bytes_temp_iter = Read(section_byte[6], offset, op_code[3][i]) + for i in range(0, len(op_code[3])): + temp, offset, read_bytes_temp_iter = Read( + section_byte[6], offset, op_code[3][i] + ) read_bytes_temp += read_bytes_temp_iter - instruction += repr(temp) + ' ' + instruction += repr(temp) + " " operands.append(repr(temp)) else: - temp, offset, read_bytes_temp = Read(section_byte[6], offset, op_code[3]) + temp, offset, read_bytes_temp = Read( + section_byte[6], offset, op_code[3] + ) instruction += repr(temp) operands.append(repr(temp)) temp_wasm_ins.opcode = op_code[0] temp_wasm_ins.opcodeint = int(byte, 16) - #temp_wasm_ins.operands = instruction + # temp_wasm_ins.operands = instruction temp_wasm_ins.operands = operands break @@ -242,15 +353,19 @@ class ObjReader(object): if not section_exists: return None - fn_cn, offset, dummy = Read(code_section[6], offset, 'varuint32') + fn_cn, offset, dummy = Read(code_section[6], offset, "varuint32") function_cnt = fn_cn CS.count = function_cnt while function_cnt > 0: - function_body_length, offset, dummy = Read(code_section[6], offset, 'varuint32') + function_body_length, offset, dummy = Read( + code_section[6], offset, "varuint32" + ) temp_func_bodies.body_size = function_body_length - local_count, offset, dummy = Read(code_section[6], offset, 'varuint32') + local_count, offset, dummy = Read( + code_section[6], offset, "varuint32" + ) temp_func_bodies.local_count = local_count # local_count_size will eventually hold how many bytes we will read @@ -258,9 +373,13 @@ class ObjReader(object): local_count_size = dummy if local_count != 0: for i in range(0, local_count): - partial_local_count, offset, dummy = Read(code_section[6], offset, 'varuint32') + partial_local_count, offset, dummy = Read( + code_section[6], offset, "varuint32" + ) local_count_size += dummy - partial_local_type, offset, dummy = Read(code_section[6], offset, 'uint8') + partial_local_type, offset, dummy = Read( + code_section[6], offset, "uint8" + ) local_count_size += dummy temp_local_entry.count = partial_local_count temp_local_entry.type = partial_local_type @@ -271,14 +390,31 @@ class ObjReader(object): read_bytes_so_far = local_count_size for i in range(0, function_body_length - local_count_size): - offset, matched, read_bytes, temp_wasm_ins = self.Disassemble(code_section, offset) + offset, matched, read_bytes, temp_wasm_ins = self.Disassemble( + code_section, offset + ) temp_func_bodies.code.append(deepcopy(temp_wasm_ins)) if not matched: - print(Colors.red + 'did not match anything' + Colors.ENDC) - print(Colors.red + 'code section offset: ' + repr(offset) + Colors.ENDC) - print(Colors.red + 'read bytes: ' + repr(read_bytes) + Colors.ENDC) - print(Colors.red + 'wasm ins: ' + repr(temp_wasm_ins.opcode) + Colors.ENDC) + print(Colors.red + "did not match anything" + Colors.ENDC) + print( + Colors.red + + "code section offset: " + + repr(offset) + + Colors.ENDC + ) + print( + Colors.red + + "read bytes: " + + repr(read_bytes) + + Colors.ENDC + ) + print( + Colors.red + + "wasm ins: " + + repr(temp_wasm_ins.opcode) + + Colors.ENDC + ) for iter in temp_func_bodies.code: print(iter.opcode) @@ -295,7 +431,7 @@ class ObjReader(object): temp_func_bodies.locals = [] temp_func_bodies.code = [] function_cnt -= 1 - return(CS) + return CS # parsed the data section. returns a Data_Section class def ReadDataSection(self): @@ -313,27 +449,35 @@ class ObjReader(object): if not section_exists: return None - data_entry_count, offset, dummy = Read(data_section[6], offset, 'varuint32') + data_entry_count, offset, dummy = Read( + data_section[6], offset, "varuint32" + ) DS.count = data_entry_count while data_entry_count != 0: - linear_memory_index, offset, dummy = Read(data_section[6], offset, 'varuint32') + linear_memory_index, offset, dummy = Read( + data_section[6], offset, "varuint32" + ) temp_data_segment.index = linear_memory_index # reading in the init-expr while loop: # @DEVI-FIXME-this only works for none extended opcodes - if data_section[6][offset] == 0x0b: + if data_section[6][offset] == 0x0B: loop = False - data_char, offset, dummy = Read(data_section[6], offset, 'uint8') + data_char, offset, dummy = Read( + data_section[6], offset, "uint8" + ) init_expr.append(data_char) temp_data_segment.offset = init_expr - data_entry_length, offset, dummy = Read(data_section[6], offset, 'varuint32') + data_entry_length, offset, dummy = Read( + data_section[6], offset, "varuint32" + ) temp_data_segment.size = data_entry_length - data_itself = data_section[6][offset:offset + data_entry_length] + data_itself = data_section[6][offset : offset + data_entry_length] temp_data_segment.data = data_itself offset += data_entry_length @@ -342,7 +486,7 @@ class ObjReader(object): data_entry_count -= 1 init_expr = [] loop = True - return(DS) + return DS # parses the import section. returns an Import_Section class def ReadImportSection(self): @@ -360,11 +504,15 @@ class ObjReader(object): if not section_exists: return None - import_cnt, offset, dummy = Read(import_section[6], offset, 'varuint32') + import_cnt, offset, dummy = Read( + import_section[6], offset, "varuint32" + ) IS.count = import_cnt while import_cnt != 0: - module_length, offset, dummy = Read(import_section[6], offset, 'varuint32') + module_length, offset, dummy = Read( + import_section[6], offset, "varuint32" + ) temp_import_entry.module_len = module_length for i in range(0, module_length): @@ -372,44 +520,66 @@ class ObjReader(object): temp_import_entry.module_str = module_name offset += module_length - field_length, offset, dummy = Read(import_section[6], offset, 'varuint32') + field_length, offset, dummy = Read( + import_section[6], offset, "varuint32" + ) temp_import_entry.field_len = field_length for i in range(0, field_length): field_name.append(import_section[6][offset + i]) temp_import_entry.field_str = field_name offset += field_length - kind, offset, dummy = Read(import_section[6], offset, 'uint8') + kind, offset, dummy = Read(import_section[6], offset, "uint8") temp_import_entry.kind = kind # function type if kind == 0: - import_type, offset, dummy = Read(import_section[6], offset, 'varuint32') + import_type, offset, dummy = Read( + import_section[6], offset, "varuint32" + ) temp_import_entry.type = import_type # table type elif kind == 1: table_type = Table_Type() - table_type.elemet_type, offset, dummy = Read(import_section[6], offset, 'varint7') + table_type.elemet_type, offset, dummy = Read( + import_section[6], offset, "varint7" + ) rsz_limits = Resizable_Limits() - rsz_limits.flags, offset, dummy = Read(import_section[6], offset, 'varuint1') - rsz_limits.initial, offset, dummy = Read(import_section[6], offset, 'varuint32') + rsz_limits.flags, offset, dummy = Read( + import_section[6], offset, "varuint1" + ) + rsz_limits.initial, offset, dummy = Read( + import_section[6], offset, "varuint32" + ) if rsz_limits.flags: - rsz_limits.maximum, offset, dummy = Read(import_section[6], offset, 'varuint32') + rsz_limits.maximum, offset, dummy = Read( + import_section[6], offset, "varuint32" + ) table_type.limit = rsz_limits temp_import_entry.type = table_type elif kind == 2: memory_type = Memory_Type() rsz_limits = Resizable_Limits() - rsz_limits.flags, offset, dummy = Read(import_section[6], offset, 'varuint1') - rsz_limits.initial, offset, dummy = Read(import_section[6], offset, 'varuint32') + rsz_limits.flags, offset, dummy = Read( + import_section[6], offset, "varuint1" + ) + rsz_limits.initial, offset, dummy = Read( + import_section[6], offset, "varuint32" + ) if rsz_limits.flags: - rsz_limits.maximum, offset, dummy = Read(import_section[6], offset, 'varuint32') + rsz_limits.maximum, offset, dummy = Read( + import_section[6], offset, "varuint32" + ) memory_type.limits = rsz_limits temp_import_entry.type = memory_type elif kind == 3: global_type = Global_Type() - global_type.content_type, offset, dummy = Read(import_section[6], offset, 'uint8') - global_type.mutability, offset, dummy = Read(import_section[6], offset, 'varuint1') + global_type.content_type, offset, dummy = Read( + import_section[6], offset, "uint8" + ) + global_type.mutability, offset, dummy = Read( + import_section[6], offset, "varuint1" + ) temp_import_entry.type = global_type IS.import_entry.append(deepcopy(temp_import_entry)) @@ -417,7 +587,7 @@ class ObjReader(object): import_cnt -= 1 module_name = [] field_name = [] - return(IS) + return IS # parses the export section, returns an Export_Section class def ReadExportSection(self): @@ -434,11 +604,15 @@ class ObjReader(object): if not section_exists: return None - export_entry_cnt, offset, dummy = Read(export_section[6], offset, 'varuint32') + export_entry_cnt, offset, dummy = Read( + export_section[6], offset, "varuint32" + ) ES.count = export_entry_cnt while export_entry_cnt != 0: - field_length, offset, dummy = Read(export_section[6], offset, 'varuint32') + field_length, offset, dummy = Read( + export_section[6], offset, "varuint32" + ) temp_export_entry.field_len = field_length for i in range(0, field_length): @@ -446,17 +620,17 @@ class ObjReader(object): temp_export_entry.fiels_str = field_name offset += field_length - kind, offset, dummy = Read(export_section[6], offset, 'uint8') + kind, offset, dummy = Read(export_section[6], offset, "uint8") temp_export_entry.kind = kind - index, offset, dummy = Read(export_section[6], offset, 'varuint32') + index, offset, dummy = Read(export_section[6], offset, "varuint32") temp_export_entry.index = index ES.export_entries.append(deepcopy(temp_export_entry)) export_entry_cnt -= 1 field_name = [] - return(ES) + return ES # parses the type section, returns a Type_Section class def ReadTypeSection(self): @@ -474,14 +648,18 @@ class ObjReader(object): if not section_exists: return None - type_entry_count, offset, dummy = Read(type_section[6], offset, 'varuint32') + type_entry_count, offset, dummy = Read( + type_section[6], offset, "varuint32" + ) TS.count = type_entry_count while type_entry_count != 0: - form, offset, dummy = Read(type_section[6], offset, 'varint7') + form, offset, dummy = Read(type_section[6], offset, "varint7") temp_func_type.form = form - param_count, offset, dummy = Read(type_section[6], offset, 'varuint32') + param_count, offset, dummy = Read( + type_section[6], offset, "varuint32" + ) temp_func_type.param_cnt = param_count for i in range(0, param_count): @@ -490,7 +668,9 @@ class ObjReader(object): offset += param_count # @DEVI-FIXME- only works for MVP || single return value - return_count, offset, dummy = Read(type_section[6], offset, 'varuint1') + return_count, offset, dummy = Read( + type_section[6], offset, "varuint1" + ) temp_func_type.return_cnt = return_count for i in range(0, return_count): @@ -503,7 +683,7 @@ class ObjReader(object): type_entry_count -= 1 param_list = [] return_list = [] - return(TS) + return TS # parses the function section, returns a Function_section class def ReadFunctionSection(self): @@ -519,15 +699,19 @@ class ObjReader(object): if not section_exists: return None - function_entry_count, offset, dummy = Read(function_section[6], offset, 'varuint32') + function_entry_count, offset, dummy = Read( + function_section[6], offset, "varuint32" + ) FS.count = function_entry_count for i in range(0, function_entry_count): - index, offset, dummy = Read(function_section[6], offset, 'varuint32') + index, offset, dummy = Read( + function_section[6], offset, "varuint32" + ) index_to_type.append(index) FS.type_section_index = index_to_type offset += function_entry_count - return(FS) + return FS # parses the element secction, returns an Element_Section class def ReadElementSection(self): @@ -547,26 +731,34 @@ class ObjReader(object): if not section_exists: return None - element_entry_count, offset, dummy = Read(element_section[6], offset, 'varuint32') + element_entry_count, offset, dummy = Read( + element_section[6], offset, "varuint32" + ) ES.count = element_entry_count while element_entry_count != 0: - table_index, offset, dummy = Read(element_section[6], offset, 'varuint32') + table_index, offset, dummy = Read( + element_section[6], offset, "varuint32" + ) temp_elem_segment.index = table_index # @DEVI-FIXME-only works for non-extneded opcodes while loop: - if element_section[6][offset] == 0x0b: + if element_section[6][offset] == 0x0B: loop = False init_expr.append(element_section[6][offset]) offset += 1 temp_elem_segment.offset = init_expr - num_elements, offset, dummy = Read(element_section[6], offset, 'varuint32') + num_elements, offset, dummy = Read( + element_section[6], offset, "varuint32" + ) temp_elem_segment.num_elem = num_elements for i in range(0, num_elements): - index, offset, dummy = Read(element_section[6], offset, 'varuint32') + index, offset, dummy = Read( + element_section[6], offset, "varuint32" + ) function_indices.append(index) temp_elem_segment.elems = function_indices offset += num_elements @@ -577,7 +769,7 @@ class ObjReader(object): init_expr = [] function_indices = [] element_entry_count -= 1 - return(ES) + return ES # parses the memory section, returns a Memory_Section class def ReadMemorySection(self): @@ -594,23 +786,29 @@ class ObjReader(object): if not section_exists: return None - num_linear_mems, offset, dummy = Read(memory_section[6], offset, 'varuint32') + num_linear_mems, offset, dummy = Read( + memory_section[6], offset, "varuint32" + ) MS.count = num_linear_mems while num_linear_mems != 0: - flag, offset, dummy = Read(memory_section[6], offset, 'varuint1') + flag, offset, dummy = Read(memory_section[6], offset, "varuint1") temp_rsz_limits.flags = flag - initial,offset, dummy = Read(memory_section[6], offset, 'varuint32') + initial, offset, dummy = Read( + memory_section[6], offset, "varuint32" + ) temp_rsz_limits.initial = initial if flag: - maximum, offset, dummy = Read(memory_section[6], offset, 'varuint32') + maximum, offset, dummy = Read( + memory_section[6], offset, "varuint32" + ) temp_rsz_limits.maximum = maximum MS.memory_types.append(deepcopy(temp_rsz_limits)) num_linear_mems -= 1 - return(MS) + return MS # parses the table section, returns a Table_Section class def ReadTableSection(self): @@ -628,28 +826,36 @@ class ObjReader(object): if not section_exists: return None - table_count, offset, dummy = Read(table_section[6], offset, 'varuint32') + table_count, offset, dummy = Read( + table_section[6], offset, "varuint32" + ) TS.count = table_count while table_count != 0: - element_type, offset, dummy = Read(table_section[6], offset, 'varint7') + element_type, offset, dummy = Read( + table_section[6], offset, "varint7" + ) temp_table_type.element_type = element_type - flag, offset, dummy = Read(table_section[6], offset, 'varuint1') + flag, offset, dummy = Read(table_section[6], offset, "varuint1") temp_rsz_limits.flags = flag - initial, offset, dummy = Read(table_section[6], offset, 'varuint32') + initial, offset, dummy = Read( + table_section[6], offset, "varuint32" + ) temp_rsz_limits.initial = initial if flag: - maximum, offset, dummy = Read(table_section[6], offset, 'varuint32') + maximum, offset, dummy = Read( + table_section[6], offset, "varuint32" + ) temp_rsz_limits.maximum = maximum temp_table_type.limit = temp_rsz_limits TS.table_types.append(deepcopy(temp_table_type)) table_count -= 1 - return(TS) + return TS # parses the global section, returns a Global_Section class def ReadGlobalSection(self): @@ -669,19 +875,23 @@ class ObjReader(object): if not section_exists: return None - count, offset, dummy = Read(global_section[6], offset, 'varuint32') + count, offset, dummy = Read(global_section[6], offset, "varuint32") GS.count = count while count != 0: - content_type, offset, dummy = Read(global_section[6], offset, 'uint8') + content_type, offset, dummy = Read( + global_section[6], offset, "uint8" + ) temp_gl_type.content_type = content_type - mutability, offset, dummy = Read(global_section[6], offset, 'varuint1') + mutability, offset, dummy = Read( + global_section[6], offset, "varuint1" + ) temp_gl_type.mutability = mutability # @DEVI-FIXME-only works for non-extended opcodes while loop: - if global_section[6][offset] == 0x0b: + if global_section[6][offset] == 0x0B: loop = False init_expr.append(global_section[6][offset]) offset += 1 @@ -690,11 +900,10 @@ class ObjReader(object): temp_gl_var.global_type = temp_gl_type GS.global_variables.append(deepcopy(temp_gl_var)) - count -= 1 loop = True init_expr = [] - return(GS) + return GS # parses the start section, returns a Start_Section def ReadStartSection(self): @@ -709,31 +918,33 @@ class ObjReader(object): if not section_exists: return None - function_index, offset, dummy = Read(start_section[6], offset, 'varuint32') + function_index, offset, dummy = Read( + start_section[6], offset, "varuint32" + ) SS.function_section_index = function_index - return(SS) + return SS def ReadRelocationSection(self): 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) + return RS def ReadNameSection(self): 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 - return(NS) + return NS def ReadLinkingSection(self): pass @@ -743,16 +954,23 @@ class ObjReader(object): # unused-returns the cursor location in the object file def getCursorLocation(self): - return(self.wasm_file.tell()) + return self.wasm_file.tell() # a convinience method-builds a module class and returns it def parse(self): - return(Module(self.ReadTypeSection(), self.ReadImportSection(), - self.ReadFunctionSection(), self.ReadTableSection(), - self.ReadMemorySection(), self.ReadGlobalSection(), - self.ReadExportSection(), self.ReadStartSection(), - self.ReadElementSection(), self.ReadCodeSection(), - self.ReadDataSection())) + return Module( + self.ReadTypeSection(), + self.ReadImportSection(), + self.ReadFunctionSection(), + self.ReadTableSection(), + self.ReadMemorySection(), + self.ReadGlobalSection(), + self.ReadExportSection(), + self.ReadStartSection(), + self.ReadElementSection(), + self.ReadCodeSection(), + self.ReadDataSection(), + ) # WIP-basically how the assembler is constructed @@ -783,12 +1001,12 @@ class ParserV1(object): header_stack = headerparser.ParseBodyV3(True) wasm_codeemitter = WASM_CodeEmitter(expr_stack) - #wasm_codeemitter.Obj_Header_32() - #wasm_codeemitter.Dump_Obj_STDOUT() + # wasm_codeemitter.Obj_Header_32() + # wasm_codeemitter.Dump_Obj_STDOUT() - #wasm_codeemitter.SetNewStack(header_stack) - #wasm_codeemitter.EmitTypeHeader() - #wasm_codeemitter.PrintTypeHeaderObj() + # wasm_codeemitter.SetNewStack(header_stack) + # wasm_codeemitter.EmitTypeHeader() + # wasm_codeemitter.PrintTypeHeaderObj() # our interpreter class @@ -803,7 +1021,7 @@ class PythonInterpreter(object): # returns the list of modules that we have parsed so far def getmodules(self): - return(self.modules) + return self.modules def appendsection(self, section): self.sections.append(section) @@ -814,171 +1032,431 @@ class PythonInterpreter(object): # convinience method.calls the ObjReader to parse a wasm obj file. # returns a module class. def parse(self, file_path): - section = ReadWASM(file_path, 'little', False, True) + section = ReadWASM(file_path, "little", False, True) self.appendsection(section) parser = ObjReader(section) - return(parser.parse()) + return parser.parse() # dumps the object sections' info to stdout. pretty print. def dump_sections(self, module, dbgsection): all = False if dbgsection == "": all = True - print(Colors.blue + Colors.BOLD + - 'BEGENNING OF MODULE' + Colors.ENDC) + print(Colors.blue + Colors.BOLD + "BEGENNING OF MODULE" + Colors.ENDC) # type_section if module.type_section is not None and (dbgsection == "type" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Type Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.type_section.count)) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Type Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.type_section.count)) for iter in module.type_section.func_types: - print(Colors.cyan + 'form: ' + repr(iter.form) + Colors.ENDC) - print(Colors.green + 'param count: ' + repr(iter.param_cnt) + Colors.ENDC) - print(Colors.red + 'param types: ' + repr(iter.param_types) + Colors.ENDC) - print(Colors.purple + 'return count: ' + repr(iter.return_cnt) + Colors.ENDC) - print(Colors.yellow + 'return type: ' + repr(iter.return_type) + Colors.ENDC) + print(Colors.cyan + "form: " + repr(iter.form) + Colors.ENDC) + print( + Colors.green + + "param count: " + + repr(iter.param_cnt) + + Colors.ENDC + ) + print( + Colors.red + + "param types: " + + repr(iter.param_types) + + Colors.ENDC + ) + print( + Colors.purple + + "return count: " + + repr(iter.return_cnt) + + Colors.ENDC + ) + print( + Colors.yellow + + "return type: " + + repr(iter.return_type) + + Colors.ENDC + ) # import_section - if module.import_section is not None and (dbgsection == "import" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Import Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.import_section.count)) + if module.import_section is not None and ( + dbgsection == "import" or all + ): + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Import Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.import_section.count)) for iter in module.import_section.import_entry: - print(Colors.cyan + 'module length: ' + repr(iter.module_len) + Colors.ENDC) - print(Colors.green + 'module str: ' + repr(iter.module_str) + Colors.ENDC) - print(Colors.red + 'field length: ' + repr(iter.field_len) + Colors.ENDC) - print(Colors.purple + 'field str: ' + repr(iter.field_str) + Colors.ENDC) - print(Colors.purple + 'field str: ' + ''.join([chr(elem) for elem in iter.field_str]) + Colors.ENDC) - print(Colors.yellow + 'kind: ' + repr(iter.kind) + Colors.ENDC) - print(Colors.grey + 'type: ' + repr(iter.type) + Colors.ENDC) - if (type(iter.type) == Table_Type): - print('element_type: ' + repr(iter.type.element_type)) - print('limit: ' + repr(iter.type.limit)) - print('flags: ' + repr(iter.type.limit.flags)) - print('initial: ' + repr(iter.type.limit.initial)) - print('maximum: ' + repr(iter.type.limit.maximum)) - if (type(iter.type) == Global_Type): - print('value_type: ' + repr(iter.type.content_type)) - print('mutability: ' + repr(iter.type.mutability)) - if (type(iter.type) == Memory_Type): - print('flags: ' + repr(iter.type.limits.flags)) - print('flags: ' + repr(iter.type.limits.initial)) - print('flags: ' + repr(iter.type.limits.maximum)) + print( + Colors.cyan + + "module length: " + + repr(iter.module_len) + + Colors.ENDC + ) + print( + Colors.green + + "module str: " + + repr(iter.module_str) + + Colors.ENDC + ) + print( + Colors.red + + "field length: " + + repr(iter.field_len) + + Colors.ENDC + ) + print( + Colors.purple + + "field str: " + + repr(iter.field_str) + + Colors.ENDC + ) + print( + Colors.purple + + "field str: " + + "".join([chr(elem) for elem in iter.field_str]) + + Colors.ENDC + ) + print(Colors.yellow + "kind: " + repr(iter.kind) + Colors.ENDC) + print(Colors.grey + "type: " + repr(iter.type) + Colors.ENDC) + if type(iter.type) == Table_Type: + print("element_type: " + repr(iter.type.element_type)) + print("limit: " + repr(iter.type.limit)) + print("flags: " + repr(iter.type.limit.flags)) + print("initial: " + repr(iter.type.limit.initial)) + print("maximum: " + repr(iter.type.limit.maximum)) + if type(iter.type) == Global_Type: + print("value_type: " + repr(iter.type.content_type)) + print("mutability: " + repr(iter.type.mutability)) + if type(iter.type) == Memory_Type: + print("flags: " + repr(iter.type.limits.flags)) + print("flags: " + repr(iter.type.limits.initial)) + print("flags: " + repr(iter.type.limits.maximum)) # function_section - if module.function_section is not None and (dbgsection == "function" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Function Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.function_section.count)) + if module.function_section is not None and ( + dbgsection == "function" or all + ): + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print( + Colors.blue + Colors.BOLD + "Function Section:" + Colors.ENDC + ) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.function_section.count)) for iter in module.function_section.type_section_index: - print(Colors.cyan + 'type section index: ' + repr(iter) + Colors.ENDC) + print( + Colors.cyan + + "type section index: " + + repr(iter) + + Colors.ENDC + ) # table_section if module.table_section is not None and (dbgsection == "table" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Table Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.table_section.count)) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Table Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.table_section.count)) for iter in module.table_section.table_types: - print(Colors.cyan + 'element type: ' + repr(iter.element_type) + Colors.ENDC) - print(Colors.green + 'Resizable_Limits:flags: ' + repr(iter.limit.flags) + Colors.ENDC) - print(Colors.red + 'Resizable_Limits:initial: ' + repr(iter.limit.initial) + Colors.ENDC) - print(Colors.purple + 'Resizable_Limits:maximum: ' + repr(iter.limit.maximum) + Colors.ENDC) + print( + Colors.cyan + + "element type: " + + repr(iter.element_type) + + Colors.ENDC + ) + print( + Colors.green + + "Resizable_Limits:flags: " + + repr(iter.limit.flags) + + Colors.ENDC + ) + print( + Colors.red + + "Resizable_Limits:initial: " + + repr(iter.limit.initial) + + Colors.ENDC + ) + print( + Colors.purple + + "Resizable_Limits:maximum: " + + repr(iter.limit.maximum) + + Colors.ENDC + ) # memory_section - if module.memory_section is not None and (dbgsection == "memory" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Memory Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.memory_section.count)) + if module.memory_section is not None and ( + dbgsection == "memory" or all + ): + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Memory Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.memory_section.count)) for iter in module.memory_section.memory_types: - print(Colors.green + 'Resizable_Limits:flags: ' + repr(iter.flags) + Colors.ENDC) - print(Colors.red + 'Resizable_Limits:initial: ' + repr(iter.initial) + Colors.ENDC) - print(Colors.purple + 'Resizable_Limits:maximum: ' + repr(iter.maximum) + Colors.ENDC) + print( + Colors.green + + "Resizable_Limits:flags: " + + repr(iter.flags) + + Colors.ENDC + ) + print( + Colors.red + + "Resizable_Limits:initial: " + + repr(iter.initial) + + Colors.ENDC + ) + print( + Colors.purple + + "Resizable_Limits:maximum: " + + repr(iter.maximum) + + Colors.ENDC + ) # global_section - if module.global_section is not None and (dbgsection == "global" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Global Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.global_section.count)) + if module.global_section is not None and ( + dbgsection == "global" or all + ): + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Global Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.global_section.count)) for iter in module.global_section.global_variables: - print(Colors.green + 'global type: ' + repr(iter.global_type) + Colors.ENDC) - print(Colors.red + 'init expr: ' + repr(iter.init_expr) + Colors.ENDC) + print( + Colors.green + + "global type: " + + repr(iter.global_type) + + Colors.ENDC + ) + print( + Colors.red + + "init expr: " + + repr(iter.init_expr) + + Colors.ENDC + ) # export_section - if module.export_section is not None and (dbgsection == "export" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Export Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.export_section.count)) + if module.export_section is not None and ( + dbgsection == "export" or all + ): + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Export Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.export_section.count)) for iter in module.export_section.export_entries: - print(Colors.green + 'field length: ' + repr(iter.field_len) + Colors.ENDC) - print(Colors.red + 'field str: ' + repr(iter.field_str) + Colors.ENDC) - print(Colors.purple + 'kind: ' + repr(iter.kind) + Colors.ENDC) - print(Colors.cyan + 'index: ' + repr(iter.index) + Colors.ENDC) + print( + Colors.green + + "field length: " + + repr(iter.field_len) + + Colors.ENDC + ) + print( + Colors.red + + "field str: " + + repr(iter.field_str) + + Colors.ENDC + ) + print(Colors.purple + "kind: " + repr(iter.kind) + Colors.ENDC) + print(Colors.cyan + "index: " + repr(iter.index) + Colors.ENDC) # start_section if module.start_section is not None and (dbgsection == "start" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Start Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('start function index: ' + repr(module.start_section.function_section_index)) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Start Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print( + "start function index: " + + repr(module.start_section.function_section_index) + ) # element_section - if module.element_section is not None and (dbgsection == "element" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Element Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.element_section.count)) + if module.element_section is not None and ( + dbgsection == "element" or all + ): + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Element Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.element_section.count)) for iter in module.element_section.elem_segments: - print(Colors.green + 'index: ' + repr(iter.index) + Colors.ENDC) - print(Colors.red + 'offset: ' + repr(iter.offset) + Colors.ENDC) - print(Colors.purple + 'num_elem: ' + repr(iter.num_elem) + Colors.ENDC) - print(Colors.cyan + 'elemes:' + repr(iter.elems) + Colors.ENDC) + print( + Colors.green + "index: " + repr(iter.index) + Colors.ENDC + ) + print( + Colors.red + "offset: " + repr(iter.offset) + Colors.ENDC + ) + print( + Colors.purple + + "num_elem: " + + repr(iter.num_elem) + + Colors.ENDC + ) + print(Colors.cyan + "elemes:" + repr(iter.elems) + Colors.ENDC) # code_section if module.code_section is not None and (dbgsection == "code" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Code Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.code_section.count)) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Code Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.code_section.count)) for iter in module.code_section.func_bodies: - print(Colors.green + 'body size: ' + repr(iter.body_size) + Colors.ENDC) - print(Colors.red + 'local enrty count: ' + repr(iter.local_count) + Colors.ENDC) + print( + Colors.green + + "body size: " + + repr(iter.body_size) + + Colors.ENDC + ) + print( + Colors.red + + "local enrty count: " + + repr(iter.local_count) + + Colors.ENDC + ) for iterer in iter.locals: - print(Colors.blue + 'local count: ' + repr(iterer.count) + Colors.ENDC) - print(Colors.blue + 'local type: ' + repr(iterer.type) + Colors.ENDC) + print( + Colors.blue + + "local count: " + + repr(iterer.count) + + Colors.ENDC + ) + print( + Colors.blue + + "local type: " + + repr(iterer.type) + + Colors.ENDC + ) for iterer in iter.code: - instruction = iterer.opcode + ' ' + repr(iterer.operands) + instruction = iterer.opcode + " " + repr(iterer.operands) print(Colors.yellow + instruction + Colors.ENDC, end="") print("\t", end="") - print(Colors.cyan + 'opcode: ' + repr(iterer.opcode) + Colors.ENDC, end="") + print( + Colors.cyan + + "opcode: " + + repr(iterer.opcode) + + Colors.ENDC, + end="", + ) print("\t", end="") - print(Colors.grey + 'immediate: ' + repr(iterer.operands) + Colors.ENDC, end="") + print( + Colors.grey + + "immediate: " + + repr(iterer.operands) + + Colors.ENDC, + end="", + ) print("\t", end="") - print(Colors.UNDERLINE + "num of operands: " + repr(len(iterer.operands)) + Colors.ENDC) + print( + Colors.UNDERLINE + + "num of operands: " + + repr(len(iterer.operands)) + + Colors.ENDC + ) # data_section if module.data_section is not None and (dbgsection == "data" or all): - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print(Colors.blue + Colors.BOLD + 'Data Section:' + Colors.ENDC) - print(Colors.blue + '------------------------------------------------------' + Colors.ENDC) - print('count: ' + repr(module.data_section.count)) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print(Colors.blue + Colors.BOLD + "Data Section:" + Colors.ENDC) + print( + Colors.blue + + "------------------------------------------------------" + + Colors.ENDC + ) + print("count: " + repr(module.data_section.count)) for iter in module.data_section.data_segments: - print(Colors.green + 'index: ' + repr(iter.index) + Colors.ENDC) - print(Colors.red + 'offset: ' + repr(iter.offset) + Colors.ENDC) - print(Colors.purple + 'size: ' + repr(iter.size) + Colors.ENDC) - print(Colors.cyan + 'data:' + repr(iter.data) + Colors.ENDC) + print( + Colors.green + "index: " + repr(iter.index) + Colors.ENDC + ) + print( + Colors.red + "offset: " + repr(iter.offset) + Colors.ENDC + ) + print(Colors.purple + "size: " + repr(iter.size) + Colors.ENDC) + print(Colors.cyan + "data:" + repr(iter.data) + Colors.ENDC) # palceholder for the validation tests def runValidations(self): modulevalidation = ModuleValidation(self.modules[0]) - return(modulevalidation.ValidateAll()) + return modulevalidation.ValidateAll() + def premain(argparser): interpreter = PythonInterpreter() @@ -990,9 +1468,13 @@ def premain(argparser): if argparser.args.dbg or argparser.args.dbgsection: interpreter.dump_sections(module, argparser.args.dbgsection) if interpreter.runValidations(): - print(Colors.red + "validations are not implemented yet" + Colors.ENDC) + print( + Colors.red + + "validations are not implemented yet" + + Colors.ENDC + ) else: - print(Colors.red + 'failed validation tests' + Colors.ENDC) + print(Colors.red + "failed validation tests" + Colors.ENDC) vm = VM(interpreter.getmodules()) vm.setFlags(argparser.getParseFlags()) ms = vm.getState() @@ -1011,11 +1493,15 @@ def premain(argparser): shell = code.InteractiveConsole(variables) shell.interact(banner="WASM python REPL") if argparser.args.hexdump: - dumpprettysections(interpreter.getsections(), argparser.args.hexdump, "") + dumpprettysections( + interpreter.getsections(), argparser.args.hexdump, "" + ) if argparser.args.sectiondump is not None: - dumpprettysections(interpreter.getsections(), 24, argparser.args.sectiondump) + dumpprettysections( + interpreter.getsections(), 24, argparser.args.sectiondump + ) if argparser.args.wast: - #print(argparser.getWASTPath()) + # print(argparser.getWASTPath()) parser = ParserV1(argparser.getWASTPath()) parser.run() # WIP-the assmebler @@ -1027,6 +1513,7 @@ def premain(argparser): print("not implemented yet") sys.exit(1) + def main(): argparser = Argparser() if argparser.args.dbg: @@ -1040,5 +1527,6 @@ def main(): else: premain(argparser) + if __name__ == "__main__": main() diff --git a/section_structs.py b/section_structs.py index 219f7ea..f5e2998 100644 --- a/section_structs.py +++ b/section_structs.py @@ -9,24 +9,28 @@ class WASM_SECTION(object): self.name = str() self.payload_data = bytes() -class WASM_SEGMENT_INFO(): + +class WASM_SEGMENT_INFO: def __int__(self): self.count = int() self.segments = [] -class WASM_SEGMENT_INFO_SEGMENT(): + +class WASM_SEGMENT_INFO_SEGMENT: def __init__(self): self.name_len = int() self.name_data = str() self.alignment = int() self.flags = int() -class WASM_COMDAT_INFO_SUB(): + +class WASM_COMDAT_INFO_SUB: def __init__(self): self.count = int() self.comdats = [] -class COMDAT(): + +class COMDAT: def __init(self): self.name_len = int() self.name_str = str() @@ -34,52 +38,60 @@ class COMDAT(): self.count = int() self.comdat_syms = [] -class COMDAT_SYM(): + +class COMDAT_SYM: def __init(self): self.kind = int() self.index = int() -class WASM_COMDAT_KIND(): + +class WASM_COMDAT_KIND: WASM_COMDAT_DATA = 0 WASM_COMDAT_FUNCTION = 1 WASM_COMDAT_GLOBAL = 2 -class WASM_INIT_FUNCS(): + +class WASM_INIT_FUNCS: def __init__(self): self.count = int() self.functions = [] -class WASM_SYMBOL_TABLE(): + +class WASM_SYMBOL_TABLE: def __init__(self): self.count = int() self.syminfo = [] -class SYM_INFO(): + +class SYM_INFO: def __init__(self): self.kind = int() self.flags = int() -class SYM_INFO_FLAGS(): + +class SYM_INFO_FLAGS: WASM_SYM_BINDING_WEAK = 1 WASM_SYM_BINDING_LOCAL = 2 WASM_SYM_VISIBILITY_HIDDEN = 4 WASM_SYM_UNDEFINED = 16 -class Rel_Entry(): +class Rel_Entry: def __int__(self): self.type = int() self.offset = int() self.index = int() -class RelA_Entry(): + +class RelA_Entry: def __int__(self): self.type = int() self.offset = int() self.index = int() self.addend = int() -class Relocation_Section(): + +class Relocation_Section: def __int__(self): self.section_id = int() self.name_length = int() @@ -87,7 +99,8 @@ class Relocation_Section(): self.count = int() self.entries = int() -class Func_Type(): + +class Func_Type: def __init__(self): self.form = int() self.param_cnt = int() @@ -95,43 +108,51 @@ class Func_Type(): self.return_cnt = int() self.return_type = [] -class Global_Type(): + +class Global_Type: def __init__(self): self.content_type = int() self.mutability = int() -class Resizable_Limits(): + +class Resizable_Limits: def __init__(self): self.flags = int() self.initial = int() self.maximum = int() -class Table_Type(): + +class Table_Type: def __init__(self): self.element_type = int() self.limit = Resizable_Limits() -class External_Kind(): + +class External_Kind: def __init__(self): self.Function = 0 self.Table = 1 self.Memory = 2 self.Global = 3 -class Memory_Type(): + +class Memory_Type: def __init__(self): self.limits = [Resizable_Limits()] + # @DEVI-FIXME- -class Init_Expr(): +class Init_Expr: pass -class Type_Section(): + +class Type_Section: def __init__(self): self.count = [] self.func_types = [] -class Import_Entry(): + +class Import_Entry: def __init__(self): self.module_len = int() self.module_str = [] @@ -140,80 +161,94 @@ class Import_Entry(): self.kind = int() self.type = int() -class Import_Section(): + +class Import_Section: def __init__(self): self.count = [] self.import_entry = [] -class Function_Section(): + +class Function_Section: def __init__(self): self.count = [] self.type_section_index = [int()] -class Table_Section(): + +class Table_Section: def __init__(self): self.count = [] self.table_types = [] -class Memory_Section(): + +class Memory_Section: def __init__(self): self.count = [] # Resizable_Limits self.memory_types = [] -class Global_Variable(): + +class Global_Variable: def __init__(self): self.global_type = Global_Type() self.init_expr = [] -class Global_Section(): + +class Global_Section: def __init__(self): self.count = [] # Global_Variable self.global_variables = [] -class Export_Entry(): + +class Export_Entry: def __init__(self): self.field_len = int() self.field_str = [] self.kind = int() self.index = int() -class Export_Section(): + +class Export_Section: def __init__(self): self.count = [] # Export_Entry self.export_entries = [] -class Start_Section(): + +class Start_Section: def __init__(self): self.function_section_index = [] -class Elem_Segment(): + +class Elem_Segment: def __init__(self): self.index = int() self.offset = [] self.num_elem = int() self.elems = [] -class Element_Section(): + +class Element_Section: def __init__(self): self.count = [] # Elem_Segment self.elem_segments = [] -class Local_Entry(): + +class Local_Entry: def __init__(self): self.count = int() self.type = int() -class WASM_Ins(): + +class WASM_Ins: def __init__(self): self.opcode = str() self.opcodeint = int() self.operands = [] -class Func_Body(): + +class Func_Body: def __init__(self): self.body_size = int() self.local_count = int() @@ -223,63 +258,84 @@ class Func_Body(): self.code = [] self.end = int() -class Code_Section(): + +class Code_Section: def __init__(self): self.count = [] # Func_Body self.func_bodies = [] -class Data_Segment(): + +class Data_Segment: def __init__(self): self.index = int() self.offset = [] self.size = int() self.data = [] -class Data_Section(): + +class Data_Section: def __init__(self): self.count = [] # Data_Segment self.data_segments = [] -class Name_Type(): + +class Name_Type: Module = 0 Function = 1 Local = 2 + class Name_Section_Entry(object): def __init__(self, name_type, name_payload_len, name_payload_data): self.name_type = name_type self.name_payload_len = name_payload_len self.name_payload_data = name_payload_data + class Name_Section(object): def __init__(self, name_section_entry): self.name_section_entry = [] self.name_section_entry = name_section_entry + class Module_Name(object): def __init__(self, name_len, name_str): self.name_len = name_len self.name_str = name_str + class Naming(object): def __init__(self, index, name_len, name_str): self.index = index self.name_len = name_len self.name_str = name_str + class Name_Map(object): def __init__(self, count, naming): self.count = count self.naming = [] self.naming = naming + # the module class -class Module(): - def __init__(self, type_section, import_section, function_section, - table_section, memory_section, global_section, export_section, - start_section, element_section, code_section, data_section): +class Module: + def __init__( + self, + type_section, + import_section, + function_section, + table_section, + memory_section, + global_section, + export_section, + start_section, + element_section, + code_section, + data_section, + ): self.type_section = type_section self.import_section = import_section self.function_section = function_section @@ -292,10 +348,11 @@ class Module(): self.code_section = code_section self.data_section = data_section -''' + +""" class RT_INS_CELL(object): def __init__(self): label : str mnemonic : str ops : list -''' +""" @@ -2,9 +2,24 @@ from opcodes import * import numpy as np import struct as stc + class ParseFlags: - def __init__(self, wast_path, wasm_path, as_path, disa_path, out_path, dbg, unval, memdump - , idxspc, run, metric, gas, entry): + def __init__( + self, + wast_path, + wasm_path, + as_path, + disa_path, + out_path, + dbg, + unval, + memdump, + idxspc, + run, + metric, + gas, + entry, + ): self.wast_path = wast_path self.wasm_path = wasm_path self.as_path = as_path @@ -19,44 +34,48 @@ class ParseFlags: self.gas = gas self.entry = entry + # pretty print class Colors: - purple = '\033[95m' - blue = '\033[94m' - green = '\033[92m' - yellow = '\033[93m' - red = '\033[91m' - grey = '\033[1;37m' - darkgrey = '\033[1;30m' - cyan = '\033[1;36m' - ENDC = '\033[0m' - BOLD = '\033[1m' - UNDERLINE = '\033[4m' + purple = "\033[95m" + blue = "\033[94m" + green = "\033[92m" + yellow = "\033[93m" + red = "\033[91m" + grey = "\033[1;37m" + darkgrey = "\033[1;30m" + cyan = "\033[1;36m" + ENDC = "\033[0m" + BOLD = "\033[1m" + UNDERLINE = "\033[4m" + def LEB128UnsignedDecode(bytelist): result = 0 shift = 0 for byte in bytelist: - result |= (byte & 0x7f) << shift + result |= (byte & 0x7F) << shift if (byte & 0x80) == 0: break shift += 7 - return(result) + return result + def LEB128SignedDecode(bytelist): result = 0 shift = 0 for byte in bytelist: - result |= (byte & 0x7f) << shift + result |= (byte & 0x7F) << shift last_byte = byte shift += 7 if (byte & 0x80) == 0: break if last_byte & 0x40: - result |= - (1 << shift) + result |= -(1 << shift) + + return result - return(result) def LEB128UnsignedEncode(int_val): if int_val < 0: @@ -66,54 +85,62 @@ def LEB128UnsignedEncode(int_val): byte_array = bytearray() while int_val: - byte = int_val & 0x7f + byte = int_val & 0x7F byte_array.append(byte | 0x80) int_val >>= 7 byte_array[-1] ^= 0x80 - return(byte_array) + return byte_array def LEB128SignedEncode(int_val): byte_array = bytearray() while True: - byte = int_val & 0x7f + byte = int_val & 0x7F byte_array.append(byte | 0x80) int_val >>= 7 - if (int_val == 0 and byte&0x40 == 0) or (int_val == -1 and byte&0x40): + if (int_val == 0 and byte & 0x40 == 0) or ( + int_val == -1 and byte & 0x40 + ): byte_array[-1] ^= 0x80 break - return(byte_array) + return byte_array + # @DEVI-FIXME-MVP-only-we currently inly support consts and get_global # interprets the init-exprs def init_interpret(expr): offset = 0 - byte, offset, dummy = Read(expr, offset, 'uint8') + byte, offset, dummy = Read(expr, offset, "uint8") const = int() if byte == 65: # @DEVI-FIXME-the spec says varint32, obviously we are not doing that # since it will return neg values which are meningless and break things - const, offset, dummy = Read(expr, offset, 'varuint32') + const, offset, dummy = Read(expr, offset, "varuint32") elif byte == 66: - const, offset, dummy = Read(expr, offset, 'varint64') + const, offset, dummy = Read(expr, offset, "varint64") elif byte == 67: - const, offset, dummy = Read(expr, offset, 'uint32') + const, offset, dummy = Read(expr, offset, "uint32") elif byte == 68: - const, offset, dummy = Read(expr, offset, 'uint64') + const, offset, dummy = Read(expr, offset, "uint64") elif byte == 35: pass else: - raise Exception(Colors.red + "illegal opcode for an MVP init expr." + Colors.ENDC) + raise Exception( + Colors.red + "illegal opcode for an MVP init expr." + Colors.ENDC + ) - block_end, offset, dummy = Read(expr, offset, 'uint8') + block_end, offset, dummy = Read(expr, offset, "uint8") if block_end != 11: - raise Exception(Colors.red + "init expr has no block end." + Colors.ENDC) + raise Exception( + Colors.red + "init expr has no block end." + Colors.ENDC + ) + + return const - return(const) # reads different-typed values from a byte array, takes in the bytearray, the # current offset the read should be performed from and the kind of value that @@ -124,7 +151,12 @@ def Read(section_byte, offset, kind): return_list = int() read_bytes = 0 - if kind == 'varuint1' or kind == 'varuint7' or kind == 'varuint32' or kind == 'varuint64': + if ( + kind == "varuint1" + or kind == "varuint7" + or kind == "varuint32" + or kind == "varuint64" + ): while True: byte = int(section_byte[offset]) read_bytes += 1 @@ -141,22 +173,34 @@ def Read(section_byte, offset, kind): break return_list = LEB128UnsignedDecode(operand) - #operand = [] - elif kind == 'uint8' or kind == 'uint16' or kind == 'uint32' or kind == 'uint64': - byte = section_byte[offset: offset + TypeDic[kind]] + # 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 = [] - elif kind == 'varint1' or kind == 'varint7' or kind == 'varint32' or kind == 'varint64': + return_list = int.from_bytes( + operand[0], byteorder="little", signed=False + ) + # operand = [] + elif ( + kind == "varint1" + or kind == "varint7" + or kind == "varint32" + or kind == "varint64" + ): while True: byte = int(section_byte[offset]) read_bytes += 1 offset += 1 operand.append(byte) # @DEVI-what happens when we decode a 56-bit value? - if byte == 0x80 or byte == 0xff: + if byte == 0x80 or byte == 0xFF: pass elif byte & 0x80 != 0: pass @@ -164,54 +208,74 @@ 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): rot_size_rem = rot_size % type_length - return (((val >> rot_size_rem) & (2**type_length - 1)) | ((val & (2**rot_size_rem - 1)) << (type_length - rot_size_rem))) + return ((val >> rot_size_rem) & (2 ** type_length - 1)) | ( + (val & (2 ** rot_size_rem - 1)) << (type_length - rot_size_rem) + ) + def rol(val, type_length, rot_size): rot_size_rem = rot_size % type_length - return (((val << rot_size_rem) & (2**type_length - 1)) | ((val & ((2**type_length - 1) - (2**(type_length - rot_size_rem) - 1))) >> (type_length - rot_size_rem))) + return ((val << rot_size_rem) & (2 ** type_length - 1)) | ( + ( + val + & ( + (2 ** type_length - 1) + - (2 ** (type_length - rot_size_rem) - 1) + ) + ) + >> (type_length - rot_size_rem) + ) + # @DEVI-these are here because i wanted to test them to make sure what i thik is # happening is really happening def reinterpretf32toi32(val): - return (stc.unpack("i", stc.pack("f" ,val))[0]) + return stc.unpack("i", stc.pack("f", val))[0] + def reinterpretf64toi64(val): - return (stc.unpack("Q", stc.pack("d", val))[0]) + return stc.unpack("Q", stc.pack("d", val))[0] + def reinterpreti32tof32(val): - return (stc.unpack("f", stc.pack("i", val))[0]) + return stc.unpack("f", stc.pack("i", val))[0] + def reinterpreti64tof64(val): - return (stc.unpack("d", stc.pack("Q", val))[0]) + return stc.unpack("d", stc.pack("Q", val))[0] + # @DEVI-FIXME def clz(val, _type): cnt = int() - if _type == 'uint32': - #bits = np.uint32(val) + if _type == "uint32": + # bits = np.uint32(val) power = 31 while power > -1: - if val & 2**power == 0: + if val & 2 ** power == 0: cnt += 1 else: break power -= 1 - elif _type == 'uint64': - #bits = bin(np.uint64(val)) + elif _type == "uint64": + # bits = bin(np.uint64(val)) power = 63 while power > -1: - if val & 2**power == 0: + if val & 2 ** power == 0: cnt += 1 else: break power -= 1 else: - raise Exception(Colors.red + "unsupported type passed to clz." + Colors.ENDC) + raise Exception( + Colors.red + "unsupported type passed to clz." + Colors.ENDC + ) return cnt @@ -219,104 +283,177 @@ def clz(val, _type): def ctz(val, _type): cnt = int() power = int() - if _type == 'uint32': - #bits = np.uint32(val) + if _type == "uint32": + # bits = np.uint32(val) while power < 32: - if val & 2**power == 0: + if val & 2 ** power == 0: cnt += 1 else: break power += 1 - elif _type == 'uint64': - #bits = bin(np.uint64(val)) + elif _type == "uint64": + # bits = bin(np.uint64(val)) while power < 64: - if val & 2**power == 0: + if val & 2 ** power == 0: cnt += 1 else: break power += 1 else: - raise Exception(Colors.red + "unsupported type passed to ctz." + Colors.ENDC) + raise Exception( + Colors.red + "unsupported type passed to ctz." + Colors.ENDC + ) return cnt + # @DEVI-FIXME def pop_cnt(val, _type): cnt = int() power = int() - if _type == 'uint32': - #bits = np.uint32(val) + if _type == "uint32": + # bits = np.uint32(val) while power < 32: - if val & 2**power != 0: + if val & 2 ** power != 0: cnt += 1 power += 1 - elif _type == 'uint64': - #bits = bin(np.uint64(val)) + elif _type == "uint64": + # bits = bin(np.uint64(val)) while power < 64: - if val & 2**power != 0: + if val & 2 ** power != 0: cnt += 1 power += 1 else: - raise Exception(Colors.red + "unsupported type passed to pop_cnt." + Colors.ENDC) + raise Exception( + Colors.red + "unsupported type passed to pop_cnt." + Colors.ENDC + ) return cnt + def gen_label(label_stack): counter += 1 label_stack.append(counter) + def dumpprettysections(sections_list, width, section_name): line_counter = 0 str_list = [] module_counter = 0 section_offset = 0 for sections in sections_list: - print (Colors.cyan + Colors.BOLD + "module " + repr(module_counter) + Colors.ENDC) + print( + Colors.cyan + + Colors.BOLD + + "module " + + repr(module_counter) + + Colors.ENDC + ) for section in sections.section_list: - if section_name == "": pass + if section_name == "": + pass else: if section_name != SectionID[section[0]]: continue - print(Colors.green + Colors.BOLD + SectionID[section[0]] + " section" + Colors.ENDC) - #print(Colors.green + "length: " + Colors.blue + section[1] + Colors.ENDC) - print(Colors.green + "length: " + Colors.blue + repr(section[2]) + Colors.ENDC) - print(Colors.green + "is custom section: " + Colors.blue + repr(section[3]) + Colors.ENDC) - print(Colors.green + "name length: " + Colors.blue + repr(section[4]) + Colors.ENDC) - - #print(section[5]) - #print(''.join(chr(char) for char in section[5])) - #print( ''.join([chr(char) for char in section[5]])) + print( + Colors.green + + Colors.BOLD + + SectionID[section[0]] + + " section" + + Colors.ENDC + ) + # print(Colors.green + "length: " + Colors.blue + section[1] + Colors.ENDC) + print( + Colors.green + + "length: " + + Colors.blue + + repr(section[2]) + + Colors.ENDC + ) + print( + Colors.green + + "is custom section: " + + Colors.blue + + repr(section[3]) + + Colors.ENDC + ) + print( + Colors.green + + "name length: " + + Colors.blue + + repr(section[4]) + + Colors.ENDC + ) + + # print(section[5]) + # print(''.join(chr(char) for char in section[5])) + # print( ''.join([chr(char) for char in section[5]])) if type(section[5]) == list: - print(Colors.green + "name: " + Colors.blue + ''.join([chr(char) for char in section[5]]) + Colors.ENDC) + print( + Colors.green + + "name: " + + Colors.blue + + "".join([chr(char) for char in section[5]]) + + Colors.ENDC + ) else: - print(Colors.green + "name: " + Colors.blue + section[5] + Colors.ENDC) + print( + Colors.green + + "name: " + + Colors.blue + + section[5] + + Colors.ENDC + ) print("\t", end="") for offset in range(0, width): if offset <= 15: - print(Colors.blue + hex(offset) + " " + Colors.ENDC, end="") + print( + Colors.blue + hex(offset) + " " + Colors.ENDC, end="" + ) else: - print(Colors.blue + hex(offset) + " " + Colors.ENDC, end="") + print( + Colors.blue + hex(offset) + " " + Colors.ENDC, end="" + ) print() - print(Colors.blue + Colors.BOLD + hex(section_offset) + "\t" + Colors.ENDC, end="") + print( + Colors.blue + + Colors.BOLD + + hex(section_offset) + + "\t" + + Colors.ENDC, + end="", + ) for byte in section[6]: if line_counter == width: section_offset += width - #print("\t\t", end="") + # print("\t\t", end="") line_counter = 0 for char in str_list: print(Colors.green + "|" + Colors.ENDC, end="") - if ord(char) < 32 or ord(char) > 127: print(" ", end="") - else: print(char, end="") + if ord(char) < 32 or ord(char) > 127: + print(" ", end="") + else: + print(char, end="") str_list = [] print() - print(Colors.blue + Colors.BOLD + hex(section_offset) + "\t" + Colors.ENDC, end="") - print(format(byte, '02x') + " ", end="") + print( + Colors.blue + + Colors.BOLD + + hex(section_offset) + + "\t" + + Colors.ENDC, + end="", + ) + print(format(byte, "02x") + " ", end="") str_list.append(chr(byte)) line_counter += 1 - #print(" ", end="") - for i in range(0, width - line_counter): print(" ", end="") + # print(" ", end="") + for i in range(0, width - line_counter): + print(" ", end="") for char in str_list: - if ord(char) < 32: print(" ", end="") - else: print(char, end="") + if ord(char) < 32: + print(" ", end="") + else: + print(char, end="") print(Colors.green + "|" + Colors.ENDC, end="") str_list = [] line_counter = 0 @@ -327,79 +464,87 @@ def dumpprettysections(sections_list, width, section_name): module_counter += 1 section_offset = 0 + def popcnt32(r1): temp = r1 temp = (temp & 0x55555555) + ((temp >> 1) & 0x55555555) temp = (temp & 0x33333333) + ((temp >> 2) & 0x33333333) - temp = (temp & 0x0f0f0f0f) + ((temp >> 4) & 0x0f0f0f0f) - temp = (temp & 0x00ff00ff) + ((temp >> 8) & 0x00ff00ff) - temp = (temp & 0x0000ffff) + ((temp >> 16) & 0x0000ffff) + temp = (temp & 0x0F0F0F0F) + ((temp >> 4) & 0x0F0F0F0F) + temp = (temp & 0x00FF00FF) + ((temp >> 8) & 0x00FF00FF) + temp = (temp & 0x0000FFFF) + ((temp >> 16) & 0x0000FFFF) return temp + def popcnt64(r1): temp = r1 temp = (temp & 0x5555555555555555) + ((temp >> 1) & 0x5555555555555555) temp = (temp & 0x3333333333333333) + ((temp >> 2) & 0x3333333333333333) - temp = (temp & 0x0f0f0f0f0f0f0f0f) + ((temp >> 4) & 0x0f0f0f0f0f0f0f0f) - temp = (temp & 0x00ff00ff00ff00ff) + ((temp >> 8) & 0x00ff00ff00ff00ff) - temp = (temp & 0x0000ffff0000ffff) + ((temp >> 16) & 0x0000ffff0000ffff) - temp = (temp & 0x00000000ffffffff) + ((temp >> 32) & 0x00000000ffffffff) + temp = (temp & 0x0F0F0F0F0F0F0F0F) + ((temp >> 4) & 0x0F0F0F0F0F0F0F0F) + temp = (temp & 0x00FF00FF00FF00FF) + ((temp >> 8) & 0x00FF00FF00FF00FF) + temp = (temp & 0x0000FFFF0000FFFF) + ((temp >> 16) & 0x0000FFFF0000FFFF) + temp = (temp & 0x00000000FFFFFFFF) + ((temp >> 32) & 0x00000000FFFFFFFF) return temp + def clz32(r1): - if r1 == 0: return 32 + if r1 == 0: + return 32 temp_r1 = r1 n = 0 - if temp_r1 & 0xffff0000 == 0: + if temp_r1 & 0xFFFF0000 == 0: n += 16 temp_r1 = temp_r1 << 16 - if temp_r1 & 0xff000000 == 0: + if temp_r1 & 0xFF000000 == 0: n += 8 temp_r1 = temp_r1 << 8 - if temp_r1 & 0xf0000000 == 0: + if temp_r1 & 0xF0000000 == 0: n += 4 temp_r1 = temp_r1 << 4 - if temp_r1 & 0xc0000000 == 0: + if temp_r1 & 0xC0000000 == 0: n += 2 temp_r1 = temp_r1 << 2 if temp_r1 & 0x8000000 == 0: n += 1 return n + def clz64(r1): - if r1 == 0: return 64 + if r1 == 0: + return 64 temp_r1 = r1 n = 0 - if temp_r1 & 0xffffffff00000000 == 0: + if temp_r1 & 0xFFFFFFFF00000000 == 0: n += 32 temp_r1 = temp_r1 << 32 - if temp_r1 & 0xffff000000000000 == 0: + if temp_r1 & 0xFFFF000000000000 == 0: n += 16 temp_r1 == temp_r1 << 16 - if temp_r1 & 0xff00000000000000 == 0: - n+= 8 + if temp_r1 & 0xFF00000000000000 == 0: + n += 8 temp_r1 = temp_r1 << 8 - if temp_r1 & 0xf000000000000000 == 0: + if temp_r1 & 0xF000000000000000 == 0: n += 4 temp_r1 = temp_r1 << 4 - if temp_r1 & 0xc000000000000000 == 0: + if temp_r1 & 0xC000000000000000 == 0: n += 2 temp_r1 = temp_r1 << 2 if temp_r1 & 0x8000000000000000 == 0: n += 1 return n + def ctz32(r1): - if r1 == 0: return 32 + if r1 == 0: + return 32 temp_r1 = r1 n = 0 - if temp_r1 & 0x0000ffff == 0: + if temp_r1 & 0x0000FFFF == 0: n += 16 temp_r1 = temp_r1 >> 16 - if temp_r1 & 0x000000ff == 0: + if temp_r1 & 0x000000FF == 0: n += 8 temp_r1 = temp_r1 >> 8 - if temp_r1 & 0x0000000f == 0: + if temp_r1 & 0x0000000F == 0: n += 4 temp_r1 = temp_r1 >> 4 if temp_r1 & 0x00000003 == 0: @@ -409,20 +554,22 @@ def ctz32(r1): n += 1 return n + def ctz64(r1): - if r1 == 0: return 64 + if r1 == 0: + return 64 temp_r1 = r1 n = 0 - if temp_r1 & 0x00000000ffffffff == 0: + if temp_r1 & 0x00000000FFFFFFFF == 0: n += 32 temp_r1 = temp_r1 >> 32 - if temp_r1 & 0x000000000000ffff == 0: + if temp_r1 & 0x000000000000FFFF == 0: n += 16 temp_r1 = temp_r1 >> 16 - if temp_r1 & 0x00000000000000ff == 0: + if temp_r1 & 0x00000000000000FF == 0: n += 8 temp_r1 = temp_r1 >> 8 - if temp_r1 & 0x000000000000000f == 0: + if temp_r1 & 0x000000000000000F == 0: n += 4 temp_r1 = temp_r1 >> 4 if temp_r1 & 0x0000000000000003 == 0: |