public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/43774]  New: option -O2 generates wrong assembly code
@ 2010-04-17  3:33 dirtysalt1987 at gmail dot com
  2010-04-17  6:55 ` [Bug c/43774] " jakub at gcc dot gnu dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: dirtysalt1987 at gmail dot com @ 2010-04-17  3:33 UTC (permalink / raw)
  To: gcc-bugs

compiling the following code with -O2, the program will core dump.
I check the assembly code output, it seems the 'strlen' function call is
replaced by the 'builtin strlen' funciton and will read the first four byte on
a invalid memory page.
And if i replace the mmap with malloc and run under the Valgrind[3.5.0],the
Valgrind also reports 'Invalid read of size 4'.

Ps:How to workaround this piece of code???I think there are two ways
a.mmap 4 bytes or more to make sure the strlen will not read the invalid memory
b.use the gcc option '-fno-builtin-strlen' to make sure the 'strlen' is not
replaced.
But I'm not sure is there a more elegant way to workaroud this??

====================================================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
typedef struct _x_t
{
    int offset; //'strlen' is replaced iff. this field exists.
    char data[0];
}x_t;

int main()
{   
    //make a 4K memory page.
    char *buff=mmap(NULL,4096,PROT_WRITE | PROT_READ ,MAP_PRIVATE |
MAP_ANONYMOUS,0,0);
    char *buffer = buff+4096-11;
    strcpy(buffer,"0123456789");  
    x_t *x=(x_t*)buffer;
    printf("%d\n",strlen(x->data)); //read a invalid page.
    munmap(buff,4096);
    return 0;
}


-- 
           Summary: option -O2 generates wrong assembly code
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dirtysalt1987 at gmail dot com
 GCC build triplet: GNU/linux 2.6.9 Intel Xeon
  GCC host triplet: GNU/linux 2.6.9 Intel Xeon
GCC target triplet: GNU/linux 2.6.9 Intel Xeon


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43774


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

* [Bug c/43774] option -O2 generates wrong assembly code
  2010-04-17  3:33 [Bug c/43774] New: option -O2 generates wrong assembly code dirtysalt1987 at gmail dot com
@ 2010-04-17  6:55 ` jakub at gcc dot gnu dot org
  2010-04-17  8:12 ` dirtysalt1987 at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jakub at gcc dot gnu dot org @ 2010-04-17  6:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from jakub at gcc dot gnu dot org  2010-04-17 06:55 -------
You should make the struct packed, because otherwise you are accessing it
unaligned.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43774


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

* [Bug c/43774] option -O2 generates wrong assembly code
  2010-04-17  3:33 [Bug c/43774] New: option -O2 generates wrong assembly code dirtysalt1987 at gmail dot com
  2010-04-17  6:55 ` [Bug c/43774] " jakub at gcc dot gnu dot org
@ 2010-04-17  8:12 ` dirtysalt1987 at gmail dot com
  2010-04-17  9:45 ` dirtysalt1987 at gmail dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: dirtysalt1987 at gmail dot com @ 2010-04-17  8:12 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from dirtysalt1987 at gmail dot com  2010-04-17 08:11 -------
