How to use newRand method of prog Package

Best Syzkaller code snippet using prog.newRand

rand_test.go

Source:rand_test.go Github

copy

Full Screen

...8 "sort"9 "testing"10)11func TestNotEscaping(t *testing.T) {12 r := newRand(nil, rand.NewSource(0))13 s := &state{14 files: map[string]bool{"./file0": true},15 }16 bound := 100000017 if testing.Short() {18 bound = 100019 }20 for i := 0; i < bound; i++ {21 fn := r.filenameImpl(s)22 if escapingFilename(fn) {23 t.Errorf("sandbox escaping file name %q", fn)24 }25 }26}27func TestDeterminism(t *testing.T) {28 target, rs, iters := initTest(t)29 iters /= 10 // takes too long30 ct := target.DefaultChoiceTable()31 var corpus []*Prog32 for i := 0; i < iters; i++ {33 seed := rs.Int63()34 rs1 := rand.NewSource(seed)35 p1 := generateProg(t, target, rs1, ct, corpus)36 rs2 := rand.NewSource(seed)37 p2 := generateProg(t, target, rs2, ct, corpus)38 ps1 := string(p1.Serialize())39 ps2 := string(p2.Serialize())40 r1 := rs1.Int63()41 r2 := rs2.Int63()42 if r1 != r2 || ps1 != ps2 {43 t.Errorf("seed=%v\nprog 1 (%v):\n%v\nprog 2 (%v):\n%v", seed, r1, ps1, r2, ps2)44 }45 corpus = append(corpus, p1)46 }47}48func generateProg(t *testing.T, target *Target, rs rand.Source, ct *ChoiceTable, corpus []*Prog) *Prog {49 p := target.Generate(rs, 5, ct)50 p.Mutate(rs, 10, ct, corpus)51 for i, c := range p.Calls {52 comps := make(CompMap)53 for v := range extractValues(c) {54 comps.AddComp(v, v+1)55 comps.AddComp(v, v+10)56 }57 p.MutateWithHints(i, comps, func(p1 *Prog) {58 p = p1.Clone()59 })60 }61 for _, crash := range []bool{false, true} {62 p, _ = Minimize(p, -1, crash, func(*Prog, int) bool {63 return rs.Int63()%10 == 064 })65 }66 data := p.Serialize()67 var err error68 p, err = target.Deserialize(data, NonStrict)69 if err != nil {70 t.Fatal(err)71 }72 return p73}74// Checks that a generated program contains only enabled syscalls.75func TestEnabledCalls(t *testing.T) {76 target, rs, iters := initTest(t)77 enabledCalls := map[string]bool{"open": true, "read": true, "dup3": true, "write": true, "close": true}78 enabled := make(map[*Syscall]bool)79 for c := range enabledCalls {80 enabled[target.SyscallMap[c]] = true81 }82 ct := target.BuildChoiceTable(nil, enabled)83 const tries = 1084 for i := 0; i < tries; i++ {85 p := target.Generate(rs, 50, ct)86 for it := 0; it < iters/tries; it++ {87 p.Mutate(rs, 50, ct, nil)88 }89 for _, c := range p.Calls {90 if _, ok := enabledCalls[c.Meta.Name]; !ok {91 t.Fatalf("program contains a syscall that is not enabled: %v\n", c.Meta.Name)92 }93 }94 }95}96func TestSizeGenerateConstArg(t *testing.T) {97 target, rs, iters := initRandomTargetTest(t, "test", "64")98 r := newRand(target, rs)99 ForeachType(target.Syscalls, func(typ Type, ctx TypeCtx) {100 if _, ok := typ.(*IntType); !ok {101 return102 }103 bits := typ.TypeBitSize()104 limit := uint64(1<<bits - 1)105 for i := 0; i < iters; i++ {106 newArg, _ := typ.generate(r, nil, ctx.Dir)107 newVal := newArg.(*ConstArg).Val108 if newVal > limit {109 t.Fatalf("invalid generated value: %d. (arg bitsize: %d; max value: %d)", newVal, bits, limit)110 }111 }112 })113}114func TestFlags(t *testing.T) {115 // This test does not test anything, it just prints resulting116 // distribution of values for different scenarios.117 tests := []struct {118 vv []uint64119 bitmask bool120 old uint64121 }{122 {[]uint64{0, 1, 2, 3}, false, 0},123 {[]uint64{0, 1, 2, 3}, false, 2},124 {[]uint64{1, 2, 3, 4}, false, 0},125 {[]uint64{1, 2, 3, 4}, false, 2},126 {[]uint64{1, 2, 4, 8}, true, 0},127 {[]uint64{1, 2, 4, 8}, true, 2},128 {[]uint64{7}, false, 0},129 {[]uint64{7}, false, 7},130 {[]uint64{1, 2}, true, 0},131 {[]uint64{1, 2}, true, 2},132 }133 target, rs, _ := initRandomTargetTest(t, "test", "64")134 r := newRand(target, rs)135 for _, test := range tests {136 results := make(map[uint64]uint64)137 const throws = 1e4138 for i := 0; i < throws; i++ {139 var v uint64140 for {141 v = r.flags(test.vv, test.bitmask, test.old)142 if test.old == 0 || test.old != v {143 break144 }145 }146 if v > 100 {147 v = 999 // to not print all possible random values we generated148 }...

Full Screen

Full Screen

generation.go

Source:generation.go Github

copy

Full Screen

...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}...

