Skip to content

Commit

Permalink
added lua lm_module_t and LM_FindModule
Browse files Browse the repository at this point in the history
  • Loading branch information
rdbo committed Dec 5, 2023
1 parent d2f274c commit adcc06c
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
19 changes: 19 additions & 0 deletions bindings/lua/src/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,33 @@ int lua_LM_FindProcess(lua_State *L)
return 1;
}

int lua_LM_FindModule(lua_State *L)
{
lm_module_t *udata;
const char *modname;

modname = luaL_checkstring(L, 1);

lua_create_lm_module(L);
udata = (lm_module_t *)lua_touserdata(L, lua_gettop(L));

if (LM_FindModule((lm_string_t)modname, udata) != LM_TRUE) {
lua_pushnil(L);
}

return 1;
}

int luaopen_libmem_lua(lua_State *L)
{
luaL_Reg functions[] = {
{ "LM_FindProcess", lua_LM_FindProcess },
{ "LM_FindModule", lua_LM_FindModule },
{ NULL, NULL }
};

lua_define_lm_process(L);
lua_define_lm_module(L);

luaL_register(L, "libmem_lua", functions);

Expand Down
51 changes: 50 additions & 1 deletion bindings/lua/src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
return 1; \
}
#define LM_PROCESS_META "lm_process_t"
#define LM_MODULE_META "lm_module_t"

/****************
* lm_process_t *
***************/

int lua_lm_process_index(lua_State *L)
{
Expand Down Expand Up @@ -66,7 +71,7 @@ int lua_lm_process_tostring(lua_State *L)

void lua_create_lm_process(lua_State *L)
{
lm_process_t *udata = lua_newuserdata(L, sizeof(lm_process_t));
lm_process_t *udata = (lm_process_t *)lua_newuserdata(L, sizeof(lm_process_t));
luaL_getmetatable(L, LM_PROCESS_META);
lua_setmetatable(L, lua_gettop(L) - 1);
}
Expand All @@ -79,3 +84,47 @@ void lua_define_lm_process(lua_State *L)
lua_pushcfunction(L, lua_lm_process_tostring);
lua_setfield(L, lua_gettop(L) - 1, "__tostring");
}

/***************
* lm_module_t *
***************/

int lua_lm_module_index(lua_State *L)
{
lm_module_t *udata = (lm_module_t *)luaL_checkudata(L, 1, LM_MODULE_META);
const char *entry = luaL_checkstring(L, 2);

LUA_ENTRY_INT(base)
LUA_ENTRY_INT(end)
LUA_ENTRY_INT(size)
LUA_ENTRY_STR(path)
LUA_ENTRY_STR(name)

lua_pushnil(L);
return 1;
}

int lua_lm_module_tostring(lua_State *L)
{
lm_module_t *udata = (lm_module_t *)luaL_checkudata(L, 1, LM_MODULE_META);

lua_pushfstring(L, "lm_module_t(base: %p size: %p end: %p path: \"%s\" name: \"%s\")", udata->base, udata->size, udata->end, udata->path, udata->name);

return 1;
}

void lua_create_lm_module(lua_State *L)
{
lm_module_t *udata = (lm_module_t *)lua_newuserdata(L, sizeof(lm_module_t));
luaL_getmetatable(L, LM_MODULE_META);
lua_setmetatable(L, lua_gettop(L) - 1);
}

void lua_define_lm_module(lua_State *L)
{
luaL_newmetatable(L, LM_MODULE_META);
lua_pushcfunction(L, lua_lm_module_index);
lua_setfield(L, lua_gettop(L) - 1, "__index");
lua_pushcfunction(L, lua_lm_module_tostring);
lua_setfield(L, lua_gettop(L) - 1, "__tostring");
}
1 change: 1 addition & 0 deletions bindings/lua/src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
void lua_define_##type(lua_State *L);

DECLTYPE(lm_process)
DECLTYPE(lm_module)

#endif
16 changes: 16 additions & 0 deletions bindings/lua/tests/test.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
print("Lua Tests")

function separator()
print("====================")
end

local libmem = require("libmem_lua")
print("libmem_lua loaded")

Expand All @@ -13,3 +17,15 @@ print("Path: " .. target_proc.path)
print("Name: " .. target_proc.name)
print("(invalid): " .. (target_proc.invalid or "nil"))
print(target_proc)

separator()

mod = libmem.LM_FindModule("liblibmem.so")
print("[*] Find Module")
print("Base: " .. mod.base)
print("Size: " .. mod.size)
print("End: " .. mod["end"]) -- NOTE: mod.end won't work!
print("Path: " .. mod.path)
print("Name: " .. mod.name)
print("(invalid): " .. (mod.invalid or "nil"))
print(mod)

0 comments on commit adcc06c

Please sign in to comment.