hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
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
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2020, ARM Limited. All rights reserved.
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */
 
#include <errno.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
 
#include "xtest_test.h"
#include "xtest_helpers.h"
#include "xtest_uuid_helpers.h"
#include <signed_hdr.h>
#include <util.h>
 
#include <pta_invoke_tests.h>
#include <ta_crypt.h>
#include <ta_os_test.h>
#include <ta_create_fail_test.h>
#include <ta_rpc_test.h>
#include <ta_sims_test.h>
#include <ta_miss_test.h>
#include <ta_sims_keepalive_test.h>
#include <ta_concurrent.h>
#include <ta_tpm_log_test.h>
#include <ta_supp_plugin.h>
#include <sdp_basic.h>
#include <pta_secstor_ta_mgmt.h>
 
#include <test_supp_plugin.h>
 
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
 
struct xtest_crypto_session {
   ADBG_Case_t *c;
   TEEC_Session *session;
   uint32_t cmd_id_sha256;
   uint32_t cmd_id_aes256ecb_encrypt;
   uint32_t cmd_id_aes256ecb_decrypt;
};
 
static void xtest_crypto_test(struct xtest_crypto_session *cs)
{
   uint32_t ret_orig = 0;
   uint8_t crypt_out[16] = { };
   uint8_t crypt_in[16] = { 22, 17 };
 
   crypt_in[15] = 60;
 
   Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
 
       op.params[0].tmpref.buffer = crypt_in;
       op.params[0].tmpref.size = sizeof(crypt_in);
       op.params[1].tmpref.buffer = crypt_out;
       op.params[1].tmpref.size = sizeof(crypt_out);
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                        TEEC_MEMREF_TEMP_OUTPUT,
                        TEEC_NONE, TEEC_NONE);
 
       (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
                          TEEC_InvokeCommand(cs->session,
                       cs->
                       cmd_id_aes256ecb_encrypt,
                       &op,
                       &ret_orig));
   }
   Do_ADBG_EndSubCase(cs->c, "AES encrypt");
 
   Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
       uint8_t out[16] = { };
 
       op.params[0].tmpref.buffer = crypt_out;
       op.params[0].tmpref.size = sizeof(crypt_out);
       op.params[1].tmpref.buffer = out;
       op.params[1].tmpref.size = sizeof(out);
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                        TEEC_MEMREF_TEMP_OUTPUT,
                        TEEC_NONE, TEEC_NONE);
 
       (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
                          TEEC_InvokeCommand(cs->session,
                       cs->
                       cmd_id_aes256ecb_decrypt,
                       &op,
                       &ret_orig));
 
       if (!ADBG_EXPECT(cs->c, 0,
                memcmp(crypt_in, out, sizeof(crypt_in)))) {
           Do_ADBG_Log("crypt_in:");
           Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
           Do_ADBG_Log("out:");
           Do_ADBG_HexLog(out, sizeof(out), 16);
       }
   }
   Do_ADBG_EndSubCase(cs->c, "AES decrypt");
 
   Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
       static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
       static const uint8_t sha256_out[] = {
           0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
           0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
           0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
           0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
       };
       uint8_t out[32] = { };
 
       op.params[0].tmpref.buffer = (void *)sha256_in;
       op.params[0].tmpref.size = sizeof(sha256_in);
       op.params[1].tmpref.buffer = out;
       op.params[1].tmpref.size = sizeof(out);
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                        TEEC_MEMREF_TEMP_OUTPUT,
                        TEEC_NONE, TEEC_NONE);
 
       (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
                          TEEC_InvokeCommand(cs->session,
                                 cs->
                                 cmd_id_sha256,
                                 &op,
                                 &ret_orig));
 
       if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
                         sizeof(sha256_out)))) {
           Do_ADBG_Log("sha256_out:");
           Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
           Do_ADBG_Log("out:");
           Do_ADBG_HexLog(out, sizeof(out), 16);
       }
   }
   Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
 
   Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
       static const uint8_t in[] = {
           0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
           0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
           0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
           0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
       };
       static const uint8_t exp_out[] = {
           0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
           0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
           0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
           0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
       };
       uint8_t out[sizeof(exp_out)] = { };
 
       op.params[0].tmpref.buffer = (void *)in;
       op.params[0].tmpref.size = sizeof(in);
       op.params[1].tmpref.buffer = out;
       op.params[1].tmpref.size = sizeof(out);
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                        TEEC_MEMREF_TEMP_OUTPUT,
                        TEEC_NONE, TEEC_NONE);
 
       (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
                   TEEC_InvokeCommand(cs->session,
                   cs->
                   cmd_id_aes256ecb_encrypt,
                   &op,
                   &ret_orig));
 
       if (!ADBG_EXPECT(cs->c, 0,
                memcmp(exp_out, out, sizeof(exp_out)))) {
           Do_ADBG_Log("exp_out:");
           Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
           Do_ADBG_Log("out:");
           Do_ADBG_HexLog(out, sizeof(out), 16);
       }
   }
   Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
 
   Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
       static const uint8_t in[] = {
           0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
           0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
           0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
           0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
       };
       static const uint8_t exp_out[] = {
           0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
           0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
           0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
           0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
       };
       uint8_t out[sizeof(exp_out)] = { };
 
       op.params[0].tmpref.buffer = (void *)in;
       op.params[0].tmpref.size = sizeof(in);
       op.params[1].tmpref.buffer = out;
       op.params[1].tmpref.size = sizeof(out);
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                        TEEC_MEMREF_TEMP_OUTPUT,
                        TEEC_NONE, TEEC_NONE);
 
       (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
                      TEEC_InvokeCommand(cs->session,
                   cs->
                   cmd_id_aes256ecb_decrypt,
                   &op,
                   &ret_orig));
 
       if (!ADBG_EXPECT(cs->c, 0,
                memcmp(exp_out, out, sizeof(exp_out)))) {
           Do_ADBG_Log("exp_out:");
           Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
           Do_ADBG_Log("out:");
           Do_ADBG_HexLog(out, sizeof(out), 16);
       }
   }
   Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
}
 
static void xtest_tee_test_1001(ADBG_Case_t *c)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
 
   /* Pseudo TA is optional: warn and nicely exit if not found */
   res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
                     &ret_orig);
   if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
       Do_ADBG_Log(" - 1001 -   skip test, pseudo TA not found");
       return;
   }
   ADBG_EXPECT_TEEC_SUCCESS(c, res);
 
   (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
       &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
 
static void xtest_tee_test_1002(ADBG_Case_t *c)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Session session = { };
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
   uint8_t buf[16 * 1024] = { };
   uint8_t exp_sum = 0;
   size_t n = 0;
 
   /* Pseudo TA is optional: warn and nicely exit if not found */
   res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
                     &ret_orig);
   if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
       Do_ADBG_Log(" - 1002 -   skip test, pseudo TA not found");
       return;
   }
   ADBG_EXPECT_TEEC_SUCCESS(c, res);
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
                    TEEC_NONE, TEEC_NONE);
   op.params[0].tmpref.size = sizeof(buf);
   op.params[0].tmpref.buffer = buf;
 
   for (n = 0; n < sizeof(buf); n++)
       buf[n] = n + 1;
   for (n = 0; n < sizeof(buf); n++)
       exp_sum += buf[n];
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
       &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
       goto out;
 
   ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
out:
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
 
struct test_1003_arg {
   uint32_t test_type;
   size_t repeat;
   size_t max_before_lockers;
   size_t max_during_lockers;
   size_t before_lockers;
   size_t during_lockers;
   TEEC_Result res;
   uint32_t error_orig;
};
 
static void *test_1003_thread(void *arg)
{
   struct test_1003_arg *a = arg;
   TEEC_Session session = { };
   size_t rounds = 64 * 1024;
   size_t n = 0;
 
   a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
                    NULL, &a->error_orig);
   if (a->res != TEEC_SUCCESS)
       return NULL;
 
   for (n = 0; n < a->repeat; n++) {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
 
       op.params[0].value.a = a->test_type;
       op.params[0].value.b = rounds;
 
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
                        TEEC_VALUE_OUTPUT,
                        TEEC_NONE, TEEC_NONE);
       a->res = TEEC_InvokeCommand(&session,
                       PTA_INVOKE_TESTS_CMD_MUTEX,
                       &op, &a->error_orig);
       if (a->test_type == PTA_MUTEX_TEST_WRITER &&
           op.params[1].value.b != 1) {
           Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
           a->res = TEEC_ERROR_BAD_STATE;
           a->error_orig = 42;
           break;
       }
 
       if (a->test_type == PTA_MUTEX_TEST_READER) {
           if (op.params[1].value.a > a->max_before_lockers)
               a->max_before_lockers = op.params[1].value.a;
 
           if (op.params[1].value.b > a->max_during_lockers)
               a->max_during_lockers = op.params[1].value.b;
 
           a->before_lockers += op.params[1].value.a;
           a->during_lockers += op.params[1].value.b;
       }
   }
   TEEC_CloseSession(&session);
 
   return NULL;
}
 
#define TEST_1003_THREAD_COUNT        (3 * 2)
 
static void xtest_tee_test_1003(ADBG_Case_t *c)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
   size_t repeat = 20;
   struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
   size_t max_read_concurrency = 0;
   size_t max_read_waiters = 0;
   size_t num_concurrent_read_lockers = 0;
   size_t num_concurrent_read_waiters = 0;
   size_t n = 0;
   size_t nt = TEST_1003_THREAD_COUNT;
   double mean_read_concurrency = 0;
   double mean_read_waiters = 0;
   size_t num_writers = 0;
   size_t num_readers = 0;
   pthread_t thr[TEST_1003_THREAD_COUNT] = { };
 
   /* Pseudo TA is optional: warn and nicely exit if not found */
   res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
                     &ret_orig);
   if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
       Do_ADBG_Log(" - 1003 -   skip test, pseudo TA not found");
       return;
   }
   ADBG_EXPECT_TEEC_SUCCESS(c, res);
   TEEC_CloseSession(&session);
 
   for (n = 0; n < nt; n++) {
       if (n % 3) {
           arg[n].test_type = PTA_MUTEX_TEST_READER;
           num_readers++;
       } else {
           arg[n].test_type = PTA_MUTEX_TEST_WRITER;
           num_writers++;
       }
       arg[n].repeat = repeat;
       if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
                       test_1003_thread, arg + n)))
           nt = n; /* break loop and start cleanup */
   }
 
   for (n = 0; n < nt; n++) {
       ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
       if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
           Do_ADBG_Log("error origin %" PRIu32,
                   arg[n].error_orig);
       if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
           if (arg[n].max_during_lockers > max_read_concurrency)
               max_read_concurrency =
                   arg[n].max_during_lockers;
 
           if (arg[n].max_before_lockers > max_read_waiters)
               max_read_waiters = arg[n].max_before_lockers;
 
           num_concurrent_read_lockers += arg[n].during_lockers;
           num_concurrent_read_waiters += arg[n].before_lockers;
       }
   }
 
   mean_read_concurrency = (double)num_concurrent_read_lockers /
               (double)(repeat * num_readers);
   mean_read_waiters = (double)num_concurrent_read_waiters /
               (double)(repeat * num_readers);
 
   Do_ADBG_Log("    Number of parallel threads: %d (%zu writers and %zu readers)",
           TEST_1003_THREAD_COUNT, num_writers, num_readers);
   Do_ADBG_Log("    Max read concurrency: %zu", max_read_concurrency);
   Do_ADBG_Log("    Max read waiters: %zu", max_read_waiters);
   Do_ADBG_Log("    Mean read concurrency: %g", mean_read_concurrency);
   Do_ADBG_Log("    Mean read waiting: %g", mean_read_waiters);
}
ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
        "Core internal read/write mutex");
 
static void xtest_tee_test_1004(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
   struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
                      TA_CRYPT_CMD_AES256ECB_ENC,
                      TA_CRYPT_CMD_AES256ECB_DEC };
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
                         &session, &crypt_user_ta_uuid,
                         NULL, &ret_orig)))
       return;
 
   /* Run the "complete crypto test suite" */
   xtest_crypto_test(&cs);
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
 
static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
{
   TEEC_Session session = { };
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
                               &ret_orig)))
       return;
 
   op.params[0].value.a = n;
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
                    TEEC_NONE);
 
   (void)ADBG_EXPECT_TEEC_RESULT(c,
       TEEC_ERROR_TARGET_DEAD,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
                  &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_RESULT(c,
           TEEC_ERROR_TARGET_DEAD,
           TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
                   &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
 
   TEEC_CloseSession(&session);
}
 
static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
                                  size_t size)
{
   TEEC_Session session = { };
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
   TEEC_SharedMemory shm = { };
 
   shm.size = size;
   shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
       return;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
                               &ret_orig)))
       goto rel_shm;
 
   op.params[0].value.a = (uint32_t)n;
   op.params[1].memref.parent = &shm;
   op.params[1].memref.size = size;
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
                    TEEC_NONE, TEEC_NONE);
 
   (void)ADBG_EXPECT_TEEC_RESULT(c,
       TEEC_ERROR_TARGET_DEAD,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
                  &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_RESULT(c,
           TEEC_ERROR_TARGET_DEAD,
           TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
                   &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
 
   TEEC_CloseSession(&session);
rel_shm:
   TEEC_ReleaseSharedMemory(&shm);
}
 
static void xtest_tee_test_1005(ADBG_Case_t *c)
{
   uint32_t ret_orig = 0;
#define MAX_SESSIONS    3
   TEEC_Session sessions[MAX_SESSIONS];
   int i = 0;
 
   for (i = 0; i < MAX_SESSIONS; i++) {
       if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&sessions[i],
                       &concurrent_ta_uuid,
                       NULL, &ret_orig)))
           break;
   }
 
   for (; --i >= 0; )
       TEEC_CloseSession(&sessions[i]);
}
ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
 
static void xtest_tee_test_1006(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint8_t buf[32] = { };
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
                   &ret_orig)))
       return;
 
   op.params[0].tmpref.buffer = buf;
   op.params[0].tmpref.size = sizeof(buf);
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
                    TEEC_NONE, TEEC_NONE);
 
   (void)ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
                  &ret_orig));
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
       "Test Basic OS features");
 