Full Screen

Full Screen

newRand

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(rand.Intn(100))4 fmt.Println(rand.Intn(100))5 fmt.Println(rand.Intn(100))6 fmt.Println(rand.Intn(100))7}8import (9func main() {10 fmt.Print(time.Now().UnixNano(), ", ")11 fmt.Println(rand.Intn(100))12 fmt.Print(time.Now().UnixNano(), ", ")13 fmt.Println(rand.Intn(100))14 fmt.Print(time.Now().UnixNano(), ", ")15 fmt.Println(rand.Intn(100))16 fmt.Print(time.Now().UnixNano(), ", ")17 fmt.Println(rand.Intn(100))18}19import (20func main() {21 seed := time.Now().UnixNano()22 fmt.Println("Seed:", seed)23 rand.Seed(seed)24 fmt.Println("Random number:", rand.Intn(100))25 fmt.Println("Random number:", rand.Intn(100))

Full Screen

Full Screen

newRand

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(prog.newRand())4}5import (6func main() {7 fmt.Println(prog.newRand())8}9import (10func main() {11 fmt.Println(prog.newRand())12}13import (14func main() {15 fmt.Println(prog.newRand())16}17import (18func main() {19 fmt.Println(prog.newRand())20}21import (22func main() {23 fmt.Println(prog.newRand())24}25import (26func main() {27 fmt.Println(prog.newRand())28}29import (30func main() {31 fmt.Println(prog.newRand())32}33import (34func main() {35 fmt.Println(prog.newRand())36}37import (38func main() {39 fmt.Println(prog.newRand())40}41import (42func main() {43 fmt.Println(prog.newRand())44}45import (46func main() {47 fmt.Println(prog.newRand())48}49import (50func main() {51 fmt.Println(prog.newRand())52}53import (54func main() {55 fmt.Println(prog.newRand())56}57import (58func main() {59 fmt.Println(prog.newRand())60}61import (62func main() {63 fmt.Println(prog.newRand())64}65import (66func main() {67 fmt.Println(prog.newRand())68}69import (

Full Screen

Full Screen

newRand

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(rand.Intn(10))4}5import (6func main() {7 fmt.Println(rand.Intn(10))8}9In this example, we have created a package named prog. The package contains a newRand method that returns a random number. The package is imported in the main files. The main files use the newRand method of the prog package. The output of each file is different because the random number is generated

Full Screen

Full Screen

newRand

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(prog.newRand())4}5prog.newRand undefined (type prog has no field or method newRand)6import (7type prog struct {8}9func (p *prog) NewRand() int {10 return rand.Intn(100)11}12func main() {13 p := prog{}14 fmt.Println(p.NewRand())15}

Full Screen

Full Screen

newRand

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "math/rand"3func main() {4 p.newRand()5 fmt.Println(p.rand)6}7import "fmt"8import "math/rand"9type prog struct {10}11func (p prog) newRand() {12 p.rand = rand.Intn(100)13}14func main() {15 p.newRand()16 fmt.Println(p.rand)17}18import "fmt"19import "math/rand"20type prog struct {21}22func (p *prog) newRand() {23 p.rand = rand.Intn(100)24}25func main() {26 p.newRand()27 fmt.Println(p.rand)28}29import "fmt"30import "math/rand"31type prog struct {32}33func (p *prog) newRand() {34 p.rand = rand.Intn(100)35}36func main() {37 (&p).newRand()38 fmt.Println(p.rand)39}40import "fmt"41import "math/rand"42type prog struct {43}44func (p *prog) newRand() {45 p.rand = rand.Intn(100)46}47func main() {48 p.newRand()49 fmt.Println((&p).rand)50}51import "fmt"52import "math/rand"53type prog struct {54}55func (p *prog) newRand() {56 p.rand = rand.Intn(100)57}58func main() {59 p.newRand()60 fmt.Println(p.rand)61}62import "fmt"63import "math/rand"64type prog struct {65}66func (p prog) newRand() {67 p.rand = rand.Intn(100

Full Screen

Full Screen

newRand

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 r := rand.New(rand.NewSource(time.Now().UnixNano()))4 fmt.Println(r.Intn(100))5 fmt.Println(r.Intn(100))6 fmt.Println(r.Intn(100))7}8import (9func main() {10 r := rand.New(rand.NewSource(time.Now().UnixNano()))11 for i := 0; i < 10; i++ {12 fmt.Println(r.Intn(100))13 }14}15import (16func main() {17 r := rand.New(rand.NewSource(time.Now().UnixNano()))18 for i := 0; i < 10; i++ {19 fmt.Println(r.Intn(100))20 }21}22import (23func main() {24 r := rand.New(rand.NewSource(time.Now().UnixNano()))25 for i := 0; i < 10; i++ {26 fmt.Println(r.Intn(100))27 }28}29import (30func main() {

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