1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
package msgp
import (
"archive/zip"
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"os"
"runtime"
"strconv"
"testing"
)
// Fuzz Reader tests the Reader methods with fuzzing inputs.
func FuzzReader(f *testing.F) {
addFuzzDataFromZip(f, "testdata/FuzzCorpus.zip")
f.Fuzz(func(t *testing.T, data []byte) {
if len(data) == 0 || len(data) > 1<<20 {
return
}
var tmp5 [5]byte
r := NewReader(bytes.NewReader(data))
// Set some rather low limits for the reader to avoid excessive memory usage.
r.SetMaxRecursionDepth(1000)
r.SetMaxElements(10000)
r.SetMaxStringLength(1000)
reset := func() {
t.Helper()
// Reset the test state if needed
r.Reset(bytes.NewReader(data))
}
// Uses reset when data is read off the stream.
r.BufferSize()
r.Buffered()
r.CopyNext(io.Discard)
reset()
r.IsNil()
r.NextType()
r.Read(tmp5[:])
reset()
r.ReadArrayHeader()
reset()
r.ReadBool()
reset()
r.ReadByte()
reset()
r.ReadBytes(tmp5[:])
reset()
r.ReadComplex128()
reset()
r.ReadComplex64()
reset()
r.ReadDuration()
reset()
r.ReadExactBytes(tmp5[:])
reset()
r.ReadExtension(testExt{})
reset()
r.ReadExtensionRaw()
reset()
r.ReadFloat32()
reset()
r.ReadFloat64()
reset()
r.ReadFull(tmp5[:])
reset()
r.ReadInt()
reset()
r.ReadInt16()
reset()
r.ReadInt32()
reset()
r.ReadInt64()
reset()
r.ReadInt8()
reset()
r.ReadIntf()
reset()
runtime.GC()
r.ReadJSONNumber()
reset()
r.ReadMapHeader()
reset()
r.ReadMapKey(tmp5[:])
reset()
runtime.GC()
r.ReadMapKeyPtr()
reset()
r.ReadMapStrIntf(map[string]interface{}{})
reset()
r.ReadNil()
reset()
r.ReadString()
reset()
r.ReadStringAsBytes(tmp5[:])
reset()
r.ReadStringHeader()
reset()
r.ReadTime()
reset()
r.ReadTimeUTC()
reset()
r.ReadUint()
reset()
r.ReadUint16()
reset()
r.ReadUint32()
reset()
r.ReadUint64()
reset()
r.ReadUint64()
reset()
r.ReadUint8()
reset()
r.Skip()
reset()
r.WriteToJSON(io.Discard)
})
}
// Test all byte reading functions with fuzzing inputs.
// Since we are reading bytes memory allocations should be well controlled.
func FuzzReadBytes(f *testing.F) {
addFuzzDataFromZip(f, "testdata/FuzzCorpus.zip")
f.Fuzz(func(t *testing.T, data []byte) {
if len(data) == 0 || len(data) > 1<<20 {
return
}
var tmp5 [5]byte
ReadArrayHeaderBytes(data)
ReadBoolBytes(data)
ReadByteBytes(data)
ReadBytesBytes(data, tmp5[:])
ReadBytesZC(data)
ReadComplex128Bytes(data)
ReadComplex64Bytes(data)
ReadDurationBytes(data)
ReadExactBytes(data, tmp5[:])
ReadExtensionBytes(data, testExt{})
ReadFloat32Bytes(data)
ReadFloat64Bytes(data)
ReadInt8Bytes(data)
ReadInt16Bytes(data)
ReadInt32Bytes(data)
ReadInt64Bytes(data)
ReadIntBytes(data)
ReadIntfBytes(data)
ReadJSONNumberBytes(data)
ReadMapHeaderBytes(data)
ReadMapKeyZC(data)
ReadMapStrIntfBytes(data, map[string]interface{}{})
ReadNilBytes(data)
ReadStringBytes(data)
ReadStringAsBytes(data, tmp5[:])
ReadStringBytes(data)
ReadStringZC(data)
ReadTimeBytes(data)
ReadTimeUTCBytes(data)
ReadUint8Bytes(data)
ReadUint16Bytes(data)
ReadUint32Bytes(data)
ReadUint64Bytes(data)
ReadUintBytes(data)
UnmarshalAsJSON(io.Discard, data)
Skip(data)
})
}
type testExt struct{}
func (e testExt) ExtensionType() int8 {
return 42
}
func (e testExt) Len() int {
return 3
}
var marshalExtContent = bytes.Repeat([]byte("x"), 3)
func (e testExt) MarshalBinaryTo(i []byte) error {
if len(i) < e.Len() {
return io.ErrShortBuffer
}
copy(i, marshalExtContent)
return nil
}
func (e testExt) UnmarshalBinary(i []byte) error {
if len(i) < e.Len() {
return io.ErrUnexpectedEOF
}
return nil
}
// addFuzzDataFromZip will read the supplied zip and add all as corpus for f.
// Byte slices only.
func addFuzzDataFromZip(f *testing.F, filename string) {
file, err := os.Open(filename)
if err != nil {
f.Fatal(err)
}
fi, err := file.Stat()
if fi == nil {
return
}
if err != nil {
f.Fatal(err)
}
zr, err := zip.NewReader(file, fi.Size())
if err != nil {
f.Fatal(err)
}
for _, file := range zr.File {
rc, err := file.Open()
if err != nil {
f.Fatal(err)
}
b, err := io.ReadAll(rc)
if err != nil {
f.Fatal(err)
}
rc.Close()
if !bytes.HasPrefix(b, []byte("go test fuzz")) {
continue
}
vals, err := unmarshalCorpusFile(b)
if err != nil {
f.Fatal(err)
}
for _, v := range vals {
f.Add(v)
}
}
}
// unmarshalCorpusFile decodes corpus bytes into their respective values.
func unmarshalCorpusFile(b []byte) ([][]byte, error) {
if len(b) == 0 {
return nil, fmt.Errorf("cannot unmarshal empty string")
}
lines := bytes.Split(b, []byte("\n"))
if len(lines) < 2 {
return nil, fmt.Errorf("must include version and at least one value")
}
var vals = make([][]byte, 0, len(lines)-1)
for _, line := range lines[1:] {
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
v, err := parseCorpusValue(line)
if err != nil {
return nil, fmt.Errorf("malformed line %q: %v", line, err)
}
vals = append(vals, v)
}
return vals, nil
}
// parseCorpusValue
func parseCorpusValue(line []byte) ([]byte, error) {
fs := token.NewFileSet()
expr, err := parser.ParseExprFrom(fs, "(test)", line, 0)
if err != nil {
return nil, err
}
call, ok := expr.(*ast.CallExpr)
if !ok {
return nil, fmt.Errorf("expected call expression")
}
if len(call.Args) != 1 {
return nil, fmt.Errorf("expected call expression with 1 argument; got %d", len(call.Args))
}
arg := call.Args[0]
if arrayType, ok := call.Fun.(*ast.ArrayType); ok {
if arrayType.Len != nil {
return nil, fmt.Errorf("expected []byte or primitive type")
}
elt, ok := arrayType.Elt.(*ast.Ident)
if !ok || elt.Name != "byte" {
return nil, fmt.Errorf("expected []byte")
}
lit, ok := arg.(*ast.BasicLit)
if !ok || lit.Kind != token.STRING {
return nil, fmt.Errorf("string literal required for type []byte")
}
s, err := strconv.Unquote(lit.Value)
if err != nil {
return nil, err
}
return []byte(s), nil
}
return nil, fmt.Errorf("expected []byte")
}
|