Skip to content

Commit a13d66a

Browse files
committed
emmy hook
1 parent b6cb0fe commit a13d66a

28 files changed

+2928
-83
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ macro(source_group_by_dir proj_dir source_files)
4040
endif(MSVC OR APPLE)
4141
endmacro(source_group_by_dir)
4242

43-
option(EMMY_BUILD_AS_HOOK "build as hook mode" OFF)
4443
set(EMMY_LUA_VERSION "lua53")
4544

4645
if(${EMMY_LUA_VERSION} STREQUAL "lua53")

Shared/shme.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "shme.h"
2+
3+
bool CreateMemFile(SharedFile* file) {
4+
// Get a handle to our file map
5+
const auto MapFile = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, SHMEMSIZE, SHMEMNAME);
6+
if (MapFile == nullptr) {
7+
MessageBoxA(nullptr, "Failed to create file mapping!", "DLL_PROCESS_ATTACH", MB_OK | MB_ICONERROR);
8+
return false;
9+
}
10+
11+
// Get our shared memory pointer
12+
const auto MemFile = MapViewOfFile(MapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
13+
if (MemFile == nullptr) {
14+
MessageBoxA(nullptr, "Failed to map shared memory!", "DLL_PROCESS_ATTACH", MB_OK | MB_ICONERROR);
15+
return false;
16+
}
17+
file->lpMemFile = MemFile;
18+
file->hMapFile = MapFile;
19+
return true;
20+
}
21+
22+
bool CloseMemFile(SharedFile* file) {
23+
UnmapViewOfFile(file->lpMemFile);
24+
CloseHandle(file->hMapFile);
25+
return true;
26+
}
27+
28+
bool ReadSharedData(TSharedData& data) {
29+
SharedFile file = {};
30+
if (!CreateMemFile(&file)) {
31+
return false;
32+
}
33+
memcpy(&data, file.lpMemFile, SHMEMSIZE);
34+
// Clean up
35+
CloseMemFile(&file);
36+
return true;
37+
}
38+
39+
bool WriteSharedData(HANDLE hMapFile, LPVOID lpMemFile, TSharedData& data) {
40+
memset(lpMemFile, 0, SHMEMSIZE);
41+
memcpy(lpMemFile, &data, sizeof(TSharedData));
42+
return true;
43+
}

Shared/shme.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef _SHME_H_
2+
#define _SHME_H_
3+
4+
#include <Windows.h>
5+
6+
// Data struct to be shared between processes
7+
struct TSharedData {
8+
DWORD dwOffset = 0;
9+
HMODULE hModule = nullptr;
10+
LPDWORD lpInit = nullptr;
11+
};
12+
13+
struct SharedFile {
14+
HANDLE hMapFile;
15+
LPVOID lpMemFile;
16+
};
17+
18+
// Size (in bytes) of data to be shared
19+
#define SHMEMSIZE sizeof(TSharedData)
20+
// Name of the shared file map (NOTE: Global namespaces must have the SeCreateGlobalPrivilege privilege)
21+
#define SHMEMNAME "InjectedDllName_SHMEM"
22+
23+
bool CreateMemFile(SharedFile* file);
24+
25+
bool CloseMemFile(SharedFile* file);
26+
27+
bool ReadSharedData(TSharedData& data);
28+
29+
bool WriteSharedData(HANDLE hMapFile, LPVOID lpMemFile, TSharedData& data);
30+
#endif

emmy_core/CMakeLists.txt

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,19 @@ option(EMMY_USE_LUA_SOURCE "Build with lua source" OFF)
88
set(LINK_LIBRARIES uv_a)
99

