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.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
fix(gameengine): stop two inline asm blocks clobbering EBX/ESI/EDI
Olcay Seygan0346934c4ef0
diff --git a/GeneralsMD/Code/GameEngine/Include/Common/crc.h b/GeneralsMD/Code/GameEngine/Include/Common/crc.hindex 145097505..861941e6a 100644--- a/GeneralsMD/Code/GameEngine/Include/Common/crc.h+++ b/GeneralsMD/Code/GameEngine/Include/Common/crc.h@@ -87,8 +87,15 @@ public:// ASM version, verified by comparing resulting data with C++ version dataunsigned *crcPtr=&crc;+ // Same rule as fast_float_trunc in BaseType.h: EBX, ESI and EDI belong to the+ // caller, and this block uses all three. Nothing has crashed on it yet, but that+ // is luck -- whichever of them the compiler happens to hold a live value in is+ // gone when the block ends._asm{+ push ebx+ push esi+ push edimov esi,[buf]mov ecx,[len]dec ecx@@ -103,6 +110,9 @@ public:dec ecxjns lpmov dword ptr [edi],ebx+ pop edi+ pop esi+ pop ebx};}diff --git a/GeneralsMD/Code/Libraries/Include/Lib/BaseType.h b/GeneralsMD/Code/Libraries/Include/Lib/BaseType.hindex 6332778b7..79c1183ae 100644--- a/GeneralsMD/Code/Libraries/Include/Lib/BaseType.h+++ b/GeneralsMD/Code/Libraries/Include/Lib/BaseType.h@@ -206,14 +206,18 @@ __forceinline long fast_float2long_round(float f)// code courtesy of Martin Hoffesommer (grin)__forceinline float fast_float_trunc(float f){+ // EDX, not EBX, for the zero: an __asm block has to leave EBX/ESI/EDI the way it+ // found them, and this one did not. Where the compiler parks the saved ESP in EBX+ // (W3DTreeBuffer::doLighting does) the epilogue's "mov esp,ebx" then set ESP to 0+ // and the next pop faulted -- the crash that ended every run at the main menu._asm{mov ecx,[f]shr ecx,23mov eax,0xff800000- xor ebx,ebx+ xor edx,edxsub cl,127- cmovc eax,ebx+ cmovc eax,edxsar eax,cland [f],eax}diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 4a0e24fd3..2d1ff4ff1 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -10,6 +10,10 @@* is not ported yet, so the test supplies its own out of LocalFile, which is* plain CRT _open/_read and does live in gameengine.*/+/* crc.h pulls winsock2.h, so it has to come before anything that drags in+ windows.h (and with it winsock.h) - otherwise ws2def.h redefines sockaddr. */+#include "Common/crc.h"+#include "test_harness.h"#include "Common/AsciiString.h"@@ -352,3 +356,74 @@ TEST(stackdump_walks_the_callers)printf( "%s\n", s_stackText );CHECK( strstr( s_stackText, "stackdump_walks_the_callers" ) != NULL );}++/* Two __asm blocks in headers everything includes wrote to registers that belong+ to the caller. fast_float_trunc's "xor ebx,ebx" is what killed every run at the+ main menu: W3DTreeBuffer::doLighting keeps its saved ESP in EBX, so the epilogue's+ "mov esp,ebx" set ESP to zero and the following pop faulted. The witnesses below+ put a sentinel in each register, run the block, and read the register back. */+static unsigned truncEbxWitness( float f )+{+ unsigned ebxOut;+ volatile float t;+ __asm mov ebx, 0x0BADF00D+ t = fast_float_trunc( f );+ __asm mov ebxOut, ebx+ (void)t;+ return ebxOut;+}++TEST(fast_float_trunc_leaves_ebx_alone)+{+ CHECK_EQ( truncEbxWitness( 3.75f ), 0x0BADF00D );+ CHECK_NEAR( fast_float_trunc( 3.75f ), 3.0f, 0.0001f );+ CHECK_NEAR( fast_float_trunc( -3.75f ), -3.0f, 0.0001f );+}++/* The length is a parameter and not a strlen() call on purpose: anything the compiler+ emits between the two blocks below is free to use these registers itself, so the+ witness only stays honest while the call is the only thing in between. */+static void crcRegisterWitness( const char *text, Int len, unsigned *ebxOut, unsigned *esiOut, unsigned *ediOut )+{+ CRC crc;+ __asm+ {+ mov ebx, 0x0BADF00D+ mov esi, 0x0BADBEEF+ mov edi, 0x0BADCAFE+ }+ crc.computeCRC( text, len );+ __asm+ {+ mov eax, ebxOut+ mov dword ptr [eax], ebx+ mov eax, esiOut+ mov dword ptr [eax], esi+ mov eax, ediOut+ mov dword ptr [eax], edi+ }+}++TEST(crc_computecrc_leaves_the_callee_saved_registers_alone)+{+ const char *text = "the quick brown fox";+ unsigned ebxOut = 0, esiOut = 0, ediOut = 0;+ crcRegisterWitness( text, (Int)strlen( text ), &ebxOut, &esiOut, &ediOut );+ CHECK_EQ( ebxOut, 0x0BADF00D );+ CHECK_EQ( esiOut, 0x0BADBEEF );+ CHECK_EQ( ediOut, 0x0BADCAFE );++ /* ...and it still computes what the C++ version in the header's comment does. */+ UnsignedInt expected = 0;+ for( const UnsignedByte *p = (const UnsignedByte *)text; *p; ++p )+ {+ UnsignedInt hibit = ( expected & 0x80000000 ) ? 1 : 0;+ expected <<= 1;+ expected += *p;+ expected += hibit;+ }++ CRC crc;+ crc.computeCRC( text, (Int)strlen( text ) );+ CHECK_EQ( crc.get(), expected );+}