How to use mutateData method of prog Package

Best Syzkaller code snippet using prog.mutateData

mutation.go

Source:mutation.go Github

copy

Full Screen

...45 idx := r.Intn(len(blobs))46 arg := blobs[idx]47 base := bases[idx]48 baseSize := base.Res.Size()49 arg.data = mutateData(r, arg.Data(), 0, maxBlobLen)50 // Update base pointer if size has increased.51 if baseSize < base.Res.Size() {52 s := analyze(ct, p, p.Calls[0])53 newArg := r.allocAddr(s, base.Type(), base.Res.Size(), base.Res)54 *base = *newArg55 }56 case r.nOutOf(1, 100):57 // Splice with another prog from corpus.58 if len(corpus) == 0 || len(p.Calls) == 0 {59 retry = true60 continue61 }62 p0 := corpus[r.Intn(len(corpus))]63 p0c := p0.Clone()64 idx := r.Intn(len(p.Calls))65 p.Calls = append(p.Calls[:idx], append(p0c.Calls, p.Calls[idx:]...)...)66 for i := len(p.Calls) - 1; i >= ncalls; i-- {67 p.removeCall(i)68 }69 case r.nOutOf(20, 31):70 // Insert a new call.71 if len(p.Calls) >= ncalls {72 retry = true73 continue74 }75 idx := r.biasedRand(len(p.Calls)+1, 5)76 var c *Call77 if idx < len(p.Calls) {78 c = p.Calls[idx]79 }80 s := analyze(ct, p, c)81 calls := r.generateCall(s, p)82 p.insertBefore(c, calls)83 case r.nOutOf(10, 11):84 // Change args of a call.85 if len(p.Calls) == 0 {86 retry = true87 continue88 }89 c := p.Calls[r.Intn(len(p.Calls))]90 if len(c.Args) == 0 {91 retry = true92 continue93 }94 s := analyze(ct, p, c)95 updateSizes := true96 retryArg := false97 for stop := false; !stop || retryArg; stop = r.oneOf(3) {98 retryArg = false99 ma := &mutationArgs{target: p.Target}100 ForeachArg(c, ma.collectArg)101 if len(ma.args) == 0 {102 retry = true103 continue outer104 }105 idx := r.Intn(len(ma.args))106 arg, ctx := ma.args[idx], ma.ctxes[idx]107 calls, ok := p.Target.mutateArg(r, s, arg, ctx, &updateSizes)108 if !ok {109 retryArg = true110 continue111 }112 p.insertBefore(c, calls)113 if updateSizes {114 p.Target.assignSizesCall(c)115 }116 p.Target.SanitizeCall(c)117 }118 default:119 // Remove a random call.120 if len(p.Calls) == 0 {121 retry = true122 continue123 }124 idx := r.Intn(len(p.Calls))125 p.removeCall(idx)126 }127 }128 for _, c := range p.Calls {129 p.Target.SanitizeCall(c)130 }131 if debug {132 if err := p.validate(); err != nil {133 panic(err)134 }135 }136}137func (target *Target) mutateArg(r *randGen, s *state, arg Arg, ctx ArgCtx, updateSizes *bool) (calls []*Call, ok bool) {138 var baseSize uint64139 if ctx.Base != nil {140 baseSize = ctx.Base.Res.Size()141 }142 switch t := arg.Type().(type) {143 case *IntType, *FlagsType:144 a := arg.(*ConstArg)145 if r.bin() {146 var newArg Arg147 newArg, calls = r.generateArg(s, arg.Type())148 replaceArg(arg, newArg)149 } else {150 switch {151 case r.nOutOf(1, 3):152 a.Val += uint64(r.Intn(4)) + 1153 case r.nOutOf(1, 2):154 a.Val -= uint64(r.Intn(4)) + 1155 default:156 a.Val ^= 1 << uint64(r.Intn(64))157 }158 }159 case *LenType:160 if !r.mutateSize(arg.(*ConstArg), *ctx.Parent) {161 return nil, false162 }163 *updateSizes = false164 case *ResourceType, *VmaType, *ProcType:165 var newArg Arg166 newArg, calls = r.generateArg(s, arg.Type())167 replaceArg(arg, newArg)168 case *BufferType:169 a := arg.(*DataArg)170 switch t.Kind {171 case BufferBlobRand, BufferBlobRange:172 data := append([]byte{}, a.Data()...)173 minLen, maxLen := uint64(0), maxBlobLen174 if t.Kind == BufferBlobRange {175 minLen, maxLen = t.RangeBegin, t.RangeEnd176 }177 a.data = mutateData(r, data, minLen, maxLen)178 case BufferString:179 data := append([]byte{}, a.Data()...)180 if r.bin() {181 minLen, maxLen := uint64(0), maxBlobLen182 if t.TypeSize != 0 {183 minLen, maxLen = t.TypeSize, t.TypeSize184 }185 a.data = mutateData(r, data, minLen, maxLen)186 } else {187 a.data = r.randString(s, t)188 }189 case BufferFilename:190 a.data = []byte(r.filename(s, t))191 case BufferText:192 data := append([]byte{}, a.Data()...)193 a.data = r.mutateText(t.Text, data)194 default:195 panic("unknown buffer kind")196 }197 case *ArrayType:198 a := arg.(*GroupArg)199 count := uint64(0)200 switch t.Kind {201 case ArrayRandLen:202 for count == uint64(len(a.Inner)) {203 count = r.randArrayLen()204 }205 case ArrayRangeLen:206 if t.RangeBegin == t.RangeEnd {207 panic("trying to mutate fixed length array")208 }209 for count == uint64(len(a.Inner)) {210 count = r.randRange(t.RangeBegin, t.RangeEnd)211 }212 }213 if count > uint64(len(a.Inner)) {214 for count > uint64(len(a.Inner)) {215 newArg, newCalls := r.generateArg(s, t.Type)216 a.Inner = append(a.Inner, newArg)217 calls = append(calls, newCalls...)218 for _, c := range newCalls {219 s.analyze(c)220 }221 }222 } else if count < uint64(len(a.Inner)) {223 for _, arg := range a.Inner[count:] {224 removeArg(arg)225 }226 a.Inner = a.Inner[:count]227 }228 // TODO: swap elements of the array229 case *PtrType:230 a := arg.(*PointerArg)231 newArg := r.allocAddr(s, t, a.Res.Size(), a.Res)232 replaceArg(arg, newArg)233 case *StructType:234 gen := target.SpecialTypes[t.Name()]235 if gen == nil {236 panic("bad arg returned by mutationArgs: StructType")237 }238 var newArg Arg239 newArg, calls = gen(&Gen{r, s}, t, arg)240 for i, f := range newArg.(*GroupArg).Inner {241 replaceArg(arg.(*GroupArg).Inner[i], f)242 }243 case *UnionType:244 if gen := target.SpecialTypes[t.Name()]; gen != nil {245 var newArg Arg246 newArg, calls = gen(&Gen{r, s}, t, arg)247 replaceArg(arg, newArg)248 } else {249 a := arg.(*UnionArg)250 current := -1251 for i, option := range t.Fields {252 if a.Option.Type().FieldName() == option.FieldName() {253 current = i254 break255 }256 }257 if current == -1 {258 panic("can't find current option in union")259 }260 newIdx := r.Intn(len(t.Fields) - 1)261 if newIdx >= current {262 newIdx++263 }264 optType := t.Fields[newIdx]265 removeArg(a.Option)266 var newOpt Arg267 newOpt, calls = r.generateArg(s, optType)268 replaceArg(arg, MakeUnionArg(t, newOpt))269 }270 case *CsumType:271 panic("bad arg returned by mutationArgs: CsumType")272 case *ConstType:273 panic("bad arg returned by mutationArgs: ConstType")274 default:275 panic(fmt.Sprintf("bad arg returned by mutationArgs: %#v, type=%#v", arg, arg.Type()))276 }277 // Update base pointer if size has increased.278 if base := ctx.Base; base != nil && baseSize < base.Res.Size() {279 newArg := r.allocAddr(s, base.Type(), base.Res.Size(), base.Res)280 *base = *newArg281 }282 for _, c := range calls {283 target.SanitizeCall(c)284 }285 return calls, true286}287type mutationArgs struct {288 target *Target289 args []Arg290 ctxes []ArgCtx291 ignoreSpecial bool292}293func (ma *mutationArgs) collectArg(arg Arg, ctx *ArgCtx) {294 ignoreSpecial := ma.ignoreSpecial295 ma.ignoreSpecial = false296 switch typ := arg.Type().(type) {297 case *StructType:298 if ma.target.SpecialTypes[typ.Name()] == nil || ignoreSpecial {299 return // For structs only individual fields are updated.300 }301 // These special structs are mutated as a whole.302 ctx.Stop = true303 case *UnionType:304 if ma.target.SpecialTypes[typ.Name()] == nil && len(typ.Fields) == 1 || ignoreSpecial {305 return306 }307 ctx.Stop = true308 case *ArrayType:309 // Don't mutate fixed-size arrays.310 if typ.Kind == ArrayRangeLen && typ.RangeBegin == typ.RangeEnd {311 return312 }313 case *CsumType:314 return // Checksum is updated when the checksummed data changes.315 case *ConstType:316 return // Well, this is const.317 case *BufferType:318 if typ.Kind == BufferString && len(typ.Values) == 1 {319 return // string const320 }321 case *PtrType:322 if arg.(*PointerArg).IsNull() {323 // TODO: we ought to mutate this, but we don't have code for this yet.324 return325 }326 }327 typ := arg.Type()328 if typ == nil || typ.Dir() == DirOut || !typ.Varlen() && typ.Size() == 0 {329 return330 }331 ma.args = append(ma.args, arg)332 ma.ctxes = append(ma.ctxes, *ctx)333}334func mutateData(r *randGen, data []byte, minLen, maxLen uint64) []byte {335 const maxInc = 35336 retry := false337loop:338 for stop := false; !stop || retry; stop = r.oneOf(3) {339 retry = false340 // TODO(dvyukov): duplicate part of data.341 switch r.Intn(7) {342 case 0:343 // Flip bit in byte.344 if len(data) == 0 {345 retry = true346 continue loop347 }348 byt := r.Intn(len(data))...