static void xtest_tee_test_1007(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
                               &ret_orig)))
       return;
 
   (void)ADBG_EXPECT_TEEC_RESULT(c,
       TEEC_ERROR_TARGET_DEAD,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
                  &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
 
   (void)ADBG_EXPECT_TEEC_RESULT(c,
       TEEC_ERROR_TARGET_DEAD,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
                  &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
 
#ifndef TA_DIR
# ifdef __ANDROID__
#define TA_DIR "/vendor/lib/optee_armtz"
# else
#define TA_DIR "/lib/optee_armtz"
# endif
#endif
 
static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
{
   char buf[PATH_MAX] = { };
 
   snprintf(buf, sizeof(buf),
       "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
       TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
       uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
       uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
       uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
       uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
 
   return fopen(buf, mode);
}
 
static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
{
   TEEC_Session session = { };
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
   TEEC_Result res = TEEC_ERROR_GENERIC;
   uint32_t ret_orig = 0;
   FILE *f = NULL;
   bool r = false;
   uint8_t *buf = NULL;
   size_t sz = 0;
   size_t fread_res = 0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
       goto out;
 
   f = open_ta_file(&create_fail_test_ta_uuid, "rb");
   if (!ADBG_EXPECT_NOT_NULL(c, f))
       goto out;
   if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
       goto out;
   sz = ftell(f);
   rewind(f);
 
   buf = malloc(sz);
   if (!ADBG_EXPECT_NOT_NULL(c, buf))
       goto out;
 
   fread_res = fread(buf, 1, sz, f);
   if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
       goto out;
 
   fclose(f);
   f = NULL;
 
   buf[MIN(offs, sz)] ^= mask;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
                    TEEC_NONE, TEEC_NONE);
   op.params[0].tmpref.buffer = buf;
   op.params[0].tmpref.size = sz;
 
   res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
                &ret_orig);
   r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
out:
   free(buf);
   if (f)
       fclose(f);
   TEEC_CloseSession(&session);
   return r;
}
 
static void test_1008_corrupt_ta(ADBG_Case_t *c)
{
   TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
 
   res = xtest_teec_open_session(&session, &uuid, NULL, &ret_orig);
   if (res) {
       if (ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
                       res))
           Do_ADBG_Log("PTA Secure Storage TA Management not found: skip test");
       return;
   }
   TEEC_CloseSession(&session);
 
   ADBG_EXPECT_TRUE(c,
       load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
   ADBG_EXPECT_TRUE(c,
       load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
   ADBG_EXPECT_TRUE(c,
       load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
   ADBG_EXPECT_TRUE(c,
       load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
   ADBG_EXPECT_TRUE(c,
       load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
   ADBG_EXPECT_TRUE(c,
       load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
   ADBG_EXPECT_TRUE(c,
       load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
   ADBG_EXPECT_TRUE(c,
       load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
   ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
   ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
}
 
static void xtest_tee_test_1008(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   TEEC_Session session_crypt = { };
   uint32_t ret_orig = 0;
 
   Do_ADBG_BeginSubCase(c, "Invoke command");
   {
       if (ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&session, &os_test_ta_uuid,
                                   NULL, &ret_orig))) {
 
           (void)ADBG_EXPECT_TEEC_SUCCESS(c,
               TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
                                  NULL, &ret_orig));
           TEEC_CloseSession(&session);
       }
 
   }
   Do_ADBG_EndSubCase(c, "Invoke command");
 
   Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
 
       op.params[0].value.a = 2000;
       op.paramTypes = TEEC_PARAM_TYPES(
           TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
 
       if (ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&session,
                       &os_test_ta_uuid,
                       NULL,
                                   &ret_orig))) {
 
           (void)ADBG_EXPECT_TEEC_SUCCESS(c,
             TEEC_InvokeCommand(&session,
                                TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
                                &op, &ret_orig));
           TEEC_CloseSession(&session);
       }
   }
   Do_ADBG_EndSubCase(c, "Invoke command with timeout");
 
   Do_ADBG_BeginSubCase(c, "Create session fail");
   {
       size_t n = 0;
 
       for (n = 0; n < 100; n++) {
           Do_ADBG_Log("n = %zu", n);
           (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
               xtest_teec_open_session(&session_crypt,
                   &create_fail_test_ta_uuid,
                   NULL, &ret_orig));
           /* level > 0 may be used to detect/debug memory leaks */
           if (!level)
               break;
       }
   }
   Do_ADBG_EndSubCase(c, "Create session fail");
 
   Do_ADBG_BeginSubCase(c, "Load corrupt TA");
   test_1008_corrupt_ta(c);
   Do_ADBG_EndSubCase(c, "Load corrupt TA");
}
ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
       "TEE internal client API");
 
static void *cancellation_thread(void *arg)
{
   /*
    * Sleep 0.5 seconds before cancellation to make sure that the other
    * thread is in RPC_WAIT.
    */
   (void)usleep(500000);
   TEEC_RequestCancellation(arg);
   return NULL;
}
 
static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
                                        uint32_t timeout, bool cancel)
{
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
   pthread_t thr;
 
   memset(&thr, 0, sizeof(thr));
 
   Do_ADBG_BeginSubCase(c, "%s", subcase);
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
 
       if (ADBG_EXPECT_TEEC_SUCCESS(c,
                xtest_teec_open_session(&session, &os_test_ta_uuid,
                                        NULL, &ret_orig))) {
 
           (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
                      TEEC_ORIGIN_TRUSTED_APP,
                      ret_orig);
 
           op.params[0].value.a = timeout;
           op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
                                            TEEC_NONE,
                                            TEEC_NONE, TEEC_NONE);
           if (cancel) {
               (void)ADBG_EXPECT(c, 0,
                     pthread_create(&thr, NULL,
                                    cancellation_thread, &op));
 
               (void)ADBG_EXPECT_TEEC_RESULT(c,
                          TEEC_ERROR_CANCEL,
                              TEEC_InvokeCommand(&session,
                                            TA_OS_TEST_CMD_WAIT,
                                             &op,
                                             &ret_orig));
           } else
 
           (void)ADBG_EXPECT_TEEC_SUCCESS(c,
                      TEEC_InvokeCommand(&session,
                                         TA_OS_TEST_CMD_WAIT,
                                         &op,
                                         &ret_orig));
           if (cancel)
               (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
 
           TEEC_CloseSession(&session);
       }
   }
   Do_ADBG_EndSubCase(c, "%s", subcase);
}
 
static void xtest_tee_test_1009(ADBG_Case_t *c)
{
   xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
   xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
   xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
   xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
}
ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
 
static void xtest_tee_test_1010(ADBG_Case_t *c)
{
   unsigned int n = 0;
   unsigned int idx = 0;
   size_t memref_sz[] = { 1024, 65536 };
 
   for (n = 1; n <= 5; n++) {
       Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
       xtest_tee_test_invalid_mem_access(c, n);
       Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
   }
 
   for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
       for (n = 1; n <= 5; n++) {
           Do_ADBG_BeginSubCase(c,
               "Invalid memory access %u with %zu bytes memref",
               n, memref_sz[idx]);
           xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
           Do_ADBG_EndSubCase(c,
               "Invalid memory access %u with %zu bytes memref",
               n, memref_sz[idx]);
       }
   }
}
ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
       "Invalid memory access");
 
static void xtest_tee_test_1011(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
   struct xtest_crypto_session cs = {
       c, &session, TA_RPC_CMD_CRYPT_SHA256,
       TA_RPC_CMD_CRYPT_AES256ECB_ENC,
       TA_RPC_CMD_CRYPT_AES256ECB_DEC
   };
   struct xtest_crypto_session cs_privmem = {
       c, &session,
       TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
       TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
       TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
   };
   TEEC_UUID uuid = rpc_test_ta_uuid;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
       return;
 
   Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
   /*
    * Run the "complete crypto test suite" using TA-to-TA
    * communication
    */
   xtest_crypto_test(&cs);
   Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
 
   Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
   /*
    * Run the "complete crypto test suite" using TA-to-TA
    * communication via TA private memory.
    */
   xtest_crypto_test(&cs_privmem);
   Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
       "Test TA-to-TA features with User Crypt TA");
 
/*
 * Note that this test is failing when
 * - running twice in a raw
 * - and the user TA is statically linked
 * This is because the counter is not reseted when opening the first session
 * in case the TA is statically linked
 */
static void xtest_tee_test_1012(ADBG_Case_t *c)
{
   TEEC_Session session1 = { };
   TEEC_Session session2 = { };
   uint32_t ret_orig = 0;
   TEEC_UUID uuid = sims_test_ta_uuid;
 
   Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
       static const uint8_t in[] = {
           0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
           0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
           0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
           0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
       };
       uint8_t out[32] = { };
       int i = 0;
 
       if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&session1, &uuid, NULL,
                                   &ret_orig)))
           return;
 
       op.params[0].value.a = 0;
       op.params[1].tmpref.buffer = (void *)in;
       op.params[1].tmpref.size = sizeof(in);
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
                        TEEC_MEMREF_TEMP_INPUT,
                        TEEC_NONE, TEEC_NONE);
 
       (void)ADBG_EXPECT_TEEC_SUCCESS(c,
           TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
                      &ret_orig));
 
       for (i = 1; i < 3; i++) {
           if (!ADBG_EXPECT_TEEC_SUCCESS(c,
               xtest_teec_open_session(&session2, &uuid, NULL,
                                       &ret_orig)))
               continue;
 
           op.params[0].value.a = 0;
           op.params[1].tmpref.buffer = out;
           op.params[1].tmpref.size = sizeof(out);
           op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
                       TEEC_MEMREF_TEMP_OUTPUT,
                       TEEC_NONE, TEEC_NONE);
 
           (void)ADBG_EXPECT_TEEC_SUCCESS(c,
               TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
                          &op, &ret_orig));
 
           if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
                       sizeof(out))) {
               Do_ADBG_Log("in:");
               Do_ADBG_HexLog(in, sizeof(in), 16);
               Do_ADBG_Log("out:");
               Do_ADBG_HexLog(out, sizeof(out), 16);
           }
 
           op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
                            TEEC_NONE, TEEC_NONE,
                            TEEC_NONE);
 
           (void)ADBG_EXPECT_TEEC_SUCCESS(c,
               TEEC_InvokeCommand(&session1,
                          TA_SIMS_CMD_GET_COUNTER,
                          &op, &ret_orig));
 
           (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
 
           (void)ADBG_EXPECT_TEEC_SUCCESS(c,
               TEEC_InvokeCommand(&session2,
                          TA_SIMS_CMD_GET_COUNTER, &op,
                          &ret_orig));
 
           (void)ADBG_EXPECT(c, i, op.params[0].value.a);
           TEEC_CloseSession(&session2);
       }
 
       memset(out, 0, sizeof(out));
       op.params[0].value.a = 0;
       op.params[1].tmpref.buffer = out;
       op.params[1].tmpref.size = sizeof(out);
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
                        TEEC_MEMREF_TEMP_OUTPUT,
                        TEEC_NONE, TEEC_NONE);
 
       (void)ADBG_EXPECT_TEEC_SUCCESS(c,
           TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
                      &ret_orig));
 
       if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
           Do_ADBG_Log("in:");
           Do_ADBG_HexLog(in, sizeof(in), 16);
           Do_ADBG_Log("out:");
           Do_ADBG_HexLog(out, sizeof(out), 16);
       }
 
       TEEC_CloseSession(&session1);
   }
   Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
}
ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
       "Test Single Instance Multi Session features with SIMS TA");
 
