ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
// 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 generates sys descriptions of syscalls, types and resources
// from textual descriptions.
package compiler
 
import (
   "fmt"
   "sort"
   "strconv"
   "strings"
 
   "github.com/google/syzkaller/pkg/ast"
   "github.com/google/syzkaller/prog"
   "github.com/google/syzkaller/sys/targets"
)
 
// Overview of compilation process:
// 1. ast.Parse on text file does tokenization and builds AST.
//    This step catches basic syntax errors. AST contains full debug info.
// 2. ExtractConsts as AST returns set of constant identifiers.
//    This step also does verification of include/incdir/define AST nodes.
// 3. User translates constants to values.
// 4. Compile on AST and const values does the rest of the work and returns Prog
//    containing generated prog objects.
// 4.1. assignSyscallNumbers: uses consts to assign syscall numbers.
//      This step also detects unsupported syscalls and discards no longer
//      needed AST nodes (inlcude, define, comments, etc).
// 4.2. patchConsts: patches Int nodes referring to consts with corresponding values.
//      Also detects unsupported syscalls, structs, resources due to missing consts.
// 4.3. check: does extensive semantical checks of AST.
// 4.4. gen: generates prog objects from AST.
 
// Prog is description compilation result.
type Prog struct {
   Resources   []*prog.ResourceDesc
   Syscalls    []*prog.Syscall
   StructDescs []*prog.KeyedStruct
   // Set of unsupported syscalls/flags.
   Unsupported map[string]bool
   // Returned if consts was nil.
   fileConsts map[string]*ConstInfo
}
 
// Compile compiles sys description.
func Compile(desc *ast.Description, consts map[string]uint64, target *targets.Target, eh ast.ErrorHandler) *Prog {
   if eh == nil {
       eh = ast.LoggingHandler
   }
   comp := &compiler{
       desc:         desc.Clone(),
       target:       target,
       eh:           eh,
       ptrSize:      target.PtrSize,
       unsupported:  make(map[string]bool),
       resources:    make(map[string]*ast.Resource),
       typedefs:     make(map[string]*ast.TypeDef),
       structs:      make(map[string]*ast.Struct),
       intFlags:     make(map[string]*ast.IntFlags),
       strFlags:     make(map[string]*ast.StrFlags),
       used:         make(map[string]bool),
       usedTypedefs: make(map[string]bool),
       structDescs:  make(map[prog.StructKey]*prog.StructDesc),
       structNodes:  make(map[*prog.StructDesc]*ast.Struct),
       structVarlen: make(map[string]bool),
   }
   for name, n := range builtinTypedefs {
       comp.typedefs[name] = n
       comp.usedTypedefs[name] = true
   }
   for name, n := range builtinStrFlags {
       comp.strFlags[name] = n
   }
   comp.typecheck()
   // The subsequent, more complex, checks expect basic validity of the tree,
   // in particular corrent number of type arguments. If there were errors,
   // don't proceed to avoid out-of-bounds references to type arguments.
   if comp.errors != 0 {
       return nil
   }
   if consts == nil {
       fileConsts := comp.extractConsts()
       if comp.errors != 0 {
           return nil
       }
       return &Prog{fileConsts: fileConsts}
   }
   if comp.target.SyscallNumbers {
       comp.assignSyscallNumbers(consts)
   }
   comp.patchConsts(consts)
   comp.check()
   if comp.errors != 0 {
       return nil
   }
   for _, w := range comp.warnings {
       eh(w.pos, w.msg)
   }
   syscalls := comp.genSyscalls()
   prg := &Prog{
       Resources:   comp.genResources(),
       Syscalls:    syscalls,
       StructDescs: comp.genStructDescs(syscalls),
       Unsupported: comp.unsupported,
   }
   if comp.errors != 0 {
       return nil
   }
   return prg
}
 
