-
Notifications
You must be signed in to change notification settings - Fork 0
/
dtbcheck.c
1511 lines (1275 loc) · 38.8 KB
/
dtbcheck.c
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
/*
* dtbcheck.c - DTB checker
*
* (C) Copyright Pantelis Antoniou <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* (1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* (2) Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* (3)The name of the author may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <stdbool.h>
#include <assert.h>
#include <alloca.h>
#include <limits.h>
#include "utils.h"
#include "syexpr.h"
#include "base64.h"
#include "dt.h"
#include "nullgen.h"
#include "nullcheck.h"
#include "dtbcheck.h"
#include "yamlgen.h"
#ifdef CAN_RUN_EBPF
#include "ebpf.h"
#include "ebpf_dt.h"
#endif
#define NPSTACK_SIZE 8
#define SELECT_BASE 100000
#define ERROR_BASE 4000
#define BADTYPE_BASE 3000
#define EXISTS_BASE 2000
#define PROPC_BASE 1000
#define NODEC_BASE 0
struct frag {
struct list_head node;
const char *template;
int indent;
char *text;
};
struct var {
const char *name;
const char *value;
};
enum constraint_type {
t_select,
t_check
};
enum constraint_subtype {
st_select_ref,
st_select_prop,
st_check_category,
st_check_type,
st_check_rule,
};
struct constraint_desc {
struct list_head node;
struct constraint_desc *parent;
struct list_head children; /* constraints that operate on the same properties */
enum constraint_type type;
enum constraint_subtype subtype;
int idx; /* index in the code gen */
struct node *np; /* schema node */
struct node *npp; /* schema property node (that has the constraint) */
const char *constraint; /* if available */
const char *propname; /* propname */
int npstacksz; /* size of the stack */
struct node *npstack[]; /* the actual stack */
};
struct dtb_check_state {
/* copy from config */
const char *schema;
const char *schema_save;
const char *codegen;
bool save_temps;
/* schema loading */
struct yaml_dt_state *sdt;
/* codegen */
struct yaml_dt_state *cgdt;
struct node *cgpnp_check;
const char *input_tag;
const char *input_ext;
const char *output_tag;
const char *output_ext;
const char *compiler;
const char *cflags;
struct node *cg_common; /* -> common: */
const char *cg_common_prolog;
const char *cg_common_epilog;
struct node *cg_node; /* -> node: */
struct node *cg_node_select;
struct node *cg_node_check;
const char *cg_node_select_prolog;
const char *cg_node_select_epilog;
const char *cg_node_check_prolog;
const char *cg_node_check_epilog;
struct node *cg_property; /* -> property: */
struct node *cg_property_check;
const char *cg_property_check_prolog;
const char *cg_property_check_epilog;
const char *cg_property_check_badtype_prolog;
const char *cg_property_check_badtype_epilog;
struct node *cg_property_check_types;
struct node *cg_property_check_categories;
/* constraint list to map from error codes */
struct list_head clist;
};
#define to_dtbchk(_dt) ((struct dtb_check_state *)(_dt)->checker_state)
int output_frag(struct yaml_dt_state *dt, const struct var *vars, FILE *fp,
const char *template, int indent)
{
const char *s, *e, *le, *var;
char c;
int i, varlen;
char *vartmp;
enum {
normal,
escape,
dollar,
left_brace,
variable,
} state;
s = template;
e = s + strlen(s);
while (s < e) {
le = strchr(s, '\n');
if (!le)
le = e;
fprintf(fp, "%*s", indent * 4, "");
state = normal;
var = NULL;
while ((c = *s) && s < le) {
switch (state) {
case normal:
if (c == '\\') {
state = escape;
break;
}
if (c == '$') {
state = dollar;
break;
}
fputc(c, fp);
break;
case escape:
if (c == '$') {
fputc('$', fp);
state = normal;
break;
}
fputc('\\', fp);
fputc(c, fp);
state = normal;
break;
case dollar:
if (c != '{') {
dt_fatal(dt, "Illegal $ escape\n");
return -1;
}
state = left_brace;
break;
case left_brace:
state = variable;
var = s;
break;
case variable:
if (c != '}')
break;
varlen = s - var;
for (i = 0; vars && vars[i].name; i++) {
if (strlen(vars[i].name) == varlen &&
!memcmp(vars[i].name, var, varlen))
break;
}
if (!vars || !vars[i].name) {
vartmp = alloca(varlen + 1);
memcpy(vartmp, var, varlen);
vartmp[varlen] = '\0';
dt_fatal(dt, "Illegal variable: %s\n", vartmp);
return -1;
}
fputs(vars[i].value, fp);
var = NULL;
state = normal;
break;
}
s++;
}
if (state != normal) {
dt_fatal(dt, "Bad line state\n");
return -1;
}
fputc('\n', fp);
s = le;
if (le < e)
s++;
}
return 0;
}
static inline bool constraint_should_skip_content(struct yaml_dt_state *dt,
const struct constraint_desc *cd)
{
return cd->type == t_check && (cd->subtype == st_check_type ||
cd->subtype == st_check_category);
}
static int append_constraint(struct yaml_dt_state *dt,
struct constraint_desc *cd,
FILE *fp, const struct var *vars)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *cgdt = dtbchk->cgdt;
struct node *property_np = cd->npp;
const char *type_prolog, *type_epilog;
const char *category_prolog, *category_epilog;
struct node *nptype, *npcategory;
struct constraint_desc *cdt;
/* lookup failures are errors */
dt_set_error_on_failed_get(cgdt, true);
nptype = dt_get_node(cgdt, dtbchk->cg_property_check_types,
dt_get_string(cgdt, property_np, "type", 0, 0), 0);
type_prolog = dt_get_string(cgdt, nptype, "prolog", 0, 0);
type_epilog = dt_get_string(cgdt, nptype, "epilog", 0, 0);
npcategory = dt_get_node(cgdt, dtbchk->cg_property_check_categories,
dt_get_string(cgdt, property_np, "category", 0, 0), 0);
category_prolog = dt_get_string(cgdt, npcategory, "prolog", 0, 0);
category_epilog = dt_get_string(cgdt, npcategory, "epilog", 0, 0);
/* lookup failures are no more errors */
dt_set_error_on_failed_get(cgdt, false);
if (cgdt->error_flag)
dt_fatal(cgdt, "Bad codegen configuration\n");
output_frag(dt, vars, fp, type_prolog, 1);
output_frag(dt, vars, fp, dtbchk->cg_property_check_badtype_prolog, 1);
output_frag(dt, vars, fp, category_prolog, 1);
if (!constraint_should_skip_content(dt, cd)) {
output_frag(dt, vars, fp, dtbchk->cg_property_check_prolog, 1);
output_frag(dt, vars, fp, cd->constraint, 2);
output_frag(dt, vars, fp, dtbchk->cg_property_check_epilog, 1);
}
list_for_each_entry(cdt, &cd->children, node) {
if (constraint_should_skip_content(dt, cdt))
continue;
output_frag(dt, vars, fp, dtbchk->cg_property_check_prolog, 1);
output_frag(dt, vars, fp, cdt->constraint, 2);
output_frag(dt, vars, fp, dtbchk->cg_property_check_epilog, 1);
}
output_frag(dt, vars, fp, category_epilog, 1);
output_frag(dt, vars, fp, dtbchk->cg_property_check_badtype_epilog, 1);
output_frag(dt, vars, fp, type_epilog, 1);
return 0;
}
static int append_constraint_prop(struct yaml_dt_state *dt, struct node *npc,
const char *name, const void *data, int size,
const char *tag)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
struct property *prop;
struct ref *ref;
if (!data)
return 0;
prop = prop_alloc(to_tree(sdt), name);
prop->np = npc;
list_add_tail(&prop->node, &npc->properties);
ref = ref_alloc(to_tree(sdt), r_scalar, data, size, tag);
ref->prop = prop;
list_add_tail(&ref->node, &prop->refs);
return dt_resolve_ref(sdt, ref);
}
static int append_constraint_to_schema(struct yaml_dt_state *dt,
struct node *np,
const char *npname,
struct constraint_desc *cd)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
struct constraint_desc *cdt;
struct node *npt, *npc;
char namebuf[NODE_FULLNAME_MAX];
const char *typetxt, *subtypetxt;
struct property *prop;
const char *constraint_propname;
struct ref *ref;
int i;
/* add the contraint node if not there */
npt = node_get_child_by_name(to_tree(sdt), np, npname, 0);
if (!npt) {
npt = node_alloc(to_tree(sdt), npname, NULL);
list_add_tail(&npt->node, &np->children);
}
snprintf(namebuf, sizeof(namebuf), "c-%d", cd->idx);
npc = node_alloc(to_tree(sdt), namebuf, NULL);
list_add_tail(&npc->node, &npt->children);
snprintf(namebuf, sizeof(namebuf), "%d", cd->idx);
append_constraint_prop(dt, npc, "id",
namebuf, strlen(namebuf), NULL);
switch (cd->type) {
case t_select:
typetxt = "select";
break;
case t_check:
typetxt = "check";
break;
default:
typetxt = NULL;
break;
}
if (typetxt)
append_constraint_prop(dt, npc, "type",
typetxt, strlen(typetxt), "!str");
switch (cd->subtype) {
case st_select_ref:
subtypetxt = "select-ref";
constraint_propname = "constraint";
break;
case st_select_prop:
subtypetxt = "select-prop";
constraint_propname = "constraint";
break;
case st_check_category:
subtypetxt = "check-category";
constraint_propname = "category";
break;
case st_check_type:
subtypetxt = "check-type";
constraint_propname = "type";
break;
case st_check_rule:
subtypetxt = "check-rule";
constraint_propname = "constraint";
break;
default:
subtypetxt = NULL;
constraint_propname = NULL;
break;
}
if (subtypetxt)
append_constraint_prop(dt, npc, "subtype",
subtypetxt, strlen(subtypetxt), "!str");
if (cd->propname)
append_constraint_prop(dt, npc, "propname",
cd->propname, strlen(cd->propname),
"!str");
if (cd->constraint) {
append_constraint_prop(dt, npc, constraint_propname,
cd->constraint, strlen(cd->constraint),
"!str");
}
if (cd->np) {
dn_fullname(cd->np, namebuf, sizeof(namebuf));
append_constraint_prop(dt, npc, "rule-originator",
namebuf, strlen(namebuf),
"!str");
}
if (cd->npp) {
dn_fullname(cd->npp, namebuf, sizeof(namebuf));
append_constraint_prop(dt, npc, "rule-provider",
namebuf, strlen(namebuf),
"!str");
}
if (cd->npstacksz > 0) {
prop = prop_alloc(to_tree(sdt), "rule-stack");
prop->np = npc;
list_add_tail(&prop->node, &npc->properties);
for (i = 0; i < cd->npstacksz; i++) {
dn_fullname(cd->npstack[i], namebuf, sizeof(namebuf));
ref = ref_alloc(to_tree(sdt), r_scalar,
namebuf, strlen(namebuf), "!str");
ref->prop = prop;
list_add_tail(&ref->node, &prop->refs);
dt_resolve_ref(sdt, ref);
}
}
list_for_each_entry(cdt, &cd->children, node)
append_constraint_to_schema(dt, npc, "subconstraints", cdt);
return 0;
}
static int count_selected_single(struct yaml_dt_state *dt, struct node *np)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
return dt_get_rcount(sdt, np, "selected", 0);
}
static void count_selected(struct yaml_dt_state *dt, struct node *np, int *count)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
struct node *npp;
int i, rcount;
*count += count_selected_single(dt, np);
rcount = dt_get_rcount(sdt, np, "inherits", 0);
for (i = 0; i < rcount; i++) {
npp = dt_get_noderef(sdt, np, "inherits", 0, i);
if (!npp)
continue;
count_selected(dt, npp, count);
}
}
static int count_constraints_single(struct yaml_dt_state *dt, struct node *np)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
struct node *child, *npp;
int count, constraint_count;
/* constraints of the node */
count = dt_get_rcount(sdt, np, "constraint", 0);
/* constraints of the properties */
for_each_child_of_node(np, child) {
if (strcmp(child->name, "properties"))
continue;
for_each_child_of_node(child, npp) {
constraint_count = dt_get_rcount(sdt, npp, "constraint", 0);
if (dt_get_string(sdt, npp, "type", 0, 0))
constraint_count++;
count += constraint_count;
}
}
return count;
}
static void count_constraints(struct yaml_dt_state *dt, struct node *np, int *count)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
struct node *npp;
int i, rcount;
/* constraints of the node */
*count += count_constraints_single(dt, np);
/* iterating into inherits */
rcount = dt_get_rcount(sdt, np, "inherits", 0);
for (i = 0; i < rcount; i++) {
npp = dt_get_noderef(sdt, np, "inherits", 0, i);
if (!npp)
continue;
count_constraints(dt, npp, count);
}
}
void print_inherits(struct yaml_dt_state *dt, struct node *np, int depth)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
char namebuf[NODE_FULLNAME_MAX];
struct node *npp;
int i, rcount, scount, ccount;
scount = 0;
count_selected(dt, np, &scount);
ccount = 0;
count_constraints(dt, np, &ccount);
fprintf(stderr, "%*s", depth * 4, "");
fprintf(stderr, "%s - selected#=%d constraints#=%d\n",
dn_fullname(np, namebuf, sizeof(namebuf)),
scount, ccount);
rcount = dt_get_rcount(sdt, np, "inherits", 0);
for (i = 0; i < rcount; i++) {
npp = dt_get_noderef(sdt, np, "inherits", 0, i);
if (!npp) {
fprintf(stderr, "%*s", depth * 4, "");
fprintf(stderr, "#%d of inherits not a ref\n", i);
continue;
}
print_inherits(dt, npp, depth + 1);
}
}
static void save_file(struct yaml_dt_state *dt, const char *base,
enum constraint_type type, const char *ext,
void *data, size_t size)
{
FILE *fp;
char *filename;
int ret;
size_t nwrite;
ret = asprintf(&filename, "%s%s%s", base,
type == t_select ? "-select" : "-check",
ext);
if (ret == -1)
dt_fatal(dt, "Failed to allocate string %s%s%s\n", base,
type == t_select ? "-select" : "-check", ext);
fp = fopen(filename, "wb");
if (!fp)
dt_fatal(dt, "Failed to open %s%s%s\n", base,
type == t_select ? "-select" : "-check", ext);
nwrite = fwrite(data, 1, size, fp);
if (nwrite != size)
dt_fatal(dt, "Failed to write %s%s%s\n", base,
type == t_select ? "-select" : "-check", ext);
fclose(fp);
free(filename);
}
static bool constraint_eq(const struct constraint_desc *cd1,
const struct constraint_desc *cd2)
{
return cd1->type == cd2->type && cd1->subtype == cd2->subtype &&
!strcmp(cd1->constraint, cd2->constraint);
}
static void add_constraint(struct yaml_dt_state *dt,
struct list_head *clist,
enum constraint_type type, enum constraint_subtype subtype, int *idxp,
struct node **npstack, int top, int stacksz,
struct node *npp, const char *constraint,
const char *propname)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *cgdt = dtbchk->cgdt;
struct node *np = npstack[top];
struct constraint_desc *cd, *cdt, *cdn;
struct node *nptype, *npcategory;
struct list_head *lh;
size_t size;
/* constraint must have type and category properties */
nptype = dt_get_node(cgdt, dtbchk->cg_property_check_types,
dt_get_string(cgdt, npp, "type", 0, 0), 0);
if (!nptype)
return;
npcategory = dt_get_node(cgdt, dtbchk->cg_property_check_categories,
dt_get_string(cgdt, npp, "category", 0, 0), 0);
if (!npcategory)
return;
size = sizeof(*cd) + sizeof(*npstack) * (top + 1);
cd = malloc(size);
assert(cd);
memset(cd, 0, size);
cd->type = type;
cd->subtype = subtype;
cd->idx = *idxp;
cd->np = np;
cd->npp = npp;
cd->constraint = constraint;
cd->propname = propname;
cd->npstacksz = top + 1;
memcpy(cd->npstack, npstack, sizeof(*npstack) * cd->npstacksz);
INIT_LIST_HEAD(&cd->children);
cd->parent = NULL;
/* if a property constraint exist with that name */
list_for_each_entry(cdt, clist, node) {
if (cdt->type == cd->type && !strcmp(cdt->propname, cd->propname)) {
cd->parent = cdt;
break;
}
}
lh = cd->parent ? &cdt->children : clist;
if (cd->parent) {
if (constraint_eq(cd->parent, cd)) {
free(cd);
return;
}
/* detect duplicate constraints */
list_for_each_entry(cdn, lh, node) {
if (constraint_eq(cdn, cd)) {
free(cd);
return;
}
}
}
list_add_tail(&cd->node, lh);
(*idxp)++;
/*
{
char namebuf[NODE_FULLNAME_MAX];
const char *typename;
const char *subtypename;
int i;
switch (type) {
case t_select:
typename = "select";
break;
case t_check:
typename = "check";
break;
default:
typename = "*unknown*";
break;
}
switch (subtype) {
case st_select_ref:
subtypename = "ref";
break;
case st_select_prop:
subtypename = "prop";
break;
case st_check_category:
subtypename = "category";
break;
case st_check_type:
subtypename = "type";
break;
case st_check_rule:
subtypename = "rule";
break;
default:
subtypename = "*unknown*";
break;
}
fprintf(stderr, "%s type=%s, subtype=%s idx=%d\n", __func__, typename, subtypename, *idxp - 1);
fprintf(stderr, " np=%s\n", dn_fullname(np, namebuf, sizeof(namebuf)));
fprintf(stderr, " npp=%s\n", dn_fullname(npp, namebuf, sizeof(namebuf)));
fprintf(stderr, " constraint=%s\n", constraint ? : "<NULL>");
fprintf(stderr, " propname=%s\n", propname ? : "<NULL>");
for (i = 0; i < cd->npstacksz; i++)
fprintf(stderr, " npstack[%d]=%s\n", i,
dn_fullname(cd->npstack[i], namebuf, sizeof(namebuf)));
fprintf(stderr, " nptype=%s\n", dn_fullname(nptype, namebuf, sizeof(namebuf)));
fprintf(stderr, " npcategory=%s\n", dn_fullname(npcategory, namebuf, sizeof(namebuf)));
}
*/
}
static void free_constraint(struct yaml_dt_state *dt, struct constraint_desc *cd)
{
struct constraint_desc *cdc, *cdcn;
list_del(&cd->node);
list_for_each_entry_safe(cdc, cdcn, &cd->children, node)
free_constraint(dt, cdc);
free(cd);
}
static void collect_constraint_prop(struct yaml_dt_state *dt,
struct node **npstack, int top, int stacksz,
enum constraint_type type, enum constraint_subtype subtype,
struct list_head *clist, int *idxp, struct node *npp,
const char *propname)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
struct property *cprop;
const char *constraint;
struct ref *cref;
int k, l;
/* iterate over all constraints */
for (k = 0; (cprop = dt_get_property(sdt, npp, "constraint", k)); k++) {
/* iterate over all refs in the "constraints" property */
for (l = 0; (cref = dt_get_ref(sdt, cprop, l)); l++) {
constraint = dt_get_string(sdt, npp, "constraint", k, l);
if (!constraint) {
tree_error_at_ref(to_tree(sdt), cref,
"not a string\n");
continue;
}
add_constraint(dt, clist, type, subtype, idxp,
npstack, top, stacksz, npp, constraint,
propname);
}
}
}
static void collect_constraint_check_default(struct yaml_dt_state *dt,
struct node **npstack, int top, int stacksz,
struct list_head *clist, int *idxp, struct node *npp,
const char *propname)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
const char *category;
const char *ptype;
/* those may not exist if the rule is not defining properties */
category = dt_get_string(sdt, npp, "category", 0, 0);
if (category)
add_constraint(dt, clist, t_check, st_check_category, idxp,
npstack, top, stacksz, npp, category,
propname);
ptype = dt_get_string(sdt, npp, "type", 0, 0);
if (!ptype)
add_constraint(dt, clist, t_check, st_check_type, idxp,
npstack, top, stacksz, npp, ptype,
propname);
}
static void collect_constraint_node(struct yaml_dt_state *dt,
struct node **npstack, int top, int stacksz,
enum constraint_type type, struct list_head *clist, int *idxp)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct yaml_dt_state *sdt = dtbchk->sdt;
struct node *np = npstack[top];
struct node *npp, *npt, *nppt;
const char *propname;
struct property *prop;
struct ref *ref;
int i, j, m, n, rcount;
if (type == t_select) {
/* iterate over all selected properties */
for (i = 0; (prop = dt_get_property(sdt, np, "selected", i)); i++) {
/* iterate over all refs in the "selected" property */
for (j = 0; (ref = dt_get_ref(sdt, prop, j)); j++) {
/* try *ref first */
npp = dt_ref_noderef(sdt, ref);
if (npp) {
collect_constraint_prop(dt, npstack, top, stacksz,
type, st_select_ref, clist, idxp,
npp, npp->name);
continue;
}
/* try property name */
propname = dt_ref_string(sdt, ref);
if (!propname) {
tree_error_at_ref(to_tree(sdt), ref,
"Not a constraint reference or property\n");
continue;
}
/* now collect all the constraints for that property name */
for (m = top; m >= 0 && (npt = npstack[m]); m--) {
for (n = 0; (npp = dt_get_node(sdt, npt, "properties", n)); n++) {
for_each_child_of_node(npp, nppt) {
/* property name must match */
if (strcmp(nppt->name, propname))
continue;
collect_constraint_prop(dt, npstack, top, stacksz,
t_select, st_select_prop, clist, idxp,
nppt, propname);
}
}
}
}
}
} else if (type == t_check) {
/* now collect all the constraints for that property name */
for (m = top; m >= 0 && (npt = npstack[m]); m--) {
for (n = 0; (npp = dt_get_node(sdt, npt, "properties", n)); n++) {
for_each_child_of_node(npp, nppt) {
collect_constraint_check_default(dt, npstack, top, stacksz,
clist, idxp, nppt, nppt->name);
/* collect the rule constraints */
collect_constraint_prop(dt, npstack, top, stacksz,
t_check, st_check_rule, clist, idxp,
nppt, nppt->name);
}
}
}
} else
return;
/* collect constraints we've inherited */
rcount = dt_get_rcount(sdt, np, "inherits", 0);
for (i = 0; i < rcount; i++) {
npp = dt_get_noderef(sdt, np, "inherits", 0, i);
if (npp) {
assert(top + 1 < stacksz);
npstack[++top] = npp;
collect_constraint_node(dt, npstack, top, stacksz, type, clist, idxp);
}
}
}
static int prepare_schema_node(struct yaml_dt_state *dt,
struct yaml_dt_state *sdt, struct node *np,
int *idxp)
{
struct dtb_check_state *dtbchk = to_dtbchk(dt);
struct property *prop;
struct ref *ref;
int err;
char idxbuf[9];
#define NODE_NAME_IDX 0
#define PROPERTY_NAME_IDX 1
#define PROPERTY_INDEX_IDX 2
#define RULE_NAME_IDX 3
#define VARS_COUNT 4
struct var vars[VARS_COUNT + 1];
char *buf = NULL;
size_t size;
FILE *fp;
void *output;
size_t output_size;
void *b64_output;
size_t b64_output_size;
const char *source_name;
const char *output_name;;
const char *prolog;
const char *epilog;
struct list_head clist;
struct constraint_desc *cd, *cdn;
struct node *npstack[NPSTACK_SIZE];
struct node *npt;
int idx, ret;
source_name = "check-rule-source";
output_name = "check-rule-output";
prolog = dtbchk->cg_node_check_prolog;
epilog = dtbchk->cg_node_check_epilog;
INIT_LIST_HEAD(&clist);
npstack[0] = np;
collect_constraint_node(dt, npstack, 0, ARRAY_SIZE(npstack), t_select, &clist, idxp);
collect_constraint_node(dt, npstack, 0, ARRAY_SIZE(npstack), t_check, &clist, idxp);
/* nothing to do */
if (list_empty(&clist))
return 0;
if (!dtbchk->codegen)
dt_fatal(dt, "required codegen is missing\n");
/* open memstream */
fp = open_memstream(&buf, &size);
assert(fp);
/* setup the variables to expand */
snprintf(idxbuf, sizeof(idxbuf) - 1, "%d", -1);
idxbuf[sizeof(idxbuf) - 1] = '\0';
vars[NODE_NAME_IDX].name = "NODE_NAME";
vars[NODE_NAME_IDX].value = np->name;
vars[PROPERTY_NAME_IDX].name = "PROPERTY_NAME";
vars[PROPERTY_NAME_IDX].value = "";
vars[PROPERTY_INDEX_IDX].name = "PROPERTY_INDEX";
vars[PROPERTY_INDEX_IDX].value = idxbuf;
vars[RULE_NAME_IDX].name = "RULE_NAME";
vars[RULE_NAME_IDX].value = "";
vars[VARS_COUNT].name = NULL;
vars[VARS_COUNT].value = NULL;
output_frag(dt, vars, fp, dtbchk->cg_common_prolog, 0);
output_frag(dt, vars, fp, prolog, 0);
/* iterate over all constraints */
list_for_each_entry_safe(cd, cdn, &clist, node) {
/* update variables */
idx = cd->idx;
if (cd->type == t_select)
idx += SELECT_BASE; /* select errors return values under 100000 */
snprintf(idxbuf, sizeof(idxbuf) - 1, "%d", idx);
idxbuf[sizeof(idxbuf) - 1] = '\0';
vars[PROPERTY_INDEX_IDX].value = idxbuf;
vars[PROPERTY_NAME_IDX].value = cd->propname;
vars[RULE_NAME_IDX].value = cd->np->name;
dt_debug(dt, "Appending constraint for %s prop %s (%s): %s\n",
cd->np->name, cd->npp->name, cd->propname, cd->constraint);
append_constraint(dt, cd, fp, vars);
append_constraint_to_schema(dt, np, "constraints", cd);
list_del(&cd->node);
list_add_tail(&cd->node, &dtbchk->clist);
}
/* mark end */
snprintf(idxbuf, sizeof(idxbuf) - 1, "%d", -1);
idxbuf[sizeof(idxbuf) - 1] = '\0';
vars[PROPERTY_INDEX_IDX].value = idxbuf;
vars[PROPERTY_NAME_IDX].value = "";
vars[RULE_NAME_IDX].value = "";
output_frag(dt, vars, fp, epilog, 0);
output_frag(dt, vars, fp, dtbchk->cg_common_epilog, 0);
fclose(fp);
err = 0;
if (!size)
goto out;
/* add the source */
prop = prop_alloc(to_tree(sdt), source_name);
prop->np = np;