struct test_1013_thread_arg {
   const TEEC_UUID *uuid;
   uint32_t cmd;
   uint32_t repeat;
   TEEC_SharedMemory *shm;
   uint32_t error_orig;
   TEEC_Result res;
   uint32_t max_concurrency;
   const uint8_t *in;
   size_t in_len;
   uint8_t *out;
   size_t out_len;
};
 
static void *test_1013_thread(void *arg)
{
   struct test_1013_thread_arg *a = arg;
   TEEC_Session session = { };
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint8_t p2 = TEEC_NONE;
   uint8_t p3 = TEEC_NONE;
 
   a->res = xtest_teec_open_session(&session, a->uuid, NULL,
                    &a->error_orig);
   if (a->res != TEEC_SUCCESS)
       return NULL;
 
   op.params[0].memref.parent = a->shm;
   op.params[0].memref.size = a->shm->size;
   op.params[0].memref.offset = 0;
   op.params[1].value.a = a->repeat;
   op.params[1].value.b = 0;
   op.params[2].tmpref.buffer = (void *)a->in;
   op.params[2].tmpref.size = a->in_len;
   op.params[3].tmpref.buffer = a->out;
   op.params[3].tmpref.size = a->out_len;
 
   if (a->in_len)
       p2 = TEEC_MEMREF_TEMP_INPUT;
   if (a->out_len)
       p3 = TEEC_MEMREF_TEMP_OUTPUT;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
                    TEEC_VALUE_INOUT, p2, p3);
 
   a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
   a->max_concurrency = op.params[1].value.b;
   a->out_len = op.params[3].tmpref.size;
   TEEC_CloseSession(&session);
   return NULL;
}
 
#define NUM_THREADS 3
 
static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
                      const TEEC_UUID *uuid)
{
   size_t nt = 0;
   size_t n = 0;
   size_t repeat = 1000;
   TEEC_SharedMemory shm = { };
   size_t max_concurrency = 0;
   struct test_1013_thread_arg arg[NUM_THREADS] = { };
   static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
   static const uint8_t sha256_out[] = {
       0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
       0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
       0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
       0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
   };
   uint8_t out[32] = { };
   pthread_t thr[NUM_THREADS] = { };
 
   Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
   *mean_concurrency = 0;
 
   shm.size = sizeof(struct ta_concurrent_shm);
   shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
       return;
 
   memset(shm.buffer, 0, shm.size);
   max_concurrency = 0;
   nt = NUM_THREADS;
 
   for (n = 0; n < nt; n++) {
       arg[n].uuid = uuid;
       arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
       arg[n].repeat = repeat * 10;
       arg[n].shm = &shm;
       if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
                       test_1013_thread, arg + n)))
           nt = n; /* break loop and start cleanup */
   }
 
   for (n = 0; n < nt; n++) {
       ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
       ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
       if (arg[n].max_concurrency > max_concurrency)
           max_concurrency = arg[n].max_concurrency;
   }
 
   /*
    * Concurrency can be limited by several factors, for instance in a
    * single CPU system it's dependent on the Preemption Model used by
    * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
    * best result there).
    */
   (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
   (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, NUM_THREADS);
   *mean_concurrency += max_concurrency;
   Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
 
   Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
   memset(shm.buffer, 0, shm.size);
   memset(arg, 0, sizeof(arg));
   max_concurrency = 0;
   nt = NUM_THREADS;
 
   for (n = 0; n < nt; n++) {
       arg[n].uuid = uuid;
       arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
       arg[n].repeat = repeat;
       arg[n].shm = &shm;
       arg[n].in = sha256_in;
       arg[n].in_len = sizeof(sha256_in);
       arg[n].out = out;
       arg[n].out_len = sizeof(out);
       if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
                       test_1013_thread, arg + n)))
           nt = n; /* break loop and start cleanup */
   }
 
   for (n = 0; n < nt; n++) {
       if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
           ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
           ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
                      arg[n].out, arg[n].out_len);
       if (arg[n].max_concurrency > max_concurrency)
           max_concurrency = arg[n].max_concurrency;
   }
   *mean_concurrency += max_concurrency;
   Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
 
   *mean_concurrency /= 2.0;
   TEEC_ReleaseSharedMemory(&shm);
}
 
static void xtest_tee_test_1013(ADBG_Case_t *c)
{
   int i = 0;
   double mean_concurrency = 0;
   double concurrency = 0;
   int nb_loops = 24;
 
   if (level == 0)
       nb_loops /= 2;
 
   Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
   mean_concurrency = 0;
   for (i = 0; i < nb_loops; i++) {
       xtest_tee_test_1013_single(c, &concurrency,
                      &concurrent_ta_uuid);
       mean_concurrency += concurrency;
   }
   mean_concurrency /= nb_loops;
 
   Do_ADBG_Log("    Number of parallel threads: %d", NUM_THREADS);
   Do_ADBG_Log("    Mean concurrency: %g", mean_concurrency);
   Do_ADBG_EndSubCase(c, "Using small concurrency TA");
 
#ifndef CFG_PAGED_USER_TA
   Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
   mean_concurrency = 0;
   for (i = 0; i < nb_loops; i++) {
       xtest_tee_test_1013_single(c, &concurrency,
                      &concurrent_large_ta_uuid);
       mean_concurrency += concurrency;
   }
   mean_concurrency /= nb_loops;
 
   Do_ADBG_Log("    Number of parallel threads: %d", NUM_THREADS);
   Do_ADBG_Log("    Mean concurrency: %g", mean_concurrency);
   Do_ADBG_EndSubCase(c, "Using large concurrency TA");
#endif
}
ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
       "Test concurrency with concurrent TA");
 
#ifdef CFG_SECURE_DATA_PATH
static void xtest_tee_test_1014(ADBG_Case_t *c)
{
   UNUSED(c);
 
   int size = 17000;
   int loop = 10;
   int ion_heap = DEFAULT_ION_HEAP_TYPE;
   int rnd_offset = 1;
   int test = 0;
   int ret = 0;
 
   test = TEST_NS_TO_TA;
   Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
   ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
   ADBG_EXPECT(c, 0, ret);
   Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
 
   test = TEST_TA_TO_TA;
   Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
   ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
   ADBG_EXPECT(c, 0, ret);
   Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
 
   test = TEST_TA_TO_PTA;
   Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
   ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
   ADBG_EXPECT(c, 0, ret);
   Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a test pTA (invoke_tests.pta)");
 
   test = TEST_NS_TO_PTA;
   Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
   ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
   ADBG_EXPECT(c, 1, ret);
   Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes a test pTA (invoke_tests.pta) (should fail)");
 
   Do_ADBG_BeginSubCase(c, "SDP: Invoke TA with out of bounds SDP memref");
   ret = sdp_out_of_bounds_memref_test(size, ion_heap, 0);
   ADBG_EXPECT(c, 0, ret);
   Do_ADBG_EndSubCase(c, NULL);
}
ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
       "Test secure data path against SDP TAs and pTAs");
#endif /*CFG_SECURE_DATA_PATH*/
 
static void xtest_tee_test_1015(ADBG_Case_t *c)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
 
   /* Pseudo TA is optional: warn and nicely exit if not found */
   res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
                     &ret_orig);
   if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
       Do_ADBG_Log(" - 1015 -   skip test, pseudo TA not found");
       return;
   }
   ADBG_EXPECT_TEEC_SUCCESS(c, res);
 
   ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
                  NULL, &ret_orig));
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
       "FS hash-tree corner cases");
 
static void xtest_tee_test_1016(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
                               &ret_orig)))
       return;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
                    TEEC_NONE);
 
   (void)ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
                  &ret_orig));
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
       "Test TA to TA transfers (in/out/inout memrefs on the stack)");
 
static void xtest_tee_test_1017(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
   TEEC_SharedMemory shm = { };
   size_t page_size = 4096;
 
   shm.size = 8 * page_size;
   shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
       return;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
                               &ret_orig)))
       goto out;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
                    TEEC_MEMREF_PARTIAL_INPUT,
                    TEEC_MEMREF_PARTIAL_OUTPUT,
                    TEEC_MEMREF_PARTIAL_OUTPUT);
 
   /*
    * The first two memrefs are supposed to be combined into in
    * region and the last two memrefs should have one region each
    * when the parameters are mapped for the TA.
    */
   op.params[0].memref.parent = &shm;
   op.params[0].memref.size = page_size;
   op.params[0].memref.offset = 0;
 
   op.params[1].memref.parent = &shm;
   op.params[1].memref.size = page_size;
   op.params[1].memref.offset = page_size;
 
   op.params[2].memref.parent = &shm;
   op.params[2].memref.size = page_size;
   op.params[2].memref.offset = 4 * page_size;
 
   op.params[3].memref.parent = &shm;
   op.params[3].memref.size = 2 * page_size;
   op.params[3].memref.offset = 6 * page_size;
 
   (void)ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
                  &ret_orig));
 
   TEEC_CloseSession(&session);
