How to use serialize method of prog Package

Best Syzkaller code snippet using prog.serialize

prog_test.go

Source:prog_test.go Github

copy

Full Screen

...32 continue33 }34 // Ensure that we can restore all arguments of all calls.35 prog := fmt.Sprintf("%v()", meta.Name)36 p, err := target.Deserialize([]byte(prog), NonStrict)37 if err != nil {38 t.Fatalf("failed to restore default args in prog %q: %v", prog, err)39 }40 if len(p.Calls) != 1 || p.Calls[0].Meta.Name != meta.Name {41 t.Fatalf("restored bad program from prog %q: %q", prog, p.Serialize())42 }43 }44}45func testSerialize(t *testing.T, verbose bool) {46 target, rs, iters := initTest(t)47 ct := target.DefaultChoiceTable()48 for i := 0; i < iters; i++ {49 p := target.Generate(rs, 10, ct)50 var data []byte51 mode := NonStrict52 if verbose {53 data = p.SerializeVerbose()54 mode = Strict55 } else {56 data = p.Serialize()57 }58 p1, err := target.Deserialize(data, mode)59 if err != nil {60 t.Fatalf("failed to deserialize program: %v\n%s", err, data)61 }62 if p1 == nil {63 t.Fatalf("deserialized nil program:\n%s", data)64 }65 var data1 []byte66 if verbose {67 data1 = p1.SerializeVerbose()68 } else {69 data1 = p1.Serialize()70 }71 if len(p.Calls) != len(p1.Calls) {72 t.Fatalf("different number of calls")73 }74 if !bytes.Equal(data, data1) {75 t.Fatalf("program changed after serialize/deserialize\noriginal:\n%s\n\nnew:\n%s\n", data, data1)76 }77 }78}79func TestSerialize(t *testing.T) {80 testSerialize(t, false)81}82func TestSerializeVerbose(t *testing.T) {83 testSerialize(t, true)84}85func TestVmaType(t *testing.T) {86 target, rs, iters := initRandomTargetTest(t, "test", "64")87 ct := target.DefaultChoiceTable()88 meta := target.SyscallMap["test$vma0"]89 r := newRand(target, rs)90 pageSize := target.PageSize91 for i := 0; i < iters; i++ {92 s := newState(target, ct, nil)93 calls := r.generateParticularCall(s, meta)94 c := calls[len(calls)-1]95 if c.Meta.Name != "test$vma0" {96 t.Fatalf("generated wrong call %v", c.Meta.Name)97 }98 if len(c.Args) != 6 {99 t.Fatalf("generated wrong number of args %v", len(c.Args))100 }101 check := func(v, l Arg, min, max uint64) {102 va, ok := v.(*PointerArg)103 if !ok {104 t.Fatalf("vma has bad type: %v", v)105 }106 la, ok := l.(*ConstArg)107 if !ok {108 t.Fatalf("len has bad type: %v", l)109 }110 if va.VmaSize < min || va.VmaSize > max {111 t.Fatalf("vma has bad size: %v, want [%v-%v]",112 va.VmaSize, min, max)113 }114 if la.Val < min || la.Val > max {115 t.Fatalf("len has bad value: %v, want [%v-%v]",116 la.Val, min, max)117 }118 }119 check(c.Args[0], c.Args[1], 1*pageSize, 1e5*pageSize)120 check(c.Args[2], c.Args[3], 5*pageSize, 5*pageSize)121 check(c.Args[4], c.Args[5], 7*pageSize, 9*pageSize)122 }123}124// TestCrossTarget ensures that a program serialized for one arch can be125// deserialized for another arch. This happens when managers exchange126// programs via hub.127func TestCrossTarget(t *testing.T) {128 t.Parallel()129 const OS = "linux"130 var archs []string131 for _, target := range AllTargets() {132 if target.OS == OS {133 archs = append(archs, target.Arch)134 }135 }136 for _, arch := range archs {137 target, err := GetTarget(OS, arch)138 if err != nil {139 t.Fatal(err)140 }141 var crossTargets []*Target142 for _, crossArch := range archs {143 if crossArch == arch {144 continue145 }146 crossTarget, err := GetTarget(OS, crossArch)147 if err != nil {148 t.Fatal(err)149 }150 crossTargets = append(crossTargets, crossTarget)151 }152 t.Run(fmt.Sprintf("%v/%v", OS, arch), func(t *testing.T) {153 t.Parallel()154 testCrossTarget(t, target, crossTargets)155 })156 }157}158func testCrossTarget(t *testing.T, target *Target, crossTargets []*Target) {159 ct := target.DefaultChoiceTable()160 rs := randSource(t)161 iters := 100162 if testing.Short() {163 iters /= 10164 }165 for i := 0; i < iters; i++ {166 p := target.Generate(rs, 20, ct)167 testCrossArchProg(t, p, crossTargets)168 p, err := target.Deserialize(p.Serialize(), NonStrict)169 if err != nil {170 t.Fatal(err)171 }172 testCrossArchProg(t, p, crossTargets)173 p.Mutate(rs, 20, ct, nil)174 testCrossArchProg(t, p, crossTargets)175 p, _ = Minimize(p, -1, false, func(*Prog, int) bool {176 return rs.Int63()%2 == 0177 })178 testCrossArchProg(t, p, crossTargets)179 }180}181func testCrossArchProg(t *testing.T, p *Prog, crossTargets []*Target) {182 serialized := p.Serialize()183 for _, crossTarget := range crossTargets {184 _, err := crossTarget.Deserialize(serialized, NonStrict)185 if err == nil || strings.Contains(err.Error(), "unknown syscall") {186 continue187 }188 t.Fatalf("failed to deserialize for %v/%v: %v\n%s",189 crossTarget.OS, crossTarget.Arch, err, serialized)190 }191}192func TestSpecialStructs(t *testing.T) {193 testEachTargetRandom(t, func(t *testing.T, target *Target, rs rand.Source, iters int) {194 _ = target.GenerateAllSyzProg(rs)195 ct := target.DefaultChoiceTable()196 for special, gen := range target.SpecialTypes {197 t.Run(special, func(t *testing.T) {198 var typ Type199 for i := 0; i < len(target.Syscalls) && typ == nil; i++ {200 ForeachCallType(target.Syscalls[i], func(t Type, ctx TypeCtx) {201 if ctx.Dir == DirOut {202 return203 }204 if s, ok := t.(*StructType); ok && s.Name() == special {205 typ = s206 }207 if s, ok := t.(*UnionType); ok && s.Name() == special {208 typ = s209 }210 })211 }212 if typ == nil {213 t.Fatal("can't find struct description")214 }215 g := &Gen{newRand(target, rs), newState(target, ct, nil)}216 for i := 0; i < iters/len(target.SpecialTypes); i++ {217 var arg Arg218 for i := 0; i < 2; i++ {219 arg, _ = gen(g, typ, DirInOut, arg)220 if arg.Dir() != DirInOut {221 t.Fatalf("got wrong arg dir %v", arg.Dir())222 }223 }224 }225 })226 }227 })228}229func TestEscapingPaths(t *testing.T) {230 paths := map[string]bool{231 "/": true,232 "/\x00": true,233 "/file/..": true,234 "/file/../..": true,235 "./..": true,236 "..": true,237 "file/../../file": true,238 "../file": true,239 "./file/../../file/file": true,240 "": false,241 ".": false,242 "file": false,243 "./file": false,244 "./file/..": false,245 }246 for path, want := range paths {247 got := escapingFilename(path)248 if got != want {249 t.Errorf("path %q: got %v, want %v", path, got, want)250 }251 }252}253func TestFallbackSignal(t *testing.T) {254 type desc struct {255 prog string256 info []CallInfo257 }258 tests := []desc{259 // Test restored errno values and that non-executed syscalls don't get fallback signal.260 {261 `262fallback$0()263fallback$0()264fallback$0()265`,266 []CallInfo{267 {268 Flags: CallExecuted,269 Errno: 0,270 Signal: make([]uint32, 1),271 },272 {273 Flags: CallExecuted,274 Errno: 42,275 Signal: make([]uint32, 1),276 },277 {},278 },279 },280 // Test different cases of argument-dependent signal and that unsuccessful calls don't get it.281 {282 `283r0 = fallback$0()284fallback$1(r0)285fallback$1(r0)286fallback$1(0xffffffffffffffff)287fallback$1(0x0)288fallback$1(0x0)289`,290 []CallInfo{291 {292 Flags: CallExecuted,293 Errno: 0,294 Signal: make([]uint32, 1),295 },296 {297 Flags: CallExecuted,298 Errno: 1,299 Signal: make([]uint32, 1),300 },301 {302 Flags: CallExecuted,303 Errno: 0,304 Signal: make([]uint32, 2),305 },306 {307 Flags: CallExecuted,308 Errno: 0,309 Signal: make([]uint32, 1),310 },311 {312 Flags: CallExecuted,313 Errno: 0,314 Signal: make([]uint32, 2),315 },316 {317 Flags: CallExecuted,318 Errno: 2,319 Signal: make([]uint32, 1),320 },321 },322 },323 // Test that calls get no signal after a successful seccomp.324 {325 `326fallback$0()327fallback$0()328breaks_returns()329fallback$0()330breaks_returns()331fallback$0()332fallback$0()333`,334 []CallInfo{335 {336 Flags: CallExecuted,337 Errno: 0,338 Signal: make([]uint32, 1),339 },340 {341 Flags: CallExecuted,342 Errno: 0,343 Signal: make([]uint32, 1),344 },345 {346 Flags: CallExecuted,347 Errno: 1,348 Signal: make([]uint32, 1),349 },350 {351 Flags: CallExecuted,352 Errno: 0,353 },354 {355 Flags: CallExecuted,356 Errno: 0,357 },358 {359 Flags: CallExecuted,360 },361 {362 Flags: CallExecuted,363 },364 },365 },366 {367 `368fallback$0()369breaks_returns()370fallback$0()371breaks_returns()372fallback$0()373`,374 []CallInfo{375 {376 Flags: CallExecuted,377 Errno: 0,378 Signal: make([]uint32, 1),379 },380 {381 Flags: CallExecuted,382 Errno: 1,383 Signal: make([]uint32, 1),384 },385 {386 Flags: CallExecuted,387 Errno: 0,388 },389 {390 Flags: CallExecuted,391 Errno: 0,392 },393 {394 Flags: CallExecuted,395 },396 },397 },398 }399 target, err := GetTarget("test", "64")400 if err != nil {401 t.Fatal(err)402 }403 for i, test := range tests {404 t.Run(fmt.Sprint(i), func(t *testing.T) {405 p, err := target.Deserialize([]byte(test.prog), Strict)406 if err != nil {407 t.Fatal(err)408 }409 if len(p.Calls) != len(test.info) {410 t.Fatalf("call=%v info=%v", len(p.Calls), len(test.info))411 }412 wantSignal := make([]int, len(test.info))413 for i := range test.info {414 wantSignal[i] = len(test.info[i].Signal)415 test.info[i].Signal = nil416 }417 p.FallbackSignal(test.info)418 for i := range test.info {419 if len(test.info[i].Signal) != wantSignal[i] {...

