diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2018-05-17 19:07:46 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2018-05-17 19:07:46 +0200 |
| commit | 3717901c109694be56bb24593db945e4367ecf14 (patch) | |
| tree | ff61059fa9512b1b7eb21d7662405e99bff7cc41 /pkg/csource | |
| parent | 9753d3be5e6c79e271ed128795039f161ee339b7 (diff) | |
pkg/csource: serialize options into json
Current format is painful to parse after changes.
Switch to json.
Diffstat (limited to 'pkg/csource')
| -rw-r--r-- | pkg/csource/options.go | 43 | ||||
| -rw-r--r-- | pkg/csource/options_test.go | 17 |
2 files changed, 43 insertions, 17 deletions
diff --git a/pkg/csource/options.go b/pkg/csource/options.go index d6fdf4739..7b9ed7050 100644 --- a/pkg/csource/options.go +++ b/pkg/csource/options.go @@ -5,6 +5,7 @@ package csource import ( "bytes" + "encoding/json" "errors" "fmt" ) @@ -12,27 +13,27 @@ import ( // Options control various aspects of source generation. // Dashboard also provides serialized Options along with syzkaller reproducers. type Options struct { - Threaded bool - Collide bool - Repeat bool - Procs int - Sandbox string + Threaded bool `json:"threaded,omitempty"` + Collide bool `json:"collide,omitempty"` + Repeat bool `json:"repeat,omitempty"` + Procs int `json:"procs"` + Sandbox string `json:"sandbox"` - Fault bool // inject fault into FaultCall/FaultNth - FaultCall int - FaultNth int + Fault bool `json:"fault,omitempty"` // inject fault into FaultCall/FaultNth + FaultCall int `json:"fault_call,omitempty"` + FaultNth int `json:"fault_nth,omitempty"` // These options allow for a more fine-tuned control over the generated C code. - EnableTun bool - UseTmpDir bool - EnableCgroups bool - HandleSegv bool - WaitRepeat bool - Debug bool + EnableTun bool `json:"tun,omitempty"` + UseTmpDir bool `json:"tmpdir,omitempty"` + EnableCgroups bool `json:"cgroups,omitempty"` + HandleSegv bool `json:"segv,omitempty"` + WaitRepeat bool `json:"waitrepeat,omitempty"` + Debug bool `json:"debug,omitempty"` // Generate code for use with repro package to prints log messages, // which allows to distinguish between a hang and an absent crash. - Repro bool + Repro bool `json:"repro,omitempty"` } // Check checks if the opts combination is valid or not. @@ -69,12 +70,20 @@ func (opts Options) Check() error { } func (opts Options) Serialize() []byte { - return []byte(fmt.Sprintf("%+v", opts)) + data, err := json.Marshal(opts) + if err != nil { + panic(err) + } + return data } func DeserializeOptions(data []byte) (Options, error) { - data = bytes.Replace(data, []byte("Sandbox: "), []byte("Sandbox:empty "), -1) var opts Options + if err := json.Unmarshal(data, &opts); err == nil { + return opts, nil + } + // Support for legacy formats. + data = bytes.Replace(data, []byte("Sandbox: "), []byte("Sandbox:empty "), -1) n, err := fmt.Sscanf(string(data), "{Threaded:%t Collide:%t Repeat:%t Procs:%d Sandbox:%s"+ " Fault:%t FaultCall:%d FaultNth:%d EnableTun:%t UseTmpDir:%t"+ diff --git a/pkg/csource/options_test.go b/pkg/csource/options_test.go index 4f5b0f91e..9aee780a3 100644 --- a/pkg/csource/options_test.go +++ b/pkg/csource/options_test.go @@ -27,6 +27,23 @@ func TestParseOptionsCanned(t *testing.T) { // so we need to be able to parse old formats. // nolint: lll canned := map[string]Options{ + `{"threaded":true,"collide":true,"repeat":true,"procs":10,"sandbox":"namespace","fault":true,"fault_call":1,"fault_nth":2,"tun":true,"tmpdir":true,"cgroups":true,"segv":true,"waitrepeat":true,"debug":true,"repro":true}`: Options{ + Threaded: true, + Collide: true, + Repeat: true, + Procs: 10, + Sandbox: "namespace", + Fault: true, + FaultCall: 1, + FaultNth: 2, + EnableTun: true, + UseTmpDir: true, + EnableCgroups: true, + HandleSegv: true, + WaitRepeat: true, + Debug: true, + Repro: true, + }, "{Threaded:true Collide:true Repeat:true Procs:1 Sandbox:none Fault:false FaultCall:-1 FaultNth:0 EnableTun:true UseTmpDir:true HandleSegv:true WaitRepeat:true Debug:false Repro:false}": Options{ Threaded: true, Collide: true, |