Full Screen

Full Screen

mutateData

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 p := prog{}4 p.mutateData()5 fmt.Println(p.data)6}7type prog struct {8}9func (p *prog) mutateData() {10}11func (p *prog) mutateData() {12func (p prog) mutateData() {13func (p prog) mutateData() {

Full Screen

Full Screen

mutateData

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 p := prog{name: "Go", version: 1.2}4 fmt.Println(p.name, p.version)5 p.mutateData()6 fmt.Println(p.name, p.version)7}8type prog struct {9}10func (p *prog) mutateData() {11}

Full Screen

Full Screen

mutateData

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3fmt.Println(a.data)4a.mutateData()5fmt.Println(a.data)6}7import "fmt"8func main() {9fmt.Println(a.data)10a.mutateData()11fmt.Println(a.data)12}

Full Screen

Full Screen

mutateData

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 fmt.Println(a)4 fmt.Println(b)5 fmt.Println(c)6}7import "fmt"8func main() {9 fmt.Println(a)10 fmt.Println(b)11 fmt.Println(c)12}13import "fmt"14func main() {15 fmt.Println(a)16 fmt.Println(b)17 fmt.Println(c)18}19import "fmt"20func main() {21 fmt.Println(a)22 fmt.Println(b)23 fmt.Println(c)24}25import "fmt"26func main() {27 fmt.Println(a)28 fmt.Println(b)29 fmt.Println(c)30}31import "fmt"32func main() {33 fmt.Println(a)34 fmt.Println(b)35 fmt.Println(c)36}37import "fmt"38func main() {

Full Screen

Full Screen

mutateData

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 p.mutateData()4 fmt.Println("name:", p.name, "id:", p.id)5}6import "fmt"7func main() {8 fmt.Println("a:", a, "b:", *b)9 fmt.Println("a:", a, "b:", *b)10}11import "fmt"12func main() {13 fmt.Println("a:", a, "b:", *b)14 fmt.Println("a:", a, "b:", *b)15}16import "fmt"17func main() {18 fmt.Println("a:", a, "b:", *b)19 fmt.Println("a:", a, "b:", *b)20 fmt.Println("a:", a, "b:", *b)21 fmt.Println("a:", a, "b:", *b, "c:", c)22}

