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

     1  // Copyright 2024 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 corpus
     5  
     6  import (
     7  	"sort"
     8  
     9  	"github.com/google/syzkaller/pkg/signal"
    10  )
    11  
    12  func (corpus *Corpus) Minimize(cover bool) {
    13  	corpus.mu.Lock()
    14  	defer corpus.mu.Unlock()
    15  
    16  	inputs := make([]signal.Context, 0, len(corpus.progs))
    17  	for _, inp := range corpus.progs {
    18  		inputs = append(inputs, signal.Context{
    19  			Signal:  inp.Signal,
    20  			Context: inp,
    21  		})
    22  	}
    23  
    24  	// Note: inputs are unsorted (based on map iteration).
    25  	// This gives some intentional non-determinism during minimization.
    26  	// However, we want to give preference to non-squashed inputs,
    27  	// so let's sort by this criteria.
    28  	sort.SliceStable(inputs, func(i, j int) bool {
    29  		firstAny := inputs[i].Context.(*Item).HasAny
    30  		secondAny := inputs[j].Context.(*Item).HasAny
    31  		return !firstAny && secondAny
    32  	})
    33  
    34  	corpus.progs = make(map[string]*Item)
    35  	programsList := &ProgramsList{}
    36  	for _, ctx := range signal.Minimize(inputs) {
    37  		inp := ctx.(*Item)
    38  		corpus.progs[inp.Sig] = inp
    39  		programsList.saveProgram(inp.Prog, inp.Signal)
    40  	}
    41  	corpus.ProgramsList.replace(programsList)
    42  }