dri-devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Grazvydas Ignotas <notasas@gmail.com>
To: dri-devel@lists.freedesktop.org
Subject: [PATCH libdrm] xf86drm: fix aliasing violation
Date: Sun, 11 Dec 2016 20:03:57 +0200	[thread overview]
Message-ID: <1481479437-30537-2-git-send-email-notasas@gmail.com> (raw)

Just tell the compiler that drm_event will alias the char buffer,
so that it has no excuse to warn or generate bad code.

Signed-off-by: Grazvydas Ignotas <notasas@gmail.com>
---
 Android.mk      |  1 +
 configure.ac    |  9 +++++++++
 libdrm_macros.h |  6 ++++++
 xf86drmMode.c   | 11 +++++++----
 4 files changed, 23 insertions(+), 4 deletions(-)

diff --git a/Android.mk b/Android.mk
index 2aa2bc9..ae7c3fe 100644
--- a/Android.mk
+++ b/Android.mk
@@ -42,6 +42,7 @@ LOCAL_C_INCLUDES := \
 
 LOCAL_CFLAGS := \
 	-DHAVE_VISIBILITY=1 \
+	-DHAVE_MAY_ALIAS=1 \
 	-DHAVE_LIBDRM_ATOMIC_PRIMITIVES=1
 include $(BUILD_STATIC_LIBRARY)
 
diff --git a/configure.ac b/configure.ac
index e0597c3..2868036 100644
--- a/configure.ac
+++ b/configure.ac
@@ -507,6 +507,15 @@ if test "x$HAVE_ATTRIBUTE_VISIBILITY" = xyes; then
     AC_DEFINE(HAVE_VISIBILITY, 1, [Compiler supports __attribute__(("hidden"))])
 fi
 
+AC_MSG_CHECKING([whether $CC supports __attribute__((__may_alias__))])
+AC_LINK_IFELSE([AC_LANG_PROGRAM([
+    struct foo_may_alias { int i; } __attribute__((__may_alias__)) foo;
+])], HAVE_ATTRIBUTE_MAY_ALIAS="yes"; AC_MSG_RESULT([yes]), AC_MSG_RESULT([no]));
+
+if test "x$HAVE_ATTRIBUTE_MAY_ALIAS" = xyes; then
+    AC_DEFINE(HAVE_MAY_ALIAS, 1, [Compiler supports __attribute__((__may_alias__))])
+fi
+
 AC_SUBST(WARN_CFLAGS)
 AC_CONFIG_FILES([
 	Makefile
diff --git a/libdrm_macros.h b/libdrm_macros.h
index 639d090..eea47ee 100644
--- a/libdrm_macros.h
+++ b/libdrm_macros.h
@@ -29,6 +29,12 @@
 #  define drm_private
 #endif
 
+#if defined(HAVE_MAY_ALIAS)
+#  define drm_may_alias __attribute__((__may_alias__))
+#else
+#  define drm_may_alias
+#endif
+
 
 /**
  * Static (compile-time) assertion.
diff --git a/xf86drmMode.c b/xf86drmMode.c
index fb22f68..a6bf5f7 100644
--- a/xf86drmMode.c
+++ b/xf86drmMode.c
@@ -52,6 +52,7 @@
 #include <stdio.h>
 #include <stdbool.h>
 
+#include "libdrm_macros.h"
 #include "xf86drmMode.h"
 #include "xf86drm.h"
 #include <drm.h>
@@ -885,9 +886,11 @@ int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
 
 int drmHandleEvent(int fd, drmEventContextPtr evctx)
 {
+	struct drm_event_aliased {
+		struct drm_event e;
+	} drm_may_alias *e;
 	char buffer[1024];
 	int len, i;
-	struct drm_event *e;
 	struct drm_event_vblank *vblank;
 
 	/* The DRM read semantics guarantees that we always get only
@@ -901,8 +904,8 @@ int drmHandleEvent(int fd, drmEventContextPtr evctx)
 
 	i = 0;
 	while (i < len) {
-		e = (struct drm_event *) &buffer[i];
-		switch (e->type) {
+		e = (struct drm_event_aliased *) &buffer[i];
+		switch (e->e.type) {
 		case DRM_EVENT_VBLANK:
 			if (evctx->version < 1 ||
 			    evctx->vblank_handler == NULL)
@@ -928,7 +931,7 @@ int drmHandleEvent(int fd, drmEventContextPtr evctx)
 		default:
 			break;
 		}
-		i += e->length;
+		i += e->e.length;
 	}
 
 	return 0;
-- 
2.7.4

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

             reply	other threads:[~2016-12-11 18:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-11 18:03 Grazvydas Ignotas [this message]
2016-12-12 13:59 ` [PATCH libdrm] xf86drm: fix aliasing violation Emil Velikov
2016-12-12 23:28   ` Grazvydas Ignotas
2016-12-14 15:46     ` Emil Velikov
2016-12-14 17:11       ` Thierry Reding

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1481479437-30537-2-git-send-email-notasas@gmail.com \
    --to=notasas@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox