December 30, 2018
https://issues.dlang.org/show_bug.cgi?id=19522

          Issue ID: 19522
           Summary: [GC] GC.query/addrOf/sizeOf fail for freed memory
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: druntime
          Assignee: nobody@puremagic.com
          Reporter: r.sagitario@gmx.de

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.

--