diff options
| author | Baozeng Ding <baozeng.dbz@alibaba-inc.com> | 2016-08-08 21:32:48 +0800 |
|---|---|---|
| committer | Baozeng Ding <baozeng.dbz@alibaba-inc.com> | 2016-08-10 13:43:15 +0800 |
| commit | 7db2edcb3364ef5415e992a18c02c73bba6cdd29 (patch) | |
| tree | cec8efa9f607da32d04b57f31da78b61e9e52ac4 | |
| parent | 39350d876d19ab0937511d969844fb9a559dfd8d (diff) | |
sys/sysgen/prog: support ranged int
This commit supports inclusive ranged int, like foo int32[-10~10], which will
generate random integer between -10 and 10. In future we will support more than
one range, like int32[0, -5~10, 50, 100~200]
| -rw-r--r-- | prog/prio.go | 3 | ||||
| -rw-r--r-- | prog/rand.go | 8 | ||||
| -rw-r--r-- | sys/decl.go | 5 | ||||
| -rw-r--r-- | sysgen/parser.go | 5 | ||||
| -rw-r--r-- | sysgen/sysgen.go | 19 |
5 files changed, 31 insertions, 9 deletions
diff --git a/prog/prio.go b/prog/prio.go index 56c50b454..4d905feda 100644 --- a/prog/prio.go +++ b/prog/prio.go @@ -1,4 +1,4 @@ -// Copyright 2015 syzkaller project authors. All rights reserved. +// Copyright 2015/2016 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 prog @@ -90,6 +90,7 @@ func calcStaticPriorities() [][]float32 { case sys.IntType: switch a.Kind { case sys.IntPlain: + case sys.IntRange: case sys.IntSignalno: noteUsage(1.0, "signalno") case sys.IntInaddr: diff --git a/prog/rand.go b/prog/rand.go index e409fe54c..4a41ff5ab 100644 --- a/prog/rand.go +++ b/prog/rand.go @@ -1,4 +1,4 @@ -// Copyright 2015 syzkaller project authors. All rights reserved. +// Copyright 2015/2016 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 prog @@ -71,6 +71,10 @@ func (r *randGen) randInt() uintptr { return v } +func (r *randGen) randRangeInt(begin int64, end int64) uintptr { + return uintptr(begin + r.Int63n(end - begin + 1)) +} + // biasedRand returns a random int in range [0..n), // probability of n-1 is k times higher than probability of 0. func (r *randGen) biasedRand(n, k int) int { @@ -744,6 +748,8 @@ func (r *randGen) generateArg(s *state, typ sys.Type, dir ArgDir, sizes map[stri v = uintptr(r.inaddr(s)) case sys.IntInport: v = uintptr(r.inport(s)) + case sys.IntRange: + v = r.randRangeInt(a.RangeBegin, a.RangeEnd) } return constArg(v), nil, nil case sys.FilenameType: diff --git a/sys/decl.go b/sys/decl.go index 8115a1d6c..4c85b9b53 100644 --- a/sys/decl.go +++ b/sys/decl.go @@ -1,4 +1,4 @@ -// Copyright 2015 syzkaller project authors. All rights reserved. +// Copyright 2015/2016 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 sys @@ -385,12 +385,15 @@ const ( IntSignalno IntInaddr IntInport + IntRange ) type IntType struct { TypeCommon TypeSize uintptr Kind IntKind + RangeBegin int64 + RangeEnd int64 } func (t IntType) Size() uintptr { diff --git a/sysgen/parser.go b/sysgen/parser.go index 7d322eec4..c0feca7ac 100644 --- a/sysgen/parser.go +++ b/sysgen/parser.go @@ -1,4 +1,4 @@ -// Copyright 2015 syzkaller project authors. All rights reserved. +// Copyright 2015/2016 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 main @@ -81,7 +81,8 @@ func (p *Parser) Ident() string { (p.s[p.i] >= 'a' && p.s[p.i] <= 'z' || p.s[p.i] >= 'A' && p.s[p.i] <= 'Z' || p.s[p.i] >= '0' && p.s[p.i] <= '9' || - p.s[p.i] == '_' || p.s[p.i] == '$') { // $ is for n-way syscalls (like ptrace$peek) + p.s[p.i] == '_' || p.s[p.i] == '$' || // $ is for n-way syscalls (like ptrace$peek) + p.s[p.i] == '-' || p.s[p.i] == '~') { // ~ is for ranged int (like int32[-3~10]) p.i++ } if start == p.i { diff --git a/sysgen/sysgen.go b/sysgen/sysgen.go index 824c8587d..9628e3cae 100644 --- a/sysgen/sysgen.go +++ b/sysgen/sysgen.go @@ -1,4 +1,4 @@ -// Copyright 2015 syzkaller project authors. All rights reserved. +// Copyright 2015/2016 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 main @@ -288,10 +288,21 @@ func generateArg(name, typ string, a []string, structs map[string]Struct, unname } fmt.Fprintf(out, "PtrType{%v, Dir: %v, Type: StrConstType{%v, Val: \"%v\"}}", common(), fmtDir("in"), common(), a[0]+"\\x00") case "int8", "int16", "int32", "int64", "intptr": - if want := 0; len(a) != want { - failf("wrong number of arguments for %v arg %v, want %v, got %v", typ, name, want, len(a)) + if len(a) == 1 { + s := a[0] + var lo, hi int64 + if _, err := fmt.Sscanf(s, "%d~%d", &lo, &hi); err != nil { + failf("failed to parse int range: %v (%v)", s, err) + } + if lo >= hi { + failf("bad int range: %v", s) + } + fmt.Fprintf(out, "IntType{%v, TypeSize: %v, Kind: IntRange, RangeBegin: %v, RangeEnd: %v}", common(), typeToSize(typ), lo, hi) + } else if len(a) == 0 { + fmt.Fprintf(out, "IntType{%v, TypeSize: %v}", common(), typeToSize(typ)) + } else { + failf("wrong number of arguments for %v arg %v, want 0 or 1, got %v", typ, name, len(a)) } - fmt.Fprintf(out, "IntType{%v, TypeSize: %v}", common(), typeToSize(typ)) case "signalno": if want := 0; len(a) != want { failf("wrong number of arguments for %v arg %v, want %v, got %v", typ, name, want, len(a)) |
