diff options
| author | bloodstalker <thabogre@gmail.com> | 2018-01-26 02:15:38 +0000 | 
|---|---|---|
| committer | bloodstalker <thabogre@gmail.com> | 2018-01-26 02:15:38 +0000 | 
| commit | c5bba9ae8121d250284ecd1ce69ffe344f04fea8 (patch) | |
| tree | 73406f955ac06644b24678858f8df7f11bc31896 | |
| parent | some tests for the ffi funcitonality on bruiuser (diff) | |
| download | mutator-c5bba9ae8121d250284ecd1ce69ffe344f04fea8.tar.gz mutator-c5bba9ae8121d250284ecd1ce69ffe344f04fea8.zip | |
fixed string type for xobjs, also xcall can now call functions by name as well
Diffstat (limited to '')
| -rw-r--r-- | bruiser/bruiser.cpp | 36 | ||||
| -rw-r--r-- | bruiser/bruiserffi.c | 12 | ||||
| -rw-r--r-- | bruiser/bruiserffi.h | 3 | ||||
| -rw-r--r-- | bruiser/executioner.h | 7 | ||||
| -rw-r--r-- | bruiser/lua-scripts/demo1.lua | 22 | 
5 files changed, 61 insertions, 19 deletions
| diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index 1eff772..a052b64 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -1259,7 +1259,17 @@ class LuaWrapper        int argc = lua_tointeger(__ls, 1);        // 2-table of strings        std::string ffi_ret_type_string = lua_tostring(__ls, 3); -      int x_index = lua_tointeger(__ls, 4); + +      void* x_ptr;  +      if (lua_type(__ls, 4) == LUA_TNUMBER) { +        x_ptr = executioner.getvptrbyindex(lua_tointeger(__ls, 4)).first; +      } else if (lua_type(__ls, 4) == LUA_TSTRING) { +        x_ptr = executioner.getvptrbyname(lua_tostring(__ls, 4)).first; +      } else { +        PRINT_WITH_COLOR_LB(RED, "argument 4 is neihter an index nor a string."); +        lua_pushnil(__ls); +        return 1; +      }        // 5-the actual args-table of values        // @DEVI-FIXME: currently we are not handling structs at all @@ -1275,6 +1285,7 @@ class LuaWrapper        else if (std::strcmp(ffi_ret_type_string.c_str(), "sint64") == 0) {ret_type = ffi_type_sint64;}        else if (std::strcmp(ffi_ret_type_string.c_str(), "float") == 0) {ret_type = ffi_type_float;}        else if (std::strcmp(ffi_ret_type_string.c_str(), "double") == 0) {ret_type = ffi_type_double;} +      else if (std::strcmp(ffi_ret_type_string.c_str(), "string") == 0) {ret_type = ffi_type_pointer;}        else if (std::strcmp(ffi_ret_type_string.c_str(), "pointer") == 0) {ret_type = ffi_type_pointer;}        else if (std::strcmp(ffi_ret_type_string.c_str(), "struct") == 0) {ret_type = ffi_type_pointer;}        else {PRINT_WITH_COLOR_LB(RED, "unknown return type string.");return 0; @@ -1292,7 +1303,6 @@ class LuaWrapper        for (int i = 1; i <= table_length_2; ++i) {          lua_rawgeti(__ls, 2, i);          args[i-1] = lua_tostring(__ls, i + numargs); -        //std::cout << YELLOW << args[i-1] << NORMAL << "\n";        }        std::list<uint64_t> uints; @@ -1319,8 +1329,8 @@ class LuaWrapper        std::cout << CYAN << "table_length: " << table_length_5 << NORMAL << "\n";        for (int i = 1; i <= table_length_5; ++i) {          lua_rawgeti(__ls, 5, i); -        if (lua_type(__ls, i) == LUA_TBOOLEAN) {} -        else if (lua_type(__ls, i) == LUA_TLIGHTUSERDATA) {} +        if (lua_type(__ls, i+numargs+argc) == LUA_TBOOLEAN) {} +        else if (lua_type(__ls, i+numargs+argc) == LUA_TLIGHTUSERDATA) {}          else if (lua_type(__ls, i+numargs+argc) == LUA_TNUMBER) {            double dummy = lua_tonumber(__ls, i + numargs + argc);            if (dummy == (long long int)dummy) { // FIXME @@ -1332,21 +1342,22 @@ class LuaWrapper              values[i-1]=&doubles.back();            } // float          } -        else if (lua_type(__ls, i) == LUA_TSTRING) {} -        else if (lua_type(__ls, i) == LUA_TTABLE) {} -        else if (lua_type(__ls, i) == LUA_TFUNCTION) {} -        else if (lua_type(__ls, i) == LUA_TUSERDATA) {} -        else if (lua_type(__ls, i) == LUA_TTHREAD) {} +        else if (lua_type(__ls, i+numargs+argc) == LUA_TSTRING) { +          strings.push_back(lua_tostring(__ls, i + numargs + argc)); +          values[i-1]=&strings.back(); +        } +        else if (lua_type(__ls, i+numargs+argc) == LUA_TTABLE) {} +        else if (lua_type(__ls, i+numargs+argc) == LUA_TFUNCTION) {} +        else if (lua_type(__ls, i+numargs+argc) == LUA_TUSERDATA) {} +        else if (lua_type(__ls, i+numargs+argc) == LUA_TTHREAD) {}        } -      auto x_ptr = executioner.getvptrbyindex(x_index).first;        void* result;        if (x_ptr != nullptr) { -        std::cout << "calling xobj named " << GREEN << executioner.getvptrbyindex(x_index).second << NORMAL << "\n";          result = ffi_callX(argc, args, ret_type, x_ptr, values);          if (result == nullptr) {PRINT_WITH_COLOR_LB(RED, "ffi_callX returned null.");return 0;} -        if (std::strcmp(ffi_ret_type_string.c_str(), "void") == 0) {return 0;} +        if (std::strcmp(ffi_ret_type_string.c_str(), "void") == 0) {lua_pushnil(__ls);}          else if (std::strcmp(ffi_ret_type_string.c_str(), "uint8") == 0) {lua_pushinteger(__ls, ffi_reinterpret_uint8_t(result));}          else if (std::strcmp(ffi_ret_type_string.c_str(), "sint8") == 0) {lua_pushinteger(__ls, ffi_reinterpret_int8_t(result));}          else if (std::strcmp(ffi_ret_type_string.c_str(), "uint16") == 0) {lua_pushinteger(__ls, ffi_reinterpret_uint16_t(result));} @@ -1357,6 +1368,7 @@ class LuaWrapper          else if (std::strcmp(ffi_ret_type_string.c_str(), "sint64") == 0) {lua_pushinteger(__ls, ffi_reinterpret_int64_t(result));}          else if (std::strcmp(ffi_ret_type_string.c_str(), "float") == 0) {lua_pushnumber(__ls, ffi_reinterpret_float(result));}          else if (std::strcmp(ffi_ret_type_string.c_str(), "double") == 0) {lua_pushnumber(__ls, ffi_reinterpret_double(result));} +        else if (std::strcmp(ffi_ret_type_string.c_str(), "string") == 0) {lua_pushstring(__ls, ffi_reinterpret_string(result));}          else if (std::strcmp(ffi_ret_type_string.c_str(), "pointer") == 0) {lua_pushinteger(__ls, ffi_reinterpret_uintptr_t(result));}          else if (std::strcmp(ffi_ret_type_string.c_str(), "struct") == 0) {}          else {PRINT_WITH_COLOR_LB(RED, "unknown return type string.");return 0;} diff --git a/bruiser/bruiserffi.c b/bruiser/bruiserffi.c index 3594af7..454e3d7 100644 --- a/bruiser/bruiserffi.c +++ b/bruiser/bruiserffi.c @@ -45,8 +45,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.*    X(int32_t, "uint8_t")\    X(int64_t, "uint8_t")\    X(uintptr_t, "uint8_t")\ -  //X(float, "uint8_t")\ -  X(double, "uint8_t")  #define X(X1,X2) REINTERPRET_GENERATOR(X1)  X_LIST_GEN @@ -55,6 +53,7 @@ X_LIST_GEN  #undef REINTERPRET_GENERATOR  float ffi_reinterpret_float(void* result) {return *(float*)&result;}  double ffi_reinterpret_double(void* result) {return *(double*)&result;} +char* ffi_reinterpret_string(void* result) {return (char*)result;}  void ffi_value_ctor(void** ret, int argc, ...) {    va_list value_list; @@ -128,6 +127,7 @@ ffi_type* ffi_type_ctor(const char* arg_string) {    else if (strcmp(arg_string, "float") == 0) {return &ffi_type_float;}    else if (strcmp(arg_string, "double") == 0) {return &ffi_type_double;}    else if (strcmp(arg_string, "pointer") == 0) {return &ffi_type_pointer;} +  else if (strcmp(arg_string, "string") == 0) {return &ffi_type_pointer;}    // @DEVI-FIXME: currently we are not handling structs at all    else if (strcmp(arg_string, "struct") == 0) {return &ffi_type_pointer;}    else { @@ -171,11 +171,13 @@ void* ffi_callX_var(int argc, const char** arg_string, ffi_type rtype, void* x_p  uint32_t add2(uint32_t a, uint32_t b) {return a+b;}  uint32_t sub2(uint32_t a, uint32_t b) {return a-b;}  double addd(double a, double b) {return a+b;} +char* passthrough(char* a) {return a;}  #pragma weak main  int main(int argc, char** argv) {    void* padd = &add2;    void* psub = &sub2;    void* padd2 = &addd; +  void* pstring = &passthrough;    int argcount = 2;    ffi_type ret_type = ffi_type_uint32;    const char* args[] = {"uint32", "uint32"}; @@ -197,6 +199,12 @@ int main(int argc, char** argv) {    void* values2[] = {&c, &d};    result = ffi_callX(argcount, args2, ret_type, padd2, values2);    fprintf(stdout, "result of calling addd is %f\n", ffi_reinterpret_double(result)); +  const char* args3[] = {"string"}; +  char* dummy = "i live!"; +  void* values3[] = {&dummy}; +  result = ffi_callX(1, args3, ffi_type_pointer, pstring, values3); +  fprintf(stdout, "result of calling passthrough is %s\n", ffi_reinterpret_string(result)); +    return 0;  }  /**********************************************************************************************************************/ diff --git a/bruiser/bruiserffi.h b/bruiser/bruiserffi.h index e9b6b27..c7a2821 100644 --- a/bruiser/bruiserffi.h +++ b/bruiser/bruiserffi.h @@ -41,8 +41,6 @@ extern "C" {    X(int32_t, "uint8_t")\    X(int64_t, "uint8_t")\    X(uintptr_t, "uint8_t")\ -  //X(float, "uint8_t")\ -  X(double, "uint8_t")  #define X(X1,X2) REINTERPRET_GENERATOR(X1)  X_LIST_GEN @@ -51,6 +49,7 @@ X_LIST_GEN  #undef REINTERPRET_GENERATOR  float ffi_reinterpret_float(void* result);  double ffi_reinterpret_double(void* result); +char* ffi_reinterpret_string(void* result);    /**     * @brief constructs the arguments to be passed to ffi_call. diff --git a/bruiser/executioner.h b/bruiser/executioner.h index 5c04be1..4793915 100644 --- a/bruiser/executioner.h +++ b/bruiser/executioner.h @@ -235,6 +235,12 @@ class Executioner {        }        return std::make_pair(nullptr, "");      } +    std::pair<void*, std::string> getvptrbyname(const char* name) { +      for (auto &iter : vptrs) { +        if (std::strcmp(name, iter.second.c_str()) == 0) return iter; +      } +      return std::make_pair(nullptr, ""); +    }    private:      std::vector<std::pair<void*, size_t>> obj_mem_ptrs; @@ -328,6 +334,7 @@ class XGlobals {      XGlobals() {}      ~XGlobals() {}    private: +    std::list<std::pair<void*, size_t>> globals;  };  /**********************************************************************************************************************/  /**********************************************************************************************************************/ diff --git a/bruiser/lua-scripts/demo1.lua b/bruiser/lua-scripts/demo1.lua index 75778d2..5ef6bd2 100644 --- a/bruiser/lua-scripts/demo1.lua +++ b/bruiser/lua-scripts/demo1.lua @@ -152,6 +152,8 @@ function main()    local adddouble_code = codeTableByName_number("'adddouble'")    local subdouble_code = codeTableByName_number("'subdouble'")    local triple_code = codeTableByName_number("'triple'") +  local quad_code = codeTableByName_number("'quad'") +  local passthrough_code = codeTableByName_number("'passthrough'")    printFuncSizes() @@ -161,6 +163,9 @@ function main()    xobjregister(adddouble_code, "adddouble")    xobjregister(subdouble_code, "subdouble")    xobjregister(triple_code, "triple") +  xobjregister(quad_code, "quad") +  xobjregister(passthrough_code, "passthrough") +    a=xcall(2,{"uint32","uint32"},"uint32",0, {30,20})    print("call add result", a)    a=xcall(2,{"uint32", "uint32"},"uint32",1, {30,20}) @@ -170,17 +175,28 @@ function main()    arg2 = 200    a=xcall(2,{"sint32", "sint32"},"sint32",1, {arg1,arg2})    print("xcall returned:",a) +    if a ~= -100 then print("test failed") end    a=xcall(2,{"double", "double"},"double",2, {333.333,222.222})    print("xcall returned:",a)    -- FIXME -  if a ~= 555.555 then print("test failed") end +  if tostring(a) ~= tostring(555.555) then print("test failed") end    a=xcall(2,{"double", "double"},"double",3, {333.333,222.222})    print("xcall returned:",a) -  if a ~= 111.111 then print("test failed") end +  if tostring(a) ~= tostring(111.111) then print("test failed") end +    a=xcall(3,{"double", "double", "double"},"double",4, {333.333,222.222,111.111})    print("xcall returned:",a) -  if a ~= 666.666 then print("test failed") end +  a=xcall(3,{"double", "double", "double"},"double","triple", {333.333,222.222,111.111}) +  print("xcall returned:",a) +  if tostring(a) ~= tostring(666.666) then print("test failed") end + +  --a=xcall(4,{"sint32", "sint32", "sint32", "sint32"},"sint32",5, {10,20,30,40}) +  --print("xcall returned:",a) +  --if a ~= 100 then print("test failed") end + +  a=xcall(1,{"string"},"string","passthrough", {"i live!"}) +  print("xcall returned:",a)  end  main() | 
