From 7fe74ec057418dcda46e4ad83b1dd1b16ac55ece Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Wed, 11 Oct 2023 01:47:20 +0100 Subject: [PATCH] [*] drop win32 requirements --- src/os.c | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/os.c b/src/os.c index 22df67f..3bac953 100644 --- a/src/os.c +++ b/src/os.c @@ -143,6 +143,19 @@ static DWORD(__stdcall* pGetLargePageMinimum)() = NULL; static BOOL(__stdcall* pGetNumaHighestNodeNumber)(PULONG HighestNodeNumber) = NULL; static BOOL(__stdcall* pGetNumaProcessorNode)(UCHAR Processor, PUCHAR NodeNumber) = NULL; static BOOL(__stdcall* pGetNumaNodeProcessorMask)(UCHAR Processor, PULONGLONG ProcessorMask) = NULL; +static BOOL(__stdcall* pLookupPrivilegeValueW)(LPCWSTR lpSystemName, + LPCWSTR lpName, + PLUID lpLuid) = NULL; +static BOOL(__stdcall* pAdjustTokenPrivileges)(HANDLE TokenHandle, + BOOL DisableAllPrivileges, + PTOKEN_PRIVILEGES NewState, + DWORD BufferLength, + PTOKEN_PRIVILEGES PreviousState, + PDWORD ReturnLength) = NULL; +static BOOL(__stdcall* pOpenProcessToken)( + HANDLE ProcessHandle, + DWORD DesiredAccess, + PHANDLE TokenHandle) = NULL; static bool mi_win_enable_large_os_pages() { @@ -155,14 +168,17 @@ static bool mi_win_enable_large_os_pages() // unsigned long err = 0; HANDLE token = NULL; - BOOL ok = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token); + BOOL ok = pOpenProcessToken && + pOpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token); if (ok) { TOKEN_PRIVILEGES tp; - ok = LookupPrivilegeValue(NULL, TEXT("SeLockMemoryPrivilege"), &tp.Privileges[0].Luid); + ok = pLookupPrivilegeValueW && + pLookupPrivilegeValueW(NULL, L"SeLockMemoryPrivilege", &tp.Privileges[0].Luid); if (ok) { tp.PrivilegeCount = 1; tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; - ok = AdjustTokenPrivileges(token, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0); + ok = pAdjustTokenPrivileges && + pAdjustTokenPrivileges(token, FALSE, &tp, 0, (PTOKEN_PRIVILEGES)NULL, 0); if (ok) { err = GetLastError(); ok = (err == ERROR_SUCCESS); @@ -211,6 +227,25 @@ void _mi_os_init(void) { pGetNumaProcessorNode = (BOOL(__stdcall*)(UCHAR, PUCHAR))GetProcAddress(hDll, "GetNumaProcessorNode"); pGetNumaNodeProcessorMask = (BOOL(__stdcall*)(UCHAR, PULONGLONG))GetProcAddress(hDll, "GetNumaNodeProcessorMask"); } + + hDll = LoadLibrary(TEXT("Advapi32.dll")); + if (hDll != NULL) { + pLookupPrivilegeValueW = (BOOL(__stdcall*)(LPCWSTR, + LPCWSTR, + PLUID))GetProcAddress(hDll, "LookupPrivilegeValueW"); + + pAdjustTokenPrivileges = (BOOL(__stdcall*)(HANDLE, + BOOL, + PTOKEN_PRIVILEGES, + DWORD, + PTOKEN_PRIVILEGES, + PDWORD))GetProcAddress(hDll, "AdjustTokenPrivileges"); + + pOpenProcessToken = (BOOL(__stdcall*)( + HANDLE , + DWORD, + PHANDLE))GetProcAddress(hDll, "OpenProcessToken"); + } if (mi_option_is_enabled(mi_option_large_os_pages) || mi_option_is_enabled(mi_option_reserve_huge_os_pages)) { mi_win_enable_large_os_pages(); }