Bug 104380 - -D_FORTIFY_SOURCE -mabi=ieeelongdouble -std=c* wrong-code
Summary: -D_FORTIFY_SOURCE -mabi=ieeelongdouble -std=c* wrong-code
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 12.0
: P3 normal
Target Milestone: 12.0
Assignee: Jakub Jelinek
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-02-04 11:40 UTC by Jakub Jelinek
Modified: 2022-02-07 16:40 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-02-04 00:00:00


Attachments
gcc12-pr104380.patch (452 bytes, patch)
2022-02-04 11:44 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jakub Jelinek 2022-02-04 11:40:04 UTC
With -O2 -D_FORTIFY_SOURCE=2 -mabi=ieeelongdouble -std=c11 (or some other -std=c* mode), the following testcase fails on powerpc64le-linux:

#include <stdio.h>
#include <stdarg.h>

static char buf[4096];
static char gfmt[] = "%Lg";

static int __attribute__ ((noinline))
foo (char *str, const char *fmt, ...)
{
  int ret;
  va_list ap;
  va_start (ap, fmt);
  ret = vsnprintf (str, 4096, fmt, ap);
  va_end (ap);
  return ret;
}

int
main ()
{
  long double dval = 128;
  int ret = foo (buf, gfmt, dval);
  if (ret != 3 || __builtin_strcmp (buf, "128") != 0)
    __builtin_abort ();
  return 0;
}

It works without -D_FORTIFY_SOURCE*, or with the GNU standard modes (-std=gnu99 etc.).
Preprocessed and reduced it is:
extern int vsnprintf (char *__restrict __s, size_t __maxlen, const char *__restrict __format, __builtin_va_list __arg)
  __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0)));
extern int __vsnprintf_chk (char *__restrict __s, size_t __n, int __flag, size_t __slen, const char *__restrict __format, __builtin_va_list __ap)
  __attribute__ ((__nothrow__ , __leaf__));
extern __inline __attribute__ ((__always_inline__)) __attribute__ ((__gnu_inline__)) __attribute__ ((__artificial__)) int
__attribute__ ((__nothrow__ , __leaf__)) vsnprintf (char *__restrict __s, size_t __n, const char *__restrict __fmt, __builtin_va_list __ap)
{
  return __builtin___vsnprintf_chk (__s, __n, 2 - 1, __builtin_object_size (__s, 2 > 1), __fmt, __ap);
}
extern __typeof (vsnprintf) vsnprintf __asm ("__vsnprintfieee128");
extern __typeof (__vsnprintf_chk) __vsnprintf_chk __asm ("__vsnprintf_chkieee128");

static char buf[4096];
static char gfmt[] = "%Lg";

static int __attribute__ ((noinline))
foo (char *str, const char *fmt, ...)
{
  int ret;
  __builtin_va_list ap;
  __builtin_va_start (ap, fmt);
  ret = vsnprintf (str, 4096, fmt, ap);
  __builtin_va_end (ap);
  return ret;
}

int
main ()
{
  long double dval = 128;
  int ret = foo (buf, gfmt, dval);
  if (ret != 3 || __builtin_strcmp (buf, "128") != 0)
    __builtin_abort ();
  return 0;
}
Comment 1 Jakub Jelinek 2022-02-04 11:41:55 UTC
Seems for non-_FORTIFY_SOURCE rs6000.cc deals with this in:
          size_t printf_len = strlen ("printf");
          size_t scanf_len = strlen ("scanf");
              
          if (len >= printf_len
              && strcmp (name + len - printf_len, "printf") == 0)
            newname = xasprintf ("__%sieee128", name);
               
          else if (len >= scanf_len
                   && strcmp (name + len - scanf_len, "scanf") == 0)
            newname = xasprintf ("__isoc99_%sieee128", name);
but we need to deal also with printf_chk.
Comment 2 Jakub Jelinek 2022-02-04 11:44:45 UTC
Created attachment 52348 [details]
gcc12-pr104380.patch

Untested fix.
Comment 3 CVS Commits 2022-02-04 17:31:41 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:8d6fffc4bcd4afa0beb0efad4f3b95394aa15618

commit r12-7059-g8d6fffc4bcd4afa0beb0efad4f3b95394aa15618
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Fri Feb 4 18:30:59 2022 +0100

    rs6000: Fix up -D_FORTIFY_SOURCE* with -mabi=ieeelongdouble [PR104380]
    
    The following testcase FAILs when configured with
    --with-long-double-format=ieee .  Only happens in the -std=c* modes, not the
    GNU modes; while the glibc headers have __asm redirects of
    vsnprintf and __vsnprinf_chk to __vsnprintfieee128 and
    __vsnprintf_chkieee128, the vsnprintf fortification extern inline gnu_inline
    always_inline wrapper calls __builtin_vsnprintf_chk and we actually emit
    a call to __vsnprinf_chk (i.e. with IBM extended long double) instead of
    __vsnprintf_chkieee128.
    
    rs6000_mangle_decl_assembler_name already had cases for *printf and *scanf,
    so this just adds another case for *printf_chk.  *scanf_chk doesn't exist.
    __ prefixing isn't done because *printf_chk already starts with __.
    
    2022-02-04  Jakub Jelinek  <jakub@redhat.com>
    
            PR target/104380
            * config/rs6000/rs6000.cc (rs6000_mangle_decl_assembler_name): Also
            adjust mangling of __builtin*printf_chk.
    
            * gcc.dg/pr104380.c: New test.
Comment 4 Jakub Jelinek 2022-02-04 17:32:18 UTC
Fixed.
Comment 5 CVS Commits 2022-02-07 16:40:05 UTC
The master branch has been updated by Jakub Jelinek <jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:5d2a330dee37ed10daa424d33ab1bcd5727c187b

