gcc in rawhide reports over 500 strict-aliasing warnings during glib2 build, and what's worse, one warning in gthread.h:344 affects over 200 other packages, in some cases could result in miscompiled code. The problem is with the g_atomic_{pointer,int}_{s,g}et APIs. They expect gpointer * or gint * first argument, but various users use the former e.g. on gsize * or pointer to pointer pointing to something other than void, the latter e.g. with guint *. On some hosts these are implemented by macros, just dereferencing the pointer and either returning what it points to or changing it. In this case with the callers casting say gsize * to gpointer * and using that in g_atomic_pointer_get it is an obvious aliasing violation, object with gsize effective type is read using gpointer type. The compiler might do anything in that case. If these aren't implemented as macros, just adding (void *) cast before casting to (gpointer *) is enough to get rid of the aliasing warning, but it would be better to be prepared for -fwhopr etc. As most of the g_atomic_pointer_get etc. users seem to be buggy, and they shouldn't need to worry thether it is expanded as macro or calls the function, the patch removes the type punning in the users and handles it in the macros. It ensures g_atomic_pointer_* is used only on objects of the same size as pointer and similarly for integers. To be prepared for whole program optimizations, the patch adds may_alias attribute to say that the access in out-of-line functions is done in alias set 0, for inline expansion the access is done using the original type, so this isn't necessary. With this patch there are no aliasing warnings in glib2 and the warning in gthread.h:344 that triggered on many other packages is gone as well. Can this be incorporated into glib2 before the mass rebuild (i.e. this week), to avoid having to rebuild all packages using glib2 headers once again?
Created attachment 332212 [details] glib-strict-aliasing.patch
Thanks, Jakub. I hope to get this into a release (and into rawhide) sometime today.
can this go upstream as well?
oops, it already is upstream. sorry for the noise. ;)