Bug 1568610 - Delete the definition of IATThunks structure. r=aklotz
authorToshihito Kikuchi <tkikuchi@mozilla.com>
Tue, 06 Aug 2019 22:58:20 +0000
changeset 486628 3689f85b1c3962f54d6ec04a1d7af80a0ad4cd2b
parent 486627 a2a038ad11fd73df887fc517275f676c2e2fbb98
child 486629 e4b8877a88384c1456a82804845bcee14bdbcdd0
push id36400
push useraciure@mozilla.com
push dateWed, 07 Aug 2019 04:33:53 +0000
treeherdermozilla-central@36592e14f6ce [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaklotz
bugs1568610
milestone70.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 1568610 - Delete the definition of IATThunks structure. r=aklotz Differential Revision: https://phabricator.services.mozilla.com/D40703
browser/app/winlauncher/DllBlocklistWin.cpp
mozglue/misc/NativeNt.h
mozglue/tests/TestNativeNt.cpp
--- a/browser/app/winlauncher/DllBlocklistWin.cpp
+++ b/browser/app/winlauncher/DllBlocklistWin.cpp
@@ -364,27 +364,28 @@ LauncherVoidResult InitializeDllBlocklis
   // As part of our mitigation of binary tampering, copy our import directory
   // from the original in our executable file.
   LauncherVoidResult importDirRestored = RestoreImportDirectory(
       aFullImagePath, ourExeImage, aChildProcess, ourModule);
   if (importDirRestored.isErr()) {
     return importDirRestored;
   }
 
-  Maybe<nt::PEHeaders::IATThunks> ntdllThunks =
+  Maybe<Span<IMAGE_THUNK_DATA>> ntdllThunks =
       ourExeImage.GetIATThunksForModule("ntdll.dll");
   if (!ntdllThunks) {
     return LAUNCHER_ERROR_FROM_WIN32(ERROR_INVALID_DATA);
   }
 
-  PIMAGE_THUNK_DATA firstIatThunk = ntdllThunks.value().mFirstThunk;
-  SIZE_T iatLength = ntdllThunks.value().Length();
   SIZE_T bytesWritten;
 
   {  // Scope for prot
+    PIMAGE_THUNK_DATA firstIatThunk = ntdllThunks.value().data();
+    SIZE_T iatLength = ntdllThunks.value().LengthBytes();
+
     AutoVirtualProtect prot(firstIatThunk, iatLength, PAGE_READWRITE,
                             aChildProcess);
     if (!prot) {
       return LAUNCHER_ERROR_FROM_MOZ_WINDOWS_ERROR(prot.GetError());
     }
 
     ok = !!::WriteProcessMemory(aChildProcess, firstIatThunk, firstIatThunk,
                                 iatLength, &bytesWritten);
--- a/mozglue/misc/NativeNt.h
+++ b/mozglue/misc/NativeNt.h
@@ -407,29 +407,18 @@ class MOZ_RAII PEHeaders final {
 
       // curImpDesc now points to the IAT for the module we're interested in
       return curImpDesc;
     }
 
     return nullptr;
   }
 
-  struct IATThunks {
-    IATThunks(PIMAGE_THUNK_DATA aFirstThunk, ptrdiff_t aNumThunks)
-        : mFirstThunk(aFirstThunk), mNumThunks(aNumThunks) {}
-
-    size_t Length() const {
-      return size_t(mNumThunks) * sizeof(IMAGE_THUNK_DATA);
-    }
-
-    PIMAGE_THUNK_DATA mFirstThunk;
-    ptrdiff_t mNumThunks;
-  };
-
-  Maybe<IATThunks> GetIATThunksForModule(const char* aModuleNameASCII) {
+  Maybe<Span<IMAGE_THUNK_DATA>> GetIATThunksForModule(
+      const char* aModuleNameASCII) {
     PIMAGE_IMPORT_DESCRIPTOR impDesc = GetIATForModule(aModuleNameASCII);
     if (!impDesc) {
       return Nothing();
     }
 
     auto firstIatThunk =
         this->template RVAToPtr<PIMAGE_THUNK_DATA>(impDesc->FirstThunk);
     if (!firstIatThunk) {
@@ -437,18 +426,17 @@ class MOZ_RAII PEHeaders final {
     }
 
     // Find the length by iterating through the table until we find a null entry
     PIMAGE_THUNK_DATA curIatThunk = firstIatThunk;
     while (IsValid(curIatThunk)) {
       ++curIatThunk;
     }
 
-    ptrdiff_t thunkCount = curIatThunk - firstIatThunk;
-    return Some(IATThunks(firstIatThunk, thunkCount));
+    return Some(MakeSpan(firstIatThunk, curIatThunk));
   }
 
   /**
    * Resources are stored in a three-level tree. To locate a particular entry,
    * you must supply a resource type, the resource id, and then the language id.
    * If aLangId == 0, we just resolve the first entry regardless of language.
    */
   template <typename T>
--- a/mozglue/tests/TestNativeNt.cpp
+++ b/mozglue/tests/TestNativeNt.cpp
@@ -146,10 +146,27 @@ int main(int argc, char* argv[]) {
   if (version != expectedVersion) {
     printf(
         "TEST-FAILED | NativeNt | kernel32.dll's detected version "
         "(0x%016llX) does not match expected version (0x%016llX)\n",
         version, expectedVersion);
     return 1;
   }
 
+  Maybe<Span<IMAGE_THUNK_DATA>> iatThunks =
+      k32headers.GetIATThunksForModule("kernel32.dll");
+  if (iatThunks) {
+    printf(
+        "TEST-FAILED | NativeNt | Detected the IAT thunk for kernel32 "
+        "in kernel32.dll\n");
+    return 1;
+  }
+
+  iatThunks = k32headers.GetIATThunksForModule("ntdll.dll");
+  if (!iatThunks) {
+    printf(
+        "TEST-FAILED | NativeNt | Unable to find the IAT thunk for "
+        "ntdll.dll in kernel32.dll\n");
+    return 1;
+  }
+
   return 0;
 }