type compiler struct {
   desc     *ast.Description
   target   *targets.Target
   eh       ast.ErrorHandler
   errors   int
   warnings []warn
   ptrSize  uint64
 
   unsupported  map[string]bool
   resources    map[string]*ast.Resource
   typedefs     map[string]*ast.TypeDef
   structs      map[string]*ast.Struct
   intFlags     map[string]*ast.IntFlags
   strFlags     map[string]*ast.StrFlags
   used         map[string]bool // contains used structs/resources
   usedTypedefs map[string]bool
 
   structDescs  map[prog.StructKey]*prog.StructDesc
   structNodes  map[*prog.StructDesc]*ast.Struct
   structVarlen map[string]bool
}
 
type warn struct {
   pos ast.Pos
   msg string
}
 
func (comp *compiler) error(pos ast.Pos, msg string, args ...interface{}) {
   comp.errors++
   comp.eh(pos, fmt.Sprintf(msg, args...))
}
 
func (comp *compiler) warning(pos ast.Pos, msg string, args ...interface{}) {
   comp.warnings = append(comp.warnings, warn{pos, fmt.Sprintf(msg, args...)})
}
 
func (comp *compiler) structIsVarlen(name string) bool {
   if varlen, ok := comp.structVarlen[name]; ok {
       return varlen
   }
   s := comp.structs[name]
   if s.IsUnion {
       if varlen, _ := comp.parseUnionAttrs(s); varlen {
           comp.structVarlen[name] = true
           return true
       }
   }
   comp.structVarlen[name] = false // to not hang on recursive types
   varlen := false
   for _, fld := range s.Fields {
       if comp.isVarlen(fld.Type) {
           varlen = true
           break
       }
   }
   comp.structVarlen[name] = varlen
   return varlen
}
 
func (comp *compiler) parseUnionAttrs(n *ast.Struct) (varlen bool, size uint64) {
   size = sizeUnassigned
   for _, attr := range n.Attrs {
       switch attr.Ident {
       case "varlen":
           if len(attr.Args) != 0 {
               comp.error(attr.Pos, "%v attribute has args", attr.Ident)
           }
           varlen = true
       case "size":
           size = comp.parseSizeAttr(attr)
       default:
           comp.error(attr.Pos, "unknown union %v attribute %v",
               n.Name.Name, attr.Ident)
       }
   }
   return
}
 
func (comp *compiler) parseStructAttrs(n *ast.Struct) (packed bool, size, align uint64) {
   size = sizeUnassigned
   for _, attr := range n.Attrs {
       switch {
       case attr.Ident == "packed":
           if len(attr.Args) != 0 {
               comp.error(attr.Pos, "%v attribute has args", attr.Ident)
           }
           packed = true
       case attr.Ident == "align_ptr":
           if len(attr.Args) != 0 {
               comp.error(attr.Pos, "%v attribute has args", attr.Ident)
           }
           align = comp.ptrSize
       case strings.HasPrefix(attr.Ident, "align_"):
           if len(attr.Args) != 0 {
               comp.error(attr.Pos, "%v attribute has args", attr.Ident)
           }
           a, err := strconv.ParseUint(attr.Ident[6:], 10, 64)
           if err != nil {
               comp.error(attr.Pos, "bad struct %v alignment %v",
                   n.Name.Name, attr.Ident[6:])
               continue
           }
           if a&(a-1) != 0 || a == 0 || a > 1<<30 {
               comp.error(attr.Pos, "bad struct %v alignment %v (must be a sane power of 2)",
                   n.Name.Name, a)
           }
           align = a
       case attr.Ident == "size":
           size = comp.parseSizeAttr(attr)
       default:
           comp.error(attr.Pos, "unknown struct %v attribute %v",
               n.Name.Name, attr.Ident)
       }
   }
   return
}
 
