www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8658] New: Passing large structs to function b value causes stack corruption

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8658

           Summary: Passing large structs to function b value causes stack
                    corruption
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: r.sagitario gmx.de


--- Comment #0 from Rainer Schuetze <r.sagitario gmx.de> 2012-09-14 01:41:33
PDT ---
If the arguments passed to a function exceed 64kB the stack gets corrupted.
Compiling and running this code without optimizations leads to a crash:

struct S
{
    int[16385] a;
}

void foo(S s)
{
}

void main()
{
    S s;
    for(int i = 0; i < 100; i++)
        foo(s);
}

This is caused by the frame pointer cleanup only popping the lower 16 bit of
the used stack size:

_D4test3fooFS4test1SZv  comdat
        assume  CS:_D4test3fooFS4test1SZv
                ret     4
_D4test3fooFS4test1SZv  ends

[Actually I never meant to do this, it happened because I thought I was passing
a class reference.]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 14 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8658


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim maxim-fomin.ru


--- Comment #1 from Maxim Fomin <maxim maxim-fomin.ru> 2012-09-14 09:19:36 PDT
---
(In reply to comment #0)
 If the arguments passed to a function exceed 64kB the stack gets corrupted.
 Compiling and running this code without optimizations leads to a crash:
 
<snip> Couple of comments: 1) This program doesn't crash in x64 linux 2) what exactly is requested: you allocate data on stack which certainly big enough to corrupt it, so? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 14 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8658


Rainer Schuetze <r.sagitario gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Platform|All                         |x86
         OS/Version|All                         |Windows


--- Comment #2 from Rainer Schuetze <r.sagitario gmx.de> 2012-09-14 09:28:26
PDT ---
Sorry, I didn't specify the platform: Windows 32-bit. I don't know if it
happens elsewhere. 
What happens in the example is that each call pushes 16385 words onto the
stack, but only 1 gets popped. 
For x64, the code might be different and the stack might be larger.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 14 2012