This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Further observations regarding alloca on i586-pc-linux-gnu


| Joerg Pommnitz <pommnitz@darmstadt.gmd.de> writes:
| 
| > While the following code dies with a segmentation violation
| 
| > #include <cstdlib>
| > #include <iostream>
| > class xx { public: char *xy (char *c = alloca (18)) {
| >                 strcpy (c, "Hello World!"); return c; } };
| > int main () { xx x; cout << x.xy () << endl; }
| 
| > Is this a bug? I think yes, but I'm not sure whether case #1
| > is supposed to work.
| 
| IMO, it should work on x86, as it does on sparc and alpha.  The
| problem is that the stack pointer is moved after pushing default
| arguments onto the stack.
| 
| Unfortunately, I don't have the (time required to develop) skills
| needed to fix it, so I'll leave this for someone else.
| 
| Anyway, next time you report a problem, please clearly state on which
| platform you have encountered it, and which compiler options you have
| used.  It took me some time to find out the problem would only occur
| on x86 without optimization!

After a little bit of experimenting, I found what is happening:

  cout << alloca (20) << '\n';

becomes:

???     pushl $10		# put '\n' on the stack
        addl $-20,%esp		# allocate 20 bytes on the stack
        movl %esp,%eax
        pushl %eax		# write the result of alloca() to cout
        pushl $cout
.LCFI2:
        call __ls__7ostreamPCv
        addl $8,%esp		# correct last two pushes.
        movl %eax,%eax		# put return value `cout' on the stack:
        pushl %eax
        call __ls__7ostreamc	# Try to write '\n' (fails, stack pointer wrong).
        addl $8,%esp		# "correct" wrongly stack for push $10 and last push.


Correct would be:

        addl $-20,%esp		# allocate 20 bytes on the stack
        movl %esp,%eax
!!!     pushl $10		# put '\n' on the stack
        pushl %eax		# write the result of alloca() to cout
..etc

In other words: This is a bug.

-- 
 Carlo Wood  <carlo@runaway.xs4all.nl>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]