Project Home
Project Home
Source Code
Source Code
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Compiler Bug ?: (6 Items)
   
Compiler Bug ?  
I have a feeling the following is a compiler bug (6.5.0):

#include <string.h>

struct Second {
	char dummy[1000000];
};

struct First {
	struct Second second;
};
	
struct Second test;

void foo( struct First *val ) {
	struct Second *sec = &val->second;
	memcpy( &test, sec, sizeof ( struct Second ) );
}

When compile with no optimisation it looks like:

	pushl	%ebp
	movl	%esp, %ebp
	subl	$40, %esp
	movl	8(%ebp), %eax
	movl	%eax, -12(%ebp)
	movl	$1000000, 8(%esp)
	movl	-12(%ebp), %eax
	movl	%eax, 4(%esp)
	movl	$test, (%esp)
	call	memcpy
	leave
	ret

When compile with -O

	pushl	%ebp
	movl	%esp, %ebp
	pushl	%ebx
>>	subl	$1000020, %esp
	leal	-1000008(%ebp), %ebx
	movl	$1000000, 8(%esp)
	movl	8(%ebp), %eax
	movl	%eax, 4(%esp)
	movl	%ebx, (%esp)
	call	memcpy
	movl	$1000000, 8(%esp)
	movl	%ebx, 4(%esp)
	movl	$test, (%esp)
	call	memcpy
	addl	$1000020, %esp
	popl	%ebx
	popl	%ebp
	ret

This is BAD, 1M is allocated on the stack, and the data referenced by *sec is first copied onto the stack before being 
copy by the memcpy in the C code.  Of course the program crashed because the stack isn't big enough. Even if the stack 
would be big enough that's some major overhead ...

I tried to figure out the -f option that trigger this behavior ( by getting the list via --help=optimizers ) but no luck
.

If I changed the file extention from c to cpp to get it to compile with g++ the "bug" is no there.





Re: Compiler Bug ?  
Out of curiousity, would

-fconserve-stack

change things?



On Thu, 2010-10-21 at 14:12 -0400, Mario Charest wrote:
> I have a feeling the following is a compiler bug (6.5.0):
> 
> #include <string.h>
> 
> struct Second {
>         char dummy[1000000];
> };
> 
> struct First {
>         struct Second second;
> };
>        
> struct Second test;
> 
> void foo( struct First *val ) {
>         struct Second *sec = &val->second;
>         memcpy( &test, sec, sizeof ( struct Second ) );
> }
> 
> When compile with no optimisation it looks like:
> 
>         pushl   %ebp
>         movl    %esp, %ebp
>         subl    $40, %esp
>         movl    8(%ebp), %eax
>         movl    %eax, -12(%ebp)
>         movl    $1000000, 8(%esp)
>         movl    -12(%ebp), %eax
>         movl    %eax, 4(%esp)
>         movl    $test, (%esp)
>         call    memcpy
>         leave
>         ret
> 
> When compile with -O
> 
>         pushl   %ebp
>         movl    %esp, %ebp
>         pushl   %ebx
> >>      subl    $1000020, %esp
>         leal    -1000008(%ebp), %ebx
>         movl    $1000000, 8(%esp)
>         movl    8(%ebp), %eax
>         movl    %eax, 4(%esp)
>         movl    %ebx, (%esp)
>         call    memcpy
>         movl    $1000000, 8(%esp)
>         movl    %ebx, 4(%esp)
>         movl    $test, (%esp)
>         call    memcpy
>         addl    $1000020, %esp
>         popl    %ebx
>         popl    %ebp
>         ret
> 
> This is BAD, 1M is allocated on the stack, and the data referenced by
> *sec is first copied onto the stack before being copy by the memcpy in
> the C code.  Of course the program crashed because the stack isn't big
> enough. Even if the stack would be big enough that's some major
> overhead ...
> 
> I tried to figure out the -f option that trigger this behavior ( by
> getting the list via --help=optimizers ) but no luck.
> 
> If I changed the file extention from c to cpp to get it to compile
> with g++ the "bug" is no there.
> 
> 
> 
> 
> 
> 
> 
> 
> 
> _______________________________________________
> 
> General
> http://community.qnx.com/sf/go/post71838
> 
> 
> 

RE: Compiler Bug ?  

> -----Message d'origine-----
> De : Aleksandar Ristovski [mailto:community-noreply@qnx.com]
> Envoyé : 21 octobre 2010 14:28
> À : general-toolchain
> Objet : Re: Compiler Bug ?
> 
> Out of curiousity, would
> 
> -fconserve-stack
> 
> change things?

No change, the generated assembly code is identical.