commit r12-7084-g5d2a330dee37ed10daa424d33ab1bcd5727c187b
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Feb 7 17:39:11 2022 +0100

    testsuite: Fix up testsuite/gcc.c-torture/execute/builtins/lib/chk.c for powerpc [PR104380]
    
    > > The following testcase FAILs when configured with
    > > --with-long-double-format=ieee .  Only happens in the -std=c* modes, not the
    > > GNU modes; while the glibc headers have __asm redirects of
    > > vsnprintf and __vsnprinf_chk to __vsnprintfieee128 and
    > > __vsnprintf_chkieee128, the vsnprintf fortification extern inline gnu_inline
    > > always_inline wrapper calls __builtin_vsnprintf_chk and we actually emit
    > > a call to __vsnprinf_chk (i.e. with IBM extended long double) instead of
    > > __vsnprintf_chkieee128.
    > >
    > > rs6000_mangle_decl_assembler_name already had cases for *printf and *scanf,
    > > so this just adds another case for *printf_chk.  *scanf_chk doesn't exist.
    > > __ prefixing isn't done because *printf_chk already starts with __.
    
    Unfortunately, while I've tested the testcase also with -mabi=ieeelongdouble
    by hand, the full bootstrap/regtest was on GCCFarm where glibc is too old
    to test with --with-long-double-format=ieee.
    I've done full bootstrap/regtest with that option during the weekend and
    the patch regressed:
    FAIL: gcc.c-torture/execute/builtins/snprintf-chk.c execution,  -O1
    FAIL: gcc.c-torture/execute/builtins/snprintf-chk.c execution,  -O2
    FAIL: gcc.c-torture/execute/builtins/snprintf-chk.c execution,  -O2 -flto -fno-use-linker-plugin -flto-partition=none
    FAIL: gcc.c-torture/execute/builtins/snprintf-chk.c execution,  -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects
    FAIL: gcc.c-torture/execute/builtins/snprintf-chk.c execution,  -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions
    FAIL: gcc.c-torture/execute/builtins/snprintf-chk.c execution,  -O3 -g
    FAIL: gcc.c-torture/execute/builtins/snprintf-chk.c execution,  -Og -g
    FAIL: gcc.c-torture/execute/builtins/snprintf-chk.c execution,  -Os
    FAIL: gcc.c-torture/execute/builtins/sprintf-chk.c execution,  -O1
    FAIL: gcc.c-torture/execute/builtins/sprintf-chk.c execution,  -O2
    FAIL: gcc.c-torture/execute/builtins/sprintf-chk.c execution,  -O2 -flto -fno-use-linker-plugin -flto-partition=none
    FAIL: gcc.c-torture/execute/builtins/sprintf-chk.c execution,  -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects
    FAIL: gcc.c-torture/execute/builtins/sprintf-chk.c execution,  -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions
    FAIL: gcc.c-torture/execute/builtins/sprintf-chk.c execution,  -O3 -g
    FAIL: gcc.c-torture/execute/builtins/sprintf-chk.c execution,  -Og -g
    FAIL: gcc.c-torture/execute/builtins/sprintf-chk.c execution,  -Os
    FAIL: gcc.c-torture/execute/builtins/vsnprintf-chk.c execution,  -O1
    FAIL: gcc.c-torture/execute/builtins/vsnprintf-chk.c execution,  -O2
    FAIL: gcc.c-torture/execute/builtins/vsnprintf-chk.c execution,  -O2 -flto -fno-use-linker-plugin -flto-partition=none
    FAIL: gcc.c-torture/execute/builtins/vsnprintf-chk.c execution,  -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects
    FAIL: gcc.c-torture/execute/builtins/vsnprintf-chk.c execution,  -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions
    FAIL: gcc.c-torture/execute/builtins/vsnprintf-chk.c execution,  -O3 -g
    FAIL: gcc.c-torture/execute/builtins/vsnprintf-chk.c execution,  -Og -g
    FAIL: gcc.c-torture/execute/builtins/vsnprintf-chk.c execution,  -Os
    FAIL: gcc.c-torture/execute/builtins/vsprintf-chk.c execution,  -O1
    FAIL: gcc.c-torture/execute/builtins/vsprintf-chk.c execution,  -O2
    FAIL: gcc.c-torture/execute/builtins/vsprintf-chk.c execution,  -O2 -flto -fno-use-linker-plugin -flto-partition=none
    FAIL: gcc.c-torture/execute/builtins/vsprintf-chk.c execution,  -O2 -flto -fuse-linker-plugin -fno-fat-lto-objects
    FAIL: gcc.c-torture/execute/builtins/vsprintf-chk.c execution,  -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions
    FAIL: gcc.c-torture/execute/builtins/vsprintf-chk.c execution,  -O3 -g
    FAIL: gcc.c-torture/execute/builtins/vsprintf-chk.c execution,  -Og -g
    FAIL: gcc.c-torture/execute/builtins/vsprintf-chk.c execution,  -Os
    
    The problem is that the execute/builtins/ testsuite wants to override some
    of the library functions and with the change we (correctly) call
    __*printf_chkieee128 and so lib/chk.c is no longer called but the glibc
    APIs are.
    
    2022-02-07  Jakub Jelinek  <jakub@redhat.com>
    
            PR target/104380
            * gcc.c-torture/execute/builtins/lib/chk.c (__sprintf_chkieee128,
            __vsprintf_chkieee128, __snprintf_chkieee128,
            __vsnprintf_chkieee128): New aliases to non-ieee128 suffixed functions
            for powerpc -mabi=ieeelongdouble.