Bug 19522 – [GC] GC.query/addrOf/sizeOf fail for freed memory

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-12-30T10:14:56Z
Last change time
2018-12-31T13:14:32Z
Assigned to
No Owner
Creator
Rainer Schuetze

Comments

Comment #0 by r.sagitario — 2018-12-30T10:14:56Z
According to the documentation https://dlang.org/phobos/core_memory.html#.GC.query query functions should return null/0 if the pointer "references memory not originally allocated by this garbage collector". But this program fails a couple of asserts: import core.memory; void main() { void* large = GC.malloc(10000); GC.free(large); assert(GC.query(large).base == null); assert(GC.query(large).size == 0); assert(GC.addrOf(large) == null); assert(GC.sizeOf(large) == 0); // fails void* small = GC.malloc(100); GC.free(small); assert(GC.query(small).base == null); // fails assert(GC.query(small).size == 0); // fails assert(GC.addrOf(small) == null); // fails assert(GC.sizeOf(small) == 0); // fails } GC.getAttr/setAttr/clrAttr have a similar issue, they also don't check for interior pointers. Similar stuff happens if the pointer happens to hit an address within a GC memory pool, with arbitrary return values.
Comment #1 by github-bugzilla — 2018-12-31T13:14:31Z
Commits pushed to master at https://github.com/dlang/druntime https://github.com/dlang/druntime/commit/a44c9bf00e3385247bf6b8356881bd69d07c38e2 fix Issue 19522 - [GC] GC.query/addrOf/sizeOf fail for freed memory keep track of free bits during malloc and free, so that it can be checked when determining pointer validity this also avoids having to "prepare" them during collection https://github.com/dlang/druntime/commit/29e9ac23c3f569a235895181edff1c6e364152b6 Merge pull request #2429 from rainers/gc_freebits fix Issue 19522 - [GC] GC.query/addrOf/sizeOf fail for freed memory merged-on-behalf-of: Nicholas Wilson <[email protected]>