[prev in list] [next in list] [prev in thread] [next in thread]
List: openjdk-hotspot-runtime-dev
Subject: Re: RFR: 8281213: Unsafe uses of long and size_t in MemReporterBase::diff_in_current_scale [v3]
From: Kim Barrett <kbarrett () openjdk ! org>
Date: 2022-12-28 0:13:51
Message-ID: CpXrRCUwKZQCz44dzLpAAII7LGyK26szRPbF07sNhak=.ee866fc5-dab1-4988-adc0-267b960c9acc () github ! com
[Download RAW message or body]
On Sat, 24 Dec 2022 06:33:19 GMT, Kim Barrett <kbarrett@openjdk.org> wrote:
> > Afshin Zafari has updated the pull request incrementally with one additional \
> > commit since the last revision:
> > 8281213: Unsafe uses of long and size_t in MemReporterBase::diff_in_current_scale
>
> src/hotspot/share/services/memReporter.hpp line 80:
>
> > 78:
> > 79: size_t amount = s1 - s2;
> > 80: assert(amount <= SIZE_MAX - _scale / 2, "size_t overflow");
>
> This assert can fail for sufficiently large `amount`, even though the desired
> value is computable. The value we want is
>
> size_t scaled = (amount + _scale/2) / _scale;
>
> rounded to nearest, without risk of overflow. We can split `amount` into `p + q`,
> where `q = amount % _scale` and `p = amount - q` (which is also `(amount / _scale) \
> * _scale`). Then use
>
> size_t scaled = (p + q + _scale/2) / _scale;
>
> =>
>
> size_t scaled = (p / _scale) + ((q + _scale/2) / _scale);
>
> The lefthand side of the addition is exact.
> The righthand side is 0 if `q <= (_scale - 1)/2`, else 1. (The -1 is to
> account for odd _scale values.) So
>
> size_t scaled = (amount / _scale);
> if ((amount % _scale) <= (_scale - 1)/2) {
> scaled += 1;
> }
And after all that I wrote the conditional backward. That should be `(amount % \
_scale) > (_scale - 1)/2`.
-------------
PR: https://git.openjdk.org/jdk/pull/11514
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic