How to use randBufLen method of prog Package

Best Syzkaller code snippet using prog.randBufLen

rand.go

Source:rand.go Github

copy

Full Screen

...95 // biasedRand produces: 10, 9, ..., 1, 0,96 // we want: 1, 2, ..., 9, 10, 097 return uint64(maxLen-r.biasedRand(maxLen+1, 10)+1) % (maxLen + 1)98}99func (r *randGen) randBufLen() (n uint64) {100 switch {101 case r.nOutOf(50, 56):102 n = r.rand(256)103 case r.nOutOf(5, 6):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 {...

Full Screen

Full Screen

randBufLen

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(randBufLen())6 }7}8import (9func main() {10 rand.Seed(time.Now().UnixNano())11 for i := 0; i < 10; i++ {12 fmt.Println(randBufLen())13 }14}15import (16func randBufLen() int {17 rand.Seed(time.Now().UnixNano())18 return rand.Intn(100)19}

Full Screen

Full Screen

randBufLen

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 rand.Seed(time.Now().UnixNano())4 fmt.Println(randBufLen())5}6import (7func main() {8 rand.Seed(time.Now().UnixNano())9 fmt.Println(randBufLen())10}11import (12func main() {13 rand.Seed(time.Now().UnixNano())14 fmt.Println(randBufLen())15}16import (17func main() {18 rand.Seed(time.Now().UnixNano())19 fmt.Println(randBufLen())20}21import (22func main() {23 rand.Seed(time.Now().UnixNano())24 fmt.Println(randBufLen())25}26import (27func main() {28 rand.Seed(time.Now().UnixNano())29 fmt.Println(randBufLen())30}31import (32func main() {33 rand.Seed(time.Now().UnixNano())34 fmt.Println(randBufLen())35}36import (37func main() {38 rand.Seed(time.Now().UnixNano())39 fmt.Println(randBufLen())40}41import (

Full Screen

Full Screen

randBufLen

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(prog.randBufLen())4}5import "fmt"6func main() {7 fmt.Println(prog.randBufLen())8}9import "fmt"10func main() {11 fmt.Println(prog.randBufLen())12}13import "fmt"14func main() {15 fmt.Println(prog.randBufLen())16}17import "fmt"18func main() {19 fmt.Println(prog.randBufLen())20}21import "fmt"22func main() {23 fmt.Println(prog.randBufLen())24}25import "fmt"26func main() {27 fmt.Println(prog.randBufLen())28}29import "fmt"30func main() {31 fmt.Println(prog.randBufLen())32}33import "fmt"34func main() {35 fmt.Println(prog.randBufLen())36}37import "fmt"38func main() {39 fmt.Println(prog.randBufLen())40}41import "fmt"42func main() {43 fmt.Println(prog.randBufLen())44}45import "fmt"46func main() {47 fmt.Println(prog.randBufLen())48}49import "fmt"50func main() {51 fmt.Println(prog.randBufLen())52}

Full Screen

Full Screen

randBufLen

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(prog.randBufLen())4}5import "fmt"6func main() {7 fmt.Println(prog.randBuf())8}

Full Screen

Full Screen

randBufLen

Using AI Code Generation

copy

Full Screen

1import (2type prog struct {3}4func (p prog) randBufLen() int {5 rand.Seed(time.Now().UnixNano())6 return rand.Intn(100)7}8func main() {9 p := prog{1, "test"}10 fmt.Println(p.randBufLen())11 fmt.Println(p.randBufLen())12 fmt.Println(p.randBufLen())13}14import "fmt"15func (i *int) inc() {16}17func main() {18 i.inc()19 fmt.Println(i)20}21import "fmt"22func (i int) inc() {23}24func main() {25 i.inc()26 fmt.Println(i)

Full Screen

Full Screen

randBufLen

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 p.randBufLen()4 fmt.Println(p.bufLen)5}6import "fmt"7func main() {8 p.randBufLen()9 fmt.Println(p.bufLen)10}11import "fmt"12func main() {13 p.randBufLen()14 fmt.Println(p.bufLen)15}16import "fmt"17func main() {18 p.randBufLen()19 fmt.Println(p.bufLen)20}21import "fmt"22func main() {23 p.randBufLen()24 fmt.Println(p.bufLen)25}26import "fmt"27func main() {28 p.randBufLen()29 fmt.Println(p.bufLen)30}31import "fmt"32func main() {33 p.randBufLen()34 fmt.Println(p.bufLen)35}36import "fmt"37func main() {38 p.randBufLen()39 fmt.Println(p.bufLen)40}41import "fmt"42func main() {43 p.randBufLen()44 fmt.Println(p.bufLen)45}46import "fmt"47func main() {48 p.randBufLen()49 fmt.Println(p.bufLen)50}51import "fmt"

Full Screen

Full Screen

randBufLen

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println("Hello, World!")4 p.randBufLen()5}6import "fmt"7type prog struct {8}9func (p *prog) randBufLen() {10 fmt.Println("Hello, World!")11}12import "fmt"13type prog struct {14}15func (p *prog) RandBufLen() {16 fmt.Println("Hello, World!")17}18func main() {19 fmt.Println("Hello, World!")20 p.RandBufLen()21}

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