Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(ww3d2): free FontCharsClass glyph buffers with scalar delete, not delete[]

Olcay Seygan1d6aac8a8fa0
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/StackDump.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/StackDump.cpp
index f7cc6f74e..78a035e53 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/StackDump.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/StackDump.cpp
@@ -160,7 +160,10 @@ void UninitSymbolInfo(void)
//*****************************************************************************
void MakeStackTrace(DWORD myeip,DWORD myesp,DWORD myebp, int skipFrames, void (*callback)(const char*))
{
-STACKFRAME stack_frame;
+// STACKFRAME/StackWalk (the 32-bit-only originals) stop after the first frame or two on a
+// current dbghelp.dll (same failure as debug_stack.cpp: ERROR_PARTIAL_COPY), which left every
+// exception dump with a one-line "stack". StackWalk64 with the matching 64-bit callbacks walks.
+STACKFRAME64 stack_frame;
BOOL b_ret = TRUE;
HANDLE thread = GetCurrentThread();
@@ -169,7 +172,7 @@ HANDLE process = GetCurrentProcess();
memset(&gsContext, 0, sizeof(CONTEXT));
gsContext.ContextFlags = CONTEXT_FULL;
-memset(&stack_frame, 0, sizeof(STACKFRAME));
+memset(&stack_frame, 0, sizeof(stack_frame));
stack_frame.AddrPC.Mode = AddrModeFlat;
stack_frame.AddrPC.Offset = myeip;
stack_frame.AddrStack.Mode = AddrModeFlat;
@@ -180,7 +183,7 @@ stack_frame.AddrFrame.Offset = myebp;
/*
if(GetThreadContext(thread, &gsContext))
{
- memset(&stack_frame, 0, sizeof(STACKFRAME));
+ memset(&stack_frame, 0, sizeof(stack_frame));
stack_frame.AddrPC.Mode = AddrModeFlat;
stack_frame.AddrPC.Offset = gsContext.Eip;
stack_frame.AddrStack.Mode = AddrModeFlat;
@@ -196,14 +199,14 @@ stack_frame.AddrFrame.Offset = myebp;
unsigned int skip = skipFrames;
while (b_ret&&skip)
{
- b_ret = StackWalk( IMAGE_FILE_MACHINE_I386,
+ b_ret = StackWalk64( IMAGE_FILE_MACHINE_I386,
process,
thread,
&stack_frame,
NULL, //&gsContext,
NULL,
- SymFunctionTableAccess,
- SymGetModuleBase,
+ SymFunctionTableAccess64,
+ SymGetModuleBase64,
NULL);
skip--;
}
@@ -212,14 +215,14 @@ stack_frame.AddrFrame.Offset = myebp;
while(b_ret&&skip)
{
- b_ret = StackWalk( IMAGE_FILE_MACHINE_I386,
+ b_ret = StackWalk64( IMAGE_FILE_MACHINE_I386,
process,
thread,
&stack_frame,
NULL, //&gsContext,
NULL,
- SymFunctionTableAccess,
- SymGetModuleBase,
+ SymFunctionTableAccess64,
+ SymGetModuleBase64,
NULL);
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp
index 1c04cfb11..3742b3757 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WW3D2/render2dsentence.cpp
@@ -1220,7 +1220,13 @@ FontCharsClass::FontCharsClass (void) :
FontCharsClass::~FontCharsClass (void)
{
while ( BufferList.Count() ) {
- delete [] BufferList[0];
+ // One FontCharsBuffer per entry (W3DNEW, class operator new from its own pool), so scalar
+ // delete. The original "delete []" bypassed the class operator delete for the global
+ // array delete: with a virtual destructor the compiler expects an array cookie in front
+ // of the object, so it freed (p - 4) through the DynamicMemoryAllocator and read a pixel
+ // of the previous buffer as the block's owning blob (0x04000000) - a NULL-pool freeBlock
+ // in every ~W3DDisplay, i.e. a silent crash on every exit from the game.
+ delete BufferList[0];
BufferList.Delete(0);
}
diff --git a/GeneralsMD/Code/Tests/test_ww3d2.cpp b/GeneralsMD/Code/Tests/test_ww3d2.cpp
index 7905839ab..c3a142f04 100644
--- a/GeneralsMD/Code/Tests/test_ww3d2.cpp
+++ b/GeneralsMD/Code/Tests/test_ww3d2.cpp
@@ -28,6 +28,7 @@
#include "vector3.h"
#include "vector4.h"
#include "quat.h"
+#include "render2dsentence.h"
/*
* W3DMPO_GLUE gives every pooled class an operator new that routes through
@@ -41,9 +42,23 @@ void *createW3DMemPool(const char * /*poolName*/, int /*allocationSize*/)
return (void *)1; /* opaque handle; the stub never dereferences it */
}
+/*
+ * Every pooled new must come back through freeFromW3DMemPool, with the very
+ * pointer it handed out. The 16-byte prefix mirrors the game's pool block
+ * header: zeroed, so a "delete []" on a pooled object takes the same path it
+ * takes in the game (MSVC reads the array cookie at p-4, sees 0 elements and
+ * frees p-4 through the *global* operator delete[] - never through the pool).
+ */
+static int theW3DPoolAllocs = 0, theW3DPoolFrees = 0, theW3DPoolBadFrees = 0;
+enum { W3D_POOL_PREFIX = 16, W3D_POOL_MAGIC = 0x5B10C5A1 };
+
void *allocateFromW3DMemPool(void * /*pool*/, int allocationSize)
{
- return ::operator new(size_t(allocationSize));
+ ++theW3DPoolAllocs;
+ char *raw = (char *)::operator new(size_t(allocationSize) + W3D_POOL_PREFIX);
+ memset(raw, 0, W3D_POOL_PREFIX);
+ *(int *)raw = W3D_POOL_MAGIC;
+ return raw + W3D_POOL_PREFIX;
}
void *allocateFromW3DMemPool(void *pool, int allocationSize, const char * /*msg*/, int /*unused*/)
@@ -53,7 +68,14 @@ void *allocateFromW3DMemPool(void *pool, int allocationSize, const char * /*msg*
void freeFromW3DMemPool(void * /*pool*/, void *p)
{
- ::operator delete(p);
+ ++theW3DPoolFrees;
+ char *raw = (char *)p - W3D_POOL_PREFIX;
+ if (*(int *)raw != W3D_POOL_MAGIC)
+ {
+ ++theW3DPoolBadFrees; /* not a pointer we handed out - leak it rather than corrupt the heap */
+ return;
+ }
+ ::operator delete(raw);
}
/*=========================================================================
@@ -1648,3 +1670,32 @@ TEST(clamp_color_leaves_the_callee_saved_registers_alone)
CHECK_NEAR(color.Z, 1.0f, 0.0001f);
CHECK_NEAR(color.W, 1.0f, 0.0001f);
}
+
+/*=========================================================================
+ render2dsentence - FontCharsClass owns its glyph buffers
+ =========================================================================*/
+
+/*
+ * FontCharsClass keeps its glyph pixels in FontCharsBuffer objects, each a
+ * W3DMPO allocated one at a time with W3DNEW (class operator new, its own
+ * pool). The destructor used to free them with "delete []": that skips the
+ * class operator delete for the global array delete, and since the class has
+ * a virtual destructor the compiler also expects an array cookie in front of
+ * the object, so it walked a garbage element count and freed (p - 4). In the
+ * game that was a NULL-pool MemoryPool::freeBlock in every ~W3DDisplay - a
+ * silent access violation on every exit. Here the pool stubs above count:
+ * with the bug the buffer never reaches freeFromW3DMemPool at all - the
+ * test binary dies with STATUS_HEAP_CORRUPTION (0xC0000374) on the p-4 free
+ * before the counters are even compared (checked by reverting the fix).
+ */
+TEST(fontchars_returns_its_glyph_buffers_to_the_pool)
+{
+ const int allocs = theW3DPoolAllocs, frees = theW3DPoolFrees;
+ FontCharsClass *font = W3DNEW FontCharsClass;
+ font->Initialize_GDI_Font("Arial", 12, false);
+ CHECK(font->Get_Char_Width(L'A') > 0); /* stores the glyph -> allocates a FontCharsBuffer */
+ font->Release_Ref();
+ CHECK(theW3DPoolAllocs - allocs >= 2); /* at least the font and one buffer */
+ CHECK_EQ(theW3DPoolFrees - frees, theW3DPoolAllocs - allocs);
+ CHECK_EQ(theW3DPoolBadFrees, 0);
+}