test(mem): unpoison mimalloc output in the MSan test runner

The MSan lane compiles mimalloc without -fsanitize=memory on purpose, so
MSan never sees mimalloc write its own stack. Its text output is the one
place those stack bytes reach an MSan interceptor: a message formatted in
a mimalloc stack buffer arrives at fputs still carrying the shadow an
earlier instrumented frame left there (cbm_log's line_buf), and MSan
reports initialized bytes as uninitialized.

mimalloc 3.5.3 prints an arena-alignment fallback warning on the x86-64
MSan lane, which turned this into a use-of-uninitialized-value report in
the mem suite (_mi_prim_out_stderr, 43 bytes: the per-thread prefix).

Under __has_feature(memory_sanitizer) only, the test runner now registers
a mimalloc output handler at the top of main that unpoisons the finished
text and writes it to stderr. Registering replays mimalloc's delayed-output
buffer, which its constructor already wrote to stderr before main, so the
handler skips that replay instead of repeating it behind a stray newline.
Production code and every non-MSan build are unchanged.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
This commit is contained in:
Martin Vogel committed 2026-10-28 01:07:23 +01:00
1 parent 3d65c609e1
commit 0f8b46570c
1 file changed
+38
+38
View File
@@ -2122,7 +2122,45 @@ static int tf_role_dispatch(tf_phase_t phase, int argc, char **argv) {
const char *cbm_test_program_path;
#if defined(__has_feature)
#if __has_feature(memory_sanitizer)
#define TF_MSAN_MIMALLOC_OUTPUT 1
#endif
#endif
#ifdef TF_MSAN_MIMALLOC_OUTPUT
#include <mimalloc.h>
#include <sanitizer/msan_interface.h>
/* MSan lane only. mimalloc is deliberately left uninstrumented there
* (build/msan/mimalloc.o is compiled without -fsanitize=memory), so MSan never
* sees it write its own stack. Its text output is the one place those stack
* bytes reach an MSan interceptor: a message formatted in a mimalloc stack
* buffer arrives at fputs still carrying the shadow an earlier instrumented
* frame left there, and MSan reports initialized bytes as uninitialized.
* mimalloc 3.5.3 prints an arena-alignment fallback warning on the x86-64 MSan
* lane, which tripped this in the mem suite. Routing mimalloc's output through
* this handler marks the finished text initialized before it reaches fputs. */
static bool tf_msan_mi_output_live;
static void tf_msan_mi_output(const char *msg, void *arg) {
(void)arg;
/* Registering replays mimalloc's delayed-output buffer into the handler.
* Its constructor already wrote all of that to stderr before main, so the
* replay would only repeat it behind a stray newline: skip it. */
if (!tf_msan_mi_output_live || !msg || !msg[0]) {
return;
}
__msan_unpoison_string(msg);
(void)fputs(msg, stderr);
}
#endif
int main(int argc, char **argv) {
#ifdef TF_MSAN_MIMALLOC_OUTPUT
mi_register_output(tf_msan_mi_output, NULL);
tf_msan_mi_output_live = true;
#endif
cbm_test_program_path = tf_runner_image(argc, argv);
int role_rc = tf_role_dispatch(TF_PHASE_RAW, argc, argv);
if (role_rc >= 0) {