bugTiny C Compiler - Bugs: bug #66214, UB in constant folding of double...

 
 

bug #66214: UB in constant folding of double -> signed integer conversion

Submitter:  Guest0x0 <guest0x0>
Submitted:  Fri 13 Sep 2024 10:33:48 AM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  None Status:  None
Privacy:  Public Assigned to:  None
Open/Closed:  Open
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 13 Sep 2024 10:33:48 AM UTC, original submission:  

tinycc version: 0.9.28rc, commit hash 12acbf3e92e4067155da2e6242cf4cd72b1e291e

The constant folding logic for cast in `tccgen.c:gen_cast` involves undefined behavior. When converting a double to signed integer, the double is first casted to `uint64_t`, then to the desired signed integer type. But if the double is negative, its conversion to unsigned integer is UB.

This bug result in different behavior of `tcc` on x86_64 and arm64. Consider the following program:

#include <stdio.h>
int main() {
  printf("%d\n", (int)-1.0);
}

`tcc` compiled with `gcc` on linux will output `-1` on this program, while `tcc` compiled with `clang` on OSX will output `0`.

The behavior difference boil down to different behavior of x86_64's `cvttsd2si` instruction and arm64's `fcvtzu` instruction. The two behave differently on negative input.

Guest0x0 <guest0x0>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by guest0x0 (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    No changes have been made to this item

    Back to the top

    Powered by Savane 3.13-ec04.
    Corresponding source code