D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11393 - [GC] GC realloc and free don't ignore interior pointers
Summary: [GC] GC realloc and free don't ignore interior pointers
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: druntime (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2013-10-30 18:29 UTC by safety0ff.bugz
Modified: 2020-07-05 10:53 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description safety0ff.bugz 2013-10-30 18:29:45 UTC
The documentation / spec [1][2] states that realloc and free should do nothing when they're passed interior pointers.

Currently realloc / free still operate on the underlying memory blocks regardless.

I started working on the fix for realloc but decided that it should really be discussed before spending more time on it.

Discuss :)

----
[1] http://dlang.org/phobos/core_memory.html#.GC.realloc
[2] http://dlang.org/phobos/core_memory.html#.GC.free
Comment 1 safety0ff.bugz 2013-10-30 18:41:25 UTC
I suppose this should have been spit into two bugs, but for now we can keep the discussion focused.

The reason fixing realloc requires discussion is that fixing this may break exiting code in the case where it is passed an interior pointer with a non zero size parameter.

Fixing the case where the pointer is internal and either realloc is called with size zero or free is called does not break existing code unless the GC is disabled, where it will cause memory leaks.
Comment 2 safety0ff.bugz 2013-11-03 11:25:28 UTC
Discussion thread: http://forum.dlang.org/thread/qmiayckycycetnumulbn@forum.dlang.org
Comment 3 Rainer Schuetze 2019-01-03 12:55:40 UTC
https://github.com/dlang/druntime/pull/2436
Comment 4 github-bugzilla 2019-01-04 02:56:58 UTC
Commits pushed to master at https://github.com/dlang/druntime

https://github.com/dlang/druntime/commit/e6087c2fed9e49daaa72296bd951ea56efe82ec7
fix Issue 11393,11446 - [GC] GC realloc and free don't ignore interior/non-GC owned pointers

change specification of GC.realloc to something more robust:

- return null for failure (returning the original pointer is no indication of failure as it might also be returned on success. This can easily lead to overwriting memory)
- allow freeing old memory immediately (previous implementation did this for large objects when shrinking)
- only set attribute bits on existing block if reused (previous implementation didn't set bits if it reused a small block)

refactor realloc to match that spec, add changelog, fix example

https://github.com/dlang/druntime/commit/c1c0a85f5904953b98b2e1b188a650576b83da82
Merge pull request #2436 from rainers/gc_realloc

fix Issue 11393 - [GC] GC realloc and free don't ignore interior pointers
merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
Comment 5 Dlang Bot 2020-07-05 10:53:34 UTC
dlang/druntime pull request #3149 "fix test for issue 11393 introduced by #2436" was merged into master:

- d247d9636ca568b37d7995a253e525b41b6e2d38 by Rainer Schuetze:
  fix test for issue 11393

https://github.com/dlang/druntime/pull/3149