LCOV - code coverage report
Current view: top level - Objects - obmalloc.c (source / functions) Hit Total Coverage
Test: CPython 3.12 LCOV report [commit 5e6661bce9] Lines: 373 845 44.1 %
Date: 2023-03-20 08:15:36 Functions: 42 70 60.0 %
Branches: 160 418 38.3 %

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

Generated by: LCOV version 1.14