How to use Generate method of ifuzz Package

Best Syzkaller code snippet using ifuzz.Generate

rand.go

Source:rand.go Github

copy

Full Screen

...163 if r.oneOf(100) {164 return specialFiles[r.Intn(len(specialFiles))]165 }166 if len(s.files) == 0 || r.oneOf(10) {167 // Generate a new name.168 dir := "."169 if r.oneOf(2) && len(s.files) != 0 {170 dir = r.randFromMap(s.files)171 if len(dir) > 0 && dir[len(dir)-1] == 0 {172 dir = dir[:len(dir)-1]173 }174 if r.oneOf(10) && filepath.Clean(dir)[0] != '.' {175 dir += "/.."176 }177 }178 for i := 0; ; i++ {179 f := fmt.Sprintf("%v/file%v", dir, i)180 if !s.files[f] {181 return f182 }183 }184 }185 return r.randFromMap(s.files)186}187func (r *randGen) randFromMap(m map[string]bool) string {188 files := make([]string, 0, len(m))189 for f := range m {190 files = append(files, f)191 }192 sort.Strings(files)193 return files[r.Intn(len(files))]194}195func (r *randGen) randString(s *state, t *BufferType) []byte {196 if len(t.Values) != 0 {197 return []byte(t.Values[r.Intn(len(t.Values))])198 }199 if len(s.strings) != 0 && r.bin() {200 // Return an existing string.201 // TODO(dvyukov): make s.strings indexed by string SubKind.202 return []byte(r.randFromMap(s.strings))203 }204 punct := []byte{'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+', '\\',205 '/', ':', '.', ',', '-', '\'', '[', ']', '{', '}'}206 buf := new(bytes.Buffer)207 for r.nOutOf(3, 4) {208 switch {209 case r.nOutOf(10, 21):210 dict := r.target.StringDictionary211 if len(dict) != 0 {212 buf.WriteString(dict[r.Intn(len(dict))])213 }214 case r.nOutOf(10, 11):215 buf.Write([]byte{punct[r.Intn(len(punct))]})216 default:217 buf.Write([]byte{byte(r.Intn(256))})218 }219 }220 if r.oneOf(100) == t.NoZ {221 buf.Write([]byte{0})222 }223 return buf.Bytes()224}225func (r *randGen) allocAddr(s *state, typ Type, size uint64, data Arg) *PointerArg {226 return MakePointerArg(typ, s.ma.alloc(r, size), data)227}228func (r *randGen) allocVMA(s *state, typ Type, numPages uint64) *PointerArg {229 page := s.va.alloc(r, numPages)230 return MakeVmaPointerArg(typ, page*r.target.PageSize, numPages*r.target.PageSize)231}232func (r *randGen) createResource(s *state, res *ResourceType) (arg Arg, calls []*Call) {233 if r.inCreateResource {234 special := res.SpecialValues()235 return MakeResultArg(res, nil, special[r.Intn(len(special))]), nil236 }237 r.inCreateResource = true238 defer func() { r.inCreateResource = false }()239 kind := res.Desc.Name240 // We may have no resources, but still be in createResource due to ANYRES.241 if len(r.target.resourceMap) != 0 && r.oneOf(1000) {242 // Spoof resource subkind.243 var all []string244 for kind1 := range r.target.resourceMap {245 if r.target.isCompatibleResource(res.Desc.Kind[0], kind1) {246 all = append(all, kind1)247 }248 }249 if len(all) == 0 {250 panic(fmt.Sprintf("got no spoof resources for %v in %v/%v",251 kind, r.target.OS, r.target.Arch))252 }253 sort.Strings(all)254 kind = all[r.Intn(len(all))]255 }256 // Find calls that produce the necessary resources.257 metas0 := r.target.resourceCtors[kind]258 // TODO: reduce priority of less specialized ctors.259 var metas []*Syscall260 for _, meta := range metas0 {261 if s.ct == nil || s.ct.run[meta.ID] == nil {262 continue263 }264 metas = append(metas, meta)265 }266 if len(metas) == 0 {267 return res.DefaultArg(), nil268 }269 // Now we have a set of candidate calls that can create the necessary resource.270 for i := 0; i < 1e3; i++ {271 // Generate one of them.272 meta := metas[r.Intn(len(metas))]273 calls := r.generateParticularCall(s, meta)274 s1 := newState(r.target, s.ct)275 s1.analyze(calls[len(calls)-1])276 // Now see if we have what we want.277 var allres []*ResultArg278 for kind1, res1 := range s1.resources {279 if r.target.isCompatibleResource(kind, kind1) {280 allres = append(allres, res1...)281 }282 }283 if len(allres) != 0 {284 // Bingo!285 arg := MakeResultArg(res, allres[r.Intn(len(allres))], 0)286 return arg, calls287 }288 // Discard unsuccessful calls.289 // Note: s.ma/va have already noted allocations of the new objects290 // in discarded syscalls, ideally we should recreate state291 // by analyzing the program again.292 for _, c := range calls {293 ForeachArg(c, func(arg Arg, _ *ArgCtx) {294 if a, ok := arg.(*ResultArg); ok && a.Res != nil {295 delete(a.Res.uses, a)296 }297 })298 }299 }300 // Generally we can loop several times, e.g. when we choose a call that returns301 // the resource in an array, but then generateArg generated that array of zero length.302 // But we must succeed eventually.303 var ctors []string304 for _, meta := range metas {305 ctors = append(ctors, meta.Name)306 }307 panic(fmt.Sprintf("failed to create a resource %v with %v",308 res.Desc.Kind[0], strings.Join(ctors, ", ")))309}310func (r *randGen) generateText(kind TextKind) []byte {311 switch kind {312 case TextTarget:313 if r.target.Arch == "amd64" || r.target.Arch == "386" {314 cfg := createTargetIfuzzConfig(r.target)315 return ifuzz.Generate(cfg, r.Rand)316 }317 fallthrough318 case TextArm64:319 // Just a stub, need something better.320 text := make([]byte, 50)321 for i := range text {322 text[i] = byte(r.Intn(256))323 }324 return text325 default:326 cfg := createIfuzzConfig(kind)327 return ifuzz.Generate(cfg, r.Rand)328 }329}330func (r *randGen) mutateText(kind TextKind, text []byte) []byte {331 switch kind {332 case TextTarget:333 if r.target.Arch == "amd64" || r.target.Arch == "386" {334 cfg := createTargetIfuzzConfig(r.target)335 return ifuzz.Mutate(cfg, r.Rand, text)336 }337 fallthrough338 case TextArm64:339 return mutateData(r, text, 40, 60)340 default:341 cfg := createIfuzzConfig(kind)342 return ifuzz.Mutate(cfg, r.Rand, text)343 }344}345func createTargetIfuzzConfig(target *Target) *ifuzz.Config {346 cfg := &ifuzz.Config{347 Len: 10,348 Priv: false,349 Exec: true,350 MemRegions: []ifuzz.MemRegion{351 {Start: target.DataOffset, Size: target.NumPages * target.PageSize},352 },353 }354 for _, p := range target.SpecialPointers {355 cfg.MemRegions = append(cfg.MemRegions, ifuzz.MemRegion{356 Start: p & ^target.PageSize, Size: p & ^target.PageSize + target.PageSize,357 })358 }359 switch target.Arch {360 case "amd64":361 cfg.Mode = ifuzz.ModeLong64362 case "386":363 cfg.Mode = ifuzz.ModeProt32364 default:365 panic("unknown text kind")366 }367 return cfg368}369func createIfuzzConfig(kind TextKind) *ifuzz.Config {370 cfg := &ifuzz.Config{371 Len: 10,372 Priv: true,373 Exec: true,374 MemRegions: []ifuzz.MemRegion{375 {Start: 0 << 12, Size: 1 << 12},376 {Start: 1 << 12, Size: 1 << 12},377 {Start: 2 << 12, Size: 1 << 12},378 {Start: 3 << 12, Size: 1 << 12},379 {Start: 4 << 12, Size: 1 << 12},380 {Start: 5 << 12, Size: 1 << 12},381 {Start: 6 << 12, Size: 1 << 12},382 {Start: 7 << 12, Size: 1 << 12},383 {Start: 8 << 12, Size: 1 << 12},384 {Start: 9 << 12, Size: 1 << 12},385 {Start: 0xfec00000, Size: 0x100}, // ioapic386 },387 }388 switch kind {389 case TextX86Real:390 cfg.Mode = ifuzz.ModeReal16391 case TextX86bit16:392 cfg.Mode = ifuzz.ModeProt16393 case TextX86bit32:394 cfg.Mode = ifuzz.ModeProt32395 case TextX86bit64:396 cfg.Mode = ifuzz.ModeLong64397 default:398 panic("unknown text kind")399 }400 return cfg401}402// nOutOf returns true n out of outOf times.403func (r *randGen) nOutOf(n, outOf int) bool {404 if n <= 0 || n >= outOf {405 panic("bad probability")406 }407 v := r.Intn(outOf)408 return v < n409}410func (r *randGen) generateCall(s *state, p *Prog) []*Call {411 idx := 0412 if s.ct == nil {413 idx = r.Intn(len(r.target.Syscalls))414 } else {415 call := -1416 if len(p.Calls) != 0 {417 call = p.Calls[r.Intn(len(p.Calls))].Meta.ID418 }419 idx = s.ct.Choose(r.Rand, call)420 }421 meta := r.target.Syscalls[idx]422 return r.generateParticularCall(s, meta)423}424func (r *randGen) generateParticularCall(s *state, meta *Syscall) (calls []*Call) {425 c := &Call{426 Meta: meta,427 Ret: MakeReturnArg(meta.Ret),428 }429 c.Args, calls = r.generateArgs(s, meta.Args)430 r.target.assignSizesCall(c)431 calls = append(calls, c)432 for _, c1 := range calls {433 r.target.SanitizeCall(c1)434 }435 return calls436}437// GenerateAllSyzProg generates a program that contains all pseudo syz_ calls for testing.438func (target *Target) GenerateAllSyzProg(rs rand.Source) *Prog {439 p := &Prog{440 Target: target,441 }442 r := newRand(target, rs)443 s := newState(target, nil)444 handled := make(map[string]bool)445 for _, meta := range target.Syscalls {446 if !strings.HasPrefix(meta.CallName, "syz_") || handled[meta.CallName] {447 continue448 }449 handled[meta.CallName] = true450 calls := r.generateParticularCall(s, meta)451 for _, c := range calls {452 s.analyze(c)453 p.Calls = append(p.Calls, c)454 }455 }456 if err := p.validate(); err != nil {457 panic(err)458 }459 return p460}461// GenerateSimpleProg generates the simplest non-empty program for testing462// (e.g. containing a single mmap).463func (target *Target) GenerateSimpleProg() *Prog {464 return &Prog{465 Target: target,466 Calls: []*Call{target.MakeMmap(0, target.PageSize)},467 }468}469func (target *Target) GenerateUberMmapProg() *Prog {470 return &Prog{471 Target: target,472 Calls: []*Call{target.MakeMmap(0, target.NumPages*target.PageSize)},473 }474}475func (r *randGen) generateArgs(s *state, types []Type) ([]Arg, []*Call) {476 var calls []*Call477 args := make([]Arg, len(types))478 // Generate all args. Size args have the default value 0 for now.479 for i, typ := range types {480 arg, calls1 := r.generateArg(s, typ)481 if arg == nil {482 panic(fmt.Sprintf("generated arg is nil for type '%v', types: %+v", typ.Name(), types))483 }484 args[i] = arg485 calls = append(calls, calls1...)486 }487 return args, calls488}489func (r *randGen) generateArg(s *state, typ Type) (arg Arg, calls []*Call) {490 return r.generateArgImpl(s, typ, false)491}492func (r *randGen) generateArgImpl(s *state, typ Type, ignoreSpecial bool) (arg Arg, calls []*Call) {...

Full Screen

Full Screen

Generate

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

Generate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 vm := otto.New()4 vm.Run(`5 var ifuzz = require("ifuzz");6 var f = ifuzz.Generate();7 value, _ := vm.Get("f")8 fmt.Println(value)9}10import (11func main() {12 vm := otto.New()13 vm.Run(`14 var ifuzz = require("ifuzz");15 var f = ifuzz.Generate();16 value, _ := vm.Get("f")17 fmt.Println(value)18}19import (20func main() {21 vm := otto.New()22 vm.Run(`23 var ifuzz = require("ifuzz");24 var f = ifuzz.Generate();25 value, _ := vm.Get("f")26 fmt.Println(value)27}28import (29func main() {30 vm := otto.New()31 vm.Run(`32 var ifuzz = require("ifuzz");33 var f = ifuzz.Generate();34 value, _ := vm.Get("f")35 fmt.Println(value)36}37import (38func main() {39 vm := otto.New()40 vm.Run(`41 var ifuzz = require("ifuzz");42 var f = ifuzz.Generate();43 value, _ := vm.Get("f")44 fmt.Println(value)45}46import (47func main() {48 vm := otto.New()49 vm.Run(`50 var ifuzz = require("ifuzz");51 var f = ifuzz.Generate();52 value, _ := vm.Get("f")53 fmt.Println(value)54}

Full Screen

Full Screen

Generate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 rand.Seed(time.Now().UnixNano())4 fmt.Println("Enter the number of inputs to be generated")5 fmt.Scan(&i)6 fmt.Println("Enter the type of the input 1-Integer 2-Float 3-String")7 fmt.Scan(&t)8 fmt.Println("Enter the range of the input")9 fmt.Scan(&x)10 fmt.Scan(&y)11 if t == 1 {12 I = Integer(x, y)13 } else if t == 2 {14 I = Float(x, y)15 } else if t == 3 {16 I = String(x, y)17 }18 for i > 0 {19 fmt.Println(I.Generate())20 }21}22import (23func main() {24 rand.Seed(time.Now().UnixNano())25 fmt.Println("Enter the number of inputs to be generated")26 fmt.Scan(&i)27 fmt.Println("Enter the type of the input 1-Integer 2-Float 3-String")28 fmt.Scan(&t)29 fmt.Println("Enter the range of the input")30 fmt.Scan(&x)31 fmt.Scan(&y)32 if t == 1 {33 I = Integer(x, y)34 } else if t == 2 {35 I = Float(x, y)36 } else if t == 3 {37 I = String(x, y)38 }39 for i > 0 {40 fmt.Println(I.Generate())41 }42}43import (44func main() {45 rand.Seed(time.Now().UnixNano())46 fmt.Println("Enter the number of inputs to be generated")47 fmt.Scan(&i)48 fmt.Println("Enter the type of the input 1-Integer 2-Float 3-String")49 fmt.Scan(&t)50 fmt.Println("Enter the range of the input")

Full Screen

Full Screen

Generate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fuzzer := ifuzz.New()4 fuzzer.Generate(data, 1024)5 fmt.Println(data)6}7import (8func main() {9 fuzzer := ifuzz.New()10 fuzzer.Generate(data, 1024)11 fmt.Println(data)12}13import (14func main() {15 fuzzer := ifuzz.New()16 fuzzer.Generate(data, 1024)17 fmt.Println(data)18}19import (20func main() {21 fuzzer := ifuzz.New()22 fuzzer.Generate(data, 1024)23 fmt.Println(data)24}25import (26func main() {27 fuzzer := ifuzz.New()28 fuzzer.Generate(data, 1024)29 fmt.Println(data)30}31import (32func main() {33 fuzzer := ifuzz.New()34 fuzzer.Generate(data, 1024)35 fmt.Println(data)36}37import (38func main() {39 fuzzer := ifuzz.New()40 fuzzer.Generate(data, 1024)41 fmt.Println(data

Full Screen

Full Screen

Generate

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fuzz := ifuzz.NewFuzz()4 fmt.Println(fuzz.Generate(10))5}6import (7func main() {8 fuzz := ifuzz.NewFuzz()9 fmt.Println(fuzz.Generate(10))10}11import (12func main() {13 fuzz := ifuzz.NewFuzz()14 fmt.Println(fuzz.Generate(10))15}16import (17func main() {18 fuzz := ifuzz.NewFuzz()19 fmt.Println(fuzz.Generate(10))20}21import (22func main() {23 fuzz := ifuzz.NewFuzz()24 fmt.Println(fuzz.Generate(10))25}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful