aboutsummaryrefslogtreecommitdiff
path: root/tools/tf_fuzz/parser/tf_fuzz_grammar.y
blob: 9939e72bf3cd1c3d3594d201270eccad83db4efe (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
/*
 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

%{
#include <iostream>
#include <vector>
#include <set>

#include "class_forwards.hpp"
#include "data_blocks.hpp"
#include "boilerplate.hpp"
#include "gibberish.hpp"
#include "compute.hpp"
#include "string_ops.hpp"
#include "psa_asset.hpp"
#include "find_or_create_asset.hpp"
#include "template_line.hpp"
#include "tf_fuzz.hpp"
#include "sst_asset.hpp"
#include "crypto_asset.hpp"
#include "psa_call.hpp"
#include "crypto_call.hpp"
#include "sst_call.hpp"
#include "security_call.hpp"
#include "secure_template_line.hpp"
#include "sst_template_line.hpp"
#include "crypto_template_line.hpp"

/* These items are defined in tf_fuzz_grammar.l.  Note, however that, because
   of "name mangling," defining them as extern "C" may or may not be ideal,
   depending upon which compiler -- gcc vs. g++, compiles the output from lex.
   So far, it seems best without the extern "C", including also compiling
   under Visual Studio. */
/* extern "C"
{ */
  extern int yylineno;
  int yywrap() {return 1;}
  extern char yytext[];
  extern int yyleng;
/* } */

int yylex (void);
void yyerror (tf_fuzz_info *, const char *);
    /* Sends the yyparse() argument to yyerror(), probably, to print incorrect
       text it parsed. */

/* A few consts just to make code more comprehensible: */
const bool yes_fill_in_template = true;
const bool dont_fill_in_template = false;
const bool yes_create_call = true;
const bool dont_create_call = false;

tf_fuzz_info *rsrc;

/* These are object pointers used to parse the template and create the test.  Ac-
   tually, probably only templateLin is used for now, but this is a good outline of
   of the template_line class hierarchy. */
template_line                   *templateLin = nullptr;
  sst_template_line             *sstTemplateLin = nullptr;
    set_sst_template_line       *setSstTemplateLin = nullptr;
    read_sst_template_line      *reaSstTemplateLin = nullptr;
    remove_sst_template_line    *remSstTemplateLin = nullptr;
  policy_template_line          *polTemplateLin = nullptr;
    set_policy_template_line    *setPolTemplateLin = nullptr;
    read_policy_template_line   *reaPolTemplateLin = nullptr;
  key_template_line             *keyTemplateLin = nullptr;
    set_key_template_line       *setKeyTemplateLin = nullptr;
    read_key_template_line      *reaKeyTemplateLin = nullptr;
    remove_key_template_line    *remKeyTemplateLin = nullptr;
  security_template_line        *secTemplateLin = nullptr;
    security_hash_template_line *secHasTemplateLin = nullptr;
/* Call and asset objects are presumably not immediately needed, because the objects of
   these types are within the resource object, *rsrc, but even if only just to show
   that class hierarchy: */
psa_call                        *psaCal = nullptr;
  sst_call                      *sstCal = nullptr;
    sst_set_call                *sstSetCal = nullptr;
    sst_get_call                *sstGetCal = nullptr;
    sst_remove_call             *sstRemCal = nullptr;
  crypto_call                   *cryCal = nullptr;
    policy_call                 *polCal = nullptr;
      policy_set_call           *polSetCal = nullptr;
      policy_get_call           *polGetCal = nullptr;
    key_call                    *keyCal = nullptr;
      get_key_info_call         *getKeyInfCal = nullptr;
      set_key_call              *makKeyCal = nullptr;
      destroy_key_call          *desKeyCal = nullptr;
psa_asset                       *psaAst = nullptr;
  sst_asset                     *sstAst = nullptr;
  crypto_asset                  *cryAst = nullptr;
    policy_asset                *polAst = nullptr;
    key_asset                   *keyAst = nullptr;

/* For generating random, but readable/memorable, data: */
gibberish gib;
char gib_buff[4096];  // spew gibberish into here
int rand_data_length = 0;

/* General-utility variables: */
psa_asset_usage random_asset = psa_asset_usage::all;  /* pick what type of asset at random */
bool random_name;  /* template didn't specify name, so it's generated randomly */
string literal_data;  /* literal data for an asset value */

/* Holders for state in read commands: */
expect_info expect;  /* everything about expected results and data */
set_data_info set_data;  /* everything about setting the value of PSA-asset data */
asset_name_id_info asset_info;  /* everything about identifying assets */
bool assign_data_var_specified;
string assign_data_var;
bool print_data;  /* true to just print asset data to the test log */
bool hash_data;  /* true to just print asset data to the test log */

/* The following are more tied to the template syntax than to the resulting PSA calls */
string literal;  /* temporary holder for all string literals */
string identifier;  /* temporary holder for strings representing identifiers */
string var_name;  /* a variable name */
string asset_name;  /* as parsed, not yet put into asset_info */
string aid;  /* string-typed holder for an asset ID in a list thereof */
int nid;  /* same idea as aid, but for asset ID# lists */
size_t strFind1, strFind2;  /* for searching through strings */

/* Because of the parsing order, psa_calls of the specific type have to be
   push_back()ed onto rsrc->calls before their expected results are known.  Therefore,
   inject those results after parsing the expected results.  add_expect is a vector
   index of where to start "back-filling" the expect information. */
unsigned int add_expect = 0;

/* Temporaries: */
vector<psa_asset*>::iterator t_sst_asset;
vector<psa_asset*>::iterator t_key_asset;
vector<psa_asset*>::iterator t_policy_asset;
sst_call *t_sst_call = nullptr;
key_call *t_key_call = nullptr;
policy_call *t_policy_call = nullptr;
long number;  /* temporary holder for a number, e.g., sting form of UID */
int i, j, k;

/* Relating to template-statement blocks: */
vector<template_line*> template_block_vector;  /* (must be *pointers to* templates) */
vector<int> block_order;  /* "statisticalized" order of template lines in a block */
int nesting_level = 0;
    /* how many levels deep in { } nesting currently.  Initially only 0 or 1. */
bool shuffle_not_pick;
    /* true to shuffle statements in a block, rather than pick so-and-so
       number of them at random. */
int low_nmbr_lines = 1;  /* if picking so-and-so number of template lines from a ... */
int high_nmbr_lines = 1; /*    ... block at random, these are fewest and most lines. */
int exact_nmbr_lines = 1;

/* Shortcuts, to reduce code clutter, and reduce risk of coding errors. */
#define IVM(content) if(rsrc->verbose_mode){content}  /* IVM = "If Verbose Mode" */

using namespace std;


void set_purp_str (
    char *raw_purpose,  /* the purpose C string from parser */
    tf_fuzz_info *rsrc  /* test resources containing the actual test-purpose string */
) {
    size_t l;  /* temporary of size_t type */
    string purp_str = raw_purpose;
    strFind1 = purp_str.find (" ");
    purp_str = purp_str.substr (strFind1, purp_str.length());
    purp_str.erase (0, 1);  // (extra space)
    strFind1 = purp_str.rfind (";");
    purp_str = purp_str.substr (0, strFind1);
    l = 0;
    do {  /* escape all " chars (if not already escaped) */
        l = purp_str.find ("\"", l);
        if (   l < purp_str.length()) {  /* did find a quote character */
            if (   l == 0  /* it's the first character in the string*/
                || purp_str[l-1] != '\\' /* or it's not already escaped */
               ) {
                purp_str.insert (l, "\\");  /* then escape the " char */
                l++;  /* point l to the " again */
            }
            l++;  /* point l past the " */
        }
    } while (l < purp_str.length());
    rsrc->test_purpose = purp_str;
}

/* randomize_template_lines() chooses a template-line order in cases where they are to
   be randomized -- shuffled or random picked. */
void randomize_template_lines (
    bool shuffle_not_pick,  /* true to perform a shuffle operation rather than pick */
    int &low_nmbr_lines, /* if picking so-and-so number of template lines from a ... */
    int &high_nmbr_lines, /*    ... block at random, these are fewest and most lines. */
    int &exact_nmbr_lines,
    vector<template_line*> &template_block_vector,
    vector<int> &block_order,
    tf_fuzz_info *rsrc  /* test resources containing the actual test-purpose string */
) {
    set<int> template_used;  /* used for shuffle */
    low_nmbr_lines = (low_nmbr_lines < 0)?  0 : low_nmbr_lines;
    high_nmbr_lines = (high_nmbr_lines < 0)?  0 : high_nmbr_lines;
    if (low_nmbr_lines > high_nmbr_lines) {
        int swap = low_nmbr_lines;
        low_nmbr_lines = high_nmbr_lines;
        high_nmbr_lines = swap;
    }
    template_used.clear();
    if (shuffle_not_pick) {
        /* Choose a random order in which to generate all of the
           template lines in the block: */
        while (template_used.size() < template_block_vector.size()) {
            i = rand() % template_block_vector.size();
            if (template_used.find (i) == template_used.end()) {
                /* This template not already shuffled in. */
                block_order.push_back (i);
                template_used.insert (i);
            }
        }
        /* Done shuffling;  empty out the set: */
    } else {
        if (high_nmbr_lines == low_nmbr_lines) {
            exact_nmbr_lines = low_nmbr_lines;
                /* just in case the template says "3 to 3 of"... */
        } else {
            exact_nmbr_lines =   low_nmbr_lines
                               + (rand() % (  high_nmbr_lines
                                            - low_nmbr_lines + 1  )  );
        }
        for (int j = 0;  j < exact_nmbr_lines;  ++j) {
            /* Repeatedly choose a random template line from the block: */
            i = rand() % template_block_vector.size();
            block_order.push_back (i);
        }
    }
    IVM(cout << "Order of lines in block:  " << flush;
        for (auto i : block_order) {
            cout << i << "  ";
        }
        cout << endl;
    )
}

/* interpret_template_line() fills in random data, locates PSA assets, (etc.) and
   conditionally creates PSA calls for a given template line.  Note that there needs
   to be a single place where all of this is done, so that statement blocks can be
   randomized and then dispatched from a single point. */
void interpret_template_line (
    template_line *templateLin,  /* the template line to process */
    tf_fuzz_info *rsrc,  /* program resources in general */
    set_data_info set_data, psa_asset_usage random_asset,
    bool assign_data_var_specified, expect_info expect, bool print_data, bool hash_data,
    string asset_name, string assign_data_var,
    asset_name_id_info &asset_info,  /* everything about the asset(s) involved */
    bool create_call_bool,  /* true to create the PSA call at this time */
    bool create_asset_bool,  /* true to create the PSA asset at this time */
    bool fill_in_template,  /* true to back-fill info into template */
    int instance
        /* if further differentiation to the names or IDs is needed, make instance >0 */
) {
    const bool yes_fill_in_template = true;  /* just to improve readability */
    vector<psa_asset*>::iterator t_psa_asset;

    if (fill_in_template) {
        /* Set basic parameters from the template line: */
        templateLin->asset_info.id_n_not_name = asset_info.id_n_not_name;
        templateLin->asset_info.set_name (asset_name);
        /* Fill in state parsed from the template below: */
        templateLin->assign_data_var_specified = assign_data_var_specified;
        templateLin->assign_data_var.assign (assign_data_var);
        templateLin->expect = expect;
        templateLin->print_data = print_data;
        templateLin->hash_data = hash_data;
        templateLin->random_asset = random_asset;
        templateLin->set_data.random_data = set_data.random_data;
        templateLin->set_data.string_specified = set_data.literal_data_not_file;
            /* TODO:  is this right for multiple assets? */
        if (set_data.literal_data_not_file && !set_data.random_data) {
            templateLin->set_data.set (literal_data);
        }
        /* Save names or IDs to the template-line tracker: */
        for (auto id_no : asset_info.asset_id_n_vector) {
             templateLin->asset_info.asset_id_n_vector.push_back (id_no);
        }
        asset_info.asset_id_n_vector.clear();
        for (auto as_name : asset_info.asset_name_vector) {
             templateLin->asset_info.asset_name_vector.push_back (as_name);
        }
        asset_info.asset_name_vector.clear();
    }

    if (templateLin->random_asset != psa_asset_usage::all) {
        /* Just create the call tracker;  it will address this in simulation stage: */
        templateLin->setup_call (set_data, templateLin->set_data.random_data,
                                 yes_fill_in_template, create_call_bool,
                                 templateLin, rsrc   );
    } else if (asset_info.id_n_not_name) {
        /* Not random asset;  asset(s) by ID rather than name.  Go through all
           specified asset IDs: */
        uint64_t id_no;
        for (auto id_n :  templateLin->asset_info.asset_id_n_vector) {
            id_no = id_n + (uint64_t) instance * 10000UL;
            templateLin->asset_info.set_id_n(id_no);  /* just a holder */
            asset_name = templateLin->asset_info.make_id_n_based_name (id_no);
            templateLin->asset_info.set_calc_name (asset_name);
            templateLin->expect.data_var = var_name;
            if (!set_data.literal_data_not_file) {
                templateLin->set_data.set_file (set_data.file_path);
            }
            templateLin->setup_call (set_data, templateLin->set_data.random_data,
                                     fill_in_template, create_call_bool,
                                     templateLin, rsrc   );
        }
    } else {
        /* Not random asset, asset(s) specified by name.  Go through all specified
           asset names: */
        for (auto as_name :  templateLin->asset_info.asset_name_vector) {
            /* Also copy into template line object's local vector: */
            string t_string;
            t_string.assign(as_name);
            if (instance > 0) {
                t_string += "_" + to_string (instance);
            }
            templateLin->asset_info.set_name (t_string);
            /* Give each occurrence a different random ID: */
            templateLin->asset_info.set_id_n (100 + (rand() % 10000));
                /* TODO:  unlikely, but this *could* alias! */
            templateLin->setup_call (set_data, templateLin->set_data.random_data,
                                     yes_fill_in_template, create_call_bool,
                                     templateLin, rsrc   );
        }
    }
}

%}

%start lines

%union {int valueN; int tokenN; char *str;}
%token <tokenN> PURPOSE RAW_TEXT
%token <tokenN> SET READ REMOVE SECURE DONE  /* root commands */
%token <tokenN> SST KEY POLICY NAME UID STAR ACTIVE DELETED EQUAL DATA DFNAME
%token <tokenN> CHECK VAR HASH NEQ PRINT EXPECT PASS NOTHING ERROR  /* expected results */
%token <str> IDENTIFIER_TOK LITERAL_TOK FILE_PATH_TOK  /* variables and content */
%token <valueN> NUMBER_TOK  /* variables and content */
%token <tokenN> SEMICOLON SHUFFLE TO OF OPEN_BRACE CLOSE_BRACE  /* block structure */

%define parse.error verbose
%locations
%parse-param {tf_fuzz_info *rsrc}

%%

  /* Top-level syntax: */
lines:      /* nothing */
      | line lines {
            IVM(cout << "Lines:  Line number " << yylineno << "." << endl;)
        }
      ;

line:
        PURPOSE {
            IVM(cout << "Purpose line:  " << flush;)
            set_purp_str (yytext, rsrc);
            IVM(cout << rsrc->test_purpose << endl;)
            /* Just a precaution to make sure that these vectors start out empty.
               Should already be, but purpose is typically specified first: */
            asset_info.asset_id_n_vector.clear();
            asset_info.asset_name_vector.clear();
        }
      | block {
            /* TODO:  This code may not won't work with "secure hash neq ..." */
            IVM(cout << "Block of lines." << endl;)
            /* "Statisticalize" :-) the vector of template lines, then crank
               the selected lines in order here. */
            randomize_template_lines (shuffle_not_pick,
                low_nmbr_lines, high_nmbr_lines, exact_nmbr_lines,
                template_block_vector, block_order, rsrc
            );
            /* Vector block_order contains the sequence of template lines to be
               realized, in order.  Pop the indicated template line off the
               vector and generate code from it: */
            k = 0;  /* ID adder to at least help ensure uniqueness */
            for (int i : block_order) {
                templateLin = template_block_vector[i];
                /* Note that temLin will have its fields filled in already. */
                interpret_template_line (
                    templateLin, rsrc, set_data, random_asset,
                    assign_data_var_specified, expect, print_data, hash_data,
                    asset_name, assign_data_var, asset_info,
                    yes_create_call,  /* did not create call nor asset earlier */
                    yes_create_asset,
                    dont_fill_in_template,  /* but did fill it all in before */
                    0
                );
                k++;
                for (;  add_expect < rsrc->calls.size();  ++add_expect) {
                    templateLin->expect.copy_expect_to_call (rsrc->calls[add_expect]);
                }
            }
            templateLin->asset_info.asset_id_n_vector.clear();
            templateLin->asset_info.asset_name_vector.clear();
            /* Done.  Empty out the "statisticalization" vector: */
            block_order.clear();
            /* Empty out the vector of template lines; no longer needed. */
            template_block_vector.clear();
            --nesting_level;
            IVM(cout << "Finished coding block of lines." << endl;)
        }
      | command SEMICOLON {
            IVM(cout << "Command with no expect:  \"" << flush;)
            if (nesting_level == 0) {  /* if laying down the code now... */
                for (;  add_expect < rsrc->calls.size();  ++add_expect) {
                    templateLin->expect.copy_expect_to_call (rsrc->calls[add_expect]);
                }
                delete templateLin;  /* done with this template line */
            } else {
                /* The template line is now fully decoded, so stuff it onto
                   vector of lines to be "statisticalized": */
                template_block_vector.push_back (templateLin);
            }
            IVM(cout << yytext << "\"" << endl;)
        }
      | command expect SEMICOLON {
            IVM(cout << "Command with expect:  \"" << flush;)
            if (nesting_level == 0) {
                for (;  add_expect < rsrc->calls.size();  ++add_expect) {
                    templateLin->expect.copy_expect_to_call (rsrc->calls[add_expect]);
                }
                delete templateLin;
            } else {
                template_block_vector.push_back (templateLin);
            }
            IVM(cout << yytext << "\"" << endl;)
        }
      ;

