public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* gcc 4.9.2 and above are not aware of malloc (and friends) hooks
@ 2015-06-09  4:42 Gilles Gouaillardet
  2015-06-09  4:48 ` Andrew Pinski
  0 siblings, 1 reply; 3+ messages in thread
From: Gilles Gouaillardet @ 2015-06-09  4:42 UTC (permalink / raw)
  To: gcc-bugs

[-- Attachment #1: Type: text/plain, Size: 2011 bytes --]

This is a follow-up on a discussion about OpenMPI that started at
http://www.open-mpi.org/community/lists/users/2015/06/27039.php
and continued at https://github.com/open-mpi/ompi/pull/625

The attached test program does not produce correct results with gcc
4.9.2 and gcc 5.1.0 with -O1 or greater (gcc 4.8.7 is safe)

At first glance, it seems gcc is not aware a wrapper can be added to
posix_memalign and hence posix_memalign can have some side effects
such as updating user global variables.


A workaround is to declare the global variable as volatile.

The expected output of this test program is :
global = 0
changed !
global = 1


here are the full outputs with gcc 4.9.2 and 5.1.0 and from O0 to O3

$ gcc --version ; for i in 0 1 2 3 ; do echo ; echo opt = O$i ; gcc -o
test.O$i -Wno-deprecated-declarations -O$i test.c ; ./test.O$i ; done
gcc (GCC) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


opt = O0
global = 0
changed !
global = 1

opt = O1
global = 0
global = 1

opt = O2
global = 0
global = 0

opt = O3
global = 0
global = 0


$ gcc --version ; for i in 0 1 2 3 ; do echo ; echo opt = O$i ; gcc -o
test.O$i -Wno-deprecated-declarations -O$i test.c ; ./test.O$i ; done
gcc (GCC) 5.1.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


opt = O0
global = 0
changed !
global = 1

opt = O1
global = 0
global = 0

opt = O2
global = 0
global = 0

opt = O3
global = 0
global = 0


please note the different behaviour with O1 between gcc 4.9.2 and gcc 5.1.0


Could you please comment on this issue ?
- is this a bug ?
- is this a "feature" ? (e.g. a known to be aggressive optimization
that explicitly requires the global variable is declared as volatile)


Thanks and regards,

Gilles

[-- Attachment #2: test.c --]
[-- Type: text/x-csrc, Size: 521 bytes --]

#include <stdlib.h>
#include <malloc.h>

int global;

void *hook (size_t alignment, size_t size, const void *caller);


void *hook (size_t alignment, size_t size, const void *caller) {
    global = 1;
}

int main (int argc, char *argv) {
    void * c;
    global = 0;
    printf ("global = %d\n", global);
    __memalign_hook = hook;
    if (0 == global) {
        posix_memalign(&c, 0x1000, 1);
        if (0 != global)
            printf ("changed !\n");
        printf ("global = %d\n", global);
    }
    return 0;
}

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

* Re: gcc 4.9.2 and above are not aware of malloc (and friends) hooks
  2015-06-09  4:42 gcc 4.9.2 and above are not aware of malloc (and friends) hooks Gilles Gouaillardet
@ 2015-06-09  4:48 ` Andrew Pinski
       [not found]   ` <CAAkFZ5s-k=Up53=GxK4mcaiSCx4zJM36z9c_=uOtped_25G8sQ@mail.gmail.com>
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Pinski @ 2015-06-09  4:48 UTC (permalink / raw)
  To: Gilles Gouaillardet; +Cc: GCC Bugs

On Tue, Jun 9, 2015 at 12:42 PM, Gilles Gouaillardet
<gilles.gouaillardet@gmail.com> wrote:
> This is a follow-up on a discussion about OpenMPI that started at
> http://www.open-mpi.org/community/lists/users/2015/06/27039.php
> and continued at https://github.com/open-mpi/ompi/pull/625
>
> The attached test program does not produce correct results with gcc
> 4.9.2 and gcc 5.1.0 with -O1 or greater (gcc 4.8.7 is safe)
>
> At first glance, it seems gcc is not aware a wrapper can be added to
> posix_memalign and hence posix_memalign can have some side effects
> such as updating user global variables.

mark the global variable as volatile.

Thanks,
Andrew

>
>
> A workaround is to declare the global variable as volatile.
>
> The expected output of this test program is :
> global = 0
> changed !
> global = 1
>
>
> here are the full outputs with gcc 4.9.2 and 5.1.0 and from O0 to O3
>
> $ gcc --version ; for i in 0 1 2 3 ; do echo ; echo opt = O$i ; gcc -o
> test.O$i -Wno-deprecated-declarations -O$i test.c ; ./test.O$i ; done
> gcc (GCC) 4.9.2
> Copyright (C) 2014 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions.  There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
>
> opt = O0
> global = 0
> changed !
> global = 1
>
> opt = O1
> global = 0
> global = 1
>
> opt = O2
> global = 0
> global = 0
>
> opt = O3
> global = 0
> global = 0
>
>
> $ gcc --version ; for i in 0 1 2 3 ; do echo ; echo opt = O$i ; gcc -o
> test.O$i -Wno-deprecated-declarations -O$i test.c ; ./test.O$i ; done
> gcc (GCC) 5.1.0
> Copyright (C) 2015 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions.  There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
>
> opt = O0
> global = 0
> changed !
> global = 1
>
> opt = O1
> global = 0
> global = 0
>
> opt = O2
> global = 0
> global = 0
>
> opt = O3
> global = 0
> global = 0
>
>
> please note the different behaviour with O1 between gcc 4.9.2 and gcc 5.1.0
>
>
> Could you please comment on this issue ?
> - is this a bug ?
> - is this a "feature" ? (e.g. a known to be aggressive optimization
> that explicitly requires the global variable is declared as volatile)
>
>
> Thanks and regards,
>
> Gilles


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

* Re: gcc 4.9.2 and above are not aware of malloc (and friends) hooks
       [not found]   ` <CAAkFZ5s-k=Up53=GxK4mcaiSCx4zJM36z9c_=uOtped_25G8sQ@mail.gmail.com>
