RFR: 8352141: UBSAN: fix the left shift of negative value in relocInfo.cpp, internal_word_Relocation::pack_data_to()

Vladimir Kozlov kvn at openjdk.org
Tue Mar 25 18:41:08 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 and is reported as 
> `runtime error: left shift of negative value -25 at relocInfo.cpp:...`
>  
> 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.

This code was present from the beginning of HotSpot development.

Note, the situation should never happened because we will "never" have 512 MB (2Gb / sizeof(oop) in 32-bit VM, less in 64-bit VM) offset in nmethod (it is internal_word relocation). With `section_width` 2 we should never hit overflow. 

The smaller change is better.  I am curious if Dean's simple  suggestion `offset * (1<< section_width)` will indeed avoid UBSAN. I am for such fix.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/24196#issuecomment-2752199381


More information about the hotspot-compiler-dev mailing list