[Bug c/88576] New: -fno-math-errno causes GCC to consider that malloc does not set errno

aurelien at aurel32 dot net gcc-bugzilla@gcc.gnu.org
Sat Dec 22 17:50:00 GMT 2018


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

            Bug ID: 88576
           Summary: -fno-math-errno causes GCC to consider that malloc
                    does not set errno
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aurelien at aurel32 dot net
  Target Milestone: ---

With the -fno-math-errno option, GCC optimizes-out saving and restoring errno
around a malloc call. Here is a testcase, derived from the GNU libc
string/strerror.c, to reproduce it:


typedef long unsigned int size_t;

extern char *__strerror_r (int __errnum, char *__buf, size_t __buflen);

extern void *malloc (size_t __size) __attribute__ ((__nothrow__)) __attribute__
((__malloc__));

extern __thread int __libc_errno __attribute__ ((tls_model ("initial-exec")));

static char *buf;

char *strerror (int errnum)
{
  int saved_errno;
  saved_errno = __libc_errno;
  buf = malloc (1024);
  (__libc_errno = (saved_errno));
  return __strerror_r (errnum, buf, 1024);
}


Compile with: gcc -fmath-errno -Wall -O2 -fPIC -S -c strerror.i

Without -fno-math-errno, we can see in the output assembly code that errno is
saved around the malloc call:

strerror:
.LFB0:
        .cfi_startproc
        pushq   %r12
        .cfi_def_cfa_offset 16
        .cfi_offset 12, -16
        pushq   %rbp
        .cfi_def_cfa_offset 24
        .cfi_offset 6, -24
        movl    %edi, %ebp
        movl    $1024, %edi
        pushq   %rbx
        .cfi_def_cfa_offset 32
        .cfi_offset 3, -32
        movq    __libc_errno@gottpoff(%rip), %rbx
        movl    %fs:(%rbx), %r12d
        call    malloc@PLT
        movl    %ebp, %edi
        movl    $1024, %edx
        movl    %r12d, %fs:(%rbx)
        movq    %rax, %rsi
        popq    %rbx
        .cfi_def_cfa_offset 24
        popq    %rbp
        .cfi_def_cfa_offset 16
        popq    %r12
        .cfi_def_cfa_offset 8
        jmp     __strerror_r@PLT
        .cfi_endproc


With -fno-math-errno, saving and restoring errno is optimized out:

strerror:
.LFB0:
        .cfi_startproc
        pushq   %rbx
        .cfi_def_cfa_offset 16
        .cfi_offset 3, -16
        movl    %edi, %ebx
        movl    $1024, %edi
        call    malloc@PLT
        movl    %ebx, %edi
        movl    $1024, %edx
        popq    %rbx
        .cfi_def_cfa_offset 8
        movq    %rax, %rsi
        jmp     __strerror_r@PLT
        .cfi_endproc


This is reproducible with 6.5, 7.4, 8.2 and a snapshot of trunk from
2018-12-17.


More information about the Gcc-bugs mailing list