github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/internal/traceparser/fuzz.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build gofuzz
     6  
     7  package traceparser
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"log"
    13  )
    14  
    15  // at first we ran the old parser, and return 0 if it failed, on the theory that we don't have
    16  // to do better. But that leads to very few crashes to look at.
    17  // Maybe better just to make it so that the new parser doesn't misbehave, and if it doesn't get
    18  // an error, that the old parser gets the same results. (up to whatever)
    19  // perhaps even better would be to seed corpus with examples from which the 16-byte header
    20  // has been stripped, and add it in Fuzz, so the fuzzer doesn't spend a lot of time making
    21  // changes we reject in the header. (this may not be necessary)
    22  
    23  func Fuzz(data []byte) int {
    24  	if len(data) < 16 {
    25  		return 0
    26  	}
    27  	switch x := string(data[:16]); x {
    28  	default:
    29  		return 0
    30  	case "go 1.9 trace\000\000\000\000":
    31  		break
    32  	case "go 1.10 trace\000\000\000":
    33  		break
    34  	case "go 1.11 trace\000\000\000":
    35  		break
    36  	}
    37  	p, errp := ParseBuffer(bytes.NewBuffer(data))
    38  	if errp != nil {
    39  		if p != nil {
    40  			panic(fmt.Sprintf("p not nil on error %s", errp))
    41  		}
    42  	}
    43  	// TODO(pjw): if no errors, compare parses?
    44  	return 1
    45  }
    46  
    47  func init() {
    48  	log.SetFlags(log.Lshortfile)
    49  }