[WIP] bpo-31626: Rewrite _PyMem_DebugRawRealloc()#3898
Conversation
Previously, _PyMem_DebugRawRealloc() expected that the old memory block remains readable and unchanged after realloc(). This assumption is wrong on OpenBSD. Rewrite _PyMem_DebugRawRealloc() as malloc()+free() rather than realloc(). Debug hooks are reused. The old memory block remains valid after the new memory block was allocated, so bytes can be safely copied in a portable way. The drawback is that _PyMem_DebugRawRealloc() now allocates a new memory block while the old memory block remains allocated, until the old memory block is freed, so the peak memory usage can be the double in the worst case. The second drawback is that the system realloc() is no more used in debug mode.
|
I tagged the PR as WIP because I dislike the second drawback of my change: "the system realloc() is no more used in debug mode". A bug in system realloc() would be missed completely in debug mode with this change. |
Sorry, something went wrong.
|
I'm opposed to this change. The purpose of the debug allocator is to help with finding bugs in user code. But if realloc() always returns a new reference, this can hide bugs. Some bugs now could be reproduced only with non-debug allocator. |
Sorry, something went wrong.
|
PR #3844 was merged, I abandon my change. @serhiy-storchaka also wrote a lighter version of my PR without allocating memory on the heap: allocate memory on the stack instead, PR #4210. |
Sorry, something went wrong.
Previously, _PyMem_DebugRawRealloc() expected that the old memory
block remains readable and unchanged after realloc(). This assumption
is wrong on OpenBSD.
Rewrite _PyMem_DebugRawRealloc() as malloc()+free() rather than
realloc(). Debug hooks are reused. The old memory block remains valid
after the new memory block was allocated, so bytes can be safely
copied in a portable way.
The drawback is that _PyMem_DebugRawRealloc() now allocates a new
memory block while the old memory block remains allocated, until the
old memory block is freed, so the peak memory usage can be the double
in the worst case.
The second drawback is that the system realloc() is no more used in
debug mode.
https://bugs.python.org/issue31626