-
Notifications
You must be signed in to change notification settings - Fork 114
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
Use QueueUserAPC to run NtCancelIoFile on each thread #125
Merged
mingkuang-Chuyu
merged 10 commits into
Chuyu-Team:Fea/YY/PendingPR
from
stevefan1999-personal:patch-nt-cancel-io-file-ex
Oct 22, 2024
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
932418e
use QueueUserAPC to run NtCancelIoFile on each thread
stevefan1999-personal 9399595
ignore current calling thread for APC to prevent race condition
stevefan1999-personal 00b2303
implementation for CancelIoEx as well
stevefan1999-personal 8b9bd78
simplify CancelIoEx implementation
stevefan1999-personal f590aac
use CancelIo inside the APC function instead
stevefan1999-personal e591168
fix wrong parameter passed
stevefan1999-personal 443f2be
further ignore the thread that is not for the current pid
stevefan1999-personal 3f1609c
fix deadlock on GetModuleHandleA during APC call
stevefan1999-personal 6536297
return STATUS_NOT_SUPPORTED if overlapped IO is requested
stevefan1999-personal 2ae1d0f
return STATUS_NOT_FOUND instead on cancel request with overlapped io
stevefan1999-personal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,77 @@ | ||
namespace YY::Thunks | ||
#include <tlhelp32.h> | ||
namespace YY::Thunks | ||
{ | ||
#if (YY_Thunks_Target < __WindowsNT6) | ||
|
||
// 最低受支持的客户端 Windows Vista [桌面应用|UWP 应用] | ||
// 最低受支持的服务器 Windows Server 2008[桌面应用 | UWP 应用] | ||
__DEFINE_THUNK( | ||
ntdll, | ||
12, | ||
NTSTATUS, | ||
NTAPI, | ||
NtCancelIoFileEx, | ||
ntdll, | ||
12, | ||
NTSTATUS, | ||
NTAPI, | ||
NtCancelIoFileEx, | ||
HANDLE handle, | ||
IO_STATUS_BLOCK* io, | ||
IO_STATUS_BLOCK* io_status | ||
) | ||
) | ||
{ | ||
if (const auto _pfnNtCancelIoFileEx = try_get_NtCancelIoFileEx()) | ||
{ | ||
return _pfnNtCancelIoFileEx(handle, io, io_status); | ||
} | ||
|
||
auto currentTid = GetCurrentThreadId(); | ||
HANDLE h = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, GetCurrentProcessId()); | ||
if (h != INVALID_HANDLE_VALUE) { | ||
THREADENTRY32 te; | ||
te.dwSize = sizeof(te); | ||
if (Thread32First(h, &te)) { | ||
do { | ||
if (te.th32ThreadID == currentTid) { | ||
continue; | ||
} | ||
HANDLE threadHandle = OpenThread(THREAD_SET_CONTEXT, FALSE, te.th32ThreadID); | ||
if (threadHandle != INVALID_HANDLE_VALUE) { | ||
struct CancelIoData { | ||
HANDLE handle; | ||
IO_STATUS_BLOCK* io_status; | ||
}; | ||
|
||
|
||
QueueUserAPC([](ULONG_PTR param) { | ||
auto data = (CancelIoData*)param; | ||
#ifndef __USING_NTDLL_LIB | ||
const auto NtCancelIoFile = try_get_NtCancelIoFile(); | ||
if (!NtCancelIoFile) | ||
{ | ||
// 正常来说不应该走到这里 | ||
delete data; | ||
return; | ||
} | ||
#endif | ||
|
||
NtCancelIoFile(data->handle, data->io_status); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
delete data; | ||
}, threadHandle, (ULONG_PTR) new CancelIoData {handle, io_status}); | ||
CloseHandle(threadHandle); | ||
} | ||
} while (Thread32Next(h, &te)); | ||
} | ||
CloseHandle(h); | ||
} | ||
|
||
|
||
#ifndef __USING_NTDLL_LIB | ||
const auto NtCancelIoFile = try_get_NtCancelIoFile(); | ||
if(!NtCancelIoFile) | ||
if (!NtCancelIoFile) | ||
{ | ||
// 正常来说不应该走到这里 | ||
return STATUS_NOT_SUPPORTED; | ||
} | ||
#endif | ||
// 最坏打算,清除所有的调用 | ||
return NtCancelIoFile(handle, io_status); | ||
return NtCancelIoFile(handle, io_status); | ||
} | ||
#endif | ||
|
||
|
@@ -38,16 +80,16 @@ | |
|
||
// 最低受支持的客户端 在 Windows 7 和更高版本的 Windows 中可用。 | ||
__DEFINE_THUNK( | ||
ntdll, | ||
16, | ||
NTSTATUS, | ||
NTAPI, | ||
NtOpenKeyEx, | ||
ntdll, | ||
16, | ||
NTSTATUS, | ||
NTAPI, | ||
NtOpenKeyEx, | ||
__out PHANDLE _phKeyHandle, | ||
__in ACCESS_MASK _fDesiredAccess, | ||
__in POBJECT_ATTRIBUTES _pObjectAttributes, | ||
__in ULONG _fOpenOptions | ||
) | ||
) | ||
{ | ||
if (const auto _pfnNtOpenKeyEx = try_get_NtOpenKeyEx()) | ||
{ | ||
|
@@ -118,7 +160,7 @@ | |
} | ||
} while (false); | ||
|
||
if(_hKey) | ||
if (_hKey) | ||
NtClose(_hKey); | ||
|
||
return _Status; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#125 (comment)