Skip to content

Commit f98b329

Browse files
author
Ethan Graham
committed
kfuzztest: refactor ExtractData
Refactor ExtractData to simplify error handling.
1 parent ff95c2f commit f98b329

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

pkg/kfuzztest/kfuzztest.go

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,53 @@ type KFuzzTestData struct {
103103
Types []prog.Type
104104
}
105105

106+
func extractData(vmlinuxPath string) (KFuzzTestData, error) {
107+
desc, err := ExtractDescription(vmlinuxPath)
108+
if err != nil {
109+
return KFuzzTestData{}, err
110+
}
111+
112+
var astError error
113+
eh := func(pos ast.Pos, msg string) {
114+
astError = fmt.Errorf("ast error: %v: %v", pos, msg)
115+
}
116+
descAst := ast.Parse([]byte(desc), "kfuzztest-autogen", eh)
117+
if astError != nil {
118+
return KFuzzTestData{}, astError
119+
}
120+
if descAst == nil {
121+
return KFuzzTestData{}, fmt.Errorf("failed to build AST for program")
122+
}
123+
124+
// TODO: this assumes x86_64, but KFuzzTest supports (in theory) any
125+
// architecture.
126+
target := targets.Get(targets.Linux, targets.AMD64)
127+
program := compiler.Compile(descAst, make(map[string]uint64), target, eh)
128+
if astError != nil {
129+
return KFuzzTestData{}, fmt.Errorf("failed to compile extracted KFuzzTest target: %w", astError)
130+
}
131+
132+
kFuzzTestCalls := []*prog.Syscall{}
133+
for _, call := range program.Syscalls {
134+
// The generated descriptions contain some number of built-ins, which
135+
// we want to filter out.
136+
if call.Attrs.KFuzzTest {
137+
kFuzzTestCalls = append(kFuzzTestCalls, call)
138+
}
139+
}
140+
141+
// We restore links on all generated system calls for completeness, but we
142+
// only return the filtered slice.
143+
prog.RestoreLinks(program.Syscalls, program.Resources, program.Types)
144+
145+
return KFuzzTestData{
146+
Description: desc,
147+
Calls: kFuzzTestCalls,
148+
Resources: program.Resources,
149+
Types: program.Types,
150+
}, nil
151+
}
152+
106153
type extractKFuzzTestDataState struct {
107154
once sync.Once
108155
data KFuzzTestData
@@ -119,51 +166,7 @@ var extractState extractKFuzzTestDataState
119166
// based on their path.
120167
func ExtractData(vmlinuxPath string) (KFuzzTestData, error) {
121168
extractState.once.Do(func() {
122-
desc, err := ExtractDescription(vmlinuxPath)
123-
if err != nil {
124-
extractState.err = err
125-
return
126-
}
127-
128-
var astError error
129-
eh := func(pos ast.Pos, msg string) {
130-
astError = fmt.Errorf("ast error: %v: %v", pos, msg)
131-
}
132-
descAst := ast.Parse([]byte(desc), "kfuzztest-autogen", eh)
133-
if astError != nil {
134-
extractState.err = astError
135-
return
136-
}
137-
if descAst == nil {
138-
extractState.err = fmt.Errorf("failed to build AST for program")
139-
return
140-
}
141-
142-
target := targets.Get(targets.Linux, targets.AMD64)
143-
program := compiler.Compile(descAst, make(map[string]uint64), target, eh)
144-
if astError != nil {
145-
extractState.err = fmt.Errorf("failed to compile extracted KFuzzTest target: %w", astError)
146-
}
147-
148-
kFuzzTestCalls := []*prog.Syscall{}
149-
for _, call := range program.Syscalls {
150-
// The generated descriptions contain some number of built-ins, which
151-
// we want to filter out.
152-
if call.Attrs.KFuzzTest {
153-
kFuzzTestCalls = append(kFuzzTestCalls, call)
154-
}
155-
}
156-
157-
// We restore links on all restored system calls for completeness, but we
158-
// only return the filtered slice.
159-
prog.RestoreLinks(program.Syscalls, program.Resources, program.Types)
160-
161-
extractState.data = KFuzzTestData{
162-
Description: desc,
163-
Calls: kFuzzTestCalls,
164-
Resources: program.Resources,
165-
Types: program.Types,
166-
}
169+
extractState.data, extractState.err = extractData(vmlinuxPath)
167170
})
168171

169172
return extractState.data, extractState.err

0 commit comments

Comments
 (0)