command:
        set_command {
            IVM(cout << "Set command:  \"" << yytext << "\"" << endl;)
        }
      | remove_command {
            IVM(cout << "Remove command:  \"" << yytext << "\"" << endl;)
        }
      | read_command {
            IVM(cout << "Read command:  \"" << yytext << "\"" << endl;)
        }
      | secure_command {
            IVM(cout << "Security command:  \"" << yytext << "\"" << endl;)
        }
      | done_command {
            IVM(cout << "Done command:  \"" << yytext << "\"" << endl;)
        }
      ;

expect:
        EXPECT PASS  {
            IVM(cout << "Expect pass clause:  \"" << flush;)
            templateLin->expect.set_pf_pass();
            IVM(cout << yytext << "\"" << endl;)
        }
      | EXPECT NOTHING {
            IVM(cout << "Expect nothing clause:  \"" << flush;)
            templateLin->expect.set_pf_nothing();
            IVM(cout << yytext << "\"" << endl;)
        }
      | EXPECT IDENTIFIER {
            IVM(cout << "Expect error clause:  \"" << flush;)
            templateLin->expect.set_pf_error (identifier);
            IVM(cout << yytext << "\"" << endl;)
        }
      ;

  /* Root commands: */
set_command:
        SET SST sst_set_args {
            IVM(cout << "Set SST command:  \"" << flush;)
            templateLin = new set_sst_template_line (rsrc);
            interpret_template_line (
                templateLin, rsrc, set_data, random_asset,
                assign_data_var_specified, expect, print_data, hash_data,
                asset_name, assign_data_var, asset_info,
                nesting_level == 0 /* create call unless inside {} */,
                nesting_level == 0 /* similarly, create asset unless inside {} */,
                yes_fill_in_template, 0
            );
            IVM(cout << yytext << "\"" << endl;)
        }
      | SET KEY key_set_args {
            IVM(cout << "Set key command:  \"" << flush;)
            templateLin = new set_key_template_line (rsrc);
            interpret_template_line (
                templateLin, rsrc, set_data, random_asset,
                assign_data_var_specified, expect, print_data, hash_data,
                asset_name, assign_data_var, asset_info,
                nesting_level == 0 /* create call unless inside {} */,
                nesting_level == 0 /* similarly, create asset unless inside {} */,
                yes_fill_in_template, 0
            );
            IVM(cout << yytext << "\"" << endl;)
        }
      | SET POLICY policy_set_args {
            IVM(cout << "Set policy command:  \"" << flush;)
            templateLin = new set_policy_template_line (rsrc);
            interpret_template_line (
                templateLin, rsrc, set_data, random_asset,
                assign_data_var_specified, expect, print_data, hash_data,
                asset_name, assign_data_var, asset_info,
                nesting_level == 0 /* create call unless inside {} */,
                nesting_level == 0 /* similarly, create asset unless inside {} */,
                yes_fill_in_template, 0
            );
            rsrc->calls.push_back (t_policy_call);
            IVM(cout << yytext << "\"" << endl;)
        }
      ;

