| 1 |
//go:build (amd64 || arm64) && !appengine && !noasm && gc |
| 2 |
|
| 3 |
package zstd |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
) |
| 9 |
|
| 10 |
// This file holds the parts of the assembly sequence decoder that are identical |
| 11 |
// across architectures: the context structs exchanged with the asm, the error |
| 12 |
// codes, and the decode/decodeSync/executeSimple wrappers. Each architecture |
| 13 |
// supplies the small dispatch helpers (decodeAsm, decodeSyncAsm, |
| 14 |
// executeSimpleAsm) that select the concrete asm routine — amd64 also chooses a |
| 15 |
// BMI2 variant, arm64 has a single implementation. |
| 16 |
|
| 17 |
type decodeSyncAsmContext struct { |
| 18 |
llTable []decSymbol |
| 19 |
mlTable []decSymbol |
| 20 |
ofTable []decSymbol |
| 21 |
llState uint64 |
| 22 |
mlState uint64 |
| 23 |
ofState uint64 |
| 24 |
iteration int |
| 25 |
litRemain int |
| 26 |
out []byte |
| 27 |
outPosition int |
| 28 |
literals []byte |
| 29 |
litPosition int |
| 30 |
history []byte |
| 31 |
windowSize int |
| 32 |
ll int // set on error (not for all errors, please refer to _generate/gen.go) |
| 33 |
ml int // set on error (not for all errors, please refer to _generate/gen.go) |
| 34 |
mo int // set on error (not for all errors, please refer to _generate/gen.go) |
| 35 |
} |
| 36 |
|
| 37 |
type decodeAsmContext struct { |
| 38 |
llTable []decSymbol |
| 39 |
mlTable []decSymbol |
| 40 |
ofTable []decSymbol |
| 41 |
llState uint64 |
| 42 |
mlState uint64 |
| 43 |
ofState uint64 |
| 44 |
iteration int |
| 45 |
seqs []seqVals |
| 46 |
litRemain int |
| 47 |
} |
| 48 |
|
| 49 |
type executeAsmContext struct { |
| 50 |
seqs []seqVals |
| 51 |
seqIndex int |
| 52 |
out []byte |
| 53 |
history []byte |
| 54 |
literals []byte |
| 55 |
outPosition int |
| 56 |
litPosition int |
| 57 |
windowSize int |
| 58 |
} |
| 59 |
|
| 60 |
const noError = 0 |
| 61 |
|
| 62 |
// error reported when mo == 0 && ml > 0 |
| 63 |
const errorMatchLenOfsMismatch = 1 |
| 64 |
|
| 65 |
// error reported when ml > maxMatchLen |
| 66 |
const errorMatchLenTooBig = 2 |
| 67 |
|
| 68 |
// error reported when mo > available history or mo > s.windowSize |
| 69 |
const errorMatchOffTooBig = 3 |
| 70 |
|
| 71 |
// error reported when the sum of literal lengths exeeceds the literal buffer size |
| 72 |
const errorNotEnoughLiterals = 4 |
| 73 |
|
| 74 |
// error reported when capacity of `out` is too small |
| 75 |
const errorNotEnoughSpace = 5 |
| 76 |
|
| 77 |
// error reported when bits are overread. |
| 78 |
const errorOverread = 6 |
| 79 |
|
| 80 |
// useSafeDecodeSync reports whether decodeSyncSimple must use the bounds-exact |
| 81 |
// ("safe") copy variants for the current buffer geometry. |
| 82 |
// |
| 83 |
// When the output and literal buffers have compressedBlockOverAlloc (16) |
| 84 |
// bytes of slack past their logical use, the assembly may use extended |
| 85 |
// memory copies that read and write in 16-byte blocks, overrunning the end |
| 86 |
// of a literal run or match by up to 15 bytes. Otherwise it must use the |
| 87 |
// bounds-exact ("safe") copies. This mirrors the analogous, always-dynamic |
| 88 |
// selection in executeSimple below. |
| 89 |
// |
| 90 |
// The unsafe copies were disabled in #644 (2022) as a mitigation for a |
| 91 |
// crash, but that crash's root cause — an unguarded bitReader overread that |
| 92 |
// produced out-of-range match offsets/lengths — was fixed three days later |
| 93 |
// in #645, which also added the fuzz corpus that has guarded this path since. |
| 94 |
// See #1168. An asan-instrumented fuzz job (see .github/workflows/go.yml) |
| 95 |
// covers the extended-copy path, which -race and plain fuzzing cannot. |
| 96 |
func (s *sequenceDecs) useSafeDecodeSync() bool { |
| 97 |
if s.maxSyncLen == 0 && cap(s.out)-len(s.out) < maxCompressedBlockSizeAlloc { |
| 98 |
return true |
| 99 |
} |
| 100 |
if s.maxSyncLen > 0 && cap(s.out)-len(s.out)-compressedBlockOverAlloc < int(s.maxSyncLen) { |
| 101 |
return true |
| 102 |
} |
| 103 |
if cap(s.literals) < len(s.literals)+compressedBlockOverAlloc { |
| 104 |
return true |
| 105 |
} |
| 106 |
return false |
| 107 |
} |
| 108 |
|
| 109 |
// decode sequences from the stream with the provided history but without a dictionary. |
| 110 |
func (s *sequenceDecs) decodeSyncSimple(hist []byte) (bool, error) { |
| 111 |
if len(s.dict) > 0 { |
| 112 |
return false, nil |
| 113 |
} |
| 114 |
if s.maxSyncLen == 0 && cap(s.out)-len(s.out) < maxCompressedBlockSize { |
| 115 |
return false, nil |
| 116 |
} |
| 117 |
|
| 118 |
useSafe := s.useSafeDecodeSync() |
| 119 |
|
| 120 |
br := s.br |
| 121 |
|
| 122 |
maxBlockSize := min(s.windowSize, maxCompressedBlockSize) |
| 123 |
|
| 124 |
ctx := decodeSyncAsmContext{ |
| 125 |
llTable: s.litLengths.fse.dt[:maxTablesize], |
| 126 |
mlTable: s.matchLengths.fse.dt[:maxTablesize], |
| 127 |
ofTable: s.offsets.fse.dt[:maxTablesize], |
| 128 |
llState: uint64(s.litLengths.state.state), |
| 129 |
mlState: uint64(s.matchLengths.state.state), |
| 130 |
ofState: uint64(s.offsets.state.state), |
| 131 |
iteration: s.nSeqs - 1, |
| 132 |
litRemain: len(s.literals), |
| 133 |
out: s.out, |
| 134 |
outPosition: len(s.out), |
| 135 |
literals: s.literals, |
| 136 |
windowSize: s.windowSize, |
| 137 |
history: hist, |
| 138 |
} |
| 139 |
|
| 140 |
s.seqSize = 0 |
| 141 |
startSize := len(s.out) |
| 142 |
|
| 143 |
errCode := decodeSyncAsm(s, br, &ctx, useSafe) |
| 144 |
switch errCode { |
| 145 |
case noError: |
| 146 |
break |
| 147 |
|
| 148 |
case errorMatchLenOfsMismatch: |
| 149 |
return true, fmt.Errorf("zero matchoff and matchlen (%d) > 0", ctx.ml) |
| 150 |
|
| 151 |
case errorMatchLenTooBig: |
| 152 |
return true, fmt.Errorf("match len (%d) bigger than max allowed length", ctx.ml) |
| 153 |
|
| 154 |
case errorMatchOffTooBig: |
| 155 |
return true, fmt.Errorf("match offset (%d) bigger than current history (%d)", |
| 156 |
ctx.mo, ctx.outPosition+len(hist)-startSize) |
| 157 |
|
| 158 |
case errorNotEnoughLiterals: |
| 159 |
return true, fmt.Errorf("unexpected literal count, want %d bytes, but only %d is available", |
| 160 |
ctx.ll, ctx.litRemain+ctx.ll) |
| 161 |
|
| 162 |
case errorOverread: |
| 163 |
return true, io.ErrUnexpectedEOF |
| 164 |
|
| 165 |
case errorNotEnoughSpace: |
| 166 |
size := ctx.outPosition + ctx.ll + ctx.ml |
| 167 |
if debugDecoder { |
| 168 |
println("msl:", s.maxSyncLen, "cap", cap(s.out), "bef:", startSize, "sz:", size-startSize, "mbs:", maxBlockSize, "outsz:", cap(s.out)-startSize) |
| 169 |
} |
| 170 |
return true, fmt.Errorf("output bigger than max block size (%d)", maxBlockSize) |
| 171 |
|
| 172 |
default: |
| 173 |
return true, fmt.Errorf("sequenceDecs_decode returned erroneous code %d", errCode) |
| 174 |
} |
| 175 |
|
| 176 |
s.seqSize += ctx.litRemain |
| 177 |
if s.seqSize > maxBlockSize { |
| 178 |
return true, fmt.Errorf("output bigger than max block size (%d)", maxBlockSize) |
| 179 |
} |
| 180 |
err := br.close() |
| 181 |
if err != nil { |
| 182 |
printf("Closing sequences: %v, %+v\n", err, *br) |
| 183 |
return true, err |
| 184 |
} |
| 185 |
|
| 186 |
s.literals = s.literals[ctx.litPosition:] |
| 187 |
t := ctx.outPosition |
| 188 |
s.out = s.out[:t] |
| 189 |
|
| 190 |
// Add final literals |
| 191 |
s.out = append(s.out, s.literals...) |
| 192 |
if debugDecoder { |
| 193 |
t += len(s.literals) |
| 194 |
if t != len(s.out) { |
| 195 |
panic(fmt.Errorf("length mismatch, want %d, got %d", len(s.out), t)) |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return true, nil |
| 200 |
} |
| 201 |
|
| 202 |
// decode sequences from the stream without the provided history. |
| 203 |
func (s *sequenceDecs) decode(seqs []seqVals) error { |
| 204 |
br := s.br |
| 205 |
|
| 206 |
maxBlockSize := min(s.windowSize, maxCompressedBlockSize) |
| 207 |
|
| 208 |
ctx := decodeAsmContext{ |
| 209 |
llTable: s.litLengths.fse.dt[:maxTablesize], |
| 210 |
mlTable: s.matchLengths.fse.dt[:maxTablesize], |
| 211 |
ofTable: s.offsets.fse.dt[:maxTablesize], |
| 212 |
llState: uint64(s.litLengths.state.state), |
| 213 |
mlState: uint64(s.matchLengths.state.state), |
| 214 |
ofState: uint64(s.offsets.state.state), |
| 215 |
seqs: seqs, |
| 216 |
iteration: len(seqs) - 1, |
| 217 |
litRemain: len(s.literals), |
| 218 |
} |
| 219 |
|
| 220 |
if debugDecoder { |
| 221 |
println("decode: decoding", len(seqs), "sequences", br.remain(), "bits remain on stream") |
| 222 |
} |
| 223 |
|
| 224 |
s.seqSize = 0 |
| 225 |
lte56bits := s.maxBits+s.offsets.fse.actualTableLog+s.matchLengths.fse.actualTableLog+s.litLengths.fse.actualTableLog <= 56 |
| 226 |
errCode := decodeAsm(s, br, &ctx, lte56bits) |
| 227 |
if errCode != 0 { |
| 228 |
i := len(seqs) - ctx.iteration - 1 |
| 229 |
switch errCode { |
| 230 |
case errorMatchLenOfsMismatch: |
| 231 |
ml := ctx.seqs[i].ml |
| 232 |
return fmt.Errorf("zero matchoff and matchlen (%d) > 0", ml) |
| 233 |
|
| 234 |
case errorMatchLenTooBig: |
| 235 |
ml := ctx.seqs[i].ml |
| 236 |
return fmt.Errorf("match len (%d) bigger than max allowed length", ml) |
| 237 |
|
| 238 |
case errorNotEnoughLiterals: |
| 239 |
ll := ctx.seqs[i].ll |
| 240 |
return fmt.Errorf("unexpected literal count, want %d bytes, but only %d is available", ll, ctx.litRemain+ll) |
| 241 |
case errorOverread: |
| 242 |
return io.ErrUnexpectedEOF |
| 243 |
} |
| 244 |
|
| 245 |
return fmt.Errorf("sequenceDecs_decode_amd64 returned erroneous code %d", errCode) |
| 246 |
} |
| 247 |
|
| 248 |
if ctx.litRemain < 0 { |
| 249 |
return fmt.Errorf("literal count is too big: total available %d, total requested %d", |
| 250 |
len(s.literals), len(s.literals)-ctx.litRemain) |
| 251 |
} |
| 252 |
|
| 253 |
s.seqSize += ctx.litRemain |
| 254 |
if s.seqSize > maxBlockSize { |
| 255 |
return fmt.Errorf("output bigger than max block size (%d)", maxBlockSize) |
| 256 |
} |
| 257 |
if debugDecoder { |
| 258 |
println("decode: ", br.remain(), "bits remain on stream. code:", errCode) |
| 259 |
} |
| 260 |
err := br.close() |
| 261 |
if err != nil { |
| 262 |
printf("Closing sequences: %v, %+v\n", err, *br) |
| 263 |
} |
| 264 |
return err |
| 265 |
} |
| 266 |
|
| 267 |
// executeSimple handles cases when dictionary is not used. |
| 268 |
func (s *sequenceDecs) executeSimple(seqs []seqVals, hist []byte) error { |
| 269 |
// Ensure we have enough output size... |
| 270 |
if len(s.out)+s.seqSize+compressedBlockOverAlloc > cap(s.out) { |
| 271 |
addBytes := s.seqSize + len(s.out) + compressedBlockOverAlloc |
| 272 |
s.out = append(s.out, make([]byte, addBytes)...) |
| 273 |
s.out = s.out[:len(s.out)-addBytes] |
| 274 |
} |
| 275 |
|
| 276 |
if debugDecoder { |
| 277 |
printf("Execute %d seqs with literals: %d into %d bytes\n", len(seqs), len(s.literals), s.seqSize) |
| 278 |
} |
| 279 |
|
| 280 |
var t = len(s.out) |
| 281 |
out := s.out[:t+s.seqSize] |
| 282 |
|
| 283 |
ctx := executeAsmContext{ |
| 284 |
seqs: seqs, |
| 285 |
seqIndex: 0, |
| 286 |
out: out, |
| 287 |
history: hist, |
| 288 |
outPosition: t, |
| 289 |
litPosition: 0, |
| 290 |
literals: s.literals, |
| 291 |
windowSize: s.windowSize, |
| 292 |
} |
| 293 |
// useSafe avoids overwriting the output buffer when the literals slice has |
| 294 |
// not been allocated with the required over-allocation slack. |
| 295 |
useSafe := cap(s.literals) < len(s.literals)+compressedBlockOverAlloc |
| 296 |
|
| 297 |
ok := executeSimpleAsm(&ctx, useSafe) |
| 298 |
if !ok { |
| 299 |
return fmt.Errorf("match offset (%d) bigger than current history (%d)", |
| 300 |
seqs[ctx.seqIndex].mo, ctx.outPosition+len(hist)) |
| 301 |
} |
| 302 |
s.literals = s.literals[ctx.litPosition:] |
| 303 |
t = ctx.outPosition |
| 304 |
|
| 305 |
// Add final literals |
| 306 |
copy(out[t:], s.literals) |
| 307 |
if debugDecoder { |
| 308 |
t += len(s.literals) |
| 309 |
if t != len(out) { |
| 310 |
panic(fmt.Errorf("length mismatch, want %d, got %d, ss: %d", len(out), t, s.seqSize)) |
| 311 |
} |
| 312 |
} |
| 313 |
s.out = out |
| 314 |
|
| 315 |
return nil |
| 316 |
} |