All of lore.kernel.org
 help / color / mirror / Atom feed
* Invalid compilation without -fno-strict-aliasing
@ 2003-02-25 23:46 Jean Tourrilhes
  2003-02-26 15:38 ` Horst von Brand
  2003-02-26 17:26 ` Linus Torvalds
  0 siblings, 2 replies; 34+ messages in thread
From: Jean Tourrilhes @ 2003-02-25 23:46 UTC (permalink / raw)
  To: Linux kernel mailing list

	Hi,

	It looks like a compiler bug to me...
	Some users have complained that when the following code is
compiled without the -fno-strict-aliasing, the order of the write and
memcpy is inverted (which mean a bogus len is mem-copied into the
stream).
	Code (from linux/include/net/iw_handler.h) :
--------------------------------------------
static inline char *
iwe_stream_add_event(char *	stream,		/* Stream of events */
		     char *	ends,		/* End of stream */
		     struct iw_event *iwe,	/* Payload */
		     int	event_len)	/* Real size of payload */
{
	/* Check if it's possible */
	if((stream + event_len) < ends) {
		iwe->len = event_len;
		memcpy(stream, (char *) iwe, event_len);
		stream += event_len;
	}
	return stream;
}
--------------------------------------------
	IMHO, the compiler should have enough context to know that the
reordering is dangerous. Any suggestion to make this simple code more
bullet proof is welcomed.

	Have fun...

	Jean

^ permalink raw reply	[flat|nested] 34+ messages in thread
* Re: Invalid compilation without -fno-strict-aliasing
@ 2003-02-26  4:33 Albert Cahalan
  2003-02-26 17:20 ` Jean Tourrilhes
  0 siblings, 1 reply; 34+ messages in thread
From: Albert Cahalan @ 2003-02-26  4:33 UTC (permalink / raw)
  To: linux-kernel; +Cc: jt

Jean Tourrilhes writes:

> It looks like a compiler bug to me...
> Some users have complained that when the following
> code is compiled without the -fno-strict-aliasing,
> the order of the write and memcpy is inverted (which
> mean a bogus len is mem-copied into the stream).
> Code (from linux/include/net/iw_handler.h) :
> --------------------------------------------
> static inline char *
> iwe_stream_add_event(char * stream,  /* Stream of events */
>        char * ends,  /* End of stream */
>        struct iw_event *iwe, /* Payload */
>        int event_len) /* Real size of payload */
> {
>  /* Check if it's possible */
>  if((stream + event_len) < ends) {
>   iwe->len = event_len;
>   memcpy(stream, (char *) iwe, event_len);
>   stream += event_len;
>  } return stream;
> }
> --------------------------------------------
> IMHO, the compiler should have enough context to
> know that the reordering is dangerous. Any suggestion
> to make this simple code more bullet proof is welcomed.
>
> Have fun...

Since (char*) is special, I agree that it's a bug.
In any case, a warning sure would be nice!

Now for the fun. Pass iwe->len into this
macro before the memcpy, and all should be well.

#define FORCE_TO_MEM(x) asm volatile(""::"r"(&(x)))

Like this:

   iwe->len = event_len;
   FORCE_TO_MEM(iwe->len);
   memcpy(stream, (char *) iwe, event_len);



^ permalink raw reply	[flat|nested] 34+ messages in thread
* Invalid compilation without -fno-strict-aliasing
@ 2017-11-03  1:54 Yubin Ruan
  2017-11-03 12:27 ` Paul E. McKenney
  0 siblings, 1 reply; 34+ messages in thread
From: Yubin Ruan @ 2017-11-03  1:54 UTC (permalink / raw)
  To: perfbook

Does anyone have any idea why this thread

    https://lkml.org/lkml/2003/2/25/270

is related to strict-aliasing? To me, a compiler barrier like this will fix it:

    if((stream + event_len) < ends) {
        iwe->len = event_len;
        barrier();
        memcpy(stream, (char *) iwe, event_len);
        stream += event_len;
    }

Yubin

^ permalink raw reply	[flat|nested] 34+ messages in thread

end of thread, other threads:[~2017-11-04  0:24 UTC | newest]

Thread overview: 34+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-25 23:46 Invalid compilation without -fno-strict-aliasing Jean Tourrilhes
2003-02-26 15:38 ` Horst von Brand
2003-02-26 16:04   ` Falk Hueffner
2003-02-26 20:47     ` Horst von Brand
2003-02-26 20:57       ` Daniel Jacobowitz
2003-02-26 22:22         ` Jakub Jelinek
2003-02-27 19:30           ` Linus Torvalds
2003-02-27 19:45             ` Daniel Jacobowitz
2003-02-27 20:00               ` Linus Torvalds
2003-02-27 20:35                 ` Daniel Jacobowitz
2003-02-27 20:38                   ` Linus Torvalds
2003-02-27 23:55                     ` H. Peter Anvin
2003-03-01  8:29                     ` Anton Blanchard
2003-02-26 17:22   ` Jean Tourrilhes
2003-02-26 21:07     ` Horst von Brand
2003-02-27  4:41       ` Daniel Phillips
2003-02-26 17:26 ` Linus Torvalds
2003-02-26  4:33 Albert Cahalan
2003-02-26 17:20 ` Jean Tourrilhes
2003-02-26 18:23   ` Richard B. Johnson
2003-02-26 19:22     ` Daniel Jacobowitz
2003-02-26 19:40       ` Richard B. Johnson
2003-02-26 19:42         ` Daniel Jacobowitz
2003-02-26 20:19           ` Richard B. Johnson
2003-02-26 21:30             ` Albert Cahalan
2017-11-03  1:54 Yubin Ruan
2017-11-03 12:27 ` Paul E. McKenney
2017-11-03 13:33   ` Yubin Ruan
2017-11-03 14:03     ` Paul E. McKenney
2017-11-03 14:12       ` Yubin Ruan
2017-11-03 14:38         ` Paul E. McKenney
2017-11-03 23:26           ` Yubin Ruan
2017-11-03 23:55             ` Paul E. McKenney
2017-11-04  0:24               ` Yubin Ruan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.