1
0
Fork 0

DBG: fully fixed the database problems (issue #279) + fixed a bug with the modules not being cleared on process exit

This commit is contained in:
Mr. eXoDia 2015-04-17 01:16:38 +02:00
parent d70ae9e6b3
commit 7b3aa207f3
7 changed files with 54 additions and 42 deletions

View File

@ -99,6 +99,16 @@ void efree(void* ptr, const char* reason)
GlobalFree(ptr); GlobalFree(ptr);
} }
void* json_malloc(size_t size)
{
return emalloc(size, "json:ptr");
}
void json_free(void* ptr)
{
efree(ptr, "json:ptr");
}
/** /**
\brief Gets the number of memory leaks. This number is only valid in _dbg_dbgexitsignal(). \brief Gets the number of memory leaks. This number is only valid in _dbg_dbgexitsignal().
\return The number of memory leaks. \return The number of memory leaks.
@ -256,7 +266,7 @@ bool settingboolget(const char* section, const char* name)
arch GetFileArchitecture(const char* szFileName) arch GetFileArchitecture(const char* szFileName)
{ {
arch retval = notfound; arch retval = notfound;
HANDLE hFile = CreateFileW(StringUtils::Utf8ToUtf16(szFileName).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(szFileName).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if(hFile != INVALID_HANDLE_VALUE) if(hFile != INVALID_HANDLE_VALUE)
{ {
unsigned char data[0x1000]; unsigned char data[0x1000];
@ -281,7 +291,6 @@ arch GetFileArchitecture(const char* szFileName)
} }
} }
} }
CloseHandle(hFile);
} }
return retval; return retval;
} }

View File

@ -72,6 +72,8 @@ extern char dbpath[3 * deflen];
void* emalloc(size_t size, const char* reason = "emalloc:???"); void* emalloc(size_t size, const char* reason = "emalloc:???");
void* erealloc(void* ptr, size_t size, const char* reason = "erealloc:???"); void* erealloc(void* ptr, size_t size, const char* reason = "erealloc:???");
void efree(void* ptr, const char* reason = "efree:???"); void efree(void* ptr, const char* reason = "efree:???");
void* json_malloc(size_t size);
void json_free(void* ptr);
int memleaks(); int memleaks();
void setalloctrace(const char* file); void setalloctrace(const char* file);
bool arraycontains(const char* cmd_list, const char* cmd); bool arraycontains(const char* cmd_list, const char* cmd);

View File

@ -36,20 +36,22 @@ void dbsave()
WString wdbpath = StringUtils::Utf8ToUtf16(dbpath); WString wdbpath = StringUtils::Utf8ToUtf16(dbpath);
if(json_object_size(root)) if(json_object_size(root))
{ {
FILE* jsonFile = 0; Handle hFile = CreateFileW(wdbpath.c_str(), GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0);
if(_wfopen_s(&jsonFile, wdbpath.c_str(), L"wb")) if(!hFile)
{ {
dputs("failed to open database file for editing!"); dputs("\nFailed to open database for writing!");
json_decref(root); //free root
return; return;
} }
if(json_dumpf(root, jsonFile, JSON_INDENT(4)) == -1) SetEndOfFile(hFile);
char* jsonText = json_dumps(root, JSON_INDENT(4));
DWORD written = 0;
if(!WriteFile(hFile, jsonText, strlen(jsonText), &written, 0))
{ {
dputs("couldn't write JSON to database file..."); json_free(jsonText);
json_decref(root); //free root dputs("\nFailed to write database file!");
return; return;
} }
fclose(jsonFile); json_free(jsonText);
if(!settingboolget("Engine", "DisableCompression")) if(!settingboolget("Engine", "DisableCompression"))
LZ4_compress_fileW(wdbpath.c_str(), wdbpath.c_str()); LZ4_compress_fileW(wdbpath.c_str(), wdbpath.c_str());
} }
@ -85,30 +87,32 @@ void dbload()
} }
} }
// Open the file for reading by the JSON parser // Read the database file
FILE* jsonFile = nullptr; Handle hFile = CreateFileW(databasePathW.c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
long jsonFileSize = 0; if(!hFile)
if(_wfopen_s(&jsonFile, databasePathW.c_str(), L"rb"))
{ {
dputs("\nFailed to open database file!"); dputs("\nFailed to open database file!");
return; return;
} }
// Get the current file size unsigned int jsonFileSize = GetFileSize(hFile, 0);
fseek(jsonFile, 0, SEEK_END); if(!jsonFileSize)
jsonFileSize = ftell(jsonFile); {
fseek(jsonFile, 0, SEEK_SET); dputs("\nEmpty database file!");
return;
}
// Verify that the file size is greater than 0. Memory<char*> jsonText(jsonFileSize + 1);
// This corrects a bug when a file exists, but there is no data inside. DWORD read = 0;
JSON root = nullptr; if(!ReadFile(hFile, jsonText, jsonFileSize, &read, 0))
{
dputs("\nFailed to read database file!");
return;
}
hFile.Close();
if(jsonFileSize > 0) // Deserialize JSON
root = json_loadf(jsonFile, 0, 0); JSON root = json_loads(jsonText, 0, 0);
// Release the file handle and re-compress
fclose(jsonFile);
if(lzmaStatus != LZ4_INVALID_ARCHIVE && useCompression) if(lzmaStatus != LZ4_INVALID_ARCHIVE && useCompression)
LZ4_compress_fileW(databasePathW.c_str(), databasePathW.c_str()); LZ4_compress_fileW(databasePathW.c_str(), databasePathW.c_str());

View File

@ -12,10 +12,16 @@ public:
} }
~Handle() ~Handle()
{
Close();
}
void Close()
{ {
DWORD dwFlags = 0; DWORD dwFlags = 0;
if(GetHandleInformation(mHandle, &dwFlags) && !(dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE)) if(GetHandleInformation(mHandle, &dwFlags) && !(dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE))
CloseHandle(mHandle); CloseHandle(mHandle);
mHandle = INVALID_HANDLE_VALUE;
} }
const HANDLE & operator=(const HANDLE & h) const HANDLE & operator=(const HANDLE & h)

View File

@ -72,7 +72,7 @@ static int scriptinternalstep(int fromIp) //internal step routine
static bool scriptcreatelinemap(const char* filename) static bool scriptcreatelinemap(const char* filename)
{ {
HANDLE hFile = CreateFileW(StringUtils::Utf8ToUtf16(filename).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); Handle hFile = CreateFileW(StringUtils::Utf8ToUtf16(filename).c_str(), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
if(hFile == INVALID_HANDLE_VALUE) if(hFile == INVALID_HANDLE_VALUE)
{ {
GuiScriptError(0, "CreateFile failed..."); GuiScriptError(0, "CreateFile failed...");
@ -81,7 +81,6 @@ static bool scriptcreatelinemap(const char* filename)
unsigned int filesize = GetFileSize(hFile, 0); unsigned int filesize = GetFileSize(hFile, 0);
if(!filesize) if(!filesize)
{ {
CloseHandle(hFile);
GuiScriptError(0, "Empty script..."); GuiScriptError(0, "Empty script...");
return false; return false;
} }
@ -90,11 +89,10 @@ static bool scriptcreatelinemap(const char* filename)
DWORD read = 0; DWORD read = 0;
if(!ReadFile(hFile, filedata, filesize, &read, 0)) if(!ReadFile(hFile, filedata, filesize, &read, 0))
{ {
CloseHandle(hFile);
GuiScriptError(0, "ReadFile failed..."); GuiScriptError(0, "ReadFile failed...");
return false; return false;
} }
CloseHandle(hFile); hFile.Close();
int len = (int)strlen(filedata); int len = (int)strlen(filedata);
char temp[256] = ""; char temp[256] = "";
LINEMAPENTRY entry; LINEMAPENTRY entry;

View File

@ -97,7 +97,10 @@ void SymUpdateModuleList()
std::vector<SYMBOLMODULEINFO> modList; std::vector<SYMBOLMODULEINFO> modList;
if(!SymGetModuleList(&modList)) if(!SymGetModuleList(&modList))
{
GuiSymbolUpdateModuleList(0, nullptr);
return; return;
}
// Create a new array to be sent to the GUI thread // Create a new array to be sent to the GUI thread
size_t moduleCount = modList.size(); size_t moduleCount = modList.size();

View File

@ -231,16 +231,6 @@ static DWORD WINAPI DbgCommandLoopThread(void* a)
return 0; return 0;
} }
static void* emalloc_json(size_t size)
{
return emalloc(size, "json:ptr");
}
static void efree_json(void* ptr)
{
efree(ptr, "json:ptr");
}
extern "C" DLL_EXPORT const char* _dbg_dbginit() extern "C" DLL_EXPORT const char* _dbg_dbginit()
{ {
if(!EngineCheckStructAlignment(UE_STRUCT_TITAN_ENGINE_CONTEXT, sizeof(TITAN_ENGINE_CONTEXT_t))) if(!EngineCheckStructAlignment(UE_STRUCT_TITAN_ENGINE_CONTEXT, sizeof(TITAN_ENGINE_CONTEXT_t)))
@ -250,7 +240,7 @@ extern "C" DLL_EXPORT const char* _dbg_dbginit()
SectionLockerGlobal::Initialize(); SectionLockerGlobal::Initialize();
dbginit(); dbginit();
dbgfunctionsinit(); dbgfunctionsinit();
json_set_alloc_funcs(emalloc_json, efree_json); json_set_alloc_funcs(json_malloc, json_free);
if(yr_initialize() != ERROR_SUCCESS) if(yr_initialize() != ERROR_SUCCESS)
return "Failed to initialize Yara!"; return "Failed to initialize Yara!";
wchar_t wszDir[deflen] = L""; wchar_t wszDir[deflen] = L"";