]> git.ipfire.org Git - thirdparty/gcc.git/commit
rs6000: Avoid undefined behavior caused by overflow and invalid shifts
authorKishan Parmar <kishan@linux.ibm.com>
Thu, 31 Jul 2025 11:35:02 +0000 (17:05 +0530)
committerKishan Parmar <kishan@linux.ibm.com>
Thu, 31 Jul 2025 11:35:02 +0000 (17:05 +0530)
commit647bd0a02789f17ab6e092fff60839d28fd635dc
tree6872ef7033d06da5799df2e6614922d0f9a54312
parent51c03570b3e65029fd4ac5d5ace5bddadec7ba9b
rs6000: Avoid undefined behavior caused by overflow and invalid shifts

While building GCC with --with-build-config=bootstrap-ubsan on
powerpc64le-unknown-linux-gnu, multiple UBSAN runtime errors were
encountered in rs6000.cc and rs6000.md due to undefined behavior
involving left shifts on negative values and shift exponents equal to
or exceeding the type width.

The issue was in bit pattern recognition code
(in can_be_rotated_to_negative_lis and can_be_built_by_li_and_rldic),
where signed values were shifted without handling negative inputs or
guarding against shift counts equal to the type width, causing UB.
The fix ensures shifts and rotations are done unsigned HOST_WIDE_INT,
and casting back only where needed (like for arithmetic right shifts)
with proper guards to prevent shift-by-64.

2025-07-31  Kishan Parmar  <kishan@linux.ibm.com>

gcc:
PR target/118890
* config/rs6000/rs6000.cc (can_be_rotated_to_negative_lis): Avoid left
shift of negative value and guard shift count.
(can_be_built_by_li_and_rldic): Likewise.
(rs6000_emit_set_long_const): Likewise.
* config/rs6000/rs6000.md (splitter for plus into two 16-bit parts): Fix
UB from overflow in addition.
gcc/config/rs6000/rs6000.cc
gcc/config/rs6000/rs6000.md