Bug 681238 part 1 - Intercept a function from the executable in TestDllInterceptor. r=vlad
authorMike Hommey <mh+mozilla@glandium.org>
Mon, 29 Aug 2011 13:23:35 +0200
changeset 76386 38bbf1e8ae5d4eccbea1b8f330bd96764d3ed455
parent 76385 5dda9668493b2d7fbe05df3bf242ac494d78ab4f
child 76387 ce43a8644bc03efa037d5ea6b5b3da69b5977edc
push id21098
push userbmo@edmorley.co.uk
push dateThu, 01 Sep 2011 20:43:58 +0000
treeherdermozilla-central@ce43a8644bc0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvlad
bugs681238
milestone9.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 681238 part 1 - Intercept a function from the executable in TestDllInterceptor. r=vlad
toolkit/xre/test/win/TestDllInterceptor.cpp
--- a/toolkit/xre/test/win/TestDllInterceptor.cpp
+++ b/toolkit/xre/test/win/TestDllInterceptor.cpp
@@ -33,85 +33,96 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include <stdio.h>
 #include "nsWindowsDllInterceptor.h"
 
+struct payload {
+  UINT64 a;
+  UINT64 b;
+  UINT64 c;
+
+  bool operator==(const payload &other) const {
+    return (a == other.a &&
+            b == other.b &&
+            c == other.c);
+  }
+};
+
+extern "C" __declspec(dllexport,noinline) payload rotatePayload(payload p) {
+  UINT64 tmp = p.a;
+  p.a = p.b;
+  p.b = p.c;
+  p.c = tmp;
+  return p;
+}
+
 static bool patched_func_called = false;
 
-static BOOL (WINAPI *orig_GetVersionExA)(LPOSVERSIONINFO);
+static payload (*orig_rotatePayload)(payload);
 
-static BOOL WINAPI
-patched_GetVersionExA(LPOSVERSIONINFO lpVersionInfo)
+static payload
+patched_rotatePayload(payload p)
 {
   patched_func_called = true;
-  return orig_GetVersionExA(lpVersionInfo);
-}
-
-bool osvi_equal(OSVERSIONINFO &info0, OSVERSIONINFO &info1)
-{
-  return (info0.dwMajorVersion == info1.dwMajorVersion &&
-          info0.dwMinorVersion == info1.dwMinorVersion &&
-          info0.dwBuildNumber == info1.dwBuildNumber &&
-          info0.dwPlatformId == info1.dwPlatformId &&
-          !strncmp(info0.szCSDVersion, info1.szCSDVersion, sizeof(info0.szCSDVersion)));
+  return orig_rotatePayload(p);
 }
 
 int main()
 {
-  OSVERSIONINFO info0, info1;
-  ZeroMemory(&info0, sizeof(OSVERSIONINFO));
-  ZeroMemory(&info1, sizeof(OSVERSIONINFO));
-  info0.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
-  info1.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+  payload initial = { 0x12345678, 0xfc4e9d31, 0x87654321 };
+  payload p0, p1;
+  ZeroMemory(&p0, sizeof(p0));
+  ZeroMemory(&p1, sizeof(p1));
 
-  GetVersionExA(&info0);
+  p0 = rotatePayload(initial);
 
   {
-    WindowsDllInterceptor Kernel32Intercept;
-    Kernel32Intercept.Init("kernel32.dll");
-    if (Kernel32Intercept.AddHook("GetVersionExA", reinterpret_cast<intptr_t>(patched_GetVersionExA), (void**) &orig_GetVersionExA)) {
+    WindowsDllInterceptor ExeIntercept;
+    ExeIntercept.Init("TestDllInterceptor.exe");
+    if (ExeIntercept.AddHook("rotatePayload", reinterpret_cast<intptr_t>(patched_rotatePayload), (void**) &orig_rotatePayload)) {
       printf("TEST-PASS | WindowsDllInterceptor | Hook added\n");
     } else {
       printf("TEST-UNEXPECTED-FAIL | WindowsDllInterceptor | Failed to add hook\n");
       return 1;
     }
 
-    GetVersionExA(&info1);
+    p1 = rotatePayload(initial);
 
     if (patched_func_called) {
       printf("TEST-PASS | WindowsDllInterceptor | Hook called\n");
     } else {
       printf("TEST-UNEXPECTED-FAIL | WindowsDllInterceptor | Hook was not called\n");
       return 1;
     }
 
-    if (osvi_equal(info0, info1)) {
+    if (p0 == p1) {
       printf("TEST-PASS | WindowsDllInterceptor | Hook works properly\n");
     } else {
       printf("TEST-UNEXPECTED-FAIL | WindowsDllInterceptor | Hook didn't return the right information\n");
       return 1;
     }
   }
 
   patched_func_called = false;
+  ZeroMemory(&p1, sizeof(p1));
 
-  GetVersionExA(&info1);
+  p1 = rotatePayload(initial);
 
   if (!patched_func_called) {
     printf("TEST-PASS | WindowsDllInterceptor | Hook was not called after unregistration\n");
   } else {
     printf("TEST-UNEXPECTED-FAIL | WindowsDllInterceptor | Hook was still called after unregistration\n");
     return 1;
   }
 
-  if (osvi_equal(info0, info1)) {
+  if (p0 == p1) {
     printf("TEST-PASS | WindowsDllInterceptor | Original function worked properly\n");
   } else {
     printf("TEST-UNEXPECTED-FAIL | WindowsDllInterceptor | Original function didn't return the right information\n");
     return 1;
   }
 
   printf("TEST-PASS | WindowsDllInterceptor | all checks passed\n");
   return 0;