Skip to content

Commit 95418bc

Browse files
authored
lsan: Support free_sized and free_aligned_sized from C23 (#144415)
Adds support to LSan for `free_sized` and `free_aligned_sized` from C23. Other sanitizers will be handled with their own separate PRs. For #144435 Signed-off-by: Justin King <[email protected]>
1 parent 38daa6d commit 95418bc

File tree

5 files changed

+52
-10
lines changed

5 files changed

+52
-10
lines changed

compiler-rt/lib/lsan/lsan_allocator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,10 @@ void lsan_free(void *p) {
220220
Deallocate(p);
221221
}
222222

223+
void lsan_free_sized(void *p, uptr) { Deallocate(p); }
224+
225+
void lsan_free_aligned_sized(void *p, uptr, uptr) { Deallocate(p); }
226+
223227
void *lsan_realloc(void *p, uptr size, const StackTrace &stack) {
224228
return SetErrnoOnNull(Reallocate(stack, p, size, 1));
225229
}

compiler-rt/lib/lsan/lsan_allocator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ void *lsan_aligned_alloc(uptr alignment, uptr size, const StackTrace &stack);
127127
void *lsan_memalign(uptr alignment, uptr size, const StackTrace &stack);
128128
void *lsan_malloc(uptr size, const StackTrace &stack);
129129
void lsan_free(void *p);
130+
void lsan_free_sized(void *p, uptr size);
131+
void lsan_free_aligned_sized(void *p, uptr alignment, uptr size);
130132
void *lsan_realloc(void *p, uptr size, const StackTrace &stack);
131133
void *lsan_reallocarray(void *p, uptr nmemb, uptr size,
132134
const StackTrace &stack);

compiler-rt/lib/lsan/lsan_interceptors.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,24 @@ INTERCEPTOR(void, free, void *p) {
8484
lsan_free(p);
8585
}
8686

87+
INTERCEPTOR(void, free_sized, void *p, uptr size) {
88+
if (UNLIKELY(!p))
89+
return;
90+
if (DlsymAlloc::PointerIsMine(p))
91+
return DlsymAlloc::Free(p);
92+
ENSURE_LSAN_INITED;
93+
lsan_free_sized(p, size);
94+
}
95+
96+
INTERCEPTOR(void, free_aligned_sized, void *p, uptr alignment, uptr size) {
97+
if (UNLIKELY(!p))
98+
return;
99+
if (DlsymAlloc::PointerIsMine(p))
100+
return DlsymAlloc::Free(p);
101+
ENSURE_LSAN_INITED;
102+
lsan_free_aligned_sized(p, alignment, size);
103+
}
104+
87105
INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
88106
if (DlsymAlloc::Use())
89107
return DlsymAlloc::Callocate(nmemb, size);

compiler-rt/lib/lsan/lsan_malloc_mac.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,19 @@ using namespace __lsan;
4444
void *p = lsan_valloc(size, stack)
4545
#define COMMON_MALLOC_FREE(ptr) \
4646
lsan_free(ptr)
47-
#define COMMON_MALLOC_SIZE(ptr) \
48-
uptr size = lsan_mz_size(ptr)
49-
#define COMMON_MALLOC_FILL_STATS(zone, stats)
50-
#define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
51-
(void)zone_name; \
52-
Report("mz_realloc(%p) -- attempting to realloc unallocated memory.\n", ptr);
53-
#define COMMON_MALLOC_NAMESPACE __lsan
54-
#define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
55-
#define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 0
47+
# define COMMON_MALLOC_FREE_SIZED(ptr, size) lsan_free_sized(ptr, size)
48+
# define COMMON_MALLOC_FREE_ALIGNED_SIZED(ptr, alignment, size) \
49+
lsan_free_aligned_sized(ptr, alignment, size)
50+
# define COMMON_MALLOC_SIZE(ptr) uptr size = lsan_mz_size(ptr)
51+
# define COMMON_MALLOC_FILL_STATS(zone, stats)
52+
# define COMMON_MALLOC_REPORT_UNKNOWN_REALLOC(ptr, zone_ptr, zone_name) \
53+
(void)zone_name; \
54+
Report("mz_realloc(%p) -- attempting to realloc unallocated memory.\n", \
55+
ptr);
56+
# define COMMON_MALLOC_NAMESPACE __lsan
57+
# define COMMON_MALLOC_HAS_ZONE_ENUMERATOR 0
58+
# define COMMON_MALLOC_HAS_EXTRA_INTROSPECTION_INIT 0
5659

57-
#include "sanitizer_common/sanitizer_malloc_mac.inc"
60+
# include "sanitizer_common/sanitizer_malloc_mac.inc"
5861

5962
#endif // SANITIZER_APPLE

compiler-rt/lib/sanitizer_common/sanitizer_malloc_mac.inc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,21 @@ INTERCEPTOR(void, free, void *ptr) {
144144
COMMON_MALLOC_FREE(ptr);
145145
}
146146

147+
#ifdef COMMON_MALLOC_FREE_SIZED
148+
INTERCEPTOR(void, free_sized, void *ptr, size_t size) {
149+
COMMON_MALLOC_ENTER();
150+
COMMON_MALLOC_FREE_SIZED(ptr, size);
151+
}
152+
#endif
153+
154+
#ifdef COMMON_MALLOC_FREE_ALIGNED_SIZED
155+
INTERCEPTOR(void, free_aligned_sized, void *ptr, size_t alignment,
156+
size_t size) {
157+
COMMON_MALLOC_ENTER();
158+
COMMON_MALLOC_FREE_ALIGNED_SIZED(ptr, alignment, size);
159+
}
160+
#endif
161+
147162
INTERCEPTOR(void *, realloc, void *ptr, size_t size) {
148163
COMMON_MALLOC_ENTER();
149164
COMMON_MALLOC_REALLOC(ptr, size);

0 commit comments

Comments
 (0)