RFR: 8367485: os::physical_memory is broken in 32-bit JVMs when running on 64-bit OSes [v2]
Stefan Karlsson
stefank at openjdk.org
Thu Sep 18 13:21:59 UTC 2025
On Thu, 18 Sep 2025 12:51:39 GMT, Anton Artemov <duke at openjdk.org> wrote:
>> Hi, please consider the following changes:
>>
>> In this PR we address the overflow issue in `os::physical_memory()` on Linux, which can occur when running a 32-bit JVM on a 64-bit machine, introduced by https://bugs.openjdk.org/browse/JDK-8357086. The problem is that the product of _SC_PHYS_PAGES and _SC_PAGESIZE can overflow according to the documentation.
>>
>> The issue is addressed by changing the output type of all related functions to `uint64_t`.
>>
>> Tested in tiers 1 - 5.
>
> Anton Artemov has updated the pull request incrementally with one additional commit since the last revision:
>
> 8367485: Refactoring with uint64_t
src/hotspot/os/linux/os_linux.cpp line 306:
> 304: return false;
> 305: }
> 306: value = static_cast<uint64_t>(si.totalswap * si.mem_unit);
Could the `si.totalswap * si.mem_unit` calculation overflow on 32-bit JVMs? I wonder if it would be safer to write the code this way:
static_cast<uint64_t>(si.totalswap) * si.mem_unit;
The same comment goes for si.freeswap below.
src/hotspot/os/linux/os_linux.cpp line 2572:
> 2570: struct sysinfo si;
> 2571: sysinfo(&si);
> 2572: uint64_t phys_mem = physical_memory();
You need to update the two `%zu` below.
src/hotspot/os/windows/os_windows.cpp line 3967:
> 3965: // Processor level is not available on non-NT systems, use vm_version instead
> 3966: int os::win32::_processor_level = 0;
> 3967: uint64_t os::win32::_physical_memory = 0;
The indentation is misaligned now.
src/hotspot/os/windows/os_windows.hpp line 43:
> 41: static int _processor_type;
> 42: static int _processor_level;
> 43: static uint64_t _physical_memory;
Indentation.
src/hotspot/share/gc/z/zLargePages.cpp line 34:
> 32: pd_initialize();
> 33:
> 34: const uint64_t memory = os::physical_memory();
PROPERFMT below uses `%zu`. Given that ZGC is only compiled to 64-bits maybe you could make the memory variable size_t here. I don't know if you need an explicit cast or not.
src/hotspot/share/jfr/periodic/jfrPeriodic.cpp line 534:
> 532: EventPhysicalMemory event;
> 533: event.set_totalSize(totalPhysicalMemory);
> 534: uint64_t avail_mem = 0;
I think you can remove all usages of u8 in this function.
src/hotspot/share/runtime/os.cpp line 1948:
> 1946: /* Is this a server class machine? */
> 1947: if ((os::active_processor_count() >= (int)server_processors) &&
> 1948: (phys_mem >= static_cast<uint64_t>(server_memory - missing_memory))) {
You don't need the cast if you change the type of `server_memory` and `missing_memory` to uint64_t instead.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/27335#discussion_r2359179008
PR Review Comment: https://git.openjdk.org/jdk/pull/27335#discussion_r2359191538
PR Review Comment: https://git.openjdk.org/jdk/pull/27335#discussion_r2359195029
PR Review Comment: https://git.openjdk.org/jdk/pull/27335#discussion_r2359196770
PR Review Comment: https://git.openjdk.org/jdk/pull/27335#discussion_r2359215587
PR Review Comment: https://git.openjdk.org/jdk/pull/27335#discussion_r2359221762
PR Review Comment: https://git.openjdk.org/jdk/pull/27335#discussion_r2359258332
More information about the hotspot-runtime-dev
mailing list