Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 44 additions & 29 deletions renderdoc/os/win32/win32_hook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,14 @@ static bool OrdinalAsString(void *func)
return uint64_t(func) <= 0xffff;
}

FARPROC WINAPI Hooked_GetProcAddress(HMODULE mod, LPCSTR func)
FARPROC WINAPI Hooked_GetProcAddress(HMODULE mod, LPCSTR const func)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: can you make this const LPCSTR func.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Updated Hooked_GetProcAddress to keep func unchanged and introduced a local searchFunc, without adding any extra processing or changing the existing fallback behaviour.

{
if(mod == NULL || func == NULL || mod == s_HookData->ownmodule)
return GetProcAddress(mod, func);

const LPCSTR originalRequest = func;
const bool requestIsOrdinal = OrdinalAsString((void *)originalRequest);

#if ENABLED(VERBOSE_DEBUG_HOOK)
if(OrdinalAsString((void *)func))
RDCDEBUG("Hooked_GetProcAddress(%p, %p)", mod, func);
Expand Down Expand Up @@ -807,59 +810,71 @@ FARPROC WINAPI Hooked_GetProcAddress(HMODULE mod, LPCSTR func)
RDCDEBUG("Located module %s", it->first.c_str());
#endif

LPCSTR searchFunc = func;

if(OrdinalAsString((void *)func))
{
#if ENABLED(VERBOSE_DEBUG_HOOK)
RDCDEBUG("Ordinal hook");
#endif

uint32_t ordinal = (uint16_t)(uintptr_t(func) & 0xffff);
uint32_t ordinal = (uint16_t)(uintptr_t(originalRequest) & 0xffff);

if(ordinal < it->second.OrdinalBase)
{
RDCERR("Unexpected ordinal - lower than ordinalbase %u for %s",
(uint32_t)it->second.OrdinalBase, it->first.c_str());

SetLastError(S_OK);
return GetProcAddress(mod, func);
searchFunc = NULL;
}

ordinal -= it->second.OrdinalBase;

if(ordinal >= it->second.OrdinalNames.size())
else
{
RDCERR("Unexpected ordinal - higher than fetched ordinal names (%u) for %s",
(uint32_t)it->second.OrdinalNames.size(), it->first.c_str());
ordinal -= it->second.OrdinalBase;

SetLastError(S_OK);
return GetProcAddress(mod, func);
}

func = it->second.OrdinalNames[ordinal].c_str();
if(ordinal >= it->second.OrdinalNames.size())
{
RDCERR("Unexpected ordinal - higher than fetched ordinal names (%u) for %s",
(uint32_t)it->second.OrdinalNames.size(), it->first.c_str());
searchFunc = NULL;
}
else
{
searchFunc = it->second.OrdinalNames[ordinal].c_str();

if(searchFunc && searchFunc[0] != 0)
{
#if ENABLED(VERBOSE_DEBUG_HOOK)
RDCDEBUG("found ordinal %s", func);
RDCDEBUG("found ordinal %s", searchFunc);
#endif
}
else
{
searchFunc = NULL;
}
}
}
}

FunctionHook search(func, NULL, NULL);

auto found =
std::lower_bound(it->second.FunctionHooks.begin(), it->second.FunctionHooks.end(), search);
if(found != it->second.FunctionHooks.end() && !(search < *found))
if(searchFunc && searchFunc[0] != 0)
{
FARPROC realfunc = GetProcAddress(mod, func);
FunctionHook search(searchFunc, NULL, NULL);

auto found = std::lower_bound(it->second.FunctionHooks.begin(),
it->second.FunctionHooks.end(), search);
if(found != it->second.FunctionHooks.end() && !(search < *found))
{
FARPROC realfunc = GetProcAddress(mod, requestIsOrdinal ? originalRequest : searchFunc);

#if ENABLED(VERBOSE_DEBUG_HOOK)
RDCDEBUG("Found hooked function, returning hook pointer %p", found->hook);
RDCDEBUG("Found hooked function, returning hook pointer %p", found->hook);
#endif

SetLastError(S_OK);
SetLastError(S_OK);

if(realfunc == NULL)
return NULL;
if(realfunc == NULL)
return NULL;

return (FARPROC)found->hook;
return (FARPROC)found->hook;
}
}
}
}
Expand All @@ -870,7 +885,7 @@ FARPROC WINAPI Hooked_GetProcAddress(HMODULE mod, LPCSTR func)

SetLastError(S_OK);

return GetProcAddress(mod, func);
return GetProcAddress(mod, requestIsOrdinal ? originalRequest : func);
}
static void InitHookData()
{
Expand Down
Loading