@ 2015-06-16  5:18     ` Gilles Gouaillardet
  0 siblings, 0 replies; 3+ messages in thread
From: Gilles Gouaillardet @ 2015-06-16  5:18 UTC (permalink / raw)
  To: gcc-bugs

Andrew and all,

as i explained, i already marked the global variable as volatile to get it work.

my (and other Open MPI folks) question is more about gcc :
- is this a bug ? (and it will be fixed, and volatile will not be
needed in the future)
- is this a known "feature" and it will not be fixed. (gcc
deliberately makes such aggressive optimizations that are known to be
illegal in very rare cases, so marking the variable as volatile is not
a workaround but a requirement)

thanks in advance for the clarification

Gilles

On Wed, Jun 10, 2015 at 10:33 AM, Gilles Gouaillardet
<gilles.gouaillardet@gmail.com> wrote:
> Andrew and all,
>
> as i explained, i already marked the global variable as volatile to get it work.
>
> my (and other Open MPI folks) question is more about gcc :
> - is this a bug ? (and it will be fixed, and volatile will not be
> needed in the future)
> - is this a known "feature" and it will not be fixed. (gcc
> deliberately makes such aggressive optimizations that are known to be
> illegal in very rare cases, so marking the variable as volatile is not
> a workaround but a requirement)
>
> thanks in advance for the clarification
>
> Gilles
>
> On Tue, Jun 9, 2015 at 1:48 PM, Andrew Pinski <pinskia@gmail.com> wrote:
>> On Tue, Jun 9, 2015 at 12:42 PM, Gilles Gouaillardet
>> <gilles.gouaillardet@gmail.com> wrote:
>>> This is a follow-up on a discussion about OpenMPI that started at
>>> http://www.open-mpi.org/community/lists/users/2015/06/27039.php
>>> and continued at https://github.com/open-mpi/ompi/pull/625
>>>
>>> The attached test program does not produce correct results with gcc
>>> 4.9.2 and gcc 5.1.0 with -O1 or greater (gcc 4.8.7 is safe)
>>>
>>> At first glance, it seems gcc is not aware a wrapper can be added to
>>> posix_memalign and hence posix_memalign can have some side effects
>>> such as updating user global variables.
>>
>> mark the global variable as volatile.
>>
>> Thanks,
>> Andrew
>>
>>>
>>>
>>> A workaround is to declare the global variable as volatile.
>>>
>>> The expected output of this test program is :
>>> global = 0
>>> changed !
>>> global = 1
>>>
>>>
>>> here are the full outputs with gcc 4.9.2 and 5.1.0 and from O0 to O3
>>>
>>> $ gcc --version ; for i in 0 1 2 3 ; do echo ; echo opt = O$i ; gcc -o
>>> test.O$i -Wno-deprecated-declarations -O$i test.c ; ./test.O$i ; done
>>> gcc (GCC) 4.9.2
>>> Copyright (C) 2014 Free Software Foundation, Inc.
>>> This is free software; see the source for copying conditions.  There is NO
>>> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>>>
>>>
>>> opt = O0
>>> global = 0
>>> changed !
>>> global = 1
>>>
>>> opt = O1
>>> global = 0
>>> global = 1
>>>
>>> opt = O2
>>> global = 0
>>> global = 0
>>>
>>> opt = O3
>>> global = 0
>>> global = 0
>>>
>>>
>>> $ gcc --version ; for i in 0 1 2 3 ; do echo ; echo opt = O$i ; gcc -o
>>> test.O$i -Wno-deprecated-declarations -O$i test.c ; ./test.O$i ; done
>>> gcc (GCC) 5.1.0
>>> Copyright (C) 2015 Free Software Foundation, Inc.
>>> This is free software; see the source for copying conditions.  There is NO
>>> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>>>
>>>
>>> opt = O0
>>> global = 0
>>> changed !
>>> global = 1
>>>
>>> opt = O1
>>> global = 0
>>> global = 0
>>>
>>> opt = O2
>>> global = 0
>>> global = 0
>>>
>>> opt = O3
>>> global = 0
>>> global = 0
>>>
>>>
>>> please note the different behaviour with O1 between gcc 4.9.2 and gcc 5.1.0
>>>
>>>
>>> Could you please comment on this issue ?
>>> - is this a bug ?
>>> - is this a "feature" ? (e.g. a known to be aggressive optimization
>>> that explicitly requires the global variable is declared as volatile)
>>>
>>>
>>> Thanks and regards,
>>>
>>> Gilles


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

end of thread, other threads:[~2015-06-16  5:18 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-09  4:42 gcc 4.9.2 and above are not aware of malloc (and friends) hooks Gilles Gouaillardet
2015-06-09  4:48 ` Andrew Pinski
     [not found]   ` <CAAkFZ5s-k=Up53=GxK4mcaiSCx4zJM36z9c_=uOtped_25G8sQ@mail.gmail.com>
2015-06-16  5:18     ` Gilles Gouaillardet

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