Re: dereferencing type-punned pointer will break strict-aliasing rules

From: Thomas Moestl <t.moestl_at_tu-bs.de>
Date: Mon, 28 Jul 2003 04:05:35 +0200
On Mon, 2003/07/28 at 03:59:00 +0200, Thomas Moestl wrote:
> Yes, by implying -fstrict-aliasing, so using -fno-strict-aliasing is a
> workaround. The problem is caused by the i386 PCPU_GET/PCPU_SET
> implementation:
> 
> 	#define	__PCPU_GET(name) ({						\
> 		__pcpu_type(name) __result;					\
> 										\
> 	[...]
> 		} else if (sizeof(__result) == 4) {				\
> 			u_int __i;						\
> 			__asm __volatile("movl %%fs:%1,%0"			\
> 			    : "=r" (__i)					\
> 			    : "m" (*(u_int *)(__pcpu_offset(name))));		\
> 			__result = *(__pcpu_type(name) *)&__i;			\
> 	[...]
> 
> In this case, the PCPU_GET is used to retrieve curthread, causing
> sizeof(__result) to be 4, so the cast at the end of the code snippet
> is from a u_int * to struct thread *, and __i is accessed through the
		       ^^^^^^^^^^^^^^^ struct thread **, of course.

> casted pointer, which violates the C99 aliasing rules.

	- Thomas

-- 
Thomas Moestl <t.moestl_at_tu-bs.de>	http://www.tu-bs.de/~y0015675/
              <tmm_at_FreeBSD.org>		http://people.FreeBSD.org/~tmm/
PGP fingerprint: 1C97 A604 2BD0 E492 51D0  9C0F 1FE6 4F1D 419C 776C
Received on Sun Jul 27 2003 - 17:05:19 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:16 UTC