Avoid undefined behavior due to strict aliasing

This does not make XCB conform to the C99+ strict aliasing rules, but it
does use attributes to prevent GCC and Clang from assuming the rules are
followed in the cases where XCB does violate them.

Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
This commit is contained in:
Demi Marie Obenour 2022-10-26 21:00:57 -04:00
parent 8adb5068af
commit 48dbb5b6ca
33 changed files with 64 additions and 39 deletions

View File

@ -15,7 +15,7 @@ AM_INIT_AUTOMAKE([foreign dist-xz])
AM_PATH_PYTHON([3.0])
# Set common system defines for POSIX extensions, such as _GNU_SOURCE
# Must be called before any macros that run the compiler (like AC_PROG_LIBTOOL)
# Must be called before any macros that run the compiler (like LT_INIT)
# to avoid autoconf errors.
AC_USE_SYSTEM_EXTENSIONS
AC_SYS_LARGEFILE
@ -27,6 +27,8 @@ LT_INIT([win32-dll])
# Require xorg-macros minimum of 1.18 - Initial version
m4_ifndef([XORG_MACROS_VERSION],
[m4_fatal([must install xorg-macros 1.18 or later before running autoconf/autogen])])
XORG_TESTSET_CFLAG([XCB_CFLAGS], [-fno-strict-aliasing])
AC_SUBST([XCB_CFLAGS])
XORG_MACROS_VERSION(1.18)
XORG_DEFAULT_OPTIONS
XORG_ENABLE_DEVEL_DOCS
@ -248,6 +250,23 @@ XCB_EXTENSION(XTest, yes)
XCB_EXTENSION(Xv, yes)
XCB_EXTENSION(XvMC, yes)
AC_MSG_CHECKING([[if __attribute__((may_alias)) is supported]])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
typedef struct x { int a; } __attribute__((may_alias)) x;
]])],[
AC_DEFINE([[XCB_MAY_ALIAS]], [[__attribute__((may_alias))]], [Define if __attribute__((may_alias)) works])
AC_MSG_RESULT([[yes]])
],[
AC_MSG_RESULT([[no]])
AC_MSG_CHECKING([[if -fno-strict-aliasing works]])[
case $XCB_CFLAGS in
*-fno-strict-aliasing*)]
AC_MSG_RESULT([[yes]])
AC_MSG_WARN([[may_alias not supported, programs must use -fno-strict-aliasing]]);;
*)
AC_MSG_RESULT([[no (flags are $XCB_CFLAGS)]])
AC_MSG_FAILURE([[No method of suppressing strict-aliasing found]]);;
[esac]])
AC_ARG_WITH(serverside-support, AS_HELP_STRING([--with-serverside-support], [Build with support for server-side usage of xcb. This is still EXPERIMENTAL! ABI/API may change! (default: no)]), [XCB_SERVERSIDE_SUPPORT=$withval], [XCB_SERVERSIDE_SUPPORT=no])
AM_CONDITIONAL(XCB_SERVERSIDE_SUPPORT, test "x$XCB_SERVERSIDE_SUPPORT" = "xyes")

View File

@ -4,7 +4,7 @@ EXTSOURCES = xproto.c \
bigreq.c \
xc_misc.c
AM_CFLAGS = $(BASE_CFLAGS) $(NEEDED_CFLAGS) $(XDMCP_CFLAGS)
AM_CFLAGS = $(BASE_CFLAGS) $(NEEDED_CFLAGS) $(XDMCP_CFLAGS) $(XCB_CFLAGS)
libxcb_la_LIBADD = $(NEEDED_LIBS) $(XDMCP_LIBS)
libxcb_la_SOURCES = \
xcb_conn.c xcb_out.c xcb_in.c xcb_ext.c xcb_xid.c \
@ -249,7 +249,7 @@ nodist_libxcb_ge_la_SOURCES = ge.c ge.h
endif
EXTHEADERS=$(EXTSOURCES:.c=.h)
xcbinclude_HEADERS = xcb.h xcbext.h
xcbinclude_HEADERS = xcb.h xcbext.h config.h
if XCB_HAVE_WIN32
xcbinclude_HEADERS += xcb_windefs.h
endif

View File

