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(gameengine): align global operator new to 16 bytes on linux (#55)

Levent Arican30768aa6803a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index aa87db327..0ce943104 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@ found and fixed — EA's own, not port damage.**
- One command builds it. `./build-macos.sh` leaves a Mac app ready to open and `./build-linux.sh` a folder ready to start, and each checks your machine first and names the package to install for anything missing. On Linux it can make an AppImage, a .deb, an .rpm, an Arch package and a Flatpak, none of them carrying EA's files, and the Flatpak has played a full check match on a Steam Deck.
- One game on every machine. A replay recorded on a Mac plays back on Linux and on Windows to the same checksum, and the other way round, and a Mac and a Linux PC have played a LAN match together without once disagreeing. A Mac or Linux game could not join a Windows one: the check every machine runs before a network game failed, because the Mac and Linux copies of the game's data files ended each line differently from Windows'. They agree now. A match between Windows and a Mac has not been played yet.
- Text on the Linux menus is as wide as on Windows. Linux has no Arial, and the font standing in for it reports a wider average letter, so every menu font the game sets to a fixed width came out a quarter narrower than it should, the "Generals" lettering and the menu buttons among them. Each is measured against Arial's own figure now, and lands within one percent of Windows.
+- The game no longer crashes on some Linux PCs as its window opens. The game hands out memory its own way, and on Linux the graphics driver gets its memory from the game too. Some drivers, AMD's on Debian 12 among them, need every piece lined up on a 16-byte boundary, and the game's lined up on 4, so the start died or went through depending on where the pieces happened to fall. On Linux everything outside the game's own pools now comes from the system, which lines it up as the driver expects.
- Windows on Arm laptops, the Snapdragon ones, get a build made for them. Microsoft never shipped those machines the library the game loads its textures and shaders through, so the game brings its own, the texture code the Mac and Linux versions already run. Switched on in place of Microsoft's library on an ordinary Windows PC, it draws the same battlefields, with no tree, house or patch of ground going missing. Nobody has played it on a Snapdragon laptop yet.
- Thanks to ilyasakin, whose port this is.
diff --git a/GeneralsMD/Code/BuildFingerprint.manifest b/GeneralsMD/Code/BuildFingerprint.manifest
index 37254e0f8..fb4630444 100644
--- a/GeneralsMD/Code/BuildFingerprint.manifest
+++ b/GeneralsMD/Code/BuildFingerprint.manifest
@@ -2801,6 +2801,7 @@ Tests/test_game_results.inc
Tests/test_gameengine.cpp
Tests/test_gameengine_stubs.cpp
Tests/test_gametext_csf.cpp
+Tests/test_global_new_alignment.cpp
Tests/test_glyph_rasteriser.cpp
Tests/test_harness.h
Tests/test_install_guard.sh
diff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txt
index 2310fe40e..97e4557ac 100644
--- a/GeneralsMD/Code/CMakeLists.txt
+++ b/GeneralsMD/Code/CMakeLists.txt
@@ -1299,6 +1299,20 @@ if(MSVC)
endforeach()
endif()
set_tests_properties(gametext_csf PROPERTIES SKIP_RETURN_CODE 77)
+# The global operator new against the alignment the ABI promises for it, on Linux, where it is also the
+# one Mesa's libLLVM gets. See Tests/test_global_new_alignment.cpp.
+if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
+ add_executable(test_global_new_alignment ${CMAKE_CURRENT_SOURCE_DIR}/Tests/test_global_new_alignment.cpp
+ ${GE}/Source/Common/System/GameMemory.cpp ${GE}/Source/Common/System/MemoryInit.cpp
+ ${GE}/Source/Common/System/UnicodeString.cpp ${GE}/Source/Common/System/AsciiString.cpp
+ ${GE}/Source/Common/System/WideCharFns.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Tests/gcc_eager_vtable_stubs.cpp)
+ target_include_directories(test_global_new_alignment PRIVATE $<TARGET_PROPERTY:gameengine,INCLUDE_DIRECTORIES>)
+ target_compile_definitions(test_global_new_alignment PRIVATE $<TARGET_PROPERTY:gameengine,COMPILE_DEFINITIONS>
+ ZH_EAGER_VTABLE_STUB_NO_POOL)
+ set_target_properties(test_global_new_alignment PROPERTIES CXX_STANDARD 17)
+ target_link_libraries(test_global_new_alignment PRIVATE ${ZH_ZHIO_LIBS})
+ add_test(NAME test_global_new_alignment COMMAND test_global_new_alignment)
+endif()
if(ZH_GAME_DATA)
set_tests_properties(gametext_csf PROPERTIES ENVIRONMENT "ZH_DATA_DIR=${ZH_GAME_DATA}")
endif()
diff --git a/GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp b/GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp
index 2ef1b553c..bdf0e8806 100644
--- a/GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/Common/System/GameMemory.cpp
@@ -3353,8 +3353,19 @@ void STLSpecialAlloc::deallocate(void* __p, size_t)
its .big files. The pools stay (MemoryPoolObject classes have operators of their own), and so do the
strings, which allocate from TheDynamicMemoryAllocator by name. Without the define, as in every
normal build, this file is what it was.
+
+ Linux takes the same calloc and free in every build. There the executable's operator new is the one
+ every shared library in the process gets, and Mesa's Vulkan drivers load libLLVM, whose static
+ constructors store to their blocks with SSE instructions that need the 16 bytes the ABI promises
+ (__STDCPP_DEFAULT_NEW_ALIGNMENT__). TheDynamicMemoryAllocator's blocks are 4-byte aligned
+ (MEM_BOUND_ALIGNMENT), so the game died with SIGSEGV inside SDL_CreateGPUDevice on a Radeon Vega with
+ Pardus 23 (Debian 12, Mesa 22.3), or ran, depending on where the blocks happened to land. glibc's
+ calloc gives 16. See Tests/test_global_new_alignment.cpp.
*/
-#ifndef ZH_SANITIZER_BUILD
+#if defined(ZH_SANITIZER_BUILD) || defined(__linux__)
+#define ZH_SYSTEM_GLOBAL_NEW
+#endif
+#ifndef ZH_SYSTEM_GLOBAL_NEW
//-----------------------------------------------------------------------------
/**
overload for global operator new; send requests to TheDynamicMemoryAllocator.
@@ -3507,9 +3518,9 @@ void operator delete[](void * p, const char *, int)
TheDynamicMemoryAllocator->freeBytes(p);
}
#else
-// A sanitizer build: every form from calloc, zeroed as allocateBytes zeroes, and back to free. They count
+// A sanitizer build or Linux: every form from calloc, zeroed as allocateBytes zeroes, and back to free. They count
// in theLinkTester as the forms above do, so initMemoryManager's link test holds unchanged.
-static void *sanitizerAllocate(size_t size)
+static void *systemAllocate(size_t size)
{
++theLinkTester;
void *p = calloc(1, size != 0 ? size : 1);
@@ -3517,26 +3528,26 @@ static void *sanitizerAllocate(size_t size)
throw ERROR_OUT_OF_MEMORY;
return p;
}
-static void sanitizerFree(void *p)
+static void systemFree(void *p)
{
++theLinkTester;
free(p);
}
-void *operator new(size_t size) { return sanitizerAllocate(size); }
-void *operator new[](size_t size) { return sanitizerAllocate(size); }
-void operator delete(void *p) WW_NOEXCEPT_DELETE { sanitizerFree(p); }
-void operator delete[](void *p) WW_NOEXCEPT_DELETE { sanitizerFree(p); }
-void operator delete(void *p, size_t) WW_NOEXCEPT_DELETE { sanitizerFree(p); }
-void operator delete[](void *p, size_t) WW_NOEXCEPT_DELETE { sanitizerFree(p); }
+void *operator new(size_t size) { return systemAllocate(size); }
+void *operator new[](size_t size) { return systemAllocate(size); }
+void operator delete(void *p) WW_NOEXCEPT_DELETE { systemFree(p); }
+void operator delete[](void *p) WW_NOEXCEPT_DELETE { systemFree(p); }
+void operator delete(void *p, size_t) WW_NOEXCEPT_DELETE { systemFree(p); }
+void operator delete[](void *p, size_t) WW_NOEXCEPT_DELETE { systemFree(p); }
void *operator new(size_t size, const std::nothrow_t &) WW_NOEXCEPT_DELETE { return calloc(1, size != 0 ? size : 1); }
void *operator new[](size_t size, const std::nothrow_t &) WW_NOEXCEPT_DELETE { return calloc(1, size != 0 ? size : 1); }
void operator delete(void *p, const std::nothrow_t &) WW_NOEXCEPT_DELETE { free(p); }
void operator delete[](void *p, const std::nothrow_t &) WW_NOEXCEPT_DELETE { free(p); }
-void* operator new(size_t size, const char *, int) { return sanitizerAllocate(size); }
-void operator delete(void * p, const char *, int) { sanitizerFree(p); }
-void* operator new[](size_t size, const char *, int) { return sanitizerAllocate(size); }
-void operator delete[](void * p, const char *, int) { sanitizerFree(p); }
-#endif // ZH_SANITIZER_BUILD
+void* operator new(size_t size, const char *, int) { return systemAllocate(size); }
+void operator delete(void * p, const char *, int) { systemFree(p); }
+void* operator new[](size_t size, const char *, int) { return systemAllocate(size); }
+void operator delete[](void * p, const char *, int) { systemFree(p); }
+#endif // ZH_SYSTEM_GLOBAL_NEW
//-----------------------------------------------------------------------------
#ifdef MEMORYPOOL_OVERRIDE_MALLOC
diff --git a/GeneralsMD/Code/Tests/test_global_new_alignment.cpp b/GeneralsMD/Code/Tests/test_global_new_alignment.cpp
new file mode 100644
index 000000000..b01a18a6e
--- /dev/null
+++ b/GeneralsMD/Code/Tests/test_global_new_alignment.cpp
@@ -0,0 +1,97 @@
+/*
+** Copyright 2026 levent
+** Additional terms under GNU GPL section 7 apply: see LICENSE.md.
+**
+** This program is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation, either version 3 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/* The global operator new that GameMemory.cpp defines, against the alignment the C++ ABI promises for
+ it: __STDCPP_DEFAULT_NEW_ALIGNMENT__, 16 on x86-64 and arm64.
+
+ On Linux the executable's operator new is the one every shared library in the process gets, Mesa's
+ Vulkan drivers and the libLLVM they load among them. The memory manager's blocks are 4-byte
+ aligned (MEM_BOUND_ALIGNMENT), and libLLVM's static constructors store to what they get with movaps:
+ the game died with SIGSEGV inside SDL_CreateGPUDevice on a Radeon Vega with Pardus 23 (Debian 12,
+ Mesa 22.3), or ran, depending on where the blocks happened to land. The real GameMemory.cpp and MemoryInit.cpp, and the
+ strings they name. */
+
+#include "PreRTS.h"
+
+#include "Common/CriticalSection.h"
+#include "Common/ExecutableDirectory.h"
+#include "Common/GameMemory.h"
+#include "Common/JobSystem.h"
+
+#include <stdint.h>
+#include <stdio.h>
+#include <new>
+
+static int s_failed = 0;
+#define CHECK( x ) do { if (!(x)) { printf( "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x ); ++s_failed; } } while (0)
+
+static bool aligned( const void *p )
+{
+ return ((uintptr_t)p % __STDCPP_DEFAULT_NEW_ALIGNMENT__) == 0;
+}
+
+int main( void )
+{
+ initMemoryManager();
+
+ // Every size a small object comes in, and some past the memory manager's sub-pools.
+ static const size_t sizes[] = { 1, 2, 3, 4, 5, 7, 8, 12, 16, 20, 24, 28, 32, 40, 48, 64, 100, 128,
+ 256, 1000, 4096, 100000 };
+ enum { COUNT = sizeof(sizes) / sizeof(sizes[0]) };
+ void *single[COUNT];
+ void *array[COUNT];
+ for (int i = 0; i < COUNT; ++i)
+ {
+ single[i] = ::operator new( sizes[i] );
+ array[i] = ::operator new[]( sizes[i] );
+ void *nothrow = ::operator new( sizes[i], std::nothrow );
+ CHECK( aligned( single[i] ) );
+ CHECK( aligned( array[i] ) );
+ CHECK( aligned( nothrow ) );
+ ::operator delete( nothrow );
+ }
+ for (int i = 0; i < COUNT; ++i)
+ {
+ ::operator delete( single[i] );
+ ::operator delete[]( array[i] );
+ }
+
+ printf( "%s (%d failed)\n", s_failed ? "FAILED" : "OK", s_failed );
+ return s_failed ? 1 : 0;
+}
+
+// What GameMemory.cpp, MemoryInit.cpp and the strings name that this test does not build.
+CriticalSection *TheUnicodeStringCriticalSection = NULL; // ScopedCriticalSection skips a null one
+CriticalSection *TheDmaCriticalSection = NULL;
+CriticalSection *TheMemoryPoolCriticalSection = NULL;
+Bool JobSystem::isWorkerThread() { return FALSE; }
+void JobSystem::noteWorkerAllocation() {}
+void getExecutableDirectory( char *buf, size_t size, Bool ) { if (size > 0) buf[0] = 0; } // no pool-size override
+#ifdef ALLOW_DEBUG_UTILS
+DEBUG_EXTERN_C void DebugInit( int ) {}
+#endif
+#ifdef DEBUG_LOGGING
+#include <stdarg.h>
+DEBUG_EXTERN_C void DebugLog( const char *format, ... )
+{
+ va_list args;
+ va_start( args, format );
+ vfprintf( stderr, format, args );
+ va_end( args );
+}
+#endif