From 4f743e18aecda8f5ebf25b400452bd4c3409e4aa Mon Sep 17 00:00:00 2001 From: Onur Gündüz Date: Wed, 17 Jun 2026 01:35:54 +0200 Subject: tests: align fixture safetensors headers to 8 bytes --- tests/test_safetensors.c | 9 ++++++++- tests/tiny_model.h | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_safetensors.c b/tests/test_safetensors.c index 607bfc5..1cfafd5 100644 --- a/tests/test_safetensors.c +++ b/tests/test_safetensors.c @@ -22,12 +22,19 @@ write_st(const char *path, const char *header, const void *payload, size_t paylo perror(path); exit(2); } + /* Pad the header to an 8-byte boundary with trailing spaces so the data + * section is 8-aligned, matching the real safetensors serializer; otherwise + * tensor data lands unaligned and typed reads of it are UBSan-flagged UB. */ uint64_t n = strlen(header); + uint64_t pad = (8 - (n % 8)) % 8; + uint64_t total = n + pad; unsigned char len8[8]; for (int i = 0; i < 8; i++) - len8[i] = (unsigned char)(n >> (8 * i)); + len8[i] = (unsigned char)(total >> (8 * i)); fwrite(len8, 1, 8, f); fwrite(header, 1, n, f); + for (uint64_t i = 0; i < pad; i++) + fputc(' ', f); if (payload_len) fwrite(payload, 1, payload_len, f); fclose(f); diff --git a/tests/tiny_model.h b/tests/tiny_model.h index 9aee665..54ad162 100644 --- a/tests/tiny_model.h +++ b/tests/tiny_model.h @@ -142,6 +142,18 @@ static int tm_write_safetensors_with_prefix_options(const char *path, if (hoff >= sizeof(header) - 1) return -1; + /* Pad the header with trailing spaces (valid JSON whitespace the parser + * skips) so the data section starts at a multiple of 8, exactly as the real + * safetensors serializer does. Without this the tensor data lands at an + * unaligned address and the loader's typed reads of it are UB - harmless on + * x86/arm64 but flagged by UBSan. Real model files are always aligned, so + * the fixtures must be too. */ + while ((8 + hoff) % 8 != 0) { + if (hoff >= sizeof(header)) + return -1; + header[hoff++] = ' '; + } + FILE *f = fopen(path, "wb"); if (!f) return -1; -- cgit v1.3.1