Best Syzkaller code snippet using prog.InnerArg
minimization.go
Source:minimization.go
1// Copyright 2018 syzkaller project authors. All rights reserved.2// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.3package prog4import (5 "fmt"6)7// Minimize minimizes program p into an equivalent program using the equivalence8// predicate pred. It iteratively generates simpler programs and asks pred9// whether it is equal to the original program or not. If it is equivalent then10// the simplification attempt is committed and the process continues.11func Minimize(p0 *Prog, callIndex0 int, crash bool, pred0 func(*Prog, int) bool) (*Prog, int) {12 pred := pred013 if debug {14 pred = func(p *Prog, callIndex int) bool {15 if err := p.validate(); err != nil {16 panic(err)17 }18 return pred0(p, callIndex)19 }20 }21 name0 := ""22 if callIndex0 != -1 {23 if callIndex0 < 0 || callIndex0 >= len(p0.Calls) {24 panic("bad call index")25 }26 name0 = p0.Calls[callIndex0].Meta.Name27 }28 // Try to remove all calls except the last one one-by-one.29 for i := len(p0.Calls) - 1; i >= 0; i-- {30 if i == callIndex0 {31 continue32 }33 callIndex := callIndex034 if i < callIndex {35 callIndex--36 }37 p := p0.Clone()38 p.removeCall(i)39 if !pred(p, callIndex) {40 continue41 }42 p0 = p43 callIndex0 = callIndex44 }45 var triedPaths map[string]bool46 var rec func(p *Prog, call *Call, arg Arg, path string) bool47 rec = func(p *Prog, call *Call, arg Arg, path string) bool {48 path += fmt.Sprintf("-%v", arg.Type().FieldName())49 switch typ := arg.Type().(type) {50 case *StructType:51 a := arg.(*GroupArg)52 for _, innerArg := range a.Inner {53 if rec(p, call, innerArg, path) {54 return true55 }56 }57 case *UnionType:58 a := arg.(*UnionArg)59 if rec(p, call, a.Option, path) {60 return true61 }62 case *PtrType:63 // TODO: try to remove optional ptrs64 a, ok := arg.(*PointerArg)65 if !ok {66 // Can also be *ConstArg.67 return false68 }69 if a.Res != nil {70 return rec(p, call, a.Res, path)71 }72 case *ArrayType:73 a := arg.(*GroupArg)74 for i, innerArg := range a.Inner {75 innerPath := fmt.Sprintf("%v-%v", path, i)76 if !triedPaths[innerPath] && !crash {77 if (typ.Kind == ArrayRangeLen && len(a.Inner) > int(typ.RangeBegin)) ||78 (typ.Kind == ArrayRandLen) {79 copy(a.Inner[i:], a.Inner[i+1:])80 a.Inner = a.Inner[:len(a.Inner)-1]81 removeArg(innerArg)82 p.Target.assignSizesCall(call)83 if pred(p, callIndex0) {84 p0 = p85 } else {86 triedPaths[innerPath] = true87 }88 return true89 }90 }91 if rec(p, call, innerArg, innerPath) {92 return true93 }94 }95 case *IntType, *FlagsType, *ProcType:96 // TODO: try to reset bits in ints97 // TODO: try to set separate flags98 if crash {99 return false100 }101 if triedPaths[path] {102 return false103 }104 triedPaths[path] = true105 a := arg.(*ConstArg)106 if a.Val == typ.Default() {107 return false108 }109 v0 := a.Val110 a.Val = typ.Default()111 if pred(p, callIndex0) {112 p0 = p113 return true114 }115 a.Val = v0116 case *ResourceType:117 if crash {118 return false119 }120 if triedPaths[path] {121 return false122 }123 triedPaths[path] = true124 a := arg.(*ResultArg)125 if a.Res == nil {126 return false127 }128 r0 := a.Res129 a.Res = nil130 a.Val = typ.Default()131 if pred(p, callIndex0) {132 p0 = p133 return true134 }135 a.Res = r0136 a.Val = 0137 case *BufferType:138 // TODO: try to set individual bytes to 0139 if triedPaths[path] {140 return false141 }142 triedPaths[path] = true143 if typ.Kind != BufferBlobRand && typ.Kind != BufferBlobRange ||144 typ.Dir() == DirOut {145 return false146 }147 a := arg.(*DataArg)148 minLen := int(typ.RangeBegin)149 for step := len(a.Data()) - minLen; len(a.Data()) > minLen && step > 0; {150 if len(a.Data())-step >= minLen {151 a.data = a.Data()[:len(a.Data())-step]152 p.Target.assignSizesCall(call)153 if pred(p, callIndex0) {154 continue155 }156 a.data = a.Data()[:len(a.Data())+step]157 p.Target.assignSizesCall(call)158 }159 step /= 2160 if crash {161 break162 }163 }164 p0 = p165 case *VmaType, *LenType, *CsumType, *ConstType:166 return false167 default:168 panic(fmt.Sprintf("unknown arg type '%+v'", typ))169 }170 return false171 }172 // Try to minimize individual args.173 for i := 0; i < len(p0.Calls); i++ {174 triedPaths = make(map[string]bool)175 again:176 p := p0.Clone()177 call := p.Calls[i]178 for j, arg := range call.Args {179 if rec(p, call, arg, fmt.Sprintf("%v", j)) {180 goto again181 }182 }183 }184 if callIndex0 != -1 {185 if callIndex0 < 0 || callIndex0 >= len(p0.Calls) || name0 != p0.Calls[callIndex0].Meta.Name {186 panic(fmt.Sprintf("bad call index after minimization: ncalls=%v index=%v call=%v/%v",187 len(p0.Calls), callIndex0, name0, p0.Calls[callIndex0].Meta.Name))188 }189 }190 return p0, callIndex0191}192func MinimizeRace(p0 *Prog, callIndex0 int, crash bool, pred0 func(*Prog, int, [2]int) bool, index0 [2]int) (*Prog, int, [2]int) {193 pred := pred0194 if debug {195 pred = func(p *Prog, callIndex int, index [2]int) bool {196 if err := p.validate(); err != nil {197 panic(err)198 }199 return pred0(p, callIndex, index)200 }201 }202 name0 := ""203 if callIndex0 != -1 {204 if callIndex0 < 0 || callIndex0 >= len(p0.Calls) {205 panic("bad call index")206 }207 name0 = p0.Calls[callIndex0].Meta.Name208 }209 // Try to remove all calls except the last one one-by-one.210 for i := len(p0.Calls) - 1; i >= 0; i-- {211 if i == callIndex0 || i == index0[0] || i == index0[1] {212 continue213 }214 callIndex := callIndex0215 index := index0216 if i < callIndex {217 callIndex--218 }219 if i < index[0] {220 index[0]--221 }222 if i < index[1] {223 index[1]--224 }225 p := p0.Clone()226 p.removeCall(i)227 if !pred(p, callIndex, index) {228 continue229 }230 p0 = p231 callIndex0 = callIndex232 index0 = index233 }234 var triedPaths map[string]bool235 var rec func(p *Prog, call *Call, arg Arg, path string) bool236 rec = func(p *Prog, call *Call, arg Arg, path string) bool {237 path += fmt.Sprintf("-%v", arg.Type().FieldName())238 switch typ := arg.Type().(type) {239 case *StructType:240 a := arg.(*GroupArg)241 for _, innerArg := range a.Inner {242 if rec(p, call, innerArg, path) {243 return true244 }245 }246 case *UnionType:247 a := arg.(*UnionArg)248 if rec(p, call, a.Option, path) {249 return true250 }251 case *PtrType:252 // TODO: try to remove optional ptrs253 a, ok := arg.(*PointerArg)254 if !ok {255 // Can also be *ConstArg.256 return false257 }258 if a.Res != nil {259 return rec(p, call, a.Res, path)260 }261 case *ArrayType:262 a := arg.(*GroupArg)263 for i, innerArg := range a.Inner {264 innerPath := fmt.Sprintf("%v-%v", path, i)265 if !triedPaths[innerPath] && !crash {266 if (typ.Kind == ArrayRangeLen && len(a.Inner) > int(typ.RangeBegin)) ||267 (typ.Kind == ArrayRandLen) {268 copy(a.Inner[i:], a.Inner[i+1:])269 a.Inner = a.Inner[:len(a.Inner)-1]270 removeArg(innerArg)271 p.Target.assignSizesCall(call)272 if pred(p, callIndex0, index0) {273 p0 = p274 } else {275 triedPaths[innerPath] = true276 }277 return true278 }279 }280 if rec(p, call, innerArg, innerPath) {281 return true282 }283 }284 case *IntType, *FlagsType, *ProcType:285 // TODO: try to reset bits in ints286 // TODO: try to set separate flags287 if crash {288 return false289 }290 if triedPaths[path] {291 return false292 }293 triedPaths[path] = true294 a := arg.(*ConstArg)295 if a.Val == typ.Default() {296 return false297 }298 v0 := a.Val299 a.Val = typ.Default()300 if pred(p, callIndex0, index0) {301 p0 = p302 return true303 }304 a.Val = v0305 case *ResourceType:306 if crash {307 return false308 }309 if triedPaths[path] {310 return false311 }312 triedPaths[path] = true313 a := arg.(*ResultArg)314 if a.Res == nil {315 return false316 }317 r0 := a.Res318 a.Res = nil319 a.Val = typ.Default()320 if pred(p, callIndex0, index0) {321 p0 = p322 return true323 }324 a.Res = r0325 a.Val = 0326 case *BufferType:327 // TODO: try to set individual bytes to 0328 if triedPaths[path] {329 return false330 }331 triedPaths[path] = true332 if typ.Kind != BufferBlobRand && typ.Kind != BufferBlobRange ||333 typ.Dir() == DirOut {334 return false335 }336 a := arg.(*DataArg)337 minLen := int(typ.RangeBegin)338 for step := len(a.Data()) - minLen; len(a.Data()) > minLen && step > 0; {339 if len(a.Data())-step >= minLen {340 a.data = a.Data()[:len(a.Data())-step]341 p.Target.assignSizesCall(call)342 if pred(p, callIndex0, index0) {343 continue344 }345 a.data = a.Data()[:len(a.Data())+step]346 p.Target.assignSizesCall(call)347 }348 step /= 2349 if crash {350 break351 }352 }353 p0 = p354 case *VmaType, *LenType, *CsumType, *ConstType:355 return false356 default:357 panic(fmt.Sprintf("unknown arg type '%+v'", typ))358 }359 return false360 }361 // Try to minimize individual args.362 for i := 0; i < len(p0.Calls); i++ {363 triedPaths = make(map[string]bool)364 again:365 p := p0.Clone()366 call := p.Calls[i]367 for j, arg := range call.Args {368 if rec(p, call, arg, fmt.Sprintf("%v", j)) {369 goto again370 }371 }372 }373 if callIndex0 != -1 {374 if callIndex0 < 0 || callIndex0 >= len(p0.Calls) || name0 != p0.Calls[callIndex0].Meta.Name {375 panic(fmt.Sprintf("bad call index after minimization: ncalls=%v index=%v call=%v/%v",376 len(p0.Calls), callIndex0, name0, p0.Calls[callIndex0].Meta.Name))377 }378 }379 return p0, callIndex0, index0380}...
parseInnerCalls.go
Source:parseInnerCalls.go
1package proggen2import (3 "github.com/google/syzkaller/pkg/log"4 "github.com/google/syzkaller/prog"5 "github.com/shankarapailoor/trace2syz/parser"6 "strconv"7 "strings"8)9func parseInnerCall(syzType prog.Type, traceType *parser.Call, ctx *Context) prog.Arg {10 switch traceType.CallName {11 case "htons":12 return htonsHtonl(syzType, traceType, ctx)13 case "htonl":14 return htonsHtonl(syzType, traceType, ctx)15 case "inet_addr":16 return inetAddr(syzType, traceType, ctx)17 case "inet_pton":18 return inetPton(syzType, traceType, ctx)19 case "makedev":20 return makedev(syzType, traceType, ctx)21 default:22 log.Fatalf("Inner Call: %s Unsupported", traceType.CallName)23 }24 return nil25}26func makedev(syzType prog.Type, traceType *parser.Call, ctx *Context) prog.Arg {27 var major, minor, id int6428 arg1 := traceType.Args[0].(parser.Expression)29 arg2 := traceType.Args[1].(parser.Expression)30 major = int64(arg1.Eval(ctx.Target))31 minor = int64(arg2.Eval(ctx.Target))32 id = ((minor & 0xff) | ((major & 0xfff) << 8) | ((minor & ^0xff) << 12) | ((major & ^0xfff) << 32))33 return prog.MakeConstArg(syzType, uint64(id))34}35func htonsHtonl(syzType prog.Type, traceType *parser.Call, ctx *Context) prog.Arg {36 if len(traceType.Args) > 1 {37 log.Fatalf("Cannot evaluate htonsHtonl since it has more than one arg.")38 }39 switch typ := syzType.(type) {40 case *prog.ProcType:41 switch a := traceType.Args[0].(type) {42 case parser.Expression:43 val := a.Eval(ctx.Target)44 if val >= typ.ValuesPerProc {45 return prog.MakeConstArg(syzType, typ.ValuesPerProc-1)46 }47 return prog.MakeConstArg(syzType, val)48 default:49 log.Fatalf("Expected first arg of Htons/Htonl to be expression. Got: %s", a.Name())50 }51 case *prog.ConstType, *prog.IntType, *prog.FlagsType:52 switch a := traceType.Args[0].(type) {53 case parser.Expression:54 val := a.Eval(ctx.Target)55 return prog.MakeConstArg(syzType, val)56 default:57 log.Fatalf("Expected first arg of Htons/Htonl to be expression. Got: %s", a.Name())58 }59 default:60 log.Fatalf("First arg of Htons/Htonl is not const Type: %s\n", syzType.Name())61 }62 return nil63}64func inetAddr(syzType prog.Type, traceType *parser.Call, ctx *Context) prog.Arg {65 var ip uint6466 ip4Addr := func(ipaddr string) uint64 {67 var (68 ip = strings.Split(ipaddr, ".")69 ip1, ip2, ip3, ip4 uint6470 ret uint6471 )72 ip1, _ = strconv.ParseUint(ip[0], 10, 8)73 ip2, _ = strconv.ParseUint(ip[1], 10, 8)74 ip3, _ = strconv.ParseUint(ip[2], 10, 8)75 ip4, _ = strconv.ParseUint(ip[3], 10, 8)76 ret = ip1<<24 + ip2<<16 + ip3<<8 + ip477 return ret78 }79 if len(traceType.Args) > 1 {80 log.Logf(3, "%#v", traceType.Args)81 log.Fatalf("inetAddr should only have one argument. Found: %d\n", len(traceType.Args))82 }83 switch a := traceType.Args[0].(type) {84 case *parser.BufferType:85 ip = ip4Addr(a.Val)86 default:87 log.Fatalf("Parsing inet_addr and inner arg has non ipv4 type")88 }89 switch a := syzType.(type) {90 case *prog.UnionType:91 for _, field := range a.Fields {92 if !strings.Contains(field.FieldName(), "rand") {93 continue94 }95 switch field.(type) {96 case *prog.IntType:97 return prog.MakeUnionArg(syzType, prog.MakeConstArg(field, ip))98 default:99 log.Fatalf("Rand field isn't int type. Instead is %s", field.Name())100 }101 }102 default:103 log.Fatalf("Parsing ip address for non-union type %s", a.Name())104 }105 log.Logf(4, "Generating default arg for ip address")106 return prog.DefaultArg(syzType)107}108func inetPton(syzType prog.Type, traceType *parser.Call, ctx *Context) prog.Arg {109 log.Logf(4, "type name: %s", syzType.Name())110 unionType := syzType.(*prog.UnionType)111 var optType prog.Type112 var innerArg prog.Arg113 if len(traceType.Args) != 3 {114 log.Fatalf("InetPton expects 3 args: %v.", traceType.Args)115 }116 switch a := traceType.Args[1].(type) {117 case *parser.BufferType:118 switch a.Val {119 case "::":120 optType = unionType.Fields[0]121 case "::1":122 optType = unionType.Fields[3]123 default:124 optType = unionType.Fields[0]125 }126 innerArg = prog.DefaultArg(optType)127 default:128 log.Fatalf("Parsing inet_addr and inner arg has non ipv4 type")129 }130 return prog.MakeUnionArg(syzType, innerArg)131}...
InnerArg
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println(prog.InnerArg())4}5import "fmt"6func main() {7 fmt.Println(prog.InnerArg())8}9import "fmt"10func main() {11 fmt.Println(prog.InnerArg())12}13import "fmt"14func main() {15 fmt.Println(prog.InnerArg())16}17import "fmt"18func main() {19 fmt.Println(prog.InnerArg())20}21import "fmt"22func main() {23 fmt.Println(prog.InnerArg())24}25import "fmt"26func main() {27 fmt.Println(prog.InnerArg())28}29import "fmt"30func main() {31 fmt.Println(prog.InnerArg())32}33import "fmt"34func main() {35 fmt.Println(prog.InnerArg())36}37import "fmt"38func main() {39 fmt.Println(prog.InnerArg())40}41import "fmt"42func main() {43 fmt.Println(prog.InnerArg())44}45import "fmt"46func main() {47 fmt.Println(prog.InnerArg())48}49import "fmt"50func main() {51 fmt.Println(prog.InnerArg())52}53import "fmt"54func main() {55 fmt.Println(prog.InnerArg
InnerArg
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println(prog.InnerArg())4}5func InnerArg() string {6}
InnerArg
Using AI Code Generation
1import "fmt"2func main() {3 p := prog{3,4}4 fmt.Println(p.InnerArg())5}6import "fmt"7func main() {8 p := prog{3,4}9 fmt.Println(p.InnerArg())10}11import "fmt"12func main() {13 p := prog{3,4}14 fmt.Println(p.InnerArg())15}16import "fmt"17func main() {18 p := prog{3,4}19 fmt.Println(p.InnerArg())20}21import "fmt"22func main() {23 p := prog{3,4}24 fmt.Println(p.InnerArg())25}26import "fmt"27func main() {28 p := prog{3,4}29 fmt.Println(p.InnerArg())30}31import "fmt"32func main() {33 p := prog{3,4}34 fmt.Println(p.InnerArg())35}36import "fmt"37func main() {38 p := prog{3,4}39 fmt.Println(p.InnerArg())40}41import "fmt"42func main() {43 p := prog{3,4}44 fmt.Println(p.InnerArg())45}46import "fmt"47func main() {48 p := prog{3,4}49 fmt.Println(p.InnerArg())50}51import "fmt"52func main() {53 p := prog{3,4}54 fmt.Println(p.InnerArg())55}
InnerArg
Using AI Code Generation
1import "fmt"2import "prog"3func main() {4 fmt.Println(prog.InnerArg())5}6func InnerArg() string {7}8func OuterArg() string {9}10import "fmt"11import "github.com/yourname/yourproject/prog"12func main() {13 fmt.Println(prog.InnerArg())14}
InnerArg
Using AI Code Generation
1import (2func main() {3 p := mypkg.Prog{}4 p.InnerArg()5}6import "fmt"7type Prog struct {8}9func (p *Prog) InnerArg() {10 fmt.Println(p.Arg)11}
InnerArg
Using AI Code Generation
1import "fmt"2func main() {3 p = prog{10}4 p.InnerArg()5}6import "fmt"7type prog struct {8}9func (p prog) InnerArg() {10 fmt.Println(p.i)11}
InnerArg
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println(prog.InnerArg())4}5type prog struct {6}7func (p *prog) InnerArg() int {8}9import "fmt"10import "./prog"11func main() {12 fmt.Println(prog.InnerArg())13}14type Prog struct {15}16func (p *Prog) InnerArg() int {17}
InnerArg
Using AI Code Generation
1import (2func main() {3 fmt.Println("OuterArg value is:", prog.OuterArg)4 fmt.Println("InnerArg value is:", prog.InnerArg)5}6import (7func main() {8 fmt.Println("OuterArg value is:", prog.OuterArg)9 fmt.Println("InnerArg value is:", prog.InnerArg)10}
InnerArg
Using AI Code Generation
1import (2func main() {3 p := prog.NewProg()4 fmt.Println(p.InnerArg(10))5}6func InnerArg(x int) int {7}8func NewProg() *Prog {9 return new(Prog)10}11type Prog struct{}12func (p *Prog) InnerArg(x int) int {13}14func NewProg() *Prog {15 return new(Prog)16}
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!!