tilegx: Fix infinite loop in gen-mul-tables generator

Message ID 20220622173045.1253663-1-ibuclaw@gdcproject.org
State Committed
Commit c0ad48527c314a1e9354b7c26718b56ed4abc92c
Headers
Series tilegx: Fix infinite loop in gen-mul-tables generator |

Commit Message

Iain Buclaw June 22, 2022, 5:30 p.m. UTC
  Hi,

Since around GCC 10, the condition `j < (INTMAX_MAX / 10)' will get
optimized into `j != 922337203685477580', which will result in an
infinite loop for certain inputs of `j'.

This patch just copies the condition already used by the -DTILEPRO
generator code, which doesn't fall into the same trap.

OK for mainline?  OK for backporting to all open release branches?

Regards,
Iain.

---
gcc/ChangeLog:

	* config/tilepro/gen-mul-tables.cc (tilegx_emit): Adjust loop
	condition to avoid overflow.
---
 gcc/config/tilepro/gen-mul-tables.cc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Jeff Law June 22, 2022, 6:16 p.m. UTC | #1
On 6/22/2022 11:30 AM, Iain Buclaw via Gcc-patches wrote:
> Hi,
>
> Since around GCC 10, the condition `j < (INTMAX_MAX / 10)' will get
> optimized into `j != 922337203685477580', which will result in an
> infinite loop for certain inputs of `j'.
>
> This patch just copies the condition already used by the -DTILEPRO
> generator code, which doesn't fall into the same trap.
>
> OK for mainline?  OK for backporting to all open release branches?
Sure, but note the tile ports have been deprecated for a long time and 
I've got a TODO to finally remove them.

jeff
  
Iain Buclaw June 24, 2022, 6:19 p.m. UTC | #2
Excerpts from Jeff Law via Gcc-patches's message of Juni 22, 2022 8:16 pm:
> 
> 
> On 6/22/2022 11:30 AM, Iain Buclaw via Gcc-patches wrote:
>> Hi,
>>
>> Since around GCC 10, the condition `j < (INTMAX_MAX / 10)' will get
>> optimized into `j != 922337203685477580', which will result in an
>> infinite loop for certain inputs of `j'.
>>
>> This patch just copies the condition already used by the -DTILEPRO
>> generator code, which doesn't fall into the same trap.
>>
>> OK for mainline?  OK for backporting to all open release branches?
> Sure, but note the tile ports have been deprecated for a long time and 
> I've got a TODO to finally remove them.
> 

Noted, and I wouldn't mind seeing the back of this port either.

I encountered this as I occasionally I run a modified version of
config-list.mk to test the D front-end.  As the target isn't gone yet,
best to ensure it can still build, even when it is assumed broken.

Iain.
  
Jeff Law June 24, 2022, 11:43 p.m. UTC | #3
On 6/24/2022 12:19 PM, Iain Buclaw wrote:
> Excerpts from Jeff Law via Gcc-patches's message of Juni 22, 2022 8:16 pm:
>>
>> On 6/22/2022 11:30 AM, Iain Buclaw via Gcc-patches wrote:
>>> Hi,
>>>
>>> Since around GCC 10, the condition `j < (INTMAX_MAX / 10)' will get
>>> optimized into `j != 922337203685477580', which will result in an
>>> infinite loop for certain inputs of `j'.
>>>
>>> This patch just copies the condition already used by the -DTILEPRO
>>> generator code, which doesn't fall into the same trap.
>>>
>>> OK for mainline?  OK for backporting to all open release branches?
>> Sure, but note the tile ports have been deprecated for a long time and
>> I've got a TODO to finally remove them.
>>
> Noted, and I wouldn't mind seeing the back of this port either.
>
> I encountered this as I occasionally I run a modified version of
> config-list.mk to test the D front-end.  As the target isn't gone yet,
> best to ensure it can still build, even when it is assumed broken.
Understood.  I've got no objection to you going ahead and installing the 
patch if it makes your life easier...  Just wanted to make you aware if 
you were looking to do something more with that port.

jeff
  

Patch

diff --git a/gcc/config/tilepro/gen-mul-tables.cc b/gcc/config/tilepro/gen-mul-tables.cc
index 52183982f65..c125748a328 100644
--- a/gcc/config/tilepro/gen-mul-tables.cc
+++ b/gcc/config/tilepro/gen-mul-tables.cc
@@ -1192,11 +1192,11 @@  tilegx_emit (long long multiplier, int num_ops)
     long long next_pow10;
 
     while (((j * 10) < abs_multiplier)
-	   && (j < (INTMAX_MAX / 10)))
+	   && (j < (j * 10)))
       j = j * 10;
 
     prev_pow10 = j;
-    next_pow10 = (j > (INTMAX_MAX / 10)) ? 0 : j * 10;
+    next_pow10 = j * 10;
 
     if ((abs_multiplier - prev_pow10 <= 100)
 	|| (next_pow10