Gary Morrison | ced8c6f | 2020-02-27 19:35:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef TF_FUZZ_HPP |
| 9 | #define TF_FUZZ_HPP |
| 10 | |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | #include <iostream> |
| 14 | #include <fstream> |
| 15 | |
| 16 | |
| 17 | /* This project's header files #including other project headers quickly becomes |
| 18 | unrealistically complicated. The only solution is for each .cpp to include |
| 19 | the headers it needs. |
| 20 | #include "psa_call.hpp" |
| 21 | #include "sst_asset.hpp" |
| 22 | #include "crypto_asset.hpp" |
| 23 | #include "boilerplate.hpp" |
| 24 | */ |
| 25 | |
| 26 | using namespace std; |
| 27 | |
| 28 | /* class tf_fuzz_info mostly just groups together everything needed to gather, then |
| 29 | write out the test. This, so that they can be passed into the parser. */ |
| 30 | |
| 31 | class tf_fuzz_info |
| 32 | { |
| 33 | /* In creating a test, TF-Fuzz collects together a vector of strings cataloging |
| 34 | data structures used by PSA commands, and a vector of PSA call objects. Once |
| 35 | the template is completely parsed, write_test() writes it all out. The |
| 36 | process of creating these structures also requires the boilerplate text |
| 37 | strings. In the process of building the test, it must track PSA assets to |
| 38 | "model" expected results of calls. */ |
| 39 | |
| 40 | public: |
| 41 | // Data members (this class is mostly just to group stuff together, so public): |
| 42 | vector<string> prep_code; // variable declarations to write out to test file |
| 43 | vector<psa_call*> calls; |
| 44 | /* the calls to perform: Note: must be vector *psa_call; a vector of |
| 45 | psa_call does not allow (run-time) polymorphism. */ |
| 46 | boilerplate *bplate; // the boilerplate text for building the test |
| 47 | gibberish gibbergen; // the gibberish asset-data generator |
| 48 | crc32 hashgen; // simple 32-bit LFSR-based hashing generator |
| 49 | /* Note: The following asset-lists are kept in base-class type to allow a |
| 50 | common template-line processing function in tf_fuzz_grammar.y. */ |
| 51 | vector<psa_asset*> active_sst_asset; // list of known and usable SST assets |
| 52 | vector<psa_asset*> deleted_sst_asset; // deleted SST assets |
| 53 | vector<psa_asset*> invalid_sst_asset; // SST assets with invalid attributes |
| 54 | vector<psa_asset*> active_key_asset; // list of known and usable keys |
| 55 | vector<psa_asset*> deleted_key_asset; // deleted keys |
| 56 | vector<psa_asset*> invalid_key_asset; // keys with invalid attributes |
| 57 | vector<psa_asset*> active_policy_asset; // list of known, usable policies |
| 58 | vector<psa_asset*> deleted_policy_asset; // deleted policies |
| 59 | vector<psa_asset*> invalid_policy_asset; // policies with invalid attrs |
| 60 | string test_purpose; // one text substitution to be performed at the top level |
| 61 | long rand_seed; // the original random seed, whether passed in or defaulted |
| 62 | string template_file_name, test_output_file_name; |
| 63 | FILE *template_file; |
| 64 | /* handle to the test-template input file. Unfortunately I can't seem to |
| 65 | get lex/yacc to understand C++ file references, probably because I'm |
| 66 | "extern C"ing the Lex content (Yacc/Bison turns out to be a lot easier |
| 67 | to coerce into generating C++ code than (F)Lex). */ |
| 68 | ofstream output_C_file; // handle to the output C test file |
| 69 | bool verbose_mode; // true to "think aloud" |
| 70 | bool include_hashing_code; // true to instantiate the hashing code |
| 71 | // Methods: |
| 72 | asset_search find_or_create_sst_asset ( |
| 73 | psa_asset_search criterion, // what to search on |
| 74 | psa_asset_usage where, // where to search |
| 75 | string target_name, // ignored if not searching on name |
| 76 | uint64_t target_id, // ignored if not searching on ID (e.g., SST UID) |
| 77 | long &serial_no, // search by asset's unique serial number |
| 78 | bool create_asset, // true to create the asset if it doesn't exist |
| 79 | vector<psa_asset*>::iterator &asset // returns a pointer to asset |
| 80 | ); |
| 81 | asset_search find_or_create_key_asset ( |
| 82 | psa_asset_search criterion, // what to search on |
| 83 | psa_asset_usage where, // where to search |
| 84 | string target_name, // ignored if not searching on name |
| 85 | uint64_t target_id, // also ignored if not searching on ID (e.g., SST UID) |
| 86 | long &serial_no, // search by asset's unique serial number |
| 87 | bool create_asset, // true to create the asset if it doesn't exist |
| 88 | vector<psa_asset*>:: iterator &asset // returns iterator to asset |
| 89 | ); |
| 90 | asset_search find_or_create_policy_asset ( |
| 91 | psa_asset_search criterion, // what to search on |
| 92 | psa_asset_usage where, // where to search |
| 93 | string target_name, // ignored if not searching on name |
| 94 | uint64_t target_id, // also ignored if not searching on ID (e.g., SST UID) |
| 95 | long &serial_no, // search by asset's unique serial number |
| 96 | bool create_asset, // true to create the asset if it doesn't exist |
| 97 | vector<psa_asset*>::iterator &asset // returns iterator to asset |
| 98 | ); |
| 99 | asset_search find_or_create_psa_asset ( |
| 100 | psa_asset_type asset_type, // what type of asset to find |
| 101 | psa_asset_search criterion, // what to search on |
| 102 | psa_asset_usage where, // where to search |
| 103 | string target_name, // ignored if not searching on name |
| 104 | uint64_t target_id, // also ignored if not searching on ID (e.g., SST UID) |
| 105 | long &serial_no, // search by asset's unique serial number |
| 106 | bool create_asset, // true to create the asset if it doesn't exist |
| 107 | vector<psa_asset*>::iterator &asset // returns iterator to asset |
| 108 | ); |
| 109 | void teardown_test(void); // removes any PSA resources used in the test |
| 110 | void write_test (void); // returns success==true, fail==false |
| 111 | void parse_cmd_line_params (int argc, char* argv[]); |
| 112 | // parse command-line parameters, and open files |
| 113 | tf_fuzz_info (void); // (constructor) |
| 114 | ~tf_fuzz_info (void); |
| 115 | |
| 116 | protected: |
| 117 | // Data members: |
| 118 | vector<string> teardown_calls; |
| 119 | // list of PSA commands to remove assets left over upon test completion |
| 120 | // Methods: |
| 121 | |
| 122 | private: |
| 123 | // Data members: |
| 124 | // Methods: |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | /*-------------------------------------------------------------- |
| 129 | Helper functions: |
| 130 | --------------------------------------------------------------*/ |
| 131 | |
| 132 | |
| 133 | template<typename CALL_TYPE> |
| 134 | void define_call (set_data_info set_data, bool random_data, bool fill_in_template, |
| 135 | bool create_call, template_line *temLin, tf_fuzz_info *rsrc |
| 136 | ) { |
| 137 | CALL_TYPE *the_call; |
| 138 | gibberish gib; |
| 139 | char gib_buff[1000]; |
| 140 | string t_string; |
| 141 | |
| 142 | if (fill_in_template) { |
| 143 | if (set_data.literal_data_not_file) { |
| 144 | if (random_data) { |
| 145 | int rand_data_length = 10 + (rand() % 800); |
| 146 | gib.sentence (gib_buff, gib_buff + rand_data_length - 1); |
| 147 | t_string = gib_buff; |
| 148 | temLin->set_data.set_calculated (t_string); |
| 149 | } |
| 150 | } else { |
| 151 | if (set_data.file_path == "") { // catch the most likely failure at least! |
| 152 | cerr << "Error: Tool-internal: Please report error " |
| 153 | << "#407 to the TF-Fuzz developers." << endl; |
| 154 | exit(407); |
| 155 | } |
| 156 | temLin->set_data.set_file (set_data.file_path); |
| 157 | // set in sst_asset_make_file_path |
| 158 | } |
| 159 | } |
| 160 | if (create_call) { |
| 161 | if (temLin->how_asset_found == asset_search::unsuccessful) { |
| 162 | cerr << "Error: Tool-internal: Please report error " |
| 163 | << "#401 to the TF-Fuzz developers." << endl; |
| 164 | exit(401); |
| 165 | } |
| 166 | the_call = new CALL_TYPE (rsrc, temLin->call_ser_no, |
| 167 | temLin->how_asset_found); |
| 168 | rsrc->calls.push_back(the_call); /* (note: this is not a memory leak!) */ |
| 169 | temLin->copy_template_to_asset(); |
| 170 | if (!temLin->copy_template_to_call()) { |
| 171 | cerr << "Error: Tool-internal: Please report error " |
| 172 | << "#402 to the TF-Fuzz developers." << endl; |
| 173 | exit(402); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | #endif // #ifndef TF_FUZZ_HPP |