www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3822] New: alloca() can return the same address inside a function

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

           Summary: alloca() can return the same address inside a function
           Product: D
           Version: 2.040
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: druntime
        AssignedTo: sean invisibleduck.org
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2010-02-18 11:17:11 PST ---
import std.stdio: printf;
import std.c.stdlib: alloca;
void main() {
    const int n = 8;
    for (int i; i < 2; i++)
        printf("%p\n", alloca(n));
}

It prints two times the same address, I don't know why, I think this can be
wrong.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 18 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3822


BCS <shro8822 vandals.uidaho.edu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |shro8822 vandals.uidaho.edu


--- Comment #1 from BCS <shro8822 vandals.uidaho.edu> 2010-02-18 12:03:35 PST
---
I've never used alloca so I'm not sure, so this is a guess:

alloca does stack allocation and the body of the for statement forms a scope on
the stack (this in this case contains no named variables). I'm guessing that
when that scope is exited, the allocation automatically gets deallocated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3822



--- Comment #2 from bearophile_hugs eml.cc 2010-02-18 12:32:38 PST ---
(In reply to comment #1)
 I've never used alloca so I'm not sure, so this is a guess:
 
 alloca does stack allocation and the body of the for statement forms a scope on
 the stack (this in this case contains no named variables). I'm guessing that
 when that scope is exited, the allocation automatically gets deallocated.
You can be right, thank you. Then it's very good for Phobos docs to say that alloca is relative to a scope and not to a function. The description of alloca() that I have seen says: The alloca() function allocates space in the stack frame of the caller, and returns a pointer to the allocated block. This temporary space is automatically freed when the function from which alloca() is called returns. While if you are right D alloca frees space when the scope of alloca ends and not when the function ends. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 18 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3822



--- Comment #3 from bearophile_hugs eml.cc 2010-06-07 04:04:04 PDT ---
Maybe the alloca() used by dmd frees memory as soon as the current scope is
left, instead of deferring all deallocation until function exit. See:
http://compilers.iecc.com/comparch/article/91-12-079

D documentation has to explain how exactly its alloca() works.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3822


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |nfxjfg gmail.com
            Summary|alloca() can return the     |Memory allocated with
                   |same address inside a       |alloca() is freed at end of
                   |function                    |scope instead at end of
                   |                            |function


--- Comment #4 from nfxjfg gmail.com 2010-06-07 04:17:53 PDT ---
C code that compiles in D without modification should work exactly as it does
in C.
This means this is a rather bad code gen bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 07 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3822


nfxjfg gmail.com changed:

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


--- Comment #5 from nfxjfg gmail.com 2010-06-26 14:40:47 PDT ---
There's D code in druntime that assumes memory allocated by alloca() is valid
until the end of the function:
http://dsource.org/projects/druntime/browser/trunk/src/rt/adi.d#L242
Maybe the codegen for alloca() within loops is broken, or something.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 26 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3822


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies gmail.com
           Platform|Other                       |All
            Version|2.040                       |D1 & D2
         AssignedTo|nobody puremagic.com        |yebblies gmail.com
         OS/Version|Windows                     |All


--- Comment #6 from yebblies <yebblies gmail.com> 2012-02-11 17:08:28 EST ---
The issue here is that n is a compile-time constant, so the call to alloca is
optimized away completely, and always reserving the extra space.  This
optimization is not valid if the call to alloca might be repeated.

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


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull
            Summary|Memory allocated with       |Invalid optimization of
                   |alloca() is freed at end of |alloca called with constant
                   |scope instead at end of     |size
                   |function                    |


--- Comment #7 from yebblies <yebblies gmail.com> 2012-02-11 17:29:13 EST ---
https://github.com/D-Programming-Language/dmd/pull/707

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



--- Comment #8 from bearophile_hugs eml.cc 2012-02-11 04:12:35 PST ---
(In reply to comment #7)
 https://github.com/D-Programming-Language/dmd/pull/707
Thank you for your patch, yebblies! -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 11 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3822



--- Comment #9 from yebblies <yebblies gmail.com> 2012-02-11 23:36:09 EST ---
(In reply to comment #8)
 Thank you for your patch, yebblies!
You're welcome. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 11 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3822



--- Comment #10 from github-bugzilla puremagic.com 2012-02-18 23:17:48 PST ---
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/3fc9ee7064c05d4c8db6f1aac0a8896caa954e44
Merge pull request #707 from yebblies/issue3822

Issue 3822 - Invalid optimization of alloca called with constant size

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



--- Comment #11 from github-bugzilla puremagic.com 2012-02-18 23:44:49 PST ---
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/c492323b6edc44a2bac4ea0edaed1e757854b9d0
fix Issue 3822 - Invalid optimization of alloca called with constant size

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 18 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3822


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


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