Skip to content

Commit e8742bd

Browse files
maximmasiutinvondele
authored andcommitted
Made advanced Windows API calls dynamically linked
Made advanced Windows API calls (those from Advapi32.dll) dynamically linked to avoid link errors when compiling using Intel icx compiler for Windows. #4467 No functional change
1 parent 7a6fa34 commit e8742bd

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/misc.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ using fun2_t = bool(*)(USHORT, PGROUP_AFFINITY);
3838
using fun3_t = bool(*)(HANDLE, CONST GROUP_AFFINITY*, PGROUP_AFFINITY);
3939
using fun4_t = bool(*)(USHORT, PGROUP_AFFINITY, USHORT, PUSHORT);
4040
using fun5_t = WORD(*)();
41+
using fun6_t = bool(*)(HANDLE, DWORD, PHANDLE);
42+
using fun7_t = bool(*)(LPCSTR, LPCSTR, PLUID);
43+
using fun8_t = bool(*)(HANDLE, BOOL, PTOKEN_PRIVILEGES, DWORD, PTOKEN_PRIVILEGES, PDWORD);
4144
}
4245
#endif
4346

@@ -488,11 +491,26 @@ static void* aligned_large_pages_alloc_windows([[maybe_unused]] size_t allocSize
488491
if (!largePageSize)
489492
return nullptr;
490493

494+
// Dynamically link OpenProcessToken, LookupPrivilegeValue and AdjustTokenPrivileges
495+
HMODULE k32 = GetModuleHandle("Advapi32.dll");
496+
auto fun6 = (fun6_t)(void(*)())GetProcAddress(k32, "OpenProcessToken");
497+
if (!fun6)
498+
return nullptr;
499+
auto fun7 = (fun7_t)(void(*)())GetProcAddress(k32, "LookupPrivilegeValueA");
500+
if (!fun7)
501+
return nullptr;
502+
auto fun8 = (fun8_t)(void(*)())GetProcAddress(k32, "AdjustTokenPrivileges");
503+
if (!fun8)
504+
return nullptr;
505+
506+
491507
// We need SeLockMemoryPrivilege, so try to enable it for the process
492-
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hProcessToken))
508+
// OpenProcessToken()
509+
if (!fun6(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hProcessToken))
493510
return nullptr;
494511

495-
if (LookupPrivilegeValue(nullptr, SE_LOCK_MEMORY_NAME, &luid))
512+
// LookupPrivilegeValueA()
513+
if (fun7(nullptr, SE_LOCK_MEMORY_NAME, &luid))
496514
{
497515
TOKEN_PRIVILEGES tp { };
498516
TOKEN_PRIVILEGES prevTp { };
@@ -504,7 +522,8 @@ static void* aligned_large_pages_alloc_windows([[maybe_unused]] size_t allocSize
504522

505523
// Try to enable SeLockMemoryPrivilege. Note that even if AdjustTokenPrivileges() succeeds,
506524
// we still need to query GetLastError() to ensure that the privileges were actually obtained.
507-
if (AdjustTokenPrivileges(
525+
// AdjustTokenPrivileges()
526+
if (fun8(
508527
hProcessToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), &prevTp, &prevTpLen) &&
509528
GetLastError() == ERROR_SUCCESS)
510529
{
@@ -514,7 +533,8 @@ static void* aligned_large_pages_alloc_windows([[maybe_unused]] size_t allocSize
514533
nullptr, allocSize, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);
515534

516535
// Privilege no longer needed, restore previous state
517-
AdjustTokenPrivileges(hProcessToken, FALSE, &prevTp, 0, nullptr, nullptr);
536+
// AdjustTokenPrivileges ()
537+
fun8(hProcessToken, FALSE, &prevTp, 0, nullptr, nullptr);
518538
}
519539
}
520540

0 commit comments

Comments
 (0)