<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Wed, 9 Mar 2016 at 06:57 Victor Stinner <<a href="mailto:victor.stinner@gmail.com">victor.stinner@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">2016-02-08 15:18 GMT+01:00 Victor Stinner <<a href="mailto:victor.stinner@gmail.com" target="_blank">victor.stinner@gmail.com</a>>:<br>
>> Perhaps if you add some guards somewhere :-)<br>
><br>
> We have runtime checks but only implemented in debug mode for efficiency.<br>
><br>
> By the way, I proposed once to add an environment variable to allow to<br>
> enable these checks without having to recompile Python.  Since the PEP<br>
> 445, it became easy to implement this. What do you think?<br>
> <a href="https://www.python.org/dev/peps/pep-0445/#add-a-new-pydebugmalloc-environment-variable" rel="noreferrer" target="_blank">https://www.python.org/dev/peps/pep-0445/#add-a-new-pydebugmalloc-environment-variable</a><br>
<br>
Ok, I wrote a patch to implement a new PYTHONMALLOC environment variable:<br>
<br>
   <a href="http://bugs.python.org/issue26516" rel="noreferrer" target="_blank">http://bugs.python.org/issue26516</a><br>
<br>
PYTHONMALLOC=debug installs debug hooks to:<br>
<br>
* detect API violations, ex: PyObject_Free() called on a buffer<br>
allocated by PyMem_Malloc()<br>
* detect write before the start of the buffer (buffer underflow)<br>
* detect write after the end of the buffer (buffer overflow)<br>
<br>
<a href="https://docs.python.org/dev/c-api/memory.html#c.PyMem_SetupDebugHooks" rel="noreferrer" target="_blank">https://docs.python.org/dev/c-api/memory.html#c.PyMem_SetupDebugHooks</a><br>
<br>
The main advantage of this variable is that you don't have to<br>
recompile Python in debug mode to benefit of these checks.<br></blockquote><div><br></div><div>I just wanted to say this all sounds awesome! Thanks for all the hard work on making our memory management story easier to work with, Victor.</div><div><br></div><div>-Brett</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Recompiling Python in debug mode requires to recompile *all*<br>
extensions modules since the debug ABI is incompatible. When I played<br>
with tracemalloc on Python 2 ( <a href="http://pytracemalloc.readthedocs.org/" rel="noreferrer" target="_blank">http://pytracemalloc.readthedocs.org/</a><br>
), I had such issues, it was very annoying with non-trivial extension<br>
modules like PyQt or PyGTK. With PYTHONMALLOC, you don't have to<br>
recompile extension modules anymore!<br>
<br>
<br>
With tracemalloc and PYTHONMALLOC=debug, we will have a complete tool<br>
suite to "debug memory"!<br>
<br>
My motivation for PYTHONMALLOC=debug is to detect API violations to<br>
prepare my change on PyMem_Malloc() allocator (<br>
<a href="http://bugs.python.org/issue26249" rel="noreferrer" target="_blank">http://bugs.python.org/issue26249</a> ), but also to help users to detect<br>
bugs.<br>
<br>
It's common that users report a bug: "Python crashed", but have no<br>
idea of the responsible of the crash. I hope that detection of buffer<br>
underflow & overflow will help them to detect bugs in their own<br>
extension modules.<br>
<br>
<br>
Moreover, I added PYTHONMALLOC=malloc to ease the use of external<br>
memory debugger on Python. By default, Python uses pymalloc allocator<br>
for PyObject_Malloc() which raises a lot of false positive in<br>
Valgrind. We even have a configuration (--with-valgrind) and a<br>
Valgrind suppressino file to be able to skip these false alarms in<br>
Valgrind. IMHO PYTHONMALLOC=malloc is a simpler option to use Valgrind<br>
(or other tools).<br>
<br>
Victor<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/brett%40python.org" rel="noreferrer" target="_blank">https://mail.python.org/mailman/options/python-dev/brett%40python.org</a><br>
</blockquote></div></div>