public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/13079] New: Function calls overwrite stack data
@ 2003-11-16 21:44 bmills at andrew dot cmu dot edu
  2003-11-16 22:48 ` [Bug c/13079] " pinskia at gcc dot gnu dot org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: bmills at andrew dot cmu dot edu @ 2003-11-16 21:44 UTC (permalink / raw)
  To: gcc-bugs

Function calls behave incorrectly in the presence of inline assembly;
specifically, they are not flagged as relying on the value of the stack pointer.
See the minimal test-case below for details.

Bryan Mills

========================

void foo(int bar)
{
}

int main()
{
    int returnCode;
    
    foo(-1); /* not broken */
    asm volatile("pushl $0" : : : "esp"); /* put a 0 on the stack */
    foo(-1); /* broken -- should not overwrite value just placed on stack */
    asm volatile("popl %0" : "=r" (returnCode) : : "esp"); /* should be 0,
actually 1 */
    
    exit(returnCode);
}

-- 
           Summary: Function calls overwrite stack data
           Product: gcc
           Version: 3.2.1
            Status: UNCONFIRMED
          Severity: critical
          Priority: P2
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: bmills at andrew dot cmu dot edu
                CC: gcc-bugs at gcc dot gnu dot org


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


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

* [Bug c/13079] Function calls overwrite stack data
  2003-11-16 21:44 [Bug c/13079] New: Function calls overwrite stack data bmills at andrew dot cmu dot edu
@ 2003-11-16 22:48 ` pinskia at gcc dot gnu dot org
  2003-11-16 22:55 ` [Bug inline-asm/13079] " pinskia at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2003-11-16 22:48 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2003-11-16 22:48 -------
The problem is if you want asm, use asm, do not use both and expect GCC will get it right every 
time.

-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|critical                    |normal


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


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

* [Bug inline-asm/13079] Function calls overwrite stack data
  2003-11-16 21:44 [Bug c/13079] New: Function calls overwrite stack data bmills at andrew dot cmu dot edu
  2003-11-16 22:48 ` [Bug c/13079] " pinskia at gcc dot gnu dot org
@ 2003-11-16 22:55 ` pinskia at gcc dot gnu dot org
  2003-11-16 23:04 ` pinskia at gcc dot gnu dot org
  2003-11-16 23:11 ` bmills at andrew dot cmu dot edu
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2003-11-16 22:55 UTC (permalink / raw)
  To: gcc-bugs



-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |inline-asm


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


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

* [Bug inline-asm/13079] Function calls overwrite stack data
  2003-11-16 21:44 [Bug c/13079] New: Function calls overwrite stack data bmills at andrew dot cmu dot edu
  2003-11-16 22:48 ` [Bug c/13079] " pinskia at gcc dot gnu dot org
  2003-11-16 22:55 ` [Bug inline-asm/13079] " pinskia at gcc dot gnu dot org
@ 2003-11-16 23:04 ` pinskia at gcc dot gnu dot org
  2003-11-16 23:11 ` bmills at andrew dot cmu dot edu
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2003-11-16 23:04 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pinskia at gcc dot gnu dot org  2003-11-16 23:04 -------
GCC should really error out because you cannot do this as GCC is not a direct C to asm compiler 
but rather an optimizing one and that you thing you do to try to trick GCC will just come back and 
haunt you like this.
Also this is a dup of bug 11807.

*** This bug has been marked as a duplicate of 11807 ***

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


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


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

* [Bug inline-asm/13079] Function calls overwrite stack data
  2003-11-16 21:44 [Bug c/13079] New: Function calls overwrite stack data bmills at andrew dot cmu dot edu
                   ` (2 preceding siblings ...)
  2003-11-16 23:04 ` pinskia at gcc dot gnu dot org
@ 2003-11-16 23:11 ` bmills at andrew dot cmu dot edu
  3 siblings, 0 replies; 5+ messages in thread
From: bmills at andrew dot cmu dot edu @ 2003-11-16 23:11 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From bmills at andrew dot cmu dot edu  2003-11-16 23:11 -------
Correct compilation of code is something that programmers need to be able to
rely on.  IF GCC claims to support inline assembly then it should support it in
a robust and bug-free manner, ensuring that the program will compile correctly
if the programmer has given GCC appropriate information about what they are
doing (in this case, clobbering the stack pointer, as indicated in the code).

There are clearly only two reasonable things GCC could do in this instance --
either put the stack pointer back where it was before making the function call,
or advance the stack pointer before writing the function argument.  Either is
arguably correct behaviour; however, the current behaviour is not.

If I wanted to use only high-level language constructs, I certainly wouldn't be
writing in C.  I'd write in a safer, higher-level language.  The whole point of
C is to be able to write in something that can access the machine at a very low
level and do so correctly.  Certainly, I could write my code in assembly -- but,
by that argument, GCC and C are both useless, because I "should" just write all
of my low-level code in assembly and write the rest in some higher-level language.

"That's not what it was designed for" has never been a valid argument in the
development of computers; I suspect it never will be.  If a specification
exists, it should be followed, and if uncommon cases exist they must still
generate correct code, even if it may cause inefficient code in other cases. 
Programmers need to be able to trust their compilers to generate correct code
regardless of whether they are using common features or uncommon ones. 
Optimization *must* preserve correctness of code, or it is useless.  The error
here is not the use of assembly code or the clobbering of the stack pointer, but
rather GCC's utter failure to realize that function calls depend on that value.

If the optimizer has parts that can't handle inline assembly, it shouldn't be
trying to optimize through the assembly.  Also, this is clearly not a bug
related to the fact that GCC is an optimizing compiler, because it occurs with -O0.

Moreover, a bug in the calling convention in the presence of inline assembly may
be indicative of a much larger bug in register allocation relating to function
calls that manifests itself in a much more subtle way.  If you want to just
dismiss it by saying "inline assembly is broken", you also have to add "function
calls are broken" and "stack allocation is broken", which are certainly not to
be taken lightly.

-- 


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


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

end of thread, other threads:[~2003-11-16 23:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-11-16 21:44 [Bug c/13079] New: Function calls overwrite stack data bmills at andrew dot cmu dot edu
2003-11-16 22:48 ` [Bug c/13079] " pinskia at gcc dot gnu dot org
2003-11-16 22:55 ` [Bug inline-asm/13079] " pinskia at gcc dot gnu dot org
2003-11-16 23:04 ` pinskia at gcc dot gnu dot org
2003-11-16 23:11 ` bmills at andrew dot cmu dot edu

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