1010
file(GLOB SRC_LIST
11-
*.cpp *.c proto/*.cpp
11+
*.cpp proto/*.cpp
1212
)
1313
file(GLOB HEADER_LIST
1414
*.h proto/*.h
1515
)
16+
file(GLOB HOOK_LIST
17+
dllmain.cpp hook/*.h hook/*.cpp
18+
hook/libpe/*.h hook/libpe/*.cpp
19+
)
20+
file(GLOB SHARED_LIST
21+
${CMAKE_SOURCE_DIR}/Shared/*.h
22+
${CMAKE_SOURCE_DIR}/Shared/*.cpp
23+
)
1624

1725
# rapid json
1826
add_definitions(-DRAPIDJSON_HAS_STDSTRING)
@@ -21,10 +29,6 @@ if(EMMY_CORE_BUILD_AS_DLL)
2129
add_definitions(-DEMMY_CORE_BUILD_AS_DLL)
2230
endif(EMMY_CORE_BUILD_AS_DLL)
2331

24-
if(EMMY_BUILD_AS_HOOK)
25-
add_definitions(-DEMMY_BUILD_AS_HOOK)
26-
endif(EMMY_BUILD_AS_HOOK)
27-
2832
if(EMMY_USE_LUA_SOURCE)
2933
add_definitions(-DEMMY_USE_LUA_SOURCE)
3034
include_directories(
@@ -34,35 +38,40 @@ if(EMMY_USE_LUA_SOURCE)
3438
else(EMMY_USE_LUA_SOURCE)
3539
file(GLOB SRC_LIST
3640
*.cpp *.c api/*.cpp proto/*.cpp
37-
hook/*.cpp
3841
)
3942
file(GLOB HEADER_LIST
4043
*.h api/*.h proto/*.h
41-
hook/*.h
4244
)
4345
endif(EMMY_USE_LUA_SOURCE)
4446

4547
include_directories(
48+
${CMAKE_SOURCE_DIR}/Shared
4649
${CMAKE_SOURCE_DIR}/third-party/libuv-1.29.0/include
4750
${CMAKE_SOURCE_DIR}/third-party/rapidjson-1.1.0/include
4851
${CMAKE_SOURCE_DIR}/third-party/EasyHook/EasyHookDll
4952
)
5053

5154
source_group_by_dir(${CMAKE_CURRENT_SOURCE_DIR} SRC_LIST)
5255
source_group_by_dir(${CMAKE_CURRENT_SOURCE_DIR} HEADER_LIST)
56+
source_group_by_dir(${CMAKE_CURRENT_SOURCE_DIR} HOOK_LIST)
57+
source_group("SHME" FILES SHARED_LIST)
5358

5459
add_library(emmy_core MODULE ${SRC_LIST} ${HEADER_LIST})
5560
set_target_properties(emmy_core PROPERTIES PREFIX "")
61+
add_library(emmy_hook MODULE ${SRC_LIST} ${HEADER_LIST} ${HOOK_LIST} ${SHARED_LIST})
62+
target_compile_definitions(emmy_hook PRIVATE EMMY_BUILD_AS_HOOK)
5663
if(WIN32)
57-
add_dependencies(emmy_core EasyHookDll uv_a)
58-
target_link_libraries(emmy_core EasyHookDll uv_a)
64+
add_dependencies(emmy_core uv_a)
65+
target_link_libraries(emmy_core uv_a)
66+
add_dependencies(emmy_hook EasyHook uv_a)
67+
target_link_libraries(emmy_hook EasyHook uv_a)
5968
else(WIN32)
6069
add_dependencies(emmy_core ${LINK_LIBRARIES})
6170
target_link_libraries(emmy_core ${LINK_LIBRARIES})
6271
endif(WIN32)
6372

6473
install(
65-
TARGETS emmy_core
66-
LIBRARY DESTINATION lib
74+
TARGETS emmy_core emmy_hook
75+
LIBRARY DESTINATION bin
6776
RUNTIME DESTINATION bin
6877
)

emmy_core/api/lua_api.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,6 @@ extern "C" bool SetupLuaAPI() {
257257
luaVersion = LuaVersion::LUA_51;
258258
LUA_REGISTRYINDEX = -10000;
259259
}
260-
//printf("[EMMY]lua version: %d\n", luaVersion);
260+
printf("[EMMY]lua version: %d\n", luaVersion);
261261
return true;
262262
}
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,40 @@
1414
* limitations under the License.
1515
*/
1616

17+
#if EMMY_BUILD_AS_HOOK
18+
19+
#include "hook/emmy_hook.h"
20+
1721
#if WIN32
1822
#include <Windows.h>
23+
#include "shme.h"
24+
static SharedFile file;
1925

2026
HINSTANCE g_hInstance = NULL;
2127

22-
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD reason, LPVOID reserved) {
23-
g_hInstance = hInstance;
28+
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD reason, LPVOID reserved) {
29+
g_hInstance = hModule;
2430

2531
if (reason == DLL_PROCESS_ATTACH) {
26-
// MessageBox(NULL, "Waiting to attach the debugger", NULL, MB_OK);
32+
TSharedData data;
33+
DisableThreadLibraryCalls(hModule);
34+
if (!CreateMemFile(&file)) {
35+
return FALSE;
36+
}
37+
// Set shared memory to hold what our remote process needs
38+
memset(file.lpMemFile, 0, SHMEMSIZE);
39+
data.hModule = hModule;
40+
data.lpInit = LPDWORD(StartupHookMode);
41+
data.dwOffset = DWORD(data.lpInit) - DWORD(data.hModule);
42+
memcpy(file.lpMemFile, &data, sizeof(TSharedData));
2743
}
2844
else if (reason == DLL_PROCESS_DETACH) {
2945
// Destroy();
46+
CloseMemFile(&file);
3047
}
3148

3249
return TRUE;
3350
}
3451
#endif
52+
53+
#endif

emmy_core/emmy_core.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ int stop(lua_State* L) {
9090
}
9191

9292
int gc(lua_State* L) {
93-
EmmyFacade::Get()->Destroy();
93+
EmmyFacade::Get()->OnLuaStateGC(L);
9494
return 0;
9595
}
9696