read_command:
        READ SST sst_read_args {
            IVM(cout << "Read SST command:  \"" << flush;)
            templateLin = new read_sst_template_line (rsrc);
            interpret_template_line (
                templateLin, rsrc, set_data, random_asset,
                assign_data_var_specified, expect, print_data, hash_data,
                asset_name, assign_data_var, asset_info,
                nesting_level == 0 /* create call unless inside {} */,
                dont_create_asset /* if no such asset exists, fail the call */,
                yes_fill_in_template, 0
            );
            IVM(cout << yytext << "\"" << endl;)
        }
      | READ KEY key_read_args {
            IVM(cout << "Read key command:  \"" << flush;)
            templateLin = new read_key_template_line (rsrc);
            interpret_template_line (
                templateLin, rsrc, set_data, random_asset,
                assign_data_var_specified, expect, print_data, hash_data,
                asset_name, assign_data_var, asset_info,
                nesting_level == 0 /* create call unless inside {} */,
                dont_create_asset /* if no such asset exists, fail the call */,
                yes_fill_in_template, 0
            );
            IVM(cout << yytext << "\"" << endl;)
        }
      | READ POLICY policy_read_args {
            IVM(cout << "Read policy command:  \"" << flush;)
            templateLin = new read_policy_template_line (rsrc);
            interpret_template_line (
                templateLin, rsrc, set_data, random_asset,
                assign_data_var_specified, expect, print_data, hash_data,
                asset_name, assign_data_var, asset_info,
                nesting_level == 0 /* create call unless inside {} */,
                dont_create_asset /* if no such asset exists, fail the call */,
                yes_fill_in_template, 0
            );
            IVM(cout << yytext << "\"" << endl;)
        }
      ;