(In reply to comment #1)
> You should make the struct packed, because otherwise you are accessing it
> unaligned.
>

I think the main problem coming from that GCC replace the 'strlen' function
with the following code.

.L2:
        movl    (%ecx), %eax //$ecx is the beginning of string..
        addl    $4, %ecx //seems to access the content in 4 bytes each time.
        leal    -16843009(%eax), %edx
        notl    %eax
        andl    %eax, %edx
        andl    $-2139062144, %edx
        je      .L2

But it seems the piece of code can only be used when the valid memory size is
4*N,otherwise it will make a 'Invalid read'...

So under what sitaution, GCC will replace 'strlen' call with the above code. 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43774


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

* [Bug c/43774] option -O2 generates wrong assembly code
  2010-04-17  3:33 [Bug c/43774] New: option -O2 generates wrong assembly code dirtysalt1987 at gmail dot com
  2010-04-17  6:55 ` [Bug c/43774] " jakub at gcc dot gnu dot org
  2010-04-17  8:12 ` dirtysalt1987 at gmail dot com
@ 2010-04-17  9:45 ` dirtysalt1987 at gmail dot com
  2010-04-17  9:49 ` schwab at linux-m68k dot org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: dirtysalt1987 at gmail dot com @ 2010-04-17  9:45 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from dirtysalt1987 at gmail dot com  2010-04-17 09:45 -------
(In reply to comment #1)
> You should make the struct packed, because otherwise you are accessing it
> unaligned.
> 

If I recompile it with -O0, the assembly code call 'strlen' function.And I use
Valgrind to run it,there is no 'Invalid read'


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43774


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

* [Bug c/43774] option -O2 generates wrong assembly code
  2010-04-17  3:33 [Bug c/43774] New: option -O2 generates wrong assembly code dirtysalt1987 at gmail dot com
                   ` (2 preceding siblings ...)
  2010-04-17  9:45 ` dirtysalt1987 at gmail dot com
@ 2010-04-17  9:49 ` schwab at linux-m68k dot org
  2010-04-17 10:28 ` dirtysalt1987 at gmail dot com
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: schwab at linux-m68k dot org @ 2010-04-17  9:49 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from schwab at linux-m68k dot org  2010-04-17 09:49 -------
The compiler can assume that *x is correctly aligned, so this is not a bug.


-- 

schwab at linux-m68k dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43774


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

* [Bug c/43774] option -O2 generates wrong assembly code
  2010-04-17  3:33 [Bug c/43774] New: option -O2 generates wrong assembly code dirtysalt1987 at gmail dot com
                   ` (3 preceding siblings ...)
  2010-04-17  9:49 ` schwab at linux-m68k dot org
@ 2010-04-17 10:28 ` dirtysalt1987 at gmail dot com
  2010-04-19  1:09 ` pinskia at gcc dot gnu dot org
  2010-04-19  2:25 ` dirtysalt1987 at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: dirtysalt1987 at gmail dot com @ 2010-04-17 10:28 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from dirtysalt1987 at gmail dot com  2010-04-17 10:28 -------
(In reply to comment #1)
> You should make the struct packed, because otherwise you are accessing it
> unaligned.
> 

3ku for your reply.Your reply is much more useful than Axxx's reply.3ku very
much:).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43774


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

* [Bug c/43774] option -O2 generates wrong assembly code
  2010-04-17  3:33 [Bug c/43774] New: option -O2 generates wrong assembly code dirtysalt1987 at gmail dot com
                   ` (4 preceding siblings ...)
  2010-04-17 10:28 ` dirtysalt1987 at gmail dot com
@ 2010-04-19  1:09 ` pinskia at gcc dot gnu dot org
  2010-04-19  2:25 ` dirtysalt1987 at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-04-19  1:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from pinskia at gcc dot gnu dot org  2010-04-19 01:08 -------
Just a littke more: alignof (struct _x_t) is 4 therefor so is the alignof(_x_t
::data).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43774


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

* [Bug c/43774] option -O2 generates wrong assembly code
  2010-04-17  3:33 [Bug c/43774] New: option -O2 generates wrong assembly code dirtysalt1987 at gmail dot com
                   ` (5 preceding siblings ...)
  2010-04-19  1:09 ` pinskia at gcc dot gnu dot org
@ 2010-04-19  2:25 ` dirtysalt1987 at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: dirtysalt1987 at gmail dot com @ 2010-04-19  2:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from dirtysalt1987 at gmail dot com  2010-04-19 02:25 -------
(In reply to comment #6)
> Just a littke more: alignof (struct _x_t) is 4 therefor so is the alignof(_x_t
> ::data).
> 
3ku very much,with your explanation I think I can understand it..3ku very much.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43774


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

end of thread, other threads:[~2010-04-19  2:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-17  3:33 [Bug c/43774] New: option -O2 generates wrong assembly code dirtysalt1987 at gmail dot com
2010-04-17  6:55 ` [Bug c/43774] " jakub at gcc dot gnu dot org
2010-04-17  8:12 ` dirtysalt1987 at gmail dot com
2010-04-17  9:45 ` dirtysalt1987 at gmail dot com
2010-04-17  9:49 ` schwab at linux-m68k dot org
2010-04-17 10:28 ` dirtysalt1987 at gmail dot com
2010-04-19  1:09 ` pinskia at gcc dot gnu dot org
2010-04-19  2:25 ` dirtysalt1987 at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).