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 http://mozilla.org/MPL/2.0/. */
6
7 #include "ModuleLoadFrame.h"
8 #include "mozilla/NativeNt.h"
9 #include "mozilla/UniquePtr.h"
10 #include "NtLoaderAPI.h"
11
12 #include <string.h>
13
14 #include "WindowsFallbackLoaderAPI.h"
15
IsNullTerminated(PCUNICODE_STRING aStr)16 static bool IsNullTerminated(PCUNICODE_STRING aStr) {
17 return aStr && (aStr->MaximumLength >= (aStr->Length + sizeof(WCHAR))) &&
18 aStr->Buffer && aStr->Buffer[aStr->Length / sizeof(WCHAR)] == 0;
19 }
20
21 static mozilla::FallbackLoaderAPI gFallbackLoaderAPI;
22
23 namespace mozilla {
24 namespace glue {
25
26 nt::LoaderAPI* ModuleLoadFrame::sLoaderAPI;
27
28 using GetNtLoaderAPIFn = decltype(&mozilla::GetNtLoaderAPI);
29
30 /* static */
StaticInit(nt::LoaderObserver * aNewObserver,nt::WinLauncherFunctions * aOutWinLauncherFunctions)31 void ModuleLoadFrame::StaticInit(
32 nt::LoaderObserver* aNewObserver,
33 nt::WinLauncherFunctions* aOutWinLauncherFunctions) {
34 const auto pGetNtLoaderAPI = reinterpret_cast<GetNtLoaderAPIFn>(
35 ::GetProcAddress(::GetModuleHandleW(nullptr), "GetNtLoaderAPI"));
36 if (!pGetNtLoaderAPI) {
37 // This case occurs in processes other than firefox.exe that do not contain
38 // the launcher process blocklist.
39 gFallbackLoaderAPI.SetObserver(aNewObserver);
40 sLoaderAPI = &gFallbackLoaderAPI;
41
42 if (aOutWinLauncherFunctions) {
43 aOutWinLauncherFunctions->mHandleLauncherError =
44 [](const mozilla::LauncherError&, const char*) {};
45 // We intentionally leave mInitDllBlocklistOOP null to make sure calling
46 // mInitDllBlocklistOOP in non-Firefox hits MOZ_RELEASE_ASSERT.
47 }
48 return;
49 }
50
51 sLoaderAPI = pGetNtLoaderAPI(aNewObserver);
52 MOZ_ASSERT(sLoaderAPI);
53
54 if (aOutWinLauncherFunctions) {
55 aOutWinLauncherFunctions->mInitDllBlocklistOOP =
56 sLoaderAPI->GetDllBlocklistInitFn();
57 aOutWinLauncherFunctions->mHandleLauncherError =
58 sLoaderAPI->GetHandleLauncherErrorFn();
59 }
60 }
61
ModuleLoadFrame(PCUNICODE_STRING aRequestedDllName)62 ModuleLoadFrame::ModuleLoadFrame(PCUNICODE_STRING aRequestedDllName)
63 : mAlreadyLoaded(false),
64 mContext(nullptr),
65 mDllLoadStatus(STATUS_UNSUCCESSFUL),
66 mLoadInfo(sLoaderAPI->ConstructAndNotifyBeginDllLoad(&mContext,
67 aRequestedDllName)) {
68 if (!aRequestedDllName) {
69 return;
70 }
71
72 UniquePtr<WCHAR[]> nameBuf;
73 const WCHAR* name = nullptr;
74
75 if (IsNullTerminated(aRequestedDllName)) {
76 name = aRequestedDllName->Buffer;
77 } else {
78 USHORT charLenExclNul = aRequestedDllName->Length / sizeof(WCHAR);
79 USHORT charLenInclNul = charLenExclNul + 1;
80 nameBuf = MakeUnique<WCHAR[]>(charLenInclNul);
81 if (!wcsncpy_s(nameBuf.get(), charLenInclNul, aRequestedDllName->Buffer,
82 charLenExclNul)) {
83 name = nameBuf.get();
84 }
85 }
86
87 mAlreadyLoaded = name && !!::GetModuleHandleW(name);
88 }
89
~ModuleLoadFrame()90 ModuleLoadFrame::~ModuleLoadFrame() {
91 sLoaderAPI->NotifyEndDllLoad(mContext, mDllLoadStatus, std::move(mLoadInfo));
92 }
93
SetLoadStatus(NTSTATUS aNtStatus,HANDLE aHandle)94 void ModuleLoadFrame::SetLoadStatus(NTSTATUS aNtStatus, HANDLE aHandle) {
95 mDllLoadStatus = aNtStatus;
96 void* baseAddr = mozilla::nt::PEHeaders::HModuleToBaseAddr<void*>(
97 reinterpret_cast<HMODULE>(aHandle));
98 mLoadInfo.mBaseAddr = baseAddr;
99 if (!mAlreadyLoaded) {
100 mLoadInfo.mSectionName = sLoaderAPI->GetSectionName(baseAddr);
101 }
102 }
103
104 } // namespace glue
105 } // namespace mozilla
106