aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/csource/csource.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2017-12-27 20:02:58 +0100
committerDmitry Vyukov <dvyukov@google.com>2017-12-27 20:02:58 +0100
commit086787dd7e66f35e8d9051544d1345d32866dfc7 (patch)
tree28797a916ff71132500ba8effbfc6b60e4ead888 /pkg/csource/csource.go
parent1d3e9077107e3982124fc8be67de220003848ca5 (diff)
pkg/csource: tidy up generated code a bit
Remove dup newlines around includes. Makes int values shorter if not hurting readability. Increase line len to 80. Remove {} when not needed during copyout.
Diffstat (limited to 'pkg/csource/csource.go')
-rw-r--r--pkg/csource/csource.go40
1 files changed, 25 insertions, 15 deletions
diff --git a/pkg/csource/csource.go b/pkg/csource/csource.go
index 6285ba08b..c25f8b760 100644
--- a/pkg/csource/csource.go
+++ b/pkg/csource/csource.go
@@ -141,31 +141,31 @@ func Write(p *prog.Prog, opts Options) ([]byte, error) {
}
// Remove NONFAILING and debug calls.
- out0 := ctx.w.String()
+ result := ctx.w.Bytes()
if !opts.HandleSegv {
re := regexp.MustCompile(`\t*NONFAILING\((.*)\);\n`)
- out0 = re.ReplaceAllString(out0, "$1;\n")
+ result = re.ReplaceAll(result, []byte("$1;\n"))
}
if !opts.Debug {
re := regexp.MustCompile(`\t*debug\(.*\);\n`)
- out0 = re.ReplaceAllString(out0, "")
+ result = re.ReplaceAll(result, nil)
re = regexp.MustCompile(`\t*debug_dump_data\(.*\);\n`)
- out0 = re.ReplaceAllString(out0, "")
+ result = re.ReplaceAll(result, nil)
}
- out0 = strings.Replace(out0, "NORETURN", "", -1)
- out0 = strings.Replace(out0, "PRINTF", "", -1)
+ result = bytes.Replace(result, []byte("NORETURN"), nil, -1)
+ result = bytes.Replace(result, []byte("PRINTF"), nil, -1)
// Remove duplicate new lines.
- out1 := []byte(out0)
for {
- out2 := bytes.Replace(out1, []byte{'\n', '\n', '\n'}, []byte{'\n', '\n'}, -1)
- if len(out1) == len(out2) {
+ result1 := bytes.Replace(result, []byte{'\n', '\n', '\n'}, []byte{'\n', '\n'}, -1)
+ result1 = bytes.Replace(result1, []byte("\n\n#include"), []byte("\n#include"), -1)
+ if len(result1) == len(result) {
break
}
- out1 = out2
+ result = result1
}
- return out1, nil
+ return result, nil
}
type context struct {
@@ -312,6 +312,7 @@ func (ctx *context) generateCalls(p prog.ExecProg) ([]string, uint64) {
callName := call.Meta.CallName
resCopyout := call.Index != prog.ExecNoCopyout
argCopyout := len(call.Copyout) != 0
+ argCopyoutMultiple := len(call.Copyout) > 1
emitCall := ctx.opts.EnableTun || callName != "syz_emit_ethernet" &&
callName != "syz_extract_tcp_res"
// TODO: if we don't emit the call we must also not emit copyin, copyout and fault injection.
@@ -351,7 +352,10 @@ func (ctx *context) generateCalls(p prog.ExecProg) ([]string, uint64) {
if resCopyout {
fmt.Fprintf(w, ")")
}
- fmt.Fprintf(w, " != -1) {")
+ fmt.Fprintf(w, " != -1)")
+ if argCopyoutMultiple {
+ fmt.Fprintf(w, " {")
+ }
} else {
fmt.Fprintf(w, ";")
}
@@ -363,7 +367,7 @@ func (ctx *context) generateCalls(p prog.ExecProg) ([]string, uint64) {
fmt.Fprintf(w, "\t\tNONFAILING(r[%v] = *(uint%v_t*)0x%x);\n",
copyout.Index, copyout.Size*8, copyout.Addr)
}
- if emitCall && argCopyout {
+ if emitCall && argCopyoutMultiple {
fmt.Fprintf(w, "\t}\n")
}
@@ -374,9 +378,15 @@ func (ctx *context) generateCalls(p prog.ExecProg) ([]string, uint64) {
func (ctx *context) constArgToStr(arg prog.ExecArgConst) string {
mask := (uint64(1) << (arg.Size * 8)) - 1
- val := fmt.Sprintf("0x%x", arg.Value&mask)
+ v := arg.Value & mask
+ val := fmt.Sprintf("%v", v)
+ if v == ^uint64(0)&mask {
+ val = "-1"
+ } else if v >= 10 {
+ val = fmt.Sprintf("0x%x", v)
+ }
if ctx.opts.Procs > 1 && arg.PidStride != 0 {
- val += fmt.Sprintf("+procid*0x%xul", arg.PidStride)
+ val += fmt.Sprintf("+procid*%v", arg.PidStride)
}
if arg.BigEndian {
val = fmt.Sprintf("htobe%v(%v)", arg.Size*8, val)