forked from google/syzkaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsts.go
407 lines (386 loc) · 10.7 KB
/
consts.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright 2017 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 compiler
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/google/syzkaller/pkg/ast"
"github.com/google/syzkaller/prog"
"github.com/google/syzkaller/sys/targets"
)
type ConstInfo struct {
File string
Consts []string
Includes []string
Incdirs []string
Defines map[string]string
}
func ExtractConsts(desc *ast.Description, target *targets.Target, eh ast.ErrorHandler) map[string]*ConstInfo {
res := Compile(desc, nil, target, eh)
if res == nil {
return nil
}
return res.fileConsts
}
// FabricateSyscallConsts adds syscall number constants to consts map.
// Used for test OS to not bother specifying consts for all syscalls.
func FabricateSyscallConsts(target *targets.Target, constInfo map[string]*ConstInfo, consts map[string]uint64) {
if !target.SyscallNumbers {
return
}
for _, info := range constInfo {
for _, name := range info.Consts {
if strings.HasPrefix(name, target.SyscallPrefix) {
consts[name] = 0
}
}
}
}
// extractConsts returns list of literal constants and other info required for const value extraction.
func (comp *compiler) extractConsts() map[string]*ConstInfo {
infos := make(map[string]*constInfo)
for _, decl := range comp.desc.Nodes {
pos, _, _ := decl.Info()
info := getConstInfo(infos, pos)
switch n := decl.(type) {
case *ast.Include:
info.includeArray = append(info.includeArray, n.File.Value)
case *ast.Incdir:
info.incdirArray = append(info.incdirArray, n.Dir.Value)
case *ast.Define:
v := fmt.Sprint(n.Value.Value)
switch {
case n.Value.CExpr != "":
v = n.Value.CExpr
case n.Value.Ident != "":
v = n.Value.Ident
}
name := n.Name.Name
if _, builtin := comp.builtinConsts[name]; builtin {
comp.error(pos, "redefining builtin const %v", name)
}
info.defines[name] = v
comp.addConst(infos, pos, name)
case *ast.Call:
if comp.target.SyscallNumbers && !strings.HasPrefix(n.CallName, "syz_") {
comp.addConst(infos, pos, comp.target.SyscallPrefix+n.CallName)
}
for _, attr := range n.Attrs {
if callAttrs[attr.Ident].HasArg {
comp.addConst(infos, attr.Pos, attr.Args[0].Ident)
}
}
case *ast.Struct:
for _, attr := range n.Attrs {
if structOrUnionAttrs(n)[attr.Ident].HasArg {
comp.addConst(infos, attr.Pos, attr.Args[0].Ident)
}
}
}
switch decl.(type) {
case *ast.Call, *ast.Struct, *ast.Resource, *ast.TypeDef:
comp.extractTypeConsts(infos, decl)
}
}
comp.desc.Walk(ast.Recursive(func(n0 ast.Node) {
if n, ok := n0.(*ast.Int); ok {
comp.addConst(infos, n.Pos, n.Ident)
}
}))
return convertConstInfo(infos)
}
func (comp *compiler) extractTypeConsts(infos map[string]*constInfo, n ast.Node) {
comp.foreachType(n, func(t *ast.Type, desc *typeDesc, args []*ast.Type, _ prog.IntTypeCommon) {
for i, arg := range args {
if desc.Args[i].Type.Kind == kindInt {
if arg.Ident != "" {
comp.addConst(infos, arg.Pos, arg.Ident)
}
for _, col := range arg.Colon {
if col.Ident != "" {
comp.addConst(infos, col.Pos, col.Ident)
}
}
}
}
})
}
func (comp *compiler) addConst(infos map[string]*constInfo, pos ast.Pos, name string) {
if _, builtin := comp.builtinConsts[name]; builtin {
return
}
info := getConstInfo(infos, pos)
info.consts[name] = true
}
type constInfo struct {
consts map[string]bool
defines map[string]string
includeArray []string
incdirArray []string
}
func getConstInfo(infos map[string]*constInfo, pos ast.Pos) *constInfo {
info := infos[pos.File]
if info == nil {
info = &constInfo{
consts: make(map[string]bool),
defines: make(map[string]string),
}
infos[pos.File] = info
}
return info
}
func convertConstInfo(infos map[string]*constInfo) map[string]*ConstInfo {
res := make(map[string]*ConstInfo)
for file, info := range infos {
if file == ast.BuiltinFile {
continue
}
res[file] = &ConstInfo{
File: file,
Consts: toArray(info.consts),
Includes: info.includeArray,
Incdirs: info.incdirArray,
Defines: info.defines,
}
}
return res
}
// assignSyscallNumbers assigns syscall numbers, discards unsupported syscalls.
func (comp *compiler) assignSyscallNumbers(consts map[string]uint64) {
for _, decl := range comp.desc.Nodes {
c, ok := decl.(*ast.Call)
if !ok || strings.HasPrefix(c.CallName, "syz_") {
continue
}
str := comp.target.SyscallPrefix + c.CallName
nr, ok := consts[str]
if ok {
c.NR = nr
continue
}
c.NR = ^uint64(0) // mark as unused to not generate it
name := "syscall " + c.CallName
if !comp.unsupported[name] {
comp.unsupported[name] = true
comp.warning(c.Pos, "unsupported syscall: %v due to missing const %v",
c.CallName, str)
}
}
}
// patchConsts replaces all symbolic consts with their numeric values taken from consts map.
// Updates desc and returns set of unsupported syscalls and flags.
func (comp *compiler) patchConsts(consts0 map[string]uint64) {
consts := make(map[string]uint64)
for name, val := range consts0 {
consts[name] = val
}
for name, val := range comp.builtinConsts {
if _, ok := consts[name]; ok {
panic(fmt.Sprintf("builtin const %v already defined", name))
}
consts[name] = val
}
for _, decl := range comp.desc.Nodes {
switch decl.(type) {
case *ast.IntFlags:
// Unsupported flag values are dropped.
n := decl.(*ast.IntFlags)
var values []*ast.Int
for _, v := range n.Values {
if comp.patchIntConst(v, consts, nil) {
values = append(values, v)
}
}
n.Values = values
case *ast.Resource, *ast.Struct, *ast.Call, *ast.TypeDef:
// Walk whole tree and replace consts in Type's and Int's.
missing := ""
comp.foreachType(decl, func(_ *ast.Type, desc *typeDesc,
args []*ast.Type, _ prog.IntTypeCommon) {
for i, arg := range args {
if desc.Args[i].Type.Kind == kindInt {
comp.patchTypeConst(arg, consts, &missing)
}
}
})
switch n := decl.(type) {
case *ast.Resource:
for _, v := range n.Values {
comp.patchIntConst(v, consts, &missing)
}
case *ast.Call:
for _, attr := range n.Attrs {
if callAttrs[attr.Ident].HasArg {
comp.patchTypeConst(attr.Args[0], consts, &missing)
}
}
case *ast.Struct:
for _, attr := range n.Attrs {
if structOrUnionAttrs(n)[attr.Ident].HasArg {
comp.patchTypeConst(attr.Args[0], consts, &missing)
}
}
}
if missing == "" {
continue
}
// Produce a warning about unsupported syscall/resource/struct.
// TODO(dvyukov): we should transitively remove everything that
// depends on unsupported things. Potentially we still can get,
// say, a bad int range error due to the wrong const value.
// However, if we have a union where one of the options is
// arch-specific and does not have a const value, it's probably
// better to remove just that option. But then if we get to 0
// options in the union, we still need to remove it entirely.
pos, typ, name := decl.Info()
if id := typ + " " + name; !comp.unsupported[id] {
comp.unsupported[id] = true
comp.warning(pos, "unsupported %v: %v due to missing const %v",
typ, name, missing)
}
if c, ok := decl.(*ast.Call); ok {
c.NR = ^uint64(0) // mark as unused to not generate it
}
}
}
}
func (comp *compiler) patchIntConst(n *ast.Int, consts map[string]uint64, missing *string) bool {
return comp.patchConst(&n.Value, &n.Ident, consts, missing, false)
}
func (comp *compiler) patchTypeConst(n *ast.Type, consts map[string]uint64, missing *string) {
comp.patchConst(&n.Value, &n.Ident, consts, missing, true)
for _, col := range n.Colon {
comp.patchConst(&col.Value, &col.Ident, consts, missing, true)
}
}
func (comp *compiler) patchConst(val *uint64, id *string, consts map[string]uint64, missing *string, reset bool) bool {
if *id == "" {
return true
}
if v, ok := consts[*id]; ok {
if reset {
*id = ""
}
*val = v
return true
}
if missing != nil && *missing == "" {
*missing = *id
}
// 1 is slightly safer than 0 and allows to work-around e.g. an array size
// that comes from a const missing on an arch. Also see the TODO in patchConsts.
*val = 1
return false
}
func SerializeConsts(consts map[string]uint64, undeclared map[string]bool) []byte {
type nameValuePair struct {
declared bool
name string
val uint64
}
var nv []nameValuePair
for k, v := range consts {
nv = append(nv, nameValuePair{true, k, v})
}
for k := range undeclared {
nv = append(nv, nameValuePair{false, k, 0})
}
sort.Slice(nv, func(i, j int) bool {
return nv[i].name < nv[j].name
})
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "# AUTOGENERATED FILE\n")
for _, x := range nv {
if x.declared {
fmt.Fprintf(buf, "%v = %v\n", x.name, x.val)
} else {
fmt.Fprintf(buf, "# %v is not set\n", x.name)
}
}
return buf.Bytes()
}
func DeserializeConsts(data []byte, file string, eh ast.ErrorHandler) map[string]uint64 {
consts := make(map[string]uint64)
pos := ast.Pos{
File: file,
Line: 1,
}
ok := true
s := bufio.NewScanner(bytes.NewReader(data))
for ; s.Scan(); pos.Line++ {
line := s.Text()
if line == "" || line[0] == '#' {
continue
}
eq := strings.IndexByte(line, '=')
if eq == -1 {
eh(pos, "expect '='")
ok = false
continue
}
name := strings.TrimSpace(line[:eq])
val, err := strconv.ParseUint(strings.TrimSpace(line[eq+1:]), 0, 64)
if err != nil {
eh(pos, fmt.Sprintf("failed to parse int: %v", err))
ok = false
continue
}
if _, dup := consts[name]; dup {
eh(pos, fmt.Sprintf("duplicate const %q", name))
ok = false
continue
}
consts[name] = val
}
if err := s.Err(); err != nil {
eh(pos, fmt.Sprintf("failed to parse: %v", err))
ok = false
}
if !ok {
return nil
}
return consts
}
func DeserializeConstsGlob(glob string, eh ast.ErrorHandler) map[string]uint64 {
if eh == nil {
eh = ast.LoggingHandler
}
files, err := filepath.Glob(glob)
if err != nil {
eh(ast.Pos{}, fmt.Sprintf("failed to find const files: %v", err))
return nil
}
if len(files) == 0 {
eh(ast.Pos{}, fmt.Sprintf("no const files matched by glob %q", glob))
return nil
}
consts := make(map[string]uint64)
for _, f := range files {
data, err := ioutil.ReadFile(f)
if err != nil {
eh(ast.Pos{}, fmt.Sprintf("failed to read const file: %v", err))
return nil
}
consts1 := DeserializeConsts(data, filepath.Base(f), eh)
if consts1 == nil {
consts = nil
}
if consts != nil {
for n, v := range consts1 {
if old, ok := consts[n]; ok && old != v {
eh(ast.Pos{}, fmt.Sprintf(
"different values for const %q: %v vs %v", n, v, old))
return nil
}
consts[n] = v
}
}
}
return consts
}