JDK-8281213 : Unsafe uses of long and size_t in MemReporterBase::diff_in_current_scale
  • Type: Bug
  • Component: hotspot
  • Sub-Component: runtime
  • Affected Version: 19,20,21
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2022-02-03
  • Updated: 2025-06-13
  • Resolved: 2023-01-20
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 21
21 b07Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
MemReporterBase::diff_in_current_scale is defined as follows:

  inline long diff_in_current_scale(size_t s1, size_t s2) const {
    long amount = (long)(s1 - s2);
    long scale = (long)_scale;
    amount = (amount > 0) ? (amount + scale / 2) : (amount - scale / 2);
    return amount / scale;
  }

Long and size_t can have different sizes: 4 bytes and 8 bytes (LLP64). The result of 's1 - s2' might not fit into long. It might not fit into int64_t. For example: s1 is SIZE_MAX and s2 is SIZE_MAX-MAX_INT64-1.

We should calculate diff as size_t and convert a result to long. Assertions must be added to check the diff fits into long.
Comments
A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/11514 Date: 2022-12-05 13:54:57 +0000
13-06-2025

Changeset: 26410c18 Author: Afshin Zafari <afshin.zafari@oracle.com> Committer: Evgeny Astigeevich <eastigeevich@openjdk.org> Date: 2023-01-20 10:17:07 +0000 URL: https://git.openjdk.org/jdk/commit/26410c180b88b4342217fdad63f1221786d8c37b
20-01-2023

ILW = MLM = P4
08-02-2022

Removing the use of 'long' from hotspot code is it's own task, so I think this is fine as a separate RFE.
04-02-2022

Should this become a sub-task of JDK-8135181?
03-02-2022

This looks like a duplicate of https://bugs.openjdk.java.net/browse/JDK-8135181
03-02-2022