Full Screen

Full Screen

mutateData

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 p := prog{}5 p.mutateData(100)6 fmt.Println(p.data)7}8type prog struct {9}10func (p *prog) mutateData(newData int) {11}12import (13func main() {14 fmt.Println("Hello, playground")15 p := prog{}16 p.mutateData(100)17 fmt.Println(p.data)18}19type prog struct {20}21}22import (23func main() {24 fmt.Println("Hello, playground")25 p := prog{}26 p.mutateData(100)27 fmt.Println(p.data)28}29type prog struct {30}31func (p prog) mutateData(newData int) {32}33import (34func main() {35 fmt.Println("Hello, playground")36 p := prog{}37 p.mutateData(100)38 fmt.Println(p.data)39}40type prog struct {41}42func (p *prog)

Full Screen

Full Screen

mutateData

Using AI Code Generation

copy

Full Screen

1import "fmt"2func main() {3 p := prog{}4 p.mutateData(10)5 fmt.Println(p.data)6}7import "fmt"8type prog struct {9}10func (p prog) mutateData(d int) {11}12func main() {13 p := prog{}14 p.mutateData(10)15 fmt.Println(p.data)16}17import "fmt"18type prog struct {19}20func (p *prog) mutateData(d int) {21}22func main() {23 p := prog{}24 p.mutateData(10)25 fmt.Println(p.data)26}

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