Skip to content

Commit

Permalink
force host symbol オプション追加
Browse files Browse the repository at this point in the history
  • Loading branch information
i-saint committed Aug 15, 2013
1 parent 53244b6 commit b338122
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 4 deletions.
12 changes: 10 additions & 2 deletions DynamicPatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ dpAPI dpContext* dpGetCurrentContext()
dpAPI bool dpInitialize(const dpConfig &conf)
{
if(!g_dpDefaultContext) {
DWORD opt = ::SymGetOptions();
opt |= SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES;
::SymSetOptions(opt);
::SymInitialize(::GetCurrentProcess(), NULL, TRUE);
::SymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_LOAD_LINES);

dpConfig &g_dpConfig = dpGetConfig();
g_dpConfig = conf;
Expand Down Expand Up @@ -68,6 +70,7 @@ dpAPI bool dpInitialize(const dpConfig &conf)
dpEach(cf.preload_paths, [](const std::string &v){ dpAddPreloadPath(v.c_str()); });
dpEach(cf.msbuild_commands, [](const std::string &v){ dpAddMSBuildCommand(v.c_str()); });
dpEach(cf.build_commands, [](const std::string &v){ dpAddBuildCommand(v.c_str()); });
dpEach(cf.force_host_symbol_patterns, [](const std::string &v){ dpAddForceHostSymbolPattern(v.c_str()); });
if(!cf.source_paths.empty() && (!cf.msbuild_commands.empty() || !cf.build_commands.empty())) {
dpStartAutoBuild();
}
Expand Down Expand Up @@ -135,7 +138,7 @@ dpAPI bool dpPatchByAddress(void *hook_addr)
return dpGetCurrentContext()->patchByAddress(hook_addr);
}

dpAPI bool dpUnpatchByAddress(void *target_or_hook_addr)
dpAPI bool dpUnpatchByAddress(void *target_or_hook_addr)
{
return dpGetCurrentContext()->unpatchByAddress(target_or_hook_addr);
};
Expand All @@ -145,6 +148,11 @@ dpAPI void* dpGetUnpatched(void *target_or_hook_addr)
return dpGetCurrentContext()->getUnpatched(target_or_hook_addr);
}

dpAPI void dpAddForceHostSymbolPattern(const char *pattern)
{
return dpGetCurrentContext()->addForceHostSymbolPattern(pattern);
}


dpAPI void dpAddModulePath(const char *path)
{
Expand Down
2 changes: 2 additions & 0 deletions DynamicPatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ dpAPI bool dpPatchAddressToAddress(void *target, void *hook_addr);
dpAPI bool dpPatchByAddress(void *hook_addr); // patches the host symbol that have same name of hook
dpAPI bool dpUnpatchByAddress(void *target_or_hook_addr);
dpAPI void* dpGetUnpatched(void *target_or_hook_addr);
dpAPI void dpAddForceHostSymbolPattern(const char *pattern);

dpAPI void dpAddModulePath(const char *path); // accepts wildcard. affects auto build and dpReload()
dpAPI void dpAddSourcePath(const char *path); //
Expand Down Expand Up @@ -183,6 +184,7 @@ dpAPI const char* dpGetVCVarsPath();
#define dpPatchByAddress(...)
#define dpUnpatchByAddress(...)
#define dpGetUnpatched(...)
#define dpAddForceHostSymbolPattern(...)

#define dpAddModulePath(...)
#define dpAddSourcePath(...)
Expand Down
5 changes: 4 additions & 1 deletion dpBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "DynamicPatcher.h"
#include "dpInternal.h"
#include <regex>

#ifdef _M_X64
# define dpSymPrefix
Expand Down Expand Up @@ -366,6 +365,10 @@ void* dpObjFile::getBaseAddress() const { return m_data; }

void* dpObjFile::resolveSymbol( const char *name )
{
if(dpGetLoader()->doesForceHostSymbol(name)) {
return dpGetLoader()->findHostSymbolByName(name);
}

void *sym = nullptr;
{
if(const dpSymbol *s=getSymbolTable().findSymbolByName(name)) {
Expand Down
1 change: 1 addition & 0 deletions dpConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ bool dpConfigFile::load(const char *path)
else if(sscanf(line, "preload path: \"%[^\"]\"", opt)) { preload_paths.push_back(opt); }
else if(sscanf(line, "msbuild command: \"%[^\"]\"", opt)) { msbuild_commands.push_back(opt); }
else if(sscanf(line, "build command: \"%[^\"]\"", opt)) { build_commands.push_back(opt); }
else if(sscanf(line, "force host symbol pattern: \"%[^\"]\"", opt)) { force_host_symbol_patterns.push_back(opt); }
}
fclose(f);
config_path = path;
Expand Down
6 changes: 5 additions & 1 deletion dpContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include "DynamicPatcher.h"
#include "dpInternal.h"
#include <regex>

dpContext::dpContext()
{
Expand Down Expand Up @@ -115,3 +114,8 @@ void* dpContext::getUnpatched(void *target)
}
return nullptr;
}

void dpContext::addForceHostSymbolPattern(const char *pattern)
{
m_loader->addForceHostSymbolPattern(pattern);
}
8 changes: 8 additions & 0 deletions dpInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <set>
#include <map>
#include <algorithm>
#include <regex>
#include "DynamicPatcher.h"

typedef unsigned long long dpTime;
Expand Down Expand Up @@ -233,6 +234,7 @@ struct dpConfigFile
std::vector<std::string> preload_paths;
std::vector<std::string> msbuild_commands;
std::vector<std::string> build_commands;
std::vector<std::string> force_host_symbol_patterns;
std::string config_path;

dpConfigFile();
Expand Down Expand Up @@ -411,10 +413,15 @@ class dpLoader
dpSymbol* newSymbol(const char *nam=nullptr, void *addr=nullptr, int fla=0, int sect=0, dpBinary *bin=nullptr);
void deleteSymbol(dpSymbol *sym);

void addForceHostSymbolPattern(const char *pattern);
bool doesForceHostSymbol(const char *name);

private:
typedef std::vector<dpBinary*> binary_cont;
typedef std::vector<std::regex> pattern_cont;

dpContext *m_context;
pattern_cont m_force_host_symbol_patterns;
binary_cont m_binaries;
binary_cont m_onload_queue;
dpSymbolTable m_hostsymbols;
Expand Down Expand Up @@ -534,6 +541,7 @@ class dpContext
bool patchByAddress(void *hook);
bool unpatchByAddress(void *target_or_hook_addr);
void* getUnpatched(void *target_or_hook_addr);
void addForceHostSymbolPattern(const char *pattern);

private:
dpBuilder *m_builder;
Expand Down
21 changes: 21 additions & 0 deletions dpLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,24 @@ void dpLoader::deleteSymbol(dpSymbol *sym)
sym->~dpSymbol();
m_symalloc.deallocate(sym);
}

void dpLoader::addForceHostSymbolPattern(const char *pattern)
{
m_force_host_symbol_patterns.push_back(std::regex(pattern));
}

bool dpLoader::doesForceHostSymbol(const char *name)
{
if(m_force_host_symbol_patterns.empty()) { return false; }

char demangled[4096];
dpDemangle(name, demangled, sizeof(demangled));
bool ret = false;
for(size_t i=0; i<m_force_host_symbol_patterns.size(); ++i) {
if(std::regex_match(demangled, m_force_host_symbol_patterns[i])) {
ret = true;
break;
}
}
return ret;
}
1 change: 1 addition & 0 deletions dpPatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ void* dpPatcher::patch(dpSymbol *target, dpSymbol *hook)
{
if(!target || !hook) { return nullptr; }
if(dpIsLinkFailed(target->flags) || dpIsLinkFailed(hook->flags)) { return nullptr; }
if(dpGetLoader()->doesForceHostSymbol(target->name)) { return nullptr; }

unpatchByAddress(target->address);

Expand Down

0 comments on commit b338122

Please sign in to comment.