> 
> 
> 
> On Thu, 2010-10-21 at 14:12 -0400, Mario Charest wrote:
> > I have a feeling the following is a compiler bug (6.5.0):
> >
> > #include <string.h>
> >
> > struct Second {
> >         char dummy[1000000];
> > };
> >
> > struct First {
> >         struct Second second;
> > };
> >
> > struct Second test;
> >
> > void foo( struct First *val ) {
> >         struct Second *sec = &val->second;
> >         memcpy( &test, sec, sizeof ( struct Second ) ); }
> >
> > When compile with no optimisation it looks like:
> >
> >         pushl   %ebp
> >         movl    %esp, %ebp
> >         subl    $40, %esp
> >         movl    8(%ebp), %eax
> >         movl    %eax, -12(%ebp)
> >         movl    $1000000, 8(%esp)
> >         movl    -12(%ebp), %eax
> >         movl    %eax, 4(%esp)
> >         movl    $test, (%esp)
> >         call    memcpy
> >         leave
> >         ret
> >
> > When compile with -O
> >
> >         pushl   %ebp
> >         movl    %esp, %ebp
> >         pushl   %ebx
> > >>      subl    $1000020, %esp
> >         leal    -1000008(%ebp), %ebx
> >         movl    $1000000, 8(%esp)
> >         movl    8(%ebp), %eax
> >         movl    %eax, 4(%esp)
> >         movl    %ebx, (%esp)
> >         call    memcpy
> >         movl    $1000000, 8(%esp)
> >         movl    %ebx, 4(%esp)
> >         movl    $test, (%esp)
> >         call    memcpy
> >         addl    $1000020, %esp
> >         popl    %ebx
> >         popl    %ebp
> >         ret
> >
> > This is BAD, 1M is allocated on the stack, and the data referenced by
> > *sec is first copied onto the stack before being copy by the memcpy in
> > the C code.  Of course the program crashed because the stack isn't big
> > enough. Even if the stack would be big enough that's some major
> > overhead ...
> >
> > I tried to figure out the -f option that trigger this behavior ( by
> > getting the list via --help=optimizers ) but no luck.
> >
> > If I changed the file extention from c to cpp to get it to compile
> > with g++ the "bug" is no there.
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > _______________________________________________
> >
> > General
> > http://community.qnx.com/sf/go/post71838
> >
> >
> >
> 
> 
> 
> 
> 
> _______________________________________________
> 
> General
> http://community.qnx.com/sf/go/post71842
> 
Re: Compiler Bug ?  
No comments, anyone?  This sound to me like a very nasty bug.

> I have a feeling the following is a compiler bug (6.5.0):
> 
> #include <string.h>
> 
> struct Second {
> 	char dummy[1000000];
> };
> 
> struct First {
> 	struct Second second;
> };
> 	
> struct Second test;
> 
> void foo( struct First *val ) {
> 	struct Second *sec = &val->second;
> 	memcpy( &test, sec, sizeof ( struct Second ) );
> }
> 
> When compile with no optimisation it looks like:
> 
> 	pushl	%ebp
> 	movl	%esp, %ebp
> 	subl	$40, %esp
> 	movl	8(%ebp), %eax
> 	movl	%eax, -12(%ebp)
> 	movl	$1000000, 8(%esp)
> 	movl	-12(%ebp), %eax
> 	movl	%eax, 4(%esp)
> 	movl	$test, (%esp)
> 	call	memcpy
> 	leave
> 	ret
> 
> When compile with -O
> 
> 	pushl	%ebp
> 	movl	%esp, %ebp
> 	pushl	%ebx
> >>	subl	$1000020, %esp
> 	leal	-1000008(%ebp), %ebx
> 	movl	$1000000, 8(%esp)
> 	movl	8(%ebp), %eax
> 	movl	%eax, 4(%esp)
> 	movl	%ebx, (%esp)
> 	call	memcpy
> 	movl	$1000000, 8(%esp)
> 	movl	%ebx, 4(%esp)
> 	movl	$test, (%esp)
> 	call	memcpy
> 	addl	$1000020, %esp
> 	popl	%ebx
> 	popl	%ebp
> 	ret
> 
> This is BAD, 1M is allocated on the stack, and the data referenced by *sec is 
> first copied onto the stack before being copy by the memcpy in the C code.  Of
>  course the program crashed because the stack isn't big enough. Even if the 
> stack would be big enough that's some major overhead ...
> 
> I tried to figure out the -f option that trigger this behavior ( by getting 
> the list via --help=optimizers ) but no luck.
> 
> If I changed the file extention from c to cpp to get it to compile with g++ 
> the "bug" is no there.
> 
> 
> 
> 
> 


Re: Compiler Bug ?  
> No comments, anyone?  This sound to me like a very nasty bug.

Why can't you allocate that structure dynamically with malloc? Having large objects in data segment is bad as is a bad 
style to have global static variables in general.
Re: Compiler Bug ?  
> > No comments, anyone?  This sound to me like a very nasty bug.
> 
> Why can't you allocate that structure dynamically with malloc? Having large 
> objects in data segment is bad as is a bad style to have global static 
> variables in general.

You totally missed the point.  This is a test case to show a compiler bug.   I should not have to modifidy my code to go
 around a compiler bug however bad the code is. Whether an object is statly or dynamically allocated they all go in the 
data segment.  That being said the issue is NOT with how the variable test is instanciated, in fact try it with a malloc
 and you will that nothing changes.   The bug is with this line:

struct Second *sec = &val->second;

Although  this should only copy 4 bytes; the address of val->second, it actually copies the content of val->second onto 
the STACK!!!!  Not only does it increase the likely hood of stack overwflow but it hurt performances.