Bug 104842 - mips: signed overflow in LUI_OPERAND
Summary: mips: signed overflow in LUI_OPERAND
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL: https://gcc.gnu.org/pipermail/gcc-pat...
Keywords: patch
Depends on:
Blocks: ubsan
  Show dependency treegraph
Reported: 2022-03-08 16:54 UTC by Xi Ruoyao
Modified: 2022-03-09 03:22 UTC (History)
0 users

See Also:
Target: mips64
Known to work:
Known to fail: 12.0
Last reconfirmed:


Note You need to log in before you can comment on or make changes to this bug.
Description Xi Ruoyao 2022-03-08 16:54:24 UTC
Found this building GCC on mips64el-linux-gnuabi64 with bootstrap-ubsan (testing a patch enabling ubsan for mips64*-linux-gnu*):

../../gcc/gcc/config/mips/predicates.md:382:11: runtime error: signed integer overflow: 9223372036854775807 + 65536 cannot be represented in type 'long     int'

That line uses LUI_INT (x), which expands to LUI_OPERAND (INTVAL (x)).  LUI_OPERAND is defined as:

  (((VALUE) | 0x7fff0000) == 0x7fff0000 \
   || ((VALUE) | 0x7fff0000) + 0x10000 == 0)

Obviously this will cause a signed overflow when INTVAL (x) is, for example, the maximum value of HOST_WIDE_INT.
Comment 1 CVS Commits 2022-03-09 03:21:32 UTC
The master branch has been updated by Xi Ruoyao <xry111@gcc.gnu.org>:


commit r12-7555-g2ab70a4a5c2d5a9ffb923f13b1c3b938c60dd0f0
Author: Xi Ruoyao <xry111@mengyan1223.wang>
Date:   Wed Mar 9 01:08:58 2022 +0800

    mips: avoid signed overflow in LUI_OPERAND [PR104842]
            PR target/104842
            * config/mips/mips.h (LUI_OPERAND): Cast the input to an unsigned
            value before adding an offset.
Comment 2 Xi Ruoyao 2022-03-09 03:22:05 UTC
Fixed for trunk.