out:
   TEEC_ReleaseSharedMemory(&shm);
}
ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
       "Test coalescing memrefs");
 
static void invoke_1byte_out_of_bounds(ADBG_Case_t *c, TEEC_Session *session,
                      TEEC_SharedMemory *shm)
{
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   TEEC_Result ret = TEEC_ERROR_GENERIC;
   uint32_t ret_orig = 0;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
                    TEEC_NONE, TEEC_NONE, TEEC_NONE);
 
   op.params[0].memref.parent = shm;
   op.params[0].memref.size = shm->size / 2;
   op.params[0].memref.offset = shm->size - (shm->size / 2) + 1;
 
   ret = TEEC_InvokeCommand(session, TA_OS_TEST_CMD_PARAMS,
                &op, &ret_orig);
 
   ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
   if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
       ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
       ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
   }
}
 
static void xtest_tee_test_1018(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
   TEEC_SharedMemory shm = { };
   TEEC_Result ret = TEEC_ERROR_GENERIC;
   size_t page_size = 4096;
   /* Intentionally not 4kB aligned and odd */
   uint8_t buffer[6001] = { };
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
                     xtest_teec_open_session(&session,
                                 &os_test_ta_uuid,
                                 NULL,
                                 &ret_orig)))
       return;
 
   Do_ADBG_BeginSubCase(c, "Out of bounds > 4kB on allocated shm");
 
   shm.size = 8 * page_size;
   shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
                     TEEC_AllocateSharedMemory(&xtest_teec_ctx,
                               &shm)))
       goto out;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
                    TEEC_MEMREF_PARTIAL_INPUT,
                    TEEC_MEMREF_PARTIAL_OUTPUT,
                    TEEC_MEMREF_PARTIAL_OUTPUT);
 
   /*
    * The first two memrefs are supposed to be combined into in
    * region and the last two memrefs should have one region each
    * when the parameters are mapped for the TA.
    */
   op.params[0].memref.parent = &shm;
   op.params[0].memref.size = page_size;
   op.params[0].memref.offset = 0;
 
   op.params[1].memref.parent = &shm;
   op.params[1].memref.size = page_size;
   op.params[1].memref.offset = page_size;
 
   op.params[2].memref.parent = &shm;
   op.params[2].memref.size = page_size;
   op.params[2].memref.offset = 4 * page_size;
 
   op.params[3].memref.parent = &shm;
   op.params[3].memref.size = 3 * page_size;
   op.params[3].memref.offset = 6 * page_size;
 
   ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
                &ret_orig);
 
   ADBG_EXPECT_COMPARE_UNSIGNED(c, ret_orig, !=, TEEC_ORIGIN_TRUSTED_APP);
   if (ret != TEEC_ERROR_BAD_PARAMETERS && ret != TEEC_ERROR_GENERIC) {
       ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS, ret);
       ADBG_EXPECT(c, TEEC_ERROR_GENERIC, ret);
   }
 
   TEEC_ReleaseSharedMemory(&shm);
   Do_ADBG_EndSubCase(c, NULL);
 
   Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte on registered shm");
 
   memset(&shm, 0, sizeof(shm));
   shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
   shm.buffer = buffer;
   shm.size = sizeof(buffer);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
                     TEEC_RegisterSharedMemory(&xtest_teec_ctx,
                               &shm)))
       goto out;
 
   invoke_1byte_out_of_bounds(c, &session, &shm);
 
   TEEC_ReleaseSharedMemory(&shm);
   Do_ADBG_EndSubCase(c, NULL);
 
   Do_ADBG_BeginSubCase(c, "Out of bounds by 1 byte ref on allocated shm");
 
   memset(&shm, 0, sizeof(shm));
   shm.size = sizeof(buffer);
   shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
                     TEEC_AllocateSharedMemory(&xtest_teec_ctx,
                               &shm)))
       goto out;
 
   invoke_1byte_out_of_bounds(c, &session, &shm);
 
   TEEC_ReleaseSharedMemory(&shm);
   Do_ADBG_EndSubCase(c, NULL);
 
out:
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
       "Test memref out of bounds");
 
static void xtest_tee_test_1019(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
                               &ret_orig)))
       return;
 
   (void)ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
                  &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_RESULT(c,
       TEEC_ERROR_TARGET_DEAD,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
                  NULL, &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
       "Test dynamically linked TA");
 
static void xtest_tee_test_1020(ADBG_Case_t *c)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
 
   /* Pseudo TA is optional: warn and nicely exit if not found */
   res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
                     &ret_orig);
   if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
       Do_ADBG_Log(" - 1020 -   skip test, pseudo TA not found");
       return;
   }
   ADBG_EXPECT_TEEC_SUCCESS(c, res);
 
   res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
                NULL, &ret_orig);
   if (res != TEEC_SUCCESS) {
       (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
                           ret_orig);
       if (res == TEEC_ERROR_NOT_SUPPORTED) {
           Do_ADBG_Log(" - 1020 -   skip test, feature not "
                   "implemented");
           goto out;
       }
       /* Error */
       (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
   }
out:
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
       "Test lockdep algorithm");
 
static TEEC_Result open_sec_session(TEEC_Session *session,
                   const TEEC_UUID *uuid)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   op.params[0].tmpref.buffer = (void *)uuid;
   op.params[0].tmpref.size = sizeof(*uuid);
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                    TEEC_NONE, TEEC_NONE, TEEC_NONE);
 
   res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
                &op, &ret_orig);
   if (res != TEEC_SUCCESS)
       return TEEC_ERROR_GENERIC;
 
   return res;
}
 
static TEEC_Result sims_get_counter(TEEC_Session *session,
                   uint32_t *counter)
{
   TEEC_Result res = TEEC_ERROR_GENERIC;
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
                    TEEC_NONE, TEEC_NONE, TEEC_NONE);
 
   res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
                &op, &ret_orig);
   if (res == TEEC_SUCCESS)
       *counter = op.params[0].value.a;
 
   return res;
}
 
static TEEC_Result trigger_panic(TEEC_Session *session,
                const TEEC_UUID *uuid)
{
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
 
   if (!uuid) {
       op.params[0].tmpref.buffer = NULL;
       op.params[0].tmpref.size = 0;
   } else {
       op.params[0].tmpref.buffer = (void *)uuid;
       op.params[0].tmpref.size = sizeof(*uuid);
   }
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                    TEEC_NONE, TEEC_NONE, TEEC_NONE);
 
   return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
                 &op, &ret_orig);
}
 
static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
               bool multi_instance)
{
   TEEC_Result exp_res = TEEC_ERROR_GENERIC;
   uint32_t counter = 0;
   uint32_t ret_orig = 0;
   uint32_t exp_counter = 0;
   TEEC_Session cs[3] = { };
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&cs[0], uuid, NULL,
                       &ret_orig)))
       return;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&cs[1], uuid, NULL,
                       &ret_orig)))
       goto bail0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
       goto bail1;
 
   if (!ADBG_EXPECT(c, 0, counter))
       goto bail1;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
       goto bail1;
 
   exp_counter = multi_instance ? 0 : 1;
   if (!ADBG_EXPECT(c, exp_counter, counter))
       goto bail1;
 
   if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
                    trigger_panic(&cs[1], NULL)))
       goto bail1;
 
   exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
   if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
                    sims_get_counter(&cs[0], &counter)))
       goto bail1;
 
   if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
                    sims_get_counter(&cs[1], &counter)))
       goto bail1;
 
   /* Attempt to open a session on panicked context */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&cs[1], uuid, NULL,
                       &ret_orig)))
       goto bail1;
 
   /* Sanity check of still valid TA context */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&cs[2], uuid, NULL,
                       &ret_orig)))
       goto bail1;
 
   /* Sanity check of still valid TA context */
   if (multi_instance) {
       if (!ADBG_EXPECT_TEEC_SUCCESS(c,
               sims_get_counter(&cs[0], &counter)))
           goto bail2;
 
       if (!ADBG_EXPECT(c, 0, counter))
           goto bail2;
   }
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
       goto bail2;
 
   exp_counter = multi_instance ? 0 : 1;
   if (!ADBG_EXPECT(c, exp_counter, counter))
       goto bail2;
 
