@@ -99,6 +99,20 @@ indicate_hugepages(void *p, size_t size) {
9999}
100100
101101
102+ /* Do not enable the alloc cache if the GIL is disabled, or if ASAN or MSAN
103+ * instrumentation is enabled. The cache makes ASAN use-after-free or MSAN
104+ * use-of-uninitialized-memory warnings less useful. */
105+ #ifdef Py_GIL_DISABLED
106+ #define USE_ALLOC_CACHE 0
107+ #elif defined(__has_feature )
108+ # if __has_feature (address_sanitizer ) || __has_feature (memory_sanitizer )
109+ # define USE_ALLOC_CACHE 0
110+ # endif
111+ #else
112+ #define USE_ALLOC_CACHE 1
113+ #endif
114+
115+
102116/* as the cache is managed in global variables verify the GIL is held */
103117
104118/*
@@ -115,7 +129,7 @@ _npy_alloc_cache(npy_uintp nelem, npy_uintp esz, npy_uint msz,
115129 assert ((esz == 1 && cache == datacache ) ||
116130 (esz == sizeof (npy_intp ) && cache == dimcache ));
117131 assert (PyGILState_Check ());
118- #ifndef Py_GIL_DISABLED
132+ #if USE_ALLOC_CACHE
119133 if (nelem < msz ) {
120134 if (cache [nelem ].available > 0 ) {
121135 return cache [nelem ].ptrs [-- (cache [nelem ].available )];
@@ -141,7 +155,7 @@ _npy_free_cache(void * p, npy_uintp nelem, npy_uint msz,
141155 cache_bucket * cache , void (* dealloc )(void * ))
142156{
143157 assert (PyGILState_Check ());
144- #ifndef Py_GIL_DISABLED
158+ #if USE_ALLOC_CACHE
145159 if (p != NULL && nelem < msz ) {
146160 if (cache [nelem ].available < NCACHE ) {
147161 cache [nelem ].ptrs [cache [nelem ].available ++ ] = p ;
0 commit comments