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(wwlib): read physical memory through GlobalMemoryStatusEx

Olcay Seyganac91fee29bba
diff --git a/GeneralsMD/Code/CMakeLists.txt b/GeneralsMD/Code/CMakeLists.txt
index c8a790e03..0e0df3070 100644
--- a/GeneralsMD/Code/CMakeLists.txt
+++ b/GeneralsMD/Code/CMakeLists.txt
@@ -544,6 +544,11 @@ target_include_directories(test_wwmath PRIVATE ${WW}/WWLib ${WW}/WWSaveLoad ${WW
add_lib_test(test_wwlib wwlib wwmath wwsaveload wwdebug)
target_include_directories(test_wwlib PRIVATE ${WW}/WWLib ${WW}/WWMath ${WW}/WWSaveLoad ${WW}/Wwutil)
+# generals.exe is linked /LARGEADDRESSAWARE, and that flag changes what
+# GlobalMemoryStatus saturates its 32-bit fields to (0xFFFFFFFF, not 0x7FFFFFFF).
+# cpudetect_memory_fits_in_the_signed_int_its_callers_use only means anything in a
+# binary that sees the same value the game does.
+target_link_options(test_wwlib PRIVATE /LARGEADDRESSAWARE)
add_lib_test(test_wwsaveload wwsaveload wwlib wwmath wwdebug)
target_include_directories(test_wwsaveload PRIVATE ${WW}/WWSaveLoad ${WW}/WWLib ${WW}/WWMath ${WW}/WWDebug ${WW}/Wwutil)
diff --git a/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/cpudetect.cpp b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/cpudetect.cpp
index 8c8053085..2da2c1523 100644
--- a/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/cpudetect.cpp
+++ b/GeneralsMD/Code/Libraries/Source/WWVegas/WWLib/cpudetect.cpp
@@ -897,19 +897,47 @@ void CPUDetectClass::Init_Processor_Features()
}
}
+#ifdef WIN32
+/*
+ Clamp a 64-bit byte count into the 32-bit field the callers read, at the *signed* limit. Every
+ consumer of these numbers in this tree hands them to something that holds an Int:
+ W3DShaderManager::testMinimumRequirements writes Get_Total_Physical_Memory() into an
+ Int *numRAM. Saturating at 0x7FFFFFFF is what plain GlobalMemoryStatus already did, so the
+ value a caller sees on a 32GB machine does not change.
+*/
+static unsigned clamp_to_signed_max(DWORDLONG bytes)
+{
+ const DWORDLONG limit = 0x7FFFFFFFui64;
+ return (unsigned)(bytes > limit ? limit : bytes);
+}
+#endif
+
void CPUDetectClass::Init_Memory()
{
#ifdef WIN32
- MEMORYSTATUS mem;
- GlobalMemoryStatus(&mem);
-
- TotalPhysicalMemory = mem.dwTotalPhys;
- AvailablePhysicalMemory = mem.dwAvailPhys;
- TotalPageMemory = mem.dwTotalPageFile;
- AvailablePageMemory = mem.dwAvailPageFile;
- TotalVirtualMemory = mem.dwTotalVirtual;
- AvailableVirtualMemory = mem.dwAvailVirtual;
+ /*
+ GlobalMemoryStatus saturates whatever does not fit in its 32-bit fields, and *what* it
+ saturates to depends on the exe header: a plain 32-bit process gets 0x7FFFFFFF, one linked
+ /LARGEADDRESSAWARE gets 0xFFFFFFFF. Read back into an Int that is -1, and the machine with
+ the most RAM becomes the one that fails the memory test: GameLODManager::init divides the
+ number by 256MB to set m_memPassed, so a negative value turns off the shell map, turns off
+ trees and forces low-resolution textures on the best hardware in the room. Measured on this
+ machine, same source, one exe each way: 0x7FFFFFFF without the flag, 0xFFFFFFFF with it.
+
+ GlobalMemoryStatusEx reports the real 64-bit counts, so do the saturation here where the
+ width the callers can hold is known.
+ */
+ MEMORYSTATUSEX mem;
+ mem.dwLength = sizeof(mem);
+ GlobalMemoryStatusEx(&mem);
+
+ TotalPhysicalMemory = clamp_to_signed_max(mem.ullTotalPhys);
+ AvailablePhysicalMemory = clamp_to_signed_max(mem.ullAvailPhys);
+ TotalPageMemory = clamp_to_signed_max(mem.ullTotalPageFile);
+ AvailablePageMemory = clamp_to_signed_max(mem.ullAvailPageFile);
+ TotalVirtualMemory = clamp_to_signed_max(mem.ullTotalVirtual);
+ AvailableVirtualMemory = clamp_to_signed_max(mem.ullAvailVirtual);
#elif defined(_UNIX)
#warning FIX Init_Memory()
#endif
diff --git a/GeneralsMD/Code/Tests/test_wwlib.cpp b/GeneralsMD/Code/Tests/test_wwlib.cpp
index dcff44da1..448e63d24 100644
--- a/GeneralsMD/Code/Tests/test_wwlib.cpp
+++ b/GeneralsMD/Code/Tests/test_wwlib.cpp
@@ -1280,6 +1280,27 @@ TEST(cpudetect_reports_something_sane)
CHECK(CPUDetectClass::Get_Processor_String()[0] != 0);
}
+TEST(cpudetect_memory_fits_in_the_signed_int_its_callers_use)
+{
+ /* Init_Memory used to take GlobalMemoryStatus' 32-bit fields straight. That call saturates,
+ and /LARGEADDRESSAWARE changes what it saturates to - 0xFFFFFFFF instead of 0x7FFFFFFF - so
+ the number arrived as -1 in W3DShaderManager::testMinimumRequirements' Int *numRAM.
+ GameLODManager::init then read a machine with 32GB as a machine below 256MB, and turned off
+ the shell map, the trees and full-size textures. This binary is linked
+ /LARGEADDRESSAWARE for the same reason generals.exe is, so it sees what the game sees. */
+ unsigned totalPhys = CPUDetectClass::Get_Total_Physical_Memory();
+ CHECK(totalPhys > 0);
+ CHECK((int)totalPhys > 0);
+ CHECK((int)CPUDetectClass::Get_Available_Physical_Memory() >= 0);
+ CHECK((int)CPUDetectClass::Get_Total_Page_File_Size() > 0);
+ CHECK((int)CPUDetectClass::Get_Available_Page_File_Size() >= 0);
+
+ /* The arithmetic GameLODManager::init does with it: >= PROFILE_ERROR_LIMIT, 0.94, means "has
+ at least 256MB", and its result is m_memPassed. */
+ float ratio = (float)((int)totalPhys) / (float)(256 * 1024 * 1024);
+ CHECK(ratio >= 0.94f);
+}
+
TEST(cpudetect_logs_are_printable)
{
/* Regression for the os_info bug: Get_OS_Info used to leave the struct