RFR: 8352141: UBSAN: fix the left shift of negative value in relocInfo.cpp, internal_word_Relocation::pack_data_to()
Dean Long
dlong at openjdk.org
Tue Mar 25 03:21:13 UTC 2025
On Mon, 24 Mar 2025 13:18:25 GMT, Afshin Zafari <azafari at openjdk.org> wrote:
> The `offset` variable used in left-shift op can be a large number with its sign-bit set. This makes a negative value which is UB for left-shift. Using `java_left_shif()` function is the workaround to avoid UB. This function uses reinterpret_cast to cast from signed to unsigned and back.
>
> Tests:
> linux-x64-debug tier1 on a UBSAN enabled build.
I guess it's UB because the value can go from negative to positive if the sign bit is lost.
The negative offset is coming from scaled_offset(). We set the value to negative and then flip it back later. It might be worth investigating why we do this. Is it just a clever hack so we get 1 more short value, -1..-32768 vs 1..32767?
I remember looking at a similar issue before. Déjà vu?
-------------
PR Comment: https://git.openjdk.org/jdk/pull/24196#issuecomment-2749953414
More information about the hotspot-compiler-dev
mailing list