From 1f1df4f4378bb87bb1b480593cd77669fcd56ec7 Mon Sep 17 00:00:00 2001 From: evgeny Date: Wed, 16 Sep 2026 21:12:46 +0300 Subject: [PATCH] =?UTF-8?q?mem:=20=D0=B4=D0=B5=D1=82=D0=B5=D0=BA=D1=82=20?= =?UTF-8?q?=D0=B0=D0=BD=D0=BE=D0=BC=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=20=D0=B1?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D1=88=D0=B8=D1=85=20=D0=B0=D0=BB=D0=BB=D0=BE?= =?UTF-8?q?=D0=BA=D0=B0=D1=86=D0=B8=D0=B9=20(LARGE=20ALLOC=20log=20+=20cal?= =?UTF-8?q?loc=20overflow=20guard)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mem.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/mem.c b/lib/mem.c index 6391cf65..a100e97a 100644 --- a/lib/mem.c +++ b/lib/mem.c @@ -18,6 +18,9 @@ #define PREV_OFFSET LOCATION_MAX #define NEXT_OFFSET (LOCATION_MAX + POINTER_SIZE) +// Порог детекта аномально больших аллокаций (гигантские mmap-резервации, напр. VSZ ~4G) +#define LARGE_ALLOC_LOG_THRESHOLD (8u * 1024u * 1024u) // 8 МБ + #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) #include #include @@ -121,6 +124,10 @@ void u_check(void* ptr, const char* text, const char* location) { } void* u_malloc_impl(uint32_t size, const char* location) { + if (size >= LARGE_ALLOC_LOG_THRESHOLD) { + DEBUG_WARN(DEBUG_CATEGORY_SYS, "LARGE ALLOC: %u bytes (%.1f MB) at %s", + size, (double)size / (1024.0 * 1024.0), location); + } uint32_t total = size + 12 + 2 * BOUNDARY_CHECK_SIZE + METADATA_SIZE; uint8_t* base = (uint8_t*)malloc(total); if (!base && size > 0) return NULL; @@ -151,6 +158,10 @@ void* u_malloc_impl(uint32_t size, const char* location) { } void* u_calloc_impl(uint32_t nmemb, uint32_t size, const char* location) { + if (nmemb != 0 && size > UINT32_MAX / nmemb) { + DEBUG_ERROR(DEBUG_CATEGORY_SYS, "CALLOC OVERFLOW: %u * %u (wraps uint32) at %s", nmemb, size, location); + return NULL; + } uint32_t total_size = nmemb * size; void* ptr = u_malloc_impl(total_size, location); if (ptr) memset(ptr, 0, total_size);