Skip to content

Commit fb3afac

Browse files
committed
Updated memory.find_binary()
We should rework that function at some point :D
1 parent ff20415 commit fb3afac

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/core/modules/memory/memory_scanner.cpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ CPointer* CBinaryFile::FindSymbol(char* szSymbol)
202202
return new CPointer((unsigned long) GetProcAddress((HMODULE) m_ulAddr, szSymbol));
203203

204204
#elif defined(__linux__)
205+
void* pResult = dlsym((void*) m_ulAddr, szSymbol);
206+
if (pResult)
207+
return new CPointer((unsigned long) pResult);
208+
205209
// -----------------------------------------
206210
// We need to use mmap now that VALVe has
207211
// made them all private!
@@ -466,14 +470,16 @@ bool str_ends_with(const char *szString, const char *szSuffix)
466470
return strncmp(szString + stringlen - suffixlen, szSuffix, suffixlen) == 0;
467471
}
468472

469-
CBinaryFile* CBinaryManager::FindBinary(char* szPath, bool bSrvCheck /* = true */)
473+
CBinaryFile* CBinaryManager::FindBinary(char* szPath, bool bSrvCheck /* = true */, bool bCheckExtension /* = true */)
470474
{
471475
std::string szBinaryPath = szPath;
472476
#ifdef __linux__
473-
if (bSrvCheck && !str_ends_with(szBinaryPath.data(), "_srv") && !str_ends_with(szBinaryPath.data(), ".so"))
474-
szBinaryPath += "_srv.so";
475-
else if (!str_ends_with(szBinaryPath.data(), ".so"))
476-
szBinaryPath += ".so";
477+
if (bCheckExtension) {
478+
if (bSrvCheck && !str_ends_with(szBinaryPath.data(), "_srv") && !str_ends_with(szBinaryPath.data(), ".so"))
479+
szBinaryPath += "_srv.so";
480+
else if (!str_ends_with(szBinaryPath.data(), ".so"))
481+
szBinaryPath += ".so";
482+
}
477483
#endif
478484

479485
unsigned long ulAddr = (unsigned long) dlLoadLibrary(szBinaryPath.data());
@@ -494,7 +500,7 @@ CBinaryFile* CBinaryManager::FindBinary(char* szPath, bool bSrvCheck /* = true *
494500
{
495501
szBinaryPath = "Unable to find " + szBinaryPath;
496502
#ifdef _WIN32
497-
if (!str_ends_with(szBinaryPath.data(), ".dll"))
503+
if (bCheckExtension && !str_ends_with(szBinaryPath.data(), ".dll"))
498504
szBinaryPath += ".dll";
499505
#endif
500506
BOOST_RAISE_EXCEPTION(PyExc_IOError, szBinaryPath.data())
@@ -520,15 +526,7 @@ CBinaryFile* CBinaryManager::FindBinary(char* szPath, bool bSrvCheck /* = true *
520526
ulSize = nt->OptionalHeader.SizeOfImage;
521527

522528
#elif defined(__linux__)
523-
// TODO: Retrieve whole size
524-
struct stat buf;
525-
if (stat(szBinaryPath.data(), &buf) == -1)
526-
{
527-
dlFreeLibrary((DLLib *) ulAddr);
528-
szBinaryPath = "Unable to retrieve the binary size for " + szBinaryPath;
529-
BOOST_RAISE_EXCEPTION(PyExc_RuntimeError, szBinaryPath.data())
530-
}
531-
ulSize = buf.st_size;
529+
ulSize = 0;
532530

533531
#else
534532
#error "BinaryManager::FindBinary() is not implemented on this OS"
@@ -543,7 +541,7 @@ CBinaryFile* CBinaryManager::FindBinary(char* szPath, bool bSrvCheck /* = true *
543541
//-----------------------------------------------------------------------------
544542
// Functions
545543
//-----------------------------------------------------------------------------
546-
CBinaryFile* FindBinary(char* szPath, bool bSrvCheck /* = true */)
544+
CBinaryFile* FindBinary(char* szPath, bool bSrvCheck /* = true */, bool bCheckExtension /* = true */)
547545
{
548-
return s_pBinaryManager->FindBinary(szPath, bSrvCheck);
546+
return s_pBinaryManager->FindBinary(szPath, bSrvCheck, bCheckExtension);
549547
}

src/core/modules/memory/memory_scanner.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ class CBinaryFile
7272
class CBinaryManager
7373
{
7474
public:
75-
CBinaryFile* FindBinary(char* szPath, bool bSrvCheck = true);
75+
CBinaryFile* FindBinary(char* szPath, bool bSrvCheck = true, bool bCheckExtension = true);
7676

7777
private:
7878
std::list<CBinaryFile*> m_Binaries;
7979
};
8080

8181
static CBinaryManager* s_pBinaryManager = new CBinaryManager();
8282

83-
CBinaryFile* FindBinary(char* szPath, bool bSrvCheck = true);
83+
CBinaryFile* FindBinary(char* szPath, bool bSrvCheck = true, bool bCheckExtension = true);
8484

8585
#endif // _MEMORY_SCANNER_H

src/core/modules/memory/memory_wrap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ void export_functions(scope _memory)
900900
{
901901
def("find_binary",
902902
&FindBinary,
903-
("path", arg("srv_check")=true),
903+
("path", arg("srv_check")=true, arg("check_extension")=true),
904904
"Search for a binary and return it as a :class:`BinaryFile` object.\n"
905905
"\n"
906906
":param str path: The path to the binary file (absolute, relative or just the name of the file if it's on the search path).\n"

0 commit comments

Comments
 (0)