Skip to contentSkip to conversation

netlink.c:97: error: cast increases required alignment of target type

DISCUSSION andrzej zaborowski 2 participants 6 messages Nov 9, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi,
resurrecting this thread because I think there's no reason ofono should not build on cpus with strict alignment rules, the netlink code is actually aligned correctly. These warnings are a result of -Wcast-align, the compiler can't know that the casts in the macros are safe, but they obviously are if you look at the arithmetics there. In the case of the netlink warnings they can be turned off for all users of <linux/netlink.h> using a pragma or only for this cast by casting to void * first (the only way according to gcc people), the first attached file adds them (against linux), I'll send it to linux if there's no better idea. There are also similar warnings when casting struct sockaddr_pn * to struct sockaddr * and these can be worked around in ofono, see second patch, but probably should also be in linux's include/linux/phonet.h instead.

Regards

include/linux/if_addr.h +1 -1
index a60c821..d9e7d2f 100644
@@ -56,7 +56,7 @@ struct ifa_cacheinfo
 
 /* backwards compatibility for userspace */
 #ifndef __KERNEL__
-#define IFA_RTA(r)  ((struct rtattr*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
+#define IFA_RTA(r)  ((struct rtattr*)(void*)(((char*)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrmsg))))
 #define IFA_PAYLOAD(n) NLMSG_PAYLOAD(n,sizeof(struct ifaddrmsg))
 #endif
 
include/linux/netlink.h +1 -1
index 5ba398e..8f0f76d 100644
@@ -81,7 +81,7 @@ struct nlmsghdr
 #define NLMSG_SPACE(len) NLMSG_ALIGN(NLMSG_LENGTH(len))
 #define NLMSG_DATA(nlh)  ((void*)(((char*)nlh) + NLMSG_LENGTH(0)))
 #define NLMSG_NEXT(nlh,len)	 ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \
-				  (struct nlmsghdr*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))
+				  (struct nlmsghdr*)(void*)(((char*)(nlh)) + NLMSG_ALIGN((nlh)->nlmsg_len)))
 #define NLMSG_OK(nlh,len) ((len) >= (int)sizeof(struct nlmsghdr) && \
 			   (nlh)->nlmsg_len >= sizeof(struct nlmsghdr) && \
 			   (nlh)->nlmsg_len <= (len))
include/linux/rtnetlink.h +1 -1
index ba3254e..a8fa2ae 100644
@@ -141,7 +141,7 @@ struct rtattr
 			 (rta)->rta_len >= sizeof(struct rtattr) && \
 			 (rta)->rta_len <= (len))
 #define RTA_NEXT(rta,attrlen)	((attrlen) -= RTA_ALIGN((rta)->rta_len), \
-				 (struct rtattr*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
+				 (struct rtattr*)(void*)(((char*)(rta)) + RTA_ALIGN((rta)->rta_len)))
 #define RTA_LENGTH(len)	(RTA_ALIGN(sizeof(struct rtattr)) + (len))
 #define RTA_SPACE(len)	RTA_ALIGN(RTA_LENGTH(len))
 #define RTA_DATA(rta)   ((void*)(((char*)(rta)) + RTA_LENGTH(0)))
gisi/client.c +5 -2
index f0db24e..26ace15 100644
@@ -69,6 +69,9 @@ struct _GIsiClient {
 	void *debug_data;
 };
 
+typedef struct sockaddr_pn __attribute__ ((aligned (__alignof(struct sockaddr))))
+	sockaddr_pn_align;
+
 static gboolean g_isi_callback(GIOChannel *channel, GIOCondition cond,
 				gpointer data);
 static gboolean g_isi_timeout(gpointer data);
