Bug 20155 – Allocating a struct with dtor on the GC heap can produce false pointers

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2019-08-23T09:51:56Z
Last change time
2019-09-07T07:12:50Z
Keywords
pull
Assigned to
No Owner
Creator
Rainer Schuetze

Comments

Comment #0 by r.sagitario — 2019-08-23T09:51:56Z
import core.memory; void main() { // for 64-bit, allocate a struct of size 40 static struct S { size_t[4] data; S* ptr4; } auto p1 = new S; auto p2 = new S; p2.ptr4 = p1; // a struct with a dtor with size 32, but the dtor will cause // allocation to be larger by a pointer static struct A { size_t[3] data; S* ptr3; ~this() {} } GC.free(p2); auto a = new A; // reuse same memory if(cast(void*)a is cast(void*)p2) // reusage not guaranteed { auto ptr = cast(S**)(a + 1); assert(*ptr != p1); // still same data as p2.ptr4? } } The problem is that allocating a struct with a dtor saves a pointer to the type info with the struct, but not within the beginning area of the allocation, but at the end of the actually allocated block. This contradicts assumptions of the GC that only clears the area after the requested size, assuming the rest to be initialized by the caller. Array allocations have a similar issue as they store the requested length at the end of the allocated blcok, but it is less critical because these are only 1 or 2 bytes.
Comment #1 by dlang-bot — 2019-08-23T09:56:29Z
@rainers created dlang/druntime pull request #2755 "Issue 20155 - Allocating a struct with dtor on the GC heap can produce false pointers" mentioning this issue: - Issue 20155 - Allocating a struct with dtor on the GC heap can produce false pointers clear padding area not completely zeroed for struct allocations with dtor https://github.com/dlang/druntime/pull/2755
Comment #2 by dlang-bot — 2019-08-23T09:56:29Z
@rainers updated dlang/druntime pull request #2755 "Issue 20155 - Allocating a struct with dtor on the GC heap can produce false pointers" fixing this issue: - fix issue 20155 - Allocating a struct with dtor on the GC heap can produce false pointers clear padding area not completely zeroed for struct allocations with dtor https://github.com/dlang/druntime/pull/2755
Comment #3 by dlang-bot — 2019-09-07T07:12:50Z
dlang/druntime pull request #2755 "Issue 20155 - Allocating a struct with dtor on the GC heap can produce false pointers" was merged into master: - 62da4de999c62d89a1e6360f65f51b062d2b13b3 by Rainer Schuetze: fix issue 20155 - Allocating a struct with dtor on the GC heap can produce false pointers clear padding area not completely zeroed for struct allocations with dtor https://github.com/dlang/druntime/pull/2755