remove_command:
        REMOVE SST sst_remove_args {
            IVM(cout << "Remove SST command:  \"" << flush;)
            templateLin = new remove_sst_template_line (rsrc);
            interpret_template_line (
                templateLin, rsrc, set_data, random_asset,
                assign_data_var_specified, expect, print_data, hash_data,
                asset_name, assign_data_var, asset_info,
                nesting_level == 0 /* create call unless inside {} */,
                dont_create_asset /* don't create an asset being deleted */,
                yes_fill_in_template, 0
            );
            IVM(cout << yytext << "\"" << endl;)
        }
      | REMOVE KEY key_remove_args {
            IVM(cout << "Remove key command:  \"" << flush;)
            templateLin = new remove_key_template_line (rsrc);
            templateLin->asset_info.set_name (asset_name);  // set in key_asset_name, below
            interpret_template_line (
                templateLin, rsrc, set_data, random_asset,
                assign_data_var_specified, expect, print_data, hash_data,
                asset_name, assign_data_var, asset_info,
                nesting_level == 0 /* create call unless inside {} */,
                dont_create_asset /* don't create an asset being deleted */,
                yes_fill_in_template, 0
            );
            IVM(cout << yytext << "\"" << endl;)
        }
      ;

secure_command: SECURE HASH NEQ ASSET_IDENTIFIER_LIST {
  /* TODO:  This needs to allow not only SST assets, but mix and match with others
             (keys especially) as well. */
            templateLin = new security_hash_template_line (rsrc);
            templateLin->asset_info.set_name (asset_name);
            templateLin->assign_data_var_specified = assign_data_var_specified;
            templateLin->assign_data_var.assign (assign_data_var);
            templateLin->expect = expect;
            templateLin->print_data = print_data;
            templateLin->hash_data = hash_data;
            templateLin->random_asset = random_asset;
            /* Hash checks are different from the rest in that there's a single
               "call" -- not a PSA call though -- for all of the assets cited in the
               template line.  In *other* cases, create a single call for *each*
               asset cited by the template line, but not in this case. */
            for (auto as_name : asset_info.asset_name_vector) {
                /* Also copy into template line object's local vector: */
                 templateLin->asset_info.asset_name_vector.push_back (as_name);
            }
            /* Don't need to locate the assets, so no searches required. */
            templateLin->expect.data_var = var_name;
            templateLin->setup_call (set_data, set_data.random_data, yes_fill_in_template,
                                     nesting_level == 0, templateLin, rsrc   );
            asset_info.asset_name_vector.clear();
            IVM(cout << yytext << "\"" << endl;)


        }
      ;

