Skip to content

Commit

Permalink
Opt, ntdll.lib可用时尽可能的从lib隐式依赖,减少try_get次数。
Browse files Browse the repository at this point in the history
  • Loading branch information
mingkuang-Chuyu committed Jun 29, 2024
1 parent 545348f commit de952b1
Show file tree
Hide file tree
Showing 15 changed files with 397 additions and 321 deletions.
16 changes: 10 additions & 6 deletions src/Thunks/DllMainCRTStartup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ namespace YY::Thunks::internal

static SYSTEM_PROCESS_INFORMATION* __fastcall GetCurrentProcessInfo(StringBuffer<char>& _szBuffer)
{
const auto _pfnNtQuerySystemInformation = try_get_NtQuerySystemInformation();
if (!_pfnNtQuerySystemInformation)
#if !defined(__USING_NTDLL_LIB)
const auto NtQuerySystemInformation = try_get_NtQuerySystemInformation();
if (!NtQuerySystemInformation)
return nullptr;
#endif

auto _cbBuffer = max(4096, _szBuffer.uBufferLength);
ULONG _cbRet = 0;
Expand All @@ -127,7 +129,7 @@ namespace YY::Thunks::internal
if (!_pBuffer)
return nullptr;

LONG _Status = _pfnNtQuerySystemInformation(SystemProcessInformation, _pBuffer, _cbBuffer, &_cbRet);
LONG _Status = NtQuerySystemInformation(SystemProcessInformation, _pBuffer, _cbBuffer, &_cbRet);
if (_Status >= 0)
break;

Expand Down Expand Up @@ -283,9 +285,11 @@ namespace YY::Thunks::internal
// 同时给所有历史的线程追加新DLL产生的Tls内存
do
{
const auto _pfnNtQueryInformationThread = try_get_NtQueryInformationThread();
if (!_pfnNtQueryInformationThread)
#if !defined(__USING_NTDLL_LIB)
const auto NtQueryInformationThread = try_get_NtQueryInformationThread();
if (!NtQueryInformationThread)
break;
#endif

StringBuffer<char> _Buffer;
auto _pProcessInfo = GetCurrentProcessInfo(_Buffer);
Expand All @@ -310,7 +314,7 @@ namespace YY::Thunks::internal
}

THREAD_BASIC_INFORMATION _ThreadBasicInfo = {};
LONG _Status = _pfnNtQueryInformationThread(_hThread, ThreadBasicInformation, &_ThreadBasicInfo, sizeof(_ThreadBasicInfo), nullptr);
LONG _Status = NtQueryInformationThread(_hThread, ThreadBasicInformation, &_ThreadBasicInfo, sizeof(_ThreadBasicInfo), nullptr);
if (_Status >= 0 && _ThreadBasicInfo.TebBaseAddress)
{
AllocTlsData((TEB*)_ThreadBasicInfo.TebBaseAddress);
Expand Down
12 changes: 6 additions & 6 deletions src/Thunks/YY_Thunks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,16 @@ namespace YY::Thunks::internal
*/
return ERROR_TIMEOUT;
}
else if (auto pRtlNtStatusToDosError = try_get_RtlNtStatusToDosError())
{
return pRtlNtStatusToDosError(Status);
}
else

#if !defined(__USING_NTDLL_LIB)
const auto RtlNtStatusToDosError = try_get_RtlNtStatusToDosError();
if (!RtlNtStatusToDosError)
{
//如果没有RtlNtStatusToDosError就直接设置Status代码吧,反正至少比没有错误代码强
return Status;
}

#endif
return RtlNtStatusToDosError(Status);
}

static DWORD __fastcall BaseSetLastNTError(
Expand Down
6 changes: 4 additions & 2 deletions src/Thunks/YY_Thunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,11 @@ static void __cdecl __YY_uninitialize_winapi_thunks()
if (__YY_Thunks_Process_Terminating)
return;
}
if (auto pRtlDllShutdownInProgress = (decltype(RtlDllShutdownInProgress)*)GetProcAddress(try_get_module_ntdll(), "RtlDllShutdownInProgress"))
#if (YY_Thunks_Target < __WindowsNT5_1) || !defined(__USING_NTDLL_LIB)
if (const auto RtlDllShutdownInProgress = (decltype(::RtlDllShutdownInProgress)*)GetProcAddress(try_get_module_ntdll(), "RtlDllShutdownInProgress"))
#endif
{
if(pRtlDllShutdownInProgress())
if (RtlDllShutdownInProgress())
return;
}

Expand Down
42 changes: 21 additions & 21 deletions src/Thunks/api-ms-win-core-errorhandling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,33 +96,33 @@ namespace YY::Thunks
{
return pGetErrorMode();
}
else if (auto pNtQueryInformationProcess = try_get_NtQueryInformationProcess())

