public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin
@ 2012-10-17 16:10 swalter at lexmark dot com
  2012-10-17 16:10 ` [Bug c/54954] " swalter at lexmark dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: swalter at lexmark dot com @ 2012-10-17 16:10 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

             Bug #: 54954
           Summary: malloc optimizations not disabled by -fno-builtin
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: swalter@lexmark.com


Consider the code:

static int foo;

void bar ()
{
    foo = 0;
    malloc();
    assert(foo);
}

foo is not reloaded after the call to malloc, even when -fno-builtin is thrown.
 This is incorrect behavior because malloc() could call back into the current
module, where foo is modified.  A full example is attached.

(Assume that malloc calls increment_count() if I can't get a second file to
attach...)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/54954] malloc optimizations not disabled by -fno-builtin
  2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
@ 2012-10-17 16:10 ` swalter at lexmark dot com
  2012-10-17 16:14 ` swalter at lexmark dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: swalter at lexmark dot com @ 2012-10-17 16:10 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

--- Comment #1 from swalter at lexmark dot com 2012-10-17 16:10:29 UTC ---
Created attachment 28462
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28462
Bug testcase


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/54954] malloc optimizations not disabled by -fno-builtin
  2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
  2012-10-17 16:10 ` [Bug c/54954] " swalter at lexmark dot com
@ 2012-10-17 16:14 ` swalter at lexmark dot com
  2012-10-18 10:12 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: swalter at lexmark dot com @ 2012-10-17 16:14 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

--- Comment #2 from swalter at lexmark dot com 2012-10-17 16:14:33 UTC ---
Created attachment 28463
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=28463
malloc implementation for test.c


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/54954] malloc optimizations not disabled by -fno-builtin
  2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
  2012-10-17 16:10 ` [Bug c/54954] " swalter at lexmark dot com
  2012-10-17 16:14 ` swalter at lexmark dot com
@ 2012-10-18 10:12 ` rguenth at gcc dot gnu.org
  2012-10-18 11:53 ` bren at ragh dot us
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-10-18 10:12 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
   Last reconfirmed|                            |2012-10-18
     Ever Confirmed|0                           |1

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> 2012-10-18 10:12:40 UTC ---
It works just fine for me.  Please make sure to really use -fno-builtin ;)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/54954] malloc optimizations not disabled by -fno-builtin
  2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
                   ` (2 preceding siblings ...)
  2012-10-18 10:12 ` rguenth at gcc dot gnu.org
@ 2012-10-18 11:53 ` bren at ragh dot us
  2012-10-18 13:41 ` swalter at lexmark dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: bren at ragh dot us @ 2012-10-18 11:53 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

Brendan Chandler <bren at ragh dot us> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bren at ragh dot us

--- Comment #4 from Brendan Chandler <bren at ragh dot us> 2012-10-18 11:53:01 UTC ---
Try compiling with -02 as well.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/54954] malloc optimizations not disabled by -fno-builtin
  2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
                   ` (3 preceding siblings ...)
  2012-10-18 11:53 ` bren at ragh dot us
@ 2012-10-18 13:41 ` swalter at lexmark dot com
  2013-11-20  0:58 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: swalter at lexmark dot com @ 2012-10-18 13:41 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

--- Comment #5 from swalter at lexmark dot com 2012-10-18 13:41:13 UTC ---
Thanks for looking into this, Richard.  I should have mentioned that you'll
need to build with optimization turned on.

Expected behavior:
test.c built with -O -fno-builtin runs to successful completion

Actual behavior:
test.c built with -O -fno-builtin aborts (failed assertion)
test.c built with -O0 does not abort


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/54954] malloc optimizations not disabled by -fno-builtin
  2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
                   ` (4 preceding siblings ...)
  2012-10-18 13:41 ` swalter at lexmark dot com
@ 2013-11-20  0:58 ` pinskia at gcc dot gnu.org
  2013-11-20 13:46 ` swalter at lexmark dot com
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-11-20  0:58 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |RESOLVED
         Resolution|---                         |INVALID

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
The header file malloc.h  (which is non-standard by the way) has the attribute
malloc on the malloc function call.

So this is invalid.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/54954] malloc optimizations not disabled by -fno-builtin
  2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
                   ` (5 preceding siblings ...)
  2013-11-20  0:58 ` pinskia at gcc dot gnu.org