Full Screen

Full Screen

splice.go

Source:splice.go Github

copy

Full Screen

...181 if err != nil {182 fmt.Fprintf(os.Stderr, "failed to read prog file: %v\n", err)183 os.Exit(1)184 }185 poc, err := target.Deserialize(data, prog.NonStrict)186 if err != nil {187 fmt.Fprintf(os.Stderr, "failed to deserialize the program: %v\n", err)188 os.Exit(1)189 }190 PocArgMaps := parseArg(poc)191 // logArgMap(PocArgMaps)192 var p *prog.Prog193 if flag.NArg() == 0 {194 fmt.Printf("please specify the input to be mutated\n")195 os.Exit(-1)196 } else {197 data, err := ioutil.ReadFile(flag.Arg(0))198 if err != nil {199 fmt.Fprintf(os.Stderr, "failed to read prog file: %v\n", err)200 os.Exit(1)201 }202 p, err = target.Deserialize(data, prog.NonStrict)203 pOriginal := p.Clone()204 if err != nil {205 fmt.Fprintf(os.Stderr, "failed to deserialize the program: %v\n", err)206 os.Exit(1)207 }208 fmt.Printf("Original input: %s\n", p.Serialize())209 SeedArgMaps := parseArg(p)210 // logArgMap(SeedArgMaps)211 SimilarArgMap := getSimilarTypes(SeedArgMaps, PocArgMaps)212 fmt.Printf("size of similar ArgMap : %v\n", len(SimilarArgMap))213 if len(SimilarArgMap) == 0 {214 fmt.Printf("Not similar inputs\n")215 return216 }217 // choose syscall218 sumPrio := float64(0)219 for _, mts := range SimilarArgMap {...

