| author | martin.trojer@nokia.com | 
| Fri, 31 Jul 2009 15:01:17 +0100 | |
| changeset 1 | 2fb8b9db1c86 | 
| permissions | -rw-r--r-- | 
| 1 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 1 | This document describes some caveats about the use of Valgrind with | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 2 | Python. Valgrind is used periodically by Python developers to try | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 3 | to ensure there are no memory leaks or invalid memory reads/writes. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 4 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 5 | If you don't want to read about the details of using Valgrind, there | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 6 | are still two things you must do to suppress the warnings. First, | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 7 | you must use a suppressions file. One is supplied in | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 8 | Misc/valgrind-python.supp. Second, you must do one of the following: | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 9 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 10 | * Uncomment Py_USING_MEMORY_DEBUGGER in Objects/obmalloc.c, | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 11 | then rebuild Python | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 12 | * Uncomment the lines in Misc/valgrind-python.supp that | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 13 | suppress the warnings for PyObject_Free and PyObject_Realloc | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 14 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 15 | If you want to use Valgrind more effectively and catch even more | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 16 | memory leaks, you will need to configure python --without-pymalloc. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 17 | PyMalloc allocates a few blocks in big chunks and most object | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 18 | allocations don't call malloc, they use chunks doled about by PyMalloc | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 19 | from the big blocks. This means Valgrind can't detect | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 20 | many allocations (and frees), except for those that are forwarded | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 21 | to the system malloc. Note: configuring python --without-pymalloc | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 22 | makes Python run much slower, especially when running under Valgrind. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 23 | You may need to run the tests in batches under Valgrind to keep | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 24 | the memory usage down to allow the tests to complete. It seems to take | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 25 | about 5 times longer to run --without-pymalloc. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 26 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 27 | Apr 15, 2006: | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 28 | test_ctypes causes Valgrind 3.1.1 to fail (crash). | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 29 | test_socket_ssl should be skipped when running valgrind. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 30 | The reason is that it purposely uses uninitialized memory. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 31 | This causes many spurious warnings, so it's easier to just skip it. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 32 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 33 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 34 | Details: | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 35 | -------- | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 36 | Python uses its own small-object allocation scheme on top of malloc, | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 37 | called PyMalloc. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 38 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 39 | Valgrind may show some unexpected results when PyMalloc is used. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 40 | Starting with Python 2.3, PyMalloc is used by default. You can disable | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 41 | PyMalloc when configuring python by adding the --without-pymalloc option. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 42 | If you disable PyMalloc, most of the information in this document and | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 43 | the supplied suppressions file will not be useful. As discussed above, | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 44 | disabling PyMalloc can catch more problems. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 45 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 46 | If you use valgrind on a default build of Python, you will see | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 47 | many errors like: | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 48 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 49 | ==6399== Use of uninitialised value of size 4 | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 50 | ==6399== at 0x4A9BDE7E: PyObject_Free (obmalloc.c:711) | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 51 | ==6399== by 0x4A9B8198: dictresize (dictobject.c:477) | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 52 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 53 | These are expected and not a problem. Tim Peters explains | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 54 | the situation: | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 55 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 56 | PyMalloc needs to know whether an arbitrary address is one | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 57 | that's managed by it, or is managed by the system malloc. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 58 | The current scheme allows this to be determined in constant | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 59 | time, regardless of how many memory areas are under pymalloc's | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 60 | control. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 61 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 62 | The memory pymalloc manages itself is in one or more "arenas", | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 63 | each a large contiguous memory area obtained from malloc. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 64 | The base address of each arena is saved by pymalloc | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 65 | in a vector. Each arena is carved into "pools", and a field at | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 66 | the start of each pool contains the index of that pool's arena's | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 67 | base address in that vector. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 68 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 69 | Given an arbitrary address, pymalloc computes the pool base | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 70 | address corresponding to it, then looks at "the index" stored | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 71 | near there. If the index read up is out of bounds for the | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 72 | vector of arena base addresses pymalloc maintains, then | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 73 | pymalloc knows for certain that this address is not under | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 74 | pymalloc's control. Otherwise the index is in bounds, and | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 75 | pymalloc compares | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 76 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 77 | the arena base address stored at that index in the vector | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 78 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 79 | to | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 80 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 81 | the arbitrary address pymalloc is investigating | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 82 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 83 | pymalloc controls this arbitrary address if and only if it lies | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 84 | in the arena the address's pool's index claims it lies in. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 85 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 86 |         It doesn't matter whether the memory pymalloc reads up ("the
 | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 87 | index") is initialized. If it's not initialized, then | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 88 | whatever trash gets read up will lead pymalloc to conclude | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 89 | (correctly) that the address isn't controlled by it, either | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 90 | because the index is out of bounds, or the index is in bounds | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 91 | but the arena it represents doesn't contain the address. | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 92 | |
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 93 | This determination has to be made on every call to one of | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 94 | pymalloc's free/realloc entry points, so its speed is critical | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 95 | (Python allocates and frees dynamic memory at a ferocious rate | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 96 | -- everything in Python, from integers to "stack frames", | 
| 
2fb8b9db1c86
Initial QEMU (symbian-qemu-0.9.1-12) import
 martin.trojer@nokia.com parents: diff
changeset | 97 | lives in the heap). |