github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/report/fuzz.go (about)

     1  // Copyright 2017 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package report
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/google/syzkaller/pkg/mgrconfig"
    10  	"github.com/google/syzkaller/sys/targets"
    11  )
    12  
    13  func Fuzz(data []byte) int {
    14  	res := 0
    15  	for os, reporter := range fuzzReporters {
    16  		typ := reporter.typ
    17  		containsCrash := reporter.ContainsCrash(data)
    18  		rep := reporter.Parse(data)
    19  		if containsCrash != (rep != nil) {
    20  			panic(fmt.Sprintf("%v: ContainsCrash and Parse disagree", typ))
    21  		}
    22  		if rep == nil {
    23  			continue
    24  		}
    25  		res = 1
    26  		reporter.Symbolize(rep)
    27  		if rep.Title == "" {
    28  			panic(fmt.Sprintf("%v: Title is empty", typ))
    29  		}
    30  		if len(rep.Report) == 0 {
    31  			panic(fmt.Sprintf("%v: len(Report) == 0", typ))
    32  		}
    33  		if len(rep.Output) == 0 {
    34  			panic(fmt.Sprintf("%v: len(Output) == 0", typ))
    35  		}
    36  		if os == targets.Fuchsia {
    37  			// Fuchsia has Start/End/SkipPos set incorrectly because it symbolizes before parsing.
    38  			continue
    39  		}
    40  		if rep.StartPos != 0 && rep.EndPos != 0 && rep.StartPos >= rep.EndPos {
    41  			panic(fmt.Sprintf("%v: bad StartPos\nStartPos=%v >= EndPos=%v",
    42  				typ, rep.StartPos, rep.EndPos))
    43  		}
    44  		if rep.EndPos > len(rep.Output) {
    45  			panic(fmt.Sprintf("%v: bad EndPos\nEndPos=%v > len(Output)=%v",
    46  				typ, rep.EndPos, len(rep.Output)))
    47  		}
    48  		if rep.SkipPos <= rep.StartPos || rep.SkipPos > rep.EndPos {
    49  			panic(fmt.Sprintf("%v: bad SkipPos\nSkipPos=%v: StartPos=%v EndPos=%v",
    50  				typ, rep.SkipPos, rep.StartPos, rep.EndPos))
    51  		}
    52  		// If we parse from StartPos, we must find the same report.
    53  		rep1 := reporter.ParseFrom(data, rep.StartPos)
    54  		if rep1 == nil || rep1.Title != rep.Title || rep1.StartPos != rep.StartPos {
    55  			title, startPos := "", -1
    56  			if rep1 != nil {
    57  				title, startPos = rep1.Title, rep1.StartPos
    58  			}
    59  			panic(fmt.Sprintf("%v: did not find the same reports at StartPos\n"+
    60  				"StartPos=%v/%v\nTitle0=%q\nTitle1=%q",
    61  				typ, rep.StartPos, startPos, rep.Title, title))
    62  		}
    63  	}
    64  	return res
    65  }
    66  
    67  var fuzzReporters = func() map[string]*Reporter {
    68  	reporters := make(map[string]*Reporter)
    69  	for os := range ctors {
    70  		if os == targets.Windows {
    71  			continue
    72  		}
    73  		target := targets.Get(os, targets.AMD64)
    74  		if target == nil {
    75  			continue
    76  		}
    77  		cfg := &mgrconfig.Config{
    78  			Derived: mgrconfig.Derived{
    79  				SysTarget:  target,
    80  				TargetOS:   os,
    81  				TargetArch: targets.AMD64,
    82  			},
    83  		}
    84  		reporter, err := NewReporter(cfg)
    85  		if err != nil {
    86  			panic(err)
    87  		}
    88  		if _, ok := reporter.impl.(*stub); ok {
    89  			continue
    90  		}
    91  		reporters[os] = reporter
    92  	}
    93  	return reporters
    94  }()