diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2025-01-17 10:28:16 +0100 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2025-01-17 10:02:07 +0000 |
| commit | 5d04aae8969f6c72318ce0a4cde4f027766b1a55 (patch) | |
| tree | 8f1b632847c431407090f0fe1335522ff2195a37 /prog | |
| parent | f9e07a6e597b68d3397864e6ee4550f9065c3518 (diff) | |
all: use min/max functions
They are shorter, more readable, and don't require temp vars.
Diffstat (limited to 'prog')
| -rw-r--r-- | prog/alloc.go | 10 | ||||
| -rw-r--r-- | prog/collide.go | 16 | ||||
| -rw-r--r-- | prog/collide_test.go | 6 | ||||
| -rw-r--r-- | prog/decodeexec.go | 4 | ||||
| -rw-r--r-- | prog/encoding.go | 8 | ||||
| -rw-r--r-- | prog/export_test.go | 6 | ||||
| -rw-r--r-- | prog/heatmap.go | 5 | ||||
| -rw-r--r-- | prog/heatmap_test.go | 5 | ||||
| -rw-r--r-- | prog/hints.go | 10 | ||||
| -rw-r--r-- | prog/mutation.go | 27 | ||||
| -rw-r--r-- | prog/prio.go | 8 | ||||
| -rw-r--r-- | prog/prog.go | 4 | ||||
| -rw-r--r-- | prog/rand.go | 6 | ||||
| -rw-r--r-- | prog/rotation.go | 13 |
14 files changed, 29 insertions, 99 deletions
diff --git a/prog/alloc.go b/prog/alloc.go index a4b7b7f1f..0af8c5bde 100644 --- a/prog/alloc.go +++ b/prog/alloc.go @@ -151,15 +151,9 @@ func (va *vmaAlloc) alloc(r *randGen, size uint64) uint64 { } else { page = va.used[r.rand(len(va.used))] if size > 1 && r.bin() { - off := r.rand(int(size)) - if off > page { - off = page - } - page -= off - } - if page+size > va.numPages { - page = va.numPages - size + page -= min(r.rand(int(size)), page) } + page = min(page, va.numPages-size) } if page >= va.numPages || size > va.numPages || page+size > va.numPages { panic(fmt.Sprintf("vmaAlloc: bad page=%v size=%v numPages=%v", page, size, va.numPages)) diff --git a/prog/collide.go b/prog/collide.go index 67cca6ecb..3c8a076a1 100644 --- a/prog/collide.go +++ b/prog/collide.go @@ -105,18 +105,10 @@ func DupCallCollide(origProg *Prog, rand *rand.Rand) (*Prog, error) { // For 1-call programs the behavior is similar to DoubleExecCollide. return nil, fmt.Errorf("the prog is too small for the transformation") } - // By default let's duplicate 1/3 calls in the original program. - insert := len(origProg.Calls) / 3 - if insert == 0 { - // .. but always at least one. - insert = 1 - } - if insert > maxAsyncPerProg { - insert = maxAsyncPerProg - } - if insert+len(origProg.Calls) > MaxCalls { - insert = MaxCalls - len(origProg.Calls) - } + // By default let's duplicate 1/3 calls in the original program (but at least one). + insert := max(len(origProg.Calls)/3, 1) + insert = min(insert, maxAsyncPerProg) + insert = min(insert, MaxCalls-len(origProg.Calls)) if insert == 0 { return nil, fmt.Errorf("no calls could be duplicated") } diff --git a/prog/collide_test.go b/prog/collide_test.go index 54ac156ab..01c860a2b 100644 --- a/prog/collide_test.go +++ b/prog/collide_test.go @@ -182,10 +182,8 @@ dup(r2) }, } _, rs, iters := initTest(t) - if iters > 100 { - // Let's save resources -- we don't need that many for these small tests. - iters = 100 - } + // Let's save resources -- we don't need that many for these small tests. + iters = min(iters, 100) r := rand.New(rs) for _, test := range tests { target, err := GetTarget(test.os, test.arch) diff --git a/prog/decodeexec.go b/prog/decodeexec.go index 81c70cb2b..e6921c314 100644 --- a/prog/decodeexec.go +++ b/prog/decodeexec.go @@ -293,9 +293,7 @@ func (dec *execDecoder) commitCall() { dec.numVars = dec.call.Index + 1 } for _, copyout := range dec.call.Copyout { - if dec.numVars < copyout.Index+1 { - dec.numVars = copyout.Index + 1 - } + dec.numVars = max(dec.numVars, copyout.Index+1) } dec.calls = append(dec.calls, dec.call) dec.call = ExecCall{} diff --git a/prog/encoding.go b/prog/encoding.go index b614e247e..41a03d9fd 100644 --- a/prog/encoding.go +++ b/prog/encoding.go @@ -882,12 +882,8 @@ func (p *parser) parseAddr() (uint64, uint64, error) { } if !p.unsafe { maxMem := target.NumPages * target.PageSize - if vmaSize > maxMem { - vmaSize = maxMem - } - if addr > maxMem-vmaSize { - addr = maxMem - vmaSize - } + vmaSize = min(vmaSize, maxMem) + addr = min(addr, maxMem-vmaSize) } } p.Parse(')') diff --git a/prog/export_test.go b/prog/export_test.go index 54992dba1..0b1b8c599 100644 --- a/prog/export_test.go +++ b/prog/export_test.go @@ -47,11 +47,7 @@ func testEachTarget(t *testing.T, fn func(t *testing.T, target *Target)) { func testEachTargetRandom(t *testing.T, fn func(t *testing.T, target *Target, rs rand.Source, iters int)) { t.Parallel() targets := AllTargets() - iters := testutil.IterCount() - iters /= len(targets) - if iters < 3 { - iters = 3 - } + iters := max(testutil.IterCount()/len(targets), 3) rs0 := testutil.RandSource(t) for _, target := range targets { target := target diff --git a/prog/heatmap.go b/prog/heatmap.go index 2bfbceacc..5063d51ea 100644 --- a/prog/heatmap.go +++ b/prog/heatmap.go @@ -44,10 +44,7 @@ func (hm *GenericHeatmap) NumMutations() int { // + up to 4 mutations at random so that even small images can get more than one. n += hm.r.Intn(5) // But don't do too many as it will most likely corrupt the image. - if max := 10; n > max { - n = max - } - return n + return min(n, 10) } func (hm *GenericHeatmap) ChooseLocation() int { diff --git a/prog/heatmap_test.go b/prog/heatmap_test.go index 62a0c45e5..66f3d5e68 100644 --- a/prog/heatmap_test.go +++ b/prog/heatmap_test.go @@ -94,10 +94,7 @@ func (hm *GenericHeatmap) debugPrint(t *testing.T, data []byte, regions []region // Print data. t.Logf("data: len = %d", len(data)) for j := 0; j < len(data); j += granularity { - end := j + granularity - if end > len(data) { - end = len(data) - } + end := min(j+granularity, len(data)) t.Logf("%8d: %x", j*granularity, data[j:end]) } t.Log("\n") diff --git a/prog/hints.go b/prog/hints.go index a02d2d123..90ebab179 100644 --- a/prog/hints.go +++ b/prog/hints.go @@ -208,10 +208,7 @@ replacerLoop: func checkDataArg(arg *DataArg, compMap CompMap, exec func() bool) { bytes := make([]byte, 8) data := arg.Data() - size := len(data) - if size > maxDataLength { - size = maxDataLength - } + size := min(len(data), maxDataLength) for i := 0; i < size; i++ { original := make([]byte, 8) copy(original, data[i:]) @@ -307,10 +304,7 @@ func shrinkExpand(v uint64, compMap CompMap, bitsize uint64, image bool) []uint6 mutant = v & ((1 << size) - 1) } else { width = -iwidth - size = uint64(width) * 8 - if size > bitsize { - size = bitsize - } + size = min(uint64(width)*8, bitsize) if v&(1<<(size-1)) == 0 { continue } diff --git a/prog/mutation.go b/prog/mutation.go index c6cd3c7cf..eb9e8285d 100644 --- a/prog/mutation.go +++ b/prog/mutation.go @@ -60,9 +60,7 @@ func (p *Prog) MutateWithOpts(rs rand.Source, ncalls int, ct *ChoiceTable, noMut } totalWeight := opts.weight() r := newRand(p.Target, rs) - if ncalls < len(p.Calls) { - ncalls = len(p.Calls) - } + ncalls = max(ncalls, len(p.Calls)) ctx := &mutator{ p: p, r: r, @@ -454,12 +452,8 @@ func mutateBufferSize(r *randGen, arg *DataArg, minLen, maxLen uint64) { for oldSize := arg.Size(); oldSize == arg.Size(); { arg.size += uint64(r.Intn(33)) - 16 // Cast to int64 to prevent underflows. - if int64(arg.size) < int64(minLen) { - arg.size = minLen - } - if arg.size > maxLen { - arg.size = maxLen - } + arg.size = uint64(max(int64(arg.size), int64(minLen))) + arg.size = min(arg.size, maxLen) } } @@ -780,10 +774,7 @@ var mutateDataFuncs = [...]func(r *randGen, data []byte, minLen, maxLen uint64) if len(data) == 0 || uint64(len(data)) >= maxLen { return data, false } - n := r.Intn(16) + 1 - if r := int(maxLen) - len(data); n > r { - n = r - } + n := min(r.Intn(16)+1, int(maxLen)-len(data)) pos := r.Intn(len(data)) for i := 0; i < n; i++ { data = append(data, 0) @@ -802,10 +793,7 @@ var mutateDataFuncs = [...]func(r *randGen, data []byte, minLen, maxLen uint64) if len(data) == 0 { return data, false } - n := r.Intn(16) + 1 - if n > len(data) { - n = len(data) - } + n := min(r.Intn(16)+1, len(data)) pos := 0 if n < len(data) { pos = r.Intn(len(data) - n) @@ -825,10 +813,7 @@ var mutateDataFuncs = [...]func(r *randGen, data []byte, minLen, maxLen uint64) return data, false } const max = 256 - n := max - r.biasedRand(max, 10) - if r := int(maxLen) - len(data); n > r { - n = r - } + n := min(max-r.biasedRand(max, 10), int(maxLen)-len(data)) for i := 0; i < n; i++ { data = append(data, byte(r.rand(256))) } diff --git a/prog/prio.go b/prog/prio.go index 142ad36b4..a18008271 100644 --- a/prog/prio.go +++ b/prog/prio.go @@ -7,6 +7,7 @@ import ( "fmt" "math" "math/rand" + "slices" "sort" ) @@ -61,12 +62,7 @@ func (target *Target) calcStaticPriorities() [][]int32 { } // The value assigned for self-priority (call wrt itself) have to be high, but not too high. for c0, pp := range prios { - var max int32 - for _, p := range pp { - if p > max { - max = p - } - } + max := slices.Max(pp) if max == 0 { pp[c0] = 1 } else { diff --git a/prog/prog.go b/prog/prog.go index ec899e35f..c9bd05baa 100644 --- a/prog/prog.go +++ b/prog/prog.go @@ -272,9 +272,7 @@ func (arg *GroupArg) Size() uint64 { offset += typ.AlignAttr - offset%typ.AlignAttr } } - if size < offset { - size = offset - } + size = max(size, offset) } return size case *ArrayType: diff --git a/prog/rand.go b/prog/rand.go index 3e22cb845..957cf7112 100644 --- a/prog/rand.go +++ b/prog/rand.go @@ -323,11 +323,7 @@ func (r *randGen) randFilenameLength() int { off = -off } lens := r.target.SpecialFileLenghts - res := lens[r.Intn(len(lens))] + off - if res < 0 { - res = 0 - } - return res + return max(lens[r.Intn(len(lens))]+off, 0) } func (r *randGen) randFromMap(m map[string]bool) string { diff --git a/prog/rotation.go b/prog/rotation.go index 8ec9f1947..85513e8c8 100644 --- a/prog/rotation.go +++ b/prog/rotation.go @@ -110,17 +110,10 @@ func MakeRotator(target *Target, calls map[*Syscall]bool, rnd *rand.Rand) *Rotat // However, we assume that 200 syscalls is enough for a fuzzing session, // so we cap at that level to make fuzzing more targeted. r.goal = len(calls) * 19 / 20 - if r.goal < 1 { - r.goal = 1 - } - if max := 200; r.goal > max { - r.goal = max - } + r.goal = max(r.goal, 1) + r.goal = min(r.goal, 200) // How many syscalls that don't use any resources we want to add? - r.nresourceless = r.goal * len(r.resourceless) / len(calls) - if r.nresourceless < 1 { - r.nresourceless = 1 - } + r.nresourceless = max(1, r.goal*len(r.resourceless)/len(calls)) return r } |