done_command: DONE {
            if (nesting_level != 0) {
                cerr << "\n\"done\" only available at outer-most { } nesting level."
                     << endl;
                exit (702);
            } else {
                YYACCEPT;
            }
        }
      ;

  /* Root-command parameters: */
sst_set_args:
        sst_asset_name DATA LITERAL {
            IVM(cout << "SST-create from literal data:  \"" << flush;)
            set_data.random_data = false;
            set_data.literal_data_not_file = true;
            literal.erase(0,1);  // zap the ""s
            literal.erase(literal.length()-1,1);
            literal_data.assign (literal);
            IVM(cout << yytext << "\"" << endl;)
        }
      | sst_asset_name DATA STAR {  /* TF-Fuzz supplies random data */
            IVM(cout << "SST-create from random data" << endl;)
            set_data.randomize();
            literal.assign (set_data.get());  /* just in case something uses literal */
        }
      | sst_asset_name {
            IVM(cout << "SST-create from random data (no 'data *')" << endl;)
            set_data.randomize();
            literal.assign (set_data.get());  /* just in case something uses literal */
        }
      | sst_asset_name VAR IDENTIFIER {  /* set from variable */
            IVM(cout << "SST-set set from variable:  \"" << flush;)
            assign_data_var.assign (identifier);
            assign_data_var_specified = true;
            expect.data_specified = false;
            expect.data_var_specified = false;
            IVM(cout << yytext << "\"" << endl;)
        }
      | sst_asset_name DFNAME sst_asset_set_file_path {
            set_data.literal_data_not_file = set_data.random_data = false;
            IVM(cout << "SST-create from file:  " << yytext << "\"" << endl;)
            /* TODO:  Need to decide whether the concept of using files to set SST
                       asset values has meaning, and if so, write code to write code to
                       set data appropriately from the file. */
        }
      ;

sst_read_args:
        sst_asset_name VAR IDENTIFIER {  /* dump to variable */
            IVM(cout << "SST-read dump to variable:  \"" << flush;)
            assign_data_var.assign (identifier);
            assign_data_var_specified = true;
            expect.data_specified = false;
            expect.data_var_specified = false;
            IVM(cout << yytext << "\"" << endl;)
        }
      | sst_asset_name CHECK sst_read_args_var_name {  /* check against variable */
            IVM(cout << "SST-read check against variable:  \""
                     << yytext << "\"" << endl;)
            set_data.set (literal);
            assign_data_var_specified = false;
            expect.data_specified = false;
            expect.data_var_specified = true;
            expect.data_var = identifier;
        }
      | sst_asset_name CHECK LITERAL {  /* check against literal */
            IVM(cout << "SST-read check against literal:  " << flush;)
            expect.data.assign (literal);
            expect.data.erase(0,1);    // zap the ""s
            expect.data.erase(expect.data.length()-1,1);
            assign_data_var_specified = false;  /* don't read variable */
            expect.data_specified = true;  /* check against literal data */
            expect.data_var_specified = false;  /* don't check against variable */
            IVM(cout << yytext << endl;)
        }
      | sst_asset_name PRINT {  /* print out content in test log */
            IVM(cout << "SST-read log to test log:  \"" << flush;)
            /* TODO:  set_data content probably doesn't need to be set here;
                       constructor probably sets it fine. */
            set_data.random_data = false;
            set_data.literal_data_not_file = true;
            assign_data_var_specified = false;
            expect.data_specified = false;
            expect.data_var_specified = false;
            print_data = true;
            IVM(cout << yytext << "\"" << endl;)
        }
      | sst_asset_name HASH {  /* hash the data and save for later comparison */
            IVM(cout << "SST-read hash for future data-leak detection:  \"" << flush;)
            /* TODO:  set_data content probably doesn't need to be set here;
                       constructor probably sets it fine. */
            set_data.random_data = false;
            set_data.literal_data_not_file = true;
            assign_data_var_specified = false;
            expect.data_specified = false;
            expect.data_var_specified = false;
            hash_data = true;
            rsrc->include_hashing_code = true;
            IVM(cout << yytext << "\"" << endl;)
        }
      | sst_asset_name DFNAME sst_asset_dump_file_path {  /* dump to file */
            IVM(cout << "SST-read dump to file:  \""
                     << yytext << "\"" << endl;)
            set_data.literal_data_not_file = set_data.random_data = false;
        }
      ;

