diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2024-03-13 18:52:18 +0100 |
|---|---|---|
| committer | Aleksandr Nogikh <nogikh@google.com> | 2024-03-18 10:58:52 +0000 |
| commit | fc090d205d8c3d58f190659a98795d89421b7e6b (patch) | |
| tree | 2b33cca3e6366a1565d70fdce01a51d6e50f6448 /pkg/corpus/minimize.go | |
| parent | d615901c739a765329b688494cee2f8e1b5037cb (diff) | |
pkg/corpus: a separate package for the corpus functionality
pkg/fuzzer and syz-manager have a common corpus functionality that can
be well be unified.
Create a separate pkg/corpus package that would be used by both of them.
It will simplify further work of moving pkg/fuzzer to the host.
Diffstat (limited to 'pkg/corpus/minimize.go')
| -rw-r--r-- | pkg/corpus/minimize.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/pkg/corpus/minimize.go b/pkg/corpus/minimize.go new file mode 100644 index 000000000..b47816ccf --- /dev/null +++ b/pkg/corpus/minimize.go @@ -0,0 +1,41 @@ +// Copyright 2024 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package corpus + +import ( + "sort" + + "github.com/google/syzkaller/pkg/signal" +) + +func (corpus *Corpus) Minimize(cover bool) { + corpus.mu.Lock() + defer corpus.mu.Unlock() + + inputs := make([]signal.Context, 0, len(corpus.progs)) + for _, inp := range corpus.progs { + inputs = append(inputs, signal.Context{ + Signal: inp.Signal, + Context: inp, + }) + } + + // Note: inputs are unsorted (based on map iteration). + // This gives some intentional non-determinism during minimization. + // However, we want to give preference to non-squashed inputs, + // so let's sort by this criteria. + sort.SliceStable(inputs, func(i, j int) bool { + firstAny := inputs[i].Context.(*Item).HasAny + secondAny := inputs[j].Context.(*Item).HasAny + return !firstAny && secondAny + }) + + corpus.progs = make(map[string]*Item) + corpus.ProgramsList = ProgramsList{} + for _, ctx := range signal.Minimize(inputs) { + inp := ctx.(*Item) + corpus.progs[inp.Sig] = inp + corpus.saveProgram(inp.Prog, inp.Signal) + } +} |
