D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5653 - Allocating in a destructor called during a GC corrupts memory
Summary: Allocating in a destructor called during a GC corrupts memory
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P2 critical
Assignee: No Owner
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2011-02-25 19:02 UTC by Vladimir Panteleev
Modified: 2015-06-09 05:11 UTC (History)
3 users (show)

See Also:


Attachments
Very simple patch against D1's gcx.d which throws OutofMemory when allocating during a GC run (1.79 KB, patch)
2011-02-25 19:02 UTC, Vladimir Panteleev
Details | Diff
Memory corruption test (446 bytes, application/octet-stream)
2011-05-13 04:01 UTC, Vladimir Panteleev
Details
Naive test for disallowing GC interaction after a finalizer exception (721 bytes, application/octet-stream)
2011-05-13 04:03 UTC, Vladimir Panteleev
Details

Note You need to log in before you can comment on or make changes to this issue.
Description Vladimir Panteleev 2011-02-25 19:02:00 UTC
Created attachment 922 [details]
Very simple patch against D1's gcx.d which throws OutofMemory when allocating during a GC run

D's current garbage collector is completely unprepared to handle an allocation
which is called by a finalizer. Such an allocation puts D's GC into an
inconsistent state, which ultimately leads to memory corruption.

The GC should either forbid allocating in destructors (by throwing an
exception), or properly support it (which may be non-trivial).

If the first solution is chosen, it should be noted that there are instances of
allocations in destructors in Phobos as well (such as std.zlib).
Comment 1 Vladimir Panteleev 2011-05-13 04:01:57 UTC
Created attachment 971 [details]
Memory corruption test
Comment 2 Vladimir Panteleev 2011-05-13 04:03:22 UTC
Created attachment 972 [details]
Naive test for disallowing GC interaction after a finalizer exception
Comment 4 Andrei Alexandrescu 2011-05-25 23:00:17 UTC
Fixed here: https://github.com/D-Programming-Language/phobos/pull/44 Does that take care of D1 too?
Comment 5 Vladimir Panteleev 2011-05-26 03:35:29 UTC
(In reply to comment #4)
> Fixed here: https://github.com/D-Programming-Language/phobos/pull/44 Does that
> take care of D1 too?

The Phobos pull request is for D1, the druntime pull request is for D2. Thanks!
Comment 6 Sean Kelly 2011-06-17 10:54:59 UTC
Note that this patch will cause all successive allocations by the process to generate an OOME, since gcx.running will be true forever.  This may be a good stopgap fix, but ultimately the GC has to support allocations inside a finalizer.  The best approach is probably to effectively disable the GC when it's running so an allocating finalizer would simply create a new Pool if no memory was available.  It looks like the collect routine still needs to be rewritten with this in mind, however.
Comment 7 Vladimir Panteleev 2011-06-18 04:39:19 UTC
(In reply to comment #6)
> Note that this patch will cause all successive allocations by the process to
> generate an OOME, since gcx.running will be true forever.

Yes, this is by design until someone comes up with something better.