Skip to content

Commit

Permalink
Fix callconv hook on x86
Browse files Browse the repository at this point in the history
  • Loading branch information
nomi-san committed Jul 28, 2023
1 parent 7029e11 commit 20321e1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
4 changes: 2 additions & 2 deletions core/src/browser/browser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static void HookMainBrowserClient(cef_client_t *client)
};
}

static hook::Hook<decltype(cef_browser_host_create_browser)> CefBrowserHost_CreateBrowser;
static hook::Hook<decltype(&cef_browser_host_create_browser)> CefBrowserHost_CreateBrowser;
static int Hooked_CefBrowserHost_CreateBrowser(
const cef_window_info_t* windowInfo,
struct _cef_client_t* client,
Expand Down Expand Up @@ -214,7 +214,7 @@ static void CEF_CALLBACK Hooked_OnBeforeCommandLineProcessing(
}
}

static hook::Hook<decltype(cef_initialize)> CefInitialize;
static hook::Hook<decltype(&cef_initialize)> CefInitialize;
static int Hooked_CefInitialize(const struct _cef_main_args_t* args,
const struct _cef_settings_t* settings, cef_app_t* app, void* windows_sandbox_info)
{
Expand Down
34 changes: 32 additions & 2 deletions core/src/commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ namespace hook
class Hook;

template<typename R, typename ...Args>
class Hook<R(Args...)>
class Hook<R(*)(Args...)>
{
public:
using Fn = R(*)(Args...);
Expand Down Expand Up @@ -335,7 +335,7 @@ namespace hook
}
}

private:
protected:
# pragma pack(push, 1)
struct Shellcode
{
Expand Down Expand Up @@ -382,4 +382,34 @@ namespace hook
uint8_t backup_[size];
};
};

#ifndef _WIN64
template<typename R, typename ...Args>
class Hook<R(__stdcall*)(Args...)> : public Hook<R(*)(Args...)>
{
public:
using FnStd = R(__stdcall*)(Args...);

bool hook(FnStd orig, FnStd hook)
{
return Hook<R(*)(Args...)>::hook((Fn)orig, (Fn)hook);
}

bool hook(const char *lib, const char *proc, FnStd hook)
{
return Hook<R(*)(Args...)>::hook(lib, proc, (Fn)hook);
}

R operator ()(Args ...args)
{
std::lock_guard<std::mutex> lock(mutex_);
{
RestoreGuard<sizeof(Shellcode)> _t(orig_func_, orig_code_);
{
return reinterpret_cast<FnStd>(orig_func_)(args...);
}
}
}
};
#endif
}
2 changes: 1 addition & 1 deletion core/src/dllmain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void HookBrowserProcess();
void HookRendererProcess();
void InjectThisDll(HANDLE hProcess);

static hook::Hook<decltype(CreateProcessW)> Old_CreateProcessW;
static hook::Hook<decltype(&CreateProcessW)> Old_CreateProcessW;
static BOOL WINAPI Hooked_CreateProcessW(
_In_opt_ LPCWSTR lpApplicationName,
_Inout_opt_ LPWSTR lpCommandLine,
Expand Down
4 changes: 2 additions & 2 deletions core/src/libcef.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void WarnInvalidVersion()
#define THISCALL_PARAMS void *, void * // ecx edx
#endif

static cef_color_t __fastcall
static cef_color_t
Hooked_GetBackgroundColor(THISCALL_PARAMS, cef_browser_settings_t *, cef_state_t)
{
return 0; // SK_ColorTRANSPARENT
Expand Down Expand Up @@ -78,7 +78,7 @@ bool LoadLibcefDll(bool is_browser)
const char *pattern = "55 89 E5 53 56 8B 55 0C 8B 45 08 83 FA 01 74 09";
#endif

static hook::Hook<decltype(Hooked_GetBackgroundColor)> GetBackgroundColor;
static hook::Hook<decltype(&Hooked_GetBackgroundColor)> GetBackgroundColor;
auto delegate = (decltype(&Hooked_GetBackgroundColor))utils::patternScan(module, pattern);

// Hook CefContext::GetBackgroundColor().
Expand Down
2 changes: 1 addition & 1 deletion core/src/renderer/datastore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void LoadData(str &json)
if (stream.good())
{
stream.seekg(0, std::ios::end);
size_t fileSize = stream.tellg();
size_t fileSize = (size_t)stream.tellg();
stream.seekg(0, std::ios::beg);

vec<char> buffer(fileSize);
Expand Down
4 changes: 2 additions & 2 deletions core/src/renderer/renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ static void LoadPlugins(V8Object *window)
auto entries = GetPluginEntries();
auto pluginEntries = V8Array::create((int)entries.size());

for (int index = 0; index < entries.size(); index++)
for (int index = 0; index < (int)entries.size(); index++)
{
auto entry = V8Value::string(&CefStr(entries[index]));
pluginEntries->set(index, entry);
Expand Down Expand Up @@ -293,7 +293,7 @@ static int CEF_CALLBACK Hooked_OnProcessMessageReceived(
return OnProcessMessageReceived(self, browser, frame, source_process, message);
}

static hook::Hook<decltype(cef_execute_process)> CefExecuteProcess;
static hook::Hook<decltype(&cef_execute_process)> CefExecuteProcess;
static int Hooked_CefExecuteProcess(const cef_main_args_t* args, cef_app_t* app, void* windows_sandbox_info)
{
// Hook RenderProcessHandler.
Expand Down

0 comments on commit 20321e1

Please sign in to comment.