How to use ForeachType method of prog Package

Best Syzkaller code snippet using prog.ForeachType

prog_test.go

Source:prog_test.go Github

copy

Full Screen

...17}18func TestDefault(t *testing.T) {19 target, _, _ := initTest(t)20 for _, meta := range target.Syscalls {21 ForeachType(meta, func(typ Type) {22 arg := target.defaultArg(typ)23 if !target.isDefaultArg(arg) {24 t.Errorf("default arg is not default: %s\ntype: %#v\narg: %#v",25 typ, typ, arg)26 }27 })28 }29}30func TestDefaultCallArgs(t *testing.T) {31 target, _, _ := initTest(t)32 for _, meta := range target.SyscallMap {33 // Ensure that we can restore all arguments of all calls.34 prog := fmt.Sprintf("%v()", meta.Name)35 p, err := target.Deserialize([]byte(prog))36 if err != nil {37 t.Fatalf("failed to restore default args in prog %q: %v", prog, err)38 }39 if len(p.Calls) != 1 || p.Calls[0].Meta.Name != meta.Name {40 t.Fatalf("restored bad program from prog %q: %q", prog, p.Serialize())41 }42 }43}44func TestSerialize(t *testing.T) {45 target, rs, iters := initTest(t)46 for i := 0; i < iters; i++ {47 p := target.Generate(rs, 10, nil)48 data := p.Serialize()49 p1, err := target.Deserialize(data)50 if err != nil {51 t.Fatalf("failed to deserialize program: %v\n%s", err, data)52 }53 if p1 == nil {54 t.Fatalf("deserialized nil program:\n%s", data)55 }56 data1 := p1.Serialize()57 if len(p.Calls) != len(p1.Calls) {58 t.Fatalf("different number of calls")59 }60 if !bytes.Equal(data, data1) {61 t.Fatalf("program changed after serialize/deserialize\noriginal:\n%s\n\nnew:\n%s\n", data, data1)62 }63 }64}65func TestVmaType(t *testing.T) {66 target, rs, iters := initRandomTargetTest(t, "test", "64")67 meta := target.SyscallMap["syz_test$vma0"]68 r := newRand(target, rs)69 pageSize := target.PageSize70 for i := 0; i < iters; i++ {71 s := newState(target, nil)72 calls := r.generateParticularCall(s, meta)73 c := calls[len(calls)-1]74 if c.Meta.Name != "syz_test$vma0" {75 t.Fatalf("generated wrong call %v", c.Meta.Name)76 }77 if len(c.Args) != 6 {78 t.Fatalf("generated wrong number of args %v", len(c.Args))79 }80 check := func(v, l Arg, min, max uint64) {81 va, ok := v.(*PointerArg)82 if !ok {83 t.Fatalf("vma has bad type: %v", v)84 }85 la, ok := l.(*ConstArg)86 if !ok {87 t.Fatalf("len has bad type: %v", l)88 }89 if va.VmaSize < min || va.VmaSize > max {90 t.Fatalf("vma has bad size: %v, want [%v-%v]",91 va.VmaSize, min, max)92 }93 if la.Val < min || la.Val > max {94 t.Fatalf("len has bad value: %v, want [%v-%v]",95 la.Val, min, max)96 }97 }98 check(c.Args[0], c.Args[1], 1*pageSize, 1e5*pageSize)99 check(c.Args[2], c.Args[3], 5*pageSize, 5*pageSize)100 check(c.Args[4], c.Args[5], 7*pageSize, 9*pageSize)101 }102}103// TestCrossTarget ensures that a program serialized for one arch can be104// deserialized for another arch. This happens when managers exchange105// programs via hub.106func TestCrossTarget(t *testing.T) {107 t.Parallel()108 const OS = "linux"109 var archs []string110 for _, target := range AllTargets() {111 if target.OS == OS {112 archs = append(archs, target.Arch)113 }114 }115 for _, arch := range archs {116 target, err := GetTarget(OS, arch)117 if err != nil {118 t.Fatal(err)119 }120 var crossTargets []*Target121 for _, crossArch := range archs {122 if crossArch == arch {123 continue124 }125 crossTarget, err := GetTarget(OS, crossArch)126 if err != nil {127 t.Fatal(err)128 }129 crossTargets = append(crossTargets, crossTarget)130 }131 t.Run(fmt.Sprintf("%v/%v", OS, arch), func(t *testing.T) {132 t.Parallel()133 testCrossTarget(t, target, crossTargets)134 })135 }136}137func testCrossTarget(t *testing.T, target *Target, crossTargets []*Target) {138 seed := int64(time.Now().UnixNano())139 t.Logf("seed=%v", seed)140 rs := rand.NewSource(seed)141 iters := 100142 if testing.Short() {143 iters /= 10144 }145 for i := 0; i < iters; i++ {146 p := target.Generate(rs, 20, nil)147 testCrossArchProg(t, p, crossTargets)148 p, err := target.Deserialize(p.Serialize())149 if err != nil {150 t.Fatal(err)151 }152 testCrossArchProg(t, p, crossTargets)153 p.Mutate(rs, 20, nil, nil)154 testCrossArchProg(t, p, crossTargets)155 p, _ = Minimize(p, -1, false, func(*Prog, int) bool {156 return rs.Int63()%2 == 0157 })158 testCrossArchProg(t, p, crossTargets)159 }160}161func testCrossArchProg(t *testing.T, p *Prog, crossTargets []*Target) {162 serialized := p.Serialize()163 for _, crossTarget := range crossTargets {164 _, err := crossTarget.Deserialize(serialized)165 if err == nil || strings.Contains(err.Error(), "unknown syscall") {166 continue167 }168 t.Fatalf("failed to deserialize for %v/%v: %v\n%s",169 crossTarget.OS, crossTarget.Arch, err, serialized)170 }171}172func TestSpecialStructs(t *testing.T) {173 testEachTargetRandom(t, func(t *testing.T, target *Target, rs rand.Source, iters int) {174 for special, gen := range target.SpecialTypes {175 t.Run(special, func(t *testing.T) {176 var typ Type177 for i := 0; i < len(target.Syscalls) && typ == nil; i++ {178 ForeachType(target.Syscalls[i], func(t Type) {179 if t.Dir() == DirOut {180 return181 }182 if s, ok := t.(*StructType); ok && s.Name() == special {183 typ = s184 }185 if s, ok := t.(*UnionType); ok && s.Name() == special {186 typ = s187 }188 })189 }190 if typ == nil {191 t.Fatal("can't find struct description")192 }...

Full Screen

Full Screen

resources.go

Source:resources.go Github

copy

Full Screen

...9 var metas []*Syscall10 for _, meta := range target.Syscalls {11 // Recurse into arguments to see if there is an out/inout arg of necessary type.12 ok := false13 ForeachType(meta, func(typ Type) {14 if ok {15 return16 }17 switch typ1 := typ.(type) {18 case *ResourceType:19 if typ1.Dir() != DirIn && isCompatibleResourceImpl(kind, typ1.Desc.Kind, precise) {20 ok = true21 }22 }23 })24 if ok {25 metas = append(metas, meta)26 }27 }28 return metas29}30// isCompatibleResource returns true if resource of kind src can be passed as an argument of kind dst.31func (target *Target) isCompatibleResource(dst, src string) bool {32 dstRes := target.resourceMap[dst]33 if dstRes == nil {34 panic(fmt.Sprintf("unknown resource '%v'", dst))35 }36 srcRes := target.resourceMap[src]37 if srcRes == nil {38 panic(fmt.Sprintf("unknown resource '%v'", src))39 }40 return isCompatibleResourceImpl(dstRes.Kind, srcRes.Kind, false)41}42// isCompatibleResourceImpl returns true if resource of kind src can be passed as an argument of kind dst.43// If precise is true, then it does not allow passing a less specialized resource (e.g. fd)44// as a more specialized resource (e.g. socket). Otherwise it does.45func isCompatibleResourceImpl(dst, src []string, precise bool) bool {46 if len(dst) > len(src) {47 // dst is more specialized, e.g dst=socket, src=fd.48 if precise {49 return false50 }51 dst = dst[:len(src)]52 }53 if len(src) > len(dst) {54 // src is more specialized, e.g dst=fd, src=socket.55 src = src[:len(dst)]56 }57 for i, k := range dst {58 if k != src[i] {59 return false60 }61 }62 return true63}64func (c *Syscall) inputResources() []*ResourceType {65 var resources []*ResourceType66 ForeachType(c, func(typ Type) {67 switch typ1 := typ.(type) {68 case *ResourceType:69 if typ1.Dir() != DirOut && !typ1.IsOptional {70 resources = append(resources, typ1)71 }72 }73 })74 return resources75}76func (target *Target) TransitivelyEnabledCalls(enabled map[*Syscall]bool) map[*Syscall]bool {77 supported := make(map[*Syscall]bool)78 for c := range enabled {79 supported[c] = true80 }81 inputResources := make(map[*Syscall][]*ResourceType)82 ctors := make(map[string][]*Syscall)83 for c := range supported {84 inputs := c.inputResources()85 inputResources[c] = inputs86 for _, res := range inputs {87 if _, ok := ctors[res.Desc.Name]; ok {88 continue89 }90 ctors[res.Desc.Name] = target.calcResourceCtors(res.Desc.Kind, true)91 }92 }93 for {94 n := len(supported)95 haveGettime := supported[target.SyscallMap["clock_gettime"]]96 for c := range supported {97 canCreate := true98 for _, res := range inputResources[c] {99 noctors := true100 for _, ctor := range ctors[res.Desc.Name] {101 if supported[ctor] {102 noctors = false103 break104 }105 }106 if noctors {107 canCreate = false108 break109 }110 }111 // We need to support structs as resources,112 // but for now we just special-case timespec/timeval.113 if canCreate && !haveGettime {114 ForeachType(c, func(typ Type) {115 if a, ok := typ.(*StructType); ok && a.Dir() != DirOut && (a.Name() == "timespec" || a.Name() == "timeval") {116 canCreate = false117 }118 })119 }120 if !canCreate {121 delete(supported, c)122 }123 }124 if n == len(supported) {125 break126 }127 }128 return supported...

Full Screen

Full Screen

ForeachType

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 node, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)4 if err != nil {5 fmt.Println(err)6 }7 ast.Inspect(node, func(n ast.Node) bool {8 switch x := n.(type) {9 fmt.Println(x.Name.Name)10 }11 })12}

Full Screen

Full Screen

ForeachType

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fset := token.NewFileSet()4 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)5 if err != nil {6 panic(err)7 }8 prog, _ := ssautil.AllPackages(fset, []*ast.File{f}, ssa.SanityCheckFunctions)9 prog.Build()10 for _, mem := range prog.Members {11 switch mem := mem.(type) {12 fmt.Println(mem.Name())13 }14 }15}16func CreateProgram(fset *token.FileSet, files []*ast.File, buildTags []string) (*ssa.Program, error)17import (18func main() {19 fset := token.NewFileSet()20 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)21 if err != nil {22 panic(err)23 }24 prog, _ := ssautil.CreateProgram(fset, []*ast.File{f}, nil)25 prog.Build()26 for _, mem := range prog.Members {27 switch mem := mem.(type) {28 fmt.Println(mem.Name())29 }30 }31}

