How to use randFilenameLength method of prog Package

Best Syzkaller code snippet using prog.randFilenameLength

rand.go

Source:rand.go Github

copy

Full Screen

...274 if r.oneOf(100) {275 // Make file name very long using target.SpecialFileLenghts consts.276 // Add/subtract some small const to account for our file name prefix277 // and potential kernel off-by-one's.278 fileLen := r.randFilenameLength()279 if add := fileLen - len(f); add > 0 {280 f += strings.Repeat(specialFileLenPad, add)281 }282 }283 if !s.files[f] {284 return f285 }286 }287 }288 return r.randFromMap(s.files)289}290func (r *randGen) randFilenameLength() int {291 off := r.biasedRand(10, 5)292 if r.bin() {293 off = -off294 }295 lens := r.target.SpecialFileLenghts296 res := lens[r.Intn(len(lens))] + off297 if res < 0 {298 res = 0299 }300 return res301}302func (r *randGen) randFromMap(m map[string]bool) string {303 files := make([]string, 0, len(m))304 for f := range m {305 files = append(files, f)306 }307 sort.Strings(files)308 return files[r.Intn(len(files))]309}310func (r *randGen) randString(s *state, t *BufferType) []byte {311 if len(t.Values) != 0 {312 return []byte(t.Values[r.Intn(len(t.Values))])313 }314 if len(s.strings) != 0 && r.bin() {315 // Return an existing string.316 // TODO(dvyukov): make s.strings indexed by string SubKind.317 return []byte(r.randFromMap(s.strings))318 }319 punct := []byte{'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '\\',320 '/', ':', '.', ',', '-', '\'', '[', ']', '{', '}'}321 buf := new(bytes.Buffer)322 for r.nOutOf(3, 4) {323 if r.nOutOf(10, 11) {324 buf.Write([]byte{punct[r.Intn(len(punct))]})325 } else {326 buf.Write([]byte{byte(r.Intn(256))})327 }328 }329 if r.oneOf(100) == t.NoZ {330 buf.Write([]byte{0})331 }332 return buf.Bytes()333}334func (r *randGen) allocAddr(s *state, typ Type, dir Dir, size uint64, data Arg) *PointerArg {335 return MakePointerArg(typ, dir, s.ma.alloc(r, size, data.Type().Alignment()), data)336}337func (r *randGen) allocVMA(s *state, typ Type, dir Dir, numPages uint64) *PointerArg {338 page := s.va.alloc(r, numPages)339 return MakeVmaPointerArg(typ, dir, page*r.target.PageSize, numPages*r.target.PageSize)340}341func (r *randGen) createResource(s *state, res *ResourceType, dir Dir) (Arg, []*Call) {342 if !r.inGenerateResource {343 panic("inGenerateResource is not set")344 }345 kind := res.Desc.Name346 // Find calls that produce the necessary resources.347 // TODO: reduce priority of less specialized ctors.348 metas := r.enabledCtors(s, kind)349 // We may have no resources, but still be in createResource due to ANYRES.350 if len(r.target.resourceMap) != 0 && r.oneOf(1000) {351 // Spoof resource subkind.352 var all []string353 for kind1 := range r.target.resourceMap {354 if r.target.isCompatibleResource(res.Desc.Kind[0], kind1) {355 all = append(all, kind1)356 }357 }358 if len(all) == 0 {359 panic(fmt.Sprintf("got no spoof resources for %v in %v/%v",360 kind, r.target.OS, r.target.Arch))361 }362 sort.Strings(all)363 kind1 := all[r.Intn(len(all))]364 metas1 := r.enabledCtors(s, kind1)365 if len(metas1) != 0 {366 // Don't use the resource for which we don't have any ctors.367 // It's fine per-se because below we just return nil in such case.368 // But in TestCreateResource tests we want to ensure that we don't fail369 // to create non-optional resources, and if we spoof a non-optional370 // resource with ctors with a optional resource w/o ctors, then that check will fail.371 kind, metas = kind1, metas1372 }373 }374 if len(metas) == 0 {375 // We may not have any constructors for optional input resources because we don't disable376 // syscalls based on optional inputs resources w/o ctors in TransitivelyEnabledCalls.377 return nil, nil378 }379 // Now we have a set of candidate calls that can create the necessary resource.380 // Generate one of them.381 meta := metas[r.Intn(len(metas))]382 calls := r.generateParticularCall(s, meta)383 s1 := newState(r.target, s.ct, nil)384 s1.analyze(calls[len(calls)-1])385 // Now see if we have what we want.386 var allres []*ResultArg387 for kind1, res1 := range s1.resources {388 if r.target.isCompatibleResource(kind, kind1) {389 allres = append(allres, res1...)390 }391 }392 sort.SliceStable(allres, func(i, j int) bool {393 return allres[i].Type().Name() < allres[j].Type().Name()394 })395 if len(allres) == 0 {396 panic(fmt.Sprintf("failed to create a resource %v (%v) with %v",397 res.Desc.Kind[0], kind, meta.Name))398 }399 arg := MakeResultArg(res, dir, allres[r.Intn(len(allres))], 0)400 return arg, calls401}402func (r *randGen) enabledCtors(s *state, kind string) []*Syscall {403 var metas []*Syscall404 for _, meta := range r.target.resourceCtors[kind] {405 if s.ct.Generatable(meta.ID) {406 metas = append(metas, meta)407 }408 }409 return metas410}411func (r *randGen) generateText(kind TextKind) []byte {412 switch kind {413 case TextTarget:414 if cfg := createTargetIfuzzConfig(r.target); cfg != nil {415 return ifuzz.Generate(cfg, r.Rand)416 }417 fallthrough418 case TextArm64:419 // Just a stub, need something better.420 text := make([]byte, 50)421 for i := range text {422 text[i] = byte(r.Intn(256))423 }424 return text425 default:426 cfg := createIfuzzConfig(kind)427 return ifuzz.Generate(cfg, r.Rand)428 }429}430func (r *randGen) mutateText(kind TextKind, text []byte) []byte {431 switch kind {432 case TextTarget:433 if cfg := createTargetIfuzzConfig(r.target); cfg != nil {434 return ifuzz.Mutate(cfg, r.Rand, text)435 }436 fallthrough437 case TextArm64:438 return mutateData(r, text, 40, 60)439 default:440 cfg := createIfuzzConfig(kind)441 return ifuzz.Mutate(cfg, r.Rand, text)442 }443}444func createTargetIfuzzConfig(target *Target) *ifuzz.Config {445 cfg := &ifuzz.Config{446 Len: 10,447 Priv: false,448 Exec: true,449 MemRegions: []ifuzz.MemRegion{450 {Start: target.DataOffset, Size: target.NumPages * target.PageSize},451 },452 }453 for _, p := range target.SpecialPointers {454 cfg.MemRegions = append(cfg.MemRegions, ifuzz.MemRegion{455 Start: p & ^target.PageSize, Size: p & ^target.PageSize + target.PageSize,456 })457 }458 switch target.Arch {459 case "amd64":460 cfg.Mode = ifuzz.ModeLong64461 cfg.Arch = ifuzz.ArchX86462 case "386":463 cfg.Mode = ifuzz.ModeProt32464 cfg.Arch = ifuzz.ArchX86465 case "ppc64":466 cfg.Mode = ifuzz.ModeLong64467 cfg.Arch = ifuzz.ArchPowerPC468 default:469 return nil470 }471 return cfg472}473func createIfuzzConfig(kind TextKind) *ifuzz.Config {474 cfg := &ifuzz.Config{475 Len: 10,476 Priv: true,477 Exec: true,478 MemRegions: []ifuzz.MemRegion{479 {Start: 0 << 12, Size: 1 << 12},480 {Start: 1 << 12, Size: 1 << 12},481 {Start: 2 << 12, Size: 1 << 12},482 {Start: 3 << 12, Size: 1 << 12},483 {Start: 4 << 12, Size: 1 << 12},484 {Start: 5 << 12, Size: 1 << 12},485 {Start: 6 << 12, Size: 1 << 12},486 {Start: 7 << 12, Size: 1 << 12},487 {Start: 8 << 12, Size: 1 << 12},488 {Start: 9 << 12, Size: 1 << 12},489 {Start: 0xfec00000, Size: 0x100}, // ioapic490 },491 }492 switch kind {493 case TextX86Real:494 cfg.Mode = ifuzz.ModeReal16495 cfg.Arch = ifuzz.ArchX86496 case TextX86bit16:497 cfg.Mode = ifuzz.ModeProt16498 cfg.Arch = ifuzz.ArchX86499 case TextX86bit32:500 cfg.Mode = ifuzz.ModeProt32501 cfg.Arch = ifuzz.ArchX86502 case TextX86bit64:503 cfg.Mode = ifuzz.ModeLong64504 cfg.Arch = ifuzz.ArchX86505 case TextPpc64:506 cfg.Mode = ifuzz.ModeLong64507 cfg.Arch = ifuzz.ArchPowerPC508 default:509 panic("unknown text kind")510 }511 return cfg512}513// nOutOf returns true n out of outOf times.514func (r *randGen) nOutOf(n, outOf int) bool {515 if n <= 0 || n >= outOf {516 panic("bad probability")517 }518 v := r.Intn(outOf)519 return v < n520}521func (r *randGen) generateCall(s *state, p *Prog, insertionPoint int) []*Call {522 biasCall := -1523 if insertionPoint > 0 {524 // Choosing the base call is based on the insertion point of the new calls sequence.525 insertionCall := p.Calls[r.Intn(insertionPoint)].Meta526 if !insertionCall.Attrs.NoGenerate {527 // We must be careful not to bias towards a non-generatable call.528 biasCall = insertionCall.ID529 }530 }531 idx := s.ct.choose(r.Rand, biasCall)532 meta := r.target.Syscalls[idx]533 return r.generateParticularCall(s, meta)534}535func (r *randGen) generateParticularCall(s *state, meta *Syscall) (calls []*Call) {536 if meta.Attrs.Disabled {537 panic(fmt.Sprintf("generating disabled call %v", meta.Name))538 }539 if meta.Attrs.NoGenerate {540 panic(fmt.Sprintf("generating no_generate call: %v", meta.Name))541 }542 c := MakeCall(meta, nil)543 c.Args, calls = r.generateArgs(s, meta.Args, DirIn)544 r.target.assignSizesCall(c)545 return append(calls, c)546}547// GenerateAllSyzProg generates a program that contains all pseudo syz_ calls for testing.548func (target *Target) GenerateAllSyzProg(rs rand.Source) *Prog {549 p := &Prog{550 Target: target,551 }552 r := newRand(target, rs)553 s := newState(target, target.DefaultChoiceTable(), nil)554 handled := make(map[string]bool)555 for _, meta := range target.Syscalls {556 if !strings.HasPrefix(meta.CallName, "syz_") ||557 handled[meta.CallName] ||558 meta.Attrs.Disabled ||559 meta.Attrs.NoGenerate {560 continue561 }562 handled[meta.CallName] = true563 calls := r.generateParticularCall(s, meta)564 for _, c := range calls {565 s.analyze(c)566 p.Calls = append(p.Calls, c)567 }568 }569 if err := p.validate(); err != nil {570 panic(err)571 }572 return p573}574// DataMmapProg creates program that maps data segment.575// Also used for testing as the simplest program.576func (target *Target) DataMmapProg() *Prog {577 return &Prog{578 Target: target,579 Calls: target.MakeDataMmap(),580 }581}582func (r *randGen) generateArgs(s *state, fields []Field, dir Dir) ([]Arg, []*Call) {583 var calls []*Call584 args := make([]Arg, len(fields))585 // Generate all args. Size args have the default value 0 for now.586 for i, field := range fields {587 arg, calls1 := r.generateArg(s, field.Type, field.Dir(dir))588 if arg == nil {589 panic(fmt.Sprintf("generated arg is nil for field '%v', fields: %+v", field.Type.Name(), fields))590 }591 args[i] = arg592 calls = append(calls, calls1...)593 }594 return args, calls595}596func (r *randGen) generateArg(s *state, typ Type, dir Dir) (arg Arg, calls []*Call) {597 return r.generateArgImpl(s, typ, dir, false)598}599func (r *randGen) generateArgImpl(s *state, typ Type, dir Dir, ignoreSpecial bool) (arg Arg, calls []*Call) {600 if dir == DirOut {601 // No need to generate something interesting for output scalar arguments.602 // But we still need to generate the argument itself so that it can be referenced603 // in subsequent calls. For the same reason we do generate pointer/array/struct604 // output arguments (their elements can be referenced in subsequent calls).605 switch typ.(type) {606 case *IntType, *FlagsType, *ConstType, *ProcType, *VmaType, *ResourceType:607 return typ.DefaultArg(dir), nil608 }609 }610 if typ.Optional() && r.oneOf(5) {611 if res, ok := typ.(*ResourceType); ok {612 v := res.Desc.Values[r.Intn(len(res.Desc.Values))]613 return MakeResultArg(typ, dir, nil, v), nil614 }615 return typ.DefaultArg(dir), nil616 }617 // Allow infinite recursion for optional pointers.618 if pt, ok := typ.(*PtrType); ok && typ.Optional() {619 switch pt.Elem.(type) {620 case *StructType, *ArrayType, *UnionType:621 name := pt.Elem.Name()622 r.recDepth[name]++623 defer func() {624 r.recDepth[name]--625 if r.recDepth[name] == 0 {626 delete(r.recDepth, name)627 }628 }()629 if r.recDepth[name] >= 3 {630 return MakeSpecialPointerArg(typ, dir, 0), nil631 }632 }633 }634 if !ignoreSpecial && dir != DirOut {635 switch typ.(type) {636 case *StructType, *UnionType:637 if gen := r.target.SpecialTypes[typ.Name()]; gen != nil {638 return gen(&Gen{r, s}, typ, dir, nil)639 }640 }641 }642 return typ.generate(r, s, dir)643}644func (a *ResourceType) generate(r *randGen, s *state, dir Dir) (arg Arg, calls []*Call) {645 if !r.inGenerateResource {646 // Don't allow recursion for resourceCentric/createResource.647 // That can lead to generation of huge programs and may be very slow648 // (esp. if we are generating some failing attempts in createResource already).649 r.inGenerateResource = true650 defer func() { r.inGenerateResource = false }()651 if r.oneOf(4) {652 arg, calls = r.resourceCentric(s, a, dir)653 if arg != nil {654 return655 }656 }657 if r.oneOf(3) {658 arg, calls = r.createResource(s, a, dir)659 if arg != nil {660 return661 }662 }663 }664 if r.nOutOf(9, 10) {665 arg = r.existingResource(s, a, dir)666 if arg != nil {667 return668 }669 }670 special := a.SpecialValues()671 arg = MakeResultArg(a, dir, nil, special[r.Intn(len(special))])672 return673}674func (a *BufferType) generate(r *randGen, s *state, dir Dir) (arg Arg, calls []*Call) {675 switch a.Kind {676 case BufferBlobRand, BufferBlobRange:677 sz := r.randBufLen()678 if a.Kind == BufferBlobRange {679 sz = r.randRange(a.RangeBegin, a.RangeEnd)680 }681 if dir == DirOut {682 return MakeOutDataArg(a, dir, sz), nil683 }684 data := make([]byte, sz)685 for i := range data {686 data[i] = byte(r.Intn(256))687 }688 return MakeDataArg(a, dir, data), nil689 case BufferString:690 data := r.randString(s, a)691 if dir == DirOut {692 return MakeOutDataArg(a, dir, uint64(len(data))), nil693 }694 return MakeDataArg(a, dir, data), nil695 case BufferFilename:696 if dir == DirOut {697 var sz uint64698 switch {699 case !a.Varlen():700 sz = a.Size()701 case r.nOutOf(1, 3):702 sz = r.rand(100)703 default:704 sz = uint64(r.randFilenameLength())705 }706 return MakeOutDataArg(a, dir, sz), nil707 }708 return MakeDataArg(a, dir, []byte(r.filename(s, a))), nil709 case BufferGlob:710 return MakeDataArg(a, dir, r.randString(s, a)), nil711 case BufferText:712 if dir == DirOut {713 return MakeOutDataArg(a, dir, uint64(r.Intn(100))), nil714 }715 return MakeDataArg(a, dir, r.generateText(a.Text)), nil716 default:717 panic("unknown buffer kind")718 }...

Full Screen

Full Screen

mutation.go

Source:mutation.go Github

copy

Full Screen

...319 }320 a := arg.(*DataArg)321 if a.Dir() == DirOut {322 if t.Kind == BufferFilename && r.oneOf(100) {323 a.size = uint64(r.randFilenameLength())324 } else {325 mutateBufferSize(r, a, minLen, maxLen)326 }327 return328 }329 switch t.Kind {330 case BufferBlobRand, BufferBlobRange:331 data := append([]byte{}, a.Data()...)332 a.data = mutateData(r, data, minLen, maxLen)333 case BufferString:334 if len(t.Values) != 0 {335 a.data = r.randString(s, t)336 } else {337 if t.TypeSize != 0 {...

Full Screen

Full Screen

randFilenameLength

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 rand.Seed(time.Now().UnixNano())4 for i := 0; i < 10; i++ {5 fmt.Println(randFilenameLength())6 }7}8import (9func main() {10 rand.Seed(time.Now().UnixNano())11 for i := 0; i < 10; i++ {12 fmt.Println(randFilenameLength())13 }14}

Full Screen

Full Screen

randFilenameLength

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "prog"3func main() {4 fmt.Println(prog.RandFilenameLength())5}6import "math/rand"7func RandFilenameLength() int {8 return rand.Intn(20)9}10import "math/rand"11func RandFilenameLength() int {12 return rand.Intn(50)13}14import "math/rand"15func RandFilenameLength() int {16 return rand.Intn(100)17}18import "math/rand"19func RandFilenameLength() int {20 return rand.Intn(200)21}22import "math/rand"23func RandFilenameLength() int {24 return rand.Intn(500)25}26import "math/rand"27func RandFilenameLength() int {28 return rand.Intn(1000)29}30import "math/rand"31func RandFilenameLength() int {32 return rand.Intn(2000)33}34import "math/rand"35func RandFilenameLength() int {36 return rand.Intn(5000)37}38import "math/rand"39func RandFilenameLength() int {40 return rand.Intn(10000)41}42import "math/rand"43func RandFilenameLength() int {44 return rand.Intn(20000)45}46import "math/rand"47func RandFilenameLength() int {48 return rand.Intn(50000)49}50import "math/rand"51func RandFilenameLength() int {52 return rand.Intn(100000)53}54import "math/rand"55func RandFilenameLength() int {56 return rand.Intn(200000)57}58import "math/rand"59func RandFilenameLength() int {60 return rand.Intn(500000)61}62import "math/rand"

Full Screen

Full Screen

randFilenameLength

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(prog.randFilenameLength())4}5import "math/rand"6func randFilenameLength() int {7 return rand.Intn(10) + 18}9import "fmt"10import "math/rand"11func main() {12 fmt.Println(randFilenameLength())13}14func randFilenameLength() int {15 return rand.Intn(10) + 116}17David Stancu (author) from Toronto, Canada on September 27, 2019:

Full Screen

Full Screen

randFilenameLength

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 fmt.Println(randFilenameLength())5}6import (7func randFilenameLength() int {8 return rand.Intn(10)9}

Full Screen

Full Screen

randFilenameLength

Using AI Code Generation

copy

Full Screen

1prog := new(prog)2prog.randFilenameLength()3prog := new(prog)4prog.randFilenameLength()5prog := new(prog)6prog.randFilenameLength()7prog := new(prog)8prog.randFilenameLength()9prog := new(prog)10prog.randFilenameLength()11prog := new(prog)12prog.randFilenameLength()13prog := new(prog)14prog.randFilenameLength()15prog := new(prog)16prog.randFilenameLength()17prog := new(prog)18prog.randFilenameLength()19prog := new(prog)20prog.randFilenameLength()21prog := new(prog)22prog.randFilenameLength()23prog := new(prog)24prog.randFilenameLength()25prog := new(prog)26prog.randFilenameLength()27prog := new(prog)28prog.randFilenameLength()29prog := new(prog)30prog.randFilenameLength()31prog := new(prog)32prog.randFilenameLength()33prog := new(prog)

Full Screen

Full Screen

randFilenameLength

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Random filename length is", prog.RandFilenameLength())4}5import (6func main() {7 fmt.Println("Random filename length is", prog.RandFilenameLength())8}

Full Screen

Full Screen

randFilenameLength

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := prog.randFilenameLength()4 fmt.Println(r)5 os.Exit(0)6}7import (8type prog struct {9}10func (p *prog) randFilenameLength() int {11 rand.Seed(time.Now().Unix())12 return rand.Intn(10) + 113}14import (15func main() {16 r := prog.randFilenameLength()17 fmt.Println(r)18 os.Exit(0)19}20import (21func main() {22 r := prog.randFilenameLength()23 fmt.Println(r)24 os.Exit(0)25}26import (27func main() {28 r := prog.randFilenameLength()29 fmt.Println(r)30 os.Exit(0)31}32import (33func main() {34 r := prog.randFilenameLength()35 fmt.Println(r)36 os.Exit(0)37}38import (39func main() {40 r := prog.randFilenameLength()41 fmt.Println(r)42 os.Exit(0)43}44import (45func main() {

Full Screen

Full Screen

randFilenameLength

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(prog.randFilenameLength())4}5func randFilenameLength() int {6}7func RandFilenameLength() int {8}9import "prog"10import (11func main() {12 fmt.Println(prog.RandFilenameLength())13}14import p "prog"15import (16func main() {17 fmt.Println(p.RandFilenameLength())18}19import . "prog"20import (21func main() {22 fmt.Println(RandFilenameLength())

Full Screen

Full Screen

randFilenameLength

Using AI Code Generation

copy

Full Screen

1import (2func randFilenameLength() int {3 rand.Seed(time.Now().UnixNano())4 return rand.Intn(20)5}6func main() {7 fmt.Println(randFilenameLength())8}9import (10func main() {11 fmt.Println(prog.randFilenameLength())12}13import (14func randFilenameLength() int {15 rand.Seed(time.Now().UnixNano())16 return rand.Intn(20)17}18 /usr/lib/go-1.6/src/2 (from $GOROOT)19 /home/andrew/go/src/2 (from $GOPATH)20import (21func randFilenameLength() int {22 rand.Seed(time.Now().UnixNano())

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