https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99314

            Bug ID: 99314
           Summary: [Patch] [RISC-V] g++.dg/opt/memcpy1.C
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: sinan.lin at aalto dot fi
  Target Milestone: ---

Created attachment 50272
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50272&action=edit
Patch for fixing negative length problem in riscv_expand_block_move

The problem was that the argument 'length' in function
'riscv_expand_block_move' can be a negative value, and this leads to a crash.

original code in riscv.c:

bool
riscv_expand_block_move (rtx dest, rtx src, rtx length)
{
  if (CONST_INT_P (length))
    {
      HOST_WIDE_INT factor, align;

      align = MIN (MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), BITS_PER_WORD);
      factor = BITS_PER_WORD / align;
      ... ...

Bug shooting step in gdb for g++.dg/opt/memcpy1.C:

gdb$ break riscv_expand_block_move
gdb$ r
gdb$ print length->u.hwint[0] # INTVAL(length)
gdb$ $1 = -4

My patch for this testcase:

diff --git a/gcc/config/riscv/riscv.c b/gcc/config/riscv/riscv.c
index d489717b2a5..737902faea8 100644
--- a/gcc/config/riscv/riscv.c
+++ b/gcc/config/riscv/riscv.c
@@ -3234,7 +3234,7 @@ riscv_block_move_loop (rtx dest, rtx src, HOST_WIDE_INT
length,
 bool
 riscv_expand_block_move (rtx dest, rtx src, rtx length)
 {
-  if (CONST_INT_P (length))
+  if (CONST_INT_P (length) && INTVAL (length) >= 0)
     {
       HOST_WIDE_INT factor, align;


Result of regression test before my patch on riscv-gcc-10.2.0:

/home/open/riscv-gnu-toolchain/scripts/testsuite-filter gcc newlib
/home/open/riscv-gnu-toolchain/test/allowlist `find
build-gcc-newlib-stage2/gcc/testsuite/ -name *.sum |paste -sd "," -`
                === g++: Unexpected fails for rv32imac ilp32 medlow ===
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (test for excess errors)
                === g++: Unexpected fails for rv32im ilp32 medlow ===
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (test for excess errors)
                === g++: Unexpected fails for rv32imafc ilp32f medlow ===
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (test for excess errors)
                === g++: Unexpected fails for rv32iac ilp32 medlow ===
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (test for excess errors)
                === g++: Unexpected fails for rv32i ilp32 medlow ===
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++14 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++17 (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++2a (test for excess errors)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (internal compiler error)
FAIL: g++.dg/opt/memcpy1.C  -std=gnu++98 (test for excess errors)

               ========= Summary of gcc testsuite =========
                            | # of unexpected case / # of unique unexpected
case
                            |          gcc |          g++ |     gfortran |
      rv32i/  ilp32/ medlow |    0 /     0 |    8 /     1 |      - |
    rv32iac/  ilp32/ medlow |    0 /     0 |    8 /     1 |      - |
   rv32imac/  ilp32/ medlow |    0 /     0 |    8 /     1 |      - |
     rv32im/  ilp32/ medlow |    0 /     0 |    8 /     1 |      - |
 rv64imafdc/  lp64d/ medlow |    0 /     0 |    0 /     0 |      - |
  rv32imafc/ ilp32f/ medlow |    0 /     0 |    8 /     1 |      - |
   rv64imac/   lp64/ medlow |    0 /     0 |    0 /     0 |      - |

Result of regression test after my patch on riscv-gcc-10.2.0:

/home/open/riscv-gnu-toolchain/scripts/testsuite-filter gcc newlib
/home/open/riscv-gnu-toolchain/test/allowlist `find
build-gcc-newlib-stage2/gcc/testsuite/ -name *.sum |paste -sd "," -`

               ========= Summary of gcc testsuite =========
                            | # of unexpected case / # of unique unexpected
case
                            |          gcc |          g++ |     gfortran |
      rv32i/  ilp32/ medlow |    0 /     0 |    0 /     0 |      - |
    rv32iac/  ilp32/ medlow |    0 /     0 |    0 /     0 |      - |
   rv32imac/  ilp32/ medlow |    0 /     0 |    0 /     0 |      - |
     rv32im/  ilp32/ medlow |    0 /     0 |    0 /     0 |      - |
 rv64imafdc/  lp64d/ medlow |    0 /     0 |    0 /     0 |      - |
  rv32imafc/ ilp32f/ medlow |    0 /     0 |    0 /     0 |      - |
   rv64imac/   lp64/ medlow |    0 /     0 |    0 /     0 |      - |


The attachment is my patch.

Sinan Lin
PLCT Lab
si...@isrc.iscas.ac.cn

Reply via email to