From c48de5924a3156c301acb1f8d81566bd5d61d845 Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Sun, 18 Nov 2018 02:57:14 +0100 Subject: [PATCH] Mayaqua: ability to toggle memory tracking at runtime, various logic improvements --- src/Mayaqua/Mayaqua.c | 34 ++++++++++++++----------- src/Mayaqua/Mayaqua.h | 52 ++++++++++++++++---------------------- src/Mayaqua/Memory.c | 57 ++++-------------------------------------- src/Mayaqua/Table.c | 10 -------- src/Mayaqua/Tracking.c | 48 ++++++----------------------------- src/Mayaqua/Tracking.h | 4 --- 6 files changed, 53 insertions(+), 152 deletions(-) diff --git a/src/Mayaqua/Mayaqua.c b/src/Mayaqua/Mayaqua.c index cd8708cb..5cb04c31 100644 --- a/src/Mayaqua/Mayaqua.c +++ b/src/Mayaqua/Mayaqua.c @@ -552,8 +552,11 @@ void InitMayaqua(bool memcheck, bool debug, int argc, char **argv) // Initialize the Kernel status InitKernelStatus(); - // Initialize the tracking - InitTracking(); + if (IsTrackingEnabled()) + { + // Initialize the tracking + InitTracking(); + } // Initialization of thread pool InitThreading(); @@ -696,22 +699,23 @@ void FreeMayaqua() // Release of thread pool FreeThreading(); -#ifndef VPN_SPEED - // Show the kernel status - if (g_debug) + if (IsTrackingEnabled()) { - PrintKernelStatus(); - } + // Show the kernel status + if (g_debug) + { + PrintKernelStatus(); + } - // Display the debug information - if (g_memcheck) - { - PrintDebugInformation(); - } -#endif // VPN_SPEED + // Display the debug information + if (g_memcheck) + { + PrintDebugInformation(); + } - // Release the tracking - FreeTracking(); + // Release the tracking + FreeTracking(); + } // Release of the kernel status FreeKernelStatus(); diff --git a/src/Mayaqua/Mayaqua.h b/src/Mayaqua/Mayaqua.h index c4ba1db7..ede43f1e 100644 --- a/src/Mayaqua/Mayaqua.h +++ b/src/Mayaqua/Mayaqua.h @@ -125,10 +125,8 @@ // Macro for the release flag #ifdef VPN_SPEED -#define DONT_USE_KERNEL_STATUS // Do not update the kernel status #define WIN32_USE_HEAP_API_FOR_MEMORY // Use the heap API to allocate memory #define WIN32_NO_DEBUG_HELP_DLL // Do not call the DLL for debugging -#define DONT_CHECK_HEAP // Do not check the status of the heap #define DONT_ALLOW_RUN_ON_DEBUGGER // Do not allow running on the debugger #endif // VPN_SPEED @@ -412,43 +410,35 @@ extern BOOL kernel_status_inited; #define KS_GETMAX64(id) (kernel_status_max[id]) #define KS_GETMAX(id) ((UINT)KS_GETMAX64(id)) -#ifdef DONT_USE_KERNEL_STATUS -// Disable operations of the kernel status -#define KS_INC(id) -#define KS_DEC(id) -#define KS_ADD(id, n) -#define KS_SUB(id, n) -#else // DONT_USE_KERNEL_STATUS -// Enable operations of the kernel status -#define KS_INC(id) \ -if (kernel_status_inited) { \ - KS_LOCK(id); \ - kernel_status[id]++; \ +// Operations of the kernel status +#define KS_INC(id) \ +if (IsTrackingEnabled()) { \ + KS_LOCK(id); \ + kernel_status[id]++; \ kernel_status_max[id] = MAX(kernel_status_max[id], kernel_status[id]); \ - KS_UNLOCK(id); \ + KS_UNLOCK(id); \ } -#define KS_DEC(id) \ -if (kernel_status_inited) { \ - KS_LOCK(id); \ - kernel_status[id]--; \ +#define KS_DEC(id) \ +if (IsTrackingEnabled()) { \ + KS_LOCK(id); \ + kernel_status[id]--; \ kernel_status_max[id] = MAX(kernel_status_max[id], kernel_status[id]); \ - KS_UNLOCK(id); \ + KS_UNLOCK(id); \ } -#define KS_ADD(id, n) \ -if (kernel_status_inited) { \ - KS_LOCK(id); \ - kernel_status[id] += n; \ +#define KS_ADD(id, n) \ +if (IsTrackingEnabled()) { \ + KS_LOCK(id); \ + kernel_status[id] += n; \ kernel_status_max[id] = MAX(kernel_status_max[id], kernel_status[id]); \ - KS_UNLOCK(id); \ + KS_UNLOCK(id); \ } -#define KS_SUB(id, n) \ -if (kernel_status_inited) { \ - KS_LOCK(id); \ - kernel_status[id] -= n; \ +#define KS_SUB(id, n) \ +if (IsTrackingEnabled()) { \ + KS_LOCK(id); \ + kernel_status[id] -= n; \ kernel_status_max[id] = MAX(kernel_status_max[id], kernel_status[id]); \ - KS_UNLOCK(id); \ + KS_UNLOCK(id); \ } -#endif // DONT_USE_KERNEL_STATUS // Kernel status // String related diff --git a/src/Mayaqua/Memory.c b/src/Mayaqua/Memory.c index a82d8962..e09da625 100644 --- a/src/Mayaqua/Memory.c +++ b/src/Mayaqua/Memory.c @@ -972,10 +972,6 @@ SK *NewSkEx(bool no_compact) s->p = Malloc(sizeof(void *) * s->num_reserved); s->no_compact = no_compact; -#ifndef DONT_USE_KERNEL_STATUS -// TrackNewObj(POINTER_TO_UINT64(s), "SK", 0); -#endif // DONT_USE_KERNEL_STATUS - // KS KS_INC(KS_NEWSK_COUNT); @@ -1011,10 +1007,6 @@ void CleanupSk(SK *s) DeleteLock(s->lock); Free(s); -#ifndef DONT_USE_KERNEL_STATUS -// TrackDeleteObj(POINTER_TO_UINT64(s)); -#endif // DONT_USE_KERNEL_STATUS - // KS KS_INC(KS_FREESK_COUNT); } @@ -1278,10 +1270,6 @@ void CleanupQueue(QUEUE *q) DeleteLock(q->lock); Free(q); -#ifndef DONT_USE_KERNEL_STATUS -// TrackDeleteObj(POINTER_TO_UINT64(q)); -#endif // DONT_USE_KERNEL_STATUS - // KS KS_INC(KS_FREEQUEUE_COUNT); } @@ -1297,10 +1285,6 @@ QUEUE *NewQueue() q->num_item = 0; q->fifo = NewFifo(); -#ifndef DONT_USE_KERNEL_STATUS -// TrackNewObj(POINTER_TO_UINT64(q), "QUEUE", 0); -#endif // DONT_USE_KERNEL_STATUS - // KS KS_INC(KS_NEWQUEUE_COUNT); @@ -1316,10 +1300,6 @@ QUEUE *NewQueueFast() q->num_item = 0; q->fifo = NewFifoFast(); -#ifndef DONT_USE_KERNEL_STATUS -// TrackNewObj(POINTER_TO_UINT64(q), "QUEUE", 0); -#endif // DONT_USE_KERNEL_STATUS - // KS KS_INC(KS_NEWQUEUE_COUNT); @@ -1783,10 +1763,6 @@ void CleanupList(LIST *o) // KS KS_INC(KS_FREELIST_COUNT); - -#ifndef DONT_USE_KERNEL_STATUS -// TrackDeleteObj(POINTER_TO_UINT64(o)); -#endif // DONT_USE_KERNEL_STATUS } // Check whether the specified number is already in the list @@ -2145,10 +2121,6 @@ LIST *NewListEx2(COMPARE *cmp, bool fast, bool fast_malloc) o->cmp = cmp; o->sorted = true; -#ifndef DONT_USE_KERNEL_STATUS -// TrackNewObj(POINTER_TO_UINT64(o), "LIST", 0); -#endif //DONT_USE_KERNEL_STATUS - // KS KS_INC(KS_NEWLIST_COUNT); @@ -2422,10 +2394,6 @@ void CleanupFifo(FIFO *f) Free(f->p); Free(f); -#ifndef DONT_USE_KERNEL_STATUS -// TrackDeleteObj(POINTER_TO_UINT64(f)); -#endif //DONT_USE_KERNEL_STATUS - // KS KS_INC(KS_FREEFIFO_COUNT); } @@ -2472,10 +2440,6 @@ FIFO *NewFifoEx2(bool fast, bool fixed) f->p = Malloc(FIFO_INIT_MEM_SIZE); f->fixed = false; -#ifndef DONT_USE_KERNEL_STATUS -// TrackNewObj(POINTER_TO_UINT64(f), "FIFO", 0); -#endif // DONT_USE_KERNEL_STATUS - // KS KS_INC(KS_NEWFIFO_COUNT); @@ -2793,10 +2757,6 @@ BUF *NewBuf() b->Current = 0; b->SizeReserved = INIT_BUF_SIZE; -#ifndef DONT_USE_KERNEL_STATUS -// TrackNewObj(POINTER_TO_UINT64(b), "BUF", 0); -#endif // DONT_USE_KERNEL_STATUS - // KS KS_INC(KS_NEWBUF_COUNT); KS_INC(KS_CURRENT_BUF_COUNT); @@ -3241,10 +3201,6 @@ void FreeBuf(BUF *b) // KS KS_INC(KS_FREEBUF_COUNT); KS_DEC(KS_CURRENT_BUF_COUNT); - -#ifndef DONT_USE_KERNEL_STATUS -// TrackDeleteObj(POINTER_TO_UINT64(b)); -#endif // DONT_USE_KERNEL_STATUS } // Compare BUFs whether two are identical @@ -3802,7 +3758,11 @@ void Free(void *addr) // Check the memtag void CheckMemTag(MEMTAG *tag) { -#ifndef DONT_CHECK_HEAP + if (IsTrackingEnabled() == false) + { + return; + } + // Validate arguments if (tag == NULL) { @@ -3815,7 +3775,6 @@ void CheckMemTag(MEMTAG *tag) AbortExitEx("CheckMemTag: tag->Magic != MEMTAG_MAGIC"); return; } -#endif // DONT_CHECK_HEAP } // ZeroMalloc @@ -3859,9 +3818,7 @@ void *InternalMalloc(UINT size) OSSleep(MEMORY_SLEEP_TIME); } -#ifndef DONT_USE_KERNEL_STATUS TrackNewObj(POINTER_TO_UINT64(addr), "MEM", size); -#endif //DONT_USE_KERNEL_STATUS return addr; } @@ -3879,9 +3836,7 @@ void InternalFree(void *addr) KS_DEC(KS_CURRENT_MEM_COUNT); KS_INC(KS_FREE_COUNT); -#ifndef DONT_USE_KERNEL_STATUS TrackDeleteObj(POINTER_TO_UINT64(addr)); -#endif // DONT_USE_KERNEL_STATUS // Memory release OSMemoryFree(addr); @@ -3914,9 +3869,7 @@ void *InternalReAlloc(void *addr, UINT size) OSSleep(MEMORY_SLEEP_TIME); } -#ifndef DONT_USE_KERNEL_STATUS TrackChangeObjSize(POINTER_TO_UINT64(addr), size, POINTER_TO_UINT64(new_addr)); -#endif // DONT_USE_KERNEL_STATUS return new_addr; } diff --git a/src/Mayaqua/Table.c b/src/Mayaqua/Table.c index 2ac8e97a..6270ac3a 100644 --- a/src/Mayaqua/Table.c +++ b/src/Mayaqua/Table.c @@ -1071,8 +1071,6 @@ void FreeTable() return; } - TrackingDisable(); - num = LIST_NUM(TableList); tables = ToArray(TableList); for (i = 0;i < num;i++) @@ -1088,8 +1086,6 @@ void FreeTable() Free(tables); Zero(old_table_name, sizeof(old_table_name)); - - TrackingEnable(); } // Read a string table from the buffer @@ -1479,8 +1475,6 @@ bool LoadTableW(wchar_t *filename) Zero(replace_name, sizeof(replace_name)); - TrackingDisable(); - b = ReadDump("@table_name.txt"); if (b != NULL) { @@ -1500,9 +1494,5 @@ bool LoadTableW(wchar_t *filename) ret = LoadTableMain(filename); - TrackingEnable(); - return ret; } - - diff --git a/src/Mayaqua/Tracking.c b/src/Mayaqua/Tracking.c index ae0bd1f6..c846f6ec 100644 --- a/src/Mayaqua/Tracking.c +++ b/src/Mayaqua/Tracking.c @@ -127,27 +127,14 @@ static LOCK *obj_lock; static LOCK *obj_id_lock; static UINT obj_id; static LOCK *cs_lock; -static bool disable_tracking = false; static TRACKING_LIST **hashlist; static bool do_not_get_callstack; -// Enable the tracking -void TrackingEnable() -{ - disable_tracking = false; -} - -// Disable the tracking -void TrackingDisable() -{ - disable_tracking = true; -} - // Get whether the tracking is enabled bool IsTrackingEnabled() { - return !disable_tracking; + return (IsDebug() || IsMemCheck()) && kernel_status_inited; } // Memory debug menu @@ -482,14 +469,9 @@ void TrackNewObj(UINT64 addr, char *name, UINT size) return; } - if (IsMemCheck() == false) - { - // Don't track in the release mode - return; - } - - if (disable_tracking) + if ((IsTrackingEnabled() && IsMemCheck()) == false) { + // Don't track in detail if the memory check option is not specified return; } @@ -528,14 +510,9 @@ void TrackDeleteObj(UINT64 addr) return; } - if (IsMemCheck() == false) - { - // Don't track in the release mode - return; - } - - if (disable_tracking) + if ((IsTrackingEnabled() && IsMemCheck()) == false) { + // Don't track in detail if the memory check option is not specified return; } @@ -545,11 +522,7 @@ void TrackDeleteObj(UINT64 addr) if (o == NULL) { UnlockTrackingList(); - - if (IsDebug()) - { - printf("TrackDeleteObj: 0x%x is not Object!!\n", (void *)addr); - } + Debug("TrackDeleteObj(): 0x%x not found in tracking list!\n", addr); return; } DeleteTrackingList(o, true); @@ -567,14 +540,9 @@ void TrackChangeObjSize(UINT64 addr, UINT size, UINT64 new_addr) return; } - if (IsMemCheck() == false) - { - // Don't track in the release mode - return; - } - - if (disable_tracking) + if ((IsTrackingEnabled() && IsMemCheck()) == false) { + // Don't track in detail if the memory check option is not specified return; } diff --git a/src/Mayaqua/Tracking.h b/src/Mayaqua/Tracking.h index 8706c9fe..d66c84e4 100644 --- a/src/Mayaqua/Tracking.h +++ b/src/Mayaqua/Tracking.h @@ -188,10 +188,6 @@ void PrintObjectList(TRACKING_OBJECT *o); void PrintObjectInfo(TRACKING_OBJECT *o); void DebugPrintObjectInfo(UINT id); -void TrackingEnable(); -void TrackingDisable(); bool IsTrackingEnabled(); #endif // TRACKING_H - -