46 #ifndef _DLOAD_DLOADSUP_H
47 #define _DLOAD_DLOADSUP_H
50 #pragma warning(disable:4714) // forceinline function 'YieldProcessor' not inlined
51 #pragma warning(disable:28112) // Disable Prefast warning about variables being accessed through both interlocked and non-interlocked functions
53 #define DLOAD_INLINE __inline
55 #define FAST_FAIL_DLOAD_PROTECTION_FAILURE 25
56 #define IMAGE_GUARD_PROTECT_DELAYLOAD_IAT 0x00001000
60 #define DLOAD_ASSERT(_exp) \
62 (__annotation(L"Debug", L"AssertFail", L#_exp), \
63 DbgRaiseAssertionFailure(), FALSE) : \
68 #define DLOAD_ASSERT(_exp) ((void) 0)
99 #define DLOAD_BYTE_OFFSET(Va, PageSize) ((ULONG)((LONG_PTR)(Va) & (PageSize - 1)))
129 #define DLOAD_ADDRESS_AND_SIZE_TO_SPAN_PAGES(Va,Size,PageSize) \
130 ((ULONG)((((ULONG_PTR)(Size)) / PageSize) + ((DLOAD_BYTE_OFFSET (Va, PageSize) + DLOAD_BYTE_OFFSET (Size, PageSize) + PageSize - 1) / PageSize)))
132 #define SRWLOCK_UNINITIALIZED ((HMODULE)0x0)
133 #define SRWLOCK_UNSUPPORTED ((HMODULE)0x1)
135 typedef ULONG_PTR SRWLOCK_TYPE;
140 AcquireSRWLockExclusiveProc) (
147 ReleaseSRWLockExclusiveProc) (
152 AcquireSRWLockExclusiveProc DloadAcquireSRWLockExclusive;
153 ReleaseSRWLockExclusiveProc DloadReleaseSRWLockExclusive;
154 SRWLOCK_TYPE DloadSrwLock = 0x0;
155 ULONG DloadSectionLockCount;
156 DWORD DloadSectionOldProtection;
157 ULONG DloadSectionCommitPermanent;
159 extern "C" IMAGE_LOAD_CONFIG_DIRECTORY _load_config_used;
165 #define DLOAD_UNSUPPORTED ((HMODULE)0x1)
170 ResolveDelayLoadedAPIProc) (
182 ResolveDelayLoadsFromDllProc) (
203 DloadGetSRWLockFunctionPointers (
227 FARPROC FunctionPointer;
232 if (Kernel32 == SRWLOCK_UNSUPPORTED) {
235 if (Kernel32 !=
NULL) {
239 Kernel32 = GetModuleHandleW(L
"KERNEL32.DLL");
240 if (Kernel32 ==
NULL) {
241 Kernel32 = SRWLOCK_UNSUPPORTED;
245 FunctionPointer = GetProcAddress(Kernel32,
"AcquireSRWLockExclusive");
246 if (FunctionPointer ==
NULL) {
247 Kernel32 = SRWLOCK_UNSUPPORTED;
251 DloadAcquireSRWLockExclusive = (AcquireSRWLockExclusiveProc)FunctionPointer;
252 FunctionPointer = GetProcAddress(Kernel32,
"ReleaseSRWLockExclusive");
253 if (FunctionPointer ==
NULL) {
254 Kernel32 = SRWLOCK_UNSUPPORTED;
258 DloadReleaseSRWLockExclusive = (ReleaseSRWLockExclusiveProc)FunctionPointer;
263 SRWLOCK_UNINITIALIZED);
265 if (((OldValue == SRWLOCK_UNINITIALIZED) &&
266 (Kernel32 == SRWLOCK_UNSUPPORTED)) ||
267 (OldValue == SRWLOCK_UNSUPPORTED)) {
299 if (DloadGetSRWLockFunctionPointers() != FALSE) {
300 DloadAcquireSRWLockExclusive(&DloadSrwLock);
305 while (ReadPointerAcquire((
PVOID *)&DloadSrwLock) != 0) {
309 if (InterlockedCompareExchangePointer((
PVOID *)&DloadSrwLock, (
PVOID)1, 0) == 0) {
340 if (DloadGetSRWLockFunctionPointers() != FALSE) {
341 DloadReleaseSRWLockExclusive(&DloadSrwLock);
344 #pragma warning(suppress:6387) // Passing 0 is valid, but WritePointerRelease isn't annotated properly
345 WritePointerRelease((
PVOID *)&DloadSrwLock, 0);
355 _Out_ PULONG SectionSize,
356 _Out_ PULONG SectionCharacteristics
382 PIMAGE_DATA_DIRECTORY DataDir;
383 PCIMAGE_DELAYLOAD_DESCRIPTOR DloadDesc;
387 PIMAGE_NT_HEADERS NtHeaders;
389 PIMAGE_SECTION_HEADER SectionHeader;
391 ImageBase = (PUCHAR)&__ImageBase;
392 NtHeaders = (PIMAGE_NT_HEADERS)(ImageBase + __ImageBase.e_lfanew);
393 Entries = NtHeaders->OptionalHeader.NumberOfRvaAndSizes;
395 if (IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT >= Entries) {
399 DataDir = &NtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT];
400 Rva = DataDir->VirtualAddress;
405 DloadDesc = (PCIMAGE_DELAYLOAD_DESCRIPTOR)(ImageBase + Rva);
406 SectionHeader = IMAGE_FIRST_SECTION(NtHeaders);
407 Rva = DloadDesc->ImportAddressTableRVA;
409 Index < NtHeaders->FileHeader.NumberOfSections;
410 Index += 1, SectionHeader += 1) {
412 if ((Rva >= SectionHeader->VirtualAddress) &&
413 (Rva < SectionHeader->VirtualAddress + SectionHeader->Misc.VirtualSize)) {
415 *SectionSize = SectionHeader->Misc.VirtualSize;
416 *SectionCharacteristics = SectionHeader->Characteristics;
417 return ImageBase + SectionHeader->VirtualAddress;
427 DloadMakePermanentImageCommit (
464 MEMORY_BASIC_INFORMATION MemoryInfo;
468 SYSTEM_INFO SystemInfo;
487 if (VirtualQuery(BaseAddress, &MemoryInfo,
sizeof(MemoryInfo)) == 0) {
498 if ((MemoryInfo.Protect & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE)) == 0) {
502 GetSystemInfo(&SystemInfo);
504 PageSize = SystemInfo.dwPageSize;
516 PageCount = DLOAD_ADDRESS_AND_SIZE_TO_SPAN_PAGES(BaseAddress,
519 Page = (PUCHAR)((ULONG_PTR)BaseAddress & ~(PageSize - 1));
521 while (PageCount != 0) {
522 InterlockedOr((PLONG)Page, 0);
532 DloadProtectSection (
533 _In_ ULONG Protection,
534 _Out_ PULONG OldProtection
561 ULONG Characteristics;
565 Section = DloadObtainSection(&Size, &Characteristics);
567 if (Section ==
NULL) {
568 *OldProtection = PAGE_READWRITE;
581 if (DloadSectionCommitPermanent == 0) {
582 DloadSectionCommitPermanent = 1;
584 if ((Characteristics & IMAGE_SCN_MEM_WRITE) == 0) {
602 DloadMakePermanentImageCommit(Section, Size);
612 if (VirtualProtect(Section, Size, Protection, OldProtection) == FALSE) {
622 DloadAcquireSectionWriteAccess (
660 DloadSectionLockCount += 1;
661 if (DloadSectionLockCount == 1) {
662 DloadProtectSection(PAGE_READWRITE, &DloadSectionOldProtection);
672 DloadReleaseSectionWriteAccess (
710 DloadSectionLockCount -= 1;
711 if (DloadSectionLockCount == 0) {
712 DloadProtectSection(DloadSectionOldProtection, &OldProtect);
759 if (Kernel32 !=
NULL) {
763 Kernel32 = GetModuleHandleW(L
"api-ms-win-core-delayload-l1-1-1.dll");
764 if (Kernel32 ==
NULL) {
765 Kernel32 = GetModuleHandleW(L
"KERNEL32.DLL");
766 if (Kernel32 ==
NULL) {
772 DloadResolveDelayLoadedAPI =
773 (ResolveDelayLoadedAPIProc)GetProcAddress(Kernel32,
774 "ResolveDelayLoadedAPI");
776 if (DloadResolveDelayLoadedAPI ==
NULL) {
781 DloadResolveDelayLoadsFromDll =
782 (ResolveDelayLoadsFromDllProc)GetProcAddress(Kernel32,
783 "ResolveDelayLoadsFromDll");
785 if (DloadResolveDelayLoadsFromDll ==
NULL) {
791 WritePointerRelease((
PVOID *)&DloadKernel32, Kernel32);
799 _In_ PCIMAGE_DELAYLOAD_DESCRIPTOR DelayloadDescriptor,
800 _Out_ PIMAGE_THUNK_DATA ThunkAddress,
801 _Out_ PBOOLEAN NativeHandled
835 *NativeHandled = FALSE;
839 *NativeHandled = TRUE;
844 DelayLoadFailureHook,
856 _Out_ PBOOLEAN NativeHandled
887 *NativeHandled = FALSE;
888 return HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND);
891 *NativeHandled = TRUE;
897 if (Status == STATUS_DLL_NOT_FOUND) {
898 return HRESULT_FROM_WIN32(ERROR_MOD_NOT_FOUND);
#define _Out_
Definition: sal.h:342
#define FAST_FAIL_DLOAD_PROTECTION_FAILURE
Definition: dloadsup.h:55
#define NULL
Definition: vcruntime.h:236
#define S_OK
Definition: comutil.h:62
_In_ PCIMAGE_DELAYLOAD_DESCRIPTOR _In_opt_ PDELAYLOAD_FAILURE_DLL_CALLBACK _In_opt_ PDELAYLOAD_FAILURE_SYSTEM_ROUTINE _Out_ PIMAGE_THUNK_DATA _Reserved_ ULONG Flags
Definition: dloadsup.h:172
#define _Inout_updates_(size)
Definition: sal.h:384
typedef NTSTATUS(NTAPI *ResolveDelayLoadsFromDllProc)(_In_ PVOID ParentBase
#define DLOAD_ASSERT(_exp)
Definition: dloadsup.h:68
_In_ PCIMAGE_DELAYLOAD_DESCRIPTOR _In_opt_ PDELAYLOAD_FAILURE_DLL_CALLBACK _In_opt_ PDELAYLOAD_FAILURE_SYSTEM_ROUTINE FailureSystemHook
Definition: dloadsup.h:172
DLOAD_INLINE BOOLEAN DloadResolve(VOID)
Definition: dloadsup.h:729
typedef PVOID(NTAPI *ResolveDelayLoadedAPIProc)(_In_ PVOID ParentModuleBase
#define _Releases_exclusive_lock_(lock)
Definition: ConcurrencySal.h:282
DLOAD_INLINE HRESULT WINAPI Dload__HrLoadAllImportsForDll(_In_ LPCSTR DllName, _Out_ PBOOLEAN NativeHandled)
Definition: dloadsup.h:854
#define _In_
Definition: sal.h:305
#define _In_opt_
Definition: sal.h:306
#define IMAGE_GUARD_PROTECT_DELAYLOAD_IAT
Definition: dloadsup.h:56
#define DLOAD_INLINE
Definition: dloadsup.h:53
ResolveDelayLoadedAPIProc DloadResolveDelayLoadedAPI
Definition: dloadsup.h:189
_In_ PCIMAGE_DELAYLOAD_DESCRIPTOR _In_opt_ PDELAYLOAD_FAILURE_DLL_CALLBACK _In_opt_ PDELAYLOAD_FAILURE_SYSTEM_ROUTINE _Out_ PIMAGE_THUNK_DATA ThunkAddress
Definition: dloadsup.h:172
#define DLOAD_UNSUPPORTED
Definition: dloadsup.h:165
unsigned long DWORD
Definition: concrt.h:63
_In_ PCIMAGE_DELAYLOAD_DESCRIPTOR _In_opt_ PDELAYLOAD_FAILURE_DLL_CALLBACK FailureDllHook
Definition: dloadsup.h:172
ResolveDelayLoadsFromDllProc DloadResolveDelayLoadsFromDll
Definition: dloadsup.h:190
const IMAGE_DOS_HEADER __ImageBase
Definition: delayhlp.cpp:95
_In_ PCIMAGE_DELAYLOAD_DESCRIPTOR DelayloadDescriptor
Definition: dloadsup.h:172
ExternC const PfnDliHook __pfnDliFailureHook2
Definition: delayimp.h:141
#define _Acquires_exclusive_lock_(lock)
Definition: ConcurrencySal.h:279
DLOAD_INLINE PVOID WINAPI Dload__delayLoadHelper2(_In_ PCIMAGE_DELAYLOAD_DESCRIPTOR DelayloadDescriptor, _Out_ PIMAGE_THUNK_DATA ThunkAddress, _Out_ PBOOLEAN NativeHandled)
Definition: dloadsup.h:798
#define _Inout_
Definition: sal.h:375
#define _Success_(expr)
Definition: sal.h:256
#define _Reserved_
Definition: sal.h:292
_In_ LPCSTR TargetDllName
Definition: dloadsup.h:184
HMODULE DloadKernel32
Definition: dloadsup.h:188