Bug 20214 – GC: realloc can result in false pointers if address doesn't change
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-09-14T16:55:04Z
Last change time
2019-09-16T01:27:32Z
Keywords
pull
Assigned to
No Owner
Creator
Rainer Schuetze
Comments
Comment #0 by r.sagitario — 2019-09-14T16:55:04Z
If GC.realloc doesn't change the address of the allocation (either by shrinking or extending in place), the non-allocated area can contain false pointers:
import core.memory;
import core.stdc.stdio;
void main()
{
// allocate from large pool
auto o = GC.malloc(10);
auto p = (cast(void**)GC.malloc(4096 * (void*).sizeof))[0 .. 4096];
auto q = (cast(void**)GC.malloc(4096 * (void*).sizeof))[0 .. 4096];
if (p.ptr + p.length is q.ptr)
{
q[] = o; // fill with pointers
// shrink, unused area cleared?
auto nq = (cast(void**)GC.realloc(q.ptr, 4000 * (void*).sizeof))[0 .. 4000];
assert(q.ptr is nq.ptr);
assert(q.ptr[4095] !is o);
GC.free(q.ptr);
// expected to extend in place
auto np = (cast(void**)GC.realloc(p.ptr, 4200 * (void*).sizeof))[0 .. 4200];
assert(p.ptr is np.ptr);
assert(q.ptr[4200] !is o);
}
else
printf("unexpected pointers %p and %p\n", p.ptr, q.ptr);
}
Comment #1 by dlang-bot — 2019-09-14T17:02:37Z
@rainers created dlang/druntime pull request #2798 "fix Issue 20214 - GC: realloc can result in false pointers if address…" fixing this issue:
- fix Issue 20214 - GC: realloc can result in false pointers if address doesn't change
clear additional memory also when shrinking or extending in place
https://github.com/dlang/druntime/pull/2798
Comment #2 by dlang-bot — 2019-09-16T01:27:32Z
dlang/druntime pull request #2798 "fix Issue 20214 - GC: realloc can result in false pointers if address…" was merged into master:
- 3223f9846701031f2945df702df453501aeb5a13 by Rainer Schuetze:
fix Issue 20214 - GC: realloc can result in false pointers if address doesn't change
clear additional memory also when shrinking or extending in place
https://github.com/dlang/druntime/pull/2798