blob: 9aa7762941ed7f5f56dc586b1c9e8f2ba1b8588b [file] [log] [blame]
Karl Zhang3de5ab12021-05-31 11:45:48 +08001/*
2 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include "class_forwards.hpp"
9
10#include "boilerplate.hpp"
11#include "gibberish.hpp"
12#include "compute.hpp"
13#include "string_ops.hpp"
14#include "data_blocks.hpp"
15#include "psa_asset.hpp"
16#include "find_or_create_asset.hpp"
17#include "template_line.hpp"
18#include "tf_fuzz.hpp"
19#include "crypto_asset.hpp"
20#include "psa_call.hpp"
21#include "sst_call.hpp"
22#include "sst_asset.hpp"
23#include "crypto_asset.hpp"
24#include "variables.hpp"
25
26
27
28/**********************************************************************************
29 Methods of class sst_set_call follow:
30**********************************************************************************/
31
32sst_set_call::sst_set_call (tf_fuzz_info *test_state, // (constructor)
33 long &call_ser_no,
34 asset_search how_asset_found)
35 : sst_call(test_state, call_ser_no, how_asset_found)
36{
37 // Copy the boilerplate text into local buffers:
38 prep_code.assign (""); // will fill in, depending upon template line content
39 call_code.assign (test_state->bplate->bplate_string[set_sst_call]);
40 check_code.assign (test_state->bplate->bplate_string[set_sst_check]);
41 call_description = "SST-set call";
42}
43sst_set_call::~sst_set_call (void)
44{
45 return; // just to have something to pin a breakpoint onto
46}
47
48bool sst_set_call::copy_call_to_asset (void)
49{
50 vector<psa_asset*>::iterator found_asset;
51
52 found_asset = resolve_asset (yes_create_asset, psa_asset_usage::all);
53 // Copy over everything relevant:
54 if (asset_info.how_asset_found != asset_search::not_found) {
55 asset_info.the_asset = reinterpret_cast<sst_asset*>(*found_asset);
56 /* Note: The vector is base-class, but the assets in this list
57 themselves *really are* sst_asset-type objects. */
58 int i = asset_info.the_asset->set_data.n_set_vars; // save this
59 asset_info.the_asset->set_data = set_data; // TO DO: does this make sense?!
60 asset_info.the_asset->set_data.n_set_vars = set_data.n_set_vars = ++i;
61 asset_info.the_asset->set_data.flags_string.assign (set_data.flags_string);
62 if (asset_info.how_asset_found == asset_search::created_new) {
63 asset_info.the_asset->asset_info.name_specified = asset_info.name_specified;
64 asset_info.the_asset->asset_info.set_name (asset_info.get_name());
65 asset_info.the_asset->asset_info.asset_ser_no = asset_info.asset_ser_no;
66 asset_info.the_asset->asset_info.id_n = asset_info.id_n;
67 }
68 }
69 return true;
70}
71
72bool sst_set_call::copy_asset_to_call (void)
73{
74 // Get updated asset info from the asset:
75 asset_info.asset_ser_no = asset_info.the_asset->asset_info.asset_ser_no;
76 asset_info.id_n = asset_info.the_asset->asset_info.id_n;
77 exp_data.n_exp_vars = asset_info.the_asset->exp_data.n_exp_vars;
78 exp_data.data = asset_info.the_asset->exp_data.data;
79 return true;
80}
81
82void sst_set_call::fill_in_prep_code (void)
83{
84 string var_name, length_var_name, var_name_suffix, length_var_name_suffix,
85 temp_string;
86 vector<variable_info>::iterator assign_variable;
87
88 if (assign_data_var_specified) {
89 var_name.assign (assign_data_var + "_data");
90 length_var_name.assign (assign_data_var + "_length");
91 /* If actual-data variable doesn't already exist, create variable tracker,
92 and write declaration for it: */
93 assign_variable = test_state->find_var (assign_data_var);
94 if (assign_variable == test_state->variable.end()) {
95 // No such variable exists, so:
96 test_state->make_var (assign_data_var);
97 assign_variable = test_state->find_var (assign_data_var);
98 prep_code.append (test_state->bplate->bplate_string[declare_big_string]);
99 find_replace_1st ("$var", var_name, prep_code);
100 temp_string = (char *) assign_variable->value;
101 find_replace_1st ("$init", temp_string, prep_code);
102 // Actual-data length:
103 prep_code.append (test_state->bplate->bplate_string[declare_size_t]);
104 find_replace_1st ("$var", length_var_name, prep_code);
105 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
106 // Offset (always 0 for now):
107 find_replace_1st ("$offset", "0", prep_code);
108 }
109 } else {
110 // Single string of two lines declaring string data and its length:
111 var_name_suffix = "_set_data";
112 length_var_name_suffix = "_set_length";
113 if (set_data.n_set_vars > 0) {
114 var_name_suffix += "_" + to_string(set_data.n_set_vars);
115 length_var_name_suffix += "_" + to_string(set_data.n_set_vars);
116 // We'll increment set_data.n_set_vars after we fill in the call itself.
117 }
118 var_name.assign (asset_info.get_name() + var_name_suffix);
119 length_var_name.assign (asset_info.get_name() + length_var_name_suffix);
120 prep_code = test_state->bplate->bplate_string[declare_string];
121 find_replace_1st ("$var", var_name, prep_code);
122 find_replace_1st ("$init", set_data.get(), prep_code);
123 temp_string.assign (test_state->bplate->bplate_string[declare_int]);
124 find_replace_1st ("static int", "static uint32_t", temp_string);
125 prep_code.append (temp_string);
126 find_replace_1st ("$var", length_var_name, prep_code);
127 find_replace_1st ("$init", to_string(set_data.get().length()), prep_code);
128 }
129}
130
131void sst_set_call::fill_in_command (void)
132{
133 string var_name, length_var_name, var_name_suffix, length_var_name_suffix,
134 temp_string;
135
136 // Fill in preceding comment:
137 if (asset_info.how_asset_found == asset_search::created_new) {
138 find_replace_1st ("$op", "Creating", call_code);
139 } else {
140 find_replace_1st ("$op", "Resetting", call_code);
141 }
142 if (asset_info.name_specified) {
143 find_replace_1st ("$description", "\"" + asset_info.get_name() + ",\"",
144 call_code);
145 } else {
146 find_replace_1st ("$description",
147 "UID = " + to_string((long) asset_info.id_n), call_code);
148 }
149 if (set_data.string_specified) {
150 find_replace_1st ("$data_source",
151 "\"" + set_data.get().substr (0, 10) + "...\"",
152 call_code);
153 } else if (set_data.file_specified) {
154 find_replace_1st ("$data_source", "from file " + set_data.file_path,
155 call_code);
156 } else {
157 find_replace_1st (" $data_source", "", call_code);
158 }
159 // Fill in the PSA command itself:
160 if (assign_data_var_specified) {
161 var_name.assign (assign_data_var + "_data");
162 length_var_name.assign (assign_data_var + "_length");
163 } else {
164 var_name_suffix = "_set_data";
165 if (set_data.n_set_vars > 0) {
166 var_name_suffix += "_" + to_string(set_data.n_set_vars);
167 }
168 var_name.assign (asset_info.get_name() + var_name_suffix);
169 length_var_name_suffix = "_set_length";
170 if (set_data.n_set_vars > 0) {
171 length_var_name_suffix += "_" + to_string(set_data.n_set_vars);
172 }
173 length_var_name.assign (asset_info.get_name() + length_var_name_suffix);
174 }
175 find_replace_1st ("$data", var_name, call_code);
176 find_replace_1st ("$flags", set_data.flags_string, call_code);
177 string id_string = to_string((long) asset_info.id_n);
178 find_replace_1st ("$uid", id_string, call_code);
179 find_replace_1st ("$length", length_var_name, call_code);
180 // Figure out what expected results:
181 if ( set_data.flags_string == "PSA_STORAGE_FLAG_WRITE_ONCE"
182 && set_data.n_set_vars > 0) {
183 exp_data.pf_specified = true;
184 exp_data.pf_result_string = "PSA_ERROR_NOT_PERMITTED";
185 }
186 calc_result_code();
187}
188
189/**********************************************************************************
190 End of methods of class sst_set_call.
191**********************************************************************************/
192
193
194/**********************************************************************************
195 Methods of class sst_get_call follow:
196**********************************************************************************/
197
198sst_get_call::sst_get_call (tf_fuzz_info *test_state, // (constructor)
199 long &call_ser_no,
200 asset_search how_asset_found)
201 : sst_call(test_state, call_ser_no, how_asset_found)
202{
203 // Copy the boilerplate text into local buffers:
204 prep_code.assign ("");
205 call_code.assign (test_state->bplate->bplate_string[get_sst_call]);
206 check_code.assign ("");
207 // depends upon the particular usage; will get it in fill_in_command()
208 call_description = "SST-get call";
209}
210sst_get_call::~sst_get_call (void)
211{
212 return; // just to have something to pin a breakpoint onto
213}
214
215bool sst_get_call::copy_call_to_asset (void)
216{
217 vector<psa_asset*>::iterator found_asset;
218
219 found_asset = resolve_asset (dont_create_asset, psa_asset_usage::active);
220 if (asset_info.how_asset_found != asset_search::not_found) {
221 // will be found for set calls, but not necessarily others
222 asset_info.the_asset = reinterpret_cast<sst_asset*>(*found_asset);
223 // Note: Vector is of base-class type, but the assets *are* sst_asset.
224 /* Locating the asset is all we need to do here; copy_asset_to_call() will
225 do the rest. */
226 }
227 return true;
228 // TODO: Shouldn't data be copied over?
229}
230
231bool sst_get_call::copy_asset_to_call (void)
232{
233 if (asset_info.the_asset != nullptr) {
234 // will be found for set calls, but not necessarily others
235 set_data.string_specified = asset_info.the_asset->set_data.string_specified;
236 set_data.file_specified = asset_info.the_asset->set_data.file_specified;
237 set_data.set (asset_info.the_asset->set_data.get());
238 set_data.flags_string = asset_info.the_asset->set_data.flags_string;
239 asset_info.id_n = asset_info.the_asset->asset_info.id_n;
240 asset_info.asset_ser_no = asset_info.the_asset->asset_info.asset_ser_no;
241 asset_info.name_specified = asset_info.the_asset->asset_info.name_specified;
242 asset_info.the_asset->exp_data.n_exp_vars++;
243 exp_data.n_exp_vars = asset_info.the_asset->exp_data.n_exp_vars;
244 }
245 return true;
246}
247
248void sst_get_call::fill_in_prep_code (void)
249{
250 string var_base, var_name, length_var_name, temp_string, var_name_suffix,
251 expected;
252 vector<variable_info>::iterator act_variable, exp_variable;
253
254 if (!(print_data || hash_data)) {
255 // Checking asset data verbatim against expected, so:
256 if (exp_data.data_var_specified) {
257 // Template specified a variable name to "check" against; use that:
258 var_base.assign (exp_data.data_var);
259 exp_variable = test_state->find_var (var_base);
260 if (exp_variable == test_state->variable.end()) {
261 test_state->make_var (var_base);
262 exp_variable = test_state->find_var (var_base);
263 var_name = var_base + "_data";
264 prep_code.append (test_state->bplate->bplate_string[declare_string]);
265 find_replace_1st ("$var", var_name, prep_code);
266 temp_string = (char *) exp_variable->value;
267 find_replace_1st ("$init", temp_string, prep_code);
268 // Expected-data length:
269 temp_string.assign (test_state->bplate->bplate_string[declare_int]);
270 find_replace_1st ("static int", "static size_t", temp_string);
271 }
272 } else {
273 if (exp_data.data_specified) {
274 // Checking against literal expected data:
275 expected.assign (exp_data.data);
276 } else {
277 // Check against what we believe the asset to contain:
278 expected.assign (set_data.get());
279 }
280 var_name_suffix = "_exp_data";
281 if (exp_data.n_exp_vars > 0) {
282 var_name_suffix =
283 var_name_suffix + "_" + to_string(exp_data.n_exp_vars);
284 }
285 var_name.assign (asset_info.get_name() + var_name_suffix);
286 prep_code.assign(test_state->bplate->bplate_string[declare_string]);
287 find_replace_1st("$var", var_name, prep_code);
288 find_replace_1st("$init", expected, prep_code);
289 }
290 }
291 // Actual data:
292 if (assign_data_var_specified) {
293 var_base.assign (assign_data_var);
294 } else {
295 var_base.assign (asset_info.get_name() + "_act");
296 }
297 var_name.assign (var_base + "_data");
298 length_var_name.assign (var_base + "_length");
299 /* If actual-data variable doesn't already exist, create variable tracker,
300 and write declaration for it: */
301 act_variable = test_state->find_var (var_base);
302 if (act_variable == test_state->variable.end()) {
303 test_state->make_var (var_base);
304 act_variable = test_state->find_var (var_base);
305 prep_code.append (test_state->bplate->bplate_string[declare_big_string]);
306 find_replace_1st ("$var", var_name, prep_code);
307 temp_string = (char *) act_variable->value;
308 find_replace_1st ("$init", temp_string, prep_code);
309 // Actual-data length:
310 temp_string.assign (test_state->bplate->bplate_string[declare_int]);
311 find_replace_1st ("static int", "static size_t", temp_string);
312 prep_code.append (temp_string);
313 find_replace_1st ("$var", length_var_name, prep_code);
314 find_replace_1st ("$init", to_string(temp_string.length()), prep_code);
315 // Offset (always 0 for now):
316 find_replace_1st ("$offset", "0", prep_code);
317 }
318 // If hashing the (actual) data, then create a variable for that:
319 if (hash_data && !act_variable->hash_declared) {
320 var_name = var_base + "_hash";
321 prep_code.append (test_state->bplate->bplate_string[declare_generic]);
322 // where to put the hash of the data
323 find_replace_1st ("$type", "uint32_t", prep_code);
324 find_replace_1st ("$var", var_name, prep_code);
325 find_replace_1st ("$init", "0", prep_code); // for now...
326 act_variable->hash_declared = true;
327 }
328}
329
330void sst_get_call::fill_in_command (void)
331{
332 string exp_var_name, act_var_name, act_data_length, hash_var_name,
333 id_string, var_name_suffix;
334
335/* TODO: Flesh-out/fix this (it was a good try/start, but not quite right):
336 // Fill in preceding comment:
337 if (asset_info.how_asset_found == asset_search::created_new) {
338 find_replace_1st ("$op", "Creating", call_code);
339 } else {
340 find_replace_1st ("$op", "Resetting", call_code);
341 }
342 if (asset_info.name_specified) {
343 find_replace_1st ("$description", "\"" + asset_info.get_name() + ",\"",
344 call_code);
345 } else {
346 find_replace_1st ("$description",
347 "UID = " + to_string((long) asset_info.id_n), call_code);
348 }
349 if (set_data.string_specified) {
350 find_replace_1st ("$data_source",
351 "\"" + data.substr (0, 10) + "...\"",
352 call_code);
353 } else if (set_data.file_specified) {
354 find_replace_1st ("$data_source", "from file " + set_data.file_path,
355 call_code);
356 } else {
357 find_replace_1st (" $data_source", "", call_code);
358 }
359*/ // Fill in the call itself:
360 if (print_data || hash_data) {
361 // Dump to variable; no data-check code needed:
362 check_code.assign (test_state->bplate->bplate_string[get_sst_check]);
363 } else {
364 // Check either against literal or variable, so need data-check code too:
365 check_code.assign (test_state->bplate->bplate_string[get_sst_check_all]);
366 }
367 /* Note: Can fill in the check code identically between the dump-to-variable
368 and check-data cases, because the boilerplate for the former is just an
369 abbreviated version of the latter. The find_replace_1st() calls for
370 the check-data stuff will just simply not have any effect. */
371 if (exp_data.data_var_specified) {
372 // Check against data in variable:
373 exp_var_name.assign (exp_data.data_var + "_data");
374 } else {
375 var_name_suffix = "_exp_data";
376 if (exp_data.n_exp_vars > 0) {
377 var_name_suffix =
378 var_name_suffix + "_" + to_string(exp_data.n_exp_vars);
379 }
380 exp_var_name.assign (asset_info.get_name() + var_name_suffix);
381 }
382 if (assign_data_var_specified) {
383 act_var_name.assign (assign_data_var + "_data");
384 act_data_length.assign (assign_data_var + "_length");
385 } else {
386 act_var_name.assign (asset_info.get_name() + "_act_data");
387 act_data_length.assign (asset_info.get_name() + "_act_length");
388 }
389
390 id_string = to_string((long) asset_info.id_n);
391 // Fill in the PSA command itself:
392 find_replace_1st ("$uid", id_string, call_code);
393 find_replace_all ("$length", to_string(set_data.get().length()), call_code);
394 find_replace_1st ("$offset", to_string(set_data.data_offset), call_code);
395 find_replace_1st ("$exp_data", exp_var_name, call_code);
396 find_replace_all ("$act_data", act_var_name, call_code);
397 find_replace_all ("$act_length", act_data_length, call_code);
398 // Perform most of the same substitutions in the check_code:
399// TODO: Make data checks contingent upon the PSA call itself passing?
400 find_replace_1st ("$offset", "0", check_code);
401 find_replace_1st ("$exp_data", exp_var_name, check_code);
402 find_replace_all ("$act_data", act_var_name, check_code);
403 find_replace_all ("$length", act_data_length, check_code);
404 if (print_data) {
405 check_code.append (test_state->bplate->bplate_string[test_log]);
406 find_replace_1st ("$message", act_var_name, check_code);
407 }
408 if (hash_data) {
409 hash_var_name.assign (asset_info.get_name() + "_act_hash");
410 // this is where to put the hash of the data
411 check_code.append (test_state->bplate->bplate_string[get_sst_hash]);
412 find_replace_all ("$act_data_var", act_var_name, check_code);
413 find_replace_all ("$hash_var", hash_var_name, check_code);
414 }
415 // Figure out what expected results:
416 calc_result_code(); // this only fills $expect check_code
417 // Fill in expected data, actual data, and length:
418}
419
420/**********************************************************************************
421 End of methods of class sst_get_call.
422**********************************************************************************/
423
424
425/**********************************************************************************
426 Methods of class sst_remove_call follow:
427**********************************************************************************/
428
429sst_remove_call::sst_remove_call (tf_fuzz_info *test_state, // (constructor)
430 long &call_ser_no,
431 asset_search how_asset_found)
432 : sst_call(test_state, call_ser_no, how_asset_found)
433{
434 // Copy the boilerplate text into local buffers:
435 prep_code.assign ("");
436 call_code.assign (test_state->bplate->bplate_string[remove_sst]);
437 check_code.assign (test_state->bplate->bplate_string[remove_sst_check]);
438 call_description = "SST-remove call";
439}
440sst_remove_call::~sst_remove_call (void)
441{
442 return; // just to have something to pin a breakpoint onto
443}
444
445bool sst_remove_call::copy_call_to_asset (void)
446{
447 vector<psa_asset*>::iterator found_asset;
448
449 found_asset = resolve_asset (dont_create_asset, psa_asset_usage::all);
450 if (asset_info.how_asset_found != asset_search::not_found) {
451 asset_info.the_asset = reinterpret_cast<sst_asset*>(*found_asset);
452 // Note: Vector is of base-class type, but the assets *are* sst_asset.
453 if (asset_info.how_asset_found == asset_search::found_active) {
454 // Delete asset; move it from active vector to deleted vector:
455 test_state->deleted_sst_asset.push_back (asset_info.the_asset);
456 test_state->active_sst_asset.erase (found_asset);
457 } /* if not active, we'll deem the call expected to fail. */
458 }
459 return true;
460}
461
462bool sst_remove_call::copy_asset_to_call (void)
463{
464 if (asset_info.the_asset != nullptr) {
465 set_data.string_specified = asset_info.the_asset->set_data.string_specified;
466 set_data.file_specified = asset_info.the_asset->set_data.file_specified;
467 set_data.flags_string = asset_info.the_asset->set_data.flags_string;
468 asset_info.id_n = asset_info.the_asset->asset_info.id_n;
469 asset_info.name_specified = asset_info.the_asset->asset_info.name_specified;
470 }
471 return true;
472}
473
474void sst_remove_call::fill_in_prep_code (void)
475{
476 // No prep-code.
477 return; // just to have something to pin a breakpoint onto
478}
479
480void sst_remove_call::fill_in_command (void)
481{
482 // Fill in the call:
483 string id_string = to_string((long) asset_info.id_n);
484 find_replace_1st ("$uid", id_string, call_code);
485 // Fill in expected results:
486 calc_result_code(); // this only fills $expect check_code
487}
488
489/**********************************************************************************
490 End of methods of class sst_remove_call.
491**********************************************************************************/
492