From 045e4e775c5d0ee2e7fd489cc3c236861fa5f004 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Sun, 24 Jun 2018 10:46:22 +0430 Subject: fixes 42, fixes 44 --- README.md | 15 +- bruiser/bruiser.cpp | 22 +- bruiser/lua-scripts/demo1.lua | 30 +- bruiser/lua-scripts/demo3.lua | 13 + bruiser/lua-scripts/regtest.lua | 12 + bruiser/lua-scripts/wasm.lua | 1 - bruiser/lua-scripts/xobj.lua | 34 +- bruiser/luatablegen/W_Code_Section_tablegen.c | 2 +- bruiser/luatablegen/W_Data_Section_tablegen.c | 2 +- bruiser/luatablegen/W_Data_Segment_tablegen.c | 2 +- bruiser/luatablegen/W_Elem_Segment_tablegen.c | 2 +- bruiser/luatablegen/W_Element_Section_tablegen.c | 2 +- bruiser/luatablegen/W_Export_Section_tablegen.c | 2 +- bruiser/luatablegen/W_Function_Body_tablegen.c | 2 +- bruiser/luatablegen/W_Function_Section_tablegen.c | 2 +- bruiser/luatablegen/W_Global_Entry_tablegen.c | 4 +- bruiser/luatablegen/W_Global_Section_tablegen.c | 2 +- .../luatablegen/W_Import_Section_Entry_tablegen.c | 2 +- bruiser/luatablegen/W_Import_Section_tablegen.c | 2 +- bruiser/luatablegen/W_Memory_Section_tablegen.c | 2 +- bruiser/luatablegen/W_Table_Section_tablegen.c | 2 +- .../luatablegen/W_Type_Section_Entry_tablegen.c | 4 +- bruiser/luatablegen/W_Type_Section_tablegen.c | 2 +- bruiser/luatablegen/Wasm_Module_tablegen.c | 24 +- bruiser/luatablegen/memory_type_t_tablegen.c | 2 +- bruiser/luatablegen/table_type_t_tablegen.c | 2 +- bruiser/makefile | 4 +- bruiser/tablegen.sh | 2 +- bruiser/wasm/OpCodes.py | 332 ----------------- bruiser/wasm/TBInit.py | 415 --------------------- bruiser/wasm/execute.py | 2 +- bruiser/wasm/init.py | 415 +++++++++++++++++++++ bruiser/wasm/opcodes.py | 332 +++++++++++++++++ bruiser/wasm/parse.py | 4 +- bruiser/wasm/utils.py | 2 +- extra-tools/luatablegen.py | 27 +- 36 files changed, 890 insertions(+), 836 deletions(-) create mode 100644 bruiser/lua-scripts/demo3.lua create mode 100644 bruiser/lua-scripts/regtest.lua delete mode 100644 bruiser/wasm/OpCodes.py delete mode 100644 bruiser/wasm/TBInit.py create mode 100644 bruiser/wasm/init.py create mode 100644 bruiser/wasm/opcodes.py diff --git a/README.md b/README.md index d61d0bb..87ac1df 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,9 @@ - [Overview](#overview) - [bruiser](#bruiser) - - [load.py](#load.py) + - [delf](#delf) + - [dwasm](#dwasm) + - [luatablegen](#luatablegen) - [obfuscator](#obfuscator) - [m0](#m0) - [Safercpp](#safercpp) @@ -57,10 +59,17 @@ Essentially bruiser is a Lua REPL plus: * Through the ASMRewriter functionality you can manipulate the machine code and push it back in the object. For more detail you can look at the wiki or check out bruiser's README.md.
* Luarocks: You can use your Luarocks modules/libraries in bruiser too. Just make sure `luarocks` is in your path and bruiser will take care of the rest.
-### load.py -`load.py` is a custom ELF dump script developed for bruiser. bruiser uses it to interact with ELF files.
+### delf +`delf` is a custom ELF dump script developed for bruiser. bruiser uses it to interact with ELF files.
You can also use the script as a standalone to dump info on the ELF file to stdout.
+### dwasm +'dwasm' is a custom WASM dump script. bruiser uses it to interact with WASM object files.
+The script is also usable in an standalone manner.
+ +### luatablegen +`luatablegen` is a python script that takes a json file including the details of a C structure, and generates C source and header files, a lua file including some convinience fields for the lua table and a markdown file including a summary of the table fields and their expected arg types and return types.
+ ### obfuscator Is a C/C++ source code obfuscator.
diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index 7e6a22e..99f7731 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -1375,22 +1375,24 @@ class LuaWrapper int BruiserPyLoader(lua_State* __ls ) { int numargs = lua_gettop(__ls); //std::string filename = "../bfd/load.py"; - std::string filename = "load"; + //std::string filename = "load"; + std::string filename; std::string funcname; std::string objjpath; std::string action; - if (numargs == 3) { + if (numargs == 4) { if (Verbose) std::cout << CYAN << "got args." << NORMAL << "\n"; - funcname = lua_tostring(__ls, 1); - if (funcname == "") PRINT_WITH_COLOR_LB(RED, "first argument is nil"); - objjpath = lua_tostring(__ls, 2); - if (objjpath == "") PRINT_WITH_COLOR_LB(RED, "second argument is nil"); - action = lua_tostring(__ls, 3); - if (action == "") PRINT_WITH_COLOR_LB(RED, "third argument is nil"); + filename = lua_tostring(__ls, 1); + if (filename == "") PRINT_WITH_COLOR_LB(RED, "first argument is nil"); + funcname = lua_tostring(__ls, 2); + if (funcname == "") PRINT_WITH_COLOR_LB(RED, "second argument is nil"); + objjpath = lua_tostring(__ls, 3); + if (objjpath == "") PRINT_WITH_COLOR_LB(RED, "third argument is nil"); + action = lua_tostring(__ls, 4); + if (action == "") PRINT_WITH_COLOR_LB(RED, "fourth argument is nil"); std::cout << NORMAL; - } - else { + } else { std::cout << RED << "wrong number of arguments provided. should give the python script name, python func name and the return type.\n" << NORMAL; return EXIT_FAILURE; } diff --git a/bruiser/lua-scripts/demo1.lua b/bruiser/lua-scripts/demo1.lua index c0ee9fd..c349c36 100644 --- a/bruiser/lua-scripts/demo1.lua +++ b/bruiser/lua-scripts/demo1.lua @@ -20,8 +20,8 @@ elf_file = "/home/bloodstalker/devi/hell2/bfd/test/test.so" function Demo1.getGlobalTable() local return_table = {} - local names = objload("elf_get_obj_names", elf_file, "symbol_list") - local sizes = objload("elf_get_obj_sizes", elf_file, "bytes") + local names = objload("load", "elf_get_obj_names", elf_file, "symbol_list") + local sizes = objload("load", "elf_get_obj_sizes", elf_file, "bytes") for i=1,#names,1 do return_table[names[i]] = sizes[i] end @@ -29,28 +29,28 @@ function Demo1.getGlobalTable() end function Demo1.printObjNames() - local c = objload("elf_get_obj_names", elf_file, "symbol_list") + local c = objload("load", "elf_get_obj_names", elf_file, "symbol_list") for k,v in ipairs(c) do print(k,v) end end function Demo1.printObjSizes() - local c = objload("elf_get_obj_sizes", elf_file, "bytes") + local c = objload("load", "elf_get_obj_sizes", elf_file, "bytes") for k,v in ipairs(c) do print(k,v) end end function Demo1.printFuncNames() - local c = objload("elf_get_func_names", elf_file, "symbol_list") + local c = objload("load", "elf_get_func_names", elf_file, "symbol_list") for k,v in ipairs(c) do print(k,v) end end function Demo1.printFuncCode() - local c = objload("elf_get_func_code", elf_file, "code_list") + local c = objload("load", "elf_get_func_code", elf_file, "code_list") for k,v in ipairs(c) do print(k,v) if #v ~= 0 then @@ -63,7 +63,7 @@ function Demo1.printFuncCode() end function Demo1.findMain() - local c = objload("elf_get_func_names", elf_file, "symbol_list") + local c = objload("load", "elf_get_func_names", elf_file, "symbol_list") for k,v in ipairs(c) do if v == "main" then io.write("main index is".." "..k.."\n") @@ -74,8 +74,8 @@ end function Demo1.codeTables() local return_table = {} - local func_name_table = objload("elf_get_func_names", elf_file, "symbol_list") - local code_table = objload("elf_get_func_code", elf_file, "code_list") + local func_name_table = objload("load", "elf_get_func_names", elf_file, "symbol_list") + local code_table = objload("load", "elf_get_func_code", elf_file, "code_list") for i=1,#func_name_table,1 do return_table[func_name_table[i]] = code_table[i] end @@ -84,8 +84,8 @@ end function Demo1.codeTableByName(name) local return_table = {} - local func_name_table = objload("elf_get_func_names", elf_file, "symbol_list") - local code_table = objload("elf_get_func_code", elf_file, "code_list") + local func_name_table = objload("load", "elf_get_func_names", elf_file, "symbol_list") + local code_table = objload("load", "elf_get_func_code", elf_file, "code_list") for k,v in ipairs(func_name_table) do if v == name then for k1, v1 in ipairs(code_table[k]) do @@ -99,8 +99,8 @@ end function Demo1.codeTableByName_number(name) local return_table = {} - local func_name_table = objload("elf_get_func_names", elf_file, "symbol_list") - local code_table = objload("elf_get_func_code", elf_file, "code_list") + local func_name_table = objload("load", "elf_get_func_names", elf_file, "symbol_list") + local code_table = objload("load", "elf_get_func_code", elf_file, "code_list") for k,v in ipairs(func_name_table) do if v == name then for k1, v1 in ipairs(code_table[k]) do @@ -113,8 +113,8 @@ function Demo1.codeTableByName_number(name) end function Demo1.printFuncSizes() - local func_name_table = objload("elf_get_func_names", elf_file, "symbol_list") - local code_table = objload("elf_get_func_code", elf_file, "code_list") + local func_name_table = objload("load", "elf_get_func_names", elf_file, "symbol_list") + local code_table = objload("load", "elf_get_func_code", elf_file, "code_list") local counter = 1 print("function sizes:") for k, v in ipairs(code_table) do diff --git a/bruiser/lua-scripts/demo3.lua b/bruiser/lua-scripts/demo3.lua new file mode 100644 index 0000000..89dde81 --- /dev/null +++ b/bruiser/lua-scripts/demo3.lua @@ -0,0 +1,13 @@ + +local demo3 = {} + +function demo3.init() + local wasm = require("wasm") + local wasm_file = "../wasm/test/injected.wasm" + local wasm_module = Wasm_Module() + local table_type = table_type_t() + local resizable_limit = resizable_limit_t() + table_type:set_resizable_limit(resizable_limit) +end + +return demo3 diff --git a/bruiser/lua-scripts/regtest.lua b/bruiser/lua-scripts/regtest.lua new file mode 100644 index 0000000..f7ec461 --- /dev/null +++ b/bruiser/lua-scripts/regtest.lua @@ -0,0 +1,12 @@ + +-- luarocks install luaposix +--local posix = require("posix") + +function reg_test() + local demo1 = require("demo1") + local demo2 = require("demo2") + local demo3 = require("asmtest") + print("yo") +end + +reg_test() diff --git a/bruiser/lua-scripts/wasm.lua b/bruiser/lua-scripts/wasm.lua index 3c94960..88588a6 100644 --- a/bruiser/lua-scripts/wasm.lua +++ b/bruiser/lua-scripts/wasm.lua @@ -20,7 +20,6 @@ setmetatable(resizable_limit_t, {__call = setmetatable(global_type_t, {__call = function(self, arg0, arg1) local t = self.new(arg0, arg1) - print("created",t) return t end } diff --git a/bruiser/lua-scripts/xobj.lua b/bruiser/lua-scripts/xobj.lua index 08a2945..a4f7a8d 100644 --- a/bruiser/lua-scripts/xobj.lua +++ b/bruiser/lua-scripts/xobj.lua @@ -26,8 +26,8 @@ end function xobj.getGlobalTable() local return_table = {} - local names = objload("elf_get_obj_names", elf_file, "symbol_list") - local sizes = objload("elf_get_obj_sizes", elf_file, "symbol_list") + local names = objload("load", "elf_get_obj_names", elf_file, "symbol_list") + local sizes = objload("load", "elf_get_obj_sizes", elf_file, "symbol_list") for i=1,#names,1 do return_table[names[i]] = sizes[i] end @@ -35,28 +35,28 @@ function xobj.getGlobalTable() end function xobj.printObjNames() - local c = objload("elf_get_obj_names", elf_file, "symbol_list") + local c = objload("load", "elf_get_obj_names", elf_file, "symbol_list") for k,v in ipairs(c) do print(k,v) end end function xobj.printObjSizes() - local c = objload("elf_get_obj_sizes", elf_file, "symbol_list") + local c = objload("load", "elf_get_obj_sizes", elf_file, "symbol_list") for k,v in ipairs(c) do print(k,v) end end function xobj.printFuncNames() - local c = objload("elf_get_func_names", elf_file, "symbol_list") + local c = objload("load", "elf_get_func_names", elf_file, "symbol_list") for k,v in ipairs(c) do print(k,v) end end function xobj.printFuncCode() - local c = objload("elf_get_func_code", elf_file, "code_list") + local c = objload("load", "elf_get_func_code", elf_file, "code_list") for k,v in ipairs(c) do print(k,v) if #v ~= 0 then @@ -69,7 +69,7 @@ function xobj.printFuncCode() end function xobj.findMain() - local c = objload("elf_get_func_names", elf_file, "symbol_list") + local c = objload("load", "elf_get_func_names", elf_file, "symbol_list") for k,v in ipairs(c) do if v == "'main'" then io.write("main index is".." "..k.."\n") @@ -80,8 +80,8 @@ end function xobj.codeTables() local return_table = {} - local func_name_table = objload("elf_get_func_names", elf_file, "symbol_list") - local code_table = objload("elf_get_func_code", elf_file, "code_list") + local func_name_table = objload("load", "elf_get_func_names", elf_file, "symbol_list") + local code_table = objload("load", "elf_get_func_code", elf_file, "code_list") for i=1,#func_name_table,1 do return_table[func_name_table[i]] = code_table[i] end @@ -90,8 +90,8 @@ end function xobj.codeTableByName(name) local return_table = {} - local func_name_table = objload("elf_get_func_names", elf_file, "symbol_list") - local code_table = objload("elf_get_func_code", elf_file, "code_list") + local func_name_table = objload("load", "elf_get_func_names", elf_file, "symbol_list") + local code_table = objload("load", "elf_get_func_code", elf_file, "code_list") for k,v in ipairs(func_name_table) do if v == name then for k1, v1 in ipairs(code_table[k]) do @@ -105,8 +105,8 @@ end function xobj.codeTableByName_number(name) local return_table = {} - local func_name_table = objload("elf_get_func_names", elf_file, "symbol_list") - local code_table = objload("elf_get_func_code", elf_file, "code_list") + local func_name_table = objload("load", "elf_get_func_names", elf_file, "symbol_list") + local code_table = objload("load", "elf_get_func_code", elf_file, "code_list") for k,v in ipairs(func_name_table) do if v == name then for k1, v1 in ipairs(code_table[k]) do @@ -119,8 +119,8 @@ function xobj.codeTableByName_number(name) end function xobj.printFuncSizes() - local func_name_table = objload("elf_get_func_names", elf_file, "symbol_list") - local code_table = objload("elf_get_func_code", elf_file, "code_list") + local func_name_table = objload("load", "elf_get_func_names", elf_file, "symbol_list") + local code_table = objload("load", "elf_get_func_code", elf_file, "code_list") local counter = 1 print("function sizes:") for k, v in ipairs(code_table) do @@ -130,11 +130,11 @@ function xobj.printFuncSizes() end function xobj.getTextSection(elf_exe) - return objload("elf_get_text_section", elf_exe, "bytes") + return objload("load", "elf_get_text_section", elf_exe, "bytes") end function xobj.getRODataSection(elf_exe) - return objload("elf_get_rodata_section", elf_exe, "bytes") + return objload("load", "elf_get_rodata_section", elf_exe, "bytes") end function xobj.CSDump(code) diff --git a/bruiser/luatablegen/W_Code_Section_tablegen.c b/bruiser/luatablegen/W_Code_Section_tablegen.c index 1c516ce..b4bd190 100644 --- a/bruiser/luatablegen/W_Code_Section_tablegen.c +++ b/bruiser/luatablegen/W_Code_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Code_Section_count(lua_State* __ls) { } static int setter_W_Code_Section_bodies(lua_State* __ls) { W_Code_Section* dummy = check_W_Code_Section(__ls, 1); - dummy->bodies = luaL_checkudata(__ls, 2, "W_Code_Section"); + dummy->bodies = luaL_checkudata(__ls, 2, "bodies_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Data_Section_tablegen.c b/bruiser/luatablegen/W_Data_Section_tablegen.c index 3dbc98b..9426e71 100644 --- a/bruiser/luatablegen/W_Data_Section_tablegen.c +++ b/bruiser/luatablegen/W_Data_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Data_Section_count(lua_State* __ls) { } static int setter_W_Data_Section_entries(lua_State* __ls) { W_Data_Section* dummy = check_W_Data_Section(__ls, 1); - dummy->entries = luaL_checkudata(__ls, 2, "W_Data_Section"); + dummy->entries = luaL_checkudata(__ls, 2, "entries_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Data_Segment_tablegen.c b/bruiser/luatablegen/W_Data_Segment_tablegen.c index 20b2762..1fbac4d 100644 --- a/bruiser/luatablegen/W_Data_Segment_tablegen.c +++ b/bruiser/luatablegen/W_Data_Segment_tablegen.c @@ -87,7 +87,7 @@ static int setter_W_Data_Segment_index(lua_State* __ls) { } static int setter_W_Data_Segment_offset(lua_State* __ls) { W_Data_Segment* dummy = check_W_Data_Segment(__ls, 1); - dummy->offset = luaL_checkudata(__ls, 2, "W_Data_Segment"); + dummy->offset = luaL_checkudata(__ls, 2, "offset_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Elem_Segment_tablegen.c b/bruiser/luatablegen/W_Elem_Segment_tablegen.c index 82f8a30..a9ae97d 100644 --- a/bruiser/luatablegen/W_Elem_Segment_tablegen.c +++ b/bruiser/luatablegen/W_Elem_Segment_tablegen.c @@ -87,7 +87,7 @@ static int setter_W_Elem_Segment_index(lua_State* __ls) { } static int setter_W_Elem_Segment_offset(lua_State* __ls) { W_Elem_Segment* dummy = check_W_Elem_Segment(__ls, 1); - dummy->offset = luaL_checkudata(__ls, 2, "W_Elem_Segment"); + dummy->offset = luaL_checkudata(__ls, 2, "offset_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Element_Section_tablegen.c b/bruiser/luatablegen/W_Element_Section_tablegen.c index 93e545b..b00b7fc 100644 --- a/bruiser/luatablegen/W_Element_Section_tablegen.c +++ b/bruiser/luatablegen/W_Element_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Element_Section_count(lua_State* __ls) { } static int setter_W_Element_Section_entries(lua_State* __ls) { W_Element_Section* dummy = check_W_Element_Section(__ls, 1); - dummy->entries = luaL_checkudata(__ls, 2, "W_Element_Section"); + dummy->entries = luaL_checkudata(__ls, 2, "entries_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Export_Section_tablegen.c b/bruiser/luatablegen/W_Export_Section_tablegen.c index fbd2538..3ec02c8 100644 --- a/bruiser/luatablegen/W_Export_Section_tablegen.c +++ b/bruiser/luatablegen/W_Export_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Export_Section_count(lua_State* __ls) { } static int setter_W_Export_Section_entries(lua_State* __ls) { W_Export_Section* dummy = check_W_Export_Section(__ls, 1); - dummy->entries = luaL_checkudata(__ls, 2, "W_Export_Section"); + dummy->entries = luaL_checkudata(__ls, 2, "entries_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Function_Body_tablegen.c b/bruiser/luatablegen/W_Function_Body_tablegen.c index 173c381..59a6a10 100644 --- a/bruiser/luatablegen/W_Function_Body_tablegen.c +++ b/bruiser/luatablegen/W_Function_Body_tablegen.c @@ -93,7 +93,7 @@ static int setter_W_Function_Body_local_count(lua_State* __ls) { } static int setter_W_Function_Body_locals(lua_State* __ls) { W_Function_Body* dummy = check_W_Function_Body(__ls, 1); - dummy->locals = luaL_checkudata(__ls, 2, "W_Function_Body"); + dummy->locals = luaL_checkudata(__ls, 2, "locals_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Function_Section_tablegen.c b/bruiser/luatablegen/W_Function_Section_tablegen.c index ebc249d..a2de6b6 100644 --- a/bruiser/luatablegen/W_Function_Section_tablegen.c +++ b/bruiser/luatablegen/W_Function_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Function_Section_count(lua_State* __ls) { } static int setter_W_Function_Section_types(lua_State* __ls) { W_Function_Section* dummy = check_W_Function_Section(__ls, 1); - dummy->types = luaL_checkudata(__ls, 2, "W_Function_Section"); + dummy->types = luaL_checkudata(__ls, 2, "types_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Global_Entry_tablegen.c b/bruiser/luatablegen/W_Global_Entry_tablegen.c index 9b87c0f..0259cd6 100644 --- a/bruiser/luatablegen/W_Global_Entry_tablegen.c +++ b/bruiser/luatablegen/W_Global_Entry_tablegen.c @@ -63,13 +63,13 @@ static int getter_W_Global_Entry_init(lua_State* __ls) { static int setter_W_Global_Entry_type(lua_State* __ls) { W_Global_Entry* dummy = check_W_Global_Entry(__ls, 1); - dummy->type = luaL_checkudata(__ls, 2, "W_Global_Entry"); + dummy->type = luaL_checkudata(__ls, 2, "type_t"); lua_settop(__ls, 1); return 1; } static int setter_W_Global_Entry_init(lua_State* __ls) { W_Global_Entry* dummy = check_W_Global_Entry(__ls, 1); - dummy->init = luaL_checkudata(__ls, 2, "W_Global_Entry"); + dummy->init = luaL_checkudata(__ls, 2, "init_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Global_Section_tablegen.c b/bruiser/luatablegen/W_Global_Section_tablegen.c index 1254816..0bb2056 100644 --- a/bruiser/luatablegen/W_Global_Section_tablegen.c +++ b/bruiser/luatablegen/W_Global_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Global_Section_count(lua_State* __ls) { } static int setter_W_Global_Section_globals(lua_State* __ls) { W_Global_Section* dummy = check_W_Global_Section(__ls, 1); - dummy->globals = luaL_checkudata(__ls, 2, "W_Global_Section"); + dummy->globals = luaL_checkudata(__ls, 2, "globals_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Import_Section_Entry_tablegen.c b/bruiser/luatablegen/W_Import_Section_Entry_tablegen.c index 0a568e8..97ee26b 100644 --- a/bruiser/luatablegen/W_Import_Section_Entry_tablegen.c +++ b/bruiser/luatablegen/W_Import_Section_Entry_tablegen.c @@ -129,7 +129,7 @@ static int setter_W_Import_Section_Entry_kind(lua_State* __ls) { } static int setter_W_Import_Section_Entry_type(lua_State* __ls) { W_Import_Section_Entry* dummy = check_W_Import_Section_Entry(__ls, 1); - dummy->type = luaL_checkudata(__ls, 2, "W_Import_Section_Entry"); + dummy->type = luaL_checkudata(__ls, 2, "type_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Import_Section_tablegen.c b/bruiser/luatablegen/W_Import_Section_tablegen.c index de966f9..5286c75 100644 --- a/bruiser/luatablegen/W_Import_Section_tablegen.c +++ b/bruiser/luatablegen/W_Import_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Import_Section_count(lua_State* __ls) { } static int setter_W_Import_Section_entries(lua_State* __ls) { W_Import_Section* dummy = check_W_Import_Section(__ls, 1); - dummy->entries = luaL_checkudata(__ls, 2, "W_Import_Section"); + dummy->entries = luaL_checkudata(__ls, 2, "entries_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Memory_Section_tablegen.c b/bruiser/luatablegen/W_Memory_Section_tablegen.c index 8972528..78cd664 100644 --- a/bruiser/luatablegen/W_Memory_Section_tablegen.c +++ b/bruiser/luatablegen/W_Memory_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Memory_Section_count(lua_State* __ls) { } static int setter_W_Memory_Section_entries(lua_State* __ls) { W_Memory_Section* dummy = check_W_Memory_Section(__ls, 1); - dummy->entries = luaL_checkudata(__ls, 2, "W_Memory_Section"); + dummy->entries = luaL_checkudata(__ls, 2, "entries_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Table_Section_tablegen.c b/bruiser/luatablegen/W_Table_Section_tablegen.c index 43bcdf0..c536fd3 100644 --- a/bruiser/luatablegen/W_Table_Section_tablegen.c +++ b/bruiser/luatablegen/W_Table_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Table_Section_count(lua_State* __ls) { } static int setter_W_Table_Section_entries(lua_State* __ls) { W_Table_Section* dummy = check_W_Table_Section(__ls, 1); - dummy->entries = luaL_checkudata(__ls, 2, "W_Table_Section"); + dummy->entries = luaL_checkudata(__ls, 2, "entries_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Type_Section_Entry_tablegen.c b/bruiser/luatablegen/W_Type_Section_Entry_tablegen.c index c6a38ea..5462a41 100644 --- a/bruiser/luatablegen/W_Type_Section_Entry_tablegen.c +++ b/bruiser/luatablegen/W_Type_Section_Entry_tablegen.c @@ -102,7 +102,7 @@ static int setter_W_Type_Section_Entry_param_count(lua_State* __ls) { } static int setter_W_Type_Section_Entry_param_types(lua_State* __ls) { W_Type_Section_Entry* dummy = check_W_Type_Section_Entry(__ls, 1); - dummy->param_types = luaL_checkudata(__ls, 2, "W_Type_Section_Entry"); + dummy->param_types = luaL_checkudata(__ls, 2, "param_types_t"); lua_settop(__ls, 1); return 1; } @@ -114,7 +114,7 @@ static int setter_W_Type_Section_Entry_return_count(lua_State* __ls) { } static int setter_W_Type_Section_Entry_return_types(lua_State* __ls) { W_Type_Section_Entry* dummy = check_W_Type_Section_Entry(__ls, 1); - dummy->return_types = luaL_checkudata(__ls, 2, "W_Type_Section_Entry"); + dummy->return_types = luaL_checkudata(__ls, 2, "return_types_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/W_Type_Section_tablegen.c b/bruiser/luatablegen/W_Type_Section_tablegen.c index 237c87b..d7e9f7e 100644 --- a/bruiser/luatablegen/W_Type_Section_tablegen.c +++ b/bruiser/luatablegen/W_Type_Section_tablegen.c @@ -69,7 +69,7 @@ static int setter_W_Type_Section_count(lua_State* __ls) { } static int setter_W_Type_Section_entries(lua_State* __ls) { W_Type_Section* dummy = check_W_Type_Section(__ls, 1); - dummy->entries = luaL_checkudata(__ls, 2, "W_Type_Section"); + dummy->entries = luaL_checkudata(__ls, 2, "entries_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/Wasm_Module_tablegen.c b/bruiser/luatablegen/Wasm_Module_tablegen.c index 318309e..99b603c 100644 --- a/bruiser/luatablegen/Wasm_Module_tablegen.c +++ b/bruiser/luatablegen/Wasm_Module_tablegen.c @@ -162,73 +162,73 @@ static int getter_Wasm_Module_name(lua_State* __ls) { static int setter_Wasm_Module_type_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->type_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->type_section = luaL_checkudata(__ls, 2, "type_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_import_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->import_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->import_section = luaL_checkudata(__ls, 2, "import_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_function_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->function_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->function_section = luaL_checkudata(__ls, 2, "function_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_table_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->table_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->table_section = luaL_checkudata(__ls, 2, "table_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_memory_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->memory_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->memory_section = luaL_checkudata(__ls, 2, "memory_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_global_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->global_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->global_section = luaL_checkudata(__ls, 2, "global_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_export_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->export_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->export_section = luaL_checkudata(__ls, 2, "export_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_start_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->start_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->start_section = luaL_checkudata(__ls, 2, "start_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_element_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->element_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->element_section = luaL_checkudata(__ls, 2, "element_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_code_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->code_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->code_section = luaL_checkudata(__ls, 2, "code_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_data_section(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->data_section = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->data_section = luaL_checkudata(__ls, 2, "data_section_t"); lua_settop(__ls, 1); return 1; } static int setter_Wasm_Module_W_Custom_Sections(lua_State* __ls) { Wasm_Module* dummy = check_Wasm_Module(__ls, 1); - dummy->W_Custom_Sections = luaL_checkudata(__ls, 2, "Wasm_Module"); + dummy->W_Custom_Sections = luaL_checkudata(__ls, 2, "W_Custom_Sections_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/memory_type_t_tablegen.c b/bruiser/luatablegen/memory_type_t_tablegen.c index 5f75d51..3914732 100644 --- a/bruiser/luatablegen/memory_type_t_tablegen.c +++ b/bruiser/luatablegen/memory_type_t_tablegen.c @@ -54,7 +54,7 @@ static int getter_memory_type_t_resizable_limit(lua_State* __ls) { static int setter_memory_type_t_resizable_limit(lua_State* __ls) { memory_type_t* dummy = check_memory_type_t(__ls, 1); - dummy->resizable_limit = luaL_checkudata(__ls, 2, "memory_type_t"); + dummy->resizable_limit = luaL_checkudata(__ls, 2, "resizable_limit_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/luatablegen/table_type_t_tablegen.c b/bruiser/luatablegen/table_type_t_tablegen.c index a8e5af0..c37af75 100644 --- a/bruiser/luatablegen/table_type_t_tablegen.c +++ b/bruiser/luatablegen/table_type_t_tablegen.c @@ -69,7 +69,7 @@ static int setter_table_type_t_element_type(lua_State* __ls) { } static int setter_table_type_t_resizable_limit(lua_State* __ls) { table_type_t* dummy = check_table_type_t(__ls, 1); - dummy->resizable_limit = luaL_checkudata(__ls, 2, "table_type_t"); + dummy->resizable_limit = luaL_checkudata(__ls, 2, "resizable_limit_t"); lua_settop(__ls, 1); return 1; } diff --git a/bruiser/makefile b/bruiser/makefile index ed592d8..31dff92 100644 --- a/bruiser/makefile +++ b/bruiser/makefile @@ -17,14 +17,14 @@ C_SRCS=$(wildcard *.c) #for some reason without ld the build fails on ubuntu trusty on travis #EXTRA_LD_FLAGS+=-lpthread -ldl -lutil -lm -Xlinker -lpython3 EXTRA_LD_FLAGS+=$(shell $(PY_CONF) --ldflags) -lffi -lcapstone -lkeystone -L./lua-5.3.4/src -llua -TBG_OBJLIST_INC:=$(patsubst ./luatablegen/%.c, ./luatablegen/%.o, $(wildcard ./luatablegen/*.c)) +TBG_OBJLIST_INC=$(patsubst ./luatablegen/%.c, ./luatablegen/%.o, $(wildcard ./luatablegen/*.c)) SAN?= ######################################RULES#################################### .DEFAULT: all .PHONY: all clean help -all: $(BRUISER) +all: $(BRUISER) ./wasmtablegen.json depend:.depend dependc:.dependc diff --git a/bruiser/tablegen.sh b/bruiser/tablegen.sh index 03a660c..3279e22 100755 --- a/bruiser/tablegen.sh +++ b/bruiser/tablegen.sh @@ -1,6 +1,6 @@ #!/usr/bin/bash cd $(dirname $0) -../extra-tools/luatablegen.py --tbg ./wasmtablegen.json --out ./luatablegen --luaheader ../lua-5.3.4/src --pre ./luatablegen/wasmheader.txt --headeraggr ./luatablegen/wasm_tables.h --lualibpath ./lua-scripts/wasm.lua +../extra-tools/luatablegen.py --tbg ./wasmtablegen.json --out ./luatablegen --luaheader ../lua-5.3.4/src --pre ./luatablegen/wasmheader.txt --headeraggr ./luatablegen/wasm_tables.h --lualibpath ./lua-scripts/wasm.lua --docpath /home/bloodstalker/extra/mutator.wiki/wasm.md if [[ $1 == test ]]; then make -C ./luatablegen make clean diff --git a/bruiser/wasm/OpCodes.py b/bruiser/wasm/OpCodes.py deleted file mode 100644 index f7c9a1b..0000000 --- a/bruiser/wasm/OpCodes.py +++ /dev/null @@ -1,332 +0,0 @@ -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"} - -class RelocType(Enum): - R_WEBASSEMBLY_FUNCTION_INDEX_LEB = 0 - R_WEBASSEMBLY_TABLE_INDEX_SLEB = 1 - R_WEBASSEMBLY_TABLE_INDEX_I32 = 2 - R_WEBASSEMBLY_MEMORY_ADDR_LEB = 3 - R_WEBASSEMBLY_MEMORY_ADDR_SLEB = 4 - R_WEBASSEMBLY_MEMORY_ADDR_I32 = 5 - R_WEBASSEMBLY_TYPE_INDEX_LEB = 6 - R_WEBASSEMBLY_GLOBAL_INDEX_LEB = 7 - 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 - lebs = 3 - flot = 4 - dobl = 5 - -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} - -# holds the version 1.0 wasm opcodes and immediates -class WASM_OP_Code: - version_number = 0x01 - magic_number = 0x6d736100 - PAGE_SIZE = 65536 - uint8 = 1 - uint16 = 2 - uint32 = 4 - uint64 = 8 - varuint1 = 1 - varuint7 = 1 - varuint32 = 4 - varuint64 = 8 - varint1 = 1 - varint7 = 1 - varint32 = 4 - varint64 = 8 - 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)] - - 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_dict = dict(control_flow_ops) - 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_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_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_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_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_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_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_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_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_dict = dict(reinterpretations) - 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_dict = dict(section_code) - section_code_dict_rev = {v: k for k, v in section_code_dict.items()} diff --git a/bruiser/wasm/TBInit.py b/bruiser/wasm/TBInit.py deleted file mode 100644 index f14dfd1..0000000 --- a/bruiser/wasm/TBInit.py +++ /dev/null @@ -1,415 +0,0 @@ -from utils import Colors, init_interpret, ParseFlags -from OpCodes import WASM_OP_Code -from section_structs import Code_Section, Func_Body, WASM_Ins, Resizable_Limits, Memory_Section -from execute import * -import datetime as dti -import os -import sys -import signal - - -# McCabe cyclomatic complexity metric -class Metric(): - def __init__(self, code_section): - self.code_section = code_section - self.metric = [] - self.soc = [] - - def mccabe(self): - soc = 0 - Edges = 1 - Nodes = 1 - 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: - Nodes += 2 - Edges += 4 - elif ins.opcode == 3: - Nodes += 2 - Edges += 3 - else: - pass - - self.metric.append(Edges - Nodes + 1) - self.soc.append(soc) - soc = 0 - Edges = 1 - Nodes = 1 - - def getMcCabe(self): - return self.metric - - def getSOC(self): - return self.soc - - -# handles the debug option --memdump. dumps the contents of linear memories. -def DumpLinearMems(linear_memories, threshold): - count = int() - strrep = [] - linmem_cnt = int() - for lin_mem in linear_memories: - 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: - for ch in strrep: - # @DEVI-line feed messes the pretty format up - if ord(ch) != 10: - print(Colors.green + ' ' + ch + Colors.ENDC, end = '') - else: - pass - print() - strrep = [] - print(Colors.cyan + hex(count), ':\t' + Colors.ENDC, end='') - strrep.append(str(chr(byte))) - print(Colors.blue + format(byte, '02x') + ' ' + Colors.ENDC, end='') - else: - strrep += str(chr(byte)) - print(Colors.blue + format(byte, '02x') + ' ' + Colors.ENDC, end='') - count += 1 - count = 0 - print() - - -# handles the debug options --idxspc. dumps the index spaces. -def DumpIndexSpaces(machinestate): - 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) - for iter in machinestate.Index_Space_Global: - print(Colors.blue + repr(iter) + 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) - for iter in machinestate.Index_Space_Table: - print(Colors.blue + repr(iter) + Colors.ENDC) - print('-----------------------------------------') - - -# WIP-the Truebit Machine class -class TBMachine(): - def __init__(self): - # bytearray of size PAGE_SIZE - self.Linear_Memory = [] - self.Stack_Label = list() - self.Stack_Label_Height = int() - self.Stack_Control_Flow = list() - self.Stack_Call = list() - self.Stack_Value = list() - self.Stack_Omni = list() - self.Vector_Globals = list() - self.Index_Space_Function = list() - self.Index_Space_Global = list() - self.Index_Space_Linear = list() - self.Index_Space_Table = list() - self.Index_Space_Locals = list() - self.Index_Space_Label = list() - - -# handles the initialization of the WASM machine -class TBInit(): - def __init__(self, module, machinestate): - self.module = module - self.machinestate = machinestate - - # a convenience function that runs the methods of the class. all methods - # can be called separately manually as well. - def run(self): - self.InitFuncIndexSpace() - self.InitGlobalIndexSpace() - self.InitLinearMemoryIndexSpace() - self.InitTableIndexSpace() - self.InitializeLinearMemory() - - def InitFuncIndexSpace(self): - if self.module.import_section is not None: - for iter in self.module.import_section.import_entry: - if iter.kind == 0: - name = str() - for i in iter.field_str: - name += str(chr(i)) - self.machinestate.Index_Space_Function.append(name) - - if self.module.function_section is not None: - for iter in self.module.function_section.type_section_index: - self.machinestate.Index_Space_Function.append(iter) - - def InitGlobalIndexSpace(self): - if self.module.import_section is not None: - for iter in self.module.import_section.import_entry: - if iter.kind == 3: - name = str() - for i in iter.field_str: - name += str(chr(i)) - self.machinestate.Index_Space_Global.append(name) - - if self.module.global_section is not None: - for iter in self.module.global_section.global_variables: - self.machinestate.Index_Space_Global.append(iter.init_expr) - - def InitLinearMemoryIndexSpace(self): - if self.module.import_section is not None: - for iter in self.module.import_section.import_entry: - if iter.kind == 2: - name = str() - for i in iter.field_str: - name += str(chr(i)) - self.machinestate.Index_Space_Linear.append(name) - - if self.module.memory_section is not None: - for iter in self.module.memory_section.memory_types: - self.machinestate.Index_Space_Linear.append(iter.initial) - - def InitTableIndexSpace(self): - if self.module.import_section is not None: - for iter in self.module.import_section.import_entry: - if iter.kind == 1: - name = str() - for i in iter.field_str: - name += str(chr(i)) - self.machinestate.Index_Space_Table.append(name) - - if self.module.table_section is not None: - for iter in self.module.table_section.table_types: - self.machinestate.Index_Space_Table.append(iter.element_type) - - def InitializeLinearMemory(self): - # @DEVI-we could try to pack the data in the linear memory ourselve to - # decrease the machinestate size - if self.module.memory_section is None: - rsz_limits = Resizable_Limits() - self.module.memory_section = Memory_Section() - self.module.memory_section.memory_types = [rsz_limits] - self.module.memory_section.count = 1 - for iter in self.module.memory_section.memory_types: - 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 - count += 1 - - # returns the machinestate - def getInits(self): - return(self.machinestate) - - -# WIP-holds the run-rime data structures for a wasm machine -class RTE(): - def __init__(self): - Stack_Control_Flow = list() - Stack_Value = list() - Vector_Locals = list() - Current_Position = int() - Local_Stacks = list() - - def genFuncLocalStack(func_body): - pass - - -# palceholder for the class that holds the validation functions -class ModuleValidation(): - def __init__(self, module): - self.module = module - - def TypeSection(self): - pass - - def ImportSection(self): - pass - - def FunctionSection(self): - pass - - def TableSection(self): - pass - - def MemorySection(self): - pass - - def GlobalSection(self): - pass - - def ExportSection(self): - pass - - def StartSection(self): - pass - - def ElementSection(self): - pass - - def CodeSection(self): - pass - - def DataSection(self): - pass - - def TBCustom(self): - pass - - def ValidateAll(self): - self.TypeSection() - self.ImportSection() - self.FunctionSection() - self.TableSection() - self.MemorySection() - self.GlobalSection() - self.ExportSection() - self.StartSection() - self.ElementSection() - self.CodeSection() - self.DataSection() - self.TBCustom() - - return(True) - - -# a convinience class that handles the initialization of the wasm machine and -# interpretation of the code. -class VM(): - def __init__(self, modules): - self.modules = modules - self.machinestate = TBMachine() - # @DEVI-FIXME- the first implementation is single-module only - self.init = TBInit(self.modules[0], self.machinestate) - self.init.run() - self.machinestate = self.init.getInits() - self.start_function = Func_Body() - self.ins_cache = WASM_Ins() - self.executewasm = Execute(self.machinestate) - self.totGas = int() - self.metric = Metric(modules[0].code_section) - self.parseflags = None - - def setFlags(self, parseflags): - self.parseflags = parseflags - - def getState(self): - return(self.machinestate) - - def initLocalIndexSpace(self, local_count): - for i in range(0, local_count): - self.machinestate.Index_Space_Locals.append(0) - - 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) - else: - start_index = int(self.parseflags.entry) - else: - 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) - - def getStartFunctionBody(self): - start_index = self.getStartFunctionIndex() - if isinstance(start_index, int): - 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) - - def execute(self): - 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) - for ins in self.start_function.code: - self.executewasm.getInstruction(ins.opcodeint, ins.operands) - self.executewasm.callExecuteMethod() - self.getState() - - # pre-execution hook - def startHook(self): - if self.parseflags.metric: - for mem in self.modules[0].memory_section.memory_types: - 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) - - # 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) - if self.machinestate.Stack_Omni: - print(Colors.green + "stack top: " + repr(self.machinestate.Stack_Omni.pop()) + Colors.ENDC) - - # a convinience method - def run(self): - self.startHook() - self.getStartFunctionBody() - self.initLocalIndexSpace(self.start_function.local_count) - self.execute() - self.endHook() - - -# a wrapper class for VM. it timeouts instructions that take too long to -# execute. -class Judicator(): - def __int__(self, op_time_table, module): - self.op_time_table = op_time_table - self.vm = VM(modules) - self.vm.getStartFunctionBody() - - def overseer(): - # @DEVI- forking introduces a new source of non-determinism - pid = os.fork() - # child process - if pid == 0: - 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.') - else: - print('overseer child exited with non-zero.') - # pid < 0 - else: - raise Exception(Colors.red + 'could not fork judicator overseer.' + Colors.ENDC) - - def setup(self): - signal.signal(signal.SIGALRM, self.to_sighandler) - - def set_alarm(t): - signal.alaram(t) - - def to_sighandler(signum, frame): - print(Colors.red + "execution time out..." + Colors.ENDC) - raise Exception(Colors.red + "execution time out" + Colors.ENDC) - - def run(self): - self.setup() - self.set_alaram(10) - self.overseer() diff --git a/bruiser/wasm/execute.py b/bruiser/wasm/execute.py index af8455f..40b9042 100644 --- a/bruiser/wasm/execute.py +++ b/bruiser/wasm/execute.py @@ -1,4 +1,4 @@ -from OpCodes import * +from opcodes import * from utils import Colors, ror, rol import numpy as np import math diff --git a/bruiser/wasm/init.py b/bruiser/wasm/init.py new file mode 100644 index 0000000..f3ac986 --- /dev/null +++ b/bruiser/wasm/init.py @@ -0,0 +1,415 @@ +from utils import Colors, init_interpret, ParseFlags +from opcodes import WASM_OP_Code +from section_structs import Code_Section, Func_Body, WASM_Ins, Resizable_Limits, Memory_Section +from execute import * +import datetime as dti +import os +import sys +import signal + + +# McCabe cyclomatic complexity metric +class Metric(): + def __init__(self, code_section): + self.code_section = code_section + self.metric = [] + self.soc = [] + + def mccabe(self): + soc = 0 + Edges = 1 + Nodes = 1 + 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: + Nodes += 2 + Edges += 4 + elif ins.opcode == 3: + Nodes += 2 + Edges += 3 + else: + pass + + self.metric.append(Edges - Nodes + 1) + self.soc.append(soc) + soc = 0 + Edges = 1 + Nodes = 1 + + def getMcCabe(self): + return self.metric + + def getSOC(self): + return self.soc + + +# handles the debug option --memdump. dumps the contents of linear memories. +def DumpLinearMems(linear_memories, threshold): + count = int() + strrep = [] + linmem_cnt = int() + for lin_mem in linear_memories: + 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: + for ch in strrep: + # @DEVI-line feed messes the pretty format up + if ord(ch) != 10: + print(Colors.green + ' ' + ch + Colors.ENDC, end = '') + else: + pass + print() + strrep = [] + print(Colors.cyan + hex(count), ':\t' + Colors.ENDC, end='') + strrep.append(str(chr(byte))) + print(Colors.blue + format(byte, '02x') + ' ' + Colors.ENDC, end='') + else: + strrep += str(chr(byte)) + print(Colors.blue + format(byte, '02x') + ' ' + Colors.ENDC, end='') + count += 1 + count = 0 + print() + + +# handles the debug options --idxspc. dumps the index spaces. +def DumpIndexSpaces(machinestate): + 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) + for iter in machinestate.Index_Space_Global: + print(Colors.blue + repr(iter) + 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) + for iter in machinestate.Index_Space_Table: + print(Colors.blue + repr(iter) + Colors.ENDC) + print('-----------------------------------------') + + +# WIP-the Truebit Machine class +class TBMachine(): + def __init__(self): + # bytearray of size PAGE_SIZE + self.Linear_Memory = [] + self.Stack_Label = list() + self.Stack_Label_Height = int() + self.Stack_Control_Flow = list() + self.Stack_Call = list() + self.Stack_Value = list() + self.Stack_Omni = list() + self.Vector_Globals = list() + self.Index_Space_Function = list() + self.Index_Space_Global = list() + self.Index_Space_Linear = list() + self.Index_Space_Table = list() + self.Index_Space_Locals = list() + self.Index_Space_Label = list() + + +# handles the initialization of the WASM machine +class TBInit(): + def __init__(self, module, machinestate): + self.module = module + self.machinestate = machinestate + + # a convenience function that runs the methods of the class. all methods + # can be called separately manually as well. + def run(self): + self.InitFuncIndexSpace() + self.InitGlobalIndexSpace() + self.InitLinearMemoryIndexSpace() + self.InitTableIndexSpace() + self.InitializeLinearMemory() + + def InitFuncIndexSpace(self): + if self.module.import_section is not None: + for iter in self.module.import_section.import_entry: + if iter.kind == 0: + name = str() + for i in iter.field_str: + name += str(chr(i)) + self.machinestate.Index_Space_Function.append(name) + + if self.module.function_section is not None: + for iter in self.module.function_section.type_section_index: + self.machinestate.Index_Space_Function.append(iter) + + def InitGlobalIndexSpace(self): + if self.module.import_section is not None: + for iter in self.module.import_section.import_entry: + if iter.kind == 3: + name = str() + for i in iter.field_str: + name += str(chr(i)) + self.machinestate.Index_Space_Global.append(name) + + if self.module.global_section is not None: + for iter in self.module.global_section.global_variables: + self.machinestate.Index_Space_Global.append(iter.init_expr) + + def InitLinearMemoryIndexSpace(self): + if self.module.import_section is not None: + for iter in self.module.import_section.import_entry: + if iter.kind == 2: + name = str() + for i in iter.field_str: + name += str(chr(i)) + self.machinestate.Index_Space_Linear.append(name) + + if self.module.memory_section is not None: + for iter in self.module.memory_section.memory_types: + self.machinestate.Index_Space_Linear.append(iter.initial) + + def InitTableIndexSpace(self): + if self.module.import_section is not None: + for iter in self.module.import_section.import_entry: + if iter.kind == 1: + name = str() + for i in iter.field_str: + name += str(chr(i)) + self.machinestate.Index_Space_Table.append(name) + + if self.module.table_section is not None: + for iter in self.module.table_section.table_types: + self.machinestate.Index_Space_Table.append(iter.element_type) + + def InitializeLinearMemory(self): + # @DEVI-we could try to pack the data in the linear memory ourselve to + # decrease the machinestate size + if self.module.memory_section is None: + rsz_limits = Resizable_Limits() + self.module.memory_section = Memory_Section() + self.module.memory_section.memory_types = [rsz_limits] + self.module.memory_section.count = 1 + for iter in self.module.memory_section.memory_types: + 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 + count += 1 + + # returns the machinestate + def getInits(self): + return(self.machinestate) + + +# WIP-holds the run-rime data structures for a wasm machine +class RTE(): + def __init__(self): + Stack_Control_Flow = list() + Stack_Value = list() + Vector_Locals = list() + Current_Position = int() + Local_Stacks = list() + + def genFuncLocalStack(func_body): + pass + + +# palceholder for the class that holds the validation functions +class ModuleValidation(): + def __init__(self, module): + self.module = module + + def TypeSection(self): + pass + + def ImportSection(self): + pass + + def FunctionSection(self): + pass + + def TableSection(self): + pass + + def MemorySection(self): + pass + + def GlobalSection(self): + pass + + def ExportSection(self): + pass + + def StartSection(self): + pass + + def ElementSection(self): + pass + + def CodeSection(self): + pass + + def DataSection(self): + pass + + def TBCustom(self): + pass + + def ValidateAll(self): + self.TypeSection() + self.ImportSection() + self.FunctionSection() + self.TableSection() + self.MemorySection() + self.GlobalSection() + self.ExportSection() + self.StartSection() + self.ElementSection() + self.CodeSection() + self.DataSection() + self.TBCustom() + + return(True) + + +# a convinience class that handles the initialization of the wasm machine and +# interpretation of the code. +class VM(): + def __init__(self, modules): + self.modules = modules + self.machinestate = TBMachine() + # @DEVI-FIXME- the first implementation is single-module only + self.init = TBInit(self.modules[0], self.machinestate) + self.init.run() + self.machinestate = self.init.getInits() + self.start_function = Func_Body() + self.ins_cache = WASM_Ins() + self.executewasm = Execute(self.machinestate) + self.totGas = int() + self.metric = Metric(modules[0].code_section) + self.parseflags = None + + def setFlags(self, parseflags): + self.parseflags = parseflags + + def getState(self): + return(self.machinestate) + + def initLocalIndexSpace(self, local_count): + for i in range(0, local_count): + self.machinestate.Index_Space_Locals.append(0) + + 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) + else: + start_index = int(self.parseflags.entry) + else: + 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) + + def getStartFunctionBody(self): + start_index = self.getStartFunctionIndex() + if isinstance(start_index, int): + 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) + + def execute(self): + 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) + for ins in self.start_function.code: + self.executewasm.getInstruction(ins.opcodeint, ins.operands) + self.executewasm.callExecuteMethod() + self.getState() + + # pre-execution hook + def startHook(self): + if self.parseflags.metric: + for mem in self.modules[0].memory_section.memory_types: + 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) + + # 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) + if self.machinestate.Stack_Omni: + print(Colors.green + "stack top: " + repr(self.machinestate.Stack_Omni.pop()) + Colors.ENDC) + + # a convinience method + def run(self): + self.startHook() + self.getStartFunctionBody() + self.initLocalIndexSpace(self.start_function.local_count) + self.execute() + self.endHook() + + +# a wrapper class for VM. it timeouts instructions that take too long to +# execute. +class Judicator(): + def __int__(self, op_time_table, module): + self.op_time_table = op_time_table + self.vm = VM(modules) + self.vm.getStartFunctionBody() + + def overseer(): + # @DEVI- forking introduces a new source of non-determinism + pid = os.fork() + # child process + if pid == 0: + 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.') + else: + print('overseer child exited with non-zero.') + # pid < 0 + else: + raise Exception(Colors.red + 'could not fork judicator overseer.' + Colors.ENDC) + + def setup(self): + signal.signal(signal.SIGALRM, self.to_sighandler) + + def set_alarm(t): + signal.alaram(t) + + def to_sighandler(signum, frame): + print(Colors.red + "execution time out..." + Colors.ENDC) + raise Exception(Colors.red + "execution time out" + Colors.ENDC) + + def run(self): + self.setup() + self.set_alaram(10) + self.overseer() diff --git a/bruiser/wasm/opcodes.py b/bruiser/wasm/opcodes.py new file mode 100644 index 0000000..f7c9a1b --- /dev/null +++ b/bruiser/wasm/opcodes.py @@ -0,0 +1,332 @@ +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"} + +class RelocType(Enum): + R_WEBASSEMBLY_FUNCTION_INDEX_LEB = 0 + R_WEBASSEMBLY_TABLE_INDEX_SLEB = 1 + R_WEBASSEMBLY_TABLE_INDEX_I32 = 2 + R_WEBASSEMBLY_MEMORY_ADDR_LEB = 3 + R_WEBASSEMBLY_MEMORY_ADDR_SLEB = 4 + R_WEBASSEMBLY_MEMORY_ADDR_I32 = 5 + R_WEBASSEMBLY_TYPE_INDEX_LEB = 6 + R_WEBASSEMBLY_GLOBAL_INDEX_LEB = 7 + 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 + lebs = 3 + flot = 4 + dobl = 5 + +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} + +# holds the version 1.0 wasm opcodes and immediates +class WASM_OP_Code: + version_number = 0x01 + magic_number = 0x6d736100 + PAGE_SIZE = 65536 + uint8 = 1 + uint16 = 2 + uint32 = 4 + uint64 = 8 + varuint1 = 1 + varuint7 = 1 + varuint32 = 4 + varuint64 = 8 + varint1 = 1 + varint7 = 1 + varint32 = 4 + varint64 = 8 + 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)] + + 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_dict = dict(control_flow_ops) + 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_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_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_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_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_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_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_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_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_dict = dict(reinterpretations) + 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_dict = dict(section_code) + section_code_dict_rev = {v: k for k, v in section_code_dict.items()} diff --git a/bruiser/wasm/parse.py b/bruiser/wasm/parse.py index 05c6465..424a2b6 100755 --- a/bruiser/wasm/parse.py +++ b/bruiser/wasm/parse.py @@ -6,9 +6,9 @@ import sys import re from section_structs import * from utils import * -from OpCodes import * +from opcodes import * from copy import deepcopy -from TBInit import * +from init import * import readline import code import signal diff --git a/bruiser/wasm/utils.py b/bruiser/wasm/utils.py index 6f93a94..69393b4 100644 --- a/bruiser/wasm/utils.py +++ b/bruiser/wasm/utils.py @@ -1,4 +1,4 @@ -from OpCodes import * +from opcodes import * import numpy as np import struct as stc diff --git a/extra-tools/luatablegen.py b/extra-tools/luatablegen.py index dbf7435..aca41d3 100755 --- a/extra-tools/luatablegen.py +++ b/extra-tools/luatablegen.py @@ -205,7 +205,7 @@ class TbgParser(object): c_source.write(SETTER_GEN[0].replace("XXX", struct_name).replace("YYY", field_name)) c_source.write(SETTER_GEN[1].replace("XXX", struct_name)) if lua_type == "integer": dummy = "\tdummy->" + field_name + " = " + "luaL_checkinteger(__ls, 2);\n" - elif lua_type == "lightuserdata": dummy ="\tdummy->" + field_name + " = " + "luaL_checkudata(__ls, 2, "+'"'+struct_name+'"'+");\n" + elif lua_type == "lightuserdata": dummy ="\tdummy->" + field_name + " = " + "luaL_checkudata(__ls, 2, "+'"'+field_name+"_t"+'"'+");\n" elif lua_type == "number": dummy ="\tdummy->" + field_name + " = " + "luaL_checknumber(__ls, 2);\n" elif lua_type == "string": dummy ="\tdummy->" + field_name + " = " + "luaL_checkstring(__ls, 2);\n" elif lua_type == "boolean": pass @@ -255,13 +255,28 @@ class TbgParser(object): c_source.write("\n") def docgen_md(self, d_source, struct_name, field_names, field_types, lua_types): - d_source.write("## wasm tables method list:\n") + d_source.write("## " + "__" + struct_name + "__" + ":\n") + d_source.write("\n") + d_source.write("### " + "_" + "getter fields" + "_" + ":\n") for field_name,lua_type in zip(field_names, lua_types): d_source.write(struct_name + ":" + field_name + "()" + " -- ") - d_source.write(lua_type + "
" + "\n") + if lua_type == "lightuserdata": + d_source.write("return type: " + field_name + "_t" + "
" + "\n") + else: + d_source.write("return type: " + lua_type + "
" + "\n") + d_source.write("\n") + d_source.write("### " + "_" + "setter fields" + "_" + ":\n") for field_name,lua_type in zip(field_names, lua_types): d_source.write("set_" + struct_name + ":" + field_name + "()" + " -- ") - d_source.write(lua_type + "
" + "\n") + if lua_type == "lightuserdata": + d_source.write("arg type: " + field_name + "_t" + "
" + "\n") + else: + d_source.write("arg type: " + lua_type + "
" + "\n") + d_source.write("\n") + d_source.write("### " + "_" + "constructors" + "_" + ":\n") + d_source.write(struct_name + ":new() -- needs all the args
\n") + d_source.write(struct_name + "() -- lazy constructor
\n") + d_source.write("\n") d_source.write("\n") def luagen(self): @@ -293,6 +308,8 @@ class TbgParser(object): c_source = open(self.argparser.args.outfile, "w") if self.argparser.args.docpath: d_source = open(self.argparser.args.docpath, "w") + d_source.write("The lazy constructors are inside wasm.lua.\n") + d_source.write("```lua\nlocal wasm = require(\"wasm\")\n```\n") for k, v in self.tbg_file.items(): struct_name = k field_names = v['field_name'] @@ -373,6 +390,8 @@ class TbgParser(object): m_source = open(self.argparser.args.out + "/" + "tablegen.mk", "w") # generate lua module self.luagen() + if self.argparser.args.docpath: + d_source.write("_automatically generated by luatablegen._
\n") # write code here def premain(argparser): -- cgit v1.2.3