diff --git a/seaweed-block/core/storage/wal_entry_fuzz_test.go b/seaweed-block/core/storage/wal_entry_fuzz_test.go new file mode 100644 index 0000000000..958e7c6b8a --- /dev/null +++ b/seaweed-block/core/storage/wal_entry_fuzz_test.go @@ -0,0 +1,231 @@ +package storage + +import ( + "bytes" + "encoding/binary" + "hash/crc32" + "reflect" + "testing" +) + +// walEntryFuzzSeeds are well-formed records of every kind the writers +// produce, plus truncated and corrupted variants. Under `go test` (no +// -fuzz) the fuzz targets below run exactly these seeds. +func walEntryFuzzSeeds(t testing.TB) [][]byte { + t.Helper() + enc := func(e walEntry) []byte { + b, err := e.encode() + if err != nil { + t.Fatalf("seed %+v: %v", e, err) + } + return b + } + pad, err := encodeWALPadding(walEntryHeaderSize + 16) + if err != nil { + t.Fatal(err) + } + block := bytes.Repeat([]byte{0xA5}, 512) + seeds := [][]byte{ + enc(walEntry{LSN: 1, Type: walEntryWrite, LBA: 7, Length: uint32(len(block)), Data: block}), + enc(walEntry{LSN: 9, Reserved: 2, Type: walEntryWriteBatch, LBA: 3, Length: 1024, Data: bytes.Repeat([]byte{0x5A}, 1024)}), + enc(walEntry{LSN: 4, Type: walEntryTrim, LBA: 10, Length: 8192}), + enc(walEntry{LSN: 5, Type: walEntryBarrier}), + pad, + {}, + make([]byte, walEntryHeaderSize), + } + good := seeds[0] + seeds = append(seeds, + good[:walEntryHeaderSize-1], // short header + good[:len(good)-1], // short trailer + append(append([]byte{}, good...), 0xFF), // trailing byte + ) + flipped := append([]byte{}, good...) + flipped[40] ^= 0x01 // data byte: CRC mismatch + seeds = append(seeds, flipped) + huge := append([]byte{}, good[:walEntryHeaderSize]...) + binary.LittleEndian.PutUint32(huge[26:], ^uint32(0)) // Length claims 4 GiB + seeds = append(seeds, huge) + return seeds +} + +// FuzzDecodeWALEntry: decodeWALEntry never panics; an error comes with the +// zero value; an accepted record is self-consistent and re-encodes to +// exactly the bytes it was decoded from. +func FuzzDecodeWALEntry(f *testing.F) { + for _, s := range walEntryFuzzSeeds(f) { + f.Add(s) + } + f.Fuzz(func(t *testing.T, data []byte) { + e, err := decodeWALEntry(data) + if err != nil { + if !reflect.DeepEqual(e, walEntry{}) { + t.Fatalf("error %v with a partial value %+v", err, e) + } + return + } + size := walEntryHeaderSize + len(e.Data) + if size > len(data) { + t.Fatalf("decoded %d bytes from a %d-byte buffer", size, len(data)) + } + if got := binary.LittleEndian.Uint32(data[size-4:]); got != uint32(size) { + t.Fatalf("EntrySize field %d, record is %d bytes", got, size) + } + switch e.Type { + case walEntryWrite, walEntryWriteBatch, walEntryPadding: + if uint32(len(e.Data)) != e.Length { + t.Fatalf("type %d: %d data bytes, Length %d", e.Type, len(e.Data), e.Length) + } + default: + if e.Data != nil { + t.Fatalf("type %d decoded with a payload", e.Type) + } + } + if gap := knownWALDecodeGap(e); gap != "" { + return // pinned below; see knownWALDecodeGap + } + if reason := walEntryEncoderRefuses(e); reason != "" { + t.Fatalf("decoder accepted a record the encoder refuses (%s): %+v", reason, e) + } + re, err := e.encode() + if err != nil { + t.Fatalf("re-encode of an accepted record: %v", err) + } + if !bytes.Equal(re, data[:size]) { + t.Fatalf("re-encode differs from the decoded bytes") + } + }) +} + +// FuzzWALEntryRoundTrip: every entry the encoder accepts decodes back to +// itself. +func FuzzWALEntryRoundTrip(f *testing.F) { + f.Add(uint64(1), uint64(0), uint8(walEntryWrite), uint8(0), uint64(7), uint32(4), []byte("abcd")) + f.Add(uint64(9), uint64(2), uint8(walEntryWriteBatch), uint8(0), uint64(3), uint32(2), []byte("xy")) + f.Add(uint64(4), uint64(0), uint8(walEntryTrim), uint8(0), uint64(10), uint32(8192), []byte(nil)) + f.Add(uint64(5), uint64(0), uint8(walEntryBarrier), uint8(0), uint64(0), uint32(0), []byte(nil)) + f.Add(uint64(6), uint64(0), uint8(walEntryPadding), uint8(0), uint64(0), uint32(3), []byte{0, 0, 0}) + f.Fuzz(func(t *testing.T, lsn, reserved uint64, typ, flags uint8, lba uint64, length uint32, data []byte) { + e := walEntry{LSN: lsn, Reserved: reserved, Type: typ, Flags: flags, LBA: lba, Length: length, Data: data} + if len(e.Data) == 0 { + e.Data = nil + } + buf, err := e.encode() + if err != nil { + return + } + if gap := knownWALEncodeGap(e); gap != "" { + return // pinned below; see knownWALEncodeGap + } + got, err := decodeWALEntry(buf) + if err != nil { + t.Fatalf("encoder accepted %+v but its bytes do not decode: %v", e, err) + } + if !reflect.DeepEqual(got, e) { + t.Fatalf("round trip: got %+v, want %+v", got, e) + } + }) +} + +// walEntryEncoderRefuses mirrors encodedSize's checks for a decoded entry +// ("" when the encoder accepts it). +func walEntryEncoderRefuses(e walEntry) string { + if _, err := e.encodedSize(); err != nil { + return err.Error() + } + return "" +} + +// rawWALEntry lays out header fields, payload and a valid CRC/EntrySize +// exactly as the on-disk format does, without the encoder's checks, so the +// fuzzer reaches the decoder's acceptance rules instead of the CRC. +func rawWALEntry(lsn, reserved uint64, typ, flags uint8, lba uint64, length uint32, payload []byte) []byte { + le := binary.LittleEndian + buf := make([]byte, 30+len(payload)+8) + le.PutUint64(buf[0:], lsn) + le.PutUint64(buf[8:], reserved) + buf[16] = typ + buf[17] = flags + le.PutUint64(buf[18:], lba) + le.PutUint32(buf[26:], length) + copy(buf[30:], payload) + end := 30 + len(payload) + le.PutUint32(buf[end:], crc32.ChecksumIEEE(buf[:end])) + le.PutUint32(buf[end+4:], uint32(len(buf))) + return buf +} + +// FuzzDecodeWALEntryFields: records with a valid CRC and EntrySize but +// arbitrary field values; the decoder must accept only what the encoder +// can produce. +func FuzzDecodeWALEntryFields(f *testing.F) { + f.Add(uint64(1), uint64(0), uint8(walEntryWrite), uint8(0), uint64(7), uint32(4), []byte("abcd")) + f.Add(uint64(9), uint64(2), uint8(walEntryWriteBatch), uint8(0), uint64(3), uint32(2), []byte("xy")) + f.Add(uint64(4), uint64(0), uint8(walEntryTrim), uint8(0), uint64(10), uint32(8192), []byte(nil)) + f.Add(uint64(5), uint64(0), uint8(walEntryBarrier), uint8(0), uint64(0), uint32(0), []byte(nil)) + f.Add(uint64(6), uint64(0), uint8(walEntryPadding), uint8(0), uint64(0), uint32(3), []byte{0, 0, 0}) + f.Fuzz(func(t *testing.T, lsn, reserved uint64, typ, flags uint8, lba uint64, length uint32, payload []byte) { + buf := rawWALEntry(lsn, reserved, typ, flags, lba, length, payload) + e, err := decodeWALEntry(buf) + if err != nil { + if !reflect.DeepEqual(e, walEntry{}) { + t.Fatalf("error %v with a partial value %+v", err, e) + } + return + } + if gap := knownWALDecodeGap(e); gap != "" { + return // pinned below; see knownWALDecodeGap + } + if reason := walEntryEncoderRefuses(e); reason != "" { + t.Fatalf("decoder accepted a record the encoder refuses (%s): %+v", reason, e) + } + re, err := e.encode() + if err != nil || !bytes.Equal(re, buf[:walEntryHeaderSize+len(e.Data)]) { + t.Fatalf("accepted record does not re-encode to its bytes (err %v): %+v", err, e) + } + }) +} + +// knownWALDecodeGap names the record shapes decodeWALEntry accepts although +// the encoder never produces them (found by FuzzDecodeWALEntryFields; +// reported, not fixed here). Recovery already fails closed on an invalid +// batch (WALIntegrity), so a decoder-level rejection is not a drop-in +// change: it would turn that into a torn-entry stop. Any shape not listed +// here fails the fuzz targets. +func knownWALDecodeGap(e walEntry) string { + switch e.Type { + case walEntryWrite: + if e.Length == 0 { + return "write with no data" + } + case walEntryWriteBatch: + if e.Reserved == 0 || e.Length == 0 { + return "batch shape (checked by recovery, not by the decoder)" + } + case walEntryBarrier: + if e.Length != 0 { + return "barrier with a non-zero Length" + } + case walEntryTrim, walEntryPadding: + default: + return "unknown entry type" + } + return "" +} + +// knownWALEncodeGap names the entries walEntry.encode accepts but whose +// bytes do not decode back (found by FuzzWALEntryRoundTrip; no writer +// builds them). +func knownWALEncodeGap(e walEntry) string { + switch e.Type { + case walEntryWrite, walEntryWriteBatch, walEntryTrim, walEntryBarrier: + return "" + case walEntryPadding: + if uint32(len(e.Data)) != e.Length { + return "padding whose Length differs from its payload" + } + return "" + default: + return "unknown entry type" + } +}