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