Somewhat more sophisticated leak reporting
diff --git a/OpenSSL/crypto.py b/OpenSSL/crypto.py
index aca1589..abe9734 100644
--- a/OpenSSL/crypto.py
+++ b/OpenSSL/crypto.py
@@ -22,6 +22,15 @@
+def _new_mem_buf(buffer):
+ bio = _api.BIO_new_mem_buf(buffer, len(buffer))
+ if bio == _api.NULL:
+ 1/0
+ bio = _api.ffi.gc(bio, _api.BIO_free)
+ return bio
+
+
+
def _set_asn1_time(boundary, when):
if not isinstance(when, bytes):
raise TypeError("when must be a byte string")
@@ -1748,15 +1757,6 @@
-def _new_mem_buf(buffer):
- bio = _api.BIO_new_mem_buf(buffer, len(buffer))
- if bio == _api.NULL:
- 1/0
- bio = _api.ffi.gc(bio, _api.BIO_free)
- return bio
-
-
-
def load_privatekey(type, buffer, passphrase=None):
"""
Load a private key from a buffer
diff --git a/OpenSSL/test/util.py b/OpenSSL/test/util.py
index 74277f3..d58f922 100644
--- a/OpenSSL/test/util.py
+++ b/OpenSSL/test/util.py
@@ -8,6 +8,7 @@
"""
import shutil
+import traceback
import os, os.path
from tempfile import mktemp
from unittest import TestCase
@@ -15,6 +16,8 @@
from OpenSSL.crypto import Error, _exception_from_error_queue
+import memdbg
+
if sys.version_info < (3, 0):
def b(s):
return s
@@ -33,10 +36,7 @@
"""
def setUp(self):
super(TestCase, self).setUp()
- # Enable OpenSSL's memory debugging feature
- api.CRYPTO_malloc_debug_init()
- api.CRYPTO_mem_ctrl(api.CRYPTO_MEM_CHECK_ON)
-
+ self._before = set(memdbg.heap)
def tearDown(self):
"""
@@ -47,21 +47,72 @@
import gc
gc.collect(); gc.collect(); gc.collect()
- api.CRYPTO_mem_ctrl(api.CRYPTO_MEM_CHECK_OFF)
- api.CRYPTO_malloc_init()
+ def format_leak(p):
+ stacks = memdbg.heap[p]
+ # Eventually look at multiple stacks for the realloc() case. For
+ # now just look at the original allocation location.
+ (python_stack, c_stack) = stacks[0]
- bio = api.BIO_new(api.BIO_s_mem())
- if bio == api.NULL:
- 1/0
+ stack = traceback.format_list(python_stack)[:-1]
- api.CRYPTO_mem_leaks(bio)
+ # c_stack looks something like this (interesting parts indicated
+ # with inserted arrows not part of the data):
+ #
+ # /home/exarkun/Projects/pyOpenSSL/branches/use-opentls/__pycache__/_cffi__x89095113xb9185b9b.so(+0x12cf) [0x7fe2e20582cf]
+ # /home/exarkun/Projects/cpython/2.7/python(PyCFunction_Call+0x8b) [0x56265a]
+ # /home/exarkun/Projects/cpython/2.7/python() [0x4d5f52]
+ # /home/exarkun/Projects/cpython/2.7/python(PyEval_EvalFrameEx+0x753b) [0x4d0e1e]
+ # /home/exarkun/Projects/cpython/2.7/python() [0x4d6419]
+ # /home/exarkun/Projects/cpython/2.7/python() [0x4d6129]
+ # /home/exarkun/Projects/cpython/2.7/python(PyEval_EvalFrameEx+0x753b) [0x4d0e1e]
+ # /home/exarkun/Projects/cpython/2.7/python(PyEval_EvalCodeEx+0x1043) [0x4d3726]
+ # /home/exarkun/Projects/cpython/2.7/python() [0x55fd51]
+ # /home/exarkun/Projects/cpython/2.7/python(PyObject_Call+0x7e) [0x420ee6]
+ # /home/exarkun/Projects/cpython/2.7/python(PyEval_CallObjectWithKeywords+0x158) [0x4d56ec]
+ # /home/exarkun/.local/lib/python2.7/site-packages/cffi-0.5-py2.7-linux-x86_64.egg/_cffi_backend.so(+0xe96e) [0x7fe2e38be96e]
+ # /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_closure_unix64_inner+0x1b9) [0x7fe2e36ad819]
+ # /usr/lib/x86_64-linux-gnu/libffi.so.6(ffi_closure_unix64+0x46) [0x7fe2e36adb7c]
+ # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(CRYPTO_malloc+0x64) [0x7fe2e1cef784] <------ end interesting
+ # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(lh_insert+0x16b) [0x7fe2e1d6a24b] .
+ # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(+0x61c18) [0x7fe2e1cf0c18] .
+ # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(+0x625ec) [0x7fe2e1cf15ec] .
+ # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(DSA_new_method+0xe6) [0x7fe2e1d524d6] .
+ # /lib/x86_64-linux-gnu/libcrypto.so.1.0.0(DSA_generate_parameters+0x3a) [0x7fe2e1d5364a] <------ begin interesting
+ # /home/exarkun/Projects/opentls/trunk/tls/c/__pycache__/_cffi__x305d4698xb539baaa.so(+0x1f397) [0x7fe2df84d397]
+ # /home/exarkun/Projects/cpython/2.7/python(PyCFunction_Call+0x8b) [0x56265a]
+ # /home/exarkun/Projects/cpython/2.7/python() [0x4d5f52]
+ # /home/exarkun/Projects/cpython/2.7/python(PyEval_EvalFrameEx+0x753b) [0x4d0e1e]
+ # /home/exarkun/Projects/cpython/2.7/python() [0x4d6419]
+ # ...
+ #
+ # Notice the stack is upside down compared to a Python traceback.
+ # Identify the start and end of interesting bits and stuff it into the stack we report.
- result_buffer = api.new('char**')
- buffer_length = api.BIO_get_mem_data(bio, result_buffer)
- s = api.buffer(result_buffer[0], buffer_length)[:]
- if s:
- self.fail(s)
+ # Figure the first interesting frame will be after a the cffi-compiled module
+ while '/__pycache__/_cffi__' not in c_stack[-1]:
+ c_stack.pop()
+ # Figure the last interesting frame will always be CRYPTO_malloc,
+ # since that's where we hooked in to things.
+ while 'CRYPTO_malloc' not in c_stack[0]:
+ c_stack.pop(0)
+
+ c_stack.reverse()
+ stack.extend([frame + "\n" for frame in c_stack])
+
+ # XXX :(
+ ptr = int(str(p).split()[-1][:-1], 16)
+ stack.insert(0, "Leaked 0x%x at:\n" % (ptr,))
+ return "".join(stack)
+
+ after = set(memdbg.heap)
+ leak = after - self._before
+ if leak:
+ reasons = []
+ for p in leak:
+ reasons.append(format_leak(p))
+ del memdbg.heap[p]
+ self.fail('\n'.join(reasons))
if False and self._temporaryFiles is not None:
for temp in self._temporaryFiles:
diff --git a/memdbg.py b/memdbg.py
new file mode 100644
index 0000000..7b0aef9
--- /dev/null
+++ b/memdbg.py
@@ -0,0 +1,69 @@
+
+import traceback
+
+from cffi import api as _api
+_ffi = _api.FFI()
+_ffi.cdef(
+ """
+ void *malloc(size_t size);
+ void free(void *ptr);
+ void *realloc(void *ptr, size_t size);
+
+ int CRYPTO_set_mem_functions(void *(*m)(size_t),void *(*r)(void *,size_t), void (*f)(void *));
+
+ int backtrace(void **buffer, int size);
+ char **backtrace_symbols(void *const *buffer, int size);
+ void backtrace_symbols_fd(void *const *buffer, int size, int fd);
+ """)
+_api = _ffi.verify(
+ """
+ #include <openssl/crypto.h>
+ #include <stdlib.h>
+ #include <execinfo.h>
+ """, libraries=["crypto"])
+C = _ffi.dlopen(None)
+
+heap = {}
+
+def _backtrace():
+ buf = _ffi.new("void*[]", 64)
+ result = _api.backtrace(buf, len(buf))
+ strings = _api.backtrace_symbols(buf, result)
+ stack = [_ffi.string(strings[i]) for i in range(result)]
+ C.free(strings)
+ return stack
+
+
+def _malloc(n):
+ memory = C.malloc(n)
+ python_stack = traceback.extract_stack(limit=3)
+ c_stack = _backtrace()
+ heap[memory] = [(python_stack, c_stack)]
+ return memory
+malloc = _ffi.callback("void*(*)(size_t)", _malloc)
+
+
+@_ffi.callback("void*(*)(void*, size_t)")
+def realloc(p, n):
+ memory = C.realloc(p, n)
+ old = heap.pop(p)
+
+ python_stack = traceback.extract_stack(limit=3)
+ c_stack = _backtrace()
+
+ old.append((python_stack, c_stack))
+ heap[memory] = old
+ return memory
+
+
+@_ffi.callback("void(*)(void*)")
+def free(p):
+ if p != _ffi.NULL:
+ C.free(p)
+ del heap[p]
+
+
+if _api.CRYPTO_set_mem_functions(malloc, realloc, free):
+ print 'Enabled memory debugging'
+else:
+ print 'Failed to enable memory debugging'
diff --git a/runtests.py b/runtests.py
new file mode 100644
index 0000000..2ec425b
--- /dev/null
+++ b/runtests.py
@@ -0,0 +1,8 @@
+import sys
+sys.modules['ssl'] = None
+sys.modules['_hashlib'] = None
+
+import memdbg
+
+from twisted.scripts.trial import run
+run()