1
0
Fork 0

DBG: Add asserts in a bunch of places

This commit is contained in:
Nukem 2015-10-17 23:32:52 -04:00
parent 394cae37eb
commit c0a9ec5a50
15 changed files with 138 additions and 168 deletions

View File

@ -18,7 +18,8 @@
#include <algorithm>
#include <unordered_map>
#include <tlhelp32.h>
#include "..\types.h"
#include "..\dbg_types.h"
#include "..\dbg_assert.h"
#include "..\bridge\bridgemain.h"
#include "jansson\jansson.h"
#include "jansson\jansson_x64dbg.h"

View File

@ -7,9 +7,7 @@ std::unordered_map<duint, BOOKMARKSINFO> bookmarks;
bool BookmarkSet(duint Address, bool Manual)
{
// CHECK: Export call
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// Validate the incoming address
if(!MemIsValidReadPtr(Address))
@ -34,29 +32,23 @@ bool BookmarkSet(duint Address, bool Manual)
bool BookmarkGet(duint Address)
{
// CHECK: Export call
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
SHARED_ACQUIRE(LockBookmarks);
return (bookmarks.count(ModHashFromAddr(Address)) > 0);
}
bool BookmarkDelete(duint Address)
{
// CHECK: Export call
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
EXCLUSIVE_ACQUIRE(LockBookmarks);
return (bookmarks.erase(ModHashFromAddr(Address)) > 0);
}
void BookmarkDelRange(duint Start, duint End)
{
// CHECK: Export call
if(!DbgIsDebugging())
return;
ASSERT_DEBUGGING("Export call");
// Are all bookmarks going to be deleted?
// 0x00000000 - 0xFFFFFFFF
@ -99,7 +91,7 @@ void BookmarkDelRange(duint Start, duint End)
void BookmarkCacheSave(JSON Root)
{
EXCLUSIVE_ACQUIRE(LockBookmarks);
EXCLUSIVE_ACQUIRE(LockBookmarks);
const JSON jsonBookmarks = json_array();
const JSON jsonAutoBookmarks = json_array();
@ -176,9 +168,7 @@ void BookmarkCacheLoad(JSON Root)
bool BookmarkEnum(BOOKMARKSINFO* List, size_t* Size)
{
// The array container must be set, or the size must be set, or both
if(!List && !Size)
return false;
ASSERT_FALSE(!List && !Size);
SHARED_ACQUIRE(LockBookmarks);
// Return the size if set

View File

@ -28,10 +28,7 @@ BREAKPOINT* BpInfoFromAddr(BP_TYPE Type, duint Address)
int BpGetList(std::vector<BREAKPOINT>* List)
{
// CHECK: Exported function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
SHARED_ACQUIRE(LockBreakpoints);
// Did the caller request an output?
@ -54,9 +51,7 @@ int BpGetList(std::vector<BREAKPOINT>* List)
bool BpNew(duint Address, bool Enable, bool Singleshot, short OldBytes, BP_TYPE Type, DWORD TitanType, const char* Name)
{
// CHECK: Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// Fail if the address is a bad memory region
if(!MemIsValidReadPtr(Address))
@ -93,10 +88,7 @@ bool BpNew(duint Address, bool Enable, bool Singleshot, short OldBytes, BP_TYPE
bool BpGet(duint Address, BP_TYPE Type, const char* Name, BREAKPOINT* Bp)
{
// CHECK: Export/Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
SHARED_ACQUIRE(LockBreakpoints);
// Name is optional
@ -142,22 +134,16 @@ bool BpGet(duint Address, BP_TYPE Type, const char* Name, BREAKPOINT* Bp)
bool BpDelete(duint Address, BP_TYPE Type)
{
// CHECK: Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Command function call");
EXCLUSIVE_ACQUIRE(LockBreakpoints);
// Erase the index from the global list
EXCLUSIVE_ACQUIRE(LockBreakpoints);
return (breakpoints.erase(BreakpointKey(Type, ModHashFromAddr(Address))) > 0);
}
bool BpEnable(duint Address, BP_TYPE Type, bool Enable)
{
// CHECK: Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Command function call");
EXCLUSIVE_ACQUIRE(LockBreakpoints);
// Check if the breakpoint exists first
@ -172,16 +158,13 @@ bool BpEnable(duint Address, BP_TYPE Type, bool Enable)
bool BpSetName(duint Address, BP_TYPE Type, const char* Name)
{
// CHECK: Future(?); This is not used anywhere
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Future(?): This is not used anywhere");
EXCLUSIVE_ACQUIRE(LockBreakpoints);
// If a name wasn't supplied, set to nothing
if(!Name)
Name = "";
EXCLUSIVE_ACQUIRE(LockBreakpoints);
// Check if the breakpoint exists first
BREAKPOINT* bpInfo = BpInfoFromAddr(Type, Address);
@ -194,10 +177,7 @@ bool BpSetName(duint Address, BP_TYPE Type, const char* Name)
bool BpSetTitanType(duint Address, BP_TYPE Type, int TitanType)
{
// CHECK: Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Command function call");
EXCLUSIVE_ACQUIRE(LockBreakpoints);
// Set the TitanEngine type, separate from BP_TYPE
@ -212,9 +192,7 @@ bool BpSetTitanType(duint Address, BP_TYPE Type, int TitanType)
bool BpEnumAll(BPENUMCALLBACK EnumCallback, const char* Module)
{
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
SHARED_ACQUIRE(LockBreakpoints);
// Loop each entry, executing the user's callback
@ -285,9 +263,8 @@ void BpToBridge(const BREAKPOINT* Bp, BRIDGEBP* BridgeBp)
// Convert a debugger breakpoint to an open/exported
// bridge breakpoint
//
// TOOD: ASSERT(?) These should never be null
if(!Bp || !BridgeBp)
return;
ASSERT_NONNULL(Bp);
ASSERT_NONNULL(BridgeBp);
memset(BridgeBp, 0, sizeof(BRIDGEBP));
strcpy_s(BridgeBp->mod, Bp->mod);

View File

@ -7,9 +7,7 @@ std::unordered_map<duint, COMMENTSINFO> comments;
bool CommentSet(duint Address, const char* Text, bool Manual)
{
// CHECK: Exported/Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// A valid memory address must be supplied
if(!MemIsValidReadPtr(Address))
@ -45,10 +43,7 @@ bool CommentSet(duint Address, const char* Text, bool Manual)
bool CommentGet(duint Address, char* Text)
{
// CHECK: Exported/Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
SHARED_ACQUIRE(LockComments);
// Get an existing comment and copy the string buffer
@ -68,19 +63,15 @@ bool CommentGet(duint Address, char* Text)
bool CommentDelete(duint Address)
{
// CHECK: Command/Sub function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
EXCLUSIVE_ACQUIRE(LockComments);
return (comments.erase(ModHashFromAddr(Address)) > 0);
}
void CommentDelRange(duint Start, duint End)
{
// CHECK: Export function
if(!DbgIsDebugging())
return;
ASSERT_DEBUGGING("Export call");
// Are all comments going to be deleted?
// 0x00000000 - 0xFFFFFFFF
@ -211,14 +202,10 @@ void CommentCacheLoad(JSON Root)
bool CommentEnum(COMMENTSINFO* List, size_t* Size)
{
// CHECK: Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Command function call");
// At least 1 parameter must be supplied
if(!List && !Size)
return false;
ASSERT_FALSE(!List && !Size);
SHARED_ACQUIRE(LockComments);
// Check if the user requested size only

View File

@ -7,9 +7,7 @@ std::map<ModuleRange, FUNCTIONSINFO, ModuleRangeCompare> functions;
bool FunctionAdd(duint Start, duint End, bool Manual)
{
// CHECK: Export/Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// Make sure memory is readable
if(!MemIsValidReadPtr(Start))
@ -40,9 +38,7 @@ bool FunctionAdd(duint Start, duint End, bool Manual)
bool FunctionGet(duint Address, duint* Start, duint* End)
{
// CHECK: Exported function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
const duint moduleBase = ModBaseFromAddr(Address);
@ -66,9 +62,7 @@ bool FunctionGet(duint Address, duint* Start, duint* End)
bool FunctionOverlaps(duint Start, duint End)
{
// CHECK: Exported function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// A function can't end before it begins
if(Start > End)
@ -82,9 +76,7 @@ bool FunctionOverlaps(duint Start, duint End)
bool FunctionDelete(duint Address)
{
// CHECK: Exported function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
const duint moduleBase = ModBaseFromAddr(Address);
@ -94,9 +86,7 @@ bool FunctionDelete(duint Address)
void FunctionDelRange(duint Start, duint End)
{
// CHECK: Exported function
if(!DbgIsDebugging())
return;
ASSERT_DEBUGGING("Export call");
// Should all functions be deleted?
// 0x00000000 - 0xFFFFFFFF
@ -221,14 +211,10 @@ void FunctionCacheLoad(JSON Root)
bool FunctionEnum(FUNCTIONSINFO* List, size_t* Size)
{
// CHECK: Exported function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// If a list isn't passed and the size not requested, fail
if(!List && !Size)
return false;
ASSERT_FALSE(!List && !Size);
SHARED_ACQUIRE(LockFunctions);
// Did the caller request the buffer size needed?

View File

@ -7,9 +7,7 @@ std::unordered_map<duint, LABELSINFO> labels;
bool LabelSet(duint Address, const char* Text, bool Manual)
{
// CHECK: Exported/Command function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Exported/Command function");
// A valid memory address must be supplied
if(!MemIsValidReadPtr(Address))
@ -47,10 +45,7 @@ bool LabelSet(duint Address, const char* Text, bool Manual)
bool LabelFromString(const char* Text, duint* Address)
{
// CHECK: Future? (Not used)
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Future(?): Currently not used");
SHARED_ACQUIRE(LockLabels);
for(auto & itr : labels)
@ -71,10 +66,7 @@ bool LabelFromString(const char* Text, duint* Address)
bool LabelGet(duint Address, char* Text)
{
// CHECK: Export function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
SHARED_ACQUIRE(LockLabels);
// Was the label at this address exist?
@ -92,19 +84,15 @@ bool LabelGet(duint Address, char* Text)
bool LabelDelete(duint Address)
{
// CHECK: Export function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
EXCLUSIVE_ACQUIRE(LockLabels);
return (labels.erase(ModHashFromAddr(Address)) > 0);
}
void LabelDelRange(duint Start, duint End)
{
// CHECK: Export function
if(!DbgIsDebugging())
return;
ASSERT_DEBUGGING("Export call");
// Are all comments going to be deleted?
// 0x00000000 - 0xFFFFFFFF
@ -241,9 +229,7 @@ void LabelCacheLoad(JSON Root)
bool LabelEnum(LABELSINFO* List, size_t* Size)
{
// CHECK: Export function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// At least 1 parameter is required
if(!List && !Size)

View File

@ -7,9 +7,7 @@ std::map<DepthModuleRange, LOOPSINFO, DepthModuleRangeCompare> loops;
bool LoopAdd(duint Start, duint End, bool Manual)
{
// CHECK: Export function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// Loop must begin before it ends
if(Start > End)
@ -55,9 +53,7 @@ bool LoopAdd(duint Start, duint End, bool Manual)
// Get the start/end of a loop at a certain depth and address
bool LoopGet(int Depth, duint Address, duint* Start, duint* End)
{
// CHECK: Exported function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// Get the virtual address module
const duint moduleBase = ModBaseFromAddr(Address);
@ -83,12 +79,10 @@ bool LoopGet(int Depth, duint Address, duint* Start, duint* End)
return true;
}
//check if a loop overlaps a range, inside is not overlapping
// Check if a loop overlaps a range, inside is not overlapping
bool LoopOverlaps(int Depth, duint Start, duint End, int* FinalDepth)
{
// CHECK: Export function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// Determine module addresses and lookup keys
const duint moduleBase = ModBaseFromAddr(Start);
@ -139,6 +133,7 @@ bool LoopOverlaps(int Depth, duint Start, duint End, int* FinalDepth)
// This should delete a loop and all sub-loops that matches a certain addr
bool LoopDelete(int Depth, duint Address)
{
ASSERT_ALWAYS("Function unimplemented");
return false;
}
@ -235,9 +230,7 @@ void LoopCacheLoad(JSON Root)
bool LoopEnum(LOOPSINFO* List, size_t* Size)
{
// If list or size is not requested, fail
if(!List && !Size)
return false;
ASSERT_FALSE(!List && !Size);
SHARED_ACQUIRE(LockLoops);
// See if the caller requested an output size

View File

@ -341,7 +341,7 @@ duint MemAllocRemote(duint Address, duint Size, DWORD Type, DWORD Protect)
bool MemFreeRemote(duint Address)
{
return !!VirtualFreeEx(fdProcessInfo->hProcess, (LPVOID)Address, 0, MEM_RELEASE);
return VirtualFreeEx(fdProcessInfo->hProcess, (LPVOID)Address, 0, MEM_RELEASE) == TRUE;
}
duint MemGetPageAligned(duint Address)
@ -437,9 +437,7 @@ bool MemPageRightsToString(DWORD Protect, char* Rights)
bool MemPageRightsFromString(DWORD* Protect, const char* Rights)
{
if(strlen(Rights) < 2)
return false;
ASSERT_TRUE(strlen(Rights) >= 2);
*Protect = 0;
// Check for the PAGE_GUARD flag

View File

@ -193,9 +193,7 @@ MODINFO* ModInfoFromAddr(duint Address)
bool ModNameFromAddr(duint Address, char* Name, bool Extension)
{
if(!Name)
return false;
ASSERT_NONNULL(Name);
SHARED_ACQUIRE(LockModules);
// Get a pointer to module information
@ -241,17 +239,16 @@ duint ModHashFromAddr(duint Address)
duint ModHashFromName(const char* Module)
{
// return MODINFO.hash (based on the name)
if(!Module || Module[0] == '\0')
return 0;
ASSERT_NONNULL(Module);
ASSERT_FALSE(Module[0] == '\0');
return murmurhash(Module, (int)strlen(Module));
}
duint ModBaseFromName(const char* Module)
{
if(!Module || strlen(Module) >= MAX_MODULE_SIZE)
return 0;
ASSERT_NONNULL(Module);
ASSERT_TRUE(strlen(Module) < MAX_MODULE_SIZE);
SHARED_ACQUIRE(LockModules);
for(const auto & i : modinfo)

View File

@ -14,9 +14,7 @@ std::unordered_map<duint, PATCHINFO> patches;
bool PatchSet(duint Address, unsigned char OldByte, unsigned char NewByte)
{
// CHECK: Exported function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
// Address must be valid
if(!MemIsValidReadPtr(Address))
@ -64,10 +62,7 @@ bool PatchSet(duint Address, unsigned char OldByte, unsigned char NewByte)
bool PatchGet(duint Address, PATCHINFO* Patch)
{
// CHECK: Export
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
SHARED_ACQUIRE(LockPatches);
// Find this specific address in the list
@ -89,10 +84,7 @@ bool PatchGet(duint Address, PATCHINFO* Patch)
bool PatchDelete(duint Address, bool Restore)
{
// CHECK: Export function
if(!DbgIsDebugging())
return false;
ASSERT_DEBUGGING("Export call");
EXCLUSIVE_ACQUIRE(LockPatches);
// Do a list lookup with hash
@ -112,9 +104,7 @@ bool PatchDelete(duint Address, bool Restore)
void PatchDelRange(duint Start, duint End, bool Restore)
{
// CHECK: Export call
if(!DbgIsDebugging())
return;
ASSERT_DEBUGGING("Export call");
// Are all bookmarks going to be deleted?
// 0x00000000 - 0xFFFFFFFF
@ -156,14 +146,8 @@ void PatchDelRange(duint Start, duint End, bool Restore)
bool PatchEnum(PATCHINFO* List, size_t* Size)
{
// CHECK: Exported
if(!DbgIsDebugging())
return false;
// At least one parameter is needed
if(!List && !Size)
return false;
ASSERT_DEBUGGING("Export call");
ASSERT_FALSE(!List && !Size);
SHARED_ACQUIRE(LockPatches);
// Did the user request the size?

View File

@ -132,7 +132,7 @@ DWORD64 CALLBACK StackGetModuleBaseProc64(HANDLE hProcess, DWORD64 Address)
DWORD64 CALLBACK StackTranslateAddressProc64(HANDLE hProcess, HANDLE hThread, LPADDRESS64 lpaddr)
{
__debugbreak();
ASSERT_ALWAYS("This function should never be called");
return 0;
}
@ -216,8 +216,8 @@ void stackgetcallstack(duint csp, CALLSTACK* callstack)
frame.AddrStack.Mode = AddrModeFlat;
#endif
// Container for each callstack entry
std::vector<CALLSTACKENTRY> callstackVector;
// Container for each callstack entry (20 pre-allocated entries)
std::vector<CALLSTACKENTRY> callstackVector(20);
while(true)
{

71
src/dbg_assert.h Normal file
View File

@ -0,0 +1,71 @@
/**
* Code originally from:
*
* (c) 2015 Alexandro Sanchez Bach. All rights reserved.
* https://github.com/AlexAltea/nucleus/blob/9103d7eb52cc861cb6e317f0007246bd03d663a2/nucleus/assert.h
* https://github.com/AlexAltea/nucleus/blob/9103d7eb52cc861cb6e317f0007246bd03d663a2/LICENSE
* Released under GPL v2 license. Read LICENSE for more details.
*
*/
#pragma once
#include <cassert>
// Count arguments
#define __DBG_ARGUMENT_EXPAND(x) x
#define __DBG_ARGUMENT_COUNT(...) \
__DBG_ARGUMENT_EXPAND(__DBG_ARGUMENT_EXTRACT(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0))
#define __DBG_ARGUMENT_EXTRACT(a1, a2, a3, a4, a5, a6, a7, a8, N, ...) N
// Dispatching macros
#define __DBG_MACRO_DISPATCH(function, ...) \
__DBG_MACRO_SELECT(function, __DBG_ARGUMENT_COUNT(__VA_ARGS__))
#define __DBG_MACRO_SELECT(function, argc) \
__DBG_MACRO_CONCAT(function, argc)
#define __DBG_MACRO_CONCAT(a, b) a##b
// Trigger an exception if the debugger is not currently active
#define ASSERT_DEBUGGING(message) ASSERT_TRUE(DbgIsDebugging(), message)
// Trigger an exception if expression is false
#define ASSERT_TRUE(...) \
__DBG_MACRO_DISPATCH(ASSERT_TRUE, __VA_ARGS__)(__VA_ARGS__)
#define ASSERT_TRUE1(expr) assert(expr)
#define ASSERT_TRUE2(expr, message) assert((expr) && (message))
// Trigger an exception if expression is true
#define ASSERT_FALSE(...) \
__DBG_MACRO_DISPATCH(ASSERT_FALSE, __VA_ARGS__)(__VA_ARGS__)
#define ASSERT_FALSE1(expr) assert(!(expr))
#define ASSERT_FALSE2(expr, message) assert(!(expr) && (message))
// Trigger an exception if expression is zero
#define assert_zero(...) \
__DBG_MACRO_DISPATCH(assert_zero, __VA_ARGS__)(__VA_ARGS__)
#define assert_zero1(expr) assert(expr)
#define assert_zero2(expr, message) assert(((expr) == 0) && (message))
// Trigger an exception if expression is non-zero
#define assert_nonzero(...) \
__DBG_MACRO_DISPATCH(ASSERT_TRUE, __VA_ARGS__)(__VA_ARGS__)
#define assert_nonzero1(expr) assert_nonzero(expr)
#define assert_nonzero2(expr, message) assert(((expr) != 0) && (message))
// Trigger an exception if expression is a nullptr
#define assert_null(...) \
__DBG_MACRO_DISPATCH(assert_null, __VA_ARGS__)(__VA_ARGS__)
#define assert_null1(expr) assert(expr)
#define assert_null2(expr, message) assert(((expr) == nullptr) && (message))
// Trigger an exception if expression is not a nullptr
#define ASSERT_NONNULL(...) \
__DBG_MACRO_DISPATCH(ASSERT_NONNULL, __VA_ARGS__)(__VA_ARGS__)
#define ASSERT_NONNULL1(expr) assert(expr)
#define ASSERT_NONNULL2(expr, message) assert(((expr) != nullptr) && (message))
// Trigger an exception
#define ASSERT_ALWAYS(...) \
__DBG_MACRO_DISPATCH(ASSERT_ALWAYS, __VA_ARGS__)(__VA_ARGS__)
#define ASSERT_ALWAYS0(...) assert(0)
#define ASSERT_ALWAYS1(message) assert(0 && (message))

View File

@ -2,7 +2,7 @@
#define PAGEMEMORYRIGHTS_H
#include <QDialog>
#include "types.h"
#include "dbg_types.h"
namespace Ui
{

View File

@ -3,6 +3,6 @@
#include "bridge\bridgemain.h"
#include "dbg\_dbgfunctions.h"
#include "types.h"
#include "dbg_types.h"
#endif // IMPORTS_H