|
| 1 | +/* |
| 2 | + * PROJECT: ReactOS NT Layer/System API |
| 3 | + * LICENSE: MIT (https://spdx.org/licenses/MIT) |
| 4 | + * PURPOSE: DLL Load Notification Implementation |
| 5 | + * COPYRIGHT: Copyright 2024 Ratin Gao <[email protected]> |
| 6 | + */ |
| 7 | + |
| 8 | +#include "ntdll_vista.h" |
| 9 | + |
| 10 | +/* GLOBALS *******************************************************************/ |
| 11 | + |
| 12 | +typedef struct _LDR_DLL_NOTIFICATION_ENTRY |
| 13 | +{ |
| 14 | + LIST_ENTRY List; |
| 15 | + PLDR_DLL_NOTIFICATION_FUNCTION Callback; |
| 16 | + PVOID Context; |
| 17 | +} LDR_DLL_NOTIFICATION_ENTRY, *PLDR_DLL_NOTIFICATION_ENTRY; |
| 18 | + |
| 19 | +static RTL_STATIC_LIST_HEAD(LdrpDllNotificationList); |
| 20 | + |
| 21 | +/* Initialize critical section statically */ |
| 22 | +static RTL_CRITICAL_SECTION LdrpDllNotificationLock; |
| 23 | +static RTL_CRITICAL_SECTION_DEBUG LdrpDllNotificationLockDebug = { |
| 24 | + .CriticalSection = &LdrpDllNotificationLock |
| 25 | +}; |
| 26 | +static RTL_CRITICAL_SECTION LdrpDllNotificationLock = { |
| 27 | + &LdrpDllNotificationLockDebug, |
| 28 | + -1, |
| 29 | + 0, |
| 30 | + 0, |
| 31 | + 0, |
| 32 | + 0 |
| 33 | +}; |
| 34 | + |
| 35 | +/* FUNCTIONS *****************************************************************/ |
| 36 | + |
| 37 | +NTSTATUS |
| 38 | +NTAPI |
| 39 | +LdrRegisterDllNotification( |
| 40 | + _In_ ULONG Flags, |
| 41 | + _In_ PLDR_DLL_NOTIFICATION_FUNCTION NotificationFunction, |
| 42 | + _In_opt_ PVOID Context, |
| 43 | + _Out_ PVOID *Cookie) |
| 44 | +{ |
| 45 | + PLDR_DLL_NOTIFICATION_ENTRY NewEntry; |
| 46 | + |
| 47 | + /* Check input parameters */ |
| 48 | + if (Flags != 0 || NotificationFunction == NULL || Cookie == NULL) |
| 49 | + { |
| 50 | + return STATUS_INVALID_PARAMETER; |
| 51 | + } |
| 52 | + |
| 53 | + /* Allocate new entry and assign input values */ |
| 54 | + NewEntry = RtlAllocateHeap(LdrpHeap, 0, sizeof(*NewEntry)); |
| 55 | + if (NewEntry == NULL) |
| 56 | + { |
| 57 | + return STATUS_NO_MEMORY; |
| 58 | + } |
| 59 | + NewEntry->Callback = NotificationFunction; |
| 60 | + NewEntry->Context = Context; |
| 61 | + |
| 62 | + /* Add node to the end of global list */ |
| 63 | + RtlEnterCriticalSection(&LdrpDllNotificationLock); |
| 64 | + InsertTailList(&LdrpDllNotificationList, &NewEntry->List); |
| 65 | + RtlLeaveCriticalSection(&LdrpDllNotificationLock); |
| 66 | + |
| 67 | + /* Cookie is address of the new entry */ |
| 68 | + *Cookie = NewEntry; |
| 69 | + return STATUS_SUCCESS; |
| 70 | +} |
| 71 | + |
| 72 | +NTSTATUS |
| 73 | +NTAPI |
| 74 | +LdrUnregisterDllNotification( |
| 75 | + _In_ PVOID Cookie) |
| 76 | +{ |
| 77 | + NTSTATUS Status = STATUS_DLL_NOT_FOUND; |
| 78 | + PLIST_ENTRY Entry; |
| 79 | + |
| 80 | + /* Find entry to remove */ |
| 81 | + RtlEnterCriticalSection(&LdrpDllNotificationLock); |
| 82 | + for (Entry = LdrpDllNotificationList.Flink; |
| 83 | + Entry != &LdrpDllNotificationList; |
| 84 | + Entry = Entry->Flink) |
| 85 | + { |
| 86 | + if (Entry == Cookie) |
| 87 | + { |
| 88 | + RemoveEntryList(Entry); |
| 89 | + Status = STATUS_SUCCESS; |
| 90 | + break; |
| 91 | + } |
| 92 | + } |
| 93 | + RtlLeaveCriticalSection(&LdrpDllNotificationLock); |
| 94 | + |
| 95 | + if (NT_SUCCESS(Status)) |
| 96 | + { |
| 97 | + RtlFreeHeap(LdrpHeap, 0, Entry); |
| 98 | + } |
| 99 | + return Status; |
| 100 | +} |
| 101 | + |
| 102 | +VOID |
| 103 | +NTAPI |
| 104 | +LdrpSendDllNotifications( |
| 105 | + _In_ PLDR_DATA_TABLE_ENTRY DllEntry, |
| 106 | + _In_ ULONG NotificationReason) |
| 107 | +{ |
| 108 | + PLIST_ENTRY Entry; |
| 109 | + PLDR_DLL_NOTIFICATION_ENTRY NotificationEntry; |
| 110 | + LDR_DLL_NOTIFICATION_DATA NotificationData; |
| 111 | + |
| 112 | + /* |
| 113 | + * LDR_DLL_LOADED_NOTIFICATION_DATA and LDR_DLL_UNLOADED_NOTIFICATION_DATA |
| 114 | + * currently are the same. Use C_ASSERT to ensure it, then fill either of them. |
| 115 | + */ |
| 116 | +#define LdrpAssertDllNotificationDataMember(x)\ |
| 117 | + C_ASSERT(FIELD_OFFSET(LDR_DLL_NOTIFICATION_DATA, Loaded.x) ==\ |
| 118 | + FIELD_OFFSET(LDR_DLL_NOTIFICATION_DATA, Unloaded.x)) |
| 119 | + |
| 120 | + C_ASSERT(sizeof(NotificationData.Loaded) == sizeof(NotificationData.Unloaded)); |
| 121 | + LdrpAssertDllNotificationDataMember(Flags); |
| 122 | + LdrpAssertDllNotificationDataMember(FullDllName); |
| 123 | + LdrpAssertDllNotificationDataMember(BaseDllName); |
| 124 | + LdrpAssertDllNotificationDataMember(DllBase); |
| 125 | + LdrpAssertDllNotificationDataMember(SizeOfImage); |
| 126 | + |
| 127 | +#undef LdrpAssertDllNotificationDataMember |
| 128 | + |
| 129 | + NotificationData.Loaded.Flags = 0; /* Reserved and always 0, not DllEntry->Flags */ |
| 130 | + NotificationData.Loaded.FullDllName = &DllEntry->FullDllName; |
| 131 | + NotificationData.Loaded.BaseDllName = &DllEntry->BaseDllName; |
| 132 | + NotificationData.Loaded.DllBase = DllEntry->DllBase; |
| 133 | + NotificationData.Loaded.SizeOfImage = DllEntry->SizeOfImage; |
| 134 | + |
| 135 | + /* Send notification to all registered callbacks */ |
| 136 | + RtlEnterCriticalSection(&LdrpDllNotificationLock); |
| 137 | + _SEH2_TRY |
| 138 | + { |
| 139 | + for (Entry = LdrpDllNotificationList.Flink; |
| 140 | + Entry != &LdrpDllNotificationList; |
| 141 | + Entry = Entry->Flink) |
| 142 | + { |
| 143 | + NotificationEntry = CONTAINING_RECORD(Entry, LDR_DLL_NOTIFICATION_ENTRY, List); |
| 144 | + NotificationEntry->Callback(NotificationReason, |
| 145 | + &NotificationData, |
| 146 | + NotificationEntry->Context); |
| 147 | + } |
| 148 | + } |
| 149 | + _SEH2_FINALLY |
| 150 | + { |
| 151 | + RtlLeaveCriticalSection(&LdrpDllNotificationLock); |
| 152 | + } |
| 153 | + _SEH2_END; |
| 154 | +} |
0 commit comments