@@ -233,7 +236,7 @@ GIsiRequest *g_isi_request_make(GIsiClient *cl,	const void *__restrict buf,
 				GIsiResponseFunc cb, void *opaque)
 {
 	struct iovec iov[2];
-	const struct sockaddr_pn dst = {
+	const sockaddr_pn_align dst = {
 		.spn_family = AF_PHONET,
 		.spn_resource = cl->resource,
 	};
@@ -324,7 +327,7 @@ void g_isi_request_cancel(GIsiRequest *req)
 #define PN_COMMGR 0x10
 #define PNS_SUBSCRIBED_RESOURCES_IND	0x10
 
-static const struct sockaddr_pn commgr = {
+static const sockaddr_pn_align commgr = {
 	.spn_family = AF_PHONET,
 	.spn_resource = PN_COMMGR,
 };
gisi/socket.c +5 -2
index 4373e9d..883aa14 100644
@@ -38,10 +38,13 @@
 
 #include "socket.h"
 
+typedef struct sockaddr_pn __attribute__ ((aligned (__alignof(struct sockaddr))))
+	sockaddr_pn_align;
+
 GIOChannel *phonet_new(GIsiModem *modem, uint8_t resource)
 {
 	GIOChannel *channel;
-	struct sockaddr_pn addr = {
+	sockaddr_pn_align addr = {
 		.spn_family = AF_PHONET,
 		.spn_resource = resource,
 	};
@@ -83,7 +86,7 @@ size_t phonet_peek_length(GIOChannel *channel)
 ssize_t phonet_read(GIOChannel *channel, void *restrict buf, size_t len,
 			uint16_t *restrict obj, uint8_t *restrict res)
 {
-	struct sockaddr_pn addr;
+	sockaddr_pn_align addr;
 	socklen_t addrlen = sizeof(addr);
 	ssize_t ret;
 
#1 lore raw

Le lundi 9 novembre 2009 17:45:25 andrzej zaborowski, vous avez écrit :

Quoted 6 lines

Hi,
resurrecting this thread because I think there's no reason ofono should not build on cpus with strict alignment rules, the netlink code is actually aligned correctly. These warnings are a result of -Wcast-align, the compiler can't know that the casts in the macros are safe, but they obviously are if you look at the arithmetics there. In

Quoted 8 lines

the case of the netlink warnings they can be turned off for all users of <linux/netlink.h> using a pragma or only for this cast by casting to void * first (the only way according to gcc people), the first attached file adds them (against linux), I'll send it to linux if there's no better idea. There are also similar warnings when casting struct sockaddr_pn * to struct sockaddr * and these can be worked around in ofono, see second patch, but probably should also be in linux's include/linux/phonet.h instead.

Why do you change the sockaddr_pn type name?!

Signature · 2 lines
Rémi Denis-Courmont
http://www.remlab.net/
#2 lore raw

2009/11/9 Rémi Denis-Courmont <[email protected]>:

Quoted 16 lines

Le lundi 9 novembre 2009 17:45:25 andrzej zaborowski, vous avez écrit :

Quoted 6 lines

Hi,
resurrecting this thread because I think there's no reason ofono should not build on cpus with strict alignment rules, the netlink code is actually aligned correctly.  These warnings are a result of -Wcast-align, the compiler can't know that the casts in the macros are safe, but they obviously are if you look at the arithmetics there.  In

Quoted 8 lines

the case of the netlink warnings they can be turned off for all users of <linux/netlink.h> using a pragma or only for this cast by casting to void * first (the only way according to gcc people), the first attached file adds them (against linux), I'll send it to linux if there's no better idea.  There are also similar warnings when casting struct sockaddr_pn * to struct sockaddr * and these can be worked around in ofono, see second patch, but probably should also be in linux's include/linux/phonet.h instead.

Quoted 1 line

Why do you change the sockaddr_pn type name?!

Because it's been defined without explicit alignment and I don't think you can redefine it with the same name?

Regards

#3 lore raw

Le lundi 9 novembre 2009 19:34:36 andrzej zaborowski, vous avez écrit :

Quoted 17 lines

2009/11/9 Rémi Denis-Courmont <[email protected]>:

Quoted 16 lines

Le lundi 9 novembre 2009 17:45:25 andrzej zaborowski, vous avez écrit :

Quoted 14 lines

Hi,
resurrecting this thread because I think there's no reason ofono should not build on cpus with strict alignment rules, the netlink code is actually aligned correctly. These warnings are a result of -Wcast-align, the compiler can't know that the casts in the macros are safe, but they obviously are if you look at the arithmetics there. In the case of the netlink warnings they can be turned off for all users of <linux/netlink.h> using a pragma or only for this cast by casting to void * first (the only way according to gcc people), the first attached file adds them (against linux), I'll send it to linux if there's no better idea. There are also similar warnings when casting struct sockaddr_pn * to struct sockaddr * and these can be worked around in ofono, see second patch, but probably should also be in linux's include/linux/phonet.h instead.

Quoted 4 lines
Quoted 1 line

Why do you change the sockaddr_pn type name?!

Because it's been defined without explicit alignment and I don't think you can redefine it with the same name?

As far as I know NO extra alignement is needed. The kernel will anyway memory copy from/to the sockaddr_pn buffer as part of the socket calls, so it couldn't care less about alignment.

Signature · 2 lines
Rémi Denis-Courmont
http://www.remlab.net/
#4 lore raw

2009/11/9 Rémi Denis-Courmont <[email protected]>:

Quoted 3 lines

As far as I know NO extra alignement is needed. The kernel will anyway memory copy from/to the sockaddr_pn buffer as part of the socket calls, so it couldn't care less about alignment.

Practically I'm sure you're right and maybe we should stick a (void *) cast in between the casts or disable the warning. Formally that memcpy is an implementation detail and could be true in one kernel version and false in the next version, so when you pass a struct sockaddr * to a function it should, in theory be aligned as such.

Regards

#5 lore raw

Le lundi 9 novembre 2009 19:57:07 andrzej zaborowski, vous avez écrit :

Quoted 10 lines

2009/11/9 Rémi Denis-Courmont <[email protected]>:

Quoted 3 lines

As far as I know NO extra alignement is needed. The kernel will anyway memory copy from/to the sockaddr_pn buffer as part of the socket calls, so it couldn't care less about alignment.

Practically I'm sure you're right and maybe we should stick a (void *) cast in between the casts or disable the warning. Formally that memcpy is an implementation detail and could be true in one kernel version and false in the next version, so when you pass a struct sockaddr * to a function it should, in theory be aligned as such.

Well no. The only thing you're allowed to read from someone else's sockaddr is the family field, which does have the same alignment in sockaddr_pn as in sockaddr (practically, two bytes). After that, the reader is responsible for casting back to the correct type.

So this warning is _bogus_.

Signature · 2 lines
Rémi Denis-Courmont
http://www.remlab.net/