Invalid compilation without -fno-strict-aliasing
Quoted 25 lines · Jean Tourrilhes
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 (fromlinux/include/net/iw_handler.h) :
--------------------------------------------
static inline char *
iwe_stream_add_event(char * stream, /* Stream of events */
char * ends, /* End of stream */
structiw_event*iwe, /* Payload */
intevent_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.
Quoted 1 line
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);
Quoted 35 lines · Albert Cahalan
Quoted 25 lines · Jean Tourrilhes
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 (fromlinux/include/net/iw_handler.h) :
--------------------------------------------
static inline char *
iwe_stream_add_event(char * stream, /* Stream of events */
char * ends, /* End of stream */
structiw_event*iwe, /* Payload */
intevent_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.Quoted 1 line
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->leninto this
macro before the memcpy, and all should be well.
Quoted 7 lines
#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);
I'll try that, that sounds absolutely clever (but I only
understand half of it).
Thanks a lot !
Jean
Quoted 44 lines · Jean Tourrilhes
Quoted 35 lines · Albert Cahalan
Quoted 25 lines · Jean Tourrilhes
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 (fromlinux/include/net/iw_handler.h) :
--------------------------------------------
static inline char *
iwe_stream_add_event(char * stream, /* Stream of events */
char * ends, /* End of stream */
structiw_event*iwe, /* Payload */
intevent_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.Quoted 1 line
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->leninto this
macro before the memcpy, and all should be well.Quoted 7 lines
#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);
Quoted 6 lines
I'll try that, that sounds absolutely clever (but I only
understand half of it).
Thanks a lot !Jean
-
This does absoultely nothing with egcs-2.91.66. I also modified
#define FORCE_TO_MEM(x) asm volatile(""::"r"(&(x)))
|________ this to "memory"and it still does nothing. The result of gcc -O2 -S -o xxx xxx.c just shows:
#AP
#NOAP
With no code in-between.
I also changed it to:
#define FORCE_TO_MEM(x) __asm__ __volatile__(""::"r"(&(x)))
to no avail.
What's up?
Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips). Why is the government concerned about the lunatic fringe? Think about it.
Quoted 64 lines · Richard B. Johnson
Quoted 44 lines · Jean Tourrilhes
Quoted 35 lines · Albert Cahalan
Quoted 25 lines · Jean Tourrilhes
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 (fromlinux/include/net/iw_handler.h) :
--------------------------------------------
static inline char *
iwe_stream_add_event(char * stream, /* Stream of events */
char * ends, /* End of stream */
structiw_event*iwe, /* Payload */
intevent_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.Quoted 1 line
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->leninto this
macro before the memcpy, and all should be well.Quoted 7 lines
#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);Quoted 6 lines
I'll try that, that sounds absolutely clever (but I only
understand half of it).
Thanks a lot !Jean
-This does absoultely nothing with egcs-2.91.66. I also modified
#define FORCE_TO_MEM(x) asm volatile(""::"r"(&(x))) |________ this to "memory"and it still does nothing. The result of gcc -O2 -S -o xxx
xxx.cjust shows:#AP
#NOAP
Quoted 7 lines
With no code in-between.
I also changed it to:
#defineFORCE_TO_MEM(x)__asm____volatile__(""::"r"(&(x)))
to no avail.What's up?
That's "working". Does it prevent the reordering?
Signature · 2 lines
Daniel Jacobowitz MontaVista Software Debian GNU/Linux Developer
Quoted 67 lines · Daniel Jacobowitz
Quoted 66 lines · Richard B. Johnson
Quoted 44 lines · Jean Tourrilhes
Quoted 35 lines · Albert Cahalan
Quoted 25 lines · Jean Tourrilhes
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 (fromlinux/include/net/iw_handler.h) :
--------------------------------------------
static inline char *
iwe_stream_add_event(char * stream, /* Stream of events */
char * ends, /* End of stream */
structiw_event*iwe, /* Payload */
intevent_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.Quoted 1 line
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->leninto this
macro before the memcpy, and all should be well.Quoted 7 lines
#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);Quoted 6 lines
I'll try that, that sounds absolutely clever (but I only
understand half of it).
Thanks a lot !Jean
-This does absoultely nothing with egcs-2.91.66. I also modified
#define FORCE_TO_MEM(x) asm volatile(""::"r"(&(x))) |________ this to "memory"and it still does nothing. The result of gcc -O2 -S -o xxx
xxx.cjust shows:#AP
#NOAPWith no code in-between.
Quoted 7 lines
Quoted 5 lines
I also changed it to:
#defineFORCE_TO_MEM(x)__asm____volatile__(""::"r"(&(x)))
to no avail.What's up?
That's "working". Does it prevent the reordering?
It was supposed to force x, which may be cached in a register, to be written to memory __now__. It doesn't seem to do anything. I think FORCE_TO_MEM() needs to claim that it uses most all the registers. That will make sure that any register values get written to their final memory locations.
Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips). Why is the government concerned about the lunatic fringe? Think about it.
Quoted 75 lines · Richard B. Johnson
Quoted 73 lines · Daniel Jacobowitz
Quoted 71 lines · Richard B. Johnson
Quoted 44 lines · Jean Tourrilhes
Quoted 35 lines · Albert Cahalan
Quoted 25 lines · Jean Tourrilhes
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 (fromlinux/include/net/iw_handler.h) :
--------------------------------------------
static inline char *
iwe_stream_add_event(char * stream, /* Stream of events */
char * ends, /* End of stream */
structiw_event*iwe, /* Payload */
intevent_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.Quoted 1 line
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->leninto this
macro before the memcpy, and all should be well.Quoted 7 lines
#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);Quoted 6 lines
I'll try that, that sounds absolutely clever (but I only
understand half of it).
Thanks a lot !Jean
-This does absoultely nothing with egcs-2.91.66. I also modified
#define FORCE_TO_MEM(x) asm volatile(""::"r"(&(x))) |________ this to "memory"and it still does nothing. The result of gcc -O2 -S -o xxx
xxx.cjust shows:#AP
#NOAPWith no code in-between.
I also changed it to:
#defineFORCE_TO_MEM(x)__asm____volatile__(""::"r"(&(x)))
to no avail.What's up?
Quoted 7 lines
Quoted 1 line
That's "working". Does it prevent the reordering?
It was supposed to force x, which may be cached in a register, to be written to memory
__now__. It doesn't seem to do anything. I thinkFORCE_TO_MEM()needs to claim that it uses most all the registers. That will make sure that any register values get written to their final memory locations.
If so it wouldn't be inside the #APP/#NOAPP markers. You didn't answer my other question: was X in memory at the time?
You should be using something like __asm__ __volatile__ (""::"m"(x)) anyway.
Signature · 2 lines
Daniel Jacobowitz MontaVista Software Debian GNU/Linux Developer
On Wed, 26 Feb 2003, Daniel Jacobowitz wrote:
[SNIPPED...]
Quoted 8 lines
Quoted 5 lines
It was supposed to force x, which may be cached in a register, to be written to memory
__now__. It doesn't seem to do anything. I thinkFORCE_TO_MEM()needs to claim that it uses most all the registers. That will make sure that any register values get written to their final memory locations.If so it wouldn't be inside the #APP/#NOAPP markers. You didn't answer my other question: was X in memory at the time?
It was in %ebx register and didn't go back to NNN(%esp) where it came from. Like I said, it did do anything.
Quoted 4 lines
You should be using something like
__asm____volatile__(""::"m"(x)) anyway.
Yep. Probably.
Cheers,
Dick Johnson
Penguin : Linux version 2.4.18 on an i686 machine (797.90 BogoMips). Why is the government concerned about the lunatic fringe? Think about it.
Quoted 11 lines · Richard B. Johnson
Quoted 8 lines · Daniel Jacobowitz
Quoted 5 lines
It was supposed to force x, which may be cached in a register, to be written to memory
__now__. It doesn't seem to do anything. I thinkFORCE_TO_MEM()needs to claim that it uses most all the registers. That will make sure that any register values get written to their final memory locations.If so it wouldn't be inside the #APP/#NOAPP markers. You didn't answer my other question: was X in memory at the time?
Quoted 7 lines
It was in %ebx register and didn't go back to NNN(%esp) where it came from. Like I said, it did do anything.
Quoted 2 lines
You should be using something like
__asm____volatile__(""::"m"(x)) anyway.Yep. Probably.
I wrote that from my memory and gcc documentation. With none of my 3 compilers did I see the sample code failing, so I wasn't able to perform a useful test. There may be some little detail I missed.
I've had the code working, with gcc 3.0 I'd guess, on both PowerPC and something strange. The code relies on "r" being a suitable register; this might only be true for common RISC architectures. So this is semi-portable code, able to be used where the gcc porter followed the suggestion to use "r" as the normal sort of register. Since x86 is notably lacking in the concept of general purpose registers, the above might not apply.
Add :"memory" (a clobber) on the end if you want to be brutal. This may produce slower code though.
The basic idea is to convince gcc that some assembly code will access the data via a pointer. Probably somebody else has already posted a fixed version.
With a recent gcc, look into the __may_alias__ attribute.
This kind of stuff sure is poorly documented, isn't it? Has anybody ever seen a reasonable explanation of the new C99 restrict keyword, including scope interactions? How about a list of gcc bugs in this area? Even with a recent gcc, control over aliasing is really crude. I ought to be able to specify a list of types and objects that do or do not alias, with each other or with the first one specified.