public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Shane Holden <dpejesh@yahoo.com>
To: gcc@gcc.gnu.org
Subject: __builtin_(add|mul)_overflow methods generate duplicate operations if both operands are const
Date: Sun, 12 Feb 2023 15:45:50 -0500	[thread overview]
Message-ID: <2515add9-9834-03ed-3156-fb3694149a0f@yahoo.com> (raw)
In-Reply-To: <2515add9-9834-03ed-3156-fb3694149a0f.ref@yahoo.com>

I requested a bugzilla account a few days ago and haven't heard
back so I'm just going to report this here.

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

bool add(uint8_t *r, const uint8_t *a, const uint8_t *b) {
   return __builtin_add_overflow(*a, *b, r);
}

bool mul(uint8_t *r, const uint8_t *a, const uint8_t *b) {
   return __builtin_mul_overflow(*a, *b, r);
}

int main() {
   uint8_t x;

   /* 64 + 64 should not overflow */
   x = 64;
   if (add(&x, &x, &x))
     printf("false positive: x=%i\n", x);

   /* 4 * 4 should not overflow */
   x = 4;
   if (mul(&x, &x, &x))
     printf("false positive: x=%i\n", x);

   /* 128 + 128 should overflow */
   x = 128;
   if (!add(&x, &x, &x))
     printf("false negative: x=%i\n", x);

   /* 16 * 16 should overflow */
   x = 16;
   if (!mul(&x, &x, &x))
     printf("false negative: x=%i\n", x);

   return 0;
}

$ gcc -g3 -O1 -o test test.c; ./test
false positive: x=128
false positive: x=16
false negative: x=0
false negative: x=0

The generated assembly correctly adds a with b and stores in r but
then it does the operation again before testing for carry and
returning.  If r is also one of the operands then the erroneous
second operation will use the computed value of r in place of the
original operand leading to an incorrect overflow result.

0000000000400895 <add>:
;   return __builtin_add_overflow(*a, *b, r);
   400895: 0f b6 02                      movzbl  (%rdx), %eax
   400898: 02 06                         addb    (%rsi), %al
   40089a: 88 07                         movb    %al, (%rdi)
   40089c: 0f b6 02                      movzbl  (%rdx), %eax  <<
   40089f: 02 06                         addb    (%rsi), %al   <<
   4008a1: 0f 92 c0                      setb    %al
; }
   4008a4: c3                            retq

00000000004008a5 <mul>:
;   return __builtin_mul_overflow(*a, *b, r);
   4008a5: 0f b6 06                      movzbl  (%rsi), %eax
   4008a8: f6 22                         mulb    (%rdx)
   4008aa: 88 07                         movb    %al, (%rdi)
   4008ac: 0f b6 06                      movzbl  (%rsi), %eax  <<
   4008af: f6 22                         mulb    (%rdx)        <<
   4008b1: 0f 90 c0                      seto    %al
; }
   4008b4: c3                            retq

This only seems to be triggered when both a *and* b are const.
Removing const from either or both operands generates the correct
assembly.

0000000000400855 <add>:
;   return __builtin_add_overflow(*a, *b, r);
   400855: 0f b6 02                      movzbl  (%rdx), %eax
   400858: 02 06                         addb    (%rsi), %al
   40085a: 88 07                         movb    %al, (%rdi)
   40085c: 0f 92 c0                      setb    %al
; }
   40085f: c3                            retq

0000000000400860 <mul>:
;   return __builtin_mul_overflow(*a, *b, r);
   400860: 0f b6 06                      movzbl  (%rsi), %eax
   400863: f6 22                         mulb    (%rdx)
   400865: 88 07                         movb    %al, (%rdi)
   400867: 0f 90 c0                      seto    %al
; }
   40086a: c3                            retq

Tested on the following systems:

$ uname -srm; gcc12 --version
FreeBSD 13.1-RELEASE-p2 amd64
gcc12 (FreeBSD Ports Collection) 12.2.0

$ uname -smr; gcc --version
Linux 6.1.2-1-MANJARO-ARM aarch64
gcc (GCC) 12.1.0

$ uname -srm; gcc --version
MINGW64_NT-10.0-22000 3.4.5.x86_64 x86_64
gcc.exe (Rev10, Built by MSYS2 project) 12.2.0

Poking godbolt indicates this was likely introduced in 9.4.

       reply	other threads:[~2023-02-12 20:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <2515add9-9834-03ed-3156-fb3694149a0f.ref@yahoo.com>
2023-02-12 20:45 ` David Shane Holden [this message]
2023-02-14 12:58   ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2515add9-9834-03ed-3156-fb3694149a0f@yahoo.com \
    --to=dpejesh@yahoo.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).