After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 335853 - (aliasing) Add G_GNUC_MAY_ALIAS
(aliasing)
Add G_GNUC_MAY_ALIAS
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other Linux
: Normal trivial
: ---
Assigned To: Tim Janik
gtkdev
: 373910 (view as bug list)
Depends on:
Blocks: 347585
 
 
Reported: 2006-03-24 15:50 UTC by Behdad Esfahbod
Modified: 2006-11-22 15:55 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
attachment showing example uses of G_GNUC_MAY_ALIAS (1.54 KB, text/x-csrc)
2006-11-22 15:08 UTC, Tim Janik
Details

Description Behdad Esfahbod 2006-03-24 15:50:57 UTC
As pointed out in bug #335341:

To <glib/gmacros.h> we would add this:

#if     __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
#  define G_GNUC_MAY_ALIAS __attribute__((may_alias))
#else
#  define G_GNUC_MAY_ALIAS
#endif


This can be used to mark types as may_alias, so they don't spew aliasing warnings.  Unfortunately we cannot automatically add this to all GObject's though.
Comment 1 Behdad Esfahbod 2006-05-29 21:22:39 UTC
Matthias, may I commit this, with proper doc updates?
Comment 2 Matthias Clasen 2006-05-30 12:38:09 UTC
I was a bit disappointed when I looked up may_alias in the gcc docs
and discovered it only applies to types, not to individual declarations
or function parameters. That makes it more or less useless for solving
our strict aliasing issues, no ?
Comment 3 Behdad Esfahbod 2006-05-31 05:59:20 UTC
gcc's docs are typically lacking in details.  The following sample is from 'info gcc':

typedef short __attribute__((__may_alias__)) short_a;

int
main (void)
{
  int a = 0x12345678;
  short_a *b = &a;

  b[1] = 0;

  if (a == 0x12345678)
    abort();

  exit(0);
}

where if the "short_a *b" is replaced with "short *b", it aborts.

Now expanding short_a and replacing the short_a *b line with:

  short __attribute__((__may_alias__)) *b = &a;

doesn't fix it; it still aborts.  BUT, applying the may_alias attribute to "int a" actually works.  So:

int
main (void)
{
  __attribute__((__may_alias__)) int a = 0x12345678;
  short *b = &a;

  b[1] = 0;

  if (a == 0x12345678)
    abort();

  exit(0);
}

doesn't abort.

I still need to test it more, in function prototypes and such, and see if it helps there.
Comment 4 Behdad Esfahbod 2006-09-03 02:40:25 UTC
Before I lose it again, to understand what exactly the strict-aliasing warning from gcc means and why double-casting is just hiding the warning, not fixing it, see this comment:

  http://bugzilla.gnome.org/show_bug.cgi?id=335341#c5
Comment 5 Mariano Suárez-Alvarez 2006-09-03 16:50:15 UTC
Behdad, that extract from the gcc docs cannot explain be the whole thing, because the little example you quote above in comment 3 does not have a function call.

In any case, in all the cases I've seen this warning, IIRC, what one wants to ‘set an attibute on’ is not the variable or the type but the _use_ of the variable, like on the beloved g_object_add_weak_pointer calls in the terminal code. If we make all gobject types non-aliasing, or take some other similarly draconian measure, then probably we would lose optimizations, I imagine.

The practice of adding to code such as

  g_object_add_weak_pointer (G_OBJECT (screen->priv->title_editor),
                             (void**) &screen->priv->title_editor);

an extra cast to (void*) may not be the most legible thing, but precisely says: in this particular instance, I know what I am doing; dressing it up into a G_PLEASE_TRUST_ME_I_KNOW_WHAT_I_MEAN() macro might turn it into something we can all introduce to our parents.

Comment 6 Behdad Esfahbod 2006-09-03 17:09:40 UTC
(In reply to comment #5)

> The practice of adding to code such as
> 
>   g_object_add_weak_pointer (G_OBJECT (screen->priv->title_editor),
>                              (void**) &screen->priv->title_editor);
> 
> an extra cast to (void*) may not be the most legible thing, but precisely says:
> in this particular instance, I know what I am doing; dressing it up into a
> G_PLEASE_TRUST_ME_I_KNOW_WHAT_I_MEAN() macro might turn it into something we
> can all introduce to our parents.

Right.  But in this very particular case, you are passing the same object to the same function with too different types.  This is exactly one of the few situations that the warning may actually mean something useful.  From experience, it seems like most of the aliasing problems don't happen for a majority of users/systems.  We've see FreeType breaking badly on Solaris.  Or there's only one report about gtk+ breaking by strict aliasing.  So, for the uncommon day that this bites us, I'd rather we have not forced the warning off without being 100% sure what we mean.

If you really want to get the warning shut up, try -Wno-strict-aliasing
Comment 7 Behdad Esfahbod 2006-11-13 06:01:37 UTC
*** Bug 373910 has been marked as a duplicate of this bug. ***
Comment 8 Tim Janik 2006-11-22 15:08:16 UTC
Created attachment 77028 [details]
attachment showing example uses of G_GNUC_MAY_ALIAS

here's an example program, showing off a typedef and non-typedef use of G_GNUC_MAY_ALIAS. since use of this compiler attribute is the only proper way to avoid gcc's aliasing warnings, i'll ad the macro to glib.
Comment 9 Tim Janik 2006-11-22 15:55:31 UTC
Wed Nov 22 16:09:13 2006  Tim Janik  <timj@gtk.org>

        * glib/gmacros.h: added G_GNUC_MAY_ALIAS, suggested by Mathias
        Hasselmann in bug #335341, fixes bug #335853.