Skip to content

Commit d89ccab

Browse files
committed
fix(mem): the parked result image is declared defined for MemorySanitizer
The MSan lane on 9724d90 stopped in the extraction suite's spill round-trip test: uninitialized bytes in fwrite at offset 4087 of a 6,952-byte region, the parked result's arena block (the stack overflow that followed is MemorySanitizer's own report path failing on the nested bug). The park writes a result as an opaque image: the record header, a struct copy, and the compacted arena block. Both carry padding bytes no code ever wrote, inside structs and between objects, and MSan tracks that mark through the compaction's memcpy, so zeroing the destination block only moved the report (offset 6714 in the local MSan container). The image is read back whole and never interpreted byte by byte; the padding content is irrelevant on disk. Under MSan the header and the block are declared defined right before the write (__msan_unpoison, compiled in only when the build is instrumented); every other build compiles the annotation to nothing. The exact block is also zeroed once when it is created, so the gaps between objects are deterministic in the spill file. Verification: extraction, arena and parallel suites under ASan on the fixed tree; the extraction suite in the local MSan container image passes 348 / 0 with no sanitizer report (it reproduced the CI report exactly before the fix). Signed-off-by: Martin Vogel <[email protected]>
1 parent 9e69cb7 commit d89ccab

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎internal/cbm/result_spill.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@
2121
#include "foundation/log.h"
2222
#include "foundation/mem_core.h"
2323

24+
/* The park writes a result as an opaque image: the record header (a struct
25+
* copy) and the compacted arena block. Both carry padding bytes no code ever
26+
* wrote -- inside structs, between objects -- and MemorySanitizer tracks
27+
* that mark through the compaction's memcpy, so it refuses the fwrite of an
28+
* image that is read back whole and never interpreted byte by byte (CI MSan
29+
* lane on #2202: offset 4087, then 6714, of a 6,952-byte block). Under MSan
30+
* the image is declared defined right before the write; every other build
31+
* compiles this to nothing. */
32+
#if defined(__has_feature)
33+
#if __has_feature(memory_sanitizer)
34+
#include <sanitizer/msan_interface.h>
35+
#define SPILL_IMAGE_DEFINED(p, n) __msan_unpoison((p), (n))
36+
#endif
37+
#endif
38+
#ifndef SPILL_IMAGE_DEFINED
39+
#define SPILL_IMAGE_DEFINED(p, n) ((void)0)
40+
#endif
41+
2442
#include <stdatomic.h>
2543
#include <stdio.h>
2644
#include <stdlib.h>
@@ -133,6 +151,8 @@ bool cbm_result_spill_park(cbm_result_spill_t *sp, int writer, int slot, CBMFile
133151
hdr.old_base = (uint64_t)(uintptr_t)result->arena.blocks[0];
134152
hdr.header = *result;
135153
hdr.header.cached_tree = NULL; /* never on disk: the loader gets no tree */
154+
SPILL_IMAGE_DEFINED(&hdr, sizeof(hdr));
155+
SPILL_IMAGE_DEFINED(result->arena.blocks[0], hdr.block_len);
136156
cbm_mutex_lock(&f->mu);
137157
uint64_t offset = f->end;
138158
bool ok = SPILL_SEEK(f->fp, offset) == 0 && fwrite(&hdr, sizeof(hdr), 1, f->fp) == 1 &&

‎src/foundation/arena.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ void cbm_arena_init_exact(CBMArena *a, size_t bytes) {
4545
size_t block = (bytes + ARENA_ALIGN) & ~(size_t)ARENA_ALIGN;
4646
cbm_arena_init_sized(a, block);
4747
a->grow_size = CBM_ARENA_DEFAULT_BLOCK_SIZE;
48+
/* An exact block is a compacted result's image and may be written to
49+
* disk whole (result_spill): the alignment gaps between objects and the
50+
* padding inside structs are never written by the copy, and MemorySanitizer
51+
* refuses an fwrite of uninitialized bytes (CI MSan lane on #2202, offset
52+
* 4087 of a 6,952-byte block). Zeroed once here, every byte of the image
53+
* is defined; the cost is one pass over memory the copy is about to touch. */
54+
if (a->blocks[0]) {
55+
memset(a->blocks[0], 0, a->block_sizes[0]);
56+
}
4857
}
4958

5059
static int arena_grow(CBMArena *a, size_t min_size) {

0 commit comments

Comments
 (0)