| 1 |
//go:build go1.18 |
| 2 |
|
| 3 |
package zstd |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
rdebug "runtime/debug" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/klauspost/compress/internal/cpuinfo" |
| 13 |
"github.com/klauspost/compress/internal/fuzz" |
| 14 |
) |
| 15 |
|
| 16 |
func FuzzDecodeAll(f *testing.F) { |
| 17 |
fuzz.AddFromZip(f, "testdata/decode-regression.zip", fuzz.TypeRaw, false) |
| 18 |
fuzz.AddFromZip(f, "testdata/fuzz/decode-corpus-raw.zip", fuzz.TypeRaw, testing.Short()) |
| 19 |
fuzz.AddFromZip(f, "testdata/fuzz/decode-corpus-encoded.zip", fuzz.TypeGoFuzz, testing.Short()) |
| 20 |
|
| 21 |
// compareNonAsm cross-checks the assembly decoder against a non-assembly |
| 22 |
// path and requires byte-identical output. On amd64 this disables the BMI2 |
| 23 |
// assembly, comparing the BMI2 and non-BMI2 routines. On other architectures |
| 24 |
// cpuinfo.HasBMI2 is false so the in-process check is skipped; run this |
| 25 |
// fuzzer additionally with -tags=noasm over the shared corpus to cover the |
| 26 |
// assembly-vs-generic differential there. |
| 27 |
const compareNonAsm = true |
| 28 |
|
| 29 |
f.Fuzz(func(t *testing.T, b []byte) { |
| 30 |
// Just test if we crash... |
| 31 |
defer func() { |
| 32 |
if r := recover(); r != nil { |
| 33 |
rdebug.PrintStack() |
| 34 |
t.Fatal(r) |
| 35 |
} |
| 36 |
}() |
| 37 |
|
| 38 |
decLow, err := NewReader(nil, WithDecoderLowmem(true), WithDecoderConcurrency(2), WithDecoderMaxMemory(20<<20), WithDecoderMaxWindow(1<<20), IgnoreChecksum(true)) |
| 39 |
if err != nil { |
| 40 |
t.Fatal(err) |
| 41 |
} |
| 42 |
defer decLow.Close() |
| 43 |
decHi, err := NewReader(nil, WithDecoderLowmem(false), WithDecoderConcurrency(2), WithDecoderMaxMemory(20<<20), WithDecoderMaxWindow(1<<20), IgnoreChecksum(true)) |
| 44 |
if err != nil { |
| 45 |
t.Fatal(err) |
| 46 |
} |
| 47 |
defer decHi.Close() |
| 48 |
b1, err1 := decLow.DecodeAll(b, make([]byte, 0, len(b))) |
| 49 |
b2, err2 := decHi.DecodeAll(b, make([]byte, 0, len(b))) |
| 50 |
if err1 != err2 { |
| 51 |
if (err1 == nil) != (err2 == nil) { |
| 52 |
t.Errorf("err low: %v, hi: %v", err1, err2) |
| 53 |
} |
| 54 |
} |
| 55 |
if err1 != nil { |
| 56 |
b1, b2 = b1[:0], b2[:0] |
| 57 |
} |
| 58 |
if !bytes.Equal(b1, b2) { |
| 59 |
t.Fatalf("Output mismatch, low: %v, hi: %v", err1, err2) |
| 60 |
} |
| 61 |
|
| 62 |
if compareNonAsm && cpuinfo.HasBMI2() { |
| 63 |
// Decode again with the BMI2 assembly disabled and require identical |
| 64 |
// output, exercising the assembly against the non-assembly path. |
| 65 |
// Defer the restore so BMI2 is re-enabled on every exit path. |
| 66 |
defer cpuinfo.DisableBMI2()() |
| 67 |
decRef, errRef := NewReader(nil, WithDecoderLowmem(true), WithDecoderConcurrency(1), WithDecoderMaxMemory(20<<20), WithDecoderMaxWindow(1<<20), IgnoreChecksum(true)) |
| 68 |
if errRef != nil { |
| 69 |
t.Fatal(errRef) |
| 70 |
} |
| 71 |
defer decRef.Close() |
| 72 |
bRef, errRef := decRef.DecodeAll(b, make([]byte, 0, len(b))) |
| 73 |
if (err1 == nil) != (errRef == nil) { |
| 74 |
t.Errorf("err asm: %v, non-asm: %v", err1, errRef) |
| 75 |
} |
| 76 |
if err1 == nil && !bytes.Equal(b1, bRef) { |
| 77 |
t.Fatalf("asm vs non-asm output mismatch") |
| 78 |
} |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
|
| 83 |
func FuzzDecAllNoBMI2(f *testing.F) { |
| 84 |
if !cpuinfo.HasBMI2() { |
| 85 |
f.Skip("No BMI, so already tested") |
| 86 |
return |
| 87 |
} |
| 88 |
defer cpuinfo.DisableBMI2()() |
| 89 |
FuzzDecodeAll(f) |
| 90 |
} |
| 91 |
|
| 92 |
func FuzzDecoder(f *testing.F) { |
| 93 |
fuzz.AddFromZip(f, "testdata/fuzz/decode-corpus-raw.zip", fuzz.TypeRaw, testing.Short()) |
| 94 |
fuzz.AddFromZip(f, "testdata/fuzz/decode-corpus-encoded.zip", fuzz.TypeGoFuzz, testing.Short()) |
| 95 |
//fuzz.AddFromZip(f, "testdata/fuzz/decode-oss.zip", fuzz.TypeOSSFuzz, false) |
| 96 |
|
| 97 |
brLow := newBytesReader(nil) |
| 98 |
brHi := newBytesReader(nil) |
| 99 |
f.Fuzz(func(t *testing.T, b []byte) { |
| 100 |
// Just test if we crash... |
| 101 |
defer func() { |
| 102 |
if r := recover(); r != nil { |
| 103 |
rdebug.PrintStack() |
| 104 |
t.Fatal(r) |
| 105 |
} |
| 106 |
}() |
| 107 |
brLow.Reset(b) |
| 108 |
brHi.Reset(b) |
| 109 |
decLow, err := NewReader(brLow, WithDecoderLowmem(true), WithDecoderConcurrency(2), WithDecoderMaxMemory(20<<20), WithDecoderMaxWindow(1<<20), IgnoreChecksum(true), WithDecodeBuffersBelow(8<<10)) |
| 110 |
if err != nil { |
| 111 |
t.Fatal(err) |
| 112 |
} |
| 113 |
defer decLow.Close() |
| 114 |
|
| 115 |
// Test with high memory, but sync decoding |
| 116 |
decHi, err := NewReader(brHi, WithDecoderLowmem(false), WithDecoderConcurrency(1), WithDecoderMaxMemory(20<<20), WithDecoderMaxWindow(1<<20), IgnoreChecksum(true), WithDecodeBuffersBelow(8<<10)) |
| 117 |
if err != nil { |
| 118 |
t.Fatal(err) |
| 119 |
} |
| 120 |
defer decHi.Close() |
| 121 |
|
| 122 |
if debugDecoder { |
| 123 |
fmt.Println("LOW CONCURRENT") |
| 124 |
} |
| 125 |
b1, err1 := io.ReadAll(decLow) |
| 126 |
|
| 127 |
if debugDecoder { |
| 128 |
fmt.Println("HI NOT CONCURRENT") |
| 129 |
} |
| 130 |
b2, err2 := io.ReadAll(decHi) |
| 131 |
if err1 != err2 { |
| 132 |
if (err1 == nil) != (err2 == nil) { |
| 133 |
t.Errorf("err low concurrent: %v, hi: %v", err1, err2) |
| 134 |
} |
| 135 |
} |
| 136 |
if err1 != nil { |
| 137 |
b1, b2 = b1[:0], b2[:0] |
| 138 |
} |
| 139 |
if !bytes.Equal(b1, b2) { |
| 140 |
t.Fatalf("Output mismatch, low concurrent: %v, hi: %v", err1, err2) |
| 141 |
} |
| 142 |
}) |
| 143 |
} |
| 144 |
|
| 145 |
func FuzzNoBMI2Dec(f *testing.F) { |
| 146 |
if !cpuinfo.HasBMI2() { |
| 147 |
f.Skip("No BMI, so already tested") |
| 148 |
return |
| 149 |
} |
| 150 |
defer cpuinfo.DisableBMI2()() |
| 151 |
FuzzDecoder(f) |
| 152 |
} |
| 153 |
|
| 154 |
func FuzzEncoding(f *testing.F) { |
| 155 |
fuzz.AddFromZip(f, "testdata/fuzz/encode-corpus-raw.zip", fuzz.TypeRaw, testing.Short()) |
| 156 |
fuzz.AddFromZip(f, "testdata/comp-crashers.zip", fuzz.TypeRaw, false) |
| 157 |
fuzz.AddFromZip(f, "testdata/fuzz/encode-corpus-encoded.zip", fuzz.TypeGoFuzz, testing.Short()) |
| 158 |
// Fuzzing tweaks: |
| 159 |
const ( |
| 160 |
// Test a subset of encoders. |
| 161 |
startFuzz = SpeedFastest |
| 162 |
endFuzz = SpeedBestCompression |
| 163 |
|
| 164 |
// Also tests with dictionaries... |
| 165 |
testDicts = true |
| 166 |
) |
| 167 |
|
| 168 |
var dec *Decoder |
| 169 |
var encs [SpeedBestCompression + 1]*Encoder |
| 170 |
var encsD [SpeedBestCompression + 1]*Encoder |
| 171 |
|
| 172 |
var dicts [][]byte |
| 173 |
if testDicts { |
| 174 |
zr := testCreateZipReader("testdata/dict-tests-small.zip", f) |
| 175 |
dicts = readDicts(f, zr) |
| 176 |
} |
| 177 |
|
| 178 |
if testing.Short() && *fuzzEndF > int(SpeedBetterCompression) { |
| 179 |
*fuzzEndF = int(SpeedBetterCompression) |
| 180 |
} |
| 181 |
|
| 182 |
initEnc := func() func() { |
| 183 |
var err error |
| 184 |
dec, err = NewReader(nil, WithDecoderConcurrency(2), WithDecoderDicts(dicts...), WithDecoderMaxWindow(64<<10), WithDecoderMaxMemory(uint64(*fuzzMaxF))) |
| 185 |
if err != nil { |
| 186 |
panic(err) |
| 187 |
} |
| 188 |
for level := startFuzz; level <= endFuzz; level++ { |
| 189 |
encs[level], err = NewWriter(nil, WithEncoderCRC(true), WithEncoderLevel(level), WithEncoderConcurrency(2), WithWindowSize(64<<10), WithZeroFrames(true), WithLowerEncoderMem(true)) |
| 190 |
if testDicts { |
| 191 |
encsD[level], err = NewWriter(nil, WithEncoderCRC(true), WithEncoderLevel(level), WithEncoderConcurrency(2), WithWindowSize(64<<10), WithZeroFrames(true), WithEncoderDict(dicts[int(level)%len(dicts)]), WithLowerEncoderMem(true), WithLowerEncoderMem(true)) |
| 192 |
} |
| 193 |
} |
| 194 |
return func() { |
| 195 |
dec.Close() |
| 196 |
for _, enc := range encs { |
| 197 |
if enc != nil { |
| 198 |
enc.Close() |
| 199 |
} |
| 200 |
} |
| 201 |
if testDicts { |
| 202 |
for _, enc := range encsD { |
| 203 |
if enc != nil { |
| 204 |
enc.Close() |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
f.Cleanup(initEnc()) |
| 212 |
|
| 213 |
var dst bytes.Buffer |
| 214 |
|
| 215 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 216 |
// Just test if we crash... |
| 217 |
defer func() { |
| 218 |
if r := recover(); r != nil { |
| 219 |
stack := rdebug.Stack() |
| 220 |
t.Fatalf("%v:\n%v", r, string(stack)) |
| 221 |
} |
| 222 |
}() |
| 223 |
if len(data) > *fuzzMaxF { |
| 224 |
return |
| 225 |
} |
| 226 |
var bufSize = len(data) |
| 227 |
if bufSize > 2 { |
| 228 |
// Make deterministic size |
| 229 |
bufSize = int(data[0]) | int(data[1])<<8 |
| 230 |
if bufSize >= len(data) { |
| 231 |
bufSize = len(data) / 2 |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
for level := *fuzzStartF; level <= *fuzzEndF; level++ { |
| 236 |
enc := encs[level] |
| 237 |
dst.Reset() |
| 238 |
enc.Reset(&dst) |
| 239 |
n, err := enc.Write(data) |
| 240 |
if err != nil { |
| 241 |
t.Fatal(err) |
| 242 |
} |
| 243 |
if n != len(data) { |
| 244 |
t.Fatal(fmt.Sprintln("Level", level, "Short write, got:", n, "want:", len(data))) |
| 245 |
} |
| 246 |
|
| 247 |
encoded := enc.EncodeAll(data, make([]byte, 0, bufSize)) |
| 248 |
if len(encoded) > enc.MaxEncodedSize(len(data)) { |
| 249 |
t.Errorf("max encoded size for %v: got: %d, want max: %d", len(data), len(encoded), enc.MaxEncodedSize(len(data))) |
| 250 |
} |
| 251 |
|
| 252 |
got, err := dec.DecodeAll(encoded, make([]byte, 0, bufSize)) |
| 253 |
if err != nil { |
| 254 |
t.Fatal(fmt.Sprintln("Level", level, "DecodeAll error:", err, "\norg:", len(data), "\nencoded", len(encoded))) |
| 255 |
} |
| 256 |
if !bytes.Equal(got, data) { |
| 257 |
t.Fatal(fmt.Sprintln("Level", level, "DecodeAll output mismatch\n", len(got), "org: \n", len(data), "(want)", "\nencoded:", len(encoded))) |
| 258 |
} |
| 259 |
|
| 260 |
err = enc.Close() |
| 261 |
if err != nil { |
| 262 |
t.Fatal(fmt.Sprintln("Level", level, "Close (buffer) error:", err)) |
| 263 |
} |
| 264 |
encoded2 := dst.Bytes() |
| 265 |
if len(encoded2) > enc.MaxEncodedSize(len(data)) { |
| 266 |
t.Errorf("max encoded size for %v: got: %d, want max: %d", len(data), len(encoded2), enc.MaxEncodedSize(len(data))) |
| 267 |
} |
| 268 |
if !bytes.Equal(encoded, encoded2) { |
| 269 |
got, err = dec.DecodeAll(encoded2, got[:0]) |
| 270 |
if err != nil { |
| 271 |
t.Fatal(fmt.Sprintln("Level", level, "DecodeAll (buffer) error:", err, "\norg:", len(data), "\nencoded", len(encoded2))) |
| 272 |
} |
| 273 |
if !bytes.Equal(got, data) { |
| 274 |
t.Fatal(fmt.Sprintln("Level", level, "DecodeAll (buffer) output mismatch\n", len(got), "org: \n", len(data), "(want)", "\nencoded:", len(encoded2))) |
| 275 |
} |
| 276 |
} |
| 277 |
if !testDicts { |
| 278 |
continue |
| 279 |
} |
| 280 |
enc = encsD[level] |
| 281 |
dst.Reset() |
| 282 |
enc.Reset(&dst) |
| 283 |
n, err = enc.Write(data) |
| 284 |
if err != nil { |
| 285 |
t.Fatal(err) |
| 286 |
} |
| 287 |
if n != len(data) { |
| 288 |
t.Fatal(fmt.Sprintln("Dict Level", level, "Short write, got:", n, "want:", len(data))) |
| 289 |
} |
| 290 |
|
| 291 |
encoded = enc.EncodeAll(data, encoded[:0]) |
| 292 |
if len(encoded) > enc.MaxEncodedSize(len(data)) { |
| 293 |
t.Errorf("max encoded size for %v: got: %d, want max: %d", len(data), len(encoded), enc.MaxEncodedSize(len(data))) |
| 294 |
} |
| 295 |
got, err = dec.DecodeAll(encoded, got[:0]) |
| 296 |
if err != nil { |
| 297 |
t.Fatal(fmt.Sprintln("Dict Level", level, "DecodeAll error:", err, "\norg:", len(data), "\nencoded", len(encoded))) |
| 298 |
} |
| 299 |
if !bytes.Equal(got, data) { |
| 300 |
t.Fatal(fmt.Sprintln("Dict Level", level, "DecodeAll output mismatch\n", len(got), "org: \n", len(data), "(want)", "\nencoded:", len(encoded))) |
| 301 |
} |
| 302 |
|
| 303 |
err = enc.Close() |
| 304 |
if err != nil { |
| 305 |
t.Fatal(fmt.Sprintln("Dict Level", level, "Close (buffer) error:", err)) |
| 306 |
} |
| 307 |
encoded2 = dst.Bytes() |
| 308 |
if len(encoded2) > enc.MaxEncodedSize(len(data)) { |
| 309 |
t.Errorf("max encoded size for %v: got: %d, want max: %d", len(data), len(encoded2), enc.MaxEncodedSize(len(data))) |
| 310 |
} |
| 311 |
if !bytes.Equal(encoded, encoded2) { |
| 312 |
got, err = dec.DecodeAll(encoded2, got[:0]) |
| 313 |
if err != nil { |
| 314 |
t.Fatal(fmt.Sprintln("Dict Level", level, "DecodeAll (buffer) error:", err, "\norg:", len(data), "\nencoded", len(encoded2))) |
| 315 |
} |
| 316 |
if !bytes.Equal(got, data) { |
| 317 |
t.Fatal(fmt.Sprintln("Dict Level", level, "DecodeAll (buffer) output mismatch\n", len(got), "org: \n", len(data), "(want)", "\nencoded:", len(encoded2))) |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
}) |
| 322 |
} |