/Misc/README.valgrind

http://unladen-swallow.googlecode.com/ · Unknown · 97 lines · 78 code · 19 blank · 0 comment · 0 complexity · c55d8ab8fc18adf56a3b1940f5454c3c MD5 · raw file

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