2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-06-04 00:53:02 -07:00
|
|
|
/* Win32 x86/x64 code for stack walking, symbol resolution, and function hooking */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#ifndef __nsDebugHelpWin32_h__
|
|
|
|
#define __nsDebugHelpWin32_h__
|
|
|
|
|
2010-06-04 00:53:02 -07:00
|
|
|
#if defined(_WIN32) && (defined(_M_IX86) || defined(_M_X64))
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
#include <windows.h>
|
|
|
|
#include <imagehlp.h>
|
|
|
|
#include <crtdbg.h>
|
|
|
|
#else
|
2010-06-04 00:53:02 -07:00
|
|
|
#error "nsDebugHelpWin32.h should only be included in Win32 x86/x64 builds"
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// XXX temporary hack...
|
|
|
|
//#include "hacky_defines.h"
|
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
// useful macros...
|
|
|
|
|
|
|
|
#ifdef DHW_IMPLEMENT_GLOBALS
|
2014-02-17 14:32:52 -08:00
|
|
|
#define DHW_DECLARE_FUN_GLOBAL(name_) decltype(name_)* dhw##name_
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
2014-02-17 14:32:52 -08:00
|
|
|
#define DHW_DECLARE_FUN_GLOBAL(name_) extern decltype(name_)* dhw##name_
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/**********************************************************/
|
2014-02-17 14:32:52 -08:00
|
|
|
// This is used to get 'original' function addresses from DHWImportHooker.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-17 14:32:52 -08:00
|
|
|
#define DHW_ORIGINAL(name_, hooker_) \
|
|
|
|
((decltype(name_)*) hooker_ . GetOriginalFunction())
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
// Global declarations of entry points into ImgHelp functions
|
|
|
|
|
2010-06-04 00:53:02 -07:00
|
|
|
#ifndef _WIN64
|
2014-02-17 14:32:52 -08:00
|
|
|
DHW_DECLARE_FUN_GLOBAL(EnumerateLoadedModules);
|
2010-06-04 00:53:02 -07:00
|
|
|
#else
|
2014-02-17 14:32:52 -08:00
|
|
|
DHW_DECLARE_FUN_GLOBAL(EnumerateLoadedModules64);
|
2010-06-04 00:53:02 -07:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-17 14:32:52 -08:00
|
|
|
DHW_DECLARE_FUN_GLOBAL(ImageDirectoryEntryToData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
extern bool
|
2007-03-22 10:30:00 -07:00
|
|
|
dhwEnsureImageHlpInitialized();
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
class DHWImportHooker
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
DHWImportHooker(const char* aModuleName,
|
|
|
|
const char* aFunctionName,
|
|
|
|
PROC aHook,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aExcludeOurModule = false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
~DHWImportHooker();
|
|
|
|
|
|
|
|
PROC GetOriginalFunction() {return mOriginal;}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool PatchAllModules();
|
|
|
|
bool PatchOneModule(HMODULE aModule, const char* name);
|
|
|
|
static bool ModuleLoaded(HMODULE aModule, DWORD flags);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
// I think that these should be made not static members, but allocated
|
|
|
|
// things created in an explicit static 'init' method and cleaned up in
|
|
|
|
// an explicit static 'finish' method. This would allow the application
|
|
|
|
// to have proper lifetime control over all the hooks.
|
|
|
|
|
|
|
|
static DHWImportHooker &getLoadLibraryWHooker();
|
|
|
|
static DHWImportHooker &getLoadLibraryExWHooker();
|
|
|
|
static DHWImportHooker &getLoadLibraryAHooker();
|
|
|
|
static DHWImportHooker &getLoadLibraryExAHooker();
|
|
|
|
static DHWImportHooker &getGetProcAddressHooker();
|
|
|
|
|
|
|
|
static HMODULE WINAPI LoadLibraryA(PCSTR path);
|
|
|
|
|
|
|
|
private:
|
|
|
|
DHWImportHooker* mNext;
|
|
|
|
const char* mModuleName;
|
|
|
|
const char* mFunctionName;
|
|
|
|
PROC mOriginal;
|
|
|
|
PROC mHook;
|
|
|
|
HMODULE mIgnoreModule;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mHooking;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
static PRLock* gLock;
|
|
|
|
static DHWImportHooker* gHooks;
|
2014-02-17 14:32:52 -08:00
|
|
|
static decltype(GetProcAddress)* gRealGetProcAddress;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
static HMODULE WINAPI LoadLibraryW(PCWSTR path);
|
|
|
|
static HMODULE WINAPI LoadLibraryExW(PCWSTR path, HANDLE file, DWORD flags);
|
|
|
|
static HMODULE WINAPI LoadLibraryExA(PCSTR path, HANDLE file, DWORD flags);
|
|
|
|
|
|
|
|
static FARPROC WINAPI GetProcAddress(HMODULE aModule, PCSTR aFunctionName);
|
|
|
|
};
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
// This supports the _CrtSetAllocHook based hooking.
|
|
|
|
// This system sucks because you don't get to see the allocated pointer. I
|
|
|
|
// don't think it appropriate for nsTraceMalloc, but is useful as a means to make
|
|
|
|
// malloc fail for testing purposes.
|
|
|
|
#if 0 //comment out this stuff. not necessary
|
|
|
|
|
|
|
|
class DHWAllocationSizeDebugHook
|
|
|
|
{
|
|
|
|
public:
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual bool AllocHook(size_t size) = 0;
|
|
|
|
virtual bool ReallocHook(size_t size, size_t sizeOld) = 0;
|
|
|
|
virtual bool FreeHook(size_t size) = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
extern bool dhwSetAllocationSizeDebugHook(DHWAllocationSizeDebugHook* hook);
|
|
|
|
extern bool dhwClearAllocationSizeDebugHook();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
#endif //0
|
|
|
|
|
|
|
|
#endif /* __nsDebugHelpWin32_h__ */
|