1
0
Fork 0

DBG: dynamicmem now working (crashes before)

This commit is contained in:
Mr. eXoDia 2014-08-06 22:47:50 +02:00
parent 0bff6d7427
commit b38a55bffb
11 changed files with 73 additions and 59 deletions

View File

@ -119,7 +119,7 @@ void formathex(char* string)
{
int len = (int)strlen(string);
_strupr(string);
char* new_string = Memory(len + 1, "formathex:new_string");
Memory<char*> new_string(len + 1, "formathex:new_string");
memset(new_string, 0, len + 1);
for(int i = 0, j = 0; i < len; i++)
if(isxdigit(string[i]))
@ -131,7 +131,7 @@ void formatdec(char* string)
{
int len = (int)strlen(string);
_strupr(string);
char* new_string = Memory(len + 1, "formatdec:new_string");
Memory<char*> new_string(len + 1, "formatdec:new_string");
memset(new_string, 0, len + 1);
for(int i = 0, j = 0; i < len; i++)
if(isdigit(string[i]))

View File

@ -232,7 +232,7 @@ bool apienumexports(uint base, EXPORTENUMCALLBACK cbEnum)
MEMORY_BASIC_INFORMATION mbi;
VirtualQueryEx(fdProcessInfo->hProcess, (const void*)base, &mbi, sizeof(mbi));
uint size = mbi.RegionSize;
void* buffer = Memory(size, "apienumexports:buffer");
Memory<void*> buffer(size, "apienumexports:buffer");
if(!memread(fdProcessInfo->hProcess, (const void*)base, buffer, size, 0))
return false;
IMAGE_NT_HEADERS* pnth = (IMAGE_NT_HEADERS*)((uint)buffer + GetPE32DataFromMappedFile((ULONG_PTR)buffer, 0, UE_PE_OFFSET));

View File

@ -487,7 +487,7 @@ static BOOL CALLBACK SymRegisterCallbackProc64(HANDLE hProcess, ULONG ActionCode
}
if(strstr(text, " bytes - "))
{
char* newtext = Memory(len + 1, "SymRegisterCallbackProc64:newtext");
Memory<char*> newtext(len + 1, "SymRegisterCallbackProc64:newtext");
strcpy(newtext, text);
strstr(newtext, " bytes - ")[8] = 0;
GuiSymbolLogAdd(newtext);
@ -700,7 +700,7 @@ static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo)
if(NumberOfCallBacks)
{
dprintf("TLS Callbacks: %d\n", NumberOfCallBacks);
uint* TLSCallBacks = Memory(NumberOfCallBacks * sizeof(uint), "cbCreateProcess:TLSCallBacks");
Memory<uint*> TLSCallBacks(NumberOfCallBacks * sizeof(uint), "cbCreateProcess:TLSCallBacks");
if(!TLSGrabCallBackData(DebugFileName, TLSCallBacks, &NumberOfCallBacks))
dputs("failed to get TLS callback addresses!");
else
@ -956,7 +956,7 @@ static void cbOutputDebugString(OUTPUT_DEBUG_STRING_INFO* DebugString)
if(!DebugString->fUnicode) //ASCII
{
char* DebugText = Memory(DebugString->nDebugStringLength + 1, "cbOutputDebugString:DebugText");
Memory<char*> DebugText(DebugString->nDebugStringLength + 1, "cbOutputDebugString:DebugText");
if(memread(fdProcessInfo->hProcess, DebugString->lpDebugStringData, DebugText, DebugString->nDebugStringLength, 0))
{
int len = (int)strlen(DebugText);
@ -964,7 +964,7 @@ static void cbOutputDebugString(OUTPUT_DEBUG_STRING_INFO* DebugString)
for(int i = 0; i < len; i++)
if(DebugText[i] == '\\' or DebugText[i] == '\"' or !isprint(DebugText[i]))
escape_count++;
char* DebugTextEscaped = Memory(len + escape_count * 3 + 1, "cbOutputDebugString:DebugTextEscaped");
Memory<char*> DebugTextEscaped(len + escape_count * 3 + 1, "cbOutputDebugString:DebugTextEscaped");
for(int i = 0, j = 0; i < len; i++)
{
switch(DebugText[i])
@ -1069,7 +1069,7 @@ static void cbException(EXCEPTION_DEBUG_INFO* ExceptionData)
nameInfo.dwThreadID = ((DEBUG_EVENT*)GetDebugData())->dwThreadId;
if(nameInfo.dwType == 0x1000 and nameInfo.dwFlags == 0 and threadisvalid(nameInfo.dwThreadID)) //passed basic checks
{
char* ThreadName = Memory(MAX_THREAD_NAME_SIZE, "cbException:ThreadName");
Memory<char*> ThreadName(MAX_THREAD_NAME_SIZE, "cbException:ThreadName");
memset(ThreadName, 0, MAX_THREAD_NAME_SIZE);
if(memread(fdProcessInfo->hProcess, nameInfo.szName, ThreadName, MAX_THREAD_NAME_SIZE - 1, 0))
{
@ -1078,7 +1078,7 @@ static void cbException(EXCEPTION_DEBUG_INFO* ExceptionData)
for(int i = 0; i < len; i++)
if(ThreadName[i] == '\\' or ThreadName[i] == '\"' or !isprint(ThreadName[i]))
escape_count++;
char* ThreadNameEscaped = Memory(len + escape_count * 3 + 1, "cbException:ThreadNameEscaped");
Memory<char*> ThreadNameEscaped(len + escape_count * 3 + 1, "cbException:ThreadNameEscaped");
memset(ThreadNameEscaped, 0, len + escape_count * 3 + 1);
for(int i = 0, j = 0; i < len; i++)
{

View File

@ -338,7 +338,7 @@ bool disasmgetstringat(uint addr, STRING_TYPE* type, char* ascii, char* unicode,
*type = str_none;
if(!disasmispossiblestring(addr))
return false;
unsigned char* data = Memory((maxlen + 1) * 2, "disasmgetstringat:data");
Memory<unsigned char*> data((maxlen + 1) * 2, "disasmgetstringat:data");
memset(data, 0, (maxlen + 1) * 2);
if(!memread(fdProcessInfo->hProcess, (const void*)addr, data, (maxlen + 1) * 2, 0))
return false;

View File

@ -1,25 +1,48 @@
#ifndef _DYNAMICMEM_H
#define _DYNAMICMEM_H
template<class T>
class Memory
{
public:
Memory(const char* reason = "Memory:???")
{
mPtr = 0;
mSize = 0;
mReason = reason;
}
Memory(size_t size, const char* reason = "Memory:???")
{
this->realloc(size, reason);
mPtr = reinterpret_cast<T>(emalloc(size));
mSize = size;
mReason = reason;
memset(mPtr, 0, size);
}
~Memory()
{
efree(mPtr, mReason);
efree(mPtr);
}
Memory realloc(size_t size, const char* reason = "Memory:???")
T realloc(size_t size, const char* reason = "Memory:???")
{
mPtr = reinterpret_cast<T>(erealloc(mPtr, size));
mSize = size;
mPtr = erealloc(mPtr, size, reason);
mReason = reason;
return *this;
memset(mPtr, 0, size);
return mPtr;
}
template<class U>
operator U()
{
return (U)mPtr;
}
operator T()
{
return mPtr;
}
size_t size()
@ -27,15 +50,8 @@ public:
return mSize;
}
//return a typeless pointer
template<class T>
operator T* ()
{
return static_cast<T*>(mPtr);
}
private:
void* mPtr;
T mPtr;
size_t mSize;
const char* mReason;
};

View File

@ -172,7 +172,7 @@ CMDRESULT cbInstrVarList(int argc, char* argv[])
dputs("no variables!");
return STATUS_CONTINUE;
}
VAR* variables = Memory(cbsize, "cbInstrVarList:variables");
Memory<VAR*> variables(cbsize, "cbInstrVarList:variables");
if(!varenum(variables, 0))
{
dputs("error listing variables!");
@ -182,12 +182,10 @@ CMDRESULT cbInstrVarList(int argc, char* argv[])
int varcount = (int)cbsize / sizeof(VAR);
for(int i = 0; i < varcount; i++)
{
if(variables[i].alias.length())
continue;
char name[deflen] = "";
strcpy(name, variables[i].name.c_str());
int len = (int)strlen(name);
for(int j = 0; j < len; j++)
if(name[j] == 1)
name[j] = '/';
uint value = (uint)variables[i].value.u.value;
if(variables[i].type != VAR_HIDDEN)
{
@ -923,9 +921,9 @@ CMDRESULT cbInstrGetstr(int argc, char* argv[])
dprintf("failed to get variable size \"%s\"!\n", argv[1]);
return STATUS_ERROR;
}
char* string = Memory(size + 1, "cbInstrGetstr:string");
Memory<char*> string(size + 1, "cbInstrGetstr:string");
memset(string, 0, size + 1);
if(!varget(argv[1], string, &size, 0))
if(!varget(argv[1], (char*)string, &size, 0))
{
dprintf("failed to get variable data \"%s\"!\n", argv[1]);
return STATUS_ERROR;
@ -958,9 +956,9 @@ CMDRESULT cbInstrCopystr(int argc, char* argv[])
dprintf("failed to get variable size \"%s\"!\n", argv[2]);
return STATUS_ERROR;
}
char* string = Memory(size + 1, "cbInstrGetstr:string");
Memory<char*> string(size + 1, "cbInstrGetstr:string");
memset(string, 0, size + 1);
if(!varget(argv[2], string, &size, 0))
if(!varget(argv[2], (char*)string, &size, 0))
{
dprintf("failed to get variable data \"%s\"!\n", argv[2]);
return STATUS_ERROR;
@ -1008,7 +1006,7 @@ CMDRESULT cbInstrFind(int argc, char* argv[])
dprintf("invalid memory address "fhex"!\n", addr);
return STATUS_ERROR;
}
unsigned char* data = Memory(size, "cbInstrFind:data");
Memory<unsigned char*> data(size, "cbInstrFind:data");
if(!memread(fdProcessInfo->hProcess, (const void*)base, data, size, 0))
{
dputs("failed to read memory!");
@ -1060,7 +1058,7 @@ CMDRESULT cbInstrFindAll(int argc, char* argv[])
dprintf("invalid memory address "fhex"!\n", addr);
return STATUS_ERROR;
}
unsigned char* data = Memory(size, "cbInstrFindAll:data");
Memory<unsigned char*> data(size, "cbInstrFindAll:data");
if(!memread(fdProcessInfo->hProcess, (const void*)base, data, size, 0))
{
dputs("failed to read memory!");
@ -1109,7 +1107,7 @@ CMDRESULT cbInstrFindAll(int argc, char* argv[])
GuiReferenceSetCellContent(refCount, 0, msg);
if(findData)
{
unsigned char* printData = Memory(patternsize, "cbInstrFindAll:printData");
Memory<unsigned char*> printData(patternsize, "cbInstrFindAll:printData");
memread(fdProcessInfo->hProcess, (const void*)result, printData, patternsize, 0);
for(int j = 0, k = 0; j < patternsize; j++)
{
@ -1194,7 +1192,7 @@ CMDRESULT cbInstrCommentList(int argc, char* argv[])
dputs("no comments");
return STATUS_CONTINUE;
}
COMMENTSINFO* comments = Memory(cbsize, "cbInstrCommentList:comments");
Memory<COMMENTSINFO*> comments(cbsize, "cbInstrCommentList:comments");
commentenum(comments, 0);
int count = (int)(cbsize / sizeof(COMMENTSINFO));
for(int i = 0; i < count; i++)
@ -1229,7 +1227,7 @@ CMDRESULT cbInstrLabelList(int argc, char* argv[])
dputs("no labels");
return STATUS_CONTINUE;
}
LABELSINFO* labels = Memory(cbsize, "cbInstrLabelList:labels");
Memory<LABELSINFO*> labels(cbsize, "cbInstrLabelList:labels");
labelenum(labels, 0);
int count = (int)(cbsize / sizeof(LABELSINFO));
for(int i = 0; i < count; i++)
@ -1263,7 +1261,7 @@ CMDRESULT cbInstrBookmarkList(int argc, char* argv[])
dputs("no bookmarks");
return STATUS_CONTINUE;
}
BOOKMARKSINFO* bookmarks = Memory(cbsize, "cbInstrBookmarkList:bookmarks");
Memory<BOOKMARKSINFO*> bookmarks(cbsize, "cbInstrBookmarkList:bookmarks");
bookmarkenum(bookmarks, 0);
int count = (int)(cbsize / sizeof(BOOKMARKSINFO));
for(int i = 0; i < count; i++)
@ -1298,7 +1296,7 @@ CMDRESULT cbInstrFunctionList(int argc, char* argv[])
dputs("no functions");
return STATUS_CONTINUE;
}
FUNCTIONSINFO* functions = Memory(cbsize, "cbInstrFunctionList:functions");
Memory<FUNCTIONSINFO*> functions(cbsize, "cbInstrFunctionList:functions");
functionenum(functions, 0);
int count = (int)(cbsize / sizeof(FUNCTIONSINFO));
for(int i = 0; i < count; i++)
@ -1344,7 +1342,7 @@ CMDRESULT cbInstrLoopList(int argc, char* argv[])
dputs("no loops");
return STATUS_CONTINUE;
}
LOOPSINFO* loops = Memory(cbsize, "cbInstrLoopList:loops");
Memory<LOOPSINFO*> loops(cbsize, "cbInstrLoopList:loops");
loopenum(loops, 0);
int count = (int)(cbsize / sizeof(LOOPSINFO));
for(int i = 0; i < count; i++)

View File

@ -56,7 +56,7 @@ mathformat:
void mathformat(char* text)
{
int len = (int)strlen(text);
char* temp = Memory(len + 1, "mathformat:temp");
Memory<char*> temp(len + 1, "mathformat:temp");
memset(temp, 0, len + 1);
for(int i = 0, j = 0; i < len; i++)
if(mathisoperator(text[i]) < 3 or text[i] != text[i + 1])
@ -337,7 +337,8 @@ bool mathhandlebrackets(char* expression, bool silent, bool baseonly)
return true;
expstruct.total_pairs = total_pairs;
expstruct.pairs = Memory(expstruct.total_pairs * sizeof(BRACKET_PAIR), "mathhandlebrackets:expstruct.pairs");
Memory<BRACKET_PAIR*> pairs(expstruct.total_pairs * sizeof(BRACKET_PAIR), "mathhandlebrackets:expstruct.pairs");
expstruct.pairs = pairs;
memset(expstruct.pairs, 0, expstruct.total_pairs * sizeof(BRACKET_PAIR));
matchpairs(&expstruct, expression, 0);
int deepest = 0;
@ -376,8 +377,8 @@ bool mathfromstring(const char* string, uint* value, bool silent, bool baseonly,
}
if(!highestop)
return valfromstring(string, value, silent, baseonly, value_size, isvar, 0);
char* strleft = Memory(len + 1 + negative, "mathfromstring:strleft");
char* strright = Memory(len + 1, "mathfromstring:strright");
Memory<char*> strleft(len + 1 + negative, "mathfromstring:strleft");
Memory<char*> strright(len + 1, "mathfromstring:strright");
memset(strleft, 0, len + 1);
memset(strright, 0, len + 1);
strncpy(strleft, string - negative, highestop_pos + negative);

View File

@ -79,7 +79,7 @@ void memupdatemap(HANDLE hProcess)
for(int k = 0; k < len; k++)
if(SectionName[k] == '\\' or SectionName[k] == '\"' or !isprint(SectionName[k]))
escape_count++;
char* SectionNameEscaped = Memory(len + escape_count * 3 + 1, "_dbg_memmap:SectionNameEscaped");
Memory<char*> SectionNameEscaped(len + escape_count * 3 + 1, "_dbg_memmap:SectionNameEscaped");
memset(SectionNameEscaped, 0, len + escape_count * 3 + 1);
for(int k = 0, l = 0; k < len; k++)
{
@ -211,7 +211,7 @@ bool mempatch(HANDLE hProcess, void* lpBaseAddress, const void* lpBuffer, SIZE_T
{
if(!hProcess or !lpBaseAddress or !lpBuffer or !nSize) //generic failures
return false;
unsigned char* olddata = Memory(nSize, "mempatch:olddata");
Memory<unsigned char*> olddata(nSize, "mempatch:olddata");
if(!memread(hProcess, lpBaseAddress, olddata, nSize, 0))
return memwrite(hProcess, lpBaseAddress, lpBuffer, nSize, lpNumberOfBytesWritten);
unsigned char* newdata = (unsigned char*)lpBuffer;
@ -240,7 +240,7 @@ static int formathexpattern(char* string)
{
int len = (int)strlen(string);
_strupr(string);
char* new_string = Memory(len + 1, "formathexpattern:new_string");
Memory<char*> new_string(len + 1, "formathexpattern:new_string");
memset(new_string, 0, len + 1);
for(int i = 0, j = 0; i < len; i++)
if(string[i] == '?' or isxdigit(string[i]))
@ -257,7 +257,7 @@ static bool patterntransform(const char* text, std::vector<PATTERNBYTE>* pattern
int len = (int)strlen(text);
if(!len)
return false;
char* newtext = Memory(len + 2, "transformpattern:newtext");
Memory<char*> newtext(len + 2, "transformpattern:newtext");
strcpy(newtext, text);
len = formathexpattern(newtext);
if(len % 2) //not a multiple of 2

View File

@ -31,7 +31,7 @@ int reffind(uint addr, uint size, CBREF cbRef, void* userinfo, bool silent)
else
start_size = maxsize;
}
unsigned char* data = Memory(start_size, "reffind:data");
Memory<unsigned char*> data(start_size, "reffind:data");
if(!memread(fdProcessInfo->hProcess, (const void*)start_addr, data, start_size, 0))
{
if(!silent)

View File

@ -75,7 +75,7 @@ static bool scriptcreatelinemap(const char* filename)
GuiScriptError(0, "Empty script...");
return false;
}
char* filedata = Memory(filesize + 1, "createlinemap:filedata");
Memory<char*> filedata(filesize + 1, "createlinemap:filedata");
memset(filedata, 0, filesize + 1);
DWORD read = 0;
if(!ReadFile(hFile, filedata, filesize, &read, 0))

View File

@ -1050,11 +1050,11 @@ bool valapifromstring(const char* name, uint* value, int* value_size, bool print
int found = 0;
int kernelbase = -1;
DWORD cbNeeded = 0;
uint* addrfound = 0;
Memory<uint*> addrfound;
if(EnumProcessModules(fdProcessInfo->hProcess, 0, 0, &cbNeeded))
{
addrfound = Memory(cbNeeded * sizeof(uint), "valapifromstring:addrfound");
HMODULE* hMods = Memory(cbNeeded * sizeof(HMODULE), "valapifromstring:hMods");
addrfound.realloc(cbNeeded * sizeof(uint), "valapifromstring:addrfound");
Memory<HMODULE*> hMods(cbNeeded * sizeof(HMODULE), "valapifromstring:hMods");
if(EnumProcessModules(fdProcessInfo->hProcess, hMods, cbNeeded, &cbNeeded))
{
for(unsigned int i = 0; i < cbNeeded / sizeof(HMODULE); i++)
@ -1163,7 +1163,7 @@ bool valfromstring(const char* string, uint* value, bool silent, bool baseonly,
else if(mathcontains(string)) //handle math
{
int len = (int)strlen(string);
char* newstring = Memory(len * 2, "valfromstring:newstring");
Memory<char*> newstring(len * 2, "valfromstring:newstring");
if(strstr(string, "[")) //memory brackets: []
{
for(int i = 0, j = 0; i < len; i++)
@ -1183,7 +1183,7 @@ bool valfromstring(const char* string, uint* value, bool silent, bool baseonly,
}
else
strcpy(newstring, string);
char* string_ = Memory(len + 256, "valfromstring:string_");
Memory<char*> string_(len + 256, "valfromstring:string_");
strcpy(string_, newstring);
int add = 0;
bool negative = (*string_ == '-');
@ -1217,7 +1217,7 @@ bool valfromstring(const char* string, uint* value, bool silent, bool baseonly,
return true;
}
int len = (int)strlen(string);
char* newstring = Memory(len * 2, "valfromstring:newstring");
Memory<char*> newstring(len * 2, "valfromstring:newstring");
if(strstr(string, "["))
{
for(int i = 0, j = 0; i < len; i++)
@ -1326,7 +1326,6 @@ bool valfromstring(const char* string, uint* value, bool silent, bool baseonly,
sscanf(string + inc, "%"fext"x", value);
return true;
}
if(baseonly)
return false;
else if(valapifromstring(string, value, value_size, true, silent, hexonly)) //then come APIs
@ -1374,7 +1373,7 @@ bool valtostring(const char* string, uint* value, bool silent)
return false;
}
int len = (int)strlen(string);
char* newstring = Memory(len * 2, "valfromstring:newstring");
Memory<char*> newstring(len * 2, "valfromstring:newstring");
if(strstr(string, "[")) //memory brackets: []
{
for(int i = 0, j = 0; i < len; i++)
@ -1427,7 +1426,7 @@ bool valtostring(const char* string, uint* value, bool silent)
return false;
}
bool ok = setregister(string, *value);
char* regName = Memory(strlen(string) + 1, "valtostring:regname");
Memory<char*> regName(strlen(string) + 1, "valtostring:regname");
strcpy(regName, string);
_strlwr(regName);
if(strstr(regName, "ip"))