Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

wip: scope wrap flags, fix msize recursion

Martin Vogeld7d3ae407ec2
diff --git a/Makefile.cbm b/Makefile.cbm
index ebda5034b..ec97b1b15 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -105,9 +105,12 @@ WIN32_LIBS :=
# pointer can never reach mi_free (#424).
MIMALLOC_WRAP_SYMS := malloc calloc realloc free strdup strndup _msize \
_aligned_malloc _aligned_free
+MIMALLOC_WRAP_FLAGS :=
+ifeq ($(IS_MINGW),yes)
MIMALLOC_WRAP_FLAGS := $(foreach sym,$(MIMALLOC_WRAP_SYMS),-Wl,--wrap=$(sym))
+endif
ifeq ($(IS_MINGW),yes)
-WIN32_LIBS := -lws2_32 -lpsapi -lshell32 -ladvapi32 -Wl,--allow-multiple-definition -Wl,--stack,8388608 -static $(MIMALLOC_WRAP_FLAGS)
+WIN32_LIBS := -lws2_32 -lpsapi -lshell32 -ladvapi32 -Wl,--allow-multiple-definition -Wl,--stack,8388608 -static
endif
# STATIC=1 produces a fully static binary (for Alpine/musl portable builds)
@@ -115,9 +118,9 @@ ifeq ($(STATIC),1)
STATIC_FLAGS := -static
endif
-LDFLAGS = -lm -lstdc++ -lpthread -lz $(WIN32_LIBS) $(STATIC_FLAGS)
-LDFLAGS_TEST = -lm -lstdc++ -lpthread -lz $(SANITIZE) $(WIN32_LIBS)
-LDFLAGS_TSAN = -lm -lstdc++ -lpthread -lz $(TSAN_SANITIZE) $(WIN32_LIBS)
+LDFLAGS = -lm -lstdc++ -lpthread -lz $(WIN32_LIBS) $(STATIC_FLAGS) $(MIMALLOC_WRAP_FLAGS)
+LDFLAGS_TEST = -lm -lstdc++ -lpthread -lz $(SANITIZE) $(WIN32_LIBS) $(MIMALLOC_WRAP_FLAGS)
+LDFLAGS_TSAN = -lm -lstdc++ -lpthread -lz $(TSAN_SANITIZE) $(WIN32_LIBS) $(MIMALLOC_WRAP_FLAGS)
# ── Source files ─────────────────────────────────────────────────
diff --git a/src/foundation/mem_override_win.c b/src/foundation/mem_override_win.c
index 6c7dfdf6c..acb691833 100644
--- a/src/foundation/mem_override_win.c
+++ b/src/foundation/mem_override_win.c
@@ -48,6 +48,7 @@
#include <string.h>
#ifdef _WIN32
+#include <malloc.h> /* _msize, _aligned_* */
/* The originals, supplied by the linker because of --wrap. */
void *__real_malloc(size_t size);
@@ -55,6 +56,11 @@ void *__real_calloc(size_t count, size_t size);
void *__real_realloc(void *block, size_t size);
void __real_free(void *block);
char *__real_strdup(const char *text);
+size_t __real__msize(void *block);
+/* Wrappers must call __real_* for anything that is itself wrapped: a plain
+ * _msize() call inside this file is an undefined reference to _msize, so the
+ * linker would redirect it straight back into __wrap__msize -- infinite
+ * recursion rather than the CRT's answer. */
/* True when mimalloc made this block, so it must be returned to mimalloc.
* NULL is nobody's: callers handle it before asking. */
@@ -121,7 +127,7 @@ void *__wrap_realloc(void *block, size_t size) {
* boundary once: allocate from mimalloc, move the smaller of the two
* sizes, release the original to the CRT. mi_usable_size is unavailable
* for a foreign block, so the CRT's own accounting bounds the copy. */
- size_t old_size = _msize(block);
+ size_t old_size = __real__msize(block);
void *moved = mi_malloc(size);
if (!moved) {
return NULL; /* original stays valid, as realloc requires */
@@ -135,7 +141,7 @@ size_t __wrap__msize(void *block) {
if (block && mem_override_is_ours(block)) {
return mi_usable_size(block);
}
- return block ? _msize(block) : 0;
+ return block ? __real__msize(block) : 0;
}
#endif /* _WIN32 */