bail2:
   TEEC_CloseSession(&cs[2]);
bail1:
   TEEC_CloseSession(&cs[1]);
bail0:
   TEEC_CloseSession(&cs[0]);
}
 
static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
               const TEEC_UUID *uuid2)
{
   uint32_t ret_orig = 0;
   uint32_t counter = 0;
   TEEC_Session cs[3] = { };
 
   /* Test pre-conditions */
   /* 2.1 - CA opens a session toward TA1 */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&cs[0], uuid1, NULL,
                       &ret_orig)))
       return;
 
   /* 2.2 - CA opens a session toward TA2 */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&cs[1], uuid2, NULL,
                       &ret_orig)))
       goto bail0;
 
   /* 2.3 - TA1 opens a session toward TA2 */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
       goto bail1;
 
   /* 2.4 - CA invokes TA2 which panics */
   if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
                    trigger_panic(&cs[1], NULL)))
       goto bail1;
 
   /* Expected results */
   /* 2.5 - Expect CA->TA1 session is still alive */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
       goto bail1;
 
   /* 2.6 - Expect CA->TA2 session is properly released */
   if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
                    sims_get_counter(&cs[1], &counter)))
       goto bail1;
 
bail1:
   TEEC_CloseSession(&cs[1]);
bail0:
   TEEC_CloseSession(&cs[0]);
 
   memset(cs, 0, sizeof(cs));
 
   /* Test pre-conditions */
   /* 2.1 - CA opens a session toward TA1 */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&cs[0], uuid1, NULL,
                       &ret_orig)))
       return;
 
   /* 2.2 - CA opens a session toward TA2 */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&cs[1], uuid2, NULL,
                       &ret_orig)))
       goto bail2;
 
   /* 2.3 - TA1 opens a session toward TA2 */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
       goto bail3;
 
   /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
       goto bail3;
 
   /* Expected results */
   /* 2.5 - Expect CA->TA1 session is still alive */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
       goto bail3;
 
   /* 2.6 - Expect CA->TA2 session is properly released */
   if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
                    sims_get_counter(&cs[1], &counter)))
       goto bail3;
 
bail3:
   TEEC_CloseSession(&cs[1]);
bail2:
   TEEC_CloseSession(&cs[0]);
}
 
static void xtest_tee_test_1021(ADBG_Case_t *c)
{
   Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
   test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
   Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
 
   Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
   test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
   Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
 
   Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
   test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
   Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
 
   Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
   test_panic_ta_to_ta(c, &sims_test_ta_uuid,
               &sims_keepalive_test_ta_uuid);
   Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
}
ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
        "Test panic context release");
 
static void xtest_tee_test_1022(ADBG_Case_t *c)
{
   TEEC_Session session = { 0 };
   uint32_t ret_orig = 0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&session, &os_test_ta_uuid,
               NULL, &ret_orig)))
       return;
 
   (void)ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
                  &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_RESULT(c,
       TEEC_ERROR_TARGET_DEAD,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL_PANIC,
                  NULL, &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1022, xtest_tee_test_1022,
       "Test dlopen()/dlsym()/dlclose() API");
 
/*
 * Testing the ELF initialization (.init_array)
 *
 * - The TA has a global variable which can also be accessed by the two shared
 *   libraries os_test_lib (linked with the TA) and os_test_lib_dl (loaded via
 *   dlopen())
 * - The TA and both libraries have initialization functions (declared with the
 *   "constructor" attribute) which perform the following:
 *     * The TA multiplies by 10 then adds 1
 *     * os_test_lib multiplies by 10 then adds 2
 *     * os_test_lib_dl multiplies by 10 then adds 3
 * By testing the variable value we make sure the initializations occurred in
 * the correct order.
 */
static void xtest_tee_test_1023(ADBG_Case_t *c)
{
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   TEEC_Session session = { 0 };
   uint32_t ret_orig = 0;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT, TEEC_NONE,
                    TEEC_NONE, TEEC_NONE);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&session, &os_test_ta_uuid,
               NULL, &ret_orig)))
       return;
 
   (void)ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
                  &ret_orig));
 
   /* Expected: initialization of os_test_lib, then TA */
   (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 21);
 
   (void)ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_DL, NULL,
                  &ret_orig));
 
   (void)ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_GET_GLOBAL_VAR, &op,
                  &ret_orig));
 
   /* Expected: initialization of os_test_lib_dl */
   (void)ADBG_EXPECT_COMPARE_SIGNED(c, op.params[0].value.a, ==, 213);
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1023, xtest_tee_test_1023,
       "Test ELF initialization (.init_array)");
 
#ifdef CFG_CORE_TPM_EVENT_LOG
static void xtest_tee_test_1024(ADBG_Case_t *c)
{
   TEEC_Session session = {};
   uint32_t ret_orig = 0;
 
   xtest_teec_open_session(&session, &tpm_log_test_ta_uuid,
               NULL, &ret_orig);
 
   Do_ADBG_BeginSubCase(c, "TPM test service invocation");
   ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
                 TA_TPM_TEST_GET_LOG,
                 NULL, &ret_orig));
   Do_ADBG_EndSubCase(c, "TPM test service invocation");
 
   Do_ADBG_BeginSubCase(c, "TPM test passing short buffer");
   ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(&session,
                 TA_TPM_TEST_SHORT_BUF,
                 NULL, &ret_orig));
   Do_ADBG_EndSubCase(c, "TPM test passing short buffer");
 
   TEEC_CloseSession(&session);
}
 
ADBG_CASE_DEFINE(regression, 1024, xtest_tee_test_1024,
        "Test PTA_SYSTEM_GET_TPM_EVENT_LOG Service");
#endif /* CFG_CORE_TPM_EVENT_LOG */
 
static void xtest_tee_test_1025(ADBG_Case_t *c)
{
   TEEC_Session session = {};
   TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
   uint32_t ret_orig = 0;
   uint8_t *empty_buf = NULL;
   TEEC_SharedMemory shm = { };
   TEEC_Result res = TEEC_ERROR_GENERIC;
 
   Do_ADBG_BeginSubCase(c, "Invalid NULL buffer memref registration");
 
   memset(&shm, 0, sizeof(shm));
   shm.flags = TEEC_MEM_INPUT;
   shm.buffer = NULL;
   shm.size = 0;
 
   ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
           TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
 
   memset(&shm, 0, sizeof(shm));
   shm.flags = TEEC_MEM_OUTPUT;
   shm.buffer = NULL;
   shm.size = 0;
 
   ADBG_EXPECT(c, TEEC_ERROR_BAD_PARAMETERS,
           TEEC_RegisterSharedMemory(&xtest_teec_ctx, &shm));
 
   Do_ADBG_EndSubCase(c, "Invalid NULL buffer memref registration");
 
   if (!xtest_teec_ctx.memref_null) {
       Do_ADBG_Log("Skip subcases: MEMREF_NULL capability not supported");
       return;
   }
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
                     xtest_teec_open_session(&session,
                                 &os_test_ta_uuid,
                                 NULL, &ret_orig)))
       return;
 
   empty_buf = malloc(1);
   if (!empty_buf) {
       (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_ERROR_OUT_OF_MEMORY);
       goto out_session;
   }
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                    TEEC_MEMREF_TEMP_INPUT,
                    TEEC_MEMREF_TEMP_OUTPUT,
                    TEEC_MEMREF_TEMP_OUTPUT);
 
   Do_ADBG_BeginSubCase(c,
                "Input/Output MEMREF Buffer NULL - Size 0 bytes");
 
   op.params[0].tmpref.buffer = empty_buf;
   op.params[0].tmpref.size = 0;
 
   op.params[1].tmpref.buffer = NULL;
   op.params[1].tmpref.size = 0;
 
   op.params[2].tmpref.buffer = empty_buf;
   op.params[2].tmpref.size = 0;
 
   op.params[3].tmpref.buffer = NULL;
   op.params[3].tmpref.size = 0;
 
   ADBG_EXPECT(c, TEE_SUCCESS,
           TEEC_InvokeCommand(&session,
                      TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
                      &ret_orig));
 
   Do_ADBG_EndSubCase(c, "Input/Output MEMREF Buffer NULL - Size 0 bytes");
 
   Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
 
   op.params[0].tmpref.buffer = empty_buf;
   op.params[0].tmpref.size = 1;
 
   op.params[1].tmpref.buffer = NULL;
   op.params[1].tmpref.size = 0;
 
   op.params[2].tmpref.buffer = empty_buf;
   op.params[2].tmpref.size = 0;
 
   op.params[3].tmpref.buffer = NULL;
   op.params[3].tmpref.size = 0;
 
   ADBG_EXPECT(c, TEE_ERROR_BAD_PARAMETERS,
           TEEC_InvokeCommand(&session,
                      TA_OS_TEST_CMD_NULL_MEMREF_PARAMS, &op,
                      &ret_orig));
 
   TEEC_CloseSession(&session);
 
   Do_ADBG_EndSubCase(c, "Input MEMREF Buffer NULL - Size non 0 bytes");
 
   Do_ADBG_BeginSubCase(c, "Input MEMREF Buffer NULL over PTA invocation");
 
   /* Pseudo TA is optional: warn and nicely exit if not found */
   res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
                     &ret_orig);
   if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
       Do_ADBG_Log(" - 1025 -   skip test, pseudo TA not found");
       goto out;
   }
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
       goto out;
 
   op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
                    TEEC_NONE, TEEC_NONE);
   op.params[0].tmpref.buffer = NULL;
   op.params[0].tmpref.size = 0;
 
   ADBG_EXPECT(c, TEE_SUCCESS,
           TEEC_InvokeCommand(&session,
                      PTA_INVOKE_TESTS_CMD_MEMREF_NULL,
                      &op, &ret_orig));
 
