Page MenuHomeFreeBSD

Remove some compatability with Seventh Edition UNIX realloc().
ClosedPublic

Authored by brooks on Aug 17 2019, 12:00 AM.
Tags
None
Referenced Files
F81574182: D21296.diff
Thu, Apr 18, 8:09 AM
Unknown Object (File)
Tue, Apr 16, 5:30 AM
Unknown Object (File)
Dec 29 2023, 12:23 PM
Unknown Object (File)
Dec 22 2023, 9:46 PM
Unknown Object (File)
Nov 29 2023, 8:11 PM
Unknown Object (File)
Oct 2 2023, 2:29 PM
Unknown Object (File)
Sep 23 2023, 10:49 AM
Unknown Object (File)
Sep 14 2023, 8:31 AM
Subscribers

Details

Summary

In Seventh Edition UNIX, the last pointer passed to free() was
guaranteed to not actually have been freed allowing memory to be
"compacted" via the following pattern:

free(foo);
foo = realloc(foo, newsize);

Further, Andrew Koenig reports in "C Traps and Pitfalls" that the
original realloc() implementation required this pattern.

The C standard is clear that this is Undefined Behavior. Modern
allocators don't support it and no portable code could rely on it so
remove this support.

Note: the removed implementation contains an off-by-one error and if
an item isn't found on the freelist, then twice as much memory as the
largest possible allocation will be copied.

Diff Detail

Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 25956
Build 24514: arc lint + arc unit

Event Timeline

This is good, and good commit message. V7 was released 40 years ago this past January. It flourished in the late 70s and early 80s as a platform people ported to (I'm aware of maybe a dozen such ports too 4 or 5 different architectures)... then all those ports migrated to System III or System V by maybe 1989 or so. The last time I saw this software pattern was in the mid 90s when I updated the program in question to not do that.... I doubt this code path has been execute in the last decade or two except by pedants testing V7 compatibility.

This revision is now accepted and ready to land.Aug 17 2019, 1:59 AM
pstef added inline comments.
libexec/rtld-elf/rtld_malloc.c
318

Is this comment (rtld_malloc.c:318) talking about the same compacting as you mentioned in the commit message?

libexec/rtld-elf/rtld_malloc.c
256–257

This var should be removed ...

270–271

... and this condition as well, executing the code under it unconditionally.

282–283

Note that cp is already freed at this point. If you claim that __crt_realloc() does not support/does not rely on accessibility of the block freed just before, then this copy should be done before __crt_free(cp).

  • Always follow the allocated path, don't free prematurely.
This revision now requires review to proceed.Aug 19 2019, 10:13 AM
brooks edited the summary of this revision. (Show Details)

Please fix two style issues, no need to re-post the updated patch.

libexec/rtld-elf/rtld_malloc.c
264

I would express this as

if (op->ov_magic != MAGIC)
   return (NULL);
i = op->ov_index;

I considered should we change the if (... != MAGIC) to assert, esp. because the only consumers of this malloc are rtld and libthr, but decided that we do not.

271

if (i != 0) {

This revision is now accepted and ready to land.Aug 19 2019, 12:18 PM