Skip to content

Commit 0f8b465

Browse files
committed
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 <[email protected]>
1 parent 3d65c60 commit 0f8b465

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

‎tests/test_main.c‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,7 +2122,45 @@ static int tf_role_dispatch(tf_phase_t phase, int argc, char **argv) {
21222122

21232123
const char *cbm_test_program_path;
21242124

2125+
#if defined(__has_feature)
2126+
#if __has_feature(memory_sanitizer)
2127+
#define TF_MSAN_MIMALLOC_OUTPUT 1
2128+
#endif
2129+
#endif
2130+
2131+
#ifdef TF_MSAN_MIMALLOC_OUTPUT
2132+
#include <mimalloc.h>
2133+
#include <sanitizer/msan_interface.h>
2134+
2135+
/* MSan lane only. mimalloc is deliberately left uninstrumented there
2136+
* (build/msan/mimalloc.o is compiled without -fsanitize=memory), so MSan never
2137+
* sees it write its own stack. Its text output is the one place those stack
2138+
* bytes reach an MSan interceptor: a message formatted in a mimalloc stack
2139+
* buffer arrives at fputs still carrying the shadow an earlier instrumented
2140+
* frame left there, and MSan reports initialized bytes as uninitialized.
2141+
* mimalloc 3.5.3 prints an arena-alignment fallback warning on the x86-64 MSan
2142+
* lane, which tripped this in the mem suite. Routing mimalloc's output through
2143+
* this handler marks the finished text initialized before it reaches fputs. */
2144+
static bool tf_msan_mi_output_live;
2145+
2146+
static void tf_msan_mi_output(const char *msg, void *arg) {
2147+
(void)arg;
2148+
/* Registering replays mimalloc's delayed-output buffer into the handler.
2149+
* Its constructor already wrote all of that to stderr before main, so the
2150+
* replay would only repeat it behind a stray newline: skip it. */
2151+
if (!tf_msan_mi_output_live || !msg || !msg[0]) {
2152+
return;
2153+
}
2154+
__msan_unpoison_string(msg);
2155+
(void)fputs(msg, stderr);
2156+
}
2157+
#endif
2158+
21252159
int main(int argc, char **argv) {
2160+
#ifdef TF_MSAN_MIMALLOC_OUTPUT
2161+
mi_register_output(tf_msan_mi_output, NULL);
2162+
tf_msan_mi_output_live = true;
2163+
#endif
21262164
cbm_test_program_path = tf_runner_image(argc, argv);
21272165
int role_rc = tf_role_dispatch(TF_PHASE_RAW, argc, argv);
21282166
if (role_rc >= 0) {

0 commit comments

Comments
 (0)