#if !defined(__USING_NTDLL_LIB)
const auto NtQueryInformationProcess = try_get_NtQueryInformationProcess();
if (!NtQueryInformationProcess)
{
DWORD dwDefaultHardErrorMode;
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return 0;
}
#endif
DWORD dwDefaultHardErrorMode;

auto Status = pNtQueryInformationProcess(NtCurrentProcess(), ProcessDefaultHardErrorMode, &dwDefaultHardErrorMode, sizeof(dwDefaultHardErrorMode), nullptr);
LONG Status = NtQueryInformationProcess(NtCurrentProcess(), ProcessDefaultHardErrorMode, &dwDefaultHardErrorMode, sizeof(dwDefaultHardErrorMode), nullptr);

if (Status >= 0)
if (Status >= 0)
{
if (dwDefaultHardErrorMode & 0x00000001)
{
if (dwDefaultHardErrorMode & 0x00000001)
{
return dwDefaultHardErrorMode & 0xFFFFFFFE;
}
else
{
return dwDefaultHardErrorMode | 0x00000001;
}
return dwDefaultHardErrorMode & 0xFFFFFFFE;
}
else
{
return dwDefaultHardErrorMode | 0x00000001;
}

internal::BaseSetLastNTError(Status);

return 0;
}
else
{
SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return 0;
}

internal::BaseSetLastNTError(Status);
return 0;
}
#endif
} //namespace YY::Thunks
76 changes: 41 additions & 35 deletions src/Thunks/api-ms-win-core-file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,16 @@ namespace YY::Thunks