@ -279,7 +279,7 @@ def c_open(self):
_h('#include "xcb.h"')
_c('#ifdef HAVE_CONFIG_H')
_c('#include "config.h"')
_hc('#include "config.h"')
_c('#endif')
_c('#include <stdlib.h>')
_c('#include <stdalign.h>')
@ -1642,7 +1642,7 @@ def _c_iterator(self, name):
param[0],
' ' * (len(self.c_type) + 1 - len(param[0])),
param[2])
_h('} %s;', self.c_iterator_type)
_h('} XCB_MAY_ALIAS %s;', self.c_iterator_type)
_h_setlevel(1)
_c_setlevel(1)
@ -2254,7 +2254,7 @@ def _c_complex(self, force_packed = False):
if b.type.has_name:
_h(' } %s;', b.c_field_name)
_h('} %s%s;', 'XCB_PACKED ' if force_packed else '', self.c_type)
_h('} %sXCB_MAY_ALIAS %s;', 'XCB_PACKED ' if force_packed else '', self.c_type)
def c_struct(self, name):
'''
@ -2756,7 +2756,7 @@ def _c_cookie(self, name):
_h(' **/')
_h('typedef struct %s {', self.c_cookie_type)
_h(' unsigned int sequence;')
_h('} %s;', self.c_cookie_type)
_h('} XCB_MAY_ALIAS %s;', self.c_cookie_type)
def _man_request(self, name, void, aux):
param_fields = [f for f in self.fields if f.visible]
@ -2812,7 +2812,7 @@ def _man_request(self, name, void, aux):
f.write('.SS Reply datastructure\n')
f.write('.nf\n')
f.write('.sp\n')
f.write('typedef %s %s {\n' % (self.reply.c_container, self.reply.c_type))
f.write('typedef XCB_MAY_ALIAS %s %s {\n' % (self.reply.c_container, self.reply.c_type))
struct_fields = []
maxtypelen = 0
@ -3389,7 +3389,7 @@ def c_event(self, name):
else:
# Typedef
_h('')
_h('typedef %s %s;', _t(self.name + ('event',)), _t(name + ('event',)))
_h('typedef XCB_MAY_ALIAS %s %s;', _t(self.name + ('event',)), _t(name + ('event',)))
# Create sizeof-function for eventcopies for compatibility reasons
if self.c_need_sizeof:
@ -3424,7 +3424,7 @@ def c_error(self, name):
else:
# Typedef
_h('')
_h('typedef %s %s;', _t(self.name + ('error',)), _t(name + ('error',)))
_h('typedef XCB_MAY_ALIAS %s %s;', _t(self.name + ('error',)), _t(name + ('error',)))
# Main routine starts here

View File

@ -53,8 +53,14 @@ extern "C" {
#ifdef __GNUC__
#define XCB_PACKED __attribute__((__packed__))
#ifndef XCB_MAY_ALIAS
#define XCB_MAY_ALIAS __attribute__((may_alias))
#endif
#else
#define XCB_PACKED
#ifndef XCB_MAY_ALIAS
#define XCB_MAY_ALIAS
#endif
#endif
/**

View File

@ -8,4 +8,4 @@ Description: XCB Composite Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb xcb-xfixes
Libs: -L${libdir} -lxcb-composite
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Damage Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb xcb-xfixes
Libs: -L${libdir} -lxcb-damage
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Double Buffer Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-dbe
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB DPMS Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-dpms
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB DRI2 Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-dri2
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB DRI3 Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-dri3
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB GenericEvent Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-ge
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB GLX Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-glx
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Present Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb xcb-randr xcb-xfixes xcb-sync xcb-dri3
Libs: -L${libdir} -lxcb-present
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB RandR Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb xcb-render
Libs: -L${libdir} -lxcb-randr
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Record Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-record
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Render Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-render
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB X-Resource Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-res
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Screensaver Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-screensaver
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Shape Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-shape
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Shm Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-shm
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Sync Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-sync
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Xevie Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-xevie
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB XFree86-DRI Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-xf86dri
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB XFixes Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb xcb-render xcb-shape
Libs: -L${libdir} -lxcb-xfixes
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Xinerama Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-xinerama
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB XInput Extension (EXPERIMENTAL)
Version: @PACKAGE_VERSION@
Requires.private: xcb xcb-xfixes
Libs: -L${libdir} -lxcb-xinput
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Keyboard Extension (EXPERIMENTAL)
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-xkb
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Xprint Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-xprint
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB SELinux Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-xselinux
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB XTEST Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb
Libs: -L${libdir} -lxcb-xtest
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB Xv Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb xcb-shm
Libs: -L${libdir} -lxcb-xv
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -8,4 +8,4 @@ Description: XCB XvMC Extension
Version: @PACKAGE_VERSION@
Requires.private: xcb xcb-xv
Libs: -L${libdir} -lxcb-xvmc
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@

View File

@ -10,4 +10,4 @@ Version: @PACKAGE_VERSION@
Requires.private: @NEEDED@
Libs: -L${libdir} -lxcb
Libs.private: @LIBS@
Cflags: -I${includedir}
Cflags: -I${includedir} @XCB_CFLAGS@