obmalloc.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706
  1. /* Python's malloc wrappers (see pymem.h) */
  2. #include "Python.h"
  3. #include "pycore_code.h" // stats
  4. #include "pycore_pystate.h" // _PyInterpreterState_GET
  5. #include "pycore_obmalloc.h"
  6. #include "pycore_pymem.h"
  7. #include <stdlib.h> // malloc()
  8. #include <stdbool.h>
  9. #undef uint
  10. #define uint pymem_uint
  11. /* Defined in tracemalloc.c */
  12. extern void _PyMem_DumpTraceback(int fd, const void *ptr);
  13. static void _PyObject_DebugDumpAddress(const void *p);
  14. static void _PyMem_DebugCheckAddress(const char *func, char api_id, const void *p);
  15. static void set_up_debug_hooks_domain_unlocked(PyMemAllocatorDomain domain);
  16. static void set_up_debug_hooks_unlocked(void);
  17. static void get_allocator_unlocked(PyMemAllocatorDomain, PyMemAllocatorEx *);
  18. static void set_allocator_unlocked(PyMemAllocatorDomain, PyMemAllocatorEx *);
  19. /***************************************/
  20. /* low-level allocator implementations */
  21. /***************************************/
  22. /* the default raw allocator (wraps malloc) */
  23. void *
  24. _PyMem_RawMalloc(void *Py_UNUSED(ctx), size_t size)
  25. {
  26. /* PyMem_RawMalloc(0) means malloc(1). Some systems would return NULL
  27. for malloc(0), which would be treated as an error. Some platforms would
  28. return a pointer with no memory behind it, which would break pymalloc.
  29. To solve these problems, allocate an extra byte. */
  30. if (size == 0)
  31. size = 1;
  32. return malloc(size);
  33. }
  34. void *
  35. _PyMem_RawCalloc(void *Py_UNUSED(ctx), size_t nelem, size_t elsize)
  36. {
  37. /* PyMem_RawCalloc(0, 0) means calloc(1, 1). Some systems would return NULL
  38. for calloc(0, 0), which would be treated as an error. Some platforms
  39. would return a pointer with no memory behind it, which would break
  40. pymalloc. To solve these problems, allocate an extra byte. */
  41. if (nelem == 0 || elsize == 0) {
  42. nelem = 1;
  43. elsize = 1;
  44. }
  45. return calloc(nelem, elsize);
  46. }
  47. void *
  48. _PyMem_RawRealloc(void *Py_UNUSED(ctx), void *ptr, size_t size)
  49. {
  50. if (size == 0)
  51. size = 1;
  52. return realloc(ptr, size);
  53. }
  54. void
  55. _PyMem_RawFree(void *Py_UNUSED(ctx), void *ptr)
  56. {
  57. free(ptr);
  58. }
  59. #define MALLOC_ALLOC {NULL, _PyMem_RawMalloc, _PyMem_RawCalloc, _PyMem_RawRealloc, _PyMem_RawFree}
  60. #define PYRAW_ALLOC MALLOC_ALLOC
  61. /* the default object allocator */
  62. // The actual implementation is further down.
  63. #ifdef WITH_PYMALLOC
  64. void* _PyObject_Malloc(void *ctx, size_t size);
  65. void* _PyObject_Calloc(void *ctx, size_t nelem, size_t elsize);
  66. void _PyObject_Free(void *ctx, void *p);
  67. void* _PyObject_Realloc(void *ctx, void *ptr, size_t size);
  68. # define PYMALLOC_ALLOC {NULL, _PyObject_Malloc, _PyObject_Calloc, _PyObject_Realloc, _PyObject_Free}
  69. # define PYOBJ_ALLOC PYMALLOC_ALLOC
  70. #else
  71. # define PYOBJ_ALLOC MALLOC_ALLOC
  72. #endif // WITH_PYMALLOC
  73. #define PYMEM_ALLOC PYOBJ_ALLOC
  74. /* the default debug allocators */
  75. // The actual implementation is further down.
  76. void* _PyMem_DebugRawMalloc(void *ctx, size_t size);
  77. void* _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize);
  78. void* _PyMem_DebugRawRealloc(void *ctx, void *ptr, size_t size);
  79. void _PyMem_DebugRawFree(void *ctx, void *ptr);
  80. void* _PyMem_DebugMalloc(void *ctx, size_t size);
  81. void* _PyMem_DebugCalloc(void *ctx, size_t nelem, size_t elsize);
  82. void* _PyMem_DebugRealloc(void *ctx, void *ptr, size_t size);
  83. void _PyMem_DebugFree(void *ctx, void *p);
  84. #define PYDBGRAW_ALLOC \
  85. {&_PyRuntime.allocators.debug.raw, _PyMem_DebugRawMalloc, _PyMem_DebugRawCalloc, _PyMem_DebugRawRealloc, _PyMem_DebugRawFree}
  86. #define PYDBGMEM_ALLOC \
  87. {&_PyRuntime.allocators.debug.mem, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}
  88. #define PYDBGOBJ_ALLOC \
  89. {&_PyRuntime.allocators.debug.obj, _PyMem_DebugMalloc, _PyMem_DebugCalloc, _PyMem_DebugRealloc, _PyMem_DebugFree}
  90. /* the low-level virtual memory allocator */
  91. #ifdef WITH_PYMALLOC
  92. # ifdef MS_WINDOWS
  93. # include <windows.h>
  94. # elif defined(HAVE_MMAP)
  95. # include <sys/mman.h>
  96. # ifdef MAP_ANONYMOUS
  97. # define ARENAS_USE_MMAP
  98. # endif
  99. # endif
  100. #endif
  101. void *
  102. _PyMem_ArenaAlloc(void *Py_UNUSED(ctx), size_t size)
  103. {
  104. #ifdef MS_WINDOWS
  105. return VirtualAlloc(NULL, size,
  106. MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  107. #elif defined(ARENAS_USE_MMAP)
  108. void *ptr;
  109. ptr = mmap(NULL, size, PROT_READ|PROT_WRITE,
  110. MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  111. if (ptr == MAP_FAILED)
  112. return NULL;
  113. assert(ptr != NULL);
  114. return ptr;
  115. #else
  116. return malloc(size);
  117. #endif
  118. }
  119. void
  120. _PyMem_ArenaFree(void *Py_UNUSED(ctx), void *ptr,
  121. #if defined(ARENAS_USE_MMAP)
  122. size_t size
  123. #else
  124. size_t Py_UNUSED(size)
  125. #endif
  126. )
  127. {
  128. #ifdef MS_WINDOWS
  129. VirtualFree(ptr, 0, MEM_RELEASE);
  130. #elif defined(ARENAS_USE_MMAP)
  131. munmap(ptr, size);
  132. #else
  133. free(ptr);
  134. #endif
  135. }
  136. /*******************************************/
  137. /* end low-level allocator implementations */
  138. /*******************************************/
  139. #if defined(__has_feature) /* Clang */
  140. # if __has_feature(address_sanitizer) /* is ASAN enabled? */
  141. # define _Py_NO_SANITIZE_ADDRESS \
  142. __attribute__((no_sanitize("address")))
  143. # endif
  144. # if __has_feature(thread_sanitizer) /* is TSAN enabled? */
  145. # define _Py_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
  146. # endif
  147. # if __has_feature(memory_sanitizer) /* is MSAN enabled? */
  148. # define _Py_NO_SANITIZE_MEMORY __attribute__((no_sanitize_memory))
  149. # endif
  150. #elif defined(__GNUC__)
  151. # if defined(__SANITIZE_ADDRESS__) /* GCC 4.8+, is ASAN enabled? */
  152. # define _Py_NO_SANITIZE_ADDRESS \
  153. __attribute__((no_sanitize_address))
  154. # endif
  155. // TSAN is supported since GCC 5.1, but __SANITIZE_THREAD__ macro
  156. // is provided only since GCC 7.
  157. # if __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1)
  158. # define _Py_NO_SANITIZE_THREAD __attribute__((no_sanitize_thread))
  159. # endif
  160. #endif
  161. #ifndef _Py_NO_SANITIZE_ADDRESS
  162. # define _Py_NO_SANITIZE_ADDRESS
  163. #endif
  164. #ifndef _Py_NO_SANITIZE_THREAD
  165. # define _Py_NO_SANITIZE_THREAD
  166. #endif
  167. #ifndef _Py_NO_SANITIZE_MEMORY
  168. # define _Py_NO_SANITIZE_MEMORY
  169. #endif
  170. #define ALLOCATORS_MUTEX (_PyRuntime.allocators.mutex)
  171. #define _PyMem_Raw (_PyRuntime.allocators.standard.raw)
  172. #define _PyMem (_PyRuntime.allocators.standard.mem)
  173. #define _PyObject (_PyRuntime.allocators.standard.obj)
  174. #define _PyMem_Debug (_PyRuntime.allocators.debug)
  175. #define _PyObject_Arena (_PyRuntime.allocators.obj_arena)
  176. /***************************/
  177. /* managing the allocators */
  178. /***************************/
  179. static int
  180. set_default_allocator_unlocked(PyMemAllocatorDomain domain, int debug,
  181. PyMemAllocatorEx *old_alloc)
  182. {
  183. if (old_alloc != NULL) {
  184. get_allocator_unlocked(domain, old_alloc);
  185. }
  186. PyMemAllocatorEx new_alloc;
  187. switch(domain)
  188. {
  189. case PYMEM_DOMAIN_RAW:
  190. new_alloc = (PyMemAllocatorEx)PYRAW_ALLOC;
  191. break;
  192. case PYMEM_DOMAIN_MEM:
  193. new_alloc = (PyMemAllocatorEx)PYMEM_ALLOC;
  194. break;
  195. case PYMEM_DOMAIN_OBJ:
  196. new_alloc = (PyMemAllocatorEx)PYOBJ_ALLOC;
  197. break;
  198. default:
  199. /* unknown domain */
  200. return -1;
  201. }
  202. set_allocator_unlocked(domain, &new_alloc);
  203. if (debug) {
  204. set_up_debug_hooks_domain_unlocked(domain);
  205. }
  206. return 0;
  207. }
  208. #ifdef Py_DEBUG
  209. static const int pydebug = 1;
  210. #else
  211. static const int pydebug = 0;
  212. #endif
  213. int
  214. _PyMem_SetDefaultAllocator(PyMemAllocatorDomain domain,
  215. PyMemAllocatorEx *old_alloc)
  216. {
  217. if (ALLOCATORS_MUTEX == NULL) {
  218. /* The runtime must be initializing. */
  219. return set_default_allocator_unlocked(domain, pydebug, old_alloc);
  220. }
  221. PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
  222. int res = set_default_allocator_unlocked(domain, pydebug, old_alloc);
  223. PyThread_release_lock(ALLOCATORS_MUTEX);
  224. return res;
  225. }
  226. int
  227. _PyMem_GetAllocatorName(const char *name, PyMemAllocatorName *allocator)
  228. {
  229. if (name == NULL || *name == '\0') {
  230. /* PYTHONMALLOC is empty or is not set or ignored (-E/-I command line
  231. nameions): use default memory allocators */
  232. *allocator = PYMEM_ALLOCATOR_DEFAULT;
  233. }
  234. else if (strcmp(name, "default") == 0) {
  235. *allocator = PYMEM_ALLOCATOR_DEFAULT;
  236. }
  237. else if (strcmp(name, "debug") == 0) {
  238. *allocator = PYMEM_ALLOCATOR_DEBUG;
  239. }
  240. #ifdef WITH_PYMALLOC
  241. else if (strcmp(name, "pymalloc") == 0) {
  242. *allocator = PYMEM_ALLOCATOR_PYMALLOC;
  243. }
  244. else if (strcmp(name, "pymalloc_debug") == 0) {
  245. *allocator = PYMEM_ALLOCATOR_PYMALLOC_DEBUG;
  246. }
  247. #endif
  248. else if (strcmp(name, "malloc") == 0) {
  249. *allocator = PYMEM_ALLOCATOR_MALLOC;
  250. }
  251. else if (strcmp(name, "malloc_debug") == 0) {
  252. *allocator = PYMEM_ALLOCATOR_MALLOC_DEBUG;
  253. }
  254. else {
  255. /* unknown allocator */
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. static int
  261. set_up_allocators_unlocked(PyMemAllocatorName allocator)
  262. {
  263. switch (allocator) {
  264. case PYMEM_ALLOCATOR_NOT_SET:
  265. /* do nothing */
  266. break;
  267. case PYMEM_ALLOCATOR_DEFAULT:
  268. (void)set_default_allocator_unlocked(PYMEM_DOMAIN_RAW, pydebug, NULL);
  269. (void)set_default_allocator_unlocked(PYMEM_DOMAIN_MEM, pydebug, NULL);
  270. (void)set_default_allocator_unlocked(PYMEM_DOMAIN_OBJ, pydebug, NULL);
  271. break;
  272. case PYMEM_ALLOCATOR_DEBUG:
  273. (void)set_default_allocator_unlocked(PYMEM_DOMAIN_RAW, 1, NULL);
  274. (void)set_default_allocator_unlocked(PYMEM_DOMAIN_MEM, 1, NULL);
  275. (void)set_default_allocator_unlocked(PYMEM_DOMAIN_OBJ, 1, NULL);
  276. break;
  277. #ifdef WITH_PYMALLOC
  278. case PYMEM_ALLOCATOR_PYMALLOC:
  279. case PYMEM_ALLOCATOR_PYMALLOC_DEBUG:
  280. {
  281. PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC;
  282. set_allocator_unlocked(PYMEM_DOMAIN_RAW, &malloc_alloc);
  283. PyMemAllocatorEx pymalloc = PYMALLOC_ALLOC;
  284. set_allocator_unlocked(PYMEM_DOMAIN_MEM, &pymalloc);
  285. set_allocator_unlocked(PYMEM_DOMAIN_OBJ, &pymalloc);
  286. if (allocator == PYMEM_ALLOCATOR_PYMALLOC_DEBUG) {
  287. set_up_debug_hooks_unlocked();
  288. }
  289. break;
  290. }
  291. #endif
  292. case PYMEM_ALLOCATOR_MALLOC:
  293. case PYMEM_ALLOCATOR_MALLOC_DEBUG:
  294. {
  295. PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC;
  296. set_allocator_unlocked(PYMEM_DOMAIN_RAW, &malloc_alloc);
  297. set_allocator_unlocked(PYMEM_DOMAIN_MEM, &malloc_alloc);
  298. set_allocator_unlocked(PYMEM_DOMAIN_OBJ, &malloc_alloc);
  299. if (allocator == PYMEM_ALLOCATOR_MALLOC_DEBUG) {
  300. set_up_debug_hooks_unlocked();
  301. }
  302. break;
  303. }
  304. default:
  305. /* unknown allocator */
  306. return -1;
  307. }
  308. return 0;
  309. }
  310. int
  311. _PyMem_SetupAllocators(PyMemAllocatorName allocator)
  312. {
  313. PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
  314. int res = set_up_allocators_unlocked(allocator);
  315. PyThread_release_lock(ALLOCATORS_MUTEX);
  316. return res;
  317. }
  318. static int
  319. pymemallocator_eq(PyMemAllocatorEx *a, PyMemAllocatorEx *b)
  320. {
  321. return (memcmp(a, b, sizeof(PyMemAllocatorEx)) == 0);
  322. }
  323. static const char*
  324. get_current_allocator_name_unlocked(void)
  325. {
  326. PyMemAllocatorEx malloc_alloc = MALLOC_ALLOC;
  327. #ifdef WITH_PYMALLOC
  328. PyMemAllocatorEx pymalloc = PYMALLOC_ALLOC;
  329. #endif
  330. if (pymemallocator_eq(&_PyMem_Raw, &malloc_alloc) &&
  331. pymemallocator_eq(&_PyMem, &malloc_alloc) &&
  332. pymemallocator_eq(&_PyObject, &malloc_alloc))
  333. {
  334. return "malloc";
  335. }
  336. #ifdef WITH_PYMALLOC
  337. if (pymemallocator_eq(&_PyMem_Raw, &malloc_alloc) &&
  338. pymemallocator_eq(&_PyMem, &pymalloc) &&
  339. pymemallocator_eq(&_PyObject, &pymalloc))
  340. {
  341. return "pymalloc";
  342. }
  343. #endif
  344. PyMemAllocatorEx dbg_raw = PYDBGRAW_ALLOC;
  345. PyMemAllocatorEx dbg_mem = PYDBGMEM_ALLOC;
  346. PyMemAllocatorEx dbg_obj = PYDBGOBJ_ALLOC;
  347. if (pymemallocator_eq(&_PyMem_Raw, &dbg_raw) &&
  348. pymemallocator_eq(&_PyMem, &dbg_mem) &&
  349. pymemallocator_eq(&_PyObject, &dbg_obj))
  350. {
  351. /* Debug hooks installed */
  352. if (pymemallocator_eq(&_PyMem_Debug.raw.alloc, &malloc_alloc) &&
  353. pymemallocator_eq(&_PyMem_Debug.mem.alloc, &malloc_alloc) &&
  354. pymemallocator_eq(&_PyMem_Debug.obj.alloc, &malloc_alloc))
  355. {
  356. return "malloc_debug";
  357. }
  358. #ifdef WITH_PYMALLOC
  359. if (pymemallocator_eq(&_PyMem_Debug.raw.alloc, &malloc_alloc) &&
  360. pymemallocator_eq(&_PyMem_Debug.mem.alloc, &pymalloc) &&
  361. pymemallocator_eq(&_PyMem_Debug.obj.alloc, &pymalloc))
  362. {
  363. return "pymalloc_debug";
  364. }
  365. #endif
  366. }
  367. return NULL;
  368. }
  369. const char*
  370. _PyMem_GetCurrentAllocatorName(void)
  371. {
  372. PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
  373. const char *name = get_current_allocator_name_unlocked();
  374. PyThread_release_lock(ALLOCATORS_MUTEX);
  375. return name;
  376. }
  377. #ifdef WITH_PYMALLOC
  378. static int
  379. _PyMem_DebugEnabled(void)
  380. {
  381. return (_PyObject.malloc == _PyMem_DebugMalloc);
  382. }
  383. static int
  384. _PyMem_PymallocEnabled(void)
  385. {
  386. if (_PyMem_DebugEnabled()) {
  387. return (_PyMem_Debug.obj.alloc.malloc == _PyObject_Malloc);
  388. }
  389. else {
  390. return (_PyObject.malloc == _PyObject_Malloc);
  391. }
  392. }
  393. #endif
  394. static void
  395. set_up_debug_hooks_domain_unlocked(PyMemAllocatorDomain domain)
  396. {
  397. PyMemAllocatorEx alloc;
  398. if (domain == PYMEM_DOMAIN_RAW) {
  399. if (_PyMem_Raw.malloc == _PyMem_DebugRawMalloc) {
  400. return;
  401. }
  402. get_allocator_unlocked(domain, &_PyMem_Debug.raw.alloc);
  403. alloc.ctx = &_PyMem_Debug.raw;
  404. alloc.malloc = _PyMem_DebugRawMalloc;
  405. alloc.calloc = _PyMem_DebugRawCalloc;
  406. alloc.realloc = _PyMem_DebugRawRealloc;
  407. alloc.free = _PyMem_DebugRawFree;
  408. set_allocator_unlocked(domain, &alloc);
  409. }
  410. else if (domain == PYMEM_DOMAIN_MEM) {
  411. if (_PyMem.malloc == _PyMem_DebugMalloc) {
  412. return;
  413. }
  414. get_allocator_unlocked(domain, &_PyMem_Debug.mem.alloc);
  415. alloc.ctx = &_PyMem_Debug.mem;
  416. alloc.malloc = _PyMem_DebugMalloc;
  417. alloc.calloc = _PyMem_DebugCalloc;
  418. alloc.realloc = _PyMem_DebugRealloc;
  419. alloc.free = _PyMem_DebugFree;
  420. set_allocator_unlocked(domain, &alloc);
  421. }
  422. else if (domain == PYMEM_DOMAIN_OBJ) {
  423. if (_PyObject.malloc == _PyMem_DebugMalloc) {
  424. return;
  425. }
  426. get_allocator_unlocked(domain, &_PyMem_Debug.obj.alloc);
  427. alloc.ctx = &_PyMem_Debug.obj;
  428. alloc.malloc = _PyMem_DebugMalloc;
  429. alloc.calloc = _PyMem_DebugCalloc;
  430. alloc.realloc = _PyMem_DebugRealloc;
  431. alloc.free = _PyMem_DebugFree;
  432. set_allocator_unlocked(domain, &alloc);
  433. }
  434. }
  435. static void
  436. set_up_debug_hooks_unlocked(void)
  437. {
  438. set_up_debug_hooks_domain_unlocked(PYMEM_DOMAIN_RAW);
  439. set_up_debug_hooks_domain_unlocked(PYMEM_DOMAIN_MEM);
  440. set_up_debug_hooks_domain_unlocked(PYMEM_DOMAIN_OBJ);
  441. }
  442. void
  443. PyMem_SetupDebugHooks(void)
  444. {
  445. if (ALLOCATORS_MUTEX == NULL) {
  446. /* The runtime must not be completely initialized yet. */
  447. set_up_debug_hooks_unlocked();
  448. return;
  449. }
  450. PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
  451. set_up_debug_hooks_unlocked();
  452. PyThread_release_lock(ALLOCATORS_MUTEX);
  453. }
  454. static void
  455. get_allocator_unlocked(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
  456. {
  457. switch(domain)
  458. {
  459. case PYMEM_DOMAIN_RAW: *allocator = _PyMem_Raw; break;
  460. case PYMEM_DOMAIN_MEM: *allocator = _PyMem; break;
  461. case PYMEM_DOMAIN_OBJ: *allocator = _PyObject; break;
  462. default:
  463. /* unknown domain: set all attributes to NULL */
  464. allocator->ctx = NULL;
  465. allocator->malloc = NULL;
  466. allocator->calloc = NULL;
  467. allocator->realloc = NULL;
  468. allocator->free = NULL;
  469. }
  470. }
  471. static void
  472. set_allocator_unlocked(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
  473. {
  474. switch(domain)
  475. {
  476. case PYMEM_DOMAIN_RAW: _PyMem_Raw = *allocator; break;
  477. case PYMEM_DOMAIN_MEM: _PyMem = *allocator; break;
  478. case PYMEM_DOMAIN_OBJ: _PyObject = *allocator; break;
  479. /* ignore unknown domain */
  480. }
  481. }
  482. void
  483. PyMem_GetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
  484. {
  485. if (ALLOCATORS_MUTEX == NULL) {
  486. /* The runtime must not be completely initialized yet. */
  487. get_allocator_unlocked(domain, allocator);
  488. return;
  489. }
  490. PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
  491. get_allocator_unlocked(domain, allocator);
  492. PyThread_release_lock(ALLOCATORS_MUTEX);
  493. }
  494. void
  495. PyMem_SetAllocator(PyMemAllocatorDomain domain, PyMemAllocatorEx *allocator)
  496. {
  497. if (ALLOCATORS_MUTEX == NULL) {
  498. /* The runtime must not be completely initialized yet. */
  499. set_allocator_unlocked(domain, allocator);
  500. return;
  501. }
  502. PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
  503. set_allocator_unlocked(domain, allocator);
  504. PyThread_release_lock(ALLOCATORS_MUTEX);
  505. }
  506. void
  507. PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator)
  508. {
  509. if (ALLOCATORS_MUTEX == NULL) {
  510. /* The runtime must not be completely initialized yet. */
  511. *allocator = _PyObject_Arena;
  512. return;
  513. }
  514. PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
  515. *allocator = _PyObject_Arena;
  516. PyThread_release_lock(ALLOCATORS_MUTEX);
  517. }
  518. void
  519. PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator)
  520. {
  521. if (ALLOCATORS_MUTEX == NULL) {
  522. /* The runtime must not be completely initialized yet. */
  523. _PyObject_Arena = *allocator;
  524. return;
  525. }
  526. PyThread_acquire_lock(ALLOCATORS_MUTEX, WAIT_LOCK);
  527. _PyObject_Arena = *allocator;
  528. PyThread_release_lock(ALLOCATORS_MUTEX);
  529. }
  530. /* Note that there is a possible, but very unlikely, race in any place
  531. * below where we call one of the allocator functions. We access two
  532. * fields in each case: "malloc", etc. and "ctx".
  533. *
  534. * It is unlikely that the allocator will be changed while one of those
  535. * calls is happening, much less in that very narrow window.
  536. * Furthermore, the likelihood of a race is drastically reduced by the
  537. * fact that the allocator may not be changed after runtime init
  538. * (except with a wrapper).
  539. *
  540. * With the above in mind, we currently don't worry about locking
  541. * around these uses of the runtime-global allocators state. */
  542. /*************************/
  543. /* the "arena" allocator */
  544. /*************************/
  545. void *
  546. _PyObject_VirtualAlloc(size_t size)
  547. {
  548. return _PyObject_Arena.alloc(_PyObject_Arena.ctx, size);
  549. }
  550. void
  551. _PyObject_VirtualFree(void *obj, size_t size)
  552. {
  553. _PyObject_Arena.free(_PyObject_Arena.ctx, obj, size);
  554. }
  555. /***********************/
  556. /* the "raw" allocator */
  557. /***********************/
  558. void *
  559. PyMem_RawMalloc(size_t size)
  560. {
  561. /*
  562. * Limit ourselves to PY_SSIZE_T_MAX bytes to prevent security holes.
  563. * Most python internals blindly use a signed Py_ssize_t to track
  564. * things without checking for overflows or negatives.
  565. * As size_t is unsigned, checking for size < 0 is not required.
  566. */
  567. if (size > (size_t)PY_SSIZE_T_MAX)
  568. return NULL;
  569. return _PyMem_Raw.malloc(_PyMem_Raw.ctx, size);
  570. }
  571. void *
  572. PyMem_RawCalloc(size_t nelem, size_t elsize)
  573. {
  574. /* see PyMem_RawMalloc() */
  575. if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
  576. return NULL;
  577. return _PyMem_Raw.calloc(_PyMem_Raw.ctx, nelem, elsize);
  578. }
  579. void*
  580. PyMem_RawRealloc(void *ptr, size_t new_size)
  581. {
  582. /* see PyMem_RawMalloc() */
  583. if (new_size > (size_t)PY_SSIZE_T_MAX)
  584. return NULL;
  585. return _PyMem_Raw.realloc(_PyMem_Raw.ctx, ptr, new_size);
  586. }
  587. void PyMem_RawFree(void *ptr)
  588. {
  589. _PyMem_Raw.free(_PyMem_Raw.ctx, ptr);
  590. }
  591. /***********************/
  592. /* the "mem" allocator */
  593. /***********************/
  594. void *
  595. PyMem_Malloc(size_t size)
  596. {
  597. /* see PyMem_RawMalloc() */
  598. if (size > (size_t)PY_SSIZE_T_MAX)
  599. return NULL;
  600. OBJECT_STAT_INC_COND(allocations512, size < 512);
  601. OBJECT_STAT_INC_COND(allocations4k, size >= 512 && size < 4094);
  602. OBJECT_STAT_INC_COND(allocations_big, size >= 4094);
  603. OBJECT_STAT_INC(allocations);
  604. return _PyMem.malloc(_PyMem.ctx, size);
  605. }
  606. void *
  607. PyMem_Calloc(size_t nelem, size_t elsize)
  608. {
  609. /* see PyMem_RawMalloc() */
  610. if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
  611. return NULL;
  612. OBJECT_STAT_INC_COND(allocations512, elsize < 512);
  613. OBJECT_STAT_INC_COND(allocations4k, elsize >= 512 && elsize < 4094);
  614. OBJECT_STAT_INC_COND(allocations_big, elsize >= 4094);
  615. OBJECT_STAT_INC(allocations);
  616. return _PyMem.calloc(_PyMem.ctx, nelem, elsize);
  617. }
  618. void *
  619. PyMem_Realloc(void *ptr, size_t new_size)
  620. {
  621. /* see PyMem_RawMalloc() */
  622. if (new_size > (size_t)PY_SSIZE_T_MAX)
  623. return NULL;
  624. return _PyMem.realloc(_PyMem.ctx, ptr, new_size);
  625. }
  626. void
  627. PyMem_Free(void *ptr)
  628. {
  629. OBJECT_STAT_INC(frees);
  630. _PyMem.free(_PyMem.ctx, ptr);
  631. }
  632. /***************************/
  633. /* pymem utility functions */
  634. /***************************/
  635. wchar_t*
  636. _PyMem_RawWcsdup(const wchar_t *str)
  637. {
  638. assert(str != NULL);
  639. size_t len = wcslen(str);
  640. if (len > (size_t)PY_SSIZE_T_MAX / sizeof(wchar_t) - 1) {
  641. return NULL;
  642. }
  643. size_t size = (len + 1) * sizeof(wchar_t);
  644. wchar_t *str2 = PyMem_RawMalloc(size);
  645. if (str2 == NULL) {
  646. return NULL;
  647. }
  648. memcpy(str2, str, size);
  649. return str2;
  650. }
  651. char *
  652. _PyMem_RawStrdup(const char *str)
  653. {
  654. assert(str != NULL);
  655. size_t size = strlen(str) + 1;
  656. char *copy = PyMem_RawMalloc(size);
  657. if (copy == NULL) {
  658. return NULL;
  659. }
  660. memcpy(copy, str, size);
  661. return copy;
  662. }
  663. char *
  664. _PyMem_Strdup(const char *str)
  665. {
  666. assert(str != NULL);
  667. size_t size = strlen(str) + 1;
  668. char *copy = PyMem_Malloc(size);
  669. if (copy == NULL) {
  670. return NULL;
  671. }
  672. memcpy(copy, str, size);
  673. return copy;
  674. }
  675. /**************************/
  676. /* the "object" allocator */
  677. /**************************/
  678. void *
  679. PyObject_Malloc(size_t size)
  680. {
  681. /* see PyMem_RawMalloc() */
  682. if (size > (size_t)PY_SSIZE_T_MAX)
  683. return NULL;
  684. OBJECT_STAT_INC_COND(allocations512, size < 512);
  685. OBJECT_STAT_INC_COND(allocations4k, size >= 512 && size < 4094);
  686. OBJECT_STAT_INC_COND(allocations_big, size >= 4094);
  687. OBJECT_STAT_INC(allocations);
  688. return _PyObject.malloc(_PyObject.ctx, size);
  689. }
  690. void *
  691. PyObject_Calloc(size_t nelem, size_t elsize)
  692. {
  693. /* see PyMem_RawMalloc() */
  694. if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
  695. return NULL;
  696. OBJECT_STAT_INC_COND(allocations512, elsize < 512);
  697. OBJECT_STAT_INC_COND(allocations4k, elsize >= 512 && elsize < 4094);
  698. OBJECT_STAT_INC_COND(allocations_big, elsize >= 4094);
  699. OBJECT_STAT_INC(allocations);
  700. return _PyObject.calloc(_PyObject.ctx, nelem, elsize);
  701. }
  702. void *
  703. PyObject_Realloc(void *ptr, size_t new_size)
  704. {
  705. /* see PyMem_RawMalloc() */
  706. if (new_size > (size_t)PY_SSIZE_T_MAX)
  707. return NULL;
  708. return _PyObject.realloc(_PyObject.ctx, ptr, new_size);
  709. }
  710. void
  711. PyObject_Free(void *ptr)
  712. {
  713. OBJECT_STAT_INC(frees);
  714. _PyObject.free(_PyObject.ctx, ptr);
  715. }
  716. /* If we're using GCC, use __builtin_expect() to reduce overhead of
  717. the valgrind checks */
  718. #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
  719. # define UNLIKELY(value) __builtin_expect((value), 0)
  720. # define LIKELY(value) __builtin_expect((value), 1)
  721. #else
  722. # define UNLIKELY(value) (value)
  723. # define LIKELY(value) (value)
  724. #endif
  725. #ifdef WITH_PYMALLOC
  726. #ifdef WITH_VALGRIND
  727. #include <valgrind/valgrind.h>
  728. /* -1 indicates that we haven't checked that we're running on valgrind yet. */
  729. static int running_on_valgrind = -1;
  730. #endif
  731. typedef struct _obmalloc_state OMState;
  732. static inline int
  733. has_own_state(PyInterpreterState *interp)
  734. {
  735. return (_Py_IsMainInterpreter(interp) ||
  736. !(interp->feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC) ||
  737. _Py_IsMainInterpreterFinalizing(interp));
  738. }
  739. static inline OMState *
  740. get_state(void)
  741. {
  742. PyInterpreterState *interp = _PyInterpreterState_GET();
  743. if (!has_own_state(interp)) {
  744. interp = _PyInterpreterState_Main();
  745. }
  746. return &interp->obmalloc;
  747. }
  748. // These macros all rely on a local "state" variable.
  749. #define usedpools (state->pools.used)
  750. #define allarenas (state->mgmt.arenas)
  751. #define maxarenas (state->mgmt.maxarenas)
  752. #define unused_arena_objects (state->mgmt.unused_arena_objects)
  753. #define usable_arenas (state->mgmt.usable_arenas)
  754. #define nfp2lasta (state->mgmt.nfp2lasta)
  755. #define narenas_currently_allocated (state->mgmt.narenas_currently_allocated)
  756. #define ntimes_arena_allocated (state->mgmt.ntimes_arena_allocated)
  757. #define narenas_highwater (state->mgmt.narenas_highwater)
  758. #define raw_allocated_blocks (state->mgmt.raw_allocated_blocks)
  759. Py_ssize_t
  760. _PyInterpreterState_GetAllocatedBlocks(PyInterpreterState *interp)
  761. {
  762. #ifdef Py_DEBUG
  763. assert(has_own_state(interp));
  764. #else
  765. if (!has_own_state(interp)) {
  766. _Py_FatalErrorFunc(__func__,
  767. "the interpreter doesn't have its own allocator");
  768. }
  769. #endif
  770. OMState *state = &interp->obmalloc;
  771. Py_ssize_t n = raw_allocated_blocks;
  772. /* add up allocated blocks for used pools */
  773. for (uint i = 0; i < maxarenas; ++i) {
  774. /* Skip arenas which are not allocated. */
  775. if (allarenas[i].address == 0) {
  776. continue;
  777. }
  778. uintptr_t base = (uintptr_t)_Py_ALIGN_UP(allarenas[i].address, POOL_SIZE);
  779. /* visit every pool in the arena */
  780. assert(base <= (uintptr_t) allarenas[i].pool_address);
  781. for (; base < (uintptr_t) allarenas[i].pool_address; base += POOL_SIZE) {
  782. poolp p = (poolp)base;
  783. n += p->ref.count;
  784. }
  785. }
  786. return n;
  787. }
  788. void
  789. _PyInterpreterState_FinalizeAllocatedBlocks(PyInterpreterState *interp)
  790. {
  791. if (has_own_state(interp)) {
  792. Py_ssize_t leaked = _PyInterpreterState_GetAllocatedBlocks(interp);
  793. assert(has_own_state(interp) || leaked == 0);
  794. interp->runtime->obmalloc.interpreter_leaks += leaked;
  795. }
  796. }
  797. static Py_ssize_t get_num_global_allocated_blocks(_PyRuntimeState *);
  798. /* We preserve the number of blockss leaked during runtime finalization,
  799. so they can be reported if the runtime is initialized again. */
  800. // XXX We don't lose any information by dropping this,
  801. // so we should consider doing so.
  802. static Py_ssize_t last_final_leaks = 0;
  803. void
  804. _Py_FinalizeAllocatedBlocks(_PyRuntimeState *runtime)
  805. {
  806. last_final_leaks = get_num_global_allocated_blocks(runtime);
  807. runtime->obmalloc.interpreter_leaks = 0;
  808. }
  809. static Py_ssize_t
  810. get_num_global_allocated_blocks(_PyRuntimeState *runtime)
  811. {
  812. Py_ssize_t total = 0;
  813. if (_PyRuntimeState_GetFinalizing(runtime) != NULL) {
  814. PyInterpreterState *interp = _PyInterpreterState_Main();
  815. if (interp == NULL) {
  816. /* We are at the very end of runtime finalization.
  817. We can't rely on finalizing->interp since that thread
  818. state is probably already freed, so we don't worry
  819. about it. */
  820. assert(PyInterpreterState_Head() == NULL);
  821. }
  822. else {
  823. assert(interp != NULL);
  824. /* It is probably the last interpreter but not necessarily. */
  825. assert(PyInterpreterState_Next(interp) == NULL);
  826. total += _PyInterpreterState_GetAllocatedBlocks(interp);
  827. }
  828. }
  829. else {
  830. HEAD_LOCK(runtime);
  831. PyInterpreterState *interp = PyInterpreterState_Head();
  832. assert(interp != NULL);
  833. #ifdef Py_DEBUG
  834. int got_main = 0;
  835. #endif
  836. for (; interp != NULL; interp = PyInterpreterState_Next(interp)) {
  837. #ifdef Py_DEBUG
  838. if (_Py_IsMainInterpreter(interp)) {
  839. assert(!got_main);
  840. got_main = 1;
  841. assert(has_own_state(interp));
  842. }
  843. #endif
  844. if (has_own_state(interp)) {
  845. total += _PyInterpreterState_GetAllocatedBlocks(interp);
  846. }
  847. }
  848. HEAD_UNLOCK(runtime);
  849. #ifdef Py_DEBUG
  850. assert(got_main);
  851. #endif
  852. }
  853. total += runtime->obmalloc.interpreter_leaks;
  854. total += last_final_leaks;
  855. return total;
  856. }
  857. Py_ssize_t
  858. _Py_GetGlobalAllocatedBlocks(void)
  859. {
  860. return get_num_global_allocated_blocks(&_PyRuntime);
  861. }
  862. #if WITH_PYMALLOC_RADIX_TREE
  863. /*==========================================================================*/
  864. /* radix tree for tracking arena usage. */
  865. #define arena_map_root (state->usage.arena_map_root)
  866. #ifdef USE_INTERIOR_NODES
  867. #define arena_map_mid_count (state->usage.arena_map_mid_count)
  868. #define arena_map_bot_count (state->usage.arena_map_bot_count)
  869. #endif
  870. /* Return a pointer to a bottom tree node, return NULL if it doesn't exist or
  871. * it cannot be created */
  872. static inline Py_ALWAYS_INLINE arena_map_bot_t *
  873. arena_map_get(OMState *state, pymem_block *p, int create)
  874. {
  875. #ifdef USE_INTERIOR_NODES
  876. /* sanity check that IGNORE_BITS is correct */
  877. assert(HIGH_BITS(p) == HIGH_BITS(&arena_map_root));
  878. int i1 = MAP_TOP_INDEX(p);
  879. if (arena_map_root.ptrs[i1] == NULL) {
  880. if (!create) {
  881. return NULL;
  882. }
  883. arena_map_mid_t *n = PyMem_RawCalloc(1, sizeof(arena_map_mid_t));
  884. if (n == NULL) {
  885. return NULL;
  886. }
  887. arena_map_root.ptrs[i1] = n;
  888. arena_map_mid_count++;
  889. }
  890. int i2 = MAP_MID_INDEX(p);
  891. if (arena_map_root.ptrs[i1]->ptrs[i2] == NULL) {
  892. if (!create) {
  893. return NULL;
  894. }
  895. arena_map_bot_t *n = PyMem_RawCalloc(1, sizeof(arena_map_bot_t));
  896. if (n == NULL) {
  897. return NULL;
  898. }
  899. arena_map_root.ptrs[i1]->ptrs[i2] = n;
  900. arena_map_bot_count++;
  901. }
  902. return arena_map_root.ptrs[i1]->ptrs[i2];
  903. #else
  904. return &arena_map_root;
  905. #endif
  906. }
  907. /* The radix tree only tracks arenas. So, for 16 MiB arenas, we throw
  908. * away 24 bits of the address. That reduces the space requirement of
  909. * the tree compared to similar radix tree page-map schemes. In
  910. * exchange for slashing the space requirement, it needs more
  911. * computation to check an address.
  912. *
  913. * Tracking coverage is done by "ideal" arena address. It is easier to
  914. * explain in decimal so let's say that the arena size is 100 bytes.
  915. * Then, ideal addresses are 100, 200, 300, etc. For checking if a
  916. * pointer address is inside an actual arena, we have to check two ideal
  917. * arena addresses. E.g. if pointer is 357, we need to check 200 and
  918. * 300. In the rare case that an arena is aligned in the ideal way
  919. * (e.g. base address of arena is 200) then we only have to check one
  920. * ideal address.
  921. *
  922. * The tree nodes for 200 and 300 both store the address of arena.
  923. * There are two cases: the arena starts at a lower ideal arena and
  924. * extends to this one, or the arena starts in this arena and extends to
  925. * the next ideal arena. The tail_lo and tail_hi members correspond to
  926. * these two cases.
  927. */
  928. /* mark or unmark addresses covered by arena */
  929. static int
  930. arena_map_mark_used(OMState *state, uintptr_t arena_base, int is_used)
  931. {
  932. /* sanity check that IGNORE_BITS is correct */
  933. assert(HIGH_BITS(arena_base) == HIGH_BITS(&arena_map_root));
  934. arena_map_bot_t *n_hi = arena_map_get(
  935. state, (pymem_block *)arena_base, is_used);
  936. if (n_hi == NULL) {
  937. assert(is_used); /* otherwise node should already exist */
  938. return 0; /* failed to allocate space for node */
  939. }
  940. int i3 = MAP_BOT_INDEX((pymem_block *)arena_base);
  941. int32_t tail = (int32_t)(arena_base & ARENA_SIZE_MASK);
  942. if (tail == 0) {
  943. /* is ideal arena address */
  944. n_hi->arenas[i3].tail_hi = is_used ? -1 : 0;
  945. }
  946. else {
  947. /* arena_base address is not ideal (aligned to arena size) and
  948. * so it potentially covers two MAP_BOT nodes. Get the MAP_BOT node
  949. * for the next arena. Note that it might be in different MAP_TOP
  950. * and MAP_MID nodes as well so we need to call arena_map_get()
  951. * again (do the full tree traversal).
  952. */
  953. n_hi->arenas[i3].tail_hi = is_used ? tail : 0;
  954. uintptr_t arena_base_next = arena_base + ARENA_SIZE;
  955. /* If arena_base is a legit arena address, so is arena_base_next - 1
  956. * (last address in arena). If arena_base_next overflows then it
  957. * must overflow to 0. However, that would mean arena_base was
  958. * "ideal" and we should not be in this case. */
  959. assert(arena_base < arena_base_next);
  960. arena_map_bot_t *n_lo = arena_map_get(
  961. state, (pymem_block *)arena_base_next, is_used);
  962. if (n_lo == NULL) {
  963. assert(is_used); /* otherwise should already exist */
  964. n_hi->arenas[i3].tail_hi = 0;
  965. return 0; /* failed to allocate space for node */
  966. }
  967. int i3_next = MAP_BOT_INDEX(arena_base_next);
  968. n_lo->arenas[i3_next].tail_lo = is_used ? tail : 0;
  969. }
  970. return 1;
  971. }
  972. /* Return true if 'p' is a pointer inside an obmalloc arena.
  973. * _PyObject_Free() calls this so it needs to be very fast. */
  974. static int
  975. arena_map_is_used(OMState *state, pymem_block *p)
  976. {
  977. arena_map_bot_t *n = arena_map_get(state, p, 0);
  978. if (n == NULL) {
  979. return 0;
  980. }
  981. int i3 = MAP_BOT_INDEX(p);
  982. /* ARENA_BITS must be < 32 so that the tail is a non-negative int32_t. */
  983. int32_t hi = n->arenas[i3].tail_hi;
  984. int32_t lo = n->arenas[i3].tail_lo;
  985. int32_t tail = (int32_t)(AS_UINT(p) & ARENA_SIZE_MASK);
  986. return (tail < lo) || (tail >= hi && hi != 0);
  987. }
  988. /* end of radix tree logic */
  989. /*==========================================================================*/
  990. #endif /* WITH_PYMALLOC_RADIX_TREE */
  991. /* Allocate a new arena. If we run out of memory, return NULL. Else
  992. * allocate a new arena, and return the address of an arena_object
  993. * describing the new arena. It's expected that the caller will set
  994. * `usable_arenas` to the return value.
  995. */
  996. static struct arena_object*
  997. new_arena(OMState *state)
  998. {
  999. struct arena_object* arenaobj;
  1000. uint excess; /* number of bytes above pool alignment */
  1001. void *address;
  1002. int debug_stats = _PyRuntime.obmalloc.dump_debug_stats;
  1003. if (debug_stats == -1) {
  1004. const char *opt = Py_GETENV("PYTHONMALLOCSTATS");
  1005. debug_stats = (opt != NULL && *opt != '\0');
  1006. _PyRuntime.obmalloc.dump_debug_stats = debug_stats;
  1007. }
  1008. if (debug_stats) {
  1009. _PyObject_DebugMallocStats(stderr);
  1010. }
  1011. if (unused_arena_objects == NULL) {
  1012. uint i;
  1013. uint numarenas;
  1014. size_t nbytes;
  1015. /* Double the number of arena objects on each allocation.
  1016. * Note that it's possible for `numarenas` to overflow.
  1017. */
  1018. numarenas = maxarenas ? maxarenas << 1 : INITIAL_ARENA_OBJECTS;
  1019. if (numarenas <= maxarenas)
  1020. return NULL; /* overflow */
  1021. #if SIZEOF_SIZE_T <= SIZEOF_INT
  1022. if (numarenas > SIZE_MAX / sizeof(*allarenas))
  1023. return NULL; /* overflow */
  1024. #endif
  1025. nbytes = numarenas * sizeof(*allarenas);
  1026. arenaobj = (struct arena_object *)PyMem_RawRealloc(allarenas, nbytes);
  1027. if (arenaobj == NULL)
  1028. return NULL;
  1029. allarenas = arenaobj;
  1030. /* We might need to fix pointers that were copied. However,
  1031. * new_arena only gets called when all the pages in the
  1032. * previous arenas are full. Thus, there are *no* pointers
  1033. * into the old array. Thus, we don't have to worry about
  1034. * invalid pointers. Just to be sure, some asserts:
  1035. */
  1036. assert(usable_arenas == NULL);
  1037. assert(unused_arena_objects == NULL);
  1038. /* Put the new arenas on the unused_arena_objects list. */
  1039. for (i = maxarenas; i < numarenas; ++i) {
  1040. allarenas[i].address = 0; /* mark as unassociated */
  1041. allarenas[i].nextarena = i < numarenas - 1 ?
  1042. &allarenas[i+1] : NULL;
  1043. }
  1044. /* Update globals. */
  1045. unused_arena_objects = &allarenas[maxarenas];
  1046. maxarenas = numarenas;
  1047. }
  1048. /* Take the next available arena object off the head of the list. */
  1049. assert(unused_arena_objects != NULL);
  1050. arenaobj = unused_arena_objects;
  1051. unused_arena_objects = arenaobj->nextarena;
  1052. assert(arenaobj->address == 0);
  1053. address = _PyObject_Arena.alloc(_PyObject_Arena.ctx, ARENA_SIZE);
  1054. #if WITH_PYMALLOC_RADIX_TREE
  1055. if (address != NULL) {
  1056. if (!arena_map_mark_used(state, (uintptr_t)address, 1)) {
  1057. /* marking arena in radix tree failed, abort */
  1058. _PyObject_Arena.free(_PyObject_Arena.ctx, address, ARENA_SIZE);
  1059. address = NULL;
  1060. }
  1061. }
  1062. #endif
  1063. if (address == NULL) {
  1064. /* The allocation failed: return NULL after putting the
  1065. * arenaobj back.
  1066. */
  1067. arenaobj->nextarena = unused_arena_objects;
  1068. unused_arena_objects = arenaobj;
  1069. return NULL;
  1070. }
  1071. arenaobj->address = (uintptr_t)address;
  1072. ++narenas_currently_allocated;
  1073. ++ntimes_arena_allocated;
  1074. if (narenas_currently_allocated > narenas_highwater)
  1075. narenas_highwater = narenas_currently_allocated;
  1076. arenaobj->freepools = NULL;
  1077. /* pool_address <- first pool-aligned address in the arena
  1078. nfreepools <- number of whole pools that fit after alignment */
  1079. arenaobj->pool_address = (pymem_block*)arenaobj->address;
  1080. arenaobj->nfreepools = MAX_POOLS_IN_ARENA;
  1081. excess = (uint)(arenaobj->address & POOL_SIZE_MASK);
  1082. if (excess != 0) {
  1083. --arenaobj->nfreepools;
  1084. arenaobj->pool_address += POOL_SIZE - excess;
  1085. }
  1086. arenaobj->ntotalpools = arenaobj->nfreepools;
  1087. return arenaobj;
  1088. }
  1089. #if WITH_PYMALLOC_RADIX_TREE
  1090. /* Return true if and only if P is an address that was allocated by
  1091. pymalloc. When the radix tree is used, 'poolp' is unused.
  1092. */
  1093. static bool
  1094. address_in_range(OMState *state, void *p, poolp Py_UNUSED(pool))
  1095. {
  1096. return arena_map_is_used(state, p);
  1097. }
  1098. #else
  1099. /*
  1100. address_in_range(P, POOL)
  1101. Return true if and only if P is an address that was allocated by pymalloc.
  1102. POOL must be the pool address associated with P, i.e., POOL = POOL_ADDR(P)
  1103. (the caller is asked to compute this because the macro expands POOL more than
  1104. once, and for efficiency it's best for the caller to assign POOL_ADDR(P) to a
  1105. variable and pass the latter to the macro; because address_in_range is
  1106. called on every alloc/realloc/free, micro-efficiency is important here).
  1107. Tricky: Let B be the arena base address associated with the pool, B =
  1108. arenas[(POOL)->arenaindex].address. Then P belongs to the arena if and only if
  1109. B <= P < B + ARENA_SIZE
  1110. Subtracting B throughout, this is true iff
  1111. 0 <= P-B < ARENA_SIZE
  1112. By using unsigned arithmetic, the "0 <=" half of the test can be skipped.
  1113. Obscure: A PyMem "free memory" function can call the pymalloc free or realloc
  1114. before the first arena has been allocated. `arenas` is still NULL in that
  1115. case. We're relying on that maxarenas is also 0 in that case, so that
  1116. (POOL)->arenaindex < maxarenas must be false, saving us from trying to index
  1117. into a NULL arenas.
  1118. Details: given P and POOL, the arena_object corresponding to P is AO =
  1119. arenas[(POOL)->arenaindex]. Suppose obmalloc controls P. Then (barring wild
  1120. stores, etc), POOL is the correct address of P's pool, AO.address is the
  1121. correct base address of the pool's arena, and P must be within ARENA_SIZE of
  1122. AO.address. In addition, AO.address is not 0 (no arena can start at address 0
  1123. (NULL)). Therefore address_in_range correctly reports that obmalloc
  1124. controls P.
  1125. Now suppose obmalloc does not control P (e.g., P was obtained via a direct
  1126. call to the system malloc() or realloc()). (POOL)->arenaindex may be anything
  1127. in this case -- it may even be uninitialized trash. If the trash arenaindex
  1128. is >= maxarenas, the macro correctly concludes at once that obmalloc doesn't
  1129. control P.
  1130. Else arenaindex is < maxarena, and AO is read up. If AO corresponds to an
  1131. allocated arena, obmalloc controls all the memory in slice AO.address :
  1132. AO.address+ARENA_SIZE. By case assumption, P is not controlled by obmalloc,
  1133. so P doesn't lie in that slice, so the macro correctly reports that P is not
  1134. controlled by obmalloc.
  1135. Finally, if P is not controlled by obmalloc and AO corresponds to an unused
  1136. arena_object (one not currently associated with an allocated arena),
  1137. AO.address is 0, and the second test in the macro reduces to:
  1138. P < ARENA_SIZE
  1139. If P >= ARENA_SIZE (extremely likely), the macro again correctly concludes
  1140. that P is not controlled by obmalloc. However, if P < ARENA_SIZE, this part
  1141. of the test still passes, and the third clause (AO.address != 0) is necessary
  1142. to get the correct result: AO.address is 0 in this case, so the macro
  1143. correctly reports that P is not controlled by obmalloc (despite that P lies in
  1144. slice AO.address : AO.address + ARENA_SIZE).
  1145. Note: The third (AO.address != 0) clause was added in Python 2.5. Before
  1146. 2.5, arenas were never free()'ed, and an arenaindex < maxarena always
  1147. corresponded to a currently-allocated arena, so the "P is not controlled by
  1148. obmalloc, AO corresponds to an unused arena_object, and P < ARENA_SIZE" case
  1149. was impossible.
  1150. Note that the logic is excruciating, and reading up possibly uninitialized
  1151. memory when P is not controlled by obmalloc (to get at (POOL)->arenaindex)
  1152. creates problems for some memory debuggers. The overwhelming advantage is
  1153. that this test determines whether an arbitrary address is controlled by
  1154. obmalloc in a small constant time, independent of the number of arenas
  1155. obmalloc controls. Since this test is needed at every entry point, it's
  1156. extremely desirable that it be this fast.
  1157. */
  1158. static bool _Py_NO_SANITIZE_ADDRESS
  1159. _Py_NO_SANITIZE_THREAD
  1160. _Py_NO_SANITIZE_MEMORY
  1161. address_in_range(OMState *state, void *p, poolp pool)
  1162. {
  1163. // Since address_in_range may be reading from memory which was not allocated
  1164. // by Python, it is important that pool->arenaindex is read only once, as
  1165. // another thread may be concurrently modifying the value without holding
  1166. // the GIL. The following dance forces the compiler to read pool->arenaindex
  1167. // only once.
  1168. uint arenaindex = *((volatile uint *)&pool->arenaindex);
  1169. return arenaindex < maxarenas &&
  1170. (uintptr_t)p - allarenas[arenaindex].address < ARENA_SIZE &&
  1171. allarenas[arenaindex].address != 0;
  1172. }
  1173. #endif /* !WITH_PYMALLOC_RADIX_TREE */
  1174. /*==========================================================================*/
  1175. // Called when freelist is exhausted. Extend the freelist if there is
  1176. // space for a block. Otherwise, remove this pool from usedpools.
  1177. static void
  1178. pymalloc_pool_extend(poolp pool, uint size)
  1179. {
  1180. if (UNLIKELY(pool->nextoffset <= pool->maxnextoffset)) {
  1181. /* There is room for another block. */
  1182. pool->freeblock = (pymem_block*)pool + pool->nextoffset;
  1183. pool->nextoffset += INDEX2SIZE(size);
  1184. *(pymem_block **)(pool->freeblock) = NULL;
  1185. return;
  1186. }
  1187. /* Pool is full, unlink from used pools. */
  1188. poolp next;
  1189. next = pool->nextpool;
  1190. pool = pool->prevpool;
  1191. next->prevpool = pool;
  1192. pool->nextpool = next;
  1193. }
  1194. /* called when pymalloc_alloc can not allocate a block from usedpool.
  1195. * This function takes new pool and allocate a block from it.
  1196. */
  1197. static void*
  1198. allocate_from_new_pool(OMState *state, uint size)
  1199. {
  1200. /* There isn't a pool of the right size class immediately
  1201. * available: use a free pool.
  1202. */
  1203. if (UNLIKELY(usable_arenas == NULL)) {
  1204. /* No arena has a free pool: allocate a new arena. */
  1205. #ifdef WITH_MEMORY_LIMITS
  1206. if (narenas_currently_allocated >= MAX_ARENAS) {
  1207. return NULL;
  1208. }
  1209. #endif
  1210. usable_arenas = new_arena(state);
  1211. if (usable_arenas == NULL) {
  1212. return NULL;
  1213. }
  1214. usable_arenas->nextarena = usable_arenas->prevarena = NULL;
  1215. assert(nfp2lasta[usable_arenas->nfreepools] == NULL);
  1216. nfp2lasta[usable_arenas->nfreepools] = usable_arenas;
  1217. }
  1218. assert(usable_arenas->address != 0);
  1219. /* This arena already had the smallest nfreepools value, so decreasing
  1220. * nfreepools doesn't change that, and we don't need to rearrange the
  1221. * usable_arenas list. However, if the arena becomes wholly allocated,
  1222. * we need to remove its arena_object from usable_arenas.
  1223. */
  1224. assert(usable_arenas->nfreepools > 0);
  1225. if (nfp2lasta[usable_arenas->nfreepools] == usable_arenas) {
  1226. /* It's the last of this size, so there won't be any. */
  1227. nfp2lasta[usable_arenas->nfreepools] = NULL;
  1228. }
  1229. /* If any free pools will remain, it will be the new smallest. */
  1230. if (usable_arenas->nfreepools > 1) {
  1231. assert(nfp2lasta[usable_arenas->nfreepools - 1] == NULL);
  1232. nfp2lasta[usable_arenas->nfreepools - 1] = usable_arenas;
  1233. }
  1234. /* Try to get a cached free pool. */
  1235. poolp pool = usable_arenas->freepools;
  1236. if (LIKELY(pool != NULL)) {
  1237. /* Unlink from cached pools. */
  1238. usable_arenas->freepools = pool->nextpool;
  1239. usable_arenas->nfreepools--;
  1240. if (UNLIKELY(usable_arenas->nfreepools == 0)) {
  1241. /* Wholly allocated: remove. */
  1242. assert(usable_arenas->freepools == NULL);
  1243. assert(usable_arenas->nextarena == NULL ||
  1244. usable_arenas->nextarena->prevarena ==
  1245. usable_arenas);
  1246. usable_arenas = usable_arenas->nextarena;
  1247. if (usable_arenas != NULL) {
  1248. usable_arenas->prevarena = NULL;
  1249. assert(usable_arenas->address != 0);
  1250. }
  1251. }
  1252. else {
  1253. /* nfreepools > 0: it must be that freepools
  1254. * isn't NULL, or that we haven't yet carved
  1255. * off all the arena's pools for the first
  1256. * time.
  1257. */
  1258. assert(usable_arenas->freepools != NULL ||
  1259. usable_arenas->pool_address <=
  1260. (pymem_block*)usable_arenas->address +
  1261. ARENA_SIZE - POOL_SIZE);
  1262. }
  1263. }
  1264. else {
  1265. /* Carve off a new pool. */
  1266. assert(usable_arenas->nfreepools > 0);
  1267. assert(usable_arenas->freepools == NULL);
  1268. pool = (poolp)usable_arenas->pool_address;
  1269. assert((pymem_block*)pool <= (pymem_block*)usable_arenas->address +
  1270. ARENA_SIZE - POOL_SIZE);
  1271. pool->arenaindex = (uint)(usable_arenas - allarenas);
  1272. assert(&allarenas[pool->arenaindex] == usable_arenas);
  1273. pool->szidx = DUMMY_SIZE_IDX;
  1274. usable_arenas->pool_address += POOL_SIZE;
  1275. --usable_arenas->nfreepools;
  1276. if (usable_arenas->nfreepools == 0) {
  1277. assert(usable_arenas->nextarena == NULL ||
  1278. usable_arenas->nextarena->prevarena ==
  1279. usable_arenas);
  1280. /* Unlink the arena: it is completely allocated. */
  1281. usable_arenas = usable_arenas->nextarena;
  1282. if (usable_arenas != NULL) {
  1283. usable_arenas->prevarena = NULL;
  1284. assert(usable_arenas->address != 0);
  1285. }
  1286. }
  1287. }
  1288. /* Frontlink to used pools. */
  1289. pymem_block *bp;
  1290. poolp next = usedpools[size + size]; /* == prev */
  1291. pool->nextpool = next;
  1292. pool->prevpool = next;
  1293. next->nextpool = pool;
  1294. next->prevpool = pool;
  1295. pool->ref.count = 1;
  1296. if (pool->szidx == size) {
  1297. /* Luckily, this pool last contained blocks
  1298. * of the same size class, so its header
  1299. * and free list are already initialized.
  1300. */
  1301. bp = pool->freeblock;
  1302. assert(bp != NULL);
  1303. pool->freeblock = *(pymem_block **)bp;
  1304. return bp;
  1305. }
  1306. /*
  1307. * Initialize the pool header, set up the free list to
  1308. * contain just the second block, and return the first
  1309. * block.
  1310. */
  1311. pool->szidx = size;
  1312. size = INDEX2SIZE(size);
  1313. bp = (pymem_block *)pool + POOL_OVERHEAD;
  1314. pool->nextoffset = POOL_OVERHEAD + (size << 1);
  1315. pool->maxnextoffset = POOL_SIZE - size;
  1316. pool->freeblock = bp + size;
  1317. *(pymem_block **)(pool->freeblock) = NULL;
  1318. return bp;
  1319. }
  1320. /* pymalloc allocator
  1321. Return a pointer to newly allocated memory if pymalloc allocated memory.
  1322. Return NULL if pymalloc failed to allocate the memory block: on bigger
  1323. requests, on error in the code below (as a last chance to serve the request)
  1324. or when the max memory limit has been reached.
  1325. */
  1326. static inline void*
  1327. pymalloc_alloc(OMState *state, void *Py_UNUSED(ctx), size_t nbytes)
  1328. {
  1329. #ifdef WITH_VALGRIND
  1330. if (UNLIKELY(running_on_valgrind == -1)) {
  1331. running_on_valgrind = RUNNING_ON_VALGRIND;
  1332. }
  1333. if (UNLIKELY(running_on_valgrind)) {
  1334. return NULL;
  1335. }
  1336. #endif
  1337. if (UNLIKELY(nbytes == 0)) {
  1338. return NULL;
  1339. }
  1340. if (UNLIKELY(nbytes > SMALL_REQUEST_THRESHOLD)) {
  1341. return NULL;
  1342. }
  1343. uint size = (uint)(nbytes - 1) >> ALIGNMENT_SHIFT;
  1344. poolp pool = usedpools[size + size];
  1345. pymem_block *bp;
  1346. if (LIKELY(pool != pool->nextpool)) {
  1347. /*
  1348. * There is a used pool for this size class.
  1349. * Pick up the head block of its free list.
  1350. */
  1351. ++pool->ref.count;
  1352. bp = pool->freeblock;
  1353. assert(bp != NULL);
  1354. if (UNLIKELY((pool->freeblock = *(pymem_block **)bp) == NULL)) {
  1355. // Reached the end of the free list, try to extend it.
  1356. pymalloc_pool_extend(pool, size);
  1357. }
  1358. }
  1359. else {
  1360. /* There isn't a pool of the right size class immediately
  1361. * available: use a free pool.
  1362. */
  1363. bp = allocate_from_new_pool(state, size);
  1364. }
  1365. return (void *)bp;
  1366. }
  1367. void *
  1368. _PyObject_Malloc(void *ctx, size_t nbytes)
  1369. {
  1370. OMState *state = get_state();
  1371. void* ptr = pymalloc_alloc(state, ctx, nbytes);
  1372. if (LIKELY(ptr != NULL)) {
  1373. return ptr;
  1374. }
  1375. ptr = PyMem_RawMalloc(nbytes);
  1376. if (ptr != NULL) {
  1377. raw_allocated_blocks++;
  1378. }
  1379. return ptr;
  1380. }
  1381. void *
  1382. _PyObject_Calloc(void *ctx, size_t nelem, size_t elsize)
  1383. {
  1384. assert(elsize == 0 || nelem <= (size_t)PY_SSIZE_T_MAX / elsize);
  1385. size_t nbytes = nelem * elsize;
  1386. OMState *state = get_state();
  1387. void* ptr = pymalloc_alloc(state, ctx, nbytes);
  1388. if (LIKELY(ptr != NULL)) {
  1389. memset(ptr, 0, nbytes);
  1390. return ptr;
  1391. }
  1392. ptr = PyMem_RawCalloc(nelem, elsize);
  1393. if (ptr != NULL) {
  1394. raw_allocated_blocks++;
  1395. }
  1396. return ptr;
  1397. }
  1398. static void
  1399. insert_to_usedpool(OMState *state, poolp pool)
  1400. {
  1401. assert(pool->ref.count > 0); /* else the pool is empty */
  1402. uint size = pool->szidx;
  1403. poolp next = usedpools[size + size];
  1404. poolp prev = next->prevpool;
  1405. /* insert pool before next: prev <-> pool <-> next */
  1406. pool->nextpool = next;
  1407. pool->prevpool = prev;
  1408. next->prevpool = pool;
  1409. prev->nextpool = pool;
  1410. }
  1411. static void
  1412. insert_to_freepool(OMState *state, poolp pool)
  1413. {
  1414. poolp next = pool->nextpool;
  1415. poolp prev = pool->prevpool;
  1416. next->prevpool = prev;
  1417. prev->nextpool = next;
  1418. /* Link the pool to freepools. This is a singly-linked
  1419. * list, and pool->prevpool isn't used there.
  1420. */
  1421. struct arena_object *ao = &allarenas[pool->arenaindex];
  1422. pool->nextpool = ao->freepools;
  1423. ao->freepools = pool;
  1424. uint nf = ao->nfreepools;
  1425. /* If this is the rightmost arena with this number of free pools,
  1426. * nfp2lasta[nf] needs to change. Caution: if nf is 0, there
  1427. * are no arenas in usable_arenas with that value.
  1428. */
  1429. struct arena_object* lastnf = nfp2lasta[nf];
  1430. assert((nf == 0 && lastnf == NULL) ||
  1431. (nf > 0 &&
  1432. lastnf != NULL &&
  1433. lastnf->nfreepools == nf &&
  1434. (lastnf->nextarena == NULL ||
  1435. nf < lastnf->nextarena->nfreepools)));
  1436. if (lastnf == ao) { /* it is the rightmost */
  1437. struct arena_object* p = ao->prevarena;
  1438. nfp2lasta[nf] = (p != NULL && p->nfreepools == nf) ? p : NULL;
  1439. }
  1440. ao->nfreepools = ++nf;
  1441. /* All the rest is arena management. We just freed
  1442. * a pool, and there are 4 cases for arena mgmt:
  1443. * 1. If all the pools are free, return the arena to
  1444. * the system free(). Except if this is the last
  1445. * arena in the list, keep it to avoid thrashing:
  1446. * keeping one wholly free arena in the list avoids
  1447. * pathological cases where a simple loop would
  1448. * otherwise provoke needing to allocate and free an
  1449. * arena on every iteration. See bpo-37257.
  1450. * 2. If this is the only free pool in the arena,
  1451. * add the arena back to the `usable_arenas` list.
  1452. * 3. If the "next" arena has a smaller count of free
  1453. * pools, we have to "slide this arena right" to
  1454. * restore that usable_arenas is sorted in order of
  1455. * nfreepools.
  1456. * 4. Else there's nothing more to do.
  1457. */
  1458. if (nf == ao->ntotalpools && ao->nextarena != NULL) {
  1459. /* Case 1. First unlink ao from usable_arenas.
  1460. */
  1461. assert(ao->prevarena == NULL ||
  1462. ao->prevarena->address != 0);
  1463. assert(ao ->nextarena == NULL ||
  1464. ao->nextarena->address != 0);
  1465. /* Fix the pointer in the prevarena, or the
  1466. * usable_arenas pointer.
  1467. */
  1468. if (ao->prevarena == NULL) {
  1469. usable_arenas = ao->nextarena;
  1470. assert(usable_arenas == NULL ||
  1471. usable_arenas->address != 0);
  1472. }
  1473. else {
  1474. assert(ao->prevarena->nextarena == ao);
  1475. ao->prevarena->nextarena =
  1476. ao->nextarena;
  1477. }
  1478. /* Fix the pointer in the nextarena. */
  1479. if (ao->nextarena != NULL) {
  1480. assert(ao->nextarena->prevarena == ao);
  1481. ao->nextarena->prevarena =
  1482. ao->prevarena;
  1483. }
  1484. /* Record that this arena_object slot is
  1485. * available to be reused.
  1486. */
  1487. ao->nextarena = unused_arena_objects;
  1488. unused_arena_objects = ao;
  1489. #if WITH_PYMALLOC_RADIX_TREE
  1490. /* mark arena region as not under control of obmalloc */
  1491. arena_map_mark_used(state, ao->address, 0);
  1492. #endif
  1493. /* Free the entire arena. */
  1494. _PyObject_Arena.free(_PyObject_Arena.ctx,
  1495. (void *)ao->address, ARENA_SIZE);
  1496. ao->address = 0; /* mark unassociated */
  1497. --narenas_currently_allocated;
  1498. return;
  1499. }
  1500. if (nf == 1) {
  1501. /* Case 2. Put ao at the head of
  1502. * usable_arenas. Note that because
  1503. * ao->nfreepools was 0 before, ao isn't
  1504. * currently on the usable_arenas list.
  1505. */
  1506. ao->nextarena = usable_arenas;
  1507. ao->prevarena = NULL;
  1508. if (usable_arenas)
  1509. usable_arenas->prevarena = ao;
  1510. usable_arenas = ao;
  1511. assert(usable_arenas->address != 0);
  1512. if (nfp2lasta[1] == NULL) {
  1513. nfp2lasta[1] = ao;
  1514. }
  1515. return;
  1516. }
  1517. /* If this arena is now out of order, we need to keep
  1518. * the list sorted. The list is kept sorted so that
  1519. * the "most full" arenas are used first, which allows
  1520. * the nearly empty arenas to be completely freed. In
  1521. * a few un-scientific tests, it seems like this
  1522. * approach allowed a lot more memory to be freed.
  1523. */
  1524. /* If this is the only arena with nf, record that. */
  1525. if (nfp2lasta[nf] == NULL) {
  1526. nfp2lasta[nf] = ao;
  1527. } /* else the rightmost with nf doesn't change */
  1528. /* If this was the rightmost of the old size, it remains in place. */
  1529. if (ao == lastnf) {
  1530. /* Case 4. Nothing to do. */
  1531. return;
  1532. }
  1533. /* If ao were the only arena in the list, the last block would have
  1534. * gotten us out.
  1535. */
  1536. assert(ao->nextarena != NULL);
  1537. /* Case 3: We have to move the arena towards the end of the list,
  1538. * because it has more free pools than the arena to its right. It needs
  1539. * to move to follow lastnf.
  1540. * First unlink ao from usable_arenas.
  1541. */
  1542. if (ao->prevarena != NULL) {
  1543. /* ao isn't at the head of the list */
  1544. assert(ao->prevarena->nextarena == ao);
  1545. ao->prevarena->nextarena = ao->nextarena;
  1546. }
  1547. else {
  1548. /* ao is at the head of the list */
  1549. assert(usable_arenas == ao);
  1550. usable_arenas = ao->nextarena;
  1551. }
  1552. ao->nextarena->prevarena = ao->prevarena;
  1553. /* And insert after lastnf. */
  1554. ao->prevarena = lastnf;
  1555. ao->nextarena = lastnf->nextarena;
  1556. if (ao->nextarena != NULL) {
  1557. ao->nextarena->prevarena = ao;
  1558. }
  1559. lastnf->nextarena = ao;
  1560. /* Verify that the swaps worked. */
  1561. assert(ao->nextarena == NULL || nf <= ao->nextarena->nfreepools);
  1562. assert(ao->prevarena == NULL || nf > ao->prevarena->nfreepools);
  1563. assert(ao->nextarena == NULL || ao->nextarena->prevarena == ao);
  1564. assert((usable_arenas == ao && ao->prevarena == NULL)
  1565. || ao->prevarena->nextarena == ao);
  1566. }
  1567. /* Free a memory block allocated by pymalloc_alloc().
  1568. Return 1 if it was freed.
  1569. Return 0 if the block was not allocated by pymalloc_alloc(). */
  1570. static inline int
  1571. pymalloc_free(OMState *state, void *Py_UNUSED(ctx), void *p)
  1572. {
  1573. assert(p != NULL);
  1574. #ifdef WITH_VALGRIND
  1575. if (UNLIKELY(running_on_valgrind > 0)) {
  1576. return 0;
  1577. }
  1578. #endif
  1579. poolp pool = POOL_ADDR(p);
  1580. if (UNLIKELY(!address_in_range(state, p, pool))) {
  1581. return 0;
  1582. }
  1583. /* We allocated this address. */
  1584. /* Link p to the start of the pool's freeblock list. Since
  1585. * the pool had at least the p block outstanding, the pool
  1586. * wasn't empty (so it's already in a usedpools[] list, or
  1587. * was full and is in no list -- it's not in the freeblocks
  1588. * list in any case).
  1589. */
  1590. assert(pool->ref.count > 0); /* else it was empty */
  1591. pymem_block *lastfree = pool->freeblock;
  1592. *(pymem_block **)p = lastfree;
  1593. pool->freeblock = (pymem_block *)p;
  1594. pool->ref.count--;
  1595. if (UNLIKELY(lastfree == NULL)) {
  1596. /* Pool was full, so doesn't currently live in any list:
  1597. * link it to the front of the appropriate usedpools[] list.
  1598. * This mimics LRU pool usage for new allocations and
  1599. * targets optimal filling when several pools contain
  1600. * blocks of the same size class.
  1601. */
  1602. insert_to_usedpool(state, pool);
  1603. return 1;
  1604. }
  1605. /* freeblock wasn't NULL, so the pool wasn't full,
  1606. * and the pool is in a usedpools[] list.
  1607. */
  1608. if (LIKELY(pool->ref.count != 0)) {
  1609. /* pool isn't empty: leave it in usedpools */
  1610. return 1;
  1611. }
  1612. /* Pool is now empty: unlink from usedpools, and
  1613. * link to the front of freepools. This ensures that
  1614. * previously freed pools will be allocated later
  1615. * (being not referenced, they are perhaps paged out).
  1616. */
  1617. insert_to_freepool(state, pool);
  1618. return 1;
  1619. }
  1620. void
  1621. _PyObject_Free(void *ctx, void *p)
  1622. {
  1623. /* PyObject_Free(NULL) has no effect */
  1624. if (p == NULL) {
  1625. return;
  1626. }
  1627. OMState *state = get_state();
  1628. if (UNLIKELY(!pymalloc_free(state, ctx, p))) {
  1629. /* pymalloc didn't allocate this address */
  1630. PyMem_RawFree(p);
  1631. raw_allocated_blocks--;
  1632. }
  1633. }
  1634. /* pymalloc realloc.
  1635. If nbytes==0, then as the Python docs promise, we do not treat this like
  1636. free(p), and return a non-NULL result.
  1637. Return 1 if pymalloc reallocated memory and wrote the new pointer into
  1638. newptr_p.
  1639. Return 0 if pymalloc didn't allocated p. */
  1640. static int
  1641. pymalloc_realloc(OMState *state, void *ctx,
  1642. void **newptr_p, void *p, size_t nbytes)
  1643. {
  1644. void *bp;
  1645. poolp pool;
  1646. size_t size;
  1647. assert(p != NULL);
  1648. #ifdef WITH_VALGRIND
  1649. /* Treat running_on_valgrind == -1 the same as 0 */
  1650. if (UNLIKELY(running_on_valgrind > 0)) {
  1651. return 0;
  1652. }
  1653. #endif
  1654. pool = POOL_ADDR(p);
  1655. if (!address_in_range(state, p, pool)) {
  1656. /* pymalloc is not managing this block.
  1657. If nbytes <= SMALL_REQUEST_THRESHOLD, it's tempting to try to take
  1658. over this block. However, if we do, we need to copy the valid data
  1659. from the C-managed block to one of our blocks, and there's no
  1660. portable way to know how much of the memory space starting at p is
  1661. valid.
  1662. As bug 1185883 pointed out the hard way, it's possible that the
  1663. C-managed block is "at the end" of allocated VM space, so that a
  1664. memory fault can occur if we try to copy nbytes bytes starting at p.
  1665. Instead we punt: let C continue to manage this block. */
  1666. return 0;
  1667. }
  1668. /* pymalloc is in charge of this block */
  1669. size = INDEX2SIZE(pool->szidx);
  1670. if (nbytes <= size) {
  1671. /* The block is staying the same or shrinking.
  1672. If it's shrinking, there's a tradeoff: it costs cycles to copy the
  1673. block to a smaller size class, but it wastes memory not to copy it.
  1674. The compromise here is to copy on shrink only if at least 25% of
  1675. size can be shaved off. */
  1676. if (4 * nbytes > 3 * size) {
  1677. /* It's the same, or shrinking and new/old > 3/4. */
  1678. *newptr_p = p;
  1679. return 1;
  1680. }
  1681. size = nbytes;
  1682. }
  1683. bp = _PyObject_Malloc(ctx, nbytes);
  1684. if (bp != NULL) {
  1685. memcpy(bp, p, size);
  1686. _PyObject_Free(ctx, p);
  1687. }
  1688. *newptr_p = bp;
  1689. return 1;
  1690. }
  1691. void *
  1692. _PyObject_Realloc(void *ctx, void *ptr, size_t nbytes)
  1693. {
  1694. void *ptr2;
  1695. if (ptr == NULL) {
  1696. return _PyObject_Malloc(ctx, nbytes);
  1697. }
  1698. OMState *state = get_state();
  1699. if (pymalloc_realloc(state, ctx, &ptr2, ptr, nbytes)) {
  1700. return ptr2;
  1701. }
  1702. return PyMem_RawRealloc(ptr, nbytes);
  1703. }
  1704. #else /* ! WITH_PYMALLOC */
  1705. /*==========================================================================*/
  1706. /* pymalloc not enabled: Redirect the entry points to malloc. These will
  1707. * only be used by extensions that are compiled with pymalloc enabled. */
  1708. Py_ssize_t
  1709. _PyInterpreterState_GetAllocatedBlocks(PyInterpreterState *Py_UNUSED(interp))
  1710. {
  1711. return 0;
  1712. }
  1713. Py_ssize_t
  1714. _Py_GetGlobalAllocatedBlocks(void)
  1715. {
  1716. return 0;
  1717. }
  1718. void
  1719. _PyInterpreterState_FinalizeAllocatedBlocks(PyInterpreterState *Py_UNUSED(interp))
  1720. {
  1721. return;
  1722. }
  1723. void
  1724. _Py_FinalizeAllocatedBlocks(_PyRuntimeState *Py_UNUSED(runtime))
  1725. {
  1726. return;
  1727. }
  1728. #endif /* WITH_PYMALLOC */
  1729. /*==========================================================================*/
  1730. /* A x-platform debugging allocator. This doesn't manage memory directly,
  1731. * it wraps a real allocator, adding extra debugging info to the memory blocks.
  1732. */
  1733. /* Uncomment this define to add the "serialno" field */
  1734. /* #define PYMEM_DEBUG_SERIALNO */
  1735. #ifdef PYMEM_DEBUG_SERIALNO
  1736. static size_t serialno = 0; /* incremented on each debug {m,re}alloc */
  1737. /* serialno is always incremented via calling this routine. The point is
  1738. * to supply a single place to set a breakpoint.
  1739. */
  1740. static void
  1741. bumpserialno(void)
  1742. {
  1743. ++serialno;
  1744. }
  1745. #endif
  1746. #define SST SIZEOF_SIZE_T
  1747. #ifdef PYMEM_DEBUG_SERIALNO
  1748. # define PYMEM_DEBUG_EXTRA_BYTES 4 * SST
  1749. #else
  1750. # define PYMEM_DEBUG_EXTRA_BYTES 3 * SST
  1751. #endif
  1752. /* Read sizeof(size_t) bytes at p as a big-endian size_t. */
  1753. static size_t
  1754. read_size_t(const void *p)
  1755. {
  1756. const uint8_t *q = (const uint8_t *)p;
  1757. size_t result = *q++;
  1758. int i;
  1759. for (i = SST; --i > 0; ++q)
  1760. result = (result << 8) | *q;
  1761. return result;
  1762. }
  1763. /* Write n as a big-endian size_t, MSB at address p, LSB at
  1764. * p + sizeof(size_t) - 1.
  1765. */
  1766. static void
  1767. write_size_t(void *p, size_t n)
  1768. {
  1769. uint8_t *q = (uint8_t *)p + SST - 1;
  1770. int i;
  1771. for (i = SST; --i >= 0; --q) {
  1772. *q = (uint8_t)(n & 0xff);
  1773. n >>= 8;
  1774. }
  1775. }
  1776. /* Let S = sizeof(size_t). The debug malloc asks for 4 * S extra bytes and
  1777. fills them with useful stuff, here calling the underlying malloc's result p:
  1778. p[0: S]
  1779. Number of bytes originally asked for. This is a size_t, big-endian (easier
  1780. to read in a memory dump).
  1781. p[S]
  1782. API ID. See PEP 445. This is a character, but seems undocumented.
  1783. p[S+1: 2*S]
  1784. Copies of PYMEM_FORBIDDENBYTE. Used to catch under- writes and reads.
  1785. p[2*S: 2*S+n]
  1786. The requested memory, filled with copies of PYMEM_CLEANBYTE.
  1787. Used to catch reference to uninitialized memory.
  1788. &p[2*S] is returned. Note that this is 8-byte aligned if pymalloc
  1789. handled the request itself.
  1790. p[2*S+n: 2*S+n+S]
  1791. Copies of PYMEM_FORBIDDENBYTE. Used to catch over- writes and reads.
  1792. p[2*S+n+S: 2*S+n+2*S]
  1793. A serial number, incremented by 1 on each call to _PyMem_DebugMalloc
  1794. and _PyMem_DebugRealloc.
  1795. This is a big-endian size_t.
  1796. If "bad memory" is detected later, the serial number gives an
  1797. excellent way to set a breakpoint on the next run, to capture the
  1798. instant at which this block was passed out.
  1799. If PYMEM_DEBUG_SERIALNO is not defined (default), the debug malloc only asks
  1800. for 3 * S extra bytes, and omits the last serialno field.
  1801. */
  1802. static void *
  1803. _PyMem_DebugRawAlloc(int use_calloc, void *ctx, size_t nbytes)
  1804. {
  1805. debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
  1806. uint8_t *p; /* base address of malloc'ed pad block */
  1807. uint8_t *data; /* p + 2*SST == pointer to data bytes */
  1808. uint8_t *tail; /* data + nbytes == pointer to tail pad bytes */
  1809. size_t total; /* nbytes + PYMEM_DEBUG_EXTRA_BYTES */
  1810. if (nbytes > (size_t)PY_SSIZE_T_MAX - PYMEM_DEBUG_EXTRA_BYTES) {
  1811. /* integer overflow: can't represent total as a Py_ssize_t */
  1812. return NULL;
  1813. }
  1814. total = nbytes + PYMEM_DEBUG_EXTRA_BYTES;
  1815. /* Layout: [SSSS IFFF CCCC...CCCC FFFF NNNN]
  1816. ^--- p ^--- data ^--- tail
  1817. S: nbytes stored as size_t
  1818. I: API identifier (1 byte)
  1819. F: Forbidden bytes (size_t - 1 bytes before, size_t bytes after)
  1820. C: Clean bytes used later to store actual data
  1821. N: Serial number stored as size_t
  1822. If PYMEM_DEBUG_SERIALNO is not defined (default), the last NNNN field
  1823. is omitted. */
  1824. if (use_calloc) {
  1825. p = (uint8_t *)api->alloc.calloc(api->alloc.ctx, 1, total);
  1826. }
  1827. else {
  1828. p = (uint8_t *)api->alloc.malloc(api->alloc.ctx, total);
  1829. }
  1830. if (p == NULL) {
  1831. return NULL;
  1832. }
  1833. data = p + 2*SST;
  1834. #ifdef PYMEM_DEBUG_SERIALNO
  1835. bumpserialno();
  1836. #endif
  1837. /* at p, write size (SST bytes), id (1 byte), pad (SST-1 bytes) */
  1838. write_size_t(p, nbytes);
  1839. p[SST] = (uint8_t)api->api_id;
  1840. memset(p + SST + 1, PYMEM_FORBIDDENBYTE, SST-1);
  1841. if (nbytes > 0 && !use_calloc) {
  1842. memset(data, PYMEM_CLEANBYTE, nbytes);
  1843. }
  1844. /* at tail, write pad (SST bytes) and serialno (SST bytes) */
  1845. tail = data + nbytes;
  1846. memset(tail, PYMEM_FORBIDDENBYTE, SST);
  1847. #ifdef PYMEM_DEBUG_SERIALNO
  1848. write_size_t(tail + SST, serialno);
  1849. #endif
  1850. return data;
  1851. }
  1852. void *
  1853. _PyMem_DebugRawMalloc(void *ctx, size_t nbytes)
  1854. {
  1855. return _PyMem_DebugRawAlloc(0, ctx, nbytes);
  1856. }
  1857. void *
  1858. _PyMem_DebugRawCalloc(void *ctx, size_t nelem, size_t elsize)
  1859. {
  1860. size_t nbytes;
  1861. assert(elsize == 0 || nelem <= (size_t)PY_SSIZE_T_MAX / elsize);
  1862. nbytes = nelem * elsize;
  1863. return _PyMem_DebugRawAlloc(1, ctx, nbytes);
  1864. }
  1865. /* The debug free first checks the 2*SST bytes on each end for sanity (in
  1866. particular, that the FORBIDDENBYTEs with the api ID are still intact).
  1867. Then fills the original bytes with PYMEM_DEADBYTE.
  1868. Then calls the underlying free.
  1869. */
  1870. void
  1871. _PyMem_DebugRawFree(void *ctx, void *p)
  1872. {
  1873. /* PyMem_Free(NULL) has no effect */
  1874. if (p == NULL) {
  1875. return;
  1876. }
  1877. debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
  1878. uint8_t *q = (uint8_t *)p - 2*SST; /* address returned from malloc */
  1879. size_t nbytes;
  1880. _PyMem_DebugCheckAddress(__func__, api->api_id, p);
  1881. nbytes = read_size_t(q);
  1882. nbytes += PYMEM_DEBUG_EXTRA_BYTES;
  1883. memset(q, PYMEM_DEADBYTE, nbytes);
  1884. api->alloc.free(api->alloc.ctx, q);
  1885. }
  1886. void *
  1887. _PyMem_DebugRawRealloc(void *ctx, void *p, size_t nbytes)
  1888. {
  1889. if (p == NULL) {
  1890. return _PyMem_DebugRawAlloc(0, ctx, nbytes);
  1891. }
  1892. debug_alloc_api_t *api = (debug_alloc_api_t *)ctx;
  1893. uint8_t *head; /* base address of malloc'ed pad block */
  1894. uint8_t *data; /* pointer to data bytes */
  1895. uint8_t *r;
  1896. uint8_t *tail; /* data + nbytes == pointer to tail pad bytes */
  1897. size_t total; /* 2 * SST + nbytes + 2 * SST */
  1898. size_t original_nbytes;
  1899. #define ERASED_SIZE 64
  1900. uint8_t save[2*ERASED_SIZE]; /* A copy of erased bytes. */
  1901. _PyMem_DebugCheckAddress(__func__, api->api_id, p);
  1902. data = (uint8_t *)p;
  1903. head = data - 2*SST;
  1904. original_nbytes = read_size_t(head);
  1905. if (nbytes > (size_t)PY_SSIZE_T_MAX - PYMEM_DEBUG_EXTRA_BYTES) {
  1906. /* integer overflow: can't represent total as a Py_ssize_t */
  1907. return NULL;
  1908. }
  1909. total = nbytes + PYMEM_DEBUG_EXTRA_BYTES;
  1910. tail = data + original_nbytes;
  1911. #ifdef PYMEM_DEBUG_SERIALNO
  1912. size_t block_serialno = read_size_t(tail + SST);
  1913. #endif
  1914. /* Mark the header, the trailer, ERASED_SIZE bytes at the begin and
  1915. ERASED_SIZE bytes at the end as dead and save the copy of erased bytes.
  1916. */
  1917. if (original_nbytes <= sizeof(save)) {
  1918. memcpy(save, data, original_nbytes);
  1919. memset(data - 2 * SST, PYMEM_DEADBYTE,
  1920. original_nbytes + PYMEM_DEBUG_EXTRA_BYTES);
  1921. }
  1922. else {
  1923. memcpy(save, data, ERASED_SIZE);
  1924. memset(head, PYMEM_DEADBYTE, ERASED_SIZE + 2 * SST);
  1925. memcpy(&save[ERASED_SIZE], tail - ERASED_SIZE, ERASED_SIZE);
  1926. memset(tail - ERASED_SIZE, PYMEM_DEADBYTE,
  1927. ERASED_SIZE + PYMEM_DEBUG_EXTRA_BYTES - 2 * SST);
  1928. }
  1929. /* Resize and add decorations. */
  1930. r = (uint8_t *)api->alloc.realloc(api->alloc.ctx, head, total);
  1931. if (r == NULL) {
  1932. /* if realloc() failed: rewrite header and footer which have
  1933. just been erased */
  1934. nbytes = original_nbytes;
  1935. }
  1936. else {
  1937. head = r;
  1938. #ifdef PYMEM_DEBUG_SERIALNO
  1939. bumpserialno();
  1940. block_serialno = serialno;
  1941. #endif
  1942. }
  1943. data = head + 2*SST;
  1944. write_size_t(head, nbytes);
  1945. head[SST] = (uint8_t)api->api_id;
  1946. memset(head + SST + 1, PYMEM_FORBIDDENBYTE, SST-1);
  1947. tail = data + nbytes;
  1948. memset(tail, PYMEM_FORBIDDENBYTE, SST);
  1949. #ifdef PYMEM_DEBUG_SERIALNO
  1950. write_size_t(tail + SST, block_serialno);
  1951. #endif
  1952. /* Restore saved bytes. */
  1953. if (original_nbytes <= sizeof(save)) {
  1954. memcpy(data, save, Py_MIN(nbytes, original_nbytes));
  1955. }
  1956. else {
  1957. size_t i = original_nbytes - ERASED_SIZE;
  1958. memcpy(data, save, Py_MIN(nbytes, ERASED_SIZE));
  1959. if (nbytes > i) {
  1960. memcpy(data + i, &save[ERASED_SIZE],
  1961. Py_MIN(nbytes - i, ERASED_SIZE));
  1962. }
  1963. }
  1964. if (r == NULL) {
  1965. return NULL;
  1966. }
  1967. if (nbytes > original_nbytes) {
  1968. /* growing: mark new extra memory clean */
  1969. memset(data + original_nbytes, PYMEM_CLEANBYTE,
  1970. nbytes - original_nbytes);
  1971. }
  1972. return data;
  1973. }
  1974. static inline void
  1975. _PyMem_DebugCheckGIL(const char *func)
  1976. {
  1977. if (!PyGILState_Check()) {
  1978. _Py_FatalErrorFunc(func,
  1979. "Python memory allocator called "
  1980. "without holding the GIL");
  1981. }
  1982. }
  1983. void *
  1984. _PyMem_DebugMalloc(void *ctx, size_t nbytes)
  1985. {
  1986. _PyMem_DebugCheckGIL(__func__);
  1987. return _PyMem_DebugRawMalloc(ctx, nbytes);
  1988. }
  1989. void *
  1990. _PyMem_DebugCalloc(void *ctx, size_t nelem, size_t elsize)
  1991. {
  1992. _PyMem_DebugCheckGIL(__func__);
  1993. return _PyMem_DebugRawCalloc(ctx, nelem, elsize);
  1994. }
  1995. void
  1996. _PyMem_DebugFree(void *ctx, void *ptr)
  1997. {
  1998. _PyMem_DebugCheckGIL(__func__);
  1999. _PyMem_DebugRawFree(ctx, ptr);
  2000. }
  2001. void *
  2002. _PyMem_DebugRealloc(void *ctx, void *ptr, size_t nbytes)
  2003. {
  2004. _PyMem_DebugCheckGIL(__func__);
  2005. return _PyMem_DebugRawRealloc(ctx, ptr, nbytes);
  2006. }
  2007. /* Check the forbidden bytes on both ends of the memory allocated for p.
  2008. * If anything is wrong, print info to stderr via _PyObject_DebugDumpAddress,
  2009. * and call Py_FatalError to kill the program.
  2010. * The API id, is also checked.
  2011. */
  2012. static void
  2013. _PyMem_DebugCheckAddress(const char *func, char api, const void *p)
  2014. {
  2015. assert(p != NULL);
  2016. const uint8_t *q = (const uint8_t *)p;
  2017. size_t nbytes;
  2018. const uint8_t *tail;
  2019. int i;
  2020. char id;
  2021. /* Check the API id */
  2022. id = (char)q[-SST];
  2023. if (id != api) {
  2024. _PyObject_DebugDumpAddress(p);
  2025. _Py_FatalErrorFormat(func,
  2026. "bad ID: Allocated using API '%c', "
  2027. "verified using API '%c'",
  2028. id, api);
  2029. }
  2030. /* Check the stuff at the start of p first: if there's underwrite
  2031. * corruption, the number-of-bytes field may be nuts, and checking
  2032. * the tail could lead to a segfault then.
  2033. */
  2034. for (i = SST-1; i >= 1; --i) {
  2035. if (*(q-i) != PYMEM_FORBIDDENBYTE) {
  2036. _PyObject_DebugDumpAddress(p);
  2037. _Py_FatalErrorFunc(func, "bad leading pad byte");
  2038. }
  2039. }
  2040. nbytes = read_size_t(q - 2*SST);
  2041. tail = q + nbytes;
  2042. for (i = 0; i < SST; ++i) {
  2043. if (tail[i] != PYMEM_FORBIDDENBYTE) {
  2044. _PyObject_DebugDumpAddress(p);
  2045. _Py_FatalErrorFunc(func, "bad trailing pad byte");
  2046. }
  2047. }
  2048. }
  2049. /* Display info to stderr about the memory block at p. */
  2050. static void
  2051. _PyObject_DebugDumpAddress(const void *p)
  2052. {
  2053. const uint8_t *q = (const uint8_t *)p;
  2054. const uint8_t *tail;
  2055. size_t nbytes;
  2056. int i;
  2057. int ok;
  2058. char id;
  2059. fprintf(stderr, "Debug memory block at address p=%p:", p);
  2060. if (p == NULL) {
  2061. fprintf(stderr, "\n");
  2062. return;
  2063. }
  2064. id = (char)q[-SST];
  2065. fprintf(stderr, " API '%c'\n", id);
  2066. nbytes = read_size_t(q - 2*SST);
  2067. fprintf(stderr, " %zu bytes originally requested\n", nbytes);
  2068. /* In case this is nuts, check the leading pad bytes first. */
  2069. fprintf(stderr, " The %d pad bytes at p-%d are ", SST-1, SST-1);
  2070. ok = 1;
  2071. for (i = 1; i <= SST-1; ++i) {
  2072. if (*(q-i) != PYMEM_FORBIDDENBYTE) {
  2073. ok = 0;
  2074. break;
  2075. }
  2076. }
  2077. if (ok)
  2078. fputs("FORBIDDENBYTE, as expected.\n", stderr);
  2079. else {
  2080. fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
  2081. PYMEM_FORBIDDENBYTE);
  2082. for (i = SST-1; i >= 1; --i) {
  2083. const uint8_t byte = *(q-i);
  2084. fprintf(stderr, " at p-%d: 0x%02x", i, byte);
  2085. if (byte != PYMEM_FORBIDDENBYTE)
  2086. fputs(" *** OUCH", stderr);
  2087. fputc('\n', stderr);
  2088. }
  2089. fputs(" Because memory is corrupted at the start, the "
  2090. "count of bytes requested\n"
  2091. " may be bogus, and checking the trailing pad "
  2092. "bytes may segfault.\n", stderr);
  2093. }
  2094. tail = q + nbytes;
  2095. fprintf(stderr, " The %d pad bytes at tail=%p are ", SST, (void *)tail);
  2096. ok = 1;
  2097. for (i = 0; i < SST; ++i) {
  2098. if (tail[i] != PYMEM_FORBIDDENBYTE) {
  2099. ok = 0;
  2100. break;
  2101. }
  2102. }
  2103. if (ok)
  2104. fputs("FORBIDDENBYTE, as expected.\n", stderr);
  2105. else {
  2106. fprintf(stderr, "not all FORBIDDENBYTE (0x%02x):\n",
  2107. PYMEM_FORBIDDENBYTE);
  2108. for (i = 0; i < SST; ++i) {
  2109. const uint8_t byte = tail[i];
  2110. fprintf(stderr, " at tail+%d: 0x%02x",
  2111. i, byte);
  2112. if (byte != PYMEM_FORBIDDENBYTE)
  2113. fputs(" *** OUCH", stderr);
  2114. fputc('\n', stderr);
  2115. }
  2116. }
  2117. #ifdef PYMEM_DEBUG_SERIALNO
  2118. size_t serial = read_size_t(tail + SST);
  2119. fprintf(stderr,
  2120. " The block was made by call #%zu to debug malloc/realloc.\n",
  2121. serial);
  2122. #endif
  2123. if (nbytes > 0) {
  2124. i = 0;
  2125. fputs(" Data at p:", stderr);
  2126. /* print up to 8 bytes at the start */
  2127. while (q < tail && i < 8) {
  2128. fprintf(stderr, " %02x", *q);
  2129. ++i;
  2130. ++q;
  2131. }
  2132. /* and up to 8 at the end */
  2133. if (q < tail) {
  2134. if (tail - q > 8) {
  2135. fputs(" ...", stderr);
  2136. q = tail - 8;
  2137. }
  2138. while (q < tail) {
  2139. fprintf(stderr, " %02x", *q);
  2140. ++q;
  2141. }
  2142. }
  2143. fputc('\n', stderr);
  2144. }
  2145. fputc('\n', stderr);
  2146. fflush(stderr);
  2147. _PyMem_DumpTraceback(fileno(stderr), p);
  2148. }
  2149. static size_t
  2150. printone(FILE *out, const char* msg, size_t value)
  2151. {
  2152. int i, k;
  2153. char buf[100];
  2154. size_t origvalue = value;
  2155. fputs(msg, out);
  2156. for (i = (int)strlen(msg); i < 35; ++i)
  2157. fputc(' ', out);
  2158. fputc('=', out);
  2159. /* Write the value with commas. */
  2160. i = 22;
  2161. buf[i--] = '\0';
  2162. buf[i--] = '\n';
  2163. k = 3;
  2164. do {
  2165. size_t nextvalue = value / 10;
  2166. unsigned int digit = (unsigned int)(value - nextvalue * 10);
  2167. value = nextvalue;
  2168. buf[i--] = (char)(digit + '0');
  2169. --k;
  2170. if (k == 0 && value && i >= 0) {
  2171. k = 3;
  2172. buf[i--] = ',';
  2173. }
  2174. } while (value && i >= 0);
  2175. while (i >= 0)
  2176. buf[i--] = ' ';
  2177. fputs(buf, out);
  2178. return origvalue;
  2179. }
  2180. void
  2181. _PyDebugAllocatorStats(FILE *out,
  2182. const char *block_name, int num_blocks, size_t sizeof_block)
  2183. {
  2184. char buf1[128];
  2185. char buf2[128];
  2186. PyOS_snprintf(buf1, sizeof(buf1),
  2187. "%d %ss * %zd bytes each",
  2188. num_blocks, block_name, sizeof_block);
  2189. PyOS_snprintf(buf2, sizeof(buf2),
  2190. "%48s ", buf1);
  2191. (void)printone(out, buf2, num_blocks * sizeof_block);
  2192. }
  2193. #ifdef WITH_PYMALLOC
  2194. #ifdef Py_DEBUG
  2195. /* Is target in the list? The list is traversed via the nextpool pointers.
  2196. * The list may be NULL-terminated, or circular. Return 1 if target is in
  2197. * list, else 0.
  2198. */
  2199. static int
  2200. pool_is_in_list(const poolp target, poolp list)
  2201. {
  2202. poolp origlist = list;
  2203. assert(target != NULL);
  2204. if (list == NULL)
  2205. return 0;
  2206. do {
  2207. if (target == list)
  2208. return 1;
  2209. list = list->nextpool;
  2210. } while (list != NULL && list != origlist);
  2211. return 0;
  2212. }
  2213. #endif
  2214. /* Print summary info to "out" about the state of pymalloc's structures.
  2215. * In Py_DEBUG mode, also perform some expensive internal consistency
  2216. * checks.
  2217. *
  2218. * Return 0 if the memory debug hooks are not installed or no statistics was
  2219. * written into out, return 1 otherwise.
  2220. */
  2221. int
  2222. _PyObject_DebugMallocStats(FILE *out)
  2223. {
  2224. if (!_PyMem_PymallocEnabled()) {
  2225. return 0;
  2226. }
  2227. OMState *state = get_state();
  2228. uint i;
  2229. const uint numclasses = SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT;
  2230. /* # of pools, allocated blocks, and free blocks per class index */
  2231. size_t numpools[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
  2232. size_t numblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
  2233. size_t numfreeblocks[SMALL_REQUEST_THRESHOLD >> ALIGNMENT_SHIFT];
  2234. /* total # of allocated bytes in used and full pools */
  2235. size_t allocated_bytes = 0;
  2236. /* total # of available bytes in used pools */
  2237. size_t available_bytes = 0;
  2238. /* # of free pools + pools not yet carved out of current arena */
  2239. uint numfreepools = 0;
  2240. /* # of bytes for arena alignment padding */
  2241. size_t arena_alignment = 0;
  2242. /* # of bytes in used and full pools used for pool_headers */
  2243. size_t pool_header_bytes = 0;
  2244. /* # of bytes in used and full pools wasted due to quantization,
  2245. * i.e. the necessarily leftover space at the ends of used and
  2246. * full pools.
  2247. */
  2248. size_t quantization = 0;
  2249. /* # of arenas actually allocated. */
  2250. size_t narenas = 0;
  2251. /* running total -- should equal narenas * ARENA_SIZE */
  2252. size_t total;
  2253. char buf[128];
  2254. fprintf(out, "Small block threshold = %d, in %u size classes.\n",
  2255. SMALL_REQUEST_THRESHOLD, numclasses);
  2256. for (i = 0; i < numclasses; ++i)
  2257. numpools[i] = numblocks[i] = numfreeblocks[i] = 0;
  2258. /* Because full pools aren't linked to from anything, it's easiest
  2259. * to march over all the arenas. If we're lucky, most of the memory
  2260. * will be living in full pools -- would be a shame to miss them.
  2261. */
  2262. for (i = 0; i < maxarenas; ++i) {
  2263. uintptr_t base = allarenas[i].address;
  2264. /* Skip arenas which are not allocated. */
  2265. if (allarenas[i].address == (uintptr_t)NULL)
  2266. continue;
  2267. narenas += 1;
  2268. numfreepools += allarenas[i].nfreepools;
  2269. /* round up to pool alignment */
  2270. if (base & (uintptr_t)POOL_SIZE_MASK) {
  2271. arena_alignment += POOL_SIZE;
  2272. base &= ~(uintptr_t)POOL_SIZE_MASK;
  2273. base += POOL_SIZE;
  2274. }
  2275. /* visit every pool in the arena */
  2276. assert(base <= (uintptr_t) allarenas[i].pool_address);
  2277. for (; base < (uintptr_t) allarenas[i].pool_address; base += POOL_SIZE) {
  2278. poolp p = (poolp)base;
  2279. const uint sz = p->szidx;
  2280. uint freeblocks;
  2281. if (p->ref.count == 0) {
  2282. /* currently unused */
  2283. #ifdef Py_DEBUG
  2284. assert(pool_is_in_list(p, allarenas[i].freepools));
  2285. #endif
  2286. continue;
  2287. }
  2288. ++numpools[sz];
  2289. numblocks[sz] += p->ref.count;
  2290. freeblocks = NUMBLOCKS(sz) - p->ref.count;
  2291. numfreeblocks[sz] += freeblocks;
  2292. #ifdef Py_DEBUG
  2293. if (freeblocks > 0)
  2294. assert(pool_is_in_list(p, usedpools[sz + sz]));
  2295. #endif
  2296. }
  2297. }
  2298. assert(narenas == narenas_currently_allocated);
  2299. fputc('\n', out);
  2300. fputs("class size num pools blocks in use avail blocks\n"
  2301. "----- ---- --------- ------------- ------------\n",
  2302. out);
  2303. for (i = 0; i < numclasses; ++i) {
  2304. size_t p = numpools[i];
  2305. size_t b = numblocks[i];
  2306. size_t f = numfreeblocks[i];
  2307. uint size = INDEX2SIZE(i);
  2308. if (p == 0) {
  2309. assert(b == 0 && f == 0);
  2310. continue;
  2311. }
  2312. fprintf(out, "%5u %6u %11zu %15zu %13zu\n",
  2313. i, size, p, b, f);
  2314. allocated_bytes += b * size;
  2315. available_bytes += f * size;
  2316. pool_header_bytes += p * POOL_OVERHEAD;
  2317. quantization += p * ((POOL_SIZE - POOL_OVERHEAD) % size);
  2318. }
  2319. fputc('\n', out);
  2320. #ifdef PYMEM_DEBUG_SERIALNO
  2321. if (_PyMem_DebugEnabled()) {
  2322. (void)printone(out, "# times object malloc called", serialno);
  2323. }
  2324. #endif
  2325. (void)printone(out, "# arenas allocated total", ntimes_arena_allocated);
  2326. (void)printone(out, "# arenas reclaimed", ntimes_arena_allocated - narenas);
  2327. (void)printone(out, "# arenas highwater mark", narenas_highwater);
  2328. (void)printone(out, "# arenas allocated current", narenas);
  2329. PyOS_snprintf(buf, sizeof(buf),
  2330. "%zu arenas * %d bytes/arena",
  2331. narenas, ARENA_SIZE);
  2332. (void)printone(out, buf, narenas * ARENA_SIZE);
  2333. fputc('\n', out);
  2334. /* Account for what all of those arena bytes are being used for. */
  2335. total = printone(out, "# bytes in allocated blocks", allocated_bytes);
  2336. total += printone(out, "# bytes in available blocks", available_bytes);
  2337. PyOS_snprintf(buf, sizeof(buf),
  2338. "%u unused pools * %d bytes", numfreepools, POOL_SIZE);
  2339. total += printone(out, buf, (size_t)numfreepools * POOL_SIZE);
  2340. total += printone(out, "# bytes lost to pool headers", pool_header_bytes);
  2341. total += printone(out, "# bytes lost to quantization", quantization);
  2342. total += printone(out, "# bytes lost to arena alignment", arena_alignment);
  2343. (void)printone(out, "Total", total);
  2344. assert(narenas * ARENA_SIZE == total);
  2345. #if WITH_PYMALLOC_RADIX_TREE
  2346. fputs("\narena map counts\n", out);
  2347. #ifdef USE_INTERIOR_NODES
  2348. (void)printone(out, "# arena map mid nodes", arena_map_mid_count);
  2349. (void)printone(out, "# arena map bot nodes", arena_map_bot_count);
  2350. fputc('\n', out);
  2351. #endif
  2352. total = printone(out, "# bytes lost to arena map root", sizeof(arena_map_root));
  2353. #ifdef USE_INTERIOR_NODES
  2354. total += printone(out, "# bytes lost to arena map mid",
  2355. sizeof(arena_map_mid_t) * arena_map_mid_count);
  2356. total += printone(out, "# bytes lost to arena map bot",
  2357. sizeof(arena_map_bot_t) * arena_map_bot_count);
  2358. (void)printone(out, "Total", total);
  2359. #endif
  2360. #endif
  2361. return 1;
  2362. }
  2363. #endif /* #ifdef WITH_PYMALLOC */