Full Screen

Full Screen

fuzz.go

Source:fuzz.go Github

copy

Full Screen

...7 "math/rand"8 "github.com/google/syzkaller/prog"9 _ "github.com/google/syzkaller/sys/test/gen" // import the target we use for fuzzing10)11func FuzzDeserialize(data []byte) int {12 p0, err0 := fuzzTarget.Deserialize(data, prog.NonStrict)13 p1, err1 := fuzzTarget.Deserialize(data, prog.Strict)14 if p0 == nil {15 if p1 != nil {16 panic("NonStrict is stricter than Strict")17 }18 if err0 == nil || err1 == nil {19 panic("no error")20 }21 return 022 }23 if err0 != nil {24 panic("got program and error")25 }26 data0 := p0.Serialize()27 if p1 != nil {28 if err1 != nil {29 panic("got program and error")30 }31 if !bytes.Equal(data0, p1.Serialize()) {32 panic("got different data")33 }34 }35 p2, err2 := fuzzTarget.Deserialize(data0, prog.NonStrict)36 if err2 != nil {37 panic(fmt.Sprintf("failed to parse serialized: %v\n%s", err2, data0))38 }39 if !bytes.Equal(data0, p2.Serialize()) {40 panic("got different data")41 }42 p3 := p0.Clone()43 if !bytes.Equal(data0, p3.Serialize()) {44 panic("got different data")45 }46 if n, err := p0.SerializeForExec(fuzzBuffer); err == nil {47 if _, err := fuzzTarget.DeserializeExec(fuzzBuffer[:n]); err != nil {48 panic(err)49 }50 }51 p3.Mutate(rand.NewSource(0), 3, fuzzChoiceTable, nil)52 return 053}54func FuzzParseLog(data []byte) int {55 if len(fuzzTarget.ParseLog(data)) != 0 {56 return 157 }58 return 059}60var fuzzBuffer = make([]byte, prog.ExecBufferSize)61var fuzzTarget, fuzzChoiceTable = func() (*prog.Target, *prog.ChoiceTable) {...

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2type prog struct {3}4func (p prog) serialize() ([]byte, error) {5 enc := gob.NewEncoder(&buf)6 err := enc.Encode(p)7 if err != nil {8 }9 return buf.Bytes(), nil10}11func deserialize(buf []byte) (prog, error) {12 dec := gob.NewDecoder(bytes.NewReader(buf))13 err := dec.Decode(&p)14 if err != nil {15 return prog{}, err16 }17}18func main() {19 p := prog{20 }21 buf, err := p.serialize()22 if err != nil {23 panic(err)24 }25 p2, err := deserialize(buf)26 if err != nil {27 panic(err)28 }29 fmt.Println(p2)30}31{golang 5}

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 enc := gob.NewEncoder(&network)4 dec := gob.NewDecoder(&network)5 enc.Encode(prog{1, 2, 3, 4, 5})6 dec.Decode(&q)7 fmt.Println(q)8}9import (10func main() {11 enc := gob.NewEncoder(&network)12 dec := gob.NewDecoder(&network)13 enc.Encode(prog{1, 2, 3, 4, 5})14 dec.Decode(&q)15 fmt.Println(q)16}17import (18func main() {19 enc := gob.NewEncoder(&network)20 dec := gob.NewDecoder(&network)21 enc.Encode(prog{1, 2, 3, 4, 5})22 dec.Decode(&q)23 fmt.Println(q)24}25import (26func main() {27 enc := gob.NewEncoder(&network)28 dec := gob.NewDecoder(&network)29 enc.Encode(prog{1, 2, 3, 4, 5})30 dec.Decode(&q)31 fmt.Println(q)32}33import (34func main() {35 enc := gob.NewEncoder(&network)36 dec := gob.NewDecoder(&network

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import "fmt"2type prog struct {3}4func main() {5p1 := prog{6}7p1.serialize()8}9func (p prog) serialize() {10fmt.Println("Serializing prog")11fmt.Println("Language:", p.language)12fmt.Println("Rating:", p.rating)13}

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "encoding/json"3type prog struct {4}5func main() {6 p := prog{Name: "golang", Age: 1}7 b, err := json.Marshal(p)8 if err != nil {9 fmt.Println("error:", err)10 }11 fmt.Println(string(b))12}13{"Name":"golang","Age":1}14func Unmarshal(data []byte, v interface{}) error15import "fmt"16import "encoding/json"17type prog struct {18}19func main() {20 data := []byte(`{"Name":"golang","Age":1}`)21 err := json.Unmarshal(data, &p)22 if err != nil {23 fmt.Println("error:", err)24 }25 fmt.Println(p)26}27{golang 1}28func Decode(reader io.Reader, v interface{}) error29import "fmt"30import "encoding/json"31import "strings"32type prog struct {33}34func main() {35 reader := strings.NewReader(`{"Name":"golang","Age":1}`)36 err := json.NewDecoder(reader).Decode(&p)37 if err != nil {38 fmt.Println("error:", err)39 }40 fmt.Println(p)41}42{golang 1}43func Encode(writer io.Writer, v interface{}) error44import "fmt"45import "encoding/json"46import "bytes"47type prog struct {

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2type prog struct {3}4func (p prog) serialize() []byte {5 enc := gob.NewEncoder(&buf)6 enc.Encode(p)7 return buf.Bytes()8}9func deserialize(data []byte) prog {10 dec := gob.NewDecoder(bytes.NewBuffer(data))11 dec.Decode(&p)12}13func main() {14 p := prog{

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 prog := &prog{}4 enc := json.NewEncoder(os.Stdout)5 dec := json.NewDecoder(bufio.NewReader(os.Stdin))6 enc.Encode(prog)7 dec.Decode(prog)8 fmt.Println(reflect.TypeOf(prog))9 fmt.Println(prog)10}

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p := prog{4 }5 fmt.Println("Before serialization")6 fmt.Println(p)7 p.serialize()8 p1.deserialize()9 fmt.Println("After deserialization")10 fmt.Println(p1)11}12import (13type prog struct {14}15func (p prog) serialize() {16 enc := gob.NewEncoder(&b)17 enc.Encode(p)18 ioutil.WriteFile("prog", b.Bytes(), 0644)19}20func (p *prog) deserialize() {21 f, err := os.Open("prog")22 if err != nil {23 fmt.Println(err)24 }25 b.ReadFrom(f)26 dec := gob.NewDecoder(&b)27 dec.Decode(p)28}29{Golang 2009}30{Golang 2009}

Full Screen

Full Screen

serialize

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 prog := Prog{4 }5 err := json.NewEncoder(os.Stdout).Encode(prog)6 if err != nil {7 fmt.Println(err)8 }9}10import (11func main() {12 prog := Prog{13 }14 err := json.NewEncoder(os.Stdout).Encode(prog)15 if err != nil {16 fmt.Println(err)17 }18}19import (20func main() {21 prog := Prog{22 }23 err := json.NewEncoder(os.Stdout).Encode(prog)24 if err != nil {25 fmt.Println(err)26 }27}28import (29func main() {30 prog := Prog{31 }

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