@ 2013-11-20 13:46 ` swalter at lexmark dot com
  2013-11-20 18:06 ` pinskia at gcc dot gnu.org
  2013-11-21 12:05 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: swalter at lexmark dot com @ 2013-11-20 13:46 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

--- Comment #7 from swalter at lexmark dot com ---
(In reply to Andrew Pinski from comment #6)
> The header file malloc.h  (which is non-standard by the way) has the
> attribute malloc on the malloc function call.
> 
> So this is invalid.

Sorry, can you spell that out for me?  You're saying that the behavior I'm
seeing is because I'm using malloc.h for the definition of malloc(), rather
than stdlib.h?  malloc.h's definition of malloc has __attribute__((malloc)), so
that allows gcc to perform the optimization even when -fno-builtin in thrown?


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/54954] malloc optimizations not disabled by -fno-builtin
  2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
                   ` (6 preceding siblings ...)
  2013-11-20 13:46 ` swalter at lexmark dot com
@ 2013-11-20 18:06 ` pinskia at gcc dot gnu.org
  2013-11-21 12:05 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-11-20 18:06 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to swalter from comment #7)
> (In reply to Andrew Pinski from comment #6)
> > The header file malloc.h  (which is non-standard by the way) has the
> > attribute malloc on the malloc function call.
> > 
> > So this is invalid.
> 
> Sorry, can you spell that out for me?  You're saying that the behavior I'm
> seeing is because I'm using malloc.h for the definition of malloc(), rather
> than stdlib.h?  malloc.h's definition of malloc has __attribute__((malloc)),
> so that allows gcc to perform the optimization even when -fno-builtin in
> thrown?

No I am saying the definition inside both malloc.h and stdlib.h has the
attribute on it which allows gcc to perform the optimization even when
-fno-builtin is used.  My comment about malloc.h being non-standard was an
aside comment and just pointing out the use of a non-standard header.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c/54954] malloc optimizations not disabled by -fno-builtin
  2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
                   ` (7 preceding siblings ...)
  2013-11-20 18:06 ` pinskia at gcc dot gnu.org
@ 2013-11-21 12:05 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2013-11-21 12:05 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54954

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |WORKSFORME

--- Comment #9 from Richard Biener <rguenth at gcc dot gnu.org> ---
I said it works for me because it works with -fno-builtin-malloc.

Andrew, you are wrong - the malloc attribute itself does not tell us
that the malloc call may not clobber global memory.

I've massaged the testcase to build w/o warnings:

m.c:
#include <string.h>
#include <stdio.h>

void *__libc_malloc(size_t size);

void *malloc(size_t size)
{
    increment_count();
    return (void*)__libc_malloc(size);
}

t.c:
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>

static int count = 0;

void increment_count(void)
{
        count++;
}

int main (void)
{
        char *ptr;
        count = 0;
        ptr = malloc(1);
        assert(ptr);
        assert(count);
        printf("%p\n", ptr);
        free(ptr);
        return 0;
}

rguenther@murzim:/tmp> /space/rguenther/install/gcc-4.7.0/bin/gcc -o t t.c m.c
-O2 -fno-builtin-malloc
rguenther@murzim:/tmp> ./t
0x1127010
rguenther@murzim:/tmp> /space/rguenther/install/gcc-4.7.0/bin/gcc -o t t.c m.c
-O2  
rguenther@murzim:/tmp> ./t
t: t.c:18: main: Assertion `count' failed.
Aborted


Works with all GCC versions I tried.  I tried on x86_64-linux (just in case
RTL optimizers break this in which case the target architecture may be
important).


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2013-11-21 12:05 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-10-17 16:10 [Bug c/54954] New: malloc optimizations not disabled by -fno-builtin swalter at lexmark dot com
2012-10-17 16:10 ` [Bug c/54954] " swalter at lexmark dot com
2012-10-17 16:14 ` swalter at lexmark dot com
2012-10-18 10:12 ` rguenth at gcc dot gnu.org
2012-10-18 11:53 ` bren at ragh dot us
2012-10-18 13:41 ` swalter at lexmark dot com
2013-11-20  0:58 ` pinskia at gcc dot gnu.org
2013-11-20 13:46 ` swalter at lexmark dot com
2013-11-20 18:06 ` pinskia at gcc dot gnu.org
2013-11-21 12:05 ` rguenth at gcc dot gnu.org

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).