How to use randPageCount method of prog Package

Best Syzkaller code snippet using prog.randPageCount

rand.go

Source:rand.go Github

copy

Full Screen

...104 n = 4 << 10105 }106 return107}108func (r *randGen) randPageCount() (n uint64) {109 switch {110 case r.nOutOf(100, 106):111 n = r.rand(4) + 1112 case r.nOutOf(5, 6):113 n = r.rand(20) + 1114 default:115 n = (r.rand(3) + 1) * 512116 }117 return118}119func (r *randGen) flags(vv []uint64) (v uint64) {120 switch {121 case r.nOutOf(90, 111):122 for stop := false; !stop; stop = r.bin() {123 v |= vv[r.rand(len(vv))]124 }125 case r.nOutOf(10, 21):126 v = vv[r.rand(len(vv))]127 case r.nOutOf(10, 11):128 v = 0129 default:130 v = r.rand64()131 }132 return133}134func (r *randGen) filename(s *state, typ *BufferType) string {135 fn := r.filenameImpl(s)136 if len(fn) != 0 && fn[len(fn)-1] == 0 {137 panic(fmt.Sprintf("zero-terminated filename: %q", fn))138 }139 if !typ.Varlen() {140 size := typ.Size()141 if uint64(len(fn)) < size {142 fn += string(make([]byte, size-uint64(len(fn))))143 }144 fn = fn[:size]145 } else if !typ.NoZ {146 fn += "\x00"147 }148 return fn149}150var specialFiles = []string{"", "/", "."}151func (r *randGen) filenameImpl(s *state) string {152 if r.oneOf(100) {153 return specialFiles[r.Intn(len(specialFiles))]154 }155 if len(s.files) == 0 || r.oneOf(10) {156 // Generate a new name.157 dir := "."158 if r.oneOf(2) && len(s.files) != 0 {159 files := make([]string, 0, len(s.files))160 for f := range s.files {161 files = append(files, f)162 }163 dir = files[r.Intn(len(files))]164 if len(dir) > 0 && dir[len(dir)-1] == 0 {165 dir = dir[:len(dir)-1]166 }167 }168 for i := 0; ; i++ {169 f := fmt.Sprintf("%v/file%v", dir, i)170 if !s.files[f] {171 return f172 }173 }174 }175 files := make([]string, 0, len(s.files))176 for f := range s.files {177 files = append(files, f)178 }179 return files[r.Intn(len(files))]180}181func (r *randGen) randString(s *state, t *BufferType) []byte {182 if len(t.Values) != 0 {183 return []byte(t.Values[r.Intn(len(t.Values))])184 }185 if len(s.strings) != 0 && r.bin() {186 // Return an existing string.187 // TODO(dvyukov): make s.strings indexed by string SubKind.188 strings := make([]string, 0, len(s.strings))189 for s := range s.strings {190 strings = append(strings, s)191 }192 return []byte(strings[r.Intn(len(strings))])193 }194 punct := []byte{'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '\\',195 '/', ':', '.', ',', '-', '\'', '[', ']', '{', '}'}196 buf := new(bytes.Buffer)197 for r.nOutOf(3, 4) {198 switch {199 case r.nOutOf(10, 21):200 dict := r.target.StringDictionary201 if len(dict) != 0 {202 buf.WriteString(dict[r.Intn(len(dict))])203 }204 case r.nOutOf(10, 11):205 buf.Write([]byte{punct[r.Intn(len(punct))]})206 default:207 buf.Write([]byte{byte(r.Intn(256))})208 }209 }210 if r.oneOf(100) == t.NoZ {211 buf.Write([]byte{0})212 }213 return buf.Bytes()214}215func (r *randGen) allocAddr(s *state, typ Type, size uint64, data Arg) *PointerArg {216 return MakePointerArg(typ, s.ma.alloc(r, size), data)217}218func (r *randGen) allocVMA(s *state, typ Type, numPages uint64) *PointerArg {219 page := s.va.alloc(r, numPages)220 return MakeVmaPointerArg(typ, page*r.target.PageSize, numPages*r.target.PageSize)221}222func (r *randGen) createResource(s *state, res *ResourceType) (arg Arg, calls []*Call) {223 if r.inCreateResource {224 special := res.SpecialValues()225 return MakeResultArg(res, nil, special[r.Intn(len(special))]), nil226 }227 r.inCreateResource = true228 defer func() { r.inCreateResource = false }()229 kind := res.Desc.Name230 if r.oneOf(1000) {231 // Spoof resource subkind.232 var all []string233 for kind1 := range r.target.resourceMap {234 if r.target.isCompatibleResource(res.Desc.Kind[0], kind1) {235 all = append(all, kind1)236 }237 }238 kind = all[r.Intn(len(all))]239 }240 // Find calls that produce the necessary resources.241 metas0 := r.target.resourceCtors[kind]242 // TODO: reduce priority of less specialized ctors.243 var metas []*Syscall244 for _, meta := range metas0 {245 if s.ct == nil || s.ct.run[meta.ID] == nil {246 continue247 }248 metas = append(metas, meta)249 }250 if len(metas) == 0 {251 return MakeResultArg(res, nil, res.Default()), nil252 }253 // Now we have a set of candidate calls that can create the necessary resource.254 for i := 0; i < 1e3; i++ {255 // Generate one of them.256 meta := metas[r.Intn(len(metas))]257 calls := r.generateParticularCall(s, meta)258 s1 := newState(r.target, s.ct)259 s1.analyze(calls[len(calls)-1])260 // Now see if we have what we want.261 var allres []Arg262 for kind1, res1 := range s1.resources {263 if r.target.isCompatibleResource(kind, kind1) {264 allres = append(allres, res1...)265 }266 }267 if len(allres) != 0 {268 // Bingo!269 arg := MakeResultArg(res, allres[r.Intn(len(allres))], 0)270 return arg, calls271 }272 // Discard unsuccessful calls.273 // Note: s.ma/va have already noted allocations of the new objects274 // in discarded syscalls, ideally we should recreate state275 // by analyzing the program again.276 for _, c := range calls {277 ForeachArg(c, func(arg Arg, _ *ArgCtx) {278 if a, ok := arg.(*ResultArg); ok && a.Res != nil {279 delete(*a.Res.(ArgUsed).Used(), arg)280 }281 })282 }283 }284 // Generally we can loop several times, e.g. when we choose a call that returns285 // the resource in an array, but then generateArg generated that array of zero length.286 // But we must succeed eventually.287 var ctors []string288 for _, meta := range metas {289 ctors = append(ctors, meta.Name)290 }291 panic(fmt.Sprintf("failed to create a resource %v with %v",292 res.Desc.Kind[0], strings.Join(ctors, ", ")))293}294func (r *randGen) generateText(kind TextKind) []byte {295 switch kind {296 case Text_arm64:297 // Just a stub, need something better.298 text := make([]byte, 50)299 for i := range text {300 text[i] = byte(r.Intn(256))301 }302 return text303 default:304 cfg := createIfuzzConfig(kind)305 return ifuzz.Generate(cfg, r.Rand)306 }307}308func (r *randGen) mutateText(kind TextKind, text []byte) []byte {309 switch kind {310 case Text_arm64:311 return mutateData(r, text, 40, 60)312 default:313 cfg := createIfuzzConfig(kind)314 return ifuzz.Mutate(cfg, r.Rand, text)315 }316}317func createIfuzzConfig(kind TextKind) *ifuzz.Config {318 cfg := &ifuzz.Config{319 Len: 10,320 Priv: true,321 Exec: true,322 MemRegions: []ifuzz.MemRegion{323 {0 << 12, 1 << 12},324 {1 << 12, 1 << 12},325 {2 << 12, 1 << 12},326 {3 << 12, 1 << 12},327 {4 << 12, 1 << 12},328 {5 << 12, 1 << 12},329 {6 << 12, 1 << 12},330 {7 << 12, 1 << 12},331 {8 << 12, 1 << 12},332 {9 << 12, 1 << 12},333 {0xfec00000, 0x100}, // ioapic334 },335 }336 switch kind {337 case Text_x86_real:338 cfg.Mode = ifuzz.ModeReal16339 case Text_x86_16:340 cfg.Mode = ifuzz.ModeProt16341 case Text_x86_32:342 cfg.Mode = ifuzz.ModeProt32343 case Text_x86_64:344 cfg.Mode = ifuzz.ModeLong64345 }346 return cfg347}348// nOutOf returns true n out of outOf times.349func (r *randGen) nOutOf(n, outOf int) bool {350 if n <= 0 || n >= outOf {351 panic("bad probability")352 }353 v := r.Intn(outOf)354 return v < n355}356func (r *randGen) generateCall(s *state, p *Prog) []*Call {357 idx := 0358 if s.ct == nil {359 idx = r.Intn(len(r.target.Syscalls))360 } else {361 call := -1362 if len(p.Calls) != 0 {363 call = p.Calls[r.Intn(len(p.Calls))].Meta.ID364 }365 idx = s.ct.Choose(r.Rand, call)366 }367 meta := r.target.Syscalls[idx]368 return r.generateParticularCall(s, meta)369}370func (r *randGen) generateParticularCall(s *state, meta *Syscall) (calls []*Call) {371 c := &Call{372 Meta: meta,373 Ret: MakeReturnArg(meta.Ret),374 }375 c.Args, calls = r.generateArgs(s, meta.Args)376 r.target.assignSizesCall(c)377 calls = append(calls, c)378 for _, c1 := range calls {379 r.target.SanitizeCall(c1)380 }381 return calls382}383// GenerateAllSyzProg generates a program that contains all pseudo syz_ calls for testing.384func (target *Target) GenerateAllSyzProg(rs rand.Source) *Prog {385 p := &Prog{386 Target: target,387 }388 r := newRand(target, rs)389 s := newState(target, nil)390 handled := make(map[string]bool)391 for _, meta := range target.Syscalls {392 if !strings.HasPrefix(meta.CallName, "syz_") || handled[meta.CallName] {393 continue394 }395 handled[meta.CallName] = true396 calls := r.generateParticularCall(s, meta)397 for _, c := range calls {398 s.analyze(c)399 p.Calls = append(p.Calls, c)400 }401 }402 if err := p.validate(); err != nil {403 panic(err)404 }405 return p406}407// GenerateSimpleProg generates the simplest non-empty program for testing408// (e.g. containing a single mmap).409func (target *Target) GenerateSimpleProg() *Prog {410 return &Prog{411 Target: target,412 Calls: []*Call{target.MakeMmap(0, target.PageSize)},413 }414}415func (target *Target) GenerateUberMmapProg() *Prog {416 return &Prog{417 Target: target,418 Calls: []*Call{target.MakeMmap(0, target.NumPages*target.PageSize)},419 }420}421func (r *randGen) generateArgs(s *state, types []Type) ([]Arg, []*Call) {422 var calls []*Call423 args := make([]Arg, len(types))424 // Generate all args. Size args have the default value 0 for now.425 for i, typ := range types {426 arg, calls1 := r.generateArg(s, typ)427 if arg == nil {428 panic(fmt.Sprintf("generated arg is nil for type '%v', types: %+v", typ.Name(), types))429 }430 args[i] = arg431 calls = append(calls, calls1...)432 }433 return args, calls434}435func (r *randGen) generateArg(s *state, typ Type) (arg Arg, calls []*Call) {436 return r.generateArgImpl(s, typ, false)437}438func (r *randGen) generateArgImpl(s *state, typ Type, ignoreSpecial bool) (arg Arg, calls []*Call) {439 if typ.Dir() == DirOut {440 // No need to generate something interesting for output scalar arguments.441 // But we still need to generate the argument itself so that it can be referenced442 // in subsequent calls. For the same reason we do generate pointer/array/struct443 // output arguments (their elements can be referenced in subsequent calls).444 switch typ.(type) {445 case *IntType, *FlagsType, *ConstType, *ProcType,446 *VmaType, *ResourceType:447 return r.target.defaultArg(typ), nil448 }449 }450 if typ.Optional() && r.oneOf(5) {451 return r.target.defaultArg(typ), nil452 }453 // Allow infinite recursion for optional pointers.454 if pt, ok := typ.(*PtrType); ok && typ.Optional() {455 switch pt.Type.(type) {456 case *StructType, *ArrayType, *UnionType:457 name := pt.Type.Name()458 r.recDepth[name]++459 defer func() {460 r.recDepth[name]--461 if r.recDepth[name] == 0 {462 delete(r.recDepth, name)463 }464 }()465 if r.recDepth[name] >= 3 {466 return MakeNullPointerArg(typ), nil467 }468 }469 }470 switch a := typ.(type) {471 case *ResourceType:472 switch {473 case r.nOutOf(1000, 1011):474 // Get an existing resource.475 var allres []Arg476 for name1, res1 := range s.resources {477 if name1 == "iocbptr" {478 continue479 }480 if r.target.isCompatibleResource(a.Desc.Name, name1) ||481 r.oneOf(20) && r.target.isCompatibleResource(a.Desc.Kind[0], name1) {482 allres = append(allres, res1...)483 }484 }485 if len(allres) != 0 {486 arg = MakeResultArg(a, allres[r.Intn(len(allres))], 0)487 } else {488 arg, calls = r.createResource(s, a)489 }490 case r.nOutOf(10, 11):491 // Create a new resource.492 arg, calls = r.createResource(s, a)493 default:494 special := a.SpecialValues()495 arg = MakeResultArg(a, nil, special[r.Intn(len(special))])496 }497 return arg, calls498 case *BufferType:499 switch a.Kind {500 case BufferBlobRand, BufferBlobRange:501 sz := r.randBufLen()502 if a.Kind == BufferBlobRange {503 sz = r.randRange(a.RangeBegin, a.RangeEnd)504 }505 if a.Dir() == DirOut {506 return MakeOutDataArg(a, sz), nil507 }508 data := make([]byte, sz)509 for i := range data {510 data[i] = byte(r.Intn(256))511 }512 return MakeDataArg(a, data), nil513 case BufferString:514 data := r.randString(s, a)515 if a.Dir() == DirOut {516 return MakeOutDataArg(a, uint64(len(data))), nil517 }518 return MakeDataArg(a, data), nil519 case BufferFilename:520 if a.Dir() == DirOut {521 var sz uint64522 switch {523 case !a.Varlen():524 sz = a.Size()525 case r.nOutOf(1, 3):526 sz = r.rand(100)527 case r.nOutOf(1, 2):528 sz = 108 // UNIX_PATH_MAX529 default:530 sz = 4096 // PATH_MAX531 }532 return MakeOutDataArg(a, sz), nil533 }534 return MakeDataArg(a, []byte(r.filename(s, a))), nil535 case BufferText:536 if a.Dir() == DirOut {537 return MakeOutDataArg(a, uint64(r.Intn(100))), nil538 }539 return MakeDataArg(a, r.generateText(a.Text)), nil540 default:541 panic("unknown buffer kind")542 }543 case *VmaType:544 npages := r.randPageCount()545 if a.RangeBegin != 0 || a.RangeEnd != 0 {546 npages = a.RangeBegin + uint64(r.Intn(int(a.RangeEnd-a.RangeBegin+1)))547 }548 arg := r.allocVMA(s, a, npages)549 return arg, nil550 case *FlagsType:551 return MakeConstArg(a, r.flags(a.Vals)), nil552 case *ConstType:553 return MakeConstArg(a, a.Val), nil554 case *IntType:555 v := r.randInt()556 switch a.Kind {557 case IntFileoff:558 switch {...

Full Screen

Full Screen

randPageCount

Using AI Code Generation

copy

Full Screen

1import (2type prog struct {3}4func (p prog) randPageCount() int {5 rand.Seed(time.Now().UnixNano())6 p.pagecount = rand.Intn(1000)7}8func main() {9 p := prog{}10 fmt.Println(p.randPageCount())11}12import (13type prog struct {14}15func (p *prog) randPageCount() int {16 rand.Seed(time.Now().UnixNano())17 p.pagecount = rand.Intn(1000)18}19func main() {20 p := prog{}21 fmt.Println(p.randPageCount())22}23import (24type prog struct {25}26func (p *prog) randPageCount() int {27}28func main() {29 p := prog{}30 fmt.Println(p.randPageCount())31}32import (33type prog struct {34}35func (p prog) randPageCount() int {36}37func main() {38 p := prog{}39 fmt.Println(p.randPageCount())40}

Full Screen

Full Screen

randPageCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 rand.Seed(time.Now().UnixNano())4 fmt.Println("Number of pages read: ", randPageCount())5}6import (7func randPageCount() int {8 return rand.Intn(10) + 19}10import (11func main() {12 rand.Seed(time.Now().UnixNano())13 fmt.Println("Number of pages read: ", randPageCount())14}15import (16func randPageCount() int {17 return rand.Intn(10) + 118}19import (20func main() {21 rand.Seed(time.Now().UnixNano())22 fmt.Println("Number of pages read: ", randPageCount())23}24import (25func randPageCount() int {26 return rand.Intn(10) + 127}28import (29func main() {30 rand.Seed(time.Now().UnixNano())31 fmt.Println("Number of pages read: ", randPageCount())32}33import (34func randPageCount() int {35 return rand.Intn(10) + 136}

Full Screen

Full Screen

randPageCount

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 for i := 0; i < 10; i++ {4 pages = prog.randPageCount()5 fmt.Println(pages)6 }7}8import "math/rand"9import "time"10func init() {11 rand.Seed(time.Now().Unix())12}13func randPageCount() int {14 return rand.Intn(1000)15}16import "fmt"17import "github.com/yourname/yourproject/prog"18func main() {19 for i := 0; i < 10; i++ {20 pages = prog.RandPageCount()21 fmt.Println(pages)22 }23}24import "math/rand"25import "time"26func init() {27 rand.Seed(time.Now().Unix())28}29func RandPageCount() int {30 return rand.Intn(1000)31}

Full Screen

Full Screen

randPageCount

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(p.randPageCount())4}5import "fmt"6func main() {7 fmt.Println(p.randPageCount())8}9import "fmt"10func main() {11 fmt.Println("Hello, World!")12}13import "fmt"14func PrintHello() {15 fmt.Println("Hello, World!")16}17import "fmt"18func main() {19 fmt.Println("Hello, World!")20 prog.PrintHello()21}22 c:\go\src\prog (from $GOROOT)23 C:\Users\Asus\go\src\prog (from $GOPATH)24The reason for this error is that the “prog” package is not available in the main package. To make the “prog” package available in the main package, we need to import it in the main package. We can import the “prog” package in the main package as follows:25import (26func main() {27 fmt.Println("Hello, World!")28 prog.PrintHello()29}

Full Screen

Full Screen

randPageCount

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Random page count: ", prog.randPageCount())4}5import "math/rand"6type prog struct {}7func (p prog) randPageCount() int {8 return rand.Intn(1000)9}10import "fmt"11func main() {12 fmt.Println("Random page count: ", prog.randPageCount())13}14import "math/rand"15type prog struct {}16func (p prog) RandPageCount() int {17 return rand.Intn(1000)18}19import "fmt"20func main() {21 fmt.Println("Random page count: ", prog.RandPageCount())22}23import "fmt"24func main() {25 fmt.Println("Random page count: ", prog.RandPageCount())26}27import "math/rand"28type prog struct {}29func (p prog) randPageCount() int {30 return rand.Intn(1000)31}32import "fmt"33func main() {34 fmt.Println("Random page count: ", prog.RandPageCount())35}36import "math/rand"37type prog struct {}38func (p prog) RandPageCount() int

Full Screen

Full Screen

randPageCount

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 prog1 := new(prog)4 fmt.Println("Page Count: ", prog1.pageCount)5 fmt.Println("Random Page Count: ", prog1.randPageCount())6}7import (8type prog struct {9}10func (p *prog) randPageCount() int {11 return rand.Intn(p.pageCount)12}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful