Bug 12820 – DMD can inline calls to functions that use alloca, allocating the memory in the caller function instead.

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-05-29T22:15:00Z
Last change time
2014-07-25T04:14:47Z
Keywords
pull, wrong-code
Assigned to
yebblies
Creator
opantm2+dbugs

Comments

Comment #0 by opantm2+dbugs — 2014-05-29T22:15:52Z
Functions that use 'alloca' shouldn't be allowed to be inlined because then memory is allocated in the stack of the caller function. This causes memory to not be freed when expected. Example: import core.stdc.stdlib, std.conv, std.stdio; class C { } void foo() { enum size = __traits(classInstanceSize, C); void[] mem = alloca(size)[0..size]; emplace!C(mem); } void main() { foreach(i; 0 .. 10_000_000) { foo(); } writeln("Done!"); } With -inline: rdmd -O -release -inline test2.d Segmentation fault Without -inline: rdmd -O -release test2.d Done! This problem does not occur in GDC.
Comment #1 by blah38621 — 2014-05-30T15:04:28Z
I believe that the issue here is not that DMD inlines alloca (which it should) but instead that it fails to recognize that the memory allocated by alloca should be released at the end of the scope's body. Another issue that could be considered to fix this exact code is that the alloca call there has a size known at compile-time, and so should actually be getting eliminated entirely in favor of a static array local.
Comment #2 by yebblies — 2014-07-24T16:24:08Z
Comment #3 by yebblies — 2014-07-24T16:26:37Z
(In reply to Orvid King from comment #1) > I believe that the issue here is not that DMD inlines alloca (which it > should) > but instead that it fails to recognize that the memory allocated by alloca > should be released at the end of the scope's body. alloca is defined to release memory at the end of the calling function. Releasing memory early would require a different variant of alloca, and while possible, this is difficult. Since the inliner can't do this conversion, it shouldn't be inlining alloca.
Comment #4 by github-bugzilla — 2014-07-25T04:14:45Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/292828b19a8ad42a48794292db2646cff6536089 Fix Issue 12820 - DMD can inline calls to functions that use alloca, allocating the memory in the caller function instead.