gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/uefivars/boot/fuzz.go (about)

     1  // Copyright 2020 the u-root 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  // SPDX-License-Identifier: BSD-3-Clause
     6  //
     7  
     8  // +build gofuzz
     9  
    10  package boot
    11  
    12  import (
    13  	"log"
    14  	"os"
    15  )
    16  
    17  /*
    18  go get github.com/dvyukov/go-fuzz/go-fuzz
    19  go get github.com/dvyukov/go-fuzz/go-fuzz-build
    20  
    21  go-fuzz-build -func FuzzParseFilePathList github.com/u-root/u-root/pkg/uefivars/boot
    22  go-fuzz -bin=./boot-fuzz.zip -workdir=fuzz
    23  ...
    24  */
    25  
    26  var tmpdir = "/tmp/fuzz-resolve-workdir"
    27  
    28  func init() {
    29  	//divert logging - greatly increases exec speed
    30  	null, err := os.OpenFile("/dev/null", os.O_WRONLY, 0200)
    31  	if err != nil {
    32  		panic(err)
    33  	}
    34  	log.SetOutput(null)
    35  	log.SetFlags(0)
    36  
    37  	err = os.MkdirAll(tmpdir, 0755)
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  }
    42  
    43  func FuzzParseFilePathList(data []byte) int {
    44  	list, err := ParseFilePathList(data)
    45  	if err != nil {
    46  		return 0
    47  	}
    48  	_ = list.String()
    49  	for _, p := range list {
    50  		r, err := p.Resolver()
    51  		if err != nil {
    52  			continue
    53  		}
    54  		_ = r.String()
    55  		_ = r.BlockInfo()
    56  		_, _ = r.Resolve(tmpdir)
    57  	}
    58  	return 1
    59  }