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

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).