[*] Ensure AuProcAddresses.NT.* is used for all dynamically linked symbols

This commit is contained in:
Reece Wilson 2023-07-09 10:03:29 +01:00
parent d192e2529f
commit 627bdddfdc
8 changed files with 144 additions and 135 deletions

View File

@ -12,13 +12,18 @@ namespace Aurora
void InitNTAddresses() void InitNTAddresses()
{ {
#if defined(AURORA_PLATFORM_WIN32) #if defined(AURORA_PLATFORM_WIN32)
#define ADD_LOAD_LIB(name) \ #define ADD_LOAD_LIB(name) \
auto h ## name = LoadLibraryW(k## name ## DllName); auto h ## name = GetModuleHandleW(k## name ## DllName); \
if (!h ## name) \
{ \
auto h ## name = LoadLibraryW(k## name ## DllName); \
}
ADD_LOAD_LIB(Kernel32); ADD_LOAD_LIB(Kernel32);
ADD_LOAD_LIB(Nt); ADD_LOAD_LIB(Nt);
ADD_LOAD_LIB(KernelBase); ADD_LOAD_LIB(KernelBase);
ADD_LOAD_LIB(Sync); ADD_LOAD_LIB(Sync);
ADD_LOAD_LIB(WS2);
#define ADD_GET_PROC(name, proc) \ #define ADD_GET_PROC(name, proc) \
if (h ## name) \ if (h ## name) \
@ -59,18 +64,35 @@ namespace Aurora
ADD_GET_PROC(Kernel32, GetSystemCpuSetInformation) ADD_GET_PROC(Kernel32, GetSystemCpuSetInformation)
ADD_GET_PROC(Kernel32, GetLogicalProcessorInformation) ADD_GET_PROC(Kernel32, GetLogicalProcessorInformation)
ADD_GET_PROC(Kernel32, SetThreadDescription)
ADD_GET_PROC(Kernel32, SetThreadInformation)
ADD_GET_PROC(Kernel32, SetThreadSelectedCpuSets)
ADD_GET_PROC(Kernel32, PrefetchVirtualMemory)
ADD_GET_PROC(Sync, WaitOnAddress) ADD_GET_PROC(Sync, WaitOnAddress)
ADD_GET_PROC(Sync, WakeByAddressSingle) ADD_GET_PROC(Sync, WakeByAddressSingle)
ADD_GET_PROC(Sync, WakeByAddressAll) ADD_GET_PROC(Sync, WakeByAddressAll)
ADD_GET_PROC(WS2, GetAddrInfoExCancel)
#else #else
pWaitOnAddress = WaitOnAddress; pWaitOnAddress = WaitOnAddress;
pWakeByAddressSingle = WakeByAddressSingle; pWakeByAddressSingle = WakeByAddressSingle;
pWakeByAddressAll = WakeByAddressAll; pWakeByAddressAll = WakeByAddressAll;
pVirtualAlloc2 = VirtualAlloc2FromApp; pVirtualAlloc2 = VirtualAlloc2FromApp;
pMapViewOfFile3 = MapViewOfFile3FromApp; pMapViewOfFile3 = MapViewOfFile3FromApp;
pGetSystemCpuSetInformation = GetSystemCpuSetInformation;
pGetLogicalProcessorInformation = GetLogicalProcessorInformation;
pSetThreadInformation = SetThreadInformation;
pSetThreadDescription = SetThreadDescription;
pSetThreadSelectedCpuSets = SetThreadSelectedCpuSets;
pGetAddrInfoExCancel = GetAddrInfoExCancel;
pPrefetchVirtualMemory = PrefetchVirtualMemory;
// https://github.com/LWJGL/lwjgl3/blob/master/modules/lwjgl/remotery/src/main/c/Remotery.c#L1188 // https://github.com/LWJGL/lwjgl3/blob/master/modules/lwjgl/remotery/src/main/c/Remotery.c#L1188
// Xbox main SDK has a better API we should use // Xbox main SDK has a better API we should use
// So... // So...

View File

@ -15,106 +15,151 @@ namespace Aurora
static const wchar_t *kNtDllName { L"NTDLL.dll" }; static const wchar_t *kNtDllName { L"NTDLL.dll" };
static const wchar_t *kKernel32DllName { L"Kernel32.dll" }; static const wchar_t *kKernel32DllName { L"Kernel32.dll" };
static const wchar_t *kKernelBaseDllName { L"KernelBase.dll" }; static const wchar_t *kKernelBaseDllName { L"KernelBase.dll" };
static const wchar_t *kWS2DllName { L"Ws2_32.dll" };
struct WIN32_MEMORY_RANGE_ENTRY2
{
PVOID VirtualAddress;
SIZE_T NumberOfBytes;
};
enum class THREAD_INFORMATION_CLASS
{
ThreadMemoryPriority,
ThreadAbsoluteCpuPriority,
ThreadDynamicCodePolicy,
ThreadPowerThrottling,
ThreadInformationClassMax
};
inline BOOL(_stdcall *pWaitOnAddress)( inline BOOL(_stdcall *pWaitOnAddress)(
volatile VOID * Address, volatile VOID * Address,
PVOID CompareAddress, PVOID CompareAddress,
SIZE_T AddressSize, SIZE_T AddressSize,
DWORD dwMilliseconds DWORD dwMilliseconds
); );
inline void(_stdcall *pWakeByAddressSingle)( inline void(_stdcall *pWakeByAddressSingle)(
PVOID Address PVOID Address
); );
inline void(_stdcall *pWakeByAddressAll)( inline void(_stdcall *pWakeByAddressAll)(
PVOID Address PVOID Address
); );
inline DWORD(_stdcall *pNtDelayExecution)( inline DWORD(_stdcall *pNtDelayExecution)(
BOOLEAN Alertable, BOOLEAN Alertable,
PLARGE_INTEGER DelayInterval PLARGE_INTEGER DelayInterval
); );
inline PVOID(__stdcall *pVirtualAlloc2)( inline PVOID(__stdcall *pVirtualAlloc2)(
HANDLE Process, HANDLE Process,
PVOID BaseAddress, PVOID BaseAddress,
SIZE_T Size, SIZE_T Size,
ULONG AllocationType, ULONG AllocationType,
ULONG PageProtection, ULONG PageProtection,
MEM_EXTENDED_PARAMETER *ExtendedParameters, MEM_EXTENDED_PARAMETER * ExtendedParameters,
ULONG ParameterCount ULONG ParameterCount
); );
inline PVOID(__stdcall *pMapViewOfFile3)( inline PVOID(__stdcall *pMapViewOfFile3)(
HANDLE FileMapping, HANDLE FileMapping,
HANDLE Process, HANDLE Process,
PVOID BaseAddress, PVOID BaseAddress,
ULONG64 Offset, ULONG64 Offset,
SIZE_T ViewSize, SIZE_T ViewSize,
ULONG AllocationType, ULONG AllocationType,
ULONG PageProtection, ULONG PageProtection,
MEM_EXTENDED_PARAMETER *ExtendedParameters, MEM_EXTENDED_PARAMETER * ExtendedParameters,
ULONG ParameterCount ULONG ParameterCount
); );
inline PVOID(__stdcall *pUnmapViewOfFile2)( inline PVOID(__stdcall *pUnmapViewOfFile2)(
HANDLE Process, HANDLE Process,
PVOID BaseAddress, PVOID BaseAddress,
ULONG UnmapFlags ULONG UnmapFlags
); );
inline NTSTATUS(__stdcall *pNtWaitForKeyedEvent)( inline NTSTATUS(__stdcall *pNtWaitForKeyedEvent)(
HANDLE Handle, HANDLE Handle,
PVOID Key, PVOID Key,
BOOLEAN Alertable, BOOLEAN Alertable,
PLARGE_INTEGER NTTimeout PLARGE_INTEGER NTTimeout
); );
inline NTSTATUS(__stdcall *pNtReleaseKeyedEvent)( inline NTSTATUS(__stdcall *pNtReleaseKeyedEvent)(
HANDLE Handle, HANDLE Handle,
PVOID Key, PVOID Key,
BOOLEAN Alertable, BOOLEAN Alertable,
PLARGE_INTEGER NTTimeout PLARGE_INTEGER NTTimeout
); );
inline NTSTATUS(__stdcall *pNtCreateKeyedEvent)( inline NTSTATUS(__stdcall *pNtCreateKeyedEvent)(
HANDLE Handle, HANDLE Handle,
ACCESS_MASK Access, ACCESS_MASK Access,
POBJECT_ATTRIBUTES Attr, POBJECT_ATTRIBUTES Attr,
ULONG Flags ULONG Flags
); );
inline NTSTATUS(__stdcall *pNtOpenKeyedEvent)( inline NTSTATUS(__stdcall *pNtOpenKeyedEvent)(
HANDLE Handle, HANDLE Handle,
ACCESS_MASK Access, ACCESS_MASK Access,
POBJECT_ATTRIBUTES Attr, POBJECT_ATTRIBUTES Attr,
ULONG Flags ULONG Flags
); );
inline NTSTATUS(__stdcall *pRtlWaitOnAddress)( inline NTSTATUS(__stdcall *pRtlWaitOnAddress)(
const void * addr, const void * addr,
const void * cmp, const void * cmp,
SIZE_T size, SIZE_T size,
const LARGE_INTEGER * timeout); const LARGE_INTEGER * timeout);
#if defined(AURORA_PLATFORM_WIN32) #if defined(AURORA_PLATFORM_WIN32)
inline NTSTATUS(__stdcall *pRtlGetVersion)( inline NTSTATUS(__stdcall *pRtlGetVersion)(
PRTL_OSVERSIONINFOW lpVersionInformation PRTL_OSVERSIONINFOW lpVersionInformation
); );
#endif #endif
inline BOOL(__stdcall *pGetSystemCpuSetInformation)( inline BOOL(__stdcall *pGetSystemCpuSetInformation)(
PSYSTEM_CPU_SET_INFORMATION Information, PSYSTEM_CPU_SET_INFORMATION Information,
ULONG BufferLength, ULONG BufferLength,
PULONG ReturnedLength, PULONG ReturnedLength,
HANDLE Process, HANDLE Process,
ULONG Flags ULONG Flags
); );
inline BOOL(__stdcall * pGetLogicalProcessorInformation)( inline BOOL(__stdcall *pGetLogicalProcessorInformation)(
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION Buffer, PSYSTEM_LOGICAL_PROCESSOR_INFORMATION Buffer,
PDWORD ReturnedLength PDWORD ReturnedLength
);
inline HRESULT(__stdcall *pSetThreadDescription)(
HANDLE hThread,
PCWSTR lpThreadDescription
);
inline BOOL(__stdcall *pSetThreadInformation)(
HANDLE hThread,
THREAD_INFORMATION_CLASS ThreadInformationClass,
LPVOID ThreadInformation,
DWORD ThreadInformationSize
);
inline BOOL(__stdcall *pSetThreadSelectedCpuSets)(
HANDLE Thread,
const ULONG * CpuSetIds,
ULONG CpuSetIdCount
);
inline INT(__stdcall *pGetAddrInfoExCancel)(
LPHANDLE lpHandle
);
inline BOOL(__stdcall *pPrefetchVirtualMemory)(
HANDLE hProcess,
ULONG_PTR NumberOfEntries,
WIN32_MEMORY_RANGE_ENTRY2 * VirtualAddresses,
ULONG Flags
); );
inline bool gUseNativeWaitMutex {}; inline bool gUseNativeWaitMutex {};

View File

@ -56,7 +56,7 @@ namespace Aurora::IO::Net
int iRet {}; int iRet {};
HANDLE hHandle = NULL; HANDLE hHandle = NULL;
if (GetAddrInfoExCancel_f) if (pGetAddrInfoExCancel)
{ {
iRet = GetAddrInfoExW(AuLocale::ConvertFromUTF8(this->hostname).data(), iRet = GetAddrInfoExW(AuLocale::ConvertFromUTF8(this->hostname).data(),
nullptr, nullptr,
@ -160,9 +160,9 @@ namespace Aurora::IO::Net
auto hHandle = AuExchange(this->hName_, (HANDLE)NULL); auto hHandle = AuExchange(this->hName_, (HANDLE)NULL);
if (hHandle != NULL) if (hHandle != NULL)
{ {
if (GetAddrInfoExCancel_f) if (pGetAddrInfoExCancel)
{ {
GetAddrInfoExCancel_f(&hHandle); pGetAddrInfoExCancel(&hHandle);
} }
} }

View File

@ -25,8 +25,6 @@ namespace Aurora::IO::Net
wVersionRequested = MAKEWORD(2, 2); wVersionRequested = MAKEWORD(2, 2);
gWin32NetReady = !WSAStartup(wVersionRequested, &wsaData); gWin32NetReady = !WSAStartup(wVersionRequested, &wsaData);
GetAddrInfoExCancel_f = AuReinterpretCast<decltype(GetAddrInfoExCancel_f)>(GetProcAddress(GetModuleHandleW(L"Ws2_32.dll"), "GetAddrInfoExCancel"));
#endif #endif
#if defined(AURORA_IS_LINUX_DERIVED) #if defined(AURORA_IS_LINUX_DERIVED)

View File

@ -43,11 +43,5 @@ namespace Aurora::IO::Net
{ {
static const auto kDefaultStreamSize = 32 * 1024; // ~960 clients for 30MB of 2x 32KiB streams. Seems... reasonable. static const auto kDefaultStreamSize = 32 * 1024; // ~960 clients for 30MB of 2x 32KiB streams. Seems... reasonable.
#if defined(AURORA_IS_MODERNNT_DERIVED)
inline INT(__stdcall *GetAddrInfoExCancel_f)(
LPHANDLE lpHandle
);
#endif
bool IsNetReady(); bool IsNetReady();
} }

View File

@ -25,26 +25,12 @@
namespace Aurora::Memory::Cache namespace Aurora::Memory::Cache
{ {
#if defined(AURORA_IS_MODERNNT_DERIVED)
struct WIN32_MEMORY_RANGE_ENTRY2 {
PVOID VirtualAddress;
SIZE_T NumberOfBytes;
};
BOOL (__stdcall *PrefetchVirtualMemory_f)(
HANDLE hProcess,
ULONG_PTR NumberOfEntries,
WIN32_MEMORY_RANGE_ENTRY2* VirtualAddresses,
ULONG Flags
);
#endif
AUKN_SYM void OptimizeAddressRangeOnCore(const AuList<AuPair<AuUInt, AuUInt>> &addressRanges) AUKN_SYM void OptimizeAddressRangeOnCore(const AuList<AuPair<AuUInt, AuUInt>> &addressRanges)
{ {
#if defined(AURORA_IS_MODERNNT_DERIVED) #if defined(AURORA_IS_MODERNNT_DERIVED)
AuList<WIN32_MEMORY_RANGE_ENTRY2> arry; AuList<WIN32_MEMORY_RANGE_ENTRY2> arry;
if (!PrefetchVirtualMemory_f) if (!pPrefetchVirtualMemory)
{ {
return; return;
} }
@ -82,7 +68,7 @@ namespace Aurora::Memory::Cache
} }
#if defined(AURORA_IS_MODERNNT_DERIVED) #if defined(AURORA_IS_MODERNNT_DERIVED)
if (!PrefetchVirtualMemory_f(GetCurrentProcess(), arry.size(), arry.data(), 0)) if (!pPrefetchVirtualMemory(GetCurrentProcess(), arry.size(), arry.data(), 0))
{ {
SysPushErrorHAL("Couldn't poke memory physical memory into virtual address space (array)"); SysPushErrorHAL("Couldn't poke memory physical memory into virtual address space (array)");
} }
@ -134,8 +120,6 @@ namespace Aurora::Memory::Cache
void InitCache() void InitCache()
{ {
#if defined(AURORA_IS_MODERNNT_DERIVED)
PrefetchVirtualMemory_f = AuReinterpretCast<decltype(PrefetchVirtualMemory_f)>(GetProcAddress(GetModuleHandleW(L"Kernel32.dll"), "PrefetchVirtualMemory"));
#endif
} }
} }

View File

@ -95,11 +95,8 @@ namespace Aurora::SWInfo
#if defined(AURORA_PLATFORM_WIN32) #if defined(AURORA_PLATFORM_WIN32)
RTL_OSVERSIONINFOEXW ovi {}; RTL_OSVERSIONINFOEXW ovi {};
ovi.dwOSVersionInfoSize = sizeof(ovi); ovi.dwOSVersionInfoSize = sizeof(ovi);
NTSTATUS(CALLBACK *pRtlGetVersion) (PRTL_OSVERSIONINFOW lpVersionInformation); if (pRtlGetVersion(reinterpret_cast<PRTL_OSVERSIONINFOW>(&ovi)) == 0)
pRtlGetVersion = reinterpret_cast<decltype(pRtlGetVersion)>(GetProcAddress(LoadLibraryW(L"Ntdll.dll"), "RtlGetVersion"));
if (pRtlGetVersion && pRtlGetVersion(reinterpret_cast<PRTL_OSVERSIONINFOW>(&ovi)) == 0)
{ {
osInfo.uKernelPatch = ovi.dwBuildNumber; osInfo.uKernelPatch = ovi.dwBuildNumber;
osInfo.uKernelMinor = ovi.dwMinorVersion; osInfo.uKernelMinor = ovi.dwMinorVersion;

View File

@ -26,14 +26,7 @@
#endif #endif
#if defined(AURORA_IS_MODERNNT_DERIVED) #if defined(AURORA_IS_MODERNNT_DERIVED)
BOOL (*SetThreadInformation_f)( static auto const kThreadPowerThrottling = Aurora::THREAD_INFORMATION_CLASS::ThreadPowerThrottling;
HANDLE hThread,
DWORD ThreadInformationClass,
LPVOID ThreadInformation,
DWORD ThreadInformationSize
);
static auto const kThreadPowerThrottling = 3;
struct THREAD_POWER_THROTTLING_STATE2 struct THREAD_POWER_THROTTLING_STATE2
{ {
@ -671,19 +664,9 @@ namespace Aurora::Threading::Threads
} }
else else
{ {
static HRESULT(WINAPI * SetThreadDescription_f)(HANDLE, PCWSTR); if (pSetThreadDescription)
if (!SetThreadDescription_f)
{ {
#if !defined(AURORA_PLATFORM_WIN32) pSetThreadDescription(this->handle_, AuLocale::ConvertFromUTF8(this->name_).c_str());
SetThreadDescription_f = SetThreadDescription;
#else
SetThreadDescription_f = AuReinterpretCast<decltype(SetThreadDescription_f)>(GetProcAddress(GetModuleHandleW(L"Kernel32.dll"), "SetThreadDescription"));
#endif
}
if (SetThreadDescription_f)
{
SetThreadDescription_f(this->handle_, AuLocale::ConvertFromUTF8(this->name_).c_str());
} }
} }
@ -891,12 +874,12 @@ namespace Aurora::Threading::Threads
break; break;
} }
if (SetThreadInformation_f) if (pSetThreadInformation)
{ {
SetThreadInformation_f(this->handle_, pSetThreadInformation(this->handle_,
kThreadPowerThrottling, kThreadPowerThrottling,
&throttlingState, &throttlingState,
sizeof(throttlingState)); sizeof(throttlingState));
} }
//#elif defined(AURORA_IS_XNU_DERIVED) //#elif defined(AURORA_IS_XNU_DERIVED)
@ -981,23 +964,13 @@ namespace Aurora::Threading::Threads
if ((AuBuild::kCurrentPlatform != AuBuild::EPlatform::ePlatformWin32) || if ((AuBuild::kCurrentPlatform != AuBuild::EPlatform::ePlatformWin32) ||
(AuSwInfo::IsWindows10OrGreater())) (AuSwInfo::IsWindows10OrGreater()))
{ {
static BOOL(WINAPI * SetThreadSelectedCpuSets_f)(HANDLE, const ULONG *, ULONG);
if (!SetThreadSelectedCpuSets_f)
{
#if !defined(AURORA_PLATFORM_WIN32)
SetThreadSelectedCpuSets_f = SetThreadSelectedCpuSets;
#else
SetThreadSelectedCpuSets_f = AuReinterpretCast<decltype(SetThreadSelectedCpuSets_f)>(GetProcAddress(GetModuleHandleW(L"Kernel32.dll"), "SetThreadSelectedCpuSets"));
#endif
}
auto sets = mask.CpuBitCount() ? auto sets = mask.CpuBitCount() ?
mask.ToCpuSets() : mask.ToCpuSets() :
AuHwInfo::GetCPUInfo().maskAllCores.ToCpuSets(); AuHwInfo::GetCPUInfo().maskAllCores.ToCpuSets();
if (SetThreadSelectedCpuSets_f) if (pSetThreadSelectedCpuSets)
{ {
if (SetThreadSelectedCpuSets_f(this->handle_, sets.data(), sets.size())) if (pSetThreadSelectedCpuSets(this->handle_, sets.data(), sets.size()))
{ {
return; return;
} }
@ -1244,10 +1217,6 @@ namespace Aurora::Threading::Threads
AuxUlibInitialize(); AuxUlibInitialize();
#endif #endif
#if defined(AURORA_IS_MODERNNT_DERIVED)
SetThreadInformation_f = AuReinterpretCast<decltype(SetThreadInformation_f)>(GetProcAddress(GetModuleHandleW(L"Kernel32.dll"), "SetThreadInformation"));
#endif
AttachSignalKiller(); AttachSignalKiller();
} }
} }