out_session:
   TEEC_CloseSession(&session);
out:
   Do_ADBG_EndSubCase(c, NULL);
   free(empty_buf);
}
ADBG_CASE_DEFINE(regression, 1025, xtest_tee_test_1025,
        "Test memref NULL and/or 0 bytes size");
 
#define TEE_UUID_NS_NAME_SIZE  128
 
/*
 * TEE Client UUID name space identifier (UUIDv4)
 *
 * Value here is random UUID that is allocated as name space identifier for
 * forming Client UUID's for TEE environment using UUIDv5 scheme.
 */
static const char *client_uuid_linux_ns = "58ac9ca0-2086-4683-a1b8-ec4bc08e01b6";
 
/* TEEC_LOGIN_PUBLIC's Client UUID is NIL UUID */
static TEEC_UUID client_uuid_public = { };
 
static void xtest_tee_test_1026(ADBG_Case_t *c)
{
   TEEC_Result result = TEEC_ERROR_GENERIC;
   uint32_t ret_orig = 0;
   TEEC_Session session = { };
   uint32_t login = UINT32_MAX;
   TEEC_UUID client_uuid = { };
 
   result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
                 TEEC_LOGIN_PUBLIC, NULL, NULL, &ret_orig);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       return;
 
   result = ta_os_test_cmd_client_identity(&session, &login,
                       &client_uuid);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       goto out;
 
   ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_PUBLIC);
 
   ADBG_EXPECT_EQUAL(c, &client_uuid_public, &client_uuid,
             sizeof(TEEC_UUID));
 
out:
   TEEC_CloseSession(&session);
}
 
ADBG_CASE_DEFINE(regression, 1026, xtest_tee_test_1026,
        "Session: public login");
 
/*
 * regression_1027
 * Depends on OpenSSL
 * Depends on kernel commit "tee: optee: Add support for session login client UUID generation"
 * Linaro tree: https://github.com/linaro-swg/linux/commit/ad19acdcdbc5
 * Upstream: <put sha-1 here when known>
 *
 * xtest skips the test when not built with OpenSSL.
 */
static void xtest_tee_test_1027(ADBG_Case_t *c)
{
#ifdef OPENSSL_FOUND
   TEEC_Result result = TEEC_ERROR_GENERIC;
   uint32_t ret_orig = 0;
   TEEC_Session session = { };
   uint32_t login = UINT32_MAX;
   TEEC_UUID client_uuid = { };
   TEEC_UUID expected_client_uuid = { };
   TEEC_UUID uuid_ns = { };
   char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
 
   result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       return;
 
   sprintf(uuid_name, "uid=%x", geteuid());
 
   result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
                  strlen(uuid_name));
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       return;
 
   result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
                 TEEC_LOGIN_USER, NULL, NULL, &ret_orig);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       return;
 
   result = ta_os_test_cmd_client_identity(&session, &login,
                       &client_uuid);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       goto out;
 
   ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_USER);
 
   ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
             sizeof(TEEC_UUID));
 
out:
   TEEC_CloseSession(&session);
#else /*!OPENSSL_FOUND*/
   UNUSED(c);
   UNUSED(client_uuid_linux_ns);
   /* xtest_uuid_v5() depends on OpenSSL */
   Do_ADBG_Log("OpenSSL not available, skipping test 1027");
#endif
}
 
ADBG_CASE_DEFINE(regression, 1027, xtest_tee_test_1027,
        "Session: user login for current user");
 
/*
 * regression_1028
 * Depends on OpenSSL and kernel commit "tee: optee: Add support for session login client UUID generation"
 * Linaro tree: https://github.com/linaro-swg/linux/commit/ad19acdcdbc5
 * Upstream: <put sha-1 here when known>
 *
 * xtest skips the test when not built with OpenSSL.
 */
static void xtest_tee_test_1028(ADBG_Case_t *c)
{
#ifdef OPENSSL_FOUND
   TEEC_Result result = TEEC_ERROR_GENERIC;
   uint32_t ret_orig = 0;
   TEEC_Session session = { };
   uint32_t login = UINT32_MAX;
   TEEC_UUID client_uuid = { };
   TEEC_UUID expected_client_uuid = { };
   TEEC_UUID uuid_ns = { };
   char uuid_name[TEE_UUID_NS_NAME_SIZE] = { };
   uint32_t group = 0;
 
   group = getegid();
 
   result = xtest_uuid_from_str(&uuid_ns, client_uuid_linux_ns);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       return;
 
   sprintf(uuid_name, "gid=%x", group);
 
   result = xtest_uuid_v5(&expected_client_uuid, &uuid_ns, uuid_name,
                  strlen(uuid_name));
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       return;
 
   result = TEEC_OpenSession(&xtest_teec_ctx, &session, &os_test_ta_uuid,
                 TEEC_LOGIN_GROUP, &group, NULL, &ret_orig);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       return;
 
   result = ta_os_test_cmd_client_identity(&session, &login,
                       &client_uuid);
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, result))
       goto out;
 
   ADBG_EXPECT_COMPARE_UNSIGNED(c, login, ==, TEEC_LOGIN_GROUP);
 
   ADBG_EXPECT_EQUAL(c, &expected_client_uuid, &client_uuid,
             sizeof(TEEC_UUID));
 
out:
   TEEC_CloseSession(&session);
#else /*!OPENSSL_FOUND*/
   UNUSED(c);
   /* xtest_uuid_v5() depends on OpenSSL */
   Do_ADBG_Log("OpenSSL not available, skipping test 1028");
#endif
}
 
ADBG_CASE_DEFINE(regression, 1028, xtest_tee_test_1028,
        "Session: group login for current user's effective group");
 
static void xtest_tee_test_1029(ADBG_Case_t *c)
{
   TEEC_Result res = TEEC_SUCCESS;
   TEEC_Session session = { 0 };
   uint32_t ret_orig = 0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&session, &os_test_ta_uuid,
                       NULL, &ret_orig)))
       return;
 
   Do_ADBG_BeginSubCase(c, "TLS variables (main program)");
   res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_MAIN, NULL,
                &ret_orig);
   if (res == TEEC_ERROR_NOT_SUPPORTED)
       Do_ADBG_Log(" - 1029 -   skip test, "
               "TA returned TEEC_ERROR_NOT_SUPPORTED");
   else
       ADBG_EXPECT_TEEC_SUCCESS(c, res);
   Do_ADBG_EndSubCase(c, "TLS variables (main program)");
 
   Do_ADBG_BeginSubCase(c, "TLS variables (shared library)");
   res = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TLS_TEST_SHLIB, NULL,
                &ret_orig);
   if (res == TEEC_ERROR_NOT_SUPPORTED)
       Do_ADBG_Log(" - 1029 -   skip test, "
               "TA returned TEEC_ERROR_NOT_SUPPORTED");
   else
       ADBG_EXPECT_TEEC_SUCCESS(c, res);
   Do_ADBG_EndSubCase(c, "TLS variables (shared library)");
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1029, xtest_tee_test_1029,
        "Test __thread attribute");
 
