aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--pkg/csource/csource_test.go2
-rw-r--r--pkg/fuzzer/job.go2
-rw-r--r--pkg/repro/repro.go2
-rw-r--r--prog/encoding_test.go2
-rw-r--r--prog/expr_test.go2
-rw-r--r--prog/minimization.go76
-rw-r--r--prog/minimization_test.go6
-rw-r--r--prog/prog_test.go2
-rw-r--r--prog/rand_test.go2
9 files changed, 56 insertions, 40 deletions
diff --git a/pkg/csource/csource_test.go b/pkg/csource/csource_test.go
index 86f18af39..7d67927a3 100644
--- a/pkg/csource/csource_test.go
+++ b/pkg/csource/csource_test.go
@@ -91,7 +91,7 @@ func testTarget(t *testing.T, target *prog.Target, full bool) {
opts = allOptionsSingle(target.OS)
opts = append(opts, ExecutorOpts)
} else {
- minimized, _ := prog.Minimize(syzProg, -1, false, func(p *prog.Prog, call int) bool {
+ minimized, _ := prog.Minimize(syzProg, -1, prog.MinimizeParams{}, func(p *prog.Prog, call int) bool {
return len(p.Calls) == len(syzProg.Calls)
})
p.Calls = append(p.Calls, minimized.Calls...)
diff --git a/pkg/fuzzer/job.go b/pkg/fuzzer/job.go
index dd3dc4a1a..00c7c034b 100644
--- a/pkg/fuzzer/job.go
+++ b/pkg/fuzzer/job.go
@@ -194,7 +194,7 @@ func (job *triageJob) deflake(exec func(*queue.Request, ProgTypes) *queue.Result
func (job *triageJob) minimize(newSignal signal.Signal) (stop bool) {
const minimizeAttempts = 3
- job.p, job.call = prog.Minimize(job.p, job.call, false,
+ job.p, job.call = prog.Minimize(job.p, job.call, prog.MinimizeParams{},
func(p1 *prog.Prog, call1 int) bool {
if stop {
return false
diff --git a/pkg/repro/repro.go b/pkg/repro/repro.go
index 68d67f11b..67fac3811 100644
--- a/pkg/repro/repro.go
+++ b/pkg/repro/repro.go
@@ -412,7 +412,7 @@ func (ctx *context) minimizeProg(res *Result) (*Result, error) {
ctx.stats.MinimizeProgTime = time.Since(start)
}()
- res.Prog, _ = prog.Minimize(res.Prog, -1, true,
+ res.Prog, _ = prog.Minimize(res.Prog, -1, prog.MinimizeParams{Light: true},
func(p1 *prog.Prog, callIndex int) bool {
crashed, err := ctx.testProg(p1, res.Duration, res.Opts)
if err != nil {
diff --git a/prog/encoding_test.go b/prog/encoding_test.go
index 64e10ef0f..24c479948 100644
--- a/prog/encoding_test.go
+++ b/prog/encoding_test.go
@@ -408,7 +408,7 @@ func TestSerializeDeserializeRandom(t *testing.T) {
if _, _, ok := testSerializeDeserialize(t, p0); ok {
continue
}
- p0, _ = Minimize(p0, -1, false, func(p1 *Prog, _ int) bool {
+ p0, _ = Minimize(p0, -1, MinimizeParams{}, func(p1 *Prog, _ int) bool {
_, _, ok := testSerializeDeserialize(t, p1)
return !ok
})
diff --git a/prog/expr_test.go b/prog/expr_test.go
index 8188436e1..bdb4201dd 100644
--- a/prog/expr_test.go
+++ b/prog/expr_test.go
@@ -186,7 +186,7 @@ func TestConditionalMinimize(t *testing.T) {
assert.NoError(tt, err)
p, err := target.Deserialize([]byte(test.input), Strict)
assert.NoError(tt, err)
- p1, _ := Minimize(p, 0, false, test.pred)
+ p1, _ := Minimize(p, 0, MinimizeParams{}, test.pred)
res := p1.Serialize()
assert.Equal(tt, test.output, strings.TrimSpace(string(res)))
})
diff --git a/prog/minimization.go b/prog/minimization.go
index 1760d180f..abd0a4a3d 100644
--- a/prog/minimization.go
+++ b/prog/minimization.go
@@ -9,11 +9,25 @@ import (
"reflect"
)
+type MinimizeParams struct {
+ // CallIndex was intentionally not included in this struct, since its
+ // default value should be -1, while the default value of 0 would introduce a bug.
+
+ // If RemoveCallsOnly is set to true, Minimize() focuses only on removing whole calls.
+ RemoveCallsOnly bool
+
+ // Light speeds up the minimization by
+ // 1. Not removing array elements one by one.
+ // 2. Not bisecting blobs too much.
+ // 3. Not minimizing integer values.
+ Light bool
+}
+
// Minimize minimizes program p into an equivalent program using the equivalence
// predicate pred. It iteratively generates simpler programs and asks pred
// whether it is equal to the original program or not. If it is equivalent then
// the simplification attempt is committed and the process continues.
-func Minimize(p0 *Prog, callIndex0 int, crash bool, pred0 func(*Prog, int) bool) (*Prog, int) {
+func Minimize(p0 *Prog, callIndex0 int, params MinimizeParams, pred0 func(*Prog, int) bool) (*Prog, int) {
pred := func(p *Prog, callIndex int) bool {
p.sanitizeFix()
p.debugValidate()
@@ -28,33 +42,35 @@ func Minimize(p0 *Prog, callIndex0 int, crash bool, pred0 func(*Prog, int) bool)
}
// Try to remove all calls except the last one one-by-one.
- p0, callIndex0 = removeCalls(p0, callIndex0, crash, pred)
+ p0, callIndex0 = removeCalls(p0, callIndex0, pred)
- // Try to reset all call props to their default values.
- p0 = resetCallProps(p0, callIndex0, pred)
+ if !params.RemoveCallsOnly {
+ // Try to reset all call props to their default values.
+ p0 = resetCallProps(p0, callIndex0, pred)
- // Try to minimize individual calls.
- for i := 0; i < len(p0.Calls); i++ {
- if p0.Calls[i].Meta.Attrs.NoMinimize {
- continue
- }
- ctx := &minimizeArgsCtx{
- target: p0.Target,
- p0: &p0,
- callIndex0: callIndex0,
- crash: crash,
- pred: pred,
- triedPaths: make(map[string]bool),
- }
- again:
- ctx.p = p0.Clone()
- ctx.call = ctx.p.Calls[i]
- for j, field := range ctx.call.Meta.Args {
- if ctx.do(ctx.call.Args[j], field.Name, "") {
- goto again
+ // Try to minimize individual calls.
+ for i := 0; i < len(p0.Calls); i++ {
+ if p0.Calls[i].Meta.Attrs.NoMinimize {
+ continue
+ }
+ ctx := &minimizeArgsCtx{
+ target: p0.Target,
+ p0: &p0,
+ callIndex0: callIndex0,
+ params: params,
+ pred: pred,
+ triedPaths: make(map[string]bool),
+ }
+ again:
+ ctx.p = p0.Clone()
+ ctx.call = ctx.p.Calls[i]
+ for j, field := range ctx.call.Meta.Args {
+ if ctx.do(ctx.call.Args[j], field.Name, "") {
+ goto again
+ }
}
+ p0 = minimizeCallProps(p0, i, callIndex0, pred)
}
- p0 = minimizeCallProps(p0, i, callIndex0, pred)
}
if callIndex0 != -1 {
@@ -66,7 +82,7 @@ func Minimize(p0 *Prog, callIndex0 int, crash bool, pred0 func(*Prog, int) bool)
return p0, callIndex0
}
-func removeCalls(p0 *Prog, callIndex0 int, crash bool, pred func(*Prog, int) bool) (*Prog, int) {
+func removeCalls(p0 *Prog, callIndex0 int, pred func(*Prog, int) bool) (*Prog, int) {
if callIndex0 >= 0 && callIndex0+2 < len(p0.Calls) {
// It's frequently the case that all subsequent calls were not necessary.
// Try to drop them all at once.
@@ -153,7 +169,7 @@ type minimizeArgsCtx struct {
p *Prog
call *Call
callIndex0 int
- crash bool
+ params MinimizeParams
pred func(*Prog, int) bool
triedPaths map[string]bool
}
@@ -225,7 +241,7 @@ func (typ *ArrayType) minimize(ctx *minimizeArgsCtx, arg Arg, path string) bool
elem := a.Inner[i]
elemPath := fmt.Sprintf("%v-%v", path, i)
// Try to remove individual elements one-by-one.
- if !ctx.crash && !ctx.triedPaths[elemPath] &&
+ if !ctx.params.Light && !ctx.triedPaths[elemPath] &&
(typ.Kind == ArrayRandLen ||
typ.Kind == ArrayRangeLen && uint64(len(a.Inner)) > typ.RangeBegin) {
ctx.triedPaths[elemPath] = true
@@ -268,7 +284,7 @@ func (typ *ProcType) minimize(ctx *minimizeArgsCtx, arg Arg, path string) bool {
func minimizeInt(ctx *minimizeArgsCtx, arg Arg, path string) bool {
// TODO: try to reset bits in ints
// TODO: try to set separate flags
- if ctx.crash {
+ if ctx.params.Light {
return false
}
a := arg.(*ConstArg)
@@ -296,7 +312,7 @@ func minimizeInt(ctx *minimizeArgsCtx, arg Arg, path string) bool {
}
func (typ *ResourceType) minimize(ctx *minimizeArgsCtx, arg Arg, path string) bool {
- if ctx.crash {
+ if ctx.params.Light {
return false
}
a := arg.(*ResultArg)
@@ -340,7 +356,7 @@ func (typ *BufferType) minimize(ctx *minimizeArgsCtx, arg Arg, path string) bool
ctx.target.assignSizesCall(ctx.call)
}
step /= 2
- if ctx.crash {
+ if ctx.params.Light {
break
}
}
diff --git a/prog/minimization_test.go b/prog/minimization_test.go
index 9b7652908..e0736206a 100644
--- a/prog/minimization_test.go
+++ b/prog/minimization_test.go
@@ -251,7 +251,7 @@ func TestMinimize(t *testing.T) {
if err != nil {
t.Fatalf("failed to deserialize original program #%v: %v", ti, err)
}
- p1, ci := Minimize(p, test.callIndex, false, test.pred)
+ p1, ci := Minimize(p, test.callIndex, MinimizeParams{}, test.pred)
res := p1.Serialize()
if string(res) != test.result {
t.Fatalf("minimization produced wrong result #%v\norig:\n%v\nexpect:\n%v\ngot:\n%v",
@@ -273,7 +273,7 @@ func TestMinimizeRandom(t *testing.T) {
for _, crash := range []bool{false, true} {
p := target.Generate(rs, 5, ct)
copyP := p.Clone()
- minP, _ := Minimize(p, len(p.Calls)-1, crash, func(p1 *Prog, callIndex int) bool {
+ minP, _ := Minimize(p, len(p.Calls)-1, MinimizeParams{Light: crash}, func(p1 *Prog, callIndex int) bool {
if r.Intn(2) == 0 {
return false
}
@@ -296,7 +296,7 @@ func TestMinimizeCallIndex(t *testing.T) {
for i := 0; i < iters; i++ {
p := target.Generate(rs, 5, ct)
ci := r.Intn(len(p.Calls))
- p1, ci1 := Minimize(p, ci, r.Intn(2) == 0, func(p1 *Prog, callIndex int) bool {
+ p1, ci1 := Minimize(p, ci, MinimizeParams{Light: r.Intn(2) == 0}, func(p1 *Prog, callIndex int) bool {
return r.Intn(2) == 0
})
if ci1 < 0 || ci1 >= len(p1.Calls) || p.Calls[ci].Meta.Name != p1.Calls[ci1].Meta.Name {
diff --git a/prog/prog_test.go b/prog/prog_test.go
index 1a9a2b6ba..e6c7f4a2b 100644
--- a/prog/prog_test.go
+++ b/prog/prog_test.go
@@ -195,7 +195,7 @@ func testCrossTarget(t *testing.T, target *Target, crossTargets []*Target) {
testCrossArchProg(t, p, crossTargets)
p.Mutate(rs, 20, ct, nil, nil)
testCrossArchProg(t, p, crossTargets)
- p, _ = Minimize(p, -1, false, func(*Prog, int) bool {
+ p, _ = Minimize(p, -1, MinimizeParams{}, func(*Prog, int) bool {
return rs.Int63()%2 == 0
})
testCrossArchProg(t, p, crossTargets)
diff --git a/prog/rand_test.go b/prog/rand_test.go
index 00613d42a..53b8ea338 100644
--- a/prog/rand_test.go
+++ b/prog/rand_test.go
@@ -73,7 +73,7 @@ func generateProg(t *testing.T, target *Target, rs rand.Source, ct *ChoiceTable,
})
}
for _, crash := range []bool{false, true} {
- p, _ = Minimize(p, -1, crash, func(*Prog, int) bool {
+ p, _ = Minimize(p, -1, MinimizeParams{Light: crash}, func(*Prog, int) bool {
return rs.Int63()%10 == 0
})
}