-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
features.go
80 lines (74 loc) · 2.39 KB
/
features.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2018 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package host
import (
"bytes"
"fmt"
"strings"
"time"
"github.com/google/syzkaller/pkg/csource"
"github.com/google/syzkaller/pkg/flatrpc"
"github.com/google/syzkaller/pkg/ipc"
"github.com/google/syzkaller/pkg/log"
"github.com/google/syzkaller/pkg/osutil"
"github.com/google/syzkaller/prog"
"github.com/google/syzkaller/sys/targets"
)
// SetupFeatures enables and does any one-time setup for the requested features on the host.
// Note: this can be called multiple times and must be idempotent.
func SetupFeatures(target *prog.Target, executor string, mask flatrpc.Feature, flags csource.Features) (
[]*flatrpc.FeatureInfo, error) {
if noHostChecks(target) {
return nil, nil
}
var results []*flatrpc.FeatureInfo
resultC := make(chan *flatrpc.FeatureInfo)
for feat := range flatrpc.EnumNamesFeature {
feat := feat
if mask&feat == 0 {
continue
}
opt := ipc.FlatRPCFeaturesToCSource[feat]
if opt != "" && flags != nil && !flags["binfmt_misc"].Enabled {
continue
}
results = append(results, nil)
go setupFeature(executor, feat, resultC)
}
// Feature 0 setups common things that are not part of any feature.
setupFeature(executor, 0, nil)
for i := range results {
results[i] = <-resultC
}
return results, nil
}
func setupFeature(executor string, feat flatrpc.Feature, resultC chan *flatrpc.FeatureInfo) {
args := strings.Split(executor, " ")
executor = args[0]
args = append(args[1:], "setup", fmt.Sprint(uint64(feat)))
output, err := osutil.RunCmd(3*time.Minute, "", executor, args...)
log.Logf(1, "executor %v\n%s", args, bytes.ReplaceAll(output, []byte("SYZFAIL:"), nil))
outputStr := string(output)
if err == nil {
outputStr = ""
} else if outputStr == "" {
outputStr = err.Error()
}
needSetup := true
if strings.Contains(outputStr, "feature setup is not needed") {
needSetup = false
outputStr = ""
}
if resultC != nil {
resultC <- &flatrpc.FeatureInfo{
Id: feat,
NeedSetup: needSetup,
Reason: outputStr,
}
}
}
func noHostChecks(target *prog.Target) bool {
// HostFuzzer targets can't run Go binaries on the targets,
// so we actually run on the host on another OS. The same for targets.TestOS OS.
return targets.Get(target.OS, target.Arch).HostFuzzer || target.OS == targets.TestOS
}