[issue26564] Malloc debug hooks: display memory block traceback on error

STINNER Victor report at bugs.python.org
Mon Mar 14 20:39:56 EDT 2016


New submission from STINNER Victor:

Python implements debug hooks on Python memory allocators:
https://docs.python.org/dev/c-api/memory.html#c.PyMem_SetupDebugHooks

Problem: buffer understand and buffer overflow are only detected when a memory block is released, which may occur far from the code responsible to create the buffer.

Attached patch dumps the traceback where a memory block was allocated on error in malloc debug hooks. The feature requires to enable tracemalloc to record tracebacks.


test.py used in below example:
----
import _testcapi

def f():
    _testcapi.pymem_buffer_overflow()

f()
----


Example:
---
haypo at selma$ ./python -X tracemalloc=5 test.py 
Debug memory block at address p=0x22e8be0: API 'm'
    16 bytes originally requested
    The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
    The 8 pad bytes at tail=0x22e8bf0 are not all FORBIDDENBYTE (0xfb):
        at tail+0: 0x78 *** OUCH
        ...
    The block was made by call #37240 to debug malloc/realloc.
    Data at p: cb cb cb cb cb cb cb cb cb cb cb cb cb cb cb cb

Memory block traceback (most recent call first):
  File "test.py", line 4
  File "test.py", line 6

Fatal Python error: bad trailing pad byte

Current thread 0x00007f4a93d9c700 (most recent call first):
  File "test.py", line 4 in f
  File "test.py", line 6 in <module>
Abandon (core dumped)
---

The patch adds the "Memory block traceback" section.

----------
files: pymem_tb.patch
keywords: patch
messages: 261793
nosy: haypo
priority: normal
severity: normal
status: open
title: Malloc debug hooks: display memory block traceback on error
type: enhancement
versions: Python 3.6
Added file: http://bugs.python.org/file42167/pymem_tb.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26564>
_______________________________________


More information about the Python-bugs-list mailing list