Jean-Paul Calderone | fd59312 | 2013-03-03 10:21:53 -0800 | [diff] [blame] | 1 | import sys |
| 2 | sys.modules['ssl'] = None |
| 3 | sys.modules['_hashlib'] = None |
| 4 | |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 5 | |
| 6 | import traceback |
| 7 | |
| 8 | from cffi import api as _api |
| 9 | _ffi = _api.FFI() |
| 10 | _ffi.cdef( |
| 11 | """ |
| 12 | void *malloc(size_t size); |
| 13 | void free(void *ptr); |
| 14 | void *realloc(void *ptr, size_t size); |
| 15 | |
| 16 | int CRYPTO_set_mem_functions(void *(*m)(size_t),void *(*r)(void *,size_t), void (*f)(void *)); |
| 17 | |
| 18 | int backtrace(void **buffer, int size); |
| 19 | char **backtrace_symbols(void *const *buffer, int size); |
| 20 | void backtrace_symbols_fd(void *const *buffer, int size, int fd); |
| 21 | """) |
| 22 | _api = _ffi.verify( |
| 23 | """ |
| 24 | #include <openssl/crypto.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <execinfo.h> |
| 27 | """, libraries=["crypto"]) |
| 28 | C = _ffi.dlopen(None) |
| 29 | |
Jean-Paul Calderone | fd59312 | 2013-03-03 10:21:53 -0800 | [diff] [blame] | 30 | verbose = False |
| 31 | |
| 32 | def log(s): |
| 33 | if verbose: |
Jean-Paul Calderone | 558b78a | 2014-01-10 14:08:50 -0500 | [diff] [blame] | 34 | print(s) |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 35 | |
| 36 | def _backtrace(): |
| 37 | buf = _ffi.new("void*[]", 64) |
| 38 | result = _api.backtrace(buf, len(buf)) |
| 39 | strings = _api.backtrace_symbols(buf, result) |
| 40 | stack = [_ffi.string(strings[i]) for i in range(result)] |
| 41 | C.free(strings) |
| 42 | return stack |
| 43 | |
| 44 | |
Jean-Paul Calderone | fd59312 | 2013-03-03 10:21:53 -0800 | [diff] [blame] | 45 | @_ffi.callback("void*(*)(size_t)") |
Jean-Paul Calderone | ede8b71 | 2014-01-01 12:36:46 -0500 | [diff] [blame] | 46 | def malloc(n): |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 47 | memory = C.malloc(n) |
| 48 | python_stack = traceback.extract_stack(limit=3) |
| 49 | c_stack = _backtrace() |
Jean-Paul Calderone | c2e8b41 | 2013-03-02 16:27:55 -0800 | [diff] [blame] | 50 | heap[memory] = [(n, python_stack, c_stack)] |
Jean-Paul Calderone | fd59312 | 2013-03-03 10:21:53 -0800 | [diff] [blame] | 51 | log("malloc(%d) -> %s" % (n, memory)) |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 52 | return memory |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 53 | |
| 54 | |
| 55 | @_ffi.callback("void*(*)(void*, size_t)") |
Jean-Paul Calderone | ede8b71 | 2014-01-01 12:36:46 -0500 | [diff] [blame] | 56 | def realloc(p, n): |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 57 | memory = C.realloc(p, n) |
| 58 | old = heap.pop(p) |
| 59 | |
| 60 | python_stack = traceback.extract_stack(limit=3) |
| 61 | c_stack = _backtrace() |
| 62 | |
Jean-Paul Calderone | c2e8b41 | 2013-03-02 16:27:55 -0800 | [diff] [blame] | 63 | old.append((n, python_stack, c_stack)) |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 64 | heap[memory] = old |
Jean-Paul Calderone | fd59312 | 2013-03-03 10:21:53 -0800 | [diff] [blame] | 65 | log("realloc(0x%x, %d) -> %s" % (int(_ffi.cast("int", p)), n, memory)) |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 66 | return memory |
| 67 | |
| 68 | |
| 69 | @_ffi.callback("void(*)(void*)") |
Jean-Paul Calderone | ede8b71 | 2014-01-01 12:36:46 -0500 | [diff] [blame] | 70 | def free(p): |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 71 | if p != _ffi.NULL: |
| 72 | C.free(p) |
| 73 | del heap[p] |
Jean-Paul Calderone | fd59312 | 2013-03-03 10:21:53 -0800 | [diff] [blame] | 74 | log("free(0x%x)" % (int(_ffi.cast("int", p)),)) |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 75 | |
| 76 | |
Jean-Paul Calderone | ede8b71 | 2014-01-01 12:36:46 -0500 | [diff] [blame] | 77 | if _api.CRYPTO_set_mem_functions(malloc, realloc, free): |
Jean-Paul Calderone | 558b78a | 2014-01-10 14:08:50 -0500 | [diff] [blame] | 78 | log('Enabled memory debugging') |
Jean-Paul Calderone | 8e4a663 | 2013-03-03 17:30:27 -0800 | [diff] [blame] | 79 | heap = {} |
Jean-Paul Calderone | 68a6f8f | 2013-03-01 17:56:22 -0800 | [diff] [blame] | 80 | else: |
Jean-Paul Calderone | 558b78a | 2014-01-10 14:08:50 -0500 | [diff] [blame] | 81 | log('Failed to enable memory debugging') |
Jean-Paul Calderone | 8e4a663 | 2013-03-03 17:30:27 -0800 | [diff] [blame] | 82 | heap = None |