@@ -132,18 +132,22 @@ LuaVersion luaVersion = LuaVersion::UNKNOWN;
132132
extern "C" {
133133
bool SetupLuaAPI();
134134

135-
EMMY_CORE_EXPORT int luaopen_emmy_core(struct lua_State* L) {
135+
bool install_emmy_core(struct lua_State* L) {
136136
#ifndef EMMY_USE_LUA_SOURCE
137-
if (!SetupLuaAPI()) {
138-
return 0;
139-
}
137+
if (!SetupLuaAPI()) {
138+
return false;
139+
}
140140
#endif
141+
// register helper lib
142+
luaopen_emmy_helper(L);
143+
handleStateClose(L);
144+
return true;
145+
}
141146

142-
// register helper lib
143-
luaopen_emmy_helper(L);
144-
handleStateClose(L);
145-
luaL_newlib(L, lib);
146-
147-
return 1;
148-
}
147+
EMMY_CORE_EXPORT int luaopen_emmy_core(struct lua_State* L) {
148+
if (!install_emmy_core(L))
149+
return false;
150+
luaL_newlib(L, lib);
151+
return 1;
152+
}
149153
}

emmy_core/emmy_core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ enum class LuaVersion {
4545
LUA_53 = 53
4646
};
4747

48-
extern LuaVersion luaVersion;
48+
extern LuaVersion luaVersion;

emmy_core/emmy_facade.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ void EmmyFacade::Destroy() {
152152
void EmmyFacade::OnReceiveMessage(const rapidjson::Document& document) {
153153
const auto cmd = static_cast<MessageCMD>(document["cmd"].GetInt());
154154
switch (cmd) {
155+
#if EMMY_BUILD_AS_HOOK
156+
case MessageCMD::StartHookReq:
157+
StartHook();
158+
break;
159+
#endif
155160
case MessageCMD::InitReq:
156161
OnInitReq(document);
157162
break;
@@ -165,11 +170,11 @@ void EmmyFacade::OnReceiveMessage(const rapidjson::Document& document) {
165170
OnRemoveBreakPointReq(document);
166171
break;
167172
case MessageCMD::ActionReq:
168-
assert(isIDEReady);
173+
//assert(isIDEReady);
169174
OnActionReq(document);
170175
break;
171176
case MessageCMD::EvalReq:
172-
assert(isIDEReady);
177+
//assert(isIDEReady);
173178
OnEvalReq(document);
174179
break;
175180
default:
@@ -359,3 +364,32 @@ void EmmyFacade::OnEvalResult(EvalContext* context) {
359364
transporter->Send(int(MessageCMD::EvalRsp), rspDoc);
360365
delete context;
361366
}
367+
368+
void EmmyFacade::SendLog(LogType type, const char *fmt, ...) {
369+
va_list args;
370+
va_start(args, fmt);
371+
char buff[1024] = { 0 };
372+
vsnprintf_s(buff, 1024, fmt, args);
373+
va_end(args);
374+
375+
const std::string msg = buff;
376+
377+
rapidjson::Document rspDoc;
378+
rspDoc.SetObject();
379+
auto& allocator = rspDoc.GetAllocator();
380+
rspDoc.AddMember("type", (int)type, allocator);
381+
rspDoc.AddMember("message", msg, allocator);
382+
if (transporter)
383+
transporter->Send(int(MessageCMD::LogNotify), rspDoc);
384+
}
385+
386+
void EmmyFacade::OnLuaStateGC(lua_State* L) {
387+
#if EMMY_BUILD_AS_HOOK
388+
Debugger::Get()->Stop();
389+
this->attachedStates.clear();
390+
this->isIDEReady = false;
391+
this->L = nullptr;
392+
#else
393+
Destroy();
394+
#endif
395+
}

emmy_core/emmy_facade.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818
#include <rapidjson/document.h>
1919
#include <mutex>
2020
#include <condition_variable>
21+
#include <set>
2122

2223
class Transporter;
2324
class EvalContext;
2425

26+
enum class LogType {
27+
Info, Warning, Error
28+
};
29+
2530
class EmmyFacade {
2631
Transporter* transporter;
2732
lua_State* L;
@@ -45,11 +50,21 @@ class EmmyFacade {
4550
void OnBreak();
4651
void Destroy();
4752
void OnEvalResult(EvalContext* context);
53+
void SendLog(LogType type, const char *fmt, ...);
54+
void OnLuaStateGC(lua_State* L);
4855
private:
4956
void OnInitReq(const rapidjson::Document& document);
5057
void OnReadyReq(const rapidjson::Document& document);
5158
void OnAddBreakPointReq(const rapidjson::Document& document);
5259
void OnRemoveBreakPointReq(const rapidjson::Document& document);
5360
void OnActionReq(const rapidjson::Document& document);
5461
void OnEvalReq(const rapidjson::Document& document);
62+
#ifdef EMMY_BUILD_AS_HOOK
63+
private:
64+
std::set<lua_State*> attachedStates;
65+
public:
66+
void StartupHookMode(int port);
67+
void Attach(lua_State* L);
68+
void StartHook();
69+
#endif
5570
};

0 commit comments

Comments
 (0)