sst_remove_args:
      sst_asset_name {
            IVM(cout << "SST-remove arguments:  \""
                     << yytext << "\"" << endl;)
      }
      ;

sst_asset_name:
        NAME ASSET_IDENTIFIER_LIST {
            IVM(cout << "SST-asset identifier list:  \"" << flush;)
            random_name = false;
            asset_name.assign (identifier);  /* TODO:  Not sure this ultimately has any effect... */
            random_asset = psa_asset_usage::all;  /* don't use random asset */
            asset_info.id_n_not_name = false;
            IVM(cout << yytext << "\"" << endl;)
        }
      | NAME STAR {
            IVM(cout << "SST-asset random identifier:  \"" << flush;)
            random_name = true;
            rand_data_length = 2 + (rand() % 10);
            gib.word (false, gib_buff, gib_buff + rand_data_length - 1);
            aid.assign (gib_buff);
            asset_info.asset_name_vector.push_back (aid);
            random_asset = psa_asset_usage::all;  /* don't use random asset */
            asset_info.id_n_not_name = false;
            IVM(cout << yytext << "\"" << endl;)
        }
      | UID ASSET_NUMBER_LIST {
            IVM(cout << "SST-asset UID list:  \"" << flush;)
            random_name = false;
            random_asset = psa_asset_usage::all;  /* don't use random asset */
            asset_info.id_n_not_name = true;
            asset_info.id_n_specified = true;
            IVM(cout << yytext << "\"" << endl;)
        }
      | UID STAR {
            IVM(cout << "SST-asset random UID:  \"" << flush;)
            asset_info.id_n_not_name = true;
            random_name = false;
            nid = 100 + (rand() % 10000);
            asset_info.asset_id_n_vector.push_back (nid);
            random_asset = psa_asset_usage::all;  /* don't use random asset */
            IVM(cout << yytext << "\"" << endl;)
        }
      | STAR ACTIVE {
            IVM(cout << "SST-asset random active:  \"" << flush;)
            random_asset = psa_asset_usage::active;
            asset_info.id_n_not_name = false;
            IVM(cout << yytext << "\"" << endl;)
        }
      | STAR DELETED {
            IVM(cout << "SST-asset random deleted:  \"" << flush;)
            random_asset = psa_asset_usage::deleted;
            asset_info.id_n_not_name = false;
            IVM(cout << yytext << "\"" << endl;)
        }
      ;