if (bNtQueryDirectoryFile)
{
auto pNtQueryDirectoryFile = try_get_NtQueryDirectoryFile();
if (!pNtQueryDirectoryFile)
#if !defined(__USING_NTDLL_LIB)
const auto NtQueryDirectoryFile = try_get_NtQueryDirectoryFile();
if (!NtQueryDirectoryFile)
{
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
#endif

Status = pNtQueryDirectoryFile(
Status = NtQueryDirectoryFile(
hFile,
nullptr,
nullptr,
Expand All @@ -144,15 +146,16 @@ namespace YY::Thunks
}
else
{
auto pNtQueryInformationFile = try_get_NtQueryInformationFile();

if (!pNtQueryInformationFile)
#if !defined(__USING_NTDLL_LIB)
const auto NtQueryInformationFile = try_get_NtQueryInformationFile();
if (!NtQueryInformationFile)
{
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
#endif

Status = pNtQueryInformationFile(hFile, &IoStatusBlock, lpFileInformation, dwBufferSize, NtFileInformationClass);
Status = NtQueryInformationFile(hFile, &IoStatusBlock, lpFileInformation, dwBufferSize, NtFileInformationClass);
}

if (Status >= STATUS_SUCCESS)
Expand All @@ -170,7 +173,6 @@ namespace YY::Thunks
else
{
internal::BaseSetLastNTError(Status);

return FALSE;
}
}
Expand All @@ -197,13 +199,14 @@ namespace YY::Thunks
return pSetFileInformationByHandle(hFile, FileInformationClass, lpFileInformation, dwBufferSize);
}


auto pNtSetInformationFile = try_get_NtSetInformationFile();
if (!pNtSetInformationFile)
#if !defined(__USING_NTDLL_LIB)
const auto NtSetInformationFile = try_get_NtSetInformationFile();
if (!NtSetInformationFile)
{
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
#endif

const auto ProcessHeap = ((TEB*)NtCurrentTeb())->ProcessEnvironmentBlock->ProcessHeap;
FILE_INFORMATION_CLASS NtFileInformationClass;
Expand Down Expand Up @@ -237,19 +240,21 @@ namespace YY::Thunks

if (pRenameInfo->FileNameLength < sizeof(wchar_t) || pRenameInfo->FileName[0] != L':')
{
auto pRtlDosPathNameToNtPathName_U = try_get_RtlDosPathNameToNtPathName_U();
auto pRtlFreeUnicodeString = try_get_RtlFreeUnicodeString();
#if !defined(__USING_NTDLL_LIB)
const auto RtlDosPathNameToNtPathName_U = try_get_RtlDosPathNameToNtPathName_U();
const auto RtlFreeUnicodeString = try_get_RtlFreeUnicodeString();

if (pRtlDosPathNameToNtPathName_U == nullptr || pRtlFreeUnicodeString ==nullptr)
if (RtlDosPathNameToNtPathName_U == nullptr || RtlFreeUnicodeString == nullptr)
{
SetLastError(ERROR_INVALID_FUNCTION);
return FALSE;
}
#endif

UNICODE_STRING NtName = {};


if (!pRtlDosPathNameToNtPathName_U(pRenameInfo->FileName, &NtName, nullptr, nullptr))
if (!RtlDosPathNameToNtPathName_U(pRenameInfo->FileName, &NtName, nullptr, nullptr))
{
SetLastError(ERROR_INVALID_PARAMETER);

Expand All @@ -263,7 +268,7 @@ namespace YY::Thunks
{
auto lStatus = GetLastError();

pRtlFreeUnicodeString(&NtName);
RtlFreeUnicodeString(&NtName);

SetLastError(lStatus);
return FALSE;
Expand All @@ -283,7 +288,7 @@ namespace YY::Thunks
lpFileInformation = NewRenameInfo;
dwBufferSize = dwNewBufferSize;

pRtlFreeUnicodeString(&NtName);
RtlFreeUnicodeString(&NtName);
}
}
break;
Expand Down Expand Up @@ -336,7 +341,7 @@ namespace YY::Thunks

IO_STATUS_BLOCK IoStatusBlock;

auto Status = pNtSetInformationFile(hFile, &IoStatusBlock, lpFileInformation, dwBufferSize, NtFileInformationClass);
LONG Status = NtSetInformationFile(hFile, &IoStatusBlock, lpFileInformation, dwBufferSize, NtFileInformationClass);

if (bFreeFileInformation)
{
Expand All @@ -345,11 +350,8 @@ namespace YY::Thunks

if (Status >= STATUS_SUCCESS)
return TRUE;



internal::BaseSetLastNTError(Status);

return FALSE;
}
#endif
Expand Down Expand Up @@ -399,16 +401,17 @@ namespace YY::Thunks
break;
}

#if !defined(__USING_NTDLL_LIB)
const auto NtQueryObject = try_get_NtQueryObject();
const auto NtQueryInformationFile = try_get_NtQueryInformationFile();

auto pNtQueryObject = try_get_NtQueryObject();
auto pNtQueryInformationFile = try_get_NtQueryInformationFile();

if (nullptr == pNtQueryObject
|| nullptr == pNtQueryInformationFile)
if (nullptr == NtQueryObject
|| nullptr == NtQueryInformationFile)
{
SetLastError(ERROR_INVALID_FUNCTION);
return 0;
}
#endif

UNICODE_STRING VolumeNtName = {};

Expand Down Expand Up @@ -458,7 +461,7 @@ namespace YY::Thunks
}
}

auto Status = pNtQueryObject(hFile, ObjectNameInformation, pObjectName, cbObjectName, &cbObjectName);
LONG Status = NtQueryObject(hFile, ObjectNameInformation, pObjectName, cbObjectName, &cbObjectName);

if (STATUS_BUFFER_OVERFLOW == Status)
{
Expand Down Expand Up @@ -503,7 +506,7 @@ namespace YY::Thunks

IO_STATUS_BLOCK IoStatusBlock;

auto Status = pNtQueryInformationFile(hFile, &IoStatusBlock, pFileNameInfo, cbFileNameInfo, FileNameInformation);
LONG Status = NtQueryInformationFile(hFile, &IoStatusBlock, pFileNameInfo, cbFileNameInfo, FileNameInformation);

if (STATUS_BUFFER_OVERFLOW == Status)
{
Expand Down Expand Up @@ -906,12 +909,14 @@ namespace YY::Thunks
return INVALID_HANDLE_VALUE;
}

const auto pNtCreateFile = try_get_NtCreateFile();
if (!pNtCreateFile)
#if !defined(__USING_NTDLL_LIB)
const auto NtCreateFile = try_get_NtCreateFile();
if (!NtCreateFile)
{
SetLastError(ERROR_FUNCTION_FAILED);
return INVALID_HANDLE_VALUE;
}
#endif

dwDesiredAccess |= SYNCHRONIZE | FILE_READ_ATTRIBUTES;

Expand Down Expand Up @@ -982,7 +987,7 @@ namespace YY::Thunks

IO_STATUS_BLOCK IoStatusBlock;

auto Status = pNtCreateFile(&hFile, dwDesiredAccess, &ObjectAttributes, &IoStatusBlock, nullptr, 0, dwShareMode, FILE_OPEN, CreateOptions, nullptr, 0);
auto Status = NtCreateFile(&hFile, dwDesiredAccess, &ObjectAttributes, &IoStatusBlock, nullptr, 0, dwShareMode, FILE_OPEN, CreateOptions, nullptr, 0);

if (Status < 0)
{
Expand Down Expand Up @@ -1074,13 +1079,14 @@ namespace YY::Thunks

do
{
const auto pNtCreateFile = try_get_NtCreateFile();
if (!pNtCreateFile)
#if !defined(__USING_NTDLL_LIB)
const auto NtCreateFile = try_get_NtCreateFile();
if (!NtCreateFile)
{
Status = STATUS_INVALID_INFO_CLASS;
break;
}

#endif

if ((size_t(hOriginalFile) & 0x10000003) == 3)
{
Expand Down Expand Up @@ -1194,7 +1200,7 @@ namespace YY::Thunks

HANDLE hFile;

Status = pNtCreateFile(
Status = NtCreateFile(
&hFile,
dwDesiredAccess,
&ObjectAttributes,
Expand Down
Loading

0 comments on commit de952b1

Please sign in to comment.