1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
6 
7 #include "WindowsFallbackLoaderAPI.h"
8 
9 namespace mozilla {
10 
ConstructAndNotifyBeginDllLoad(void ** aContext,PCUNICODE_STRING aRequestedDllName)11 ModuleLoadInfo FallbackLoaderAPI::ConstructAndNotifyBeginDllLoad(
12     void** aContext, PCUNICODE_STRING aRequestedDllName) {
13   ModuleLoadInfo loadInfo(aRequestedDllName);
14 
15   MOZ_ASSERT(mLoaderObserver);
16   if (mLoaderObserver) {
17     mLoaderObserver->OnBeginDllLoad(aContext, aRequestedDllName);
18   }
19 
20   return loadInfo;
21 }
22 
SubstituteForLSP(PCUNICODE_STRING aLSPLeafName,PHANDLE aOutHandle)23 bool FallbackLoaderAPI::SubstituteForLSP(PCUNICODE_STRING aLSPLeafName,
24                                          PHANDLE aOutHandle) {
25   MOZ_ASSERT(mLoaderObserver);
26   if (!mLoaderObserver) {
27     return false;
28   }
29 
30   return mLoaderObserver->SubstituteForLSP(aLSPLeafName, aOutHandle);
31 }
32 
NotifyEndDllLoad(void * aContext,NTSTATUS aLoadNtStatus,ModuleLoadInfo && aModuleLoadInfo)33 void FallbackLoaderAPI::NotifyEndDllLoad(void* aContext, NTSTATUS aLoadNtStatus,
34                                          ModuleLoadInfo&& aModuleLoadInfo) {
35   aModuleLoadInfo.SetEndLoadTimeStamp();
36 
37   if (NT_SUCCESS(aLoadNtStatus)) {
38     aModuleLoadInfo.CaptureBacktrace();
39   }
40 
41   MOZ_ASSERT(mLoaderObserver);
42   if (mLoaderObserver) {
43     mLoaderObserver->OnEndDllLoad(aContext, aLoadNtStatus,
44                                   std::move(aModuleLoadInfo));
45   }
46 }
47 
GetSectionName(void * aSectionAddr)48 nt::AllocatedUnicodeString FallbackLoaderAPI::GetSectionName(
49     void* aSectionAddr) {
50   static const StaticDynamicallyLinkedFunctionPtr<
51       decltype(&::NtQueryVirtualMemory)>
52       pNtQueryVirtualMemory(L"ntdll.dll", "NtQueryVirtualMemory");
53   MOZ_ASSERT(pNtQueryVirtualMemory);
54 
55   if (!pNtQueryVirtualMemory) {
56     return nt::AllocatedUnicodeString();
57   }
58 
59   nt::MemorySectionNameBuf buf;
60   NTSTATUS ntStatus =
61       pNtQueryVirtualMemory(::GetCurrentProcess(), aSectionAddr,
62                             MemorySectionName, &buf, sizeof(buf), nullptr);
63   if (!NT_SUCCESS(ntStatus)) {
64     return nt::AllocatedUnicodeString();
65   }
66 
67   return nt::AllocatedUnicodeString(&buf.mSectionFileName);
68 }
69 
70 nt::LoaderAPI::InitDllBlocklistOOPFnPtr
GetDllBlocklistInitFn()71 FallbackLoaderAPI::GetDllBlocklistInitFn() {
72   MOZ_ASSERT_UNREACHABLE("This should not be called so soon!");
73   return nullptr;
74 }
75 
76 nt::LoaderAPI::HandleLauncherErrorFnPtr
GetHandleLauncherErrorFn()77 FallbackLoaderAPI::GetHandleLauncherErrorFn() {
78   MOZ_ASSERT_UNREACHABLE("This should not be called so soon!");
79   return nullptr;
80 }
81 
SetObserver(nt::LoaderObserver * aLoaderObserver)82 void FallbackLoaderAPI::SetObserver(nt::LoaderObserver* aLoaderObserver) {
83   mLoaderObserver = aLoaderObserver;
84 }
85 
86 }  // namespace mozilla
87