forked from google/syzkaller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck.go
1201 lines (1142 loc) · 30.8 KB
/
check.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 (
"errors"
"fmt"
"strings"
"github.com/google/syzkaller/pkg/ast"
"github.com/google/syzkaller/prog"
"github.com/google/syzkaller/sys/targets"
)
func (comp *compiler) typecheck() {
comp.checkDirectives()
comp.checkNames()
comp.checkFields()
comp.checkTypedefs()
comp.checkTypes()
}
func (comp *compiler) check() {
comp.checkTypeValues()
comp.checkAttributeValues()
comp.checkUnused()
comp.checkRecursion()
comp.checkLenTargets()
comp.checkConstructors()
comp.checkVarlens()
comp.checkDupConsts()
}
func (comp *compiler) checkDirectives() {
includes := make(map[string]bool)
incdirs := make(map[string]bool)
defines := make(map[string]bool)
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Include:
name := n.File.Value
path := n.Pos.File + "/" + name
if includes[path] {
comp.error(n.Pos, "duplicate include %q", name)
}
includes[path] = true
case *ast.Incdir:
name := n.Dir.Value
path := n.Pos.File + "/" + name
if incdirs[path] {
comp.error(n.Pos, "duplicate incdir %q", name)
}
incdirs[path] = true
case *ast.Define:
name := n.Name.Name
path := n.Pos.File + "/" + name
if defines[path] {
comp.error(n.Pos, "duplicate define %v", name)
}
defines[path] = true
}
}
}
func (comp *compiler) checkNames() {
calls := make(map[string]*ast.Call)
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Resource, *ast.Struct, *ast.TypeDef:
pos, typ, name := decl.Info()
if reservedName[name] {
comp.error(pos, "%v uses reserved name %v", typ, name)
continue
}
if builtinTypes[name] != nil {
comp.error(pos, "%v name %v conflicts with builtin type", typ, name)
continue
}
if prev := comp.resources[name]; prev != nil {
comp.error(pos, "type %v redeclared, previously declared as resource at %v",
name, prev.Pos)
continue
}
if prev := comp.typedefs[name]; prev != nil {
comp.error(pos, "type %v redeclared, previously declared as type alias at %v",
name, prev.Pos)
continue
}
if prev := comp.structs[name]; prev != nil {
_, typ, _ := prev.Info()
comp.error(pos, "type %v redeclared, previously declared as %v at %v",
name, typ, prev.Pos)
continue
}
switch n := decl.(type) {
case *ast.Resource:
comp.resources[name] = n
case *ast.TypeDef:
comp.typedefs[name] = n
case *ast.Struct:
comp.structs[name] = n
}
case *ast.IntFlags:
name := n.Name.Name
if name == "_" {
continue
}
if reservedName[name] {
comp.error(n.Pos, "flags uses reserved name %v", name)
continue
}
if prev := comp.intFlags[name]; prev != nil {
comp.error(n.Pos, "flags %v redeclared, previously declared at %v",
name, prev.Pos)
continue
}
comp.intFlags[name] = n
case *ast.StrFlags:
name := n.Name.Name
if reservedName[name] {
comp.error(n.Pos, "string flags uses reserved name %v", name)
continue
}
if prev := comp.strFlags[name]; prev != nil {
comp.error(n.Pos, "string flags %v redeclared, previously declared at %v",
name, prev.Pos)
continue
}
comp.strFlags[name] = n
case *ast.Call:
name := n.Name.Name
if prev := calls[name]; prev != nil {
comp.error(n.Pos, "syscall %v redeclared, previously declared at %v",
name, prev.Pos)
}
calls[name] = n
}
}
}
func (comp *compiler) checkFields() {
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Struct:
_, typ, name := n.Info()
comp.checkStructFields(n, typ, name)
case *ast.TypeDef:
if n.Struct != nil {
_, typ, _ := n.Struct.Info()
comp.checkStructFields(n.Struct, "template "+typ, n.Name.Name)
}
case *ast.Call:
name := n.Name.Name
comp.checkFieldGroup(n.Args, "argument", "syscall "+name)
if len(n.Args) > prog.MaxArgs {
comp.error(n.Pos, "syscall %v has %v arguments, allowed maximum is %v",
name, len(n.Args), prog.MaxArgs)
}
}
}
}
func (comp *compiler) checkStructFields(n *ast.Struct, typ, name string) {
comp.checkFieldGroup(n.Fields, "field", typ+" "+name)
if len(n.Fields) < 1 {
comp.error(n.Pos, "%v %v has no fields, need at least 1 field", typ, name)
}
}
func (comp *compiler) checkFieldGroup(fields []*ast.Field, what, ctx string) {
existing := make(map[string]bool)
for _, f := range fields {
fn := f.Name.Name
if fn == prog.ParentRef || fn == prog.SyscallRef {
comp.error(f.Pos, "reserved %v name %v in %v", what, fn, ctx)
}
if existing[fn] {
comp.error(f.Pos, "duplicate %v %v in %v", what, fn, ctx)
}
existing[fn] = true
}
}
func (comp *compiler) checkTypedefs() {
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.TypeDef:
if len(n.Args) == 0 {
// Non-template types are fully typed, so we check them ahead of time.
err0 := comp.errors
comp.checkType(checkCtx{}, n.Type, checkIsTypedef)
if err0 != comp.errors {
// To not produce confusing errors on broken type usage.
delete(comp.typedefs, n.Name.Name)
}
} else {
// For templates we only do basic checks of arguments.
names := make(map[string]bool)
for i, arg := range n.Args {
if arg.Name == "BASE" && i != len(n.Args)-1 {
comp.error(arg.Pos, "type argument BASE must be the last argument")
}
if names[arg.Name] {
comp.error(arg.Pos, "duplicate type argument %v", arg.Name)
}
names[arg.Name] = true
for _, c := range arg.Name {
if c >= 'A' && c <= 'Z' ||
c >= '0' && c <= '9' ||
c == '_' {
continue
}
comp.error(arg.Pos, "type argument %v must be ALL_CAPS",
arg.Name)
break
}
}
}
}
}
}
func (comp *compiler) checkTypes() {
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Resource:
comp.checkType(checkCtx{}, n.Base, checkIsResourceBase)
case *ast.Struct:
comp.checkStruct(checkCtx{}, n)
case *ast.Call:
comp.checkCall(n)
}
}
}
func (comp *compiler) checkTypeValues() {
for _, decl := range comp.desc.Nodes {
switch decl.(type) {
case *ast.Call, *ast.Struct, *ast.Resource, *ast.TypeDef:
comp.foreachType(decl, func(t *ast.Type, desc *typeDesc,
args []*ast.Type, base prog.IntTypeCommon) {
if desc.CheckConsts != nil {
desc.CheckConsts(comp, t, args, base)
}
for i, arg := range args {
if check := desc.Args[i].Type.CheckConsts; check != nil {
check(comp, arg)
}
}
})
}
}
}
func (comp *compiler) checkAttributeValues() {
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Struct:
for _, attr := range n.Attrs {
desc := structOrUnionAttrs(n)[attr.Ident]
if desc.CheckConsts != nil {
desc.CheckConsts(comp, n, attr)
}
}
}
}
}
func (comp *compiler) checkLenTargets() {
warned := make(map[string]bool)
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Call:
for _, arg := range n.Args {
checked := make(map[string]bool)
parents := []parentDesc{{fields: n.Args}}
comp.checkLenType(arg.Type, arg.Type, parents, checked, warned, true)
}
}
}
}
type parentDesc struct {
name string
fields []*ast.Field
}
func parentTargetName(s *ast.Struct) string {
parentName := s.Name.Name
if pos := strings.IndexByte(parentName, '['); pos != -1 {
// For template parents name is "struct_name[ARG1, ARG2]", strip the part after '['.
parentName = parentName[:pos]
}
return parentName
}
func (comp *compiler) checkLenType(t0, t *ast.Type, parents []parentDesc,
checked, warned map[string]bool, isArg bool) {
desc := comp.getTypeDesc(t)
if desc == typeStruct {
s := comp.structs[t.Ident]
// Prune recursion, can happen even on correct tree via opt pointers.
if checked[s.Name.Name] {
return
}
checked[s.Name.Name] = true
fields := s.Fields
if s.IsUnion {
fields = nil
}
parentName := parentTargetName(s)
parents = append(parents, parentDesc{name: parentName, fields: fields})
for _, fld := range s.Fields {
comp.checkLenType(fld.Type, fld.Type, parents, checked, warned, false)
}
warned[parentName] = true
return
}
_, args, _ := comp.getArgsBase(t, isArg)
for i, arg := range args {
argDesc := desc.Args[i]
if argDesc.Type == typeArgLenTarget {
comp.checkLenTarget(arg, t0, t, parents, warned)
} else if argDesc.Type == typeArgType {
comp.checkLenType(t0, arg, parents, checked, warned, argDesc.IsArg)
}
}
}
func (comp *compiler) checkLenTarget(arg, t0, t *ast.Type, parents []parentDesc, warned map[string]bool) {
targets := append([]*ast.Type{arg}, arg.Colon...)
for i, target := range targets {
if target.Ident == prog.ParentRef && len(targets) != 1 {
comp.error(target.Pos, "%v can't be part of path expressions", prog.ParentRef)
return
}
if target.Ident == prog.SyscallRef {
if i != 0 {
comp.error(target.Pos, "syscall can't be in the middle of path expressions")
return
}
if len(targets) == 1 {
comp.error(targets[0].Pos, "no argument name after syscall reference")
return
}
}
}
comp.checkLenTargetRec(t0, t, targets, parents, warned)
}
func (comp *compiler) checkLenTargetRec(t0, t *ast.Type, targets []*ast.Type,
parents []parentDesc, warned map[string]bool) {
if len(targets) == 0 {
return
}
target := targets[0]
targets = targets[1:]
fields := parents[len(parents)-1].fields
for _, fld := range fields {
if target.Ident != fld.Name.Name {
continue
}
if fld.Type == t0 {
comp.error(target.Pos, "%v target %v refers to itself", t.Ident, target.Ident)
return
}
if len(targets) == 0 {
if t.Ident == "len" {
typ, desc := comp.derefPointers(fld.Type)
if desc == typeArray && comp.isVarlen(typ.Args[0]) {
// We can reach the same struct multiple times starting from different
// syscall arguments. Warn only once.
if !warned[parents[len(parents)-1].name] {
comp.warning(target.Pos, "len target %v refer to an array with"+
" variable-size elements (do you mean bytesize?)",
target.Ident)
}
}
}
return
}
typ, desc := comp.derefPointers(fld.Type)
if desc != typeStruct {
comp.error(target.Pos, "%v path %v does not refer to a struct", t.Ident, target.Ident)
return
}
s := comp.structs[typ.Ident]
if s.IsUnion {
comp.error(target.Pos, "%v path %v does not refer to a struct", t.Ident, target.Ident)
return
}
parents = append(parents, parentDesc{name: parentTargetName(s), fields: s.Fields})
comp.checkLenTargetRec(t0, t, targets, parents, warned)
return
}
for pi := len(parents) - 1; pi >= 0; pi-- {
parent := parents[pi]
if parent.name != "" && (parent.name == target.Ident || target.Ident == prog.ParentRef) ||
parent.name == "" && target.Ident == prog.SyscallRef {
if len(targets) == 0 {
if t.Ident == "offsetof" {
comp.error(target.Pos, "%v must refer to fields", t.Ident)
return
}
} else {
parents1 := make([]parentDesc, pi+1)
copy(parents1, parents[:pi+1])
comp.checkLenTargetRec(t0, t, targets, parents1, warned)
}
return
}
}
comp.error(target.Pos, "%v target %v does not exist", t.Ident, target.Ident)
}
func CollectUnused(desc *ast.Description, target *targets.Target, eh ast.ErrorHandler) ([]ast.Node, error) {
comp := createCompiler(desc, target, eh)
comp.typecheck()
if comp.errors > 0 {
return nil, errors.New("typecheck failed")
}
nodes := comp.collectUnused()
if comp.errors > 0 {
return nil, errors.New("collectUnused failed")
}
return nodes, nil
}
func (comp *compiler) collectUnused() []ast.Node {
var unused []ast.Node
comp.used, _, _ = comp.collectUsed(false)
structs, flags, strflags := comp.collectUsed(true)
_, _, _ = structs, flags, strflags
note := func(n ast.Node) {
if pos, _, _ := n.Info(); pos.Builtin() {
return
}
unused = append(unused, n)
}
for name, n := range comp.intFlags {
if !flags[name] {
note(n)
}
}
for name, n := range comp.strFlags {
if !strflags[name] {
note(n)
}
}
for name, n := range comp.resources {
if !structs[name] {
note(n)
}
}
for name, n := range comp.structs {
if !structs[name] {
note(n)
}
}
for name, n := range comp.typedefs {
if !comp.usedTypedefs[name] {
note(n)
}
}
return unused
}
func (comp *compiler) collectUsed(all bool) (structs, flags, strflags map[string]bool) {
structs = make(map[string]bool)
flags = make(map[string]bool)
strflags = make(map[string]bool)
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Call:
if !all && n.NR == ^uint64(0) {
break
}
for _, arg := range n.Args {
comp.collectUsedType(structs, flags, strflags, arg.Type, true)
}
if n.Ret != nil {
comp.collectUsedType(structs, flags, strflags, n.Ret, true)
}
}
}
return
}
func (comp *compiler) collectUsedType(structs, flags, strflags map[string]bool, t *ast.Type, isArg bool) {
desc := comp.getTypeDesc(t)
if desc == typeResource {
r := comp.resources[t.Ident]
for r != nil && !structs[r.Name.Name] {
structs[r.Name.Name] = true
r = comp.resources[r.Base.Ident]
}
return
}
if desc == typeStruct {
if structs[t.Ident] {
return
}
structs[t.Ident] = true
s := comp.structs[t.Ident]
for _, fld := range s.Fields {
comp.collectUsedType(structs, flags, strflags, fld.Type, false)
}
return
}
if desc == typeFlags {
flags[t.Args[0].Ident] = true
return
}
if desc == typeString {
if len(t.Args) != 0 && t.Args[0].Ident != "" {
strflags[t.Args[0].Ident] = true
}
return
}
_, args, _ := comp.getArgsBase(t, isArg)
for i, arg := range args {
if desc.Args[i].Type == typeArgType {
comp.collectUsedType(structs, flags, strflags, arg, desc.Args[i].IsArg)
}
}
}
func (comp *compiler) checkUnused() {
for _, n := range comp.collectUnused() {
pos, typ, name := n.Info()
comp.error(pos, fmt.Sprintf("unused %v %v", typ, name))
}
}
type structDir struct {
Struct string
Dir prog.Dir
}
func (comp *compiler) checkConstructors() {
ctors := make(map[string]bool) // resources for which we have ctors
checked := make(map[structDir]bool)
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Call:
for _, arg := range n.Args {
comp.checkTypeCtors(arg.Type, prog.DirIn, true, ctors, checked)
}
if n.Ret != nil {
comp.checkTypeCtors(n.Ret, prog.DirOut, true, ctors, checked)
}
}
}
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Resource:
name := n.Name.Name
if !ctors[name] && comp.used[name] {
comp.error(n.Pos, "resource %v can't be created"+
" (never mentioned as a syscall return value or output argument/field)",
name)
}
}
}
}
func (comp *compiler) checkTypeCtors(t *ast.Type, dir prog.Dir, isArg bool,
ctors map[string]bool, checked map[structDir]bool) {
desc := comp.getTypeDesc(t)
if desc == typeResource {
// TODO(dvyukov): consider changing this to "dir == prog.DirOut".
// We have few questionable cases where resources can be created
// only by inout struct fields. These structs should be split
// into two different structs: one is in and second is out.
// But that will require attaching dir to individual fields.
if dir != prog.DirIn {
r := comp.resources[t.Ident]
for r != nil && !ctors[r.Name.Name] {
ctors[r.Name.Name] = true
r = comp.resources[r.Base.Ident]
}
}
return
}
if desc == typeStruct {
s := comp.structs[t.Ident]
name := s.Name.Name
key := structDir{name, dir}
if checked[key] {
return
}
checked[key] = true
for _, fld := range s.Fields {
comp.checkTypeCtors(fld.Type, dir, false, ctors, checked)
}
return
}
if desc == typePtr {
dir = genDir(t.Args[0])
}
_, args, _ := comp.getArgsBase(t, isArg)
for i, arg := range args {
if desc.Args[i].Type == typeArgType {
comp.checkTypeCtors(arg, dir, desc.Args[i].IsArg, ctors, checked)
}
}
}
func (comp *compiler) checkRecursion() {
checked := make(map[string]bool)
for _, decl := range comp.desc.Nodes {
switch n := decl.(type) {
case *ast.Resource:
comp.checkResourceRecursion(n)
case *ast.Struct:
var path []pathElem
comp.checkStructRecursion(checked, n, path)
}
}
}
func (comp *compiler) checkResourceRecursion(n *ast.Resource) {
var seen []string
for n != nil {
if arrayContains(seen, n.Name.Name) {
chain := ""
for _, r := range seen {
chain += r + "->"
}
chain += n.Name.Name
comp.error(n.Pos, "recursive resource %v", chain)
return
}
seen = append(seen, n.Name.Name)
n = comp.resources[n.Base.Ident]
}
}
type pathElem struct {
Pos ast.Pos
Struct string
Field string
}
func (comp *compiler) checkStructRecursion(checked map[string]bool, n *ast.Struct, path []pathElem) {
name := n.Name.Name
if checked[name] {
return
}
for i, elem := range path {
if elem.Struct != name {
continue
}
path = path[i:]
str := ""
for _, elem := range path {
str += fmt.Sprintf("%v.%v -> ", elem.Struct, elem.Field)
}
str += name
comp.error(path[0].Pos, "recursive declaration: %v (mark some pointers as opt)", str)
checked[name] = true
return
}
for _, f := range n.Fields {
path = append(path, pathElem{
Pos: f.Pos,
Struct: name,
Field: f.Name.Name,
})
comp.recurseField(checked, f.Type, path)
path = path[:len(path)-1]
}
checked[name] = true
}
func (comp *compiler) recurseField(checked map[string]bool, t *ast.Type, path []pathElem) {
desc := comp.getTypeDesc(t)
if desc == typeStruct {
comp.checkStructRecursion(checked, comp.structs[t.Ident], path)
return
}
_, args, base := comp.getArgsBase(t, false)
if desc == typePtr && base.IsOptional {
return // optional pointers prune recursion
}
for i, arg := range args {
if desc.Args[i].Type == typeArgType {
comp.recurseField(checked, arg, path)
}
}
}
func (comp *compiler) checkStruct(ctx checkCtx, n *ast.Struct) {
var flags checkFlags
if !n.IsUnion {
flags |= checkIsStruct
}
for _, f := range n.Fields {
comp.checkType(ctx, f.Type, flags)
}
comp.parseAttrs(structOrUnionAttrs(n), n, n.Attrs)
}
func (comp *compiler) checkCall(n *ast.Call) {
for _, a := range n.Args {
comp.checkType(checkCtx{}, a.Type, checkIsArg)
}
if n.Ret != nil {
comp.checkType(checkCtx{}, n.Ret, checkIsArg|checkIsRet)
}
comp.parseAttrs(callAttrs, n, n.Attrs)
}
type checkFlags int
const (
checkIsArg checkFlags = 1 << iota // immediate syscall arg type
checkIsRet // immediate syscall ret type
checkIsStruct // immediate struct field type
checkIsResourceBase // immediate resource base type
checkIsTypedef // immediate type alias/template type
)
type checkCtx struct {
instantiationStack []string
}
func (comp *compiler) checkType(ctx checkCtx, t *ast.Type, flags checkFlags) {
if unexpected, _, ok := checkTypeKind(t, kindIdent); !ok {
comp.error(t.Pos, "unexpected %v, expect type", unexpected)
return
}
desc := comp.getTypeDesc(t)
if desc == nil {
comp.error(t.Pos, "unknown type %v", t.Ident)
return
}
if desc == typeTypedef {
err0 := comp.errors
// Replace t with type alias/template target type inplace,
// and check the replaced type recursively.
comp.replaceTypedef(&ctx, t, flags)
if err0 == comp.errors {
comp.checkType(ctx, t, flags)
}
return
}
err0 := comp.errors
comp.checkTypeBasic(t, desc, flags)
if err0 != comp.errors {
return
}
args := comp.checkTypeArgs(t, desc, flags)
if err0 != comp.errors {
return
}
for i, arg := range args {
if desc.Args[i].Type == typeArgType {
var innerFlags checkFlags
if desc.Args[i].IsArg {
innerFlags |= checkIsArg
}
comp.checkType(ctx, arg, innerFlags)
} else {
comp.checkTypeArg(t, arg, desc.Args[i])
}
}
if err0 != comp.errors {
return
}
if desc.Check != nil {
_, args, base := comp.getArgsBase(t, flags&checkIsArg != 0)
desc.Check(comp, t, args, base)
}
}
func (comp *compiler) checkTypeBasic(t *ast.Type, desc *typeDesc, flags checkFlags) {
for i, col := range t.Colon {
if i >= desc.MaxColon {
comp.error(col.Pos, "unexpected ':'")
return
}
if flags&checkIsStruct == 0 {
comp.error(col.Pos, "unexpected ':', only struct fields can be bitfields")
return
}
}
if flags&checkIsTypedef != 0 && !desc.CanBeTypedef {
comp.error(t.Pos, "%v can't be type alias target", t.Ident)
return
}
if flags&checkIsResourceBase != 0 &&
(desc.CanBeResourceBase == nil || !desc.CanBeResourceBase(comp, t)) {
comp.error(t.Pos, "%v can't be resource base (int types can)", t.Ident)
return
}
canBeArg, canBeRet := false, false
if desc.CanBeArgRet != nil {
canBeArg, canBeRet = desc.CanBeArgRet(comp, t)
}
if flags&checkIsRet != 0 && !canBeRet {
comp.error(t.Pos, "%v can't be syscall return", t.Ident)
return
}
if flags&checkIsArg != 0 && !canBeArg {
comp.error(t.Pos, "%v can't be syscall argument", t.Ident)
return
}
}
func (comp *compiler) checkTypeArgs(t *ast.Type, desc *typeDesc, flags checkFlags) []*ast.Type {
args, opt := removeOpt(t)
if opt != nil {
if len(opt.Args) != 0 {
comp.error(opt.Pos, "opt can't have arguments")
}
if flags&checkIsResourceBase != 0 || desc.CantBeOpt {
what := "resource base"
if desc.CantBeOpt {
what = t.Ident
}
comp.error(opt.Pos, "%v can't be marked as opt", what)
return nil
}
}
addArgs := 0
needBase := flags&checkIsArg == 0 && desc.NeedBase
if needBase {
addArgs++ // last arg must be base type, e.g. const[0, int32]
}
if len(args) > len(desc.Args)+addArgs || len(args) < len(desc.Args)-desc.OptArgs+addArgs {
comp.error(t.Pos, "wrong number of arguments for type %v, expect %v",
t.Ident, expectedTypeArgs(desc, needBase))
return nil
}
if needBase {
base := args[len(args)-1]
args = args[:len(args)-1]
comp.checkTypeArg(t, base, typeArgBase)
}
return args
}
func (comp *compiler) replaceTypedef(ctx *checkCtx, t *ast.Type, flags checkFlags) {
typedefName := t.Ident
comp.usedTypedefs[typedefName] = true
if len(t.Colon) != 0 {
comp.error(t.Pos, "type alias %v with ':'", t.Ident)
return
}
typedef := comp.typedefs[typedefName]
// Handling optional BASE argument.
if len(typedef.Args) > 0 && typedef.Args[len(typedef.Args)-1].Name == "BASE" {
if flags&checkIsArg != 0 && len(t.Args) == len(typedef.Args)-1 {
t.Args = append(t.Args, &ast.Type{
Pos: t.Pos,
Ident: "intptr",
})
} else if len(t.Args) == len(typedef.Args) {
comp.checkTypeArg(t, t.Args[len(t.Args)-1], typeArgBase)
}
}
fullTypeName := ast.SerializeNode(t)
for i, prev := range ctx.instantiationStack {
if prev == fullTypeName {
ctx.instantiationStack = append(ctx.instantiationStack, fullTypeName)
path := ""
for j := i; j < len(ctx.instantiationStack); j++ {
if j != i {
path += " -> "
}
path += ctx.instantiationStack[j]
}
comp.error(t.Pos, "type instantiation loop: %v", path)
return
}
}
ctx.instantiationStack = append(ctx.instantiationStack, fullTypeName)
nargs := len(typedef.Args)
args := t.Args
if nargs != len(t.Args) {
if nargs == 0 {
comp.error(t.Pos, "type %v is not a template", typedefName)
} else {
if flags&checkIsArg != 0 && typedef.Args[len(typedef.Args)-1].Name == "BASE" {
nargs--
}
comp.error(t.Pos, "template %v needs %v arguments instead of %v",
typedefName, nargs, len(t.Args))
}
return
}
pos0 := t.Pos
if typedef.Type != nil {
*t = *typedef.Type.Clone().(*ast.Type)
if !comp.instantiate(t, typedef.Args, args) {
return
}
} else {
if comp.structs[fullTypeName] == nil {
inst := typedef.Struct.Clone().(*ast.Struct)
inst.Name.Name = fullTypeName
if !comp.instantiate(inst, typedef.Args, args) {
return
}
comp.checkStruct(*ctx, inst)
comp.desc.Nodes = append(comp.desc.Nodes, inst)
comp.structs[fullTypeName] = inst
}
*t = ast.Type{
Ident: fullTypeName,
}
}
t.Pos = pos0
// Remove base type if it's not needed in this context.
// If desc is nil, will return an error later when we typecheck the result.
desc := comp.getTypeDesc(t)
if desc != nil && flags&checkIsArg != 0 && desc.NeedBase {
baseTypePos := len(t.Args) - 1
if t.Args[baseTypePos].Ident == "opt" {
baseTypePos--
}
copy(t.Args[baseTypePos:], t.Args[baseTypePos+1:])
t.Args = t.Args[:len(t.Args)-1]
}
}
func (comp *compiler) instantiate(templ ast.Node, params []*ast.Ident, args []*ast.Type) bool {
if len(params) == 0 {
return true
}
argMap := make(map[string]*ast.Type)
for i, param := range params {
argMap[param.Name] = args[i]
}
argUsed := make(map[string]bool)
err0 := comp.errors
ast.PostRecursive(func(n ast.Node) {
templArg, ok := n.(*ast.Type)
if !ok {
return
}
if concreteArg := argMap[templArg.Ident]; concreteArg != nil {
argUsed[templArg.Ident] = true
origArgs := templArg.Args
if len(origArgs) != 0 && len(concreteArg.Args) != 0 {
comp.error(templArg.Pos, "both template parameter %v and its usage"+
" have sub-arguments", templArg.Ident)
return
}
*templArg = *concreteArg.Clone().(*ast.Type)
if len(origArgs) != 0 {
templArg.Args = origArgs
}
}
// TODO(dvyukov): somewhat hacky, but required for int8[0:CONST_ARG]
// Need more checks here. E.g. that CONST_ARG does not have subargs.
// And if CONST_ARG is a value, then use concreteArg.Value.
// Also need to error if CONST_ARG is a string.
if len(templArg.Colon) != 0 {
col := templArg.Colon[0]
if concreteArg := argMap[col.Ident]; concreteArg != nil {
argUsed[col.Ident] = true
col.Ident = concreteArg.Ident
col.Pos = concreteArg.Pos
}
}
})(templ)
for _, param := range params {
if !argUsed[param.Name] {
comp.error(argMap[param.Name].Pos,
"template argument %v is not used", param.Name)
}
}
return err0 == comp.errors
}
func (comp *compiler) checkTypeArg(t, arg *ast.Type, argDesc namedArg) {
desc := argDesc.Type
if len(desc.Names) != 0 {
if unexpected, _, ok := checkTypeKind(arg, kindIdent); !ok {
comp.error(arg.Pos, "unexpected %v for %v argument of %v type, expect %+v",
unexpected, argDesc.Name, t.Ident, desc.Names)
return
}
if !arrayContains(desc.Names, arg.Ident) {
comp.error(arg.Pos, "unexpected value %v for %v argument of %v type, expect %+v",
arg.Ident, argDesc.Name, t.Ident, desc.Names)
return
}
} else {