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);
}
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().
\return The number of memory leaks.
@ -256,7 +266,7 @@ bool settingboolget(const char* section, const char* name)
arch GetFileArchitecture(const char* szFileName)
{
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)
{
unsigned char data[0x1000];
@ -281,7 +291,6 @@ arch GetFileArchitecture(const char* szFileName)
}
}
}
CloseHandle(hFile);
}
return retval;
}

View File

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

View File

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

View File

@ -12,10 +12,16 @@ public:
}
~Handle()
{
Close();
}
void Close()
{
DWORD dwFlags = 0;
if(GetHandleInformation(mHandle, &dwFlags) && !(dwFlags & HANDLE_FLAG_PROTECT_FROM_CLOSE))
CloseHandle(mHandle);
mHandle = INVALID_HANDLE_VALUE;
}
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)
{
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)
{
GuiScriptError(0, "CreateFile failed...");
@ -81,7 +81,6 @@ static bool scriptcreatelinemap(const char* filename)
unsigned int filesize = GetFileSize(hFile, 0);
if(!filesize)
{
CloseHandle(hFile);
GuiScriptError(0, "Empty script...");
return false;
}
@ -90,11 +89,10 @@ static bool scriptcreatelinemap(const char* filename)
DWORD read = 0;
if(!ReadFile(hFile, filedata, filesize, &read, 0))
{
CloseHandle(hFile);
GuiScriptError(0, "ReadFile failed...");
return false;
}
CloseHandle(hFile);
hFile.Close();
int len = (int)strlen(filedata);
char temp[256] = "";
LINEMAPENTRY entry;

View File

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

View File

@ -231,16 +231,6 @@ static DWORD WINAPI DbgCommandLoopThread(void* a)
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()
{
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();
dbginit();
dbgfunctionsinit();
json_set_alloc_funcs(emalloc_json, efree_json);
json_set_alloc_funcs(json_malloc, json_free);
if(yr_initialize() != ERROR_SUCCESS)
return "Failed to initialize Yara!";
wchar_t wszDir[deflen] = L"";