func (comp *compiler) parseSizeAttr(attr *ast.Type) uint64 {
   if len(attr.Args) != 1 {
       comp.error(attr.Pos, "%v attribute is expected to have 1 argument", attr.Ident)
       return sizeUnassigned
   }
   sz := attr.Args[0]
   if unexpected, _, ok := checkTypeKind(sz, kindInt); !ok {
       comp.error(sz.Pos, "unexpected %v, expect int", unexpected)
       return sizeUnassigned
   }
   if sz.HasColon || len(sz.Args) != 0 {
       comp.error(sz.Pos, "size attribute has colon or args")
       return sizeUnassigned
   }
   return sz.Value
}
 
func (comp *compiler) getTypeDesc(t *ast.Type) *typeDesc {
   if desc := builtinTypes[t.Ident]; desc != nil {
       return desc
   }
   if comp.resources[t.Ident] != nil {
       return typeResource
   }
   if comp.structs[t.Ident] != nil {
       return typeStruct
   }
   if comp.typedefs[t.Ident] != nil {
       return typeTypedef
   }
   return nil
}
 
func (comp *compiler) getArgsBase(t *ast.Type, field string, dir prog.Dir, isArg bool) (
   *typeDesc, []*ast.Type, prog.IntTypeCommon) {
   desc := comp.getTypeDesc(t)
   if desc == nil {
       panic(fmt.Sprintf("no type desc for %#v", *t))
   }
   args, opt := removeOpt(t)
   com := genCommon(t.Ident, field, sizeUnassigned, dir, opt != nil)
   base := genIntCommon(com, 0, false)
   if desc.NeedBase {
       base.TypeSize = comp.ptrSize
       if !isArg {
           baseType := args[len(args)-1]
           args = args[:len(args)-1]
           base = typeInt.Gen(comp, baseType, nil, base).(*prog.IntType).IntTypeCommon
       }
   }
   return desc, args, base
}
 
func (comp *compiler) foreachType(n0 ast.Node,
   cb func(*ast.Type, *typeDesc, []*ast.Type, prog.IntTypeCommon)) {
   switch n := n0.(type) {
   case *ast.Call:
       for _, arg := range n.Args {
           comp.foreachSubType(arg.Type, true, cb)
       }
       if n.Ret != nil {
           comp.foreachSubType(n.Ret, true, cb)
       }
   case *ast.Resource:
       comp.foreachSubType(n.Base, false, cb)
   case *ast.Struct:
       for _, f := range n.Fields {
           comp.foreachSubType(f.Type, false, cb)
       }
   case *ast.TypeDef:
       if len(n.Args) == 0 {
           comp.foreachSubType(n.Type, false, cb)
       }
   default:
       panic(fmt.Sprintf("unexpected node %#v", n0))
   }
}
 
func (comp *compiler) foreachSubType(t *ast.Type, isArg bool,
   cb func(*ast.Type, *typeDesc, []*ast.Type, prog.IntTypeCommon)) {
   desc, args, base := comp.getArgsBase(t, "", prog.DirIn, isArg)
   cb(t, desc, args, base)
   for i, arg := range args {
       if desc.Args[i].Type == typeArgType {
           comp.foreachSubType(arg, desc.Args[i].IsArg, cb)
       }
   }
}
 
func removeOpt(t *ast.Type) ([]*ast.Type, *ast.Type) {
   args := t.Args
   if last := len(args) - 1; last >= 0 && args[last].Ident == "opt" {
       return args[:last], args[last]
   }
   return args, nil
}
 
func (comp *compiler) parseIntType(name string) (size uint64, bigEndian bool) {
   be := strings.HasSuffix(name, "be")
   if be {
       name = name[:len(name)-len("be")]
   }
   size = comp.ptrSize
   if name != "intptr" {
       size, _ = strconv.ParseUint(name[3:], 10, 64)
       size /= 8
   }
   return size, be
}
 
func toArray(m map[string]bool) []string {
   delete(m, "")
   var res []string
   for v := range m {
       if v != "" {
           res = append(res, v)
       }
   }
   sort.Strings(res)
   return res
}
 
func arrayContains(a []string, v string) bool {
   for _, s := range a {
       if s == v {
           return true
       }
   }
   return false
}