static void xtest_tee_test_1030(ADBG_Case_t *c)
{
   TEEC_Session session = { 0 };
   uint32_t ret_orig = 0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&session, &os_test_ta_uuid,
                       NULL, &ret_orig)))
       return;
 
   Do_ADBG_BeginSubCase(c, "Before dlopen()");
   ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR, NULL,
                  &ret_orig));
   Do_ADBG_EndSubCase(c, "Before dlopen()");
 
   Do_ADBG_BeginSubCase(c, "After dlopen()");
   ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_DL_PHDR_DL, NULL,
                  &ret_orig));
   Do_ADBG_EndSubCase(c, "After dlopen()");
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1030, xtest_tee_test_1030,
        "Test dl_iterate_phdr()");
 
#ifndef __clang__
static void xtest_tee_test_1031(ADBG_Case_t *c)
{
   TEEC_Result ret = TEE_SUCCESS;
   TEEC_Session session = { 0 };
   uint32_t ret_orig = 0;
 
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
           xtest_teec_open_session(&session, &os_test_ta_uuid,
                       NULL, &ret_orig)))
       return;
 
   Do_ADBG_BeginSubCase(c, "Global object constructor (main program)");
   ret = TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_MAIN, NULL,
                  &ret_orig);
   if (ret == TEEC_ERROR_NOT_SUPPORTED) {
       printf("TA not built with C++ support, skipping C++ tests\n");
       Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
       goto out;
 
   }
   ADBG_EXPECT_TEEC_SUCCESS(c, ret);
   Do_ADBG_EndSubCase(c, "Global object constructor (main program)");
 
   Do_ADBG_BeginSubCase(c, "Global object constructor (shared library)");
   ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB,
                  NULL, &ret_orig));
   Do_ADBG_EndSubCase(c, "Global object constructor (shared library)");
 
   Do_ADBG_BeginSubCase(c, "Global object constructor (dlopen()ed lib)");
   ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_CTOR_SHLIB_DL,
                  NULL, &ret_orig));
   Do_ADBG_EndSubCase(c, "Global object constructor (dlopen()ed lib)");
 
   Do_ADBG_BeginSubCase(c, "Exceptions (simple)");
   ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MAIN, NULL,
                  &ret_orig));
   Do_ADBG_EndSubCase(c, "Exceptions (simple)");
 
   Do_ADBG_BeginSubCase(c, "Exceptions (mixed C/C++ frames)");
   ADBG_EXPECT_TEEC_SUCCESS(c,
       TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CXX_EXC_MIXED, NULL,
                  &ret_orig));
   Do_ADBG_EndSubCase(c, "Exceptions (mixed C/C++ frames)");
out:
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1031, xtest_tee_test_1031,
        "Test C++ features");
#endif
 
static void xtest_tee_test_1032(ADBG_Case_t *c)
{
   TEEC_Result res = TEEC_SUCCESS;
   TEEC_Context ctx = { };
   TEEC_SharedMemory shm1 = {
       .buffer = xtest_tee_test_1032,
       .size = 32,
       .flags = TEEC_MEM_INPUT,
   };
   static const uint8_t dummy_data[32] = { 1, 2, 3, 4, };
   TEEC_SharedMemory shm2 = {
       .buffer = (void *)dummy_data,
       .size = sizeof(dummy_data),
       .flags = TEEC_MEM_INPUT,
   };
 
   res = TEEC_InitializeContext(xtest_tee_name, &ctx);
   if (!ADBG_EXPECT_TEEC_SUCCESS(c, res))
       return;
 
   res = TEEC_RegisterSharedMemory(&ctx, &shm1);
   if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
       TEEC_ReleaseSharedMemory(&shm1);
 
   res = TEEC_RegisterSharedMemory(&ctx, &shm2);
   if (ADBG_EXPECT_TEEC_SUCCESS(c, res))
       TEEC_ReleaseSharedMemory(&shm2);
 
   TEEC_FinalizeContext(&ctx);
}
ADBG_CASE_DEFINE(regression, 1032, xtest_tee_test_1032,
       "Register read-only shared memory");
 
static void xtest_tee_test_1033(ADBG_Case_t *c)
{
   TEEC_Session session = { };
   uint32_t ret_orig = 0;
 
   /* TA will ping the test plugin during open session operation */
   if (!ADBG_EXPECT_TEEC_SUCCESS(c,
       xtest_teec_open_session(&session, &supp_plugin_test_ta_uuid,
                   NULL, &ret_orig)))
       return;
 
   Do_ADBG_BeginSubCase(c, "Pass values to/from a plugin");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
 
       op.params[0].value.a = 20;
       op.params[0].value.b = 10;
       op.params[1].value.a = '+';
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INOUT,
                        TEEC_VALUE_INPUT, TEEC_NONE,
                        TEEC_NONE);
 
       ADBG_EXPECT_TEEC_SUCCESS(c,
           TEEC_InvokeCommand(&session,
                      TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
                      &ret_orig));
       ADBG_EXPECT(c, 30, op.params[0].value.a);
 
       /* reassign, because the values was changed during previous op */
       op.params[0].value.a = 20;
       op.params[0].value.b = 10;
       op.params[1].value.a = '-';
       ADBG_EXPECT_TEEC_SUCCESS(c,
           TEEC_InvokeCommand(&session,
                      TA_SUPP_PLUGIN_CMD_PASS_VALUES, &op,
                      &ret_orig));
       ADBG_EXPECT(c, 10, op.params[0].value.a);
   }
   Do_ADBG_EndSubCase(c, "Pass values to/from a plugin");
 
   Do_ADBG_BeginSubCase(c, "Pass array to a plugin");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
       uint8_t to_plugin[] = { 0, 1, 2, 3, 4, 5 };
 
       op.params[0].tmpref.buffer = to_plugin;
       op.params[0].tmpref.size = sizeof(to_plugin);
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
                        TEEC_VALUE_OUTPUT,
                        TEEC_NONE, TEEC_NONE);
 
       ADBG_EXPECT_TEEC_SUCCESS(c,
           TEEC_InvokeCommand(&session,
                      TA_SUPP_PLUGIN_CMD_WRITE_ARR,
                      &op, &ret_orig));
 
       /*
        * The test plugin must calculate a sum of the input elements
        * and store it to 'op.params[1].value.a'
        */
       ADBG_EXPECT(c, 15, op.params[1].value.a);
   }
   Do_ADBG_EndSubCase(c, "Pass array to a plugin");
 
   Do_ADBG_BeginSubCase(c, "Get array from a plugin");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
       char from_plugin[64] = { };
       char expected_arr[] = "Array from plugin";
       size_t expectes_size = sizeof(expected_arr);
 
       op.params[0].tmpref.buffer = from_plugin;
       op.params[0].tmpref.size = sizeof(from_plugin);
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_OUTPUT,
                        TEEC_VALUE_OUTPUT, TEEC_NONE,
                        TEEC_NONE);
       ADBG_EXPECT_TEEC_SUCCESS(c,
           TEEC_InvokeCommand(&session,
                      TA_SUPP_PLUGIN_CMD_GET_ARR, &op,
                      &ret_orig));
       ADBG_EXPECT(c, expectes_size, op.params[1].value.a);
       ADBG_EXPECT_EQUAL(c, expected_arr, from_plugin, expectes_size);
   }
   Do_ADBG_EndSubCase(c, "Get array from a plugin");
 
   Do_ADBG_BeginSubCase(c, "Not allow bad input to a plugin");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
 
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
                        TEEC_NONE, TEEC_NONE);
       ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
           TEEC_InvokeCommand(&session,
                      TA_SUPP_PLUGIN_CMD_BAD_UUID, &op,
                      &ret_orig));
       ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
           TEEC_InvokeCommand(&session,
                      TA_SUPP_PLUGIN_CMD_BAD_IN_DATA, &op,
                      &ret_orig));
       ADBG_EXPECT_TEEC_RESULT(c, TEE_ERROR_BAD_PARAMETERS,
           TEEC_InvokeCommand(&session,
                      TA_SUPP_PLUGIN_CMD_BAD_IN_LEN, &op,
                      &ret_orig));
   }
   Do_ADBG_EndSubCase(c, "Not allow bad input to a plugin");
 
   Do_ADBG_BeginSubCase(c, "Call an unknown plugin");
   {
       TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
 
       op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE,
                        TEEC_NONE, TEEC_NONE);    
       ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_ITEM_NOT_FOUND,
           TEEC_InvokeCommand(&session,
                      TA_SUPP_PLUGIN_CMD_UNKNOWN_UUID,
                      &op, &ret_orig));
   }
   Do_ADBG_EndSubCase(c, "Call an unknown plugin");
 
   TEEC_CloseSession(&session);
}
ADBG_CASE_DEFINE(regression, 1033, xtest_tee_test_1033,
        "Test the supplicant plugin framework");