Best Syzkaller code snippet using prog.RemoveCall
mutation.go
Source:mutation.go
...84 p0c := p0.Clone()85 idx := r.Intn(len(p.Calls))86 p.Calls = append(p.Calls[:idx], append(p0c.Calls, p.Calls[idx:]...)...)87 for i := len(p.Calls) - 1; i >= ctx.ncalls; i-- {88 p.RemoveCall(i)89 }90 return true91}92func (ctx *mutator) squashAny() bool {93 p, r := ctx.p, ctx.r94 complexPtrs := p.complexPtrs()95 if len(complexPtrs) == 0 {96 return false97 }98 ptr := complexPtrs[r.Intn(len(complexPtrs))]99 if !p.Target.isAnyPtr(ptr.Type()) {100 p.Target.squashPtr(ptr, true)101 }102 var blobs []*DataArg103 var bases []*PointerArg104 ForeachSubArg(ptr, func(arg Arg, ctx *ArgCtx) {105 if data, ok := arg.(*DataArg); ok && arg.Type().Dir() != DirOut {106 blobs = append(blobs, data)107 bases = append(bases, ctx.Base)108 }109 })110 if len(blobs) == 0 {111 return false112 }113 // TODO(dvyukov): we probably want special mutation for ANY.114 // E.g. merging adjacent ANYBLOBs (we don't create them,115 // but they can appear in future); or replacing ANYRES116 // with a blob (and merging it with adjacent blobs).117 idx := r.Intn(len(blobs))118 arg := blobs[idx]119 base := bases[idx]120 baseSize := base.Res.Size()121 arg.data = mutateData(r, arg.Data(), 0, maxBlobLen)122 // Update base pointer if size has increased.123 if baseSize < base.Res.Size() {124 s := analyze(ctx.ct, p, p.Calls[0])125 newArg := r.allocAddr(s, base.Type(), base.Res.Size(), base.Res)126 *base = *newArg127 }128 return true129}130func (ctx *mutator) insertCall() bool {131 p, r := ctx.p, ctx.r132 if len(p.Calls) >= ctx.ncalls {133 return false134 }135 idx := r.biasedRand(len(p.Calls)+1, 5)136 var c *Call137 if idx < len(p.Calls) {138 c = p.Calls[idx]139 }140 s := analyze(ctx.ct, p, c)141 calls := r.generateCall(s, p)142 p.insertBefore(c, calls)143 return true144}145func (ctx *mutator) removeCall() bool {146 p, r := ctx.p, ctx.r147 if len(p.Calls) == 0 {148 return false149 }150 idx := r.Intn(len(p.Calls))151 p.RemoveCall(idx)152 return true153}154func (ctx *mutator) mutateArg() bool {155 p, r := ctx.p, ctx.r156 if len(p.Calls) == 0 {157 return false158 }159 c := p.Calls[r.Intn(len(p.Calls))]160 if len(c.Args) == 0 {161 return false162 }163 s := analyze(ctx.ct, p, c)164 updateSizes := true165 for stop, ok := false, false; !stop; stop = ok && r.oneOf(3) {166 ok = true167 ma := &mutationArgs{target: p.Target}168 ForeachArg(c, ma.collectArg)169 if len(ma.args) == 0 {170 return false171 }172 idx := r.Intn(len(ma.args))173 arg, ctx := ma.args[idx], ma.ctxes[idx]174 calls, ok1 := p.Target.mutateArg(r, s, arg, ctx, &updateSizes)175 if !ok1 {176 ok = false177 continue178 }179 p.insertBefore(c, calls)180 if updateSizes {181 p.Target.assignSizesCall(c)182 }183 p.Target.SanitizeCall(c)184 }185 return true186}187func (target *Target) mutateArgForDependencyResource(r *randGen, s *state, arg Arg, ctx ArgCtx, updateSizes *bool) ([]*Call, bool) {188 var baseSize uint64189 if ctx.Base != nil {190 baseSize = ctx.Base.Res.Size()191 }192 calls, retry, preserve := arg.Type().mutate(r, s, arg, ctx)193 if retry {194 return nil, false195 }196 if preserve {197 *updateSizes = false198 }199 // Update base pointer if size has increased.200 if base := ctx.Base; base != nil && baseSize < base.Res.Size() {201 newArg := r.allocAddr(s, base.Type(), base.Res.Size(), base.Res)202 replaceArg(base, newArg)203 }204 for _, c := range calls {205 target.SanitizeCall(c)206 }207 return calls, true208}209func (target *Target) mutateArg(r *randGen, s *state, arg Arg, ctx ArgCtx, updateSizes *bool) ([]*Call, bool) {210 var baseSize uint64211 if ctx.Base != nil {212 baseSize = ctx.Base.Res.Size()213 }214 calls, retry, preserve := arg.Type().mutate(r, s, arg, ctx)215 if retry {216 return nil, false217 }218 if preserve {219 *updateSizes = false220 }221 // Update base pointer if size has increased.222 if base := ctx.Base; base != nil && baseSize < base.Res.Size() {223 newArg := r.allocAddr(s, base.Type(), base.Res.Size(), base.Res)224 replaceArg(base, newArg)225 }226 for _, c := range calls {227 target.SanitizeCall(c)228 }229 return calls, true230}231func regenerate(r *randGen, s *state, arg Arg) (calls []*Call, retry, preserve bool) {232 var newArg Arg233 newArg, calls = r.generateArg(s, arg.Type())234 replaceArg(arg, newArg)235 return236}237func mutateInt(r *randGen, s *state, arg Arg) (calls []*Call, retry, preserve bool) {238 if r.bin() {239 return regenerate(r, s, arg)240 }241 a := arg.(*ConstArg)242 switch {243 case r.nOutOf(1, 3):244 a.Val += uint64(r.Intn(4)) + 1245 case r.nOutOf(1, 2):246 a.Val -= uint64(r.Intn(4)) + 1247 default:248 a.Val ^= 1 << uint64(r.Intn(64))249 }250 return251}252func (t *IntType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {253 return mutateInt(r, s, arg)254}255func (t *FlagsType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {256 return mutateInt(r, s, arg)257}258func (t *LenType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {259 if !r.mutateSize(arg.(*ConstArg), *ctx.Parent) {260 retry = true261 return262 }263 preserve = true264 return265}266func (t *ResourceType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {267 return regenerate(r, s, arg)268}269func (t *VmaType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {270 return regenerate(r, s, arg)271}272func (t *ProcType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {273 return regenerate(r, s, arg)274}275func (t *BufferType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {276 a := arg.(*DataArg)277 switch t.Kind {278 case BufferBlobRand, BufferBlobRange:279 data := append([]byte{}, a.Data()...)280 minLen, maxLen := uint64(0), maxBlobLen281 if t.Kind == BufferBlobRange {282 minLen, maxLen = t.RangeBegin, t.RangeEnd283 }284 a.data = mutateData(r, data, minLen, maxLen)285 case BufferString:286 data := append([]byte{}, a.Data()...)287 if r.bin() {288 minLen, maxLen := uint64(0), maxBlobLen289 if t.TypeSize != 0 {290 minLen, maxLen = t.TypeSize, t.TypeSize291 }292 a.data = mutateData(r, data, minLen, maxLen)293 } else {294 a.data = r.randString(s, t)295 }296 case BufferFilename:297 a.data = []byte(r.filename(s, t))298 case BufferText:299 data := append([]byte{}, a.Data()...)300 a.data = r.mutateText(t.Text, data)301 default:302 panic("unknown buffer kind")303 }304 return305}306func (t *ArrayType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {307 // TODO: swap elements of the array308 a := arg.(*GroupArg)309 count := uint64(0)310 switch t.Kind {311 case ArrayRandLen:312 for count == uint64(len(a.Inner)) {313 count = r.randArrayLen()314 }315 case ArrayRangeLen:316 if t.RangeBegin == t.RangeEnd {317 panic("trying to mutate fixed length array")318 }319 for count == uint64(len(a.Inner)) {320 count = r.randRange(t.RangeBegin, t.RangeEnd)321 }322 }323 if count > uint64(len(a.Inner)) {324 for count > uint64(len(a.Inner)) {325 newArg, newCalls := r.generateArg(s, t.Type)326 a.Inner = append(a.Inner, newArg)327 calls = append(calls, newCalls...)328 for _, c := range newCalls {329 s.analyze(c)330 }331 }332 } else if count < uint64(len(a.Inner)) {333 for _, arg := range a.Inner[count:] {334 removeArg(arg)335 }336 a.Inner = a.Inner[:count]337 }338 return339}340func (t *PtrType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {341 a := arg.(*PointerArg)342 if r.oneOf(1000) {343 removeArg(a.Res)344 index := r.rand(len(r.target.SpecialPointers))345 newArg := MakeSpecialPointerArg(t, index)346 replaceArg(arg, newArg)347 return348 }349 newArg := r.allocAddr(s, t, a.Res.Size(), a.Res)350 replaceArg(arg, newArg)351 return352}353func (t *StructType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {354 gen := r.target.SpecialTypes[t.Name()]355 if gen == nil {356 panic("bad arg returned by mutationArgs: StructType")357 }358 var newArg Arg359 newArg, calls = gen(&Gen{r, s}, t, arg)360 a := arg.(*GroupArg)361 for i, f := range newArg.(*GroupArg).Inner {362 replaceArg(a.Inner[i], f)363 }364 return365}366func (t *UnionType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {367 if gen := r.target.SpecialTypes[t.Name()]; gen != nil {368 var newArg Arg369 newArg, calls = gen(&Gen{r, s}, t, arg)370 replaceArg(arg, newArg)371 } else {372 a := arg.(*UnionArg)373 current := -1374 for i, option := range t.Fields {375 if a.Option.Type().FieldName() == option.FieldName() {376 current = i377 break378 }379 }380 if current == -1 {381 panic("can't find current option in union")382 }383 newIdx := r.Intn(len(t.Fields) - 1)384 if newIdx >= current {385 newIdx++386 }387 optType := t.Fields[newIdx]388 removeArg(a.Option)389 var newOpt Arg390 newOpt, calls = r.generateArg(s, optType)391 replaceArg(arg, MakeUnionArg(t, newOpt))392 }393 return394}395func (t *CsumType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {396 panic("CsumType can't be mutated")397}398func (t *ConstType) mutate(r *randGen, s *state, arg Arg, ctx ArgCtx) (calls []*Call, retry, preserve bool) {399 panic("ConstType can't be mutated")400}401type mutationArgs struct {402 target *Target403 args []Arg404 ctxes []ArgCtx405 ignoreSpecial bool406}407func (ma *mutationArgs) collectArg(arg Arg, ctx *ArgCtx) {408 ignoreSpecial := ma.ignoreSpecial409 ma.ignoreSpecial = false410 switch typ := arg.Type().(type) {411 case *StructType:412 if ma.target.SpecialTypes[typ.Name()] == nil || ignoreSpecial {413 return // For structs only individual fields are updated.414 }415 // These special structs are mutated as a whole.416 ctx.Stop = true417 case *UnionType:418 if ma.target.SpecialTypes[typ.Name()] == nil && len(typ.Fields) == 1 || ignoreSpecial {419 return420 }421 ctx.Stop = true422 case *ArrayType:423 // Don't mutate fixed-size arrays.424 if typ.Kind == ArrayRangeLen && typ.RangeBegin == typ.RangeEnd {425 return426 }427 case *CsumType:428 return // Checksum is updated when the checksummed data changes.429 case *ConstType:430 return // Well, this is const.431 case *BufferType:432 if typ.Kind == BufferString && len(typ.Values) == 1 {433 return // string const434 }435 case *PtrType:436 if arg.(*PointerArg).IsSpecial() {437 // TODO: we ought to mutate this, but we don't have code for this yet.438 return439 }440 }441 typ := arg.Type()442 if typ == nil || typ.Dir() == DirOut || !typ.Varlen() && typ.Size() == 0 {443 return444 }445 ma.args = append(ma.args, arg)446 ma.ctxes = append(ma.ctxes, *ctx)447}448func mutateData(r *randGen, data []byte, minLen, maxLen uint64) []byte {449 for stop := false; !stop; stop = stop && r.oneOf(3) {450 f := mutateDataFuncs[r.Intn(len(mutateDataFuncs))]451 data, stop = f(r, data, minLen, maxLen)452 }453 return data454}455const maxInc = 35456var mutateDataFuncs = [...]func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool){457 // TODO(dvyukov): duplicate part of data.458 // Flip bit in byte.459 func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {460 if len(data) == 0 {461 return data, false462 }463 byt := r.Intn(len(data))464 bit := r.Intn(8)465 data[byt] ^= 1 << uint(bit)466 return data, true467 },468 // Insert random bytes.469 func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {470 if len(data) == 0 {471 return data, false472 }473 n := r.Intn(16) + 1474 if r := int(maxLen) - len(data); n > r {475 n = r476 }477 pos := r.Intn(len(data))478 for i := 0; i < n; i++ {479 data = append(data, 0)480 }481 copy(data[pos+n:], data[pos:])482 for i := 0; i < n; i++ {483 data[pos+i] = byte(r.Int31())484 }485 if uint64(len(data)) > maxLen || r.bin() {486 data = data[:len(data)-n] // preserve original length487 }488 return data, true489 },490 // Remove bytes.491 func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {492 if len(data) == 0 {493 return data, false494 }495 n := r.Intn(16) + 1496 if n > len(data) {497 n = len(data)498 }499 pos := 0500 if n < len(data) {501 pos = r.Intn(len(data) - n)502 }503 copy(data[pos:], data[pos+n:])504 data = data[:len(data)-n]505 if uint64(len(data)) < minLen || r.bin() {506 for i := 0; i < n; i++ {507 data = append(data, 0) // preserve original length508 }509 }510 return data, true511 },512 // Append a bunch of bytes.513 func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {514 if uint64(len(data)) >= maxLen {515 return data, false516 }517 const max = 256518 n := max - r.biasedRand(max, 10)519 if r := int(maxLen) - len(data); n > r {520 n = r521 }522 for i := 0; i < n; i++ {523 data = append(data, byte(r.rand(256)))524 }525 return data, true526 },527 // Replace int8/int16/int32/int64 with a random value.528 func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {529 width := 1 << uint(r.Intn(4))530 if len(data) < width {531 return data, false532 }533 i := r.Intn(len(data) - width + 1)534 storeInt(data[i:], r.Uint64(), width)535 return data, true536 },537 // Add/subtract from an int8/int16/int32/int64.538 func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {539 width := 1 << uint(r.Intn(4))540 if len(data) < width {541 return data, false542 }543 i := r.Intn(len(data) - width + 1)544 v := loadInt(data[i:], width)545 delta := r.rand(2*maxInc+1) - maxInc546 if delta == 0 {547 delta = 1548 }549 if r.oneOf(10) {550 v = swapInt(v, width)551 v += delta552 v = swapInt(v, width)553 } else {554 v += delta555 }556 storeInt(data[i:], v, width)557 return data, true558 },559 // Set int8/int16/int32/int64 to an interesting value.560 func(r *randGen, data []byte, minLen, maxLen uint64) ([]byte, bool) {561 width := 1 << uint(r.Intn(4))562 if len(data) < width {563 return data, false564 }565 i := r.Intn(len(data) - width + 1)566 value := r.randInt()567 if r.oneOf(10) {568 value = swap64(value)569 }570 storeInt(data[i:], value, width)571 return data, true572 },573}574func swap16(v uint16) uint16 {575 v0 := byte(v >> 0)576 v1 := byte(v >> 8)577 v = 0578 v |= uint16(v1) << 0579 v |= uint16(v0) << 8580 return v581}582func swap32(v uint32) uint32 {583 v0 := byte(v >> 0)584 v1 := byte(v >> 8)585 v2 := byte(v >> 16)586 v3 := byte(v >> 24)587 v = 0588 v |= uint32(v3) << 0589 v |= uint32(v2) << 8590 v |= uint32(v1) << 16591 v |= uint32(v0) << 24592 return v593}594func swap64(v uint64) uint64 {595 v0 := byte(v >> 0)596 v1 := byte(v >> 8)597 v2 := byte(v >> 16)598 v3 := byte(v >> 24)599 v4 := byte(v >> 32)600 v5 := byte(v >> 40)601 v6 := byte(v >> 48)602 v7 := byte(v >> 56)603 v = 0604 v |= uint64(v7) << 0605 v |= uint64(v6) << 8606 v |= uint64(v5) << 16607 v |= uint64(v4) << 24608 v |= uint64(v3) << 32609 v |= uint64(v2) << 40610 v |= uint64(v1) << 48611 v |= uint64(v0) << 56612 return v613}614func swapInt(v uint64, size int) uint64 {615 switch size {616 case 1:617 return v618 case 2:619 return uint64(swap16(uint16(v)))620 case 4:621 return uint64(swap32(uint32(v)))622 case 8:623 return swap64(v)624 default:625 panic(fmt.Sprintf("swapInt: bad size %v", size))626 }627}628func loadInt(data []byte, size int) uint64 {629 p := unsafe.Pointer(&data[0])630 switch size {631 case 1:632 return uint64(*(*uint8)(p))633 case 2:634 return uint64(*(*uint16)(p))635 case 4:636 return uint64(*(*uint32)(p))637 case 8:638 return *(*uint64)(p)639 default:640 panic(fmt.Sprintf("loadInt: bad size %v", size))641 }642}643func storeInt(data []byte, v uint64, size int) {644 p := unsafe.Pointer(&data[0])645 switch size {646 case 1:647 *(*uint8)(p) = uint8(v)648 case 2:649 *(*uint16)(p) = uint16(v)650 case 4:651 *(*uint32)(p) = uint32(v)652 case 8:653 *(*uint64)(p) = v654 default:655 panic(fmt.Sprintf("storeInt: bad size %v", size))656 }657}658func (p *Prog) Splice(rp *Prog, idx uint32, ncalls int) bool {659 p0c := rp.Clone()660 p.Calls = append(p.Calls[:idx], append(p0c.Calls, p.Calls[idx:]...)...)661 for i := len(p.Calls) - 1; i >= ncalls; i-- {662 p.RemoveCall(i)663 }664 return true665}666func (p *Prog) MutateIoctl1Arg(rs rand.Source, idx int, ct *ChoiceTable) bool {667 r := newRand(p.Target, rs)668 c := p.Calls[idx]669 if len(c.Args) == 0 {670 return false671 }672 s := analyze(ct, p, c)673 updateSizes := true674 for stop, ok := false, false; !stop; stop = ok && r.oneOf(3) {675 ok = true676 ma := &mutationArgs{target: p.Target}677 ForeachArg(c, ma.collectArg)678 log.Logf(1, "len(ma.args) : %v", len(ma.args))679 var idx int680 if len(ma.args) == 0 {681 return false682 } else {683 idx = 0684 }685 arg, ctx := ma.args[idx], ma.ctxes[idx]686 calls, ok1 := p.Target.mutateArgForDependencyResource(r, s, arg, ctx, &updateSizes)687 if !ok1 {688 ok = false689 continue690 }691 p.insertBefore(c, calls)692 if updateSizes {693 p.Target.assignSizesCall(c)694 }695 p.Target.SanitizeCall(c)696 }697 return true698}699func (p *Prog) MutateIoctl3Arg(rs rand.Source, idx int, ct *ChoiceTable) bool {700 r := newRand(p.Target, rs)701 c := p.Calls[idx]702 if len(c.Args) == 0 {703 return false704 }705 s := analyze(ct, p, c)706 updateSizes := true707 for stop, ok := false, false; !stop; stop = ok && r.oneOf(4) {708 ok = true709 ma := &mutationArgs{target: p.Target}710 ForeachArg(c, ma.collectArg)711 log.Logf(1, "len(ma.args) : %v", len(ma.args))712 var idx int713 if len(ma.args) == 0 || len(ma.args) == 1 {714 return false715 } else {716 idx = r.Intn(len(ma.args)-1) + 1717 }718 arg, ctx := ma.args[idx], ma.ctxes[idx]719 calls, ok1 := p.Target.mutateArg(r, s, arg, ctx, &updateSizes)720 if !ok1 {721 ok = false722 continue723 }724 p.insertBefore(c, calls)725 if updateSizes {726 p.Target.assignSizesCall(c)727 }728 p.Target.SanitizeCall(c)729 }730 return true731}732func (p *Prog) GetCall(rs rand.Source, meta *Syscall, idx uint32, ct *ChoiceTable) []*Call {733 r := newRand(p.Target, rs)734 c := p.Calls[idx]735 s := analyze(ct, p, c)736 c0c := r.generateParticularCall(s, meta)737 var cc *Call738 if len(c0c) == 2 {739 cc = c0c[1]740 } else if len(c0c) == 1 {741 cc = c0c[0]742 } else {743 log.Fatalf("GetCall more than 2")744 }745 for i, arg := range cc.Args {746 switch arg.(type) {747 case *ResultArg:748 cc.Args[i] = c.Args[i]749 default:750 }751 }752 var rcc []*Call753 rcc = append(rcc, cc)754 return c0c755}756func (p *Prog) InsertCall(c0c []*Call, idx uint32, ncalls int) {757 c := p.Calls[idx]758 p.insertBefore(c, c0c)759 for i := len(p.Calls) - 1; i >= ncalls; i-- {760 p.RemoveCall(i)761 }762}763func (p *Prog) RepeatCall(idx int) {764 c := p.Calls[idx]765 //newargs := make(map[*ResultArg]*ResultArg)766 //c1 := new(Call)767 //c1.Meta = c.Meta768 //if c.Ret != nil {769 // c1.Ret = clone(c.Ret, newargs).(*ResultArg)770 //}771 //c1.Args = make([]Arg, len(c.Args))772 //for ai, arg := range c.Args {773 // c1.Args[ai] = clone(arg, newargs)774 //}...
generation.go
Source:generation.go
1// Copyright 2021, Developed by Tsinghua Wingtecher Lab and Shumuyulin Ltd, All rights reserved.2// Copyright 2015 syzkaller project authors. All rights reserved.3// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.4package prog5import (6 "math/rand"7)8// Generate generates a random program with ncalls calls.9// ct contains a set of allowed syscalls, if nil all syscalls are used.10func (target *Target) Generate(rs rand.Source, ncalls int, ct *ChoiceTable) *Prog {11 p := &Prog{12 Target: target,13 }14 r := newRand(target, rs)15 s := newState(target, ct, nil)16 for len(p.Calls) < ncalls {17 calls := r.generateCall(s, p, len(p.Calls))18 for _, c := range calls {19 s.analyze(c)20 p.Calls = append(p.Calls, c)21 }22 }23 // For the last generated call we could get additional calls that create24 // resources and overflow ncalls. Remove some of these calls.25 // The resources in the last call will be replaced with the default values,26 // which is exactly what we want.27 for len(p.Calls) > ncalls {28 p.removeCall(ncalls - 1)29 }30 p.sanitizeFix()31 p.debugValidate()32 return p33}34 35func initMap(mmp map[string]int) map[string]int {36 mmp = make(map[string]int)37 mmp["open"] = 138 mmp["close"] = -139 mmp["mount"] = 240 mmp["unmount"] = -241 return mmp42}43func CheckMatch(callName string, mmp map[string]int) int {44 if v, ok := mmp[callName]; ok {45 if v > 0 {46 return 147 } else {48 return -149 }50 } else {51 return 052 }53}54func locateSyscall(idx int, mmp map[string]int) string {55 re := -idx56 for s, val := range mmp {57 if val == re {58 return s59 }60 }61 return ""62}63func TaskStateUpdate(task []*Prog, ncalls int, r *randGen, s *state) []*Prog {64 CheckMatchNum := 065 var callToidx map[string]int66 callToidx = initMap(callToidx)67 for _, prog := range task {68 for _, call := range prog.Calls {69 isExist := CheckMatch(call.Meta.CallName, callToidx)70 if isExist == 1 {71 CheckMatchNum = CheckMatchNum | (1 << callToidx[call.Meta.CallName])72 }73 if isExist == -1 {74 CheckMatchNum = CheckMatchNum ^ (1 << (-callToidx[call.Meta.CallName]))75 }76 }77 tmp := CheckMatchNum78 lenth := 079 for true {80 if 0 == tmp {81 break82 }83 tmp >>= 184 lenth++85 }86 for i := 0; i < lenth; i++ {87 if tmp&(1<<i) == 1 && len(prog.Calls) < ncalls {88 CallName := locateSyscall(i, callToidx)89 call := r.TaskgenerateParticularCall(s, CallName)90 prog.Calls = append(prog.Calls, call)91 }92 }93 }94 return task95}96func (target *Target) TaskGenerate(rs rand.Source, ncalls int, ct *ChoiceTable) []*Prog {97 rand := rand.New(rs)98 taskLen := rand.Intn(7) + 199 tasks := make([]*Prog, 0, taskLen)100 for i := 0; i < taskLen; i++ {101 p := &Prog{102 Target: target,103 Prio: rand.Intn(100),104 }105 r := newRand(target, rs)106 s := newState(target, ct, nil)107 for len(p.Calls) < ncalls {108 calls := r.generateCall(s, p, len(p.Calls))109 for _, c := range calls {110 s.analyze(c)111 p.Calls = append(p.Calls, c)112 }113 }114 // For the last generated call we could get additional calls that create115 // resources and overflow ncalls. Remove some of these calls.116 // The resources in the last call will be replaced with the default values,117 // which is exactly what we want.118 for len(p.Calls) > ncalls {119 p.removeCall(ncalls - 1)120 }121 p.sanitizeFix()122 p.debugValidate()123 tasks = append(tasks, p)124 }125 r := newRand(target, rs)126 s := newState(target, ct, nil)127 tasks = TaskStateUpdate(tasks, taskLen, r, s)128 return tasks129}...
RemoveCall
Using AI Code Generation
1import (2func main() {3 prog := runtime.NewProgram(common.HexToAddress("0x123"), common.HexToAddress("0x456"), common.HexToAddress("0x789"), params.TestChainConfig, nil, nil, nil, nil)4 prog.AddCall(common.HexToAddress("0x123"), common.HexToAddress("0x456"), common.HexToAddress("0x789"), common.HexToAddress("0xabc"), new(big.Int), new(big.Int), []byte{}, 0, 0, 0, 0, 0, 0, false, false, false, false, false)5 prog.RemoveCall(0)6}
RemoveCall
Using AI Code Generation
1import (2func main() {3 db := ethdb.NewMemDatabase()4 contract := vm.NewContract(common.Address{}, common.Address{}, big.NewInt(0), 1000000)5 prog := vm.NewProgram(contract, db)6 stack := vm.NewStack()7 stack.Push(new(big.Int).SetInt64(100))8 stack.Push(new(big.Int).SetInt64(200))9 mem := vm.NewMemory()10 mem.Set(0, []byte{0x01, 0x02, 0x03, 0x04})11 call := vm.NewCall(vm.ADDRESS, big.NewInt(0), 10000, nil, 0, 0)12 prog.Stack().Push(call)13 prog.SetStack(stack)14 prog.SetMemory(mem)15 prog.SetProgram([]byte{0x60, 0x01, 0x60, 0x02, 0x60, 0x03})16 prog.RemoveCall()17 fmt.Println("Stack", prog.Stack())18 fmt.Println("Memory", prog.Memory())19 fmt.Println("Program", prog.Program())20}21Stack {100 200}
RemoveCall
Using AI Code Generation
1import (2func main() {3 prog := js.Global.Get("prog")4 prog.Call("RemoveCall")5}6var prog = {7 RemoveCall : function() {8 var element = document.getElementById('mydiv');9 element.parentNode.removeChild(element);10 }11};12func (o *Object) Call(m string, args ...interface{}) *Object13import (14func main() {15 prog := js.Global.Get("prog")16 prog.Call("RemoveCall")17}18var prog = {19 RemoveCall : function() {20 var element = document.getElementById('mydiv');21 element.parentNode.removeChild(element);22 }23};
RemoveCall
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, playground")4 prog := NewProgram()5 prog.AddCall("a", "b", 10)6 prog.AddCall("b", "c", 20)7 prog.RemoveCall("a", "b")8 fmt.Println(prog.GetCall("a", "b"))9 fmt.Println(prog.GetCall("a", "c"))10}
RemoveCall
Using AI Code Generation
1import "fmt"2type Prog struct {3}4func (p *Prog) RemoveCall() {5 fmt.Println("Remove Call method of Prog class")6}7func main() {8 p := Prog{"Go"}9 p.RemoveCall()10}
RemoveCall
Using AI Code Generation
1import "fmt"2func main() {3 p := prog{1, 2, 3, 4, 5}4 fmt.Println("Before RemoveCall:", p)5 p.RemoveCall(2)6 fmt.Println("After RemoveCall:", p)7}8import "fmt"9func main() {10 p := prog{1, 2, 3, 4, 5}11 fmt.Println("Before RemoveCall:", p)12 p.RemoveCall(3)13 fmt.Println("After RemoveCall:", p)14}15import "fmt"16func main() {17 p := prog{1, 2, 3, 4, 5}18 fmt.Println("Before RemoveCall:", p)19 p.RemoveCall(4)20 fmt.Println("After RemoveCall:", p)21}22import "fmt"23func main() {24 p := prog{1, 2, 3, 4, 5}25 fmt.Println("Before RemoveCall:", p)26 p.RemoveCall(5)27 fmt.Println("After RemoveCall:", p)28}29import "fmt"30func main() {31 p := prog{1, 2, 3, 4, 5}32 fmt.Println("Before RemoveCall:", p)33 p.RemoveCall(6)34 fmt.Println("After RemoveCall:", p)35}36import "fmt"37func main() {38 p := prog{1, 2, 3, 4, 5}39 fmt.Println("Before RemoveCall:", p)40 p.RemoveCall(7)41 fmt.Println("After RemoveCall:", p)42}43import "fmt"44func main() {45 p := prog{1, 2, 3, 4, 5}46 fmt.Println("Before RemoveCall:", p)
RemoveCall
Using AI Code Generation
1import "fmt"2func main() {3 fmt.Println("Hello, playground")4 p.RemoveCall(2)5}6type prog struct {7}8func (p prog) RemoveCall(i int) {9 println("RemoveCall called")10}11I am new to Go and trying to understand how to use packages. I am able to use the package by importing it in the main file. But I am not able to use the method of the class in the package. I am getting the error "undefined: prog". Can someone please help me understand where I am going wrong?12import "net/http"13func main() {14 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {15 w.Write([]byte("Hello, World!"))16 })17 http.ListenAndServe(":8080", nil)18}19I am trying to use the following code to create a simple web server in Go. It works fine when I run it locally. But when I try to run it on the Go Playground, I am getting the error "undefined: http.ListenAndServe". Can someone please help me understand what I am doing wrong? package main import "net/http" func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, World!")) }) http.ListenAndServe(":8080", nil
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!!