aboutsummaryrefslogtreecommitdiffstats
path: root/tools/syz-trace2syz/parser
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2018-12-07 10:24:24 +0100
committerDmitry Vyukov <dvyukov@google.com>2018-12-07 10:24:24 +0100
commitae17c862c97405973ff0b7209caf655b4042c068 (patch)
tree38be07ac70fc804242d4eac9eda2f56e81e302b6 /tools/syz-trace2syz/parser
parent1eb6a7e4336db98310cc7937bccf63a153c99fff (diff)
tools/syz-trace2syz/parser: use []byte instead of string for file contents
If we are handling whole files, it's more efficient to use []byte. string is not really meant to hold large amounts of data.
Diffstat (limited to 'tools/syz-trace2syz/parser')
-rw-r--r--tools/syz-trace2syz/parser/parser.go16
-rw-r--r--tools/syz-trace2syz/parser/parser_test.go15
2 files changed, 14 insertions, 17 deletions
diff --git a/tools/syz-trace2syz/parser/parser.go b/tools/syz-trace2syz/parser/parser.go
index f3a8ec92f..6efffeb06 100644
--- a/tools/syz-trace2syz/parser/parser.go
+++ b/tools/syz-trace2syz/parser/parser.go
@@ -5,6 +5,7 @@ package parser
import (
"bufio"
+ "bytes"
"io/ioutil"
"strings"
@@ -24,15 +25,12 @@ func shouldSkip(line string) bool {
strings.Contains(line, "<ptrace(SYSCALL):No such process>")
}
-// ParseLoop parses each line of a strace file in a loop
-func ParseLoop(data string) *TraceTree {
+// ParseLoop parses each line of a strace file in a loop.
+func ParseLoop(data []byte) *TraceTree {
tree := NewTraceTree()
// Creating the process tree
- const maxBufferSize = 64 * 1024 * 1024 // maxBufferSize is maximum size for buffer
- buf := make([]byte, maxBufferSize)
- scanner := bufio.NewScanner(strings.NewReader(data))
- scanner.Buffer(buf, maxBufferSize)
-
+ scanner := bufio.NewScanner(bytes.NewReader(data))
+ scanner.Buffer(nil, 64<<20)
for scanner.Scan() {
line := scanner.Text()
if shouldSkip(line) {
@@ -45,7 +43,7 @@ func ParseLoop(data string) *TraceTree {
}
tree.add(call)
}
- if len(tree.TraceMap) == 0 {
+ if scanner.Err() != nil || len(tree.TraceMap) == 0 {
return nil
}
return tree
@@ -57,7 +55,7 @@ func Parse(filename string) *TraceTree {
if err != nil {
log.Fatalf("error reading file: %s", err.Error())
}
- tree := ParseLoop(string(data))
+ tree := ParseLoop(data)
if tree != nil {
tree.Filename = filename
}
diff --git a/tools/syz-trace2syz/parser/parser_test.go b/tools/syz-trace2syz/parser/parser_test.go
index 080c25f86..2a8373611 100644
--- a/tools/syz-trace2syz/parser/parser_test.go
+++ b/tools/syz-trace2syz/parser/parser_test.go
@@ -10,7 +10,6 @@ import (
)
func TestParseLoopBasic(t *testing.T) {
-
tests := []string{
`open() = 3
fstat() = 0`,
@@ -60,7 +59,7 @@ func TestParseLoopBasic(t *testing.T) {
}
for _, test := range tests {
- tree := ParseLoop(test)
+ tree := ParseLoop([]byte(test))
if tree.RootPid != -1 {
t.Fatalf("Incorrect Root Pid: %d", tree.RootPid)
}
@@ -93,7 +92,7 @@ func TestEvaluateExpressions(t *testing.T) {
{"open(4 >> 1) = 0", 2},
}
for i, test := range tests {
- tree := ParseLoop(test.line)
+ tree := ParseLoop([]byte(test.line))
if tree.RootPid != -1 {
t.Fatalf("failed test: %d. Incorrect Root Pid: %d", i, tree.RootPid)
}
@@ -115,7 +114,7 @@ func TestParseLoopPid(t *testing.T) {
data := `1 open() = 3
1 fstat() = 0`
- tree := ParseLoop(data)
+ tree := ParseLoop([]byte(data))
if tree.RootPid != 1 {
t.Fatalf("Incorrect Root Pid: %d", tree.RootPid)
}
@@ -134,7 +133,7 @@ func TestParseLoop1Child(t *testing.T) {
1 clone() = 2
2 read() = 16`
- tree := ParseLoop(data1Child)
+ tree := ParseLoop([]byte(data1Child))
if len(tree.TraceMap) != 2 {
t.Fatalf("Incorrect Root Pid. Expected: 2, Got %d", tree.RootPid)
}
@@ -156,7 +155,7 @@ func TestParseLoop2Childs(t *testing.T) {
2 read() = 16
1 clone() = 3
3 open() = 3`
- tree := ParseLoop(data2Childs)
+ tree := ParseLoop([]byte(data2Childs))
if len(tree.TraceMap) != 3 {
t.Fatalf("Incorrect Root Pid. Expected: 3, Got %d", tree.RootPid)
}
@@ -170,7 +169,7 @@ func TestParseLoop1Grandchild(t *testing.T) {
1 clone() = 2
2 clone() = 3
3 open() = 4`
- tree := ParseLoop(data1Grandchild)
+ tree := ParseLoop([]byte(data1Grandchild))
if len(tree.Ptree[tree.RootPid]) != 1 {
t.Fatalf("Expect RootPid to have 1 child. Got %d", tree.RootPid)
}
@@ -189,7 +188,7 @@ func TestParseGroupType(t *testing.T) {
{`open([1, 2, 3]) = 0`},
}
for _, test := range tests {
- tree := ParseLoop(test.test)
+ tree := ParseLoop([]byte(test.test))
call := tree.TraceMap[tree.RootPid].Calls[0]
_, ok := call.Args[0].(*GroupType)
if !ok {