sst_asset_set_file_path:
      FILE_PATH {
            IVM(cout << "SST-asset-create file path:  \"" << flush;)
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

sst_read_args_var_name:
      IDENTIFIER {
            IVM(cout << "SST-read-arguments variable name:  \"" << flush;)
            var_name = yytext;
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

sst_asset_dump_file_path:
      FILE_PATH {
            IVM(cout << "SST-asset dump-file path:  \"" << flush;)
            set_data.file_path = yytext;
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

key_set_args:
      key_id POLICY policy_asset_name {
            IVM(cout << "Key-create arguments:  \""
                     << yytext << "\"" << endl;)
      }
      ;

key_remove_args:
      key_id {
            IVM(cout << "Key-remove arguments:  \""
                     << yytext << "\"" << endl;)
      }
      ;

key_read_args:
      key_id key_read_var_name {
            IVM(cout << "Key dump to variable:  \""
                     << yytext << "\"" << endl;)
      }
      ;

key_read_var_name:
      IDENTIFIER {
            IVM(cout << "Key-read variable name:  \""
                     << yytext << "\"" << endl;)
      }
      ;

key_id:
      IDENTIFIER {
            IVM(cout << "Key ID:  \""
                     << yytext << "\"" << endl;)
      }
      ;

policy_set_args:
      policy_asset_name {
            IVM(cout << "Policy-create arguments:  \""
                     << yytext << "\"" << endl;)
      }
      ;

policy_read_args:
      policy_asset_name policy_read_var_name {
            IVM(cout << "Policy dump to variable:  \""
                     << yytext << "\"" << endl;)
      }
      ;

policy_asset_name:
      IDENTIFIER {
            IVM(cout << "Policy Asset ID:  \"" << flush;)
            asset_name = identifier;
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

policy_read_var_name:
      IDENTIFIER {
            IVM(cout << "Policy read variable name:  \""
                     << yytext << "\"" << endl;)
      }
      ;

/* Code structuring: */
block:
        SHUFFLE block_content {
            IVM(cout << "Shuffled block:  \"" << flush;)
            if (nesting_level > 1) {
                cerr << "\nError:  Sorry, currently only one level of { } "
                     << "nesting is allowed." << endl;
                exit (500);
            }
            shuffle_not_pick = true;
            low_nmbr_lines = high_nmbr_lines = 0;  /* not used, but... */
            IVM(cout << yytext << "\"" << endl;)
        }
      | exact_sel_count OF block_content {
            IVM(cout << "Fixed number of lines from block:  \"" << flush;)
            shuffle_not_pick = false;
            /* low_nmbr_lines and high_nmbr_lines are set below. */
            IVM(cout << yytext << "\"" << endl;)
        }
      | low_sel_count TO high_sel_count OF block_content {
            IVM(cout << "Range number of lines from block:  \"" << flush;)
            if (nesting_level > 1) {
                cerr << "\nError:  Sorry, currently only one level of { } "
                     << "nesting is allowed." << endl;
                exit (502);
            }
            shuffle_not_pick = false;
            /* low_nmbr_lines and high_nmbr_lines are set below. */
            IVM(cout << yytext << "\"" << endl;)
        }
      ;

block_content:
        open_brace lines close_brace {
            IVM(cout << "Block content:  \"" << yytext << "\"" << endl;)
        }
      | line {
            IVM(cout << "Single-line would-be-block content:  \"" << flush;)
            IVM(cout << yytext << "\"" << endl;)
        }
      ;

open_brace:   OPEN_BRACE {
            IVM(cout << "Open brace:  \"" << flush;)
            template_block_vector.clear();  // clean slate of template lines
            nesting_level = 1;
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

close_brace:  CLOSE_BRACE {
            IVM(cout << "Close brace:  " << flush;)
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

  /* Low-level structures: */

  /* Please see comment before ASSET_IDENTIFIER_LIST, below. */
ASSET_NUMBER_LIST:    ASSET_NUMBER ASSET_NUMBERS;  /* at least one number */

ASSET_NUMBERS:      /* nothing, or */
      | ASSET_NUMBER ASSET_NUMBERS;

ASSET_NUMBER:     NUMBER_TOK {
            IVM(cout << "ASSET_NUMBER:  \"" << flush;)
            nid = atol(yytext);
            asset_info.asset_id_n_vector.push_back (nid);
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

  /* ASSET_IDENTIFIER* are used specifically for lists of assets in a template line.
     That, as opposed to list of identifers in general.  The difference is the need
     to queue ASSET_IDENTIFIERS up into asset_info.asset_name_vector, and have to do so
     here before they "vanish." */
ASSET_IDENTIFIER_LIST:  ASSET_IDENTIFIER ASSET_IDENTIFIERS;  /* (at least one) */

ASSET_IDENTIFIERS:
        /* nothing, or */
      | ASSET_IDENTIFIER ASSET_IDENTIFIERS;

ASSET_IDENTIFIER: IDENTIFIER_TOK {
            IVM(cout << "ASSET_IDENTIFIER:  \"" << flush;)
            aid = identifier = yytext;
            asset_info.asset_name_vector.push_back (aid);
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

IDENTIFIER: IDENTIFIER_TOK {
            IVM(cout << "IDENTIFIER:  \"" << flush;)
            identifier = yytext;
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

FILE_PATH: FILE_PATH_TOK {
            IVM(cout << "FILE_PATH:  \"" << flush;)
            set_data.file_path = yytext;
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

  /* These are related to randomized blocks of template lines: */

exact_sel_count:  NUMBER {
            IVM(cout << "Exact number of random template lines:  \"" << flush;)
            low_nmbr_lines = high_nmbr_lines = exact_nmbr_lines = number;
            ++nesting_level;
            IVM(cout << number << "\"" << endl;)
      }
      ;

low_sel_count:  NUMBER {
            IVM(cout << "Least number of random template lines:  \"" << flush;)
            low_nmbr_lines = number;
            IVM(cout << number << "\"" << endl;)
      }
      ;

high_sel_count:  NUMBER {
            IVM(cout << "Most number of random template lines:  \"" << flush;)
            high_nmbr_lines = number;
            ++nesting_level;
            IVM(cout << number << "\"" << endl;)
      }
      ;


  /* These are general-case numbers, literals and such: */

NUMBER:   NUMBER_TOK {
            IVM(cout << "NUMBER:  \"" << flush;)
            number = atol(yytext);
            IVM(cout << yytext << "\"" << endl;)
      }
      ;

LITERAL:  LITERAL_TOK {
            IVM(cout << "LITERAL:  " << flush;)
            literal = yytext;
            IVM(cout << yytext << endl;)
      }
      ;


%%