Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up clink injection by not suspending threads #507

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions clink/process/include/process/process.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ class process
int get_parent_pid() const;
void* inject_module(const char* dll);
template <typename T> void* remote_call(void* function, T const& param);
void pause();
void unpause();

private:
void* remote_call(void* function, const void* param, int param_size);
void pause(bool suspend);
int m_pid;

struct handle
Expand All @@ -50,15 +47,3 @@ void* process::remote_call(void* function, T const& param)
{
return remote_call(function, &param, sizeof(param));
}

//------------------------------------------------------------------------------
inline void process::pause()
{
pause(true);
}

//------------------------------------------------------------------------------
inline void process::unpause()
{
pause(false);
}
25 changes: 0 additions & 25 deletions clink/process/src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,6 @@ process::arch process::get_arch() const
return arch_x86;
}

//------------------------------------------------------------------------------
void process::pause(bool suspend)
{
handle th32 = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, m_pid);
if (!th32)
return;

THREADENTRY32 te = { sizeof(te) };
BOOL ok = Thread32First(th32, &te);
while (ok != FALSE)
{
if (te.th32OwnerProcessID == m_pid)
{
handle thread = OpenThread(THREAD_ALL_ACCESS, FALSE, te.th32ThreadID);
suspend ? SuspendThread(thread) : ResumeThread(thread);
}

ok = Thread32Next(th32, &te);
}
}

//------------------------------------------------------------------------------
void* process::inject_module(const char* dll_path)
{
Expand Down Expand Up @@ -180,21 +159,17 @@ void* process::remote_call(void* function, const void* param, int param_size)
static_assert(sizeof(thunk_ptrs) == sizeof(thunk_data), "");
static_assert((offsetof(thunk_data, in) & 7) == 0, "");

pause();

// The 'remote call' is actually a thread that's created in the process and
// then waited on for completion.
DWORD thread_id;
handle remote_thread = CreateRemoteThread(process_handle, nullptr, 0,
(LPTHREAD_START_ROUTINE)region.base, remote_thunk_data, 0, &thread_id);
if (!remote_thread)
{
unpause();
return 0;
}

WaitForSingleObject(remote_thread, INFINITE);
unpause();

void* call_ret = nullptr;
vm.read(&call_ret, remote_thunk_data + offsetof(thunk_data, out), sizeof(call_ret));
Expand Down
6 changes: 3 additions & 3 deletions clink/terminal/src/win_screen_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void win_screen_buffer::set_cursor(int column, int row)
column = clamp(column, 0, width);
row = clamp(row, 0, height);

COORD xy = { window.Left + column, window.Top + row };
COORD xy = { static_cast<short>(window.Left + column), static_cast<short>(window.Top + row) };
SetConsoleCursorPosition(m_handle, xy);
}

Expand All @@ -173,8 +173,8 @@ void win_screen_buffer::move_cursor(int dx, int dy)
GetConsoleScreenBufferInfo(m_handle, &csbi);

COORD xy = {
clamp(csbi.dwCursorPosition.X + dx, 0, csbi.dwSize.X - 1),
clamp(csbi.dwCursorPosition.Y + dy, 0, csbi.dwSize.Y - 1),
static_cast<short>(clamp(csbi.dwCursorPosition.X + dx, 0, csbi.dwSize.X - 1)),
static_cast<short>(clamp(csbi.dwCursorPosition.Y + dy, 0, csbi.dwSize.Y - 1)),
};
SetConsoleCursorPosition(m_handle, xy);
}
Expand Down