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:51:06 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.
Would it be useful to instead use something new like left_shift_no_overflow()? It would assert if the operation is not reversible because of overflow, and I believe it could be implemented efficiently with val * (1<<shift), which is no longer UB and detected by UBSAN but allows the C++ compiler to still optimize the multiply to use left shift when the shift value is constant power of 2, like it is here.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/24196#issuecomment-2749995261
More information about the hotspot-compiler-dev
mailing list