[*] 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()
{
#if defined(AURORA_PLATFORM_WIN32)
#define ADD_LOAD_LIB(name) \
auto h ## name = LoadLibraryW(k## name ## DllName);
#define ADD_LOAD_LIB(name) \
auto h ## name = GetModuleHandleW(k## name ## DllName); \
if (!h ## name) \
{ \
auto h ## name = LoadLibraryW(k## name ## DllName); \
}
ADD_LOAD_LIB(Kernel32);
ADD_LOAD_LIB(Nt);
ADD_LOAD_LIB(KernelBase);
ADD_LOAD_LIB(Sync);
ADD_LOAD_LIB(WS2);
#define ADD_GET_PROC(name, proc) \
if (h ## name) \
@ -59,18 +64,35 @@ namespace Aurora
ADD_GET_PROC(Kernel32, GetSystemCpuSetInformation)
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, WakeByAddressSingle)
ADD_GET_PROC(Sync, WakeByAddressAll)
ADD_GET_PROC(WS2, GetAddrInfoExCancel)
#else
pWaitOnAddress = WaitOnAddress;
pWakeByAddressSingle = WakeByAddressSingle;
pWakeByAddressAll = WakeByAddressAll;
pVirtualAlloc2 = VirtualAlloc2FromApp;
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
// Xbox main SDK has a better API we should use
// So...

View File

@ -15,106 +15,151 @@ namespace Aurora
static const wchar_t *kNtDllName { L"NTDLL.dll" };
static const wchar_t *kKernel32DllName { L"Kernel32.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)(
volatile VOID * Address,
PVOID CompareAddress,
SIZE_T AddressSize,
DWORD dwMilliseconds
volatile VOID * Address,
PVOID CompareAddress,
SIZE_T AddressSize,
DWORD dwMilliseconds
);
inline void(_stdcall *pWakeByAddressSingle)(
PVOID Address
PVOID Address
);
inline void(_stdcall *pWakeByAddressAll)(
PVOID Address
PVOID Address
);
inline DWORD(_stdcall *pNtDelayExecution)(
BOOLEAN Alertable,
PLARGE_INTEGER DelayInterval
BOOLEAN Alertable,
PLARGE_INTEGER DelayInterval
);
inline PVOID(__stdcall *pVirtualAlloc2)(
HANDLE Process,
PVOID BaseAddress,
SIZE_T Size,
ULONG AllocationType,
ULONG PageProtection,
MEM_EXTENDED_PARAMETER *ExtendedParameters,
ULONG ParameterCount
HANDLE Process,
PVOID BaseAddress,
SIZE_T Size,
ULONG AllocationType,
ULONG PageProtection,
MEM_EXTENDED_PARAMETER * ExtendedParameters,
ULONG ParameterCount
);
inline PVOID(__stdcall *pMapViewOfFile3)(
HANDLE FileMapping,
HANDLE Process,
PVOID BaseAddress,
ULONG64 Offset,
SIZE_T ViewSize,
ULONG AllocationType,
ULONG PageProtection,
MEM_EXTENDED_PARAMETER *ExtendedParameters,
ULONG ParameterCount
HANDLE FileMapping,
HANDLE Process,
PVOID BaseAddress,
ULONG64 Offset,
SIZE_T ViewSize,
ULONG AllocationType,
ULONG PageProtection,
MEM_EXTENDED_PARAMETER * ExtendedParameters,
ULONG ParameterCount
);
inline PVOID(__stdcall *pUnmapViewOfFile2)(
HANDLE Process,
PVOID BaseAddress,
ULONG UnmapFlags
HANDLE Process,
PVOID BaseAddress,
ULONG UnmapFlags
);
inline NTSTATUS(__stdcall *pNtWaitForKeyedEvent)(
HANDLE Handle,
PVOID Key,
BOOLEAN Alertable,
PLARGE_INTEGER NTTimeout
HANDLE Handle,
PVOID Key,
BOOLEAN Alertable,
PLARGE_INTEGER NTTimeout
);
inline NTSTATUS(__stdcall *pNtReleaseKeyedEvent)(
HANDLE Handle,
PVOID Key,
BOOLEAN Alertable,
PLARGE_INTEGER NTTimeout
HANDLE Handle,
PVOID Key,
BOOLEAN Alertable,
PLARGE_INTEGER NTTimeout
);
inline NTSTATUS(__stdcall *pNtCreateKeyedEvent)(
HANDLE Handle,
ACCESS_MASK Access,
POBJECT_ATTRIBUTES Attr,
ULONG Flags
HANDLE Handle,
ACCESS_MASK Access,
POBJECT_ATTRIBUTES Attr,
ULONG Flags
);
inline NTSTATUS(__stdcall *pNtOpenKeyedEvent)(
HANDLE Handle,
ACCESS_MASK Access,
POBJECT_ATTRIBUTES Attr,
ULONG Flags
HANDLE Handle,
ACCESS_MASK Access,
POBJECT_ATTRIBUTES Attr,
ULONG Flags
);
inline NTSTATUS(__stdcall *pRtlWaitOnAddress)(
const void * addr,
const void * cmp,
SIZE_T size,
const LARGE_INTEGER * timeout);
const void * addr,
const void * cmp,
SIZE_T size,
const LARGE_INTEGER * timeout);
#if defined(AURORA_PLATFORM_WIN32)
inline NTSTATUS(__stdcall *pRtlGetVersion)(
PRTL_OSVERSIONINFOW lpVersionInformation
PRTL_OSVERSIONINFOW lpVersionInformation
);
#endif
inline BOOL(__stdcall *pGetSystemCpuSetInformation)(
PSYSTEM_CPU_SET_INFORMATION Information,
ULONG BufferLength,
PULONG ReturnedLength,
HANDLE Process,
ULONG Flags
ULONG BufferLength,
PULONG ReturnedLength,
HANDLE Process,
ULONG Flags
);
inline BOOL(__stdcall * pGetLogicalProcessorInformation)(
inline BOOL(__stdcall *pGetLogicalProcessorInformation)(
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 {};

View File

@ -56,7 +56,7 @@ namespace Aurora::IO::Net
int iRet {};
HANDLE hHandle = NULL;
if (GetAddrInfoExCancel_f)
if (pGetAddrInfoExCancel)
{
iRet = GetAddrInfoExW(AuLocale::ConvertFromUTF8(this->hostname).data(),
nullptr,
@ -160,9 +160,9 @@ namespace Aurora::IO::Net
auto hHandle = AuExchange(this->hName_, (HANDLE)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);
gWin32NetReady = !WSAStartup(wVersionRequested, &wsaData);
GetAddrInfoExCancel_f = AuReinterpretCast<decltype(GetAddrInfoExCancel_f)>(GetProcAddress(GetModuleHandleW(L"Ws2_32.dll"), "GetAddrInfoExCancel"));
#endif
#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.
#if defined(AURORA_IS_MODERNNT_DERIVED)
inline INT(__stdcall *GetAddrInfoExCancel_f)(
LPHANDLE lpHandle
);
#endif
bool IsNetReady();
}

View File

@ -25,26 +25,12 @@
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)
{
#if defined(AURORA_IS_MODERNNT_DERIVED)
AuList<WIN32_MEMORY_RANGE_ENTRY2> arry;
if (!PrefetchVirtualMemory_f)
if (!pPrefetchVirtualMemory)
{
return;
}
@ -82,7 +68,7 @@ namespace Aurora::Memory::Cache
}
#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)");
}
@ -134,8 +120,6 @@ namespace Aurora::Memory::Cache
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)
RTL_OSVERSIONINFOEXW ovi {};
ovi.dwOSVersionInfoSize = sizeof(ovi);
NTSTATUS(CALLBACK *pRtlGetVersion) (PRTL_OSVERSIONINFOW lpVersionInformation);
pRtlGetVersion = reinterpret_cast<decltype(pRtlGetVersion)>(GetProcAddress(LoadLibraryW(L"Ntdll.dll"), "RtlGetVersion"));
if (pRtlGetVersion && pRtlGetVersion(reinterpret_cast<PRTL_OSVERSIONINFOW>(&ovi)) == 0)
if (pRtlGetVersion(reinterpret_cast<PRTL_OSVERSIONINFOW>(&ovi)) == 0)
{
osInfo.uKernelPatch = ovi.dwBuildNumber;
osInfo.uKernelMinor = ovi.dwMinorVersion;

View File

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