Skip to content

Commit 0506cf6

Browse files
authored
Merge pull request numpy#29006 from hawkinsp/asan
ENH: Disable the alloc cache under address and memory sanitizers
2 parents c2d5e00 + 6167ca8 commit 0506cf6

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

numpy/_core/src/multiarray/alloc.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)