Full Screen

Full Screen

ForeachType

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fset := token.NewFileSet()4 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)5 if err != nil {6 log.Fatal(err)7 }8 conf := types.Config{Importer: types.GcImporter{}}9 p, err := conf.Check("cmd", fset, []*ast.File{f}, nil)10 if err != nil {11 log.Fatal(err)12 }13 p.ForeachType(func(typ types.Type) {14 fmt.Println(typ)15 })16}17import "fmt"18func main() {19 fmt.Println("Hello, World!")20}21import (22func main() {23 fset := token.NewFileSet()24 f, err := parser.ParseFile(fset, "1.go", nil, parser.ParseComments)25 if err != nil {26 log.Fatal(err)27 }28 conf := types.Config{Importer: types.GcImporter{}}29 p, err := conf.Check("cmd", fset, []*ast.File{f}, nil)30 if err != nil {31 log.Fatal(err)32 }33 p.ForeachType(func(typ types.Type) {34 fmt.Println(typ)35 })36}37Underlying() Type38Elem() Type39Type() Type40TypeAndValue() (Type, Value)41Type() Type42TypeAndValue() (Type, Value)

Full Screen

Full Screen

ForeachType

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 src, err := parser.ParseFile(fset, "1.go", nil, 0)4 if err != nil {5 log.Fatal(err)6 }7 for _, s := range src.Decls {

Full Screen

Full Screen

ForeachType

Using AI Code Generation

copy

Full Screen

1import (2func main() {3struct foo {4 int bar;5 int baz;6};7struct foo foo1 = {8};9struct foo foo2 = {10};11struct foo foo3 = {12};13struct foo foo4 = {14};15struct foo foo5 = {16};17struct foo foo6 = {18};19struct foo foo7 = {20};21struct foo foo8 = {22};23struct foo foo9 = {24};25struct foo foo10 = {26};27struct foo foo11 = {28};29struct foo foo12 = {30};31struct foo foo13 = {32};33struct foo foo14 = {34};35struct foo foo15 = {36};37struct foo foo16 = {38};39struct foo foo17 = {40};41struct foo foo18 = {42};43struct foo foo19 = {44};45struct foo foo20 = {46};47struct foo foo21 = {

Full Screen

Full Screen

ForeachType

Using AI Code Generation

copy

Full Screen

1import (2func main() {3f, err := elf.Open("1.o")4if err != nil {5log.Fatal(err)6}7defer f.Close()8for _, t := range f.Types {9fmt.Printf("%s10}11}12import (13func main() {14f, err := elf.Open("1.o")15if err != nil {16log.Fatal(err)17}18defer f.Close()19for _, t := range f.Types {20fmt.Printf("%s21}22}23import (24func main() {25f, err := elf.Open("1.o")26if err != nil {27log.Fatal(err)28}29defer f.Close()30for _, t := range f.Types {31fmt.Printf("%s32}33}34import (35func main() {36f, err := elf.Open("1.o")37if err != nil {38log.Fatal(err)39}40defer f.Close()41for _, t := range f.Types {42fmt.Printf("%s43}44}45import (46func main() {47f, err := elf.Open("1.o")48if err != nil {49log.Fatal(err)50}51defer f.Close()52for _, t := range f.Types {53fmt.Printf("%s54}55}56import (

Full Screen

Full Screen

ForeachType

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 file, err := elf.Open("1.o")4 if err != nil {5 fmt.Println(err)6 }7 defer file.Close()8 prog.ForeachType(func(typ elf.Type) {9 fmt.Printf("Type: %s\tSize: %d10 })11}

Full Screen

Full Screen

ForeachType

Using AI Code Generation

copy

Full Screen

1func main() {2 prog, _ := loader.LoadProg("testprog")3 prog.ForeachType(func(t *prog.Type) {4 if t.Name == "" {5 }6 fmt.Printf("Type: %s7 for _, f := range t.Fields {8 fmt.Printf("Field: %s9 }10 })11}12import (13type MyStruct struct {14}15func main() {16 fmt.Println("Hello, playground")17}18import (19type MyStruct struct {20}21func main() {22 fmt.Println("Hello, playground")23}24import (25type MyStruct struct {26}27func main() {28 fmt.Println("Hello, playground")29}30import (31type MyStruct struct {32}33func main() {34 fmt.Println("Hello, playground")35}36import (37type MyStruct struct {38}39func main() {40 fmt.Println("Hello, playground")41}42import (43type MyStruct struct {44}45func main() {46 fmt.Println("Hello, playground")47}48import (49type MyStruct struct {50}51func main() {52 fmt.Println("Hello, playground")53}54import (55type MyStruct struct {56}57func main() {58 fmt.Println("Hello, playground")59}60import (

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