Bug 1503538 Part 2/5: Move AutoVirtualProtect to a shared header r=aklotz
authorCarl Corcoran <ccorcoran@mozilla.com>
Wed, 06 Mar 2019 00:40:55 +0000
changeset 520365 0fd1ec8af3fd1e2a75f08613414ed590e76ea286
parent 520364 094cdc0182bc8cc9eb5bcf741a951d147588b77a
child 520366 ed8a0908ddf00371f6f629954116fcf2bc6950b1
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs1503538
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1503538 Part 2/5: Move AutoVirtualProtect to a shared header r=aklotz AutoVirtualProtect will be useful for following patches. This moves it out of DllBlocklistWin.cpp and into WinHeaderOnlyUtils.h so it can be shared. Differential Revision: https://phabricator.services.mozilla.com/D13197
browser/app/winlauncher/DllBlocklistWin.cpp
widget/windows/WinHeaderOnlyUtils.h
--- a/browser/app/winlauncher/DllBlocklistWin.cpp
+++ b/browser/app/winlauncher/DllBlocklistWin.cpp
@@ -301,57 +301,16 @@ static NTSTATUS NTAPI patched_NtMapViewO
   }
 
   ::NtUnmapViewOfSection(aProcess, *aBaseAddress);
   return STATUS_ACCESS_DENIED;
 }
 
 namespace mozilla {
 
-class MOZ_RAII AutoVirtualProtect final {
- public:
-  AutoVirtualProtect(void* aAddress, size_t aLength, DWORD aProtFlags,
-                     HANDLE aTargetProcess = nullptr)
-      : mAddress(aAddress),
-        mLength(aLength),
-        mTargetProcess(aTargetProcess),
-        mPrevProt(0),
-        mError(WindowsError::CreateSuccess()) {
-    if (!::VirtualProtectEx(aTargetProcess, aAddress, aLength, aProtFlags,
-                            &mPrevProt)) {
-      mError = WindowsError::FromLastError();
-    }
-  }
-
-  ~AutoVirtualProtect() {
-    if (mError.IsFailure()) {
-      return;
-    }
-
-    ::VirtualProtectEx(mTargetProcess, mAddress, mLength, mPrevProt,
-                       &mPrevProt);
-  }
-
-  explicit operator bool() const { return mError.IsSuccess(); }
-
-  WindowsError GetError() const { return mError; }
-
-  AutoVirtualProtect(const AutoVirtualProtect&) = delete;
-  AutoVirtualProtect(AutoVirtualProtect&&) = delete;
-  AutoVirtualProtect& operator=(const AutoVirtualProtect&) = delete;
-  AutoVirtualProtect& operator=(AutoVirtualProtect&&) = delete;
-
- private:
-  void* mAddress;
-  size_t mLength;
-  HANDLE mTargetProcess;
-  DWORD mPrevProt;
-  WindowsError mError;
-};
-
 LauncherVoidResult InitializeDllBlocklistOOP(HANDLE aChildProcess) {
   mozilla::CrossProcessDllInterceptor intcpt(aChildProcess);
   intcpt.Init(L"ntdll.dll");
   bool ok = stub_NtMapViewOfSection.SetDetour(
       aChildProcess, intcpt, "NtMapViewOfSection", &patched_NtMapViewOfSection);
   if (!ok) {
     return LAUNCHER_ERROR_GENERIC();
   }
--- a/widget/windows/WinHeaderOnlyUtils.h
+++ b/widget/windows/WinHeaderOnlyUtils.h
@@ -356,11 +356,52 @@ inline WindowsErrorResult<bool> DoPathsP
   if (!id2) {
     Maybe<WindowsError> error = id2.GetError();
     return Err(error.valueOr(WindowsError::CreateGeneric()));
   }
 
   return id1 == id2;
 }
 
+class MOZ_RAII AutoVirtualProtect final {
+ public:
+  AutoVirtualProtect(void* aAddress, size_t aLength, DWORD aProtFlags,
+                     HANDLE aTargetProcess = nullptr)
+      : mAddress(aAddress),
+        mLength(aLength),
+        mTargetProcess(aTargetProcess),
+        mPrevProt(0),
+        mError(WindowsError::CreateSuccess()) {
+    if (!::VirtualProtectEx(aTargetProcess, aAddress, aLength, aProtFlags,
+                            &mPrevProt)) {
+      mError = WindowsError::FromLastError();
+    }
+  }
+
+  ~AutoVirtualProtect() {
+    if (mError.IsFailure()) {
+      return;
+    }
+
+    ::VirtualProtectEx(mTargetProcess, mAddress, mLength, mPrevProt,
+                       &mPrevProt);
+  }
+
+  explicit operator bool() const { return mError.IsSuccess(); }
+
+  WindowsError GetError() const { return mError; }
+
+  AutoVirtualProtect(const AutoVirtualProtect&) = delete;
+  AutoVirtualProtect(AutoVirtualProtect&&) = delete;
+  AutoVirtualProtect& operator=(const AutoVirtualProtect&) = delete;
+  AutoVirtualProtect& operator=(AutoVirtualProtect&&) = delete;
+
+ private:
+  void* mAddress;
+  size_t mLength;
+  HANDLE mTargetProcess;
+  DWORD mPrevProt;
+  WindowsError mError;
+};
+
 }  // namespace mozilla
 
 #endif  // mozilla_WinHeaderOnlyUtils_h