1414#include " emmy_debugger/proto/socket_server_transporter.h"
1515#include " shared/shme.h"
1616
17+
1718typedef TRACED_HOOK_HANDLE HOOK_HANDLE;
1819typedef NTSTATUS HOOK_STATUS;
1920
@@ -28,7 +29,11 @@ typedef int (*_lua_pcall)(lua_State* L, int nargs, int nresults, int errfunc);
2829
2930typedef int (*_lua_pcallk)(lua_State* L, int nargs, int nresults, int errfunc, lua_KContext ctx, lua_KFunction k);
3031
31- typedef int (*_lua_resume)(lua_State* L, lua_State* from, int nargs, int * nresults);
32+ typedef int (*_lua_resume_54)(lua_State* L, lua_State* from, int nargs, int * nresults);
33+
34+ typedef int (*_lua_resume_53_52)(lua_State* L, lua_State* from, int narg);
35+
36+ typedef int (*_lua_resume_51)(lua_State* L, int narg);
3237
3338typedef HMODULE (WINAPI *LoadLibraryExW_t)(LPCWSTR lpFileName, HANDLE hFile, DWORD dwFlags);
3439
@@ -91,15 +96,33 @@ int lua_error_worker(lua_State* L)
9196 return error (L);
9297}
9398
94- int lua_resume_worker (lua_State* L, lua_State* from, int nargs, int * nresults)
99+ int lua_resume_worker_54 (lua_State* L, lua_State* from, int nargs, int * nresults)
95100{
96101 LPVOID lp;
97102 LhBarrierGetCallback (&lp);
98- const auto luaResume = (_lua_resume )lp;
103+ const auto luaResume = (_lua_resume_54 )lp;
99104 EmmyFacade::Get ().Attach (L);
100105 return luaResume (L, from, nargs, nresults);
101106}
102107
108+ int lua_resume_worker_53_52 (lua_State* L, lua_State* from, int nargs)
109+ {
110+ LPVOID lp;
111+ LhBarrierGetCallback (&lp);
112+ const auto luaResume = (_lua_resume_53_52)lp;
113+ EmmyFacade::Get ().Attach (L);
114+ return luaResume (L, from, nargs);
115+ }
116+
117+ int lua_resume_worker_51 (lua_State* L, int nargs)
118+ {
119+ LPVOID lp;
120+ LhBarrierGetCallback (&lp);
121+ const auto luaResume = (_lua_resume_51)lp;
122+ EmmyFacade::Get ().Attach (L);
123+ return luaResume (L, nargs);
124+ }
125+
103126#define HOOK (FN, WORKER, REQUIRED ) {\
104127 const auto it = symbols.find (" " #FN" " );\
105128 if (it != symbols.end ()) {\
@@ -112,6 +135,8 @@ int lua_resume_worker(lua_State* L, lua_State* from, int nargs, int* nresults)
112135 }\
113136}
114137
138+ #define EXIST_SYMBOL (FN ) (symbols.find(" " #FN" " ) != symbols.end())
139+
115140void HookLuaFunctions (std::unordered_map<std::string, DWORD64>& symbols)
116141{
117142 if (symbols.empty ())
@@ -121,7 +146,20 @@ void HookLuaFunctions(std::unordered_map<std::string, DWORD64>& symbols)
121146 // lua 5.2
122147 HOOK (lua_pcallk, lua_pcallk_worker, false );
123148 // HOOK(lua_error, lua_error_worker, true);
124- HOOK (lua_resume, lua_resume_worker, false );
149+
150+ // lua5.4
151+ if (EXIST_SYMBOL (lua_newuserdatauv))
152+ {
153+ HOOK (lua_resume, lua_resume_worker_54, false );
154+ }
155+ else if (EXIST_SYMBOL (lua_rotate) || EXIST_SYMBOL (lua_callk)) // lua5.3 lua5.2
156+ {
157+ HOOK (lua_resume, lua_resume_worker_53_52, false );
158+ }
159+ else // lua5.1 or luajit
160+ {
161+ HOOK (lua_resume, lua_resume_worker_51, false );
162+ }
125163}
126164
127165void LoadSymbolsRecursively (HANDLE hProcess, HMODULE hModule)
0 commit comments