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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
// Copyright 2020 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 main
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"github.com/google/syzkaller/pkg/kconfig"
"github.com/google/syzkaller/pkg/vcs"
"gopkg.in/yaml.v3"
)
type Instance struct {
Name string
Kernel Kernel
Compiler string
Linker string
Verbatim []byte
Shell []Shell
Features Features
ConfigMap map[string]*Config
Configs []*Config
}
type Config struct {
Name string
Value string
Optional bool
Constraints []string
File string
Line int
}
type Kernel struct {
Repo string
Tag string
}
type Shell struct {
Cmd string
Constraints []string
}
type Features map[string]bool
func (features Features) Match(constraints []string) bool {
for _, feat := range constraints {
if feat[0] == '-' {
if features[feat[1:]] {
return false
}
} else if !features[feat] {
return false
}
}
return true
}
func constraintsInclude(constraints []string, what string) bool {
for _, feat := range constraints {
if feat == what {
return true
}
}
return false
}
type rawMain struct {
Instances []map[string][]string
Includes []map[string][]string
}
type rawFile struct {
Kernel struct {
Repo string
Tag string
}
Compiler string
Linker string
Shell []yaml.Node
Verbatim string
Config []yaml.Node
}
func parseMainSpec(file string) ([]*Instance, []string, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, nil, fmt.Errorf("failed to read config file: %v", err)
}
dec := yaml.NewDecoder(bytes.NewReader(data))
dec.KnownFields(true)
raw := new(rawMain)
if err := dec.Decode(raw); err != nil {
return nil, nil, fmt.Errorf("failed to parse %v: %v", file, err)
}
var unusedFeatures []string
var instances []*Instance
for _, inst := range raw.Instances {
for name, features := range inst {
if name == "_" {
unusedFeatures = features
continue
}
inst, err := parseInstance(name, filepath.Dir(file), features, raw.Includes)
if err != nil {
return nil, nil, fmt.Errorf("%v: %v", name, err)
}
instances = append(instances, inst)
if constraintsInclude(features, featBaseline) {
continue
}
inst, err = parseInstance(name+"-base", filepath.Dir(file), append(features, featBaseline), raw.Includes)
if err != nil {
return nil, nil, err
}
instances = append(instances, inst)
}
}
return instances, unusedFeatures, nil
}
func parseInstance(name, configDir string, features []string, includes []map[string][]string) (*Instance, error) {
inst := &Instance{
Name: name,
Features: make(Features),
ConfigMap: make(map[string]*Config),
}
for _, feat := range features {
inst.Features[feat] = true
}
errs := new(Errors)
for _, include := range includes {
for file, features := range include {
raw, err := parseFile(filepath.Join(configDir, "bits", file))
if err != nil {
return nil, err
}
if inst.Features.Match(features) {
mergeFile(inst, raw, file, errs)
} else if inst.Features[featReduced] && constraintsInclude(features, "-"+featReduced) {
// For fragments that we exclude because of "reduced" config,
// we want to disable all configs listed there.
// For example, if the fragment enables config FOO, and we the defconfig
// also enabled FOO, we want to disable FOO to get reduced config.
for _, node := range raw.Config {
mergeConfig(inst, file, node, true, errs)
}
}
}
}
inst.Verbatim = bytes.TrimSpace(inst.Verbatim)
return inst, errs.err()
}
func parseFile(file string) (*rawFile, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read %v: %v", file, err)
}
dec := yaml.NewDecoder(bytes.NewReader(data))
dec.KnownFields(true)
raw := new(rawFile)
if err := dec.Decode(raw); err != nil {
return nil, fmt.Errorf("failed to parse %v: %v", file, err)
}
return raw, nil
}
func mergeFile(inst *Instance, raw *rawFile, file string, errs *Errors) {
if raw.Kernel.Repo != "" || raw.Kernel.Tag != "" {
if !vcs.CheckRepoAddress(raw.Kernel.Repo) {
errs.push("%v: bad kernel repo %q", file, raw.Kernel.Repo)
}
if !vcs.CheckBranch(raw.Kernel.Tag) {
errs.push("%v: bad kernel tag %q", file, raw.Kernel.Tag)
}
if inst.Kernel.Repo != "" {
errs.push("%v: kernel is set twice", file)
}
inst.Kernel = raw.Kernel
}
if raw.Compiler != "" {
if inst.Compiler != "" {
errs.push("%v: compiler is set twice", file)
}
inst.Compiler = raw.Compiler
}
if raw.Linker != "" {
if inst.Linker != "" {
errs.push("%v: linker is set twice", file)
}
inst.Linker = raw.Linker
}
prependShell := []Shell{}
for _, node := range raw.Shell {
cmd, _, constraints, err := parseNode(node)
if err != nil {
errs.push("%v:%v: %v", file, node.Line, err)
}
prependShell = append(prependShell, Shell{
Cmd: cmd,
Constraints: constraints,
})
}
inst.Shell = append(prependShell, inst.Shell...)
if raw.Verbatim != "" {
inst.Verbatim = append(append(inst.Verbatim, strings.TrimSpace(raw.Verbatim)...), '\n')
}
for _, node := range raw.Config {
mergeConfig(inst, file, node, false, errs)
}
}
func mergeConfig(inst *Instance, file string, node yaml.Node, reduced bool, errs *Errors) {
name, val, constraints, err := parseNode(node)
if err != nil {
errs.push("%v:%v: %v", file, node.Line, err)
return
}
if reduced {
if val != kconfig.No && val != kconfig.Yes {
return
}
val = kconfig.No
constraints = append(constraints, featWeak)
}
cfg := &Config{
Name: name,
Value: val,
File: file,
Line: node.Line,
}
override, appendVal := false, false
for _, feat := range constraints {
switch feat {
case featOverride:
override = true
case featOptional:
cfg.Optional = true
case featWeak:
override, cfg.Optional = true, true
case featAppend:
override, appendVal = true, true
default:
cfg.Constraints = append(cfg.Constraints, feat)
}
}
if prev := inst.ConfigMap[name]; prev != nil {
if !override {
errs.push("%v:%v: %v is already defined at %v:%v", file, node.Line, name, prev.File, prev.Line)
}
if appendVal {
a, b := prev.Value, cfg.Value
if a == "" || a[len(a)-1] != '"' || b == "" || b[0] != '"' {
errs.push("%v:%v: bad values to append, want non-empty strings", file, node.Line)
return
}
prev.Value = a[:len(a)-1] + " " + b[1:]
} else {
*prev = *cfg
}
return
}
if override && !cfg.Optional {
errs.push("%v:%v: %v nothing to override", file, node.Line, name)
}
inst.ConfigMap[name] = cfg
inst.Configs = append(inst.Configs, cfg)
}
func parseNode(node yaml.Node) (name, val string, constraints []string, err error) {
// Simplest case: - FOO.
val = kconfig.Yes
if node.Decode(&name) == nil {
return
}
complexVal := make(map[string]yaml.Node)
if err = node.Decode(complexVal); err != nil {
return
}
var valNode yaml.Node
for k, v := range complexVal {
name, valNode = k, v
break
}
// Case: - FOO: 42.
if intVal := 0; valNode.Decode(&intVal) == nil {
val = fmt.Sprint(intVal)
return
}
if valNode.Decode(&val) == nil {
// Case: - FOO: "string".
if valNode.Style == yaml.DoubleQuotedStyle {
val = `"` + val + `"`
return
}
// Case: - FOO: n.
if valNode.Style == 0 && val == "n" {
val = kconfig.No
return
}
err = fmt.Errorf("bad config format")
return
}
// Case: - FOO: [...].
propsNode := []yaml.Node{}
if err = valNode.Decode(&propsNode); err != nil {
return
}
for _, propNode := range propsNode {
prop := ""
if err = propNode.Decode(&prop); err != nil {
return
}
if propNode.Style == yaml.DoubleQuotedStyle {
val = `"` + prop + `"`
} else if prop == "n" {
val = kconfig.No
} else if intVal, err := strconv.ParseUint(prop, 0, 64); err == nil {
val = fmt.Sprint(intVal)
} else {
constraints = append(constraints, prop)
}
}
return
}
type Errors []byte
func (errs *Errors) push(msg string, args ...interface{}) {
*errs = append(*errs, fmt.Sprintf(msg+"\n", args...)...)
}
func (errs *Errors) err() error {
if len(*errs) == 0 {
return nil
}
return fmt.Errorf("%s", *errs)
}
|