Add option to disable cache map

* Add '-nosmap' command line parameter to disable signature caching.
* Invalidate the cache map after initialization.
* Add const qualifiers where possible.
This commit is contained in:
Kawe Mazidjatari 2022-12-04 14:16:12 +01:00
parent 55b90781ee
commit e9e323f9d4
3 changed files with 70 additions and 15 deletions

View File

@ -470,9 +470,12 @@ void CheckCPU() // Respawn's engine and our SDK utilize POPCNT, SSE3 and SSSE3 (
void DetourInit() // Run the sigscan
{
bool bLogAdr = (strstr(GetCommandLineA(), "-sig_toconsole") != nullptr);
LPSTR pCommandLine = GetCommandLineA();
bool bLogAdr = (strstr(pCommandLine, "-sig_toconsole") != nullptr);
bool bInitDivider = false;
g_SigCache.SetDisabled((strstr(pCommandLine, "-nosmap") != nullptr));
g_SigCache.LoadCache(SIGDB_FILE);
for (const IDetour* pDetour : vDetour)
@ -498,6 +501,7 @@ void DetourInit() // Run the sigscan
#endif // DEDICATED
g_SigCache.WriteCache(SIGDB_FILE);
g_SigCache.InvalidateMap();
}
void DetourAddress() // Test the sigscan results

View File

@ -1,19 +1,59 @@
//===========================================================================//
//
// Purpose: Implementation of the CSigCache class.
//
//
//===========================================================================//
// sigcache.cpp
//
// The system creates a static cache file on the disk, who's blob contains a
// map of string signatures and its precomputed relative virtual address.
//
// This file gets loaded and parsed during DLL init. If the file is absent or
// outdated/corrupt, the system will generate a new cache file if enabled.
//
// By caching the relative virtual addresses, we can drop a significant amount
// of time initializing the DLL by parsing the precomputed data instead of
// searching for each signature in the memory region of the target executable.
//
///////////////////////////////////////////////////////////////////////////////
#include "core/stdafx.h"
#include "public/utility/binstream.h"
#include "public/utility/sigcache.h"
//-----------------------------------------------------------------------------
// Purpose: creates a pair of a pattern (key) and relative virtual address (value)
// Input : &svPattern -
// nRVA -
// Purpose: whether or not to disable the caching of signatures
// Input : bDisabled - (true = disabled)
//-----------------------------------------------------------------------------
void CSigCache::SetDisabled(const bool bDisabled)
{
m_bDisabled = bDisabled;
}
//-----------------------------------------------------------------------------
// Purpose: clears the signature cache memory
//-----------------------------------------------------------------------------
void CSigCache::InvalidateMap()
{
if (m_bDisabled)
{
return;
}
(*m_Cache.mutable_smap()).clear();
}
//-----------------------------------------------------------------------------
// Purpose: creates a map of a pattern and relative virtual address
// Input : &svPattern - (key)
// nRVA - (value)
//-----------------------------------------------------------------------------
void CSigCache::AddEntry(const string& svPattern, const uint64_t nRVA)
{
if (m_bDisabled)
{
return;
}
Assert(!m_bInitialized);
(*m_Cache.mutable_smap())[svPattern] = nRVA;
}
@ -26,7 +66,7 @@ void CSigCache::AddEntry(const string& svPattern, const uint64_t nRVA)
//-----------------------------------------------------------------------------
bool CSigCache::FindEntry(const string& svPattern, uint64_t& nRVA) const
{
if (m_bInitialized)
if (!m_bDisabled && m_bInitialized)
{
google::protobuf::Map sMap = m_Cache.smap();
auto p = sMap.find(svPattern);
@ -48,8 +88,13 @@ bool CSigCache::FindEntry(const string& svPattern, uint64_t& nRVA) const
bool CSigCache::LoadCache(const string& svCacheFile)
{
Assert(!m_bInitialized); // Recursive load.
CIOStream reader(svCacheFile, CIOStream::Mode_t::READ);
if (m_bDisabled)
{
return false;
}
CIOStream reader(svCacheFile, CIOStream::Mode_t::READ);
if (!reader.IsReadable())
{
return false;
@ -109,9 +154,9 @@ bool CSigCache::LoadCache(const string& svCacheFile)
// Purpose: writes the cache map to the disk
// Output : true on success, false otherwise
//-----------------------------------------------------------------------------
bool CSigCache::WriteCache(const string& svCacheFile)
bool CSigCache::WriteCache(const string& svCacheFile) const
{
if (m_bInitialized)
if (m_bDisabled || m_bInitialized)
{
// Only write when we don't have anything valid on the disk.
return false;
@ -157,7 +202,7 @@ bool CSigCache::WriteCache(const string& svCacheFile)
// *pDstBuf -
// Output : true on success, false otherwise
//-----------------------------------------------------------------------------
bool CSigCache::DecompressBlob(size_t nSrcLen, size_t& nDstLen, uint32_t& nAdler, const uint8_t* pSrcBuf, uint8_t* pDstBuf) const
bool CSigCache::DecompressBlob(const size_t nSrcLen, size_t& nDstLen, uint32_t& nAdler, const uint8_t* pSrcBuf, uint8_t* pDstBuf) const
{
lzham_decompress_params lzDecompParams{};
lzDecompParams.m_dict_size_log2 = SIGDB_DICT_SIZE;
@ -184,7 +229,7 @@ bool CSigCache::DecompressBlob(size_t nSrcLen, size_t& nDstLen, uint32_t& nAdler
// *pDstBuf -
// Output : true on success, false otherwise
//-----------------------------------------------------------------------------
bool CSigCache::CompressBlob(size_t nSrcLen, size_t& nDstLen, uint32_t& nAdler, const uint8_t* pSrcBuf, uint8_t* pDstBuf) const
bool CSigCache::CompressBlob(const size_t nSrcLen, size_t& nDstLen, uint32_t& nAdler, const uint8_t* pSrcBuf, uint8_t* pDstBuf) const
{
lzham_compress_params lzCompParams{};
lzCompParams.m_dict_size_log2 = SIGDB_DICT_SIZE;

View File

@ -18,21 +18,27 @@
class CSigCache
{
public:
CSigCache() { m_bInitialized = false; };
CSigCache()
: m_bInitialized(false)
, m_bDisabled(false) {};
~CSigCache() {};
void SetDisabled(const bool bDisabled);
void InvalidateMap();
void AddEntry(const string& svPattern, const uint64_t nRVA);
bool FindEntry(const string& svPattern, uint64_t& nRVA) const;
bool LoadCache(const string& svCacheFile);
bool WriteCache(const string& svCacheFile);
bool WriteCache(const string& svCacheFile) const;
private:
bool CompressBlob(size_t nSrcLen, size_t& nDstLen, uint32_t& nAdler32, const uint8_t* pSrcBuf, uint8_t* pDstBuf) const;
bool DecompressBlob(size_t nSrcLen, size_t& nDstLen, uint32_t& nAdler32, const uint8_t* pSrcBuf, uint8_t* pDstBuf) const;
bool CompressBlob(const size_t nSrcLen, size_t& nDstLen, uint32_t& nAdler32, const uint8_t* pSrcBuf, uint8_t* pDstBuf) const;
bool DecompressBlob(const size_t nSrcLen, size_t& nDstLen, uint32_t& nAdler32, const uint8_t* pSrcBuf, uint8_t* pDstBuf) const;
SigMap_Pb m_Cache;
bool m_bInitialized;
bool m_bDisabled;
};
#pragma pack(push, 1)