1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2014-2023 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
// This project deliberately references only kernel32.dll (ie. not even the CRT)
// so that when inserted into an application it has as small an overhead/impact
// as possible. Ideally it would be present only to be a pass-through hook and
// the first time only to allocate a little, check if this process should be hooked
// and load the renderdoc dll.
//
// The no-CRT restriction causes some awkward bits and pieces but the dll is simple
// enough that it's not a big issue.
#include "renderdocshim.h"
#include <windows.h>
struct CaptureOptions;
typedef void(__cdecl *pINTERNAL_SetCaptureOptions)(const CaptureOptions *opts);
typedef void(__cdecl *pINTERNAL_SetLogFile)(const char *logfile);
typedef void(__cdecl *pINTERNAL_SetDebugLogFile)(const char *logfile);
#if defined(RELEASE)
#define LOGPRINT(txt) \
do \
{ \
} while(0)
#else
// define this to something to get logging
//#define LOGPRINT(txt) OutputDebugStringW(txt)
#define LOGPRINT(txt) \
do \
{ \
} while(0)
#endif
void CheckHook()
{
ShimData *data = NULL;
HANDLE datahandle = OpenFileMappingA(FILE_MAP_READ, FALSE, GLOBAL_HOOK_DATA_NAME);
if(datahandle == NULL)
{
LOGPRINT(L"renderdocshim: can't open global data\n");
return;
}
data = (ShimData *)MapViewOfFile(datahandle, FILE_MAP_READ, 0, 0, sizeof(ShimData));
if(data == NULL)
{
CloseHandle(datahandle);
LOGPRINT(L"renderdocshim: can't map global data\n");
return;
}
if(data->pathmatchstring[0] == 0 || data->pathmatchstring[1] == 0 ||
data->pathmatchstring[2] == 0 || data->pathmatchstring[3] == 0)
{
LOGPRINT(L"renderdocshim: invalid pathmatchstring: '");
LOGPRINT(data->pathmatchstring);
LOGPRINT(L"'\n");
UnmapViewOfFile(data);
CloseHandle(datahandle);
return;
}
// no new[], need to use VirtualAlloc
const int exepathLen = 1024;
wchar_t *exepath = (wchar_t *)VirtualAlloc(NULL, exepathLen * sizeof(wchar_t),
MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
if(exepath)
{
// no memset :).
for(int i = 0; i < exepathLen; i++)
exepath[i] = 0;
GetModuleFileNameW(NULL, exepath, exepathLen - 1);
// no str*cmp functions
int find = FindStringOrdinal(FIND_FROMSTART, exepath, -1, data->pathmatchstring, -1, TRUE);
if(find >= 0)
{
LOGPRINT(L"renderdocshim: Hooking into '");
LOGPRINT(exepath);
LOGPRINT(L"', based on '");
LOGPRINT(data->pathmatchstring);
LOGPRINT(L"'\n");
HMODULE mod = LoadLibraryW(data->rdocpath);
if(mod)
{
pINTERNAL_SetCaptureOptions setopts =
(pINTERNAL_SetCaptureOptions)GetProcAddress(mod, "INTERNAL_SetCaptureOptions");
pINTERNAL_SetLogFile setlogfile =
(pINTERNAL_SetLogFile)GetProcAddress(mod, "INTERNAL_SetLogFile");
pINTERNAL_SetDebugLogFile setdebuglog =
(pINTERNAL_SetDebugLogFile)GetProcAddress(mod, "INTERNAL_SetDebugLogFile");
if(setopts)
setopts((const CaptureOptions *)data->opts);
if(setlogfile && data->capfile[0])
setlogfile(data->capfile);
if(setdebuglog && data->debuglog[0])
setdebuglog(data->debuglog);
}
}
else
{
LOGPRINT(L"renderdocshim: NOT Hooking into '");
LOGPRINT(exepath);
LOGPRINT(L"', based on '");
LOGPRINT(data->pathmatchstring);
LOGPRINT(L"'\n");
}
VirtualFree(exepath, 0, MEM_RELEASE);
}
else
{
LOGPRINT(L"renderdocshim: Failed to allocate exepath\n");
}
UnmapViewOfFile(data);
CloseHandle(datahandle);
}
DWORD CheckHookThread(LPVOID param)
{
CheckHook();
// this makes sure that we remove the reference to the shim dll and unload from
// the target process. That minimises the impact of having the dll inserted into
// every process
FreeLibraryAndExitThread((HMODULE)param, 0);
return 0;
}
BOOL APIENTRY dll_entry(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if(ul_reason_for_call == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(hModule);
// create a thread so that we can perform more complex actions (DllMain must be minimal
// in size, even this is a bit dodgy).
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&CheckHookThread, (LPVOID)hModule, 0, NULL);
}
return TRUE;
}
|