Best Syzkaller code snippet using prog.allocVMA
rand.go
Source:rand.go
...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 {559 case r.nOutOf(90, 101):560 v = 0561 case r.nOutOf(10, 11):562 v = r.rand(100)...
allocVMA
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, world.")4}5import "fmt"6func main() {7 fmt.Println("Hello, world.")8}9import "fmt"10func main() {11 fmt.Println("Hello, world.")12}13import "fmt"14func main() {15 fmt.Println("Hello, world.")16}17import "fmt"18func main() {19 fmt.Println("Hello, world.")20}21import "fmt"22func main() {23 fmt.Println("Hello, world.")24}25import "fmt"26func main() {27 fmt.Println("Hello, world.")28}29import "fmt"30func main() {31 fmt.Println("Hello, world.")32}33import "fmt"34func main() {35 fmt.Println("Hello, world.")36}37import "fmt"38func main() {39 fmt.Println("Hello, world.")40}41import "fmt"42func main() {43 fmt.Println("Hello, world.")44}45import "fmt"46func main() {47 fmt.Println("Hello, world.")48}49import "fmt"50func main() {51 fmt.Println("Hello, world.")52}
allocVMA
Using AI Code Generation
1import "fmt"2type Program struct {3}4func (p Program) allocVMA() {5 fmt.Println("VMA allocated")6}7func main() {8 p.allocVMA()9}10import "fmt"11type Program struct {12}13func (p Program) allocVMA() string {14}15func main() {16 fmt.Println(p.allocVMA())17}18import "fmt"19type Program struct {20}21func (p Program) allocVMA(name string) {22 fmt.Println("VMA allocated", name)23}24func main() {25 p.allocVMA("def")26}27import "fmt"28type Program struct {29}30func (p Program) allocVMA(name string, age int) {31 fmt.Println("VMA allocated", name, age)32}33func main() {34 p.allocVMA("def", 30)35}36import "fmt"37type Program struct {38}39func (p *Program) allocVMA(name string) {40 fmt.Println("VMA allocated", name)41}42func main() {43 p.allocVMA("def")44}45import "fmt"46type Program struct {47}48func (p Program) allocVMA(name string) {49 fmt.Println("VMA allocated", name)50}51func main() {52 p.allocVMA("def")53}54import "fmt"55type Program struct {56}57func (p *Program) allocVMA(name string) {58 fmt.Println("VMA allocated", name)59}60func (p Program) allocVM(name string) {61 fmt.Println("VM allocated", name)62}63func main() {
allocVMA
Using AI Code Generation
1func main() {2 prog := new(prog)3 prog.allocVMA(0x1000, 0x2000)4 prog.allocVMA(0x3000, 0x4000)5 prog.allocVMA(0x5000, 0x6000)6 prog.allocVMA(0x7000, 0x8000)7 prog.allocVMA(0x9000, 0xa000)8 prog.allocVMA(0xb000, 0xc000)9 prog.allocVMA(0xd000, 0xe000)10 prog.allocVMA(0xf000, 0x10000)11 prog.allocVMA(0x11000, 0x12000)12 prog.allocVMA(0x13000, 0x14000)13 prog.allocVMA(0x15000, 0x16000)14 prog.allocVMA(0x17000, 0x18000)15 prog.allocVMA(0x19000, 0x1a000)16 prog.allocVMA(0x1b000, 0x1c000)17 prog.allocVMA(0x1d000, 0x1e000)18 prog.allocVMA(0x1f000, 0x20000)19 prog.allocVMA(0x21000, 0x22000)20 prog.allocVMA(0x23000, 0x24000)21 prog.allocVMA(0x25000, 0x26000)22 prog.allocVMA(0x27000, 0x28000)23 prog.allocVMA(0x29000, 0x2a000)24 prog.allocVMA(0x2b000, 0x2c000)25 prog.allocVMA(0x2d000, 0x2e000)26 prog.allocVMA(0x2f000, 0x30000)27 prog.allocVMA(0x31000, 0x32000)28 prog.allocVMA(0x33000, 0x34000)29 prog.allocVMA(0x35000, 0x36000)30 prog.allocVMA(0x37000, 0x
allocVMA
Using AI Code Generation
1func main() {2 fmt.Println(prog.allocVMA(1))3}4import (5import (6type prog struct {7}8func (p *prog) allocVMA(size int) int {9 return p.vma.alloc(size)10}11type vma struct {12}13func (v *vma) alloc(size int) int {14}15prog.go:11: cannot use prog (type prog) as type *prog in argument to main.allocVMA16import (17type prog struct {18}19type vma struct {20}21func (v *vma) alloc(size int) int {22}23func (p *prog) allocVMA(size int) int {24 return p.vma.alloc(size)25}26func main() {27 fmt.Println(prog.allocVMA(1))28}29prog.go:11: cannot use prog (type prog) as type *prog in argument to main.allocVMA
allocVMA
Using AI Code Generation
1import (2type prog struct {3}4func (p prog) allocVMA(size int) {5 for i := 0; i < size; i++ {6 }7}8func (p prog) mapVMAtoPMA() {9 for i := 0; i < len(p.vma); i++ {10 if p.vma[i] == -1 {11 }12 }13}14func main() {15 if len(os.Args) != 3 {16 fmt.Println("Incorrect number of arguments")17 os.Exit(1)18 }19 pid, _ := strconv.Atoi(os.Args[1])20 p := prog{pid, name, make(map[int]int), make(map[int]int)}21 p.allocVMA(4)22 p.mapVMAtoPMA()23 fmt.Println(p.vma)24 fmt.Println(p.pma)25}
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!