blob: f87a4c96ee9c8e8d1d3e8cdf7b43a89ea09171d9 [file] [log] [blame]
Pascal Brandc639ac82015-07-02 08:53:34 +02001/*
2 * Copyright (c) 2014, STMicroelectronics International N.V.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Etienne Carrierea4653552017-01-11 10:04:24 +010014#include <limits.h>
15#include <pthread.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020016#include <stdio.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010017#include <stdlib.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020018#include <string.h>
David Brownb2865ab2016-08-02 11:44:41 -060019#include <sys/stat.h>
20#include <sys/types.h>
Etienne Carrierea4653552017-01-11 10:04:24 +010021#include <unistd.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020022
23#include "xtest_test.h"
24#include "xtest_helpers.h"
Jens Wiklander4441fe22015-10-23 16:53:02 +020025#include <signed_hdr.h>
Etienne Carriere92c34422018-02-09 13:11:40 +010026#include <util.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020027
Etienne Carriere726d8bc2017-03-21 15:45:59 +010028#include <pta_invoke_tests.h>
Pascal Brandc639ac82015-07-02 08:53:34 +020029#include <ta_crypt.h>
30#include <ta_os_test.h>
31#include <ta_create_fail_test.h>
32#include <ta_rpc_test.h>
33#include <ta_sims_test.h>
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +030034#include <ta_miss_test.h>
35#include <ta_sims_keepalive_test.h>
Jens Wiklanderac27ec12015-07-15 15:23:14 +020036#include <ta_concurrent.h>
Etienne Carriere50abf9a2017-03-24 11:33:50 +010037#include <sdp_basic.h>
Jens Wiklanderec545fb2017-11-24 16:58:07 +010038#ifdef CFG_SECSTOR_TA_MGMT_PTA
39#include <pta_secstor_ta_mgmt.h>
40#endif
41
42#ifndef MIN
43#define MIN(a, b) ((a) < (b) ? (a) : (b))
44#endif
Pascal Brandc639ac82015-07-02 08:53:34 +020045
Pascal Brandc639ac82015-07-02 08:53:34 +020046struct xtest_crypto_session {
47 ADBG_Case_t *c;
48 TEEC_Session *session;
49 uint32_t cmd_id_sha256;
50 uint32_t cmd_id_aes256ecb_encrypt;
51 uint32_t cmd_id_aes256ecb_decrypt;
52};
53
54static void xtest_crypto_test(struct xtest_crypto_session *cs)
55{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010056 uint32_t ret_orig = 0;
57 uint8_t crypt_out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020058 uint8_t crypt_in[16] = { 22, 17 };
59
60 crypt_in[15] = 60;
61
62 Do_ADBG_BeginSubCase(cs->c, "AES encrypt");
63 {
64 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
65
66 op.params[0].tmpref.buffer = crypt_in;
67 op.params[0].tmpref.size = sizeof(crypt_in);
68 op.params[1].tmpref.buffer = crypt_out;
69 op.params[1].tmpref.size = sizeof(crypt_out);
70 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
71 TEEC_MEMREF_TEMP_OUTPUT,
72 TEEC_NONE, TEEC_NONE);
73
74 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
75 TEEC_InvokeCommand(cs->session,
76 cs->
77 cmd_id_aes256ecb_encrypt,
78 &op,
79 &ret_orig));
80 }
81 Do_ADBG_EndSubCase(cs->c, "AES encrypt");
82
83 Do_ADBG_BeginSubCase(cs->c, "AES decrypt");
84 {
85 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +010086 uint8_t out[16] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +020087
88 op.params[0].tmpref.buffer = crypt_out;
89 op.params[0].tmpref.size = sizeof(crypt_out);
90 op.params[1].tmpref.buffer = out;
91 op.params[1].tmpref.size = sizeof(out);
92 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
93 TEEC_MEMREF_TEMP_OUTPUT,
94 TEEC_NONE, TEEC_NONE);
95
96 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
97 TEEC_InvokeCommand(cs->session,
98 cs->
99 cmd_id_aes256ecb_decrypt,
100 &op,
101 &ret_orig));
102
103 if (!ADBG_EXPECT(cs->c, 0,
104 memcmp(crypt_in, out, sizeof(crypt_in)))) {
105 Do_ADBG_Log("crypt_in:");
106 Do_ADBG_HexLog(crypt_in, sizeof(crypt_in), 16);
107 Do_ADBG_Log("out:");
108 Do_ADBG_HexLog(out, sizeof(out), 16);
109 }
110 }
111 Do_ADBG_EndSubCase(cs->c, "AES decrypt");
112
113 Do_ADBG_BeginSubCase(cs->c, "SHA-256 test, 3 bytes input");
114 {
115 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
116 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
117 static const uint8_t sha256_out[] = {
118 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
119 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
120 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
121 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
122 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100123 uint8_t out[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200124
125 op.params[0].tmpref.buffer = (void *)sha256_in;
126 op.params[0].tmpref.size = sizeof(sha256_in);
127 op.params[1].tmpref.buffer = out;
128 op.params[1].tmpref.size = sizeof(out);
129 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
130 TEEC_MEMREF_TEMP_OUTPUT,
131 TEEC_NONE, TEEC_NONE);
132
133 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
134 TEEC_InvokeCommand(cs->session,
135 cs->
136 cmd_id_sha256,
137 &op,
138 &ret_orig));
139
140 if (!ADBG_EXPECT(cs->c, 0, memcmp(sha256_out, out,
141 sizeof(sha256_out)))) {
142 Do_ADBG_Log("sha256_out:");
143 Do_ADBG_HexLog(sha256_out, sizeof(sha256_out), 16);
144 Do_ADBG_Log("out:");
145 Do_ADBG_HexLog(out, sizeof(out), 16);
146 }
147 }
148 Do_ADBG_EndSubCase(cs->c, "SHA-256 test, 3 bytes input");
149
Etienne Carrierea3198522017-10-26 09:48:55 +0200150 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200151 {
152 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
153 static const uint8_t in[] = {
154 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
155 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
156 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
157 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
158 };
159 static const uint8_t exp_out[] = {
160 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
161 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
162 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
163 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
164 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100165 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200166
167 op.params[0].tmpref.buffer = (void *)in;
168 op.params[0].tmpref.size = sizeof(in);
169 op.params[1].tmpref.buffer = out;
170 op.params[1].tmpref.size = sizeof(out);
171 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
172 TEEC_MEMREF_TEMP_OUTPUT,
173 TEEC_NONE, TEEC_NONE);
174
175 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
176 TEEC_InvokeCommand(cs->session,
177 cs->
178 cmd_id_aes256ecb_encrypt,
179 &op,
180 &ret_orig));
181
182 if (!ADBG_EXPECT(cs->c, 0,
183 memcmp(exp_out, out, sizeof(exp_out)))) {
184 Do_ADBG_Log("exp_out:");
185 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
186 Do_ADBG_Log("out:");
187 Do_ADBG_HexLog(out, sizeof(out), 16);
188 }
189 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200190 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB encrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200191
Etienne Carrierea3198522017-10-26 09:48:55 +0200192 Do_ADBG_BeginSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200193 {
194 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
195 static const uint8_t in[] = {
196 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
197 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
198 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
199 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
200 };
201 static const uint8_t exp_out[] = {
202 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
203 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
204 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
205 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
206 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100207 uint8_t out[sizeof(exp_out)] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200208
209 op.params[0].tmpref.buffer = (void *)in;
210 op.params[0].tmpref.size = sizeof(in);
211 op.params[1].tmpref.buffer = out;
212 op.params[1].tmpref.size = sizeof(out);
213 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
214 TEEC_MEMREF_TEMP_OUTPUT,
215 TEEC_NONE, TEEC_NONE);
216
217 (void)ADBG_EXPECT_TEEC_SUCCESS(cs->c,
218 TEEC_InvokeCommand(cs->session,
219 cs->
220 cmd_id_aes256ecb_decrypt,
221 &op,
222 &ret_orig));
223
224 if (!ADBG_EXPECT(cs->c, 0,
225 memcmp(exp_out, out, sizeof(exp_out)))) {
226 Do_ADBG_Log("exp_out:");
227 Do_ADBG_HexLog(exp_out, sizeof(exp_out), 16);
228 Do_ADBG_Log("out:");
229 Do_ADBG_HexLog(out, sizeof(out), 16);
230 }
231 }
Etienne Carrierea3198522017-10-26 09:48:55 +0200232 Do_ADBG_EndSubCase(cs->c, "AES-256 ECB decrypt (32B, fixed key)");
Pascal Brandc639ac82015-07-02 08:53:34 +0200233}
234
235static void xtest_tee_test_1001(ADBG_Case_t *c)
236{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100237 TEEC_Result res = TEEC_ERROR_GENERIC;
238 TEEC_Session session = { };
239 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200240
Etienne Carriere11093162017-10-26 09:49:04 +0200241 /* Pseudo TA is optional: warn and nicely exit if not found */
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100242 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
Jens Wiklandercf16e842016-02-10 09:07:09 +0100243 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200244 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
245 Do_ADBG_Log(" - 1001 - skip test, pseudo TA not found");
Jens Wiklandercf16e842016-02-10 09:07:09 +0100246 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200247 }
248 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Pascal Brandc639ac82015-07-02 08:53:34 +0200249
Jens Wiklandercf16e842016-02-10 09:07:09 +0100250 (void)ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
Etienne Carriere726d8bc2017-03-21 15:45:59 +0100251 &session, PTA_INVOKE_TESTS_CMD_SELF_TESTS, NULL, &ret_orig));
Jens Wiklandercf16e842016-02-10 09:07:09 +0100252 TEEC_CloseSession(&session);
Pascal Brandc639ac82015-07-02 08:53:34 +0200253}
Jens Wiklander14f48872018-06-29 15:30:13 +0200254ADBG_CASE_DEFINE(regression, 1001, xtest_tee_test_1001, "Core self tests");
Pascal Brandc639ac82015-07-02 08:53:34 +0200255
Jens Wiklander1d70a112017-10-16 15:16:39 +0200256static void xtest_tee_test_1002(ADBG_Case_t *c)
257{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100258 TEEC_Result res = TEEC_ERROR_GENERIC;
259 TEEC_Session session = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200260 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100261 uint32_t ret_orig = 0;
262 uint8_t buf[16 * 1024] = { };
Jens Wiklander1d70a112017-10-16 15:16:39 +0200263 uint8_t exp_sum = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100264 size_t n = 0;
Jens Wiklander1d70a112017-10-16 15:16:39 +0200265
Etienne Carriere11093162017-10-26 09:49:04 +0200266 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander1d70a112017-10-16 15:16:39 +0200267 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
268 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +0200269 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
270 Do_ADBG_Log(" - 1002 - skip test, pseudo TA not found");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200271 return;
Etienne Carriere11093162017-10-26 09:49:04 +0200272 }
273 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander1d70a112017-10-16 15:16:39 +0200274
275 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INOUT, TEEC_NONE,
276 TEEC_NONE, TEEC_NONE);
277 op.params[0].tmpref.size = sizeof(buf);
278 op.params[0].tmpref.buffer = buf;
279
280 for (n = 0; n < sizeof(buf); n++)
281 buf[n] = n + 1;
282 for (n = 0; n < sizeof(buf); n++)
283 exp_sum += buf[n];
284
285 if (!ADBG_EXPECT_TEEC_SUCCESS(c, TEEC_InvokeCommand(
286 &session, PTA_INVOKE_TESTS_CMD_PARAMS, &op, &ret_orig)))
287 goto out;
288
289 ADBG_EXPECT_COMPARE_SIGNED(c, exp_sum, ==, buf[0]);
290out:
291 TEEC_CloseSession(&session);
292}
Jens Wiklander14f48872018-06-29 15:30:13 +0200293ADBG_CASE_DEFINE(regression, 1002, xtest_tee_test_1002, "PTA parameters");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200294
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100295struct test_1003_arg {
296 uint32_t test_type;
297 size_t repeat;
298 size_t max_before_lockers;
299 size_t max_during_lockers;
300 size_t before_lockers;
301 size_t during_lockers;
302 TEEC_Result res;
303 uint32_t error_orig;
304};
Jens Wiklander1d70a112017-10-16 15:16:39 +0200305
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100306static void *test_1003_thread(void *arg)
307{
308 struct test_1003_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100309 TEEC_Session session = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100310 size_t rounds = 64 * 1024;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100311 size_t n = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100312
313 a->res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid,
314 NULL, &a->error_orig);
315 if (a->res != TEEC_SUCCESS)
316 return NULL;
317
318 for (n = 0; n < a->repeat; n++) {
319 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
320
321 op.params[0].value.a = a->test_type;
322 op.params[0].value.b = rounds;
323
324 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
325 TEEC_VALUE_OUTPUT,
326 TEEC_NONE, TEEC_NONE);
327 a->res = TEEC_InvokeCommand(&session,
328 PTA_INVOKE_TESTS_CMD_MUTEX,
329 &op, &a->error_orig);
330 if (a->test_type == PTA_MUTEX_TEST_WRITER &&
331 op.params[1].value.b != 1) {
332 Do_ADBG_Log("n %zu %" PRIu32, n, op.params[1].value.b);
333 a->res = TEEC_ERROR_BAD_STATE;
334 a->error_orig = 42;
335 break;
336 }
337
338 if (a->test_type == PTA_MUTEX_TEST_READER) {
339 if (op.params[1].value.a > a->max_before_lockers)
340 a->max_before_lockers = op.params[1].value.a;
341
342 if (op.params[1].value.b > a->max_during_lockers)
343 a->max_during_lockers = op.params[1].value.b;
344
345 a->before_lockers += op.params[1].value.a;
346 a->during_lockers += op.params[1].value.b;
347 }
348 }
349 TEEC_CloseSession(&session);
350
351 return NULL;
352}
353
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100354#define TEST_1003_THREAD_COUNT (3 * 2)
355
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100356static void xtest_tee_test_1003(ADBG_Case_t *c)
357{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100358 TEEC_Result res = TEEC_ERROR_GENERIC;
359 TEEC_Session session = { };
360 uint32_t ret_orig = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100361 size_t repeat = 20;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100362 struct test_1003_arg arg[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100363 size_t max_read_concurrency = 0;
364 size_t max_read_waiters = 0;
365 size_t num_concurrent_read_lockers = 0;
366 size_t num_concurrent_read_waiters = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100367 size_t n = 0;
368 size_t nt = TEST_1003_THREAD_COUNT;
369 double mean_read_concurrency = 0;
370 double mean_read_waiters = 0;
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100371 size_t num_writers = 0;
372 size_t num_readers = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100373 pthread_t thr[TEST_1003_THREAD_COUNT] = { };
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100374
375 /* Pseudo TA is optional: warn and nicely exit if not found */
376 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
377 &ret_orig);
378 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
379 Do_ADBG_Log(" - 1003 - skip test, pseudo TA not found");
380 return;
381 }
382 ADBG_EXPECT_TEEC_SUCCESS(c, res);
383 TEEC_CloseSession(&session);
384
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100385 for (n = 0; n < nt; n++) {
386 if (n % 3) {
387 arg[n].test_type = PTA_MUTEX_TEST_READER;
388 num_readers++;
389 } else {
390 arg[n].test_type = PTA_MUTEX_TEST_WRITER;
391 num_writers++;
392 }
393 arg[n].repeat = repeat;
394 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
395 test_1003_thread, arg + n)))
396 nt = n; /* break loop and start cleanup */
397 }
398
399 for (n = 0; n < nt; n++) {
400 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
401 if (!ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
402 Do_ADBG_Log("error origin %" PRIu32,
403 arg[n].error_orig);
404 if (arg[n].test_type == PTA_MUTEX_TEST_READER) {
405 if (arg[n].max_during_lockers > max_read_concurrency)
406 max_read_concurrency =
407 arg[n].max_during_lockers;
408
409 if (arg[n].max_before_lockers > max_read_waiters)
410 max_read_waiters = arg[n].max_before_lockers;
411
412 num_concurrent_read_lockers += arg[n].during_lockers;
413 num_concurrent_read_waiters += arg[n].before_lockers;
414 }
415 }
416
417 mean_read_concurrency = (double)num_concurrent_read_lockers /
418 (double)(repeat * num_readers);
419 mean_read_waiters = (double)num_concurrent_read_waiters /
420 (double)(repeat * num_readers);
421
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100422 Do_ADBG_Log(" Number of parallel threads: %d (%zu writers and %zu readers)",
423 TEST_1003_THREAD_COUNT, num_writers, num_readers);
Jens Wiklander0c86bc32017-11-13 19:52:03 +0100424 Do_ADBG_Log(" Max read concurrency: %zu", max_read_concurrency);
425 Do_ADBG_Log(" Max read waiters: %zu", max_read_waiters);
426 Do_ADBG_Log(" Mean read concurrency: %g", mean_read_concurrency);
427 Do_ADBG_Log(" Mean read waiting: %g", mean_read_waiters);
428}
Jens Wiklander14f48872018-06-29 15:30:13 +0200429ADBG_CASE_DEFINE(regression, 1003, xtest_tee_test_1003,
430 "Core internal read/write mutex");
Jens Wiklander1d70a112017-10-16 15:16:39 +0200431
Pascal Brandc639ac82015-07-02 08:53:34 +0200432static void xtest_tee_test_1004(ADBG_Case_t *c)
433{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100434 TEEC_Session session = { };
435 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200436 struct xtest_crypto_session cs = { c, &session, TA_CRYPT_CMD_SHA256,
437 TA_CRYPT_CMD_AES256ECB_ENC,
438 TA_CRYPT_CMD_AES256ECB_DEC };
439
440 if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session(
441 &session, &crypt_user_ta_uuid,
442 NULL, &ret_orig)))
443 return;
444
445 /* Run the "complete crypto test suite" */
446 xtest_crypto_test(&cs);
447
448 TEEC_CloseSession(&session);
449}
Jens Wiklander14f48872018-06-29 15:30:13 +0200450ADBG_CASE_DEFINE(regression, 1004, xtest_tee_test_1004, "Test User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200451
Etienne Carriere92c34422018-02-09 13:11:40 +0100452static void xtest_tee_test_invalid_mem_access(ADBG_Case_t *c, unsigned int n)
Pascal Brandc639ac82015-07-02 08:53:34 +0200453{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100454 TEEC_Session session = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200455 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100456 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200457
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300458 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200459 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300460 &ret_orig)))
461 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200462
463 op.params[0].value.a = n;
464 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE,
465 TEEC_NONE);
466
467 (void)ADBG_EXPECT_TEEC_RESULT(c,
468 TEEC_ERROR_TARGET_DEAD,
469 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
470 &ret_orig));
471
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300472 (void)ADBG_EXPECT_TEEC_RESULT(c,
473 TEEC_ERROR_TARGET_DEAD,
474 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
Pascal Brandc639ac82015-07-02 08:53:34 +0200475 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300476
Pascal Brandc639ac82015-07-02 08:53:34 +0200477 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
478
479 TEEC_CloseSession(&session);
480}
481
Etienne Carriere92c34422018-02-09 13:11:40 +0100482static void xtest_tee_test_invalid_mem_access2(ADBG_Case_t *c, unsigned int n,
483 size_t size)
484{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100485 TEEC_Session session = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100486 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100487 uint32_t ret_orig = 0;
488 TEEC_SharedMemory shm = { };
Etienne Carriere92c34422018-02-09 13:11:40 +0100489
Etienne Carriere92c34422018-02-09 13:11:40 +0100490 shm.size = size;
491 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
492 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
493 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
494 return;
495
496 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
497 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
498 &ret_orig)))
Jens Wiklander6987a822019-06-28 12:47:42 +0200499 goto rel_shm;
Etienne Carriere92c34422018-02-09 13:11:40 +0100500
501 op.params[0].value.a = (uint32_t)n;
502 op.params[1].memref.parent = &shm;
503 op.params[1].memref.size = size;
504 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT, TEEC_MEMREF_WHOLE,
505 TEEC_NONE, TEEC_NONE);
506
507 (void)ADBG_EXPECT_TEEC_RESULT(c,
508 TEEC_ERROR_TARGET_DEAD,
509 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
510 &ret_orig));
511
512 (void)ADBG_EXPECT_TEEC_RESULT(c,
513 TEEC_ERROR_TARGET_DEAD,
514 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BAD_MEM_ACCESS, &op,
515 &ret_orig));
516
517 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
518
519 TEEC_CloseSession(&session);
Jens Wiklander6987a822019-06-28 12:47:42 +0200520rel_shm:
521 TEEC_ReleaseSharedMemory(&shm);
Etienne Carriere92c34422018-02-09 13:11:40 +0100522}
523
Pascal Brandc639ac82015-07-02 08:53:34 +0200524static void xtest_tee_test_1005(ADBG_Case_t *c)
525{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100526 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200527#define MAX_SESSIONS 3
528 TEEC_Session sessions[MAX_SESSIONS];
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100529 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200530
531 for (i = 0; i < MAX_SESSIONS; i++) {
532 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Jens Wiklandereb6bce72016-09-23 11:37:33 +0200533 xtest_teec_open_session(&sessions[i],
534 &concurrent_ta_uuid,
Pascal Brandc639ac82015-07-02 08:53:34 +0200535 NULL, &ret_orig)))
536 break;
537 }
538
539 for (; --i >= 0; )
540 TEEC_CloseSession(&sessions[i]);
541}
Jens Wiklander14f48872018-06-29 15:30:13 +0200542ADBG_CASE_DEFINE(regression, 1005, xtest_tee_test_1005, "Many sessions");
Pascal Brandc639ac82015-07-02 08:53:34 +0200543
544static void xtest_tee_test_1006(ADBG_Case_t *c)
545{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100546 TEEC_Session session = { };
547 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200548 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100549 uint8_t buf[32] = { };
Pascal Brandc639ac82015-07-02 08:53:34 +0200550
551 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
552 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
553 &ret_orig)))
554 return;
555
556 op.params[0].tmpref.buffer = buf;
557 op.params[0].tmpref.size = sizeof(buf);
558 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
559 TEEC_NONE, TEEC_NONE);
560
561 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
562 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_BASIC, &op,
563 &ret_orig));
564
565 TEEC_CloseSession(&session);
566}
Jens Wiklander14f48872018-06-29 15:30:13 +0200567ADBG_CASE_DEFINE(regression, 1006, xtest_tee_test_1006,
568 "Test Basic OS features");
Pascal Brandc639ac82015-07-02 08:53:34 +0200569
570static void xtest_tee_test_1007(ADBG_Case_t *c)
571{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100572 TEEC_Session session = { };
573 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200574
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300575 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200576 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300577 &ret_orig)))
578 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200579
580 (void)ADBG_EXPECT_TEEC_RESULT(c,
581 TEEC_ERROR_TARGET_DEAD,
582 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PANIC, NULL,
583 &ret_orig));
584
585 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
586
587 (void)ADBG_EXPECT_TEEC_RESULT(c,
588 TEEC_ERROR_TARGET_DEAD,
589 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_INIT, NULL,
590 &ret_orig));
591
592 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
593
594 TEEC_CloseSession(&session);
595}
Jens Wiklander14f48872018-06-29 15:30:13 +0200596ADBG_CASE_DEFINE(regression, 1007, xtest_tee_test_1007, "Test Panic");
Pascal Brandc639ac82015-07-02 08:53:34 +0200597
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100598#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jerome Forissierf02a2212015-10-29 14:33:35 +0100599#ifndef TA_DIR
Victor Chong3d8798f2017-03-01 18:31:48 +0000600# ifdef __ANDROID__
Yongqin Liu286f1fc2018-06-21 22:09:15 +0800601#define TA_DIR "/vendor/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000602# else
Jerome Forissierf02a2212015-10-29 14:33:35 +0100603#define TA_DIR "/lib/optee_armtz"
Victor Chong3d8798f2017-03-01 18:31:48 +0000604# endif
Jerome Forissierf02a2212015-10-29 14:33:35 +0100605#endif
606
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100607static FILE *open_ta_file(const TEEC_UUID *uuid, const char *mode)
David Brownb2865ab2016-08-02 11:44:41 -0600608{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100609 char buf[PATH_MAX] = { };
David Brownb2865ab2016-08-02 11:44:41 -0600610
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100611 snprintf(buf, sizeof(buf),
Jens Wiklander6203b872016-12-08 19:18:29 +0100612 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100613 TA_DIR, uuid->timeLow, uuid->timeMid, uuid->timeHiAndVersion,
Jens Wiklanderb7940892015-10-23 16:02:40 +0200614 uuid->clockSeqAndNode[0], uuid->clockSeqAndNode[1],
615 uuid->clockSeqAndNode[2], uuid->clockSeqAndNode[3],
616 uuid->clockSeqAndNode[4], uuid->clockSeqAndNode[5],
David Brownb2865ab2016-08-02 11:44:41 -0600617 uuid->clockSeqAndNode[6], uuid->clockSeqAndNode[7]);
Jens Wiklanderb7940892015-10-23 16:02:40 +0200618
Jens Wiklanderb7940892015-10-23 16:02:40 +0200619 return fopen(buf, mode);
620}
621
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100622static bool load_corrupt_ta(ADBG_Case_t *c, size_t offs, uint8_t mask)
Jens Wiklander4441fe22015-10-23 16:53:02 +0200623{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100624 TEEC_Session session = { };
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100625 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
626 TEEC_UUID uuid = PTA_SECSTOR_TA_MGMT_UUID;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100627 TEEC_Result res = TEEC_ERROR_GENERIC;
628 uint32_t ret_orig = 0;
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100629 FILE *f = NULL;
630 bool r = false;
631 uint8_t *buf = NULL;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100632 size_t sz = 0;
633 size_t fread_res = 0;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200634
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100635 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
636 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
637 goto out;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200638
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100639 f = open_ta_file(&create_fail_test_ta_uuid, "rb");
640 if (!ADBG_EXPECT_NOT_NULL(c, f))
641 goto out;
642 if (!ADBG_EXPECT_TRUE(c, !fseek(f, 0, SEEK_END)))
643 goto out;
644 sz = ftell(f);
645 rewind(f);
646
647 buf = malloc(sz);
648 if (!ADBG_EXPECT_NOT_NULL(c, buf))
649 goto out;
650
651 fread_res = fread(buf, 1, sz, f);
652 if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, fread_res, ==, sz))
653 goto out;
654
Jens Wiklander4441fe22015-10-23 16:53:02 +0200655 fclose(f);
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100656 f = NULL;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200657
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100658 buf[MIN(offs, sz)] ^= mask;
Jens Wiklander4441fe22015-10-23 16:53:02 +0200659
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100660 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, TEEC_NONE,
661 TEEC_NONE, TEEC_NONE);
662 op.params[0].tmpref.buffer = buf;
663 op.params[0].tmpref.size = sz;
664
665 res = TEEC_InvokeCommand(&session, PTA_SECSTOR_TA_MGMT_BOOTSTRAP, &op,
666 &ret_orig);
667 r = ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_SECURITY, res);
668out:
669 free(buf);
670 if (f)
671 fclose(f);
672 TEEC_CloseSession(&session);
Jens Wiklander4441fe22015-10-23 16:53:02 +0200673 return r;
674}
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100675#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Jens Wiklander4441fe22015-10-23 16:53:02 +0200676
Pascal Brandc639ac82015-07-02 08:53:34 +0200677static void xtest_tee_test_1008(ADBG_Case_t *c)
678{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100679 TEEC_Session session = { };
680 TEEC_Session session_crypt = { };
681 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200682
683 Do_ADBG_BeginSubCase(c, "Invoke command");
684 {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300685 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200686 xtest_teec_open_session(&session, &os_test_ta_uuid,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300687 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200688
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300689 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
690 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CLIENT,
691 NULL, &ret_orig));
692 TEEC_CloseSession(&session);
693 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200694
Pascal Brandc639ac82015-07-02 08:53:34 +0200695 }
696 Do_ADBG_EndSubCase(c, "Invoke command");
697
698 Do_ADBG_BeginSubCase(c, "Invoke command with timeout");
699 {
700 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
701
702 op.params[0].value.a = 2000;
703 op.paramTypes = TEEC_PARAM_TYPES(
704 TEEC_VALUE_INPUT, TEEC_NONE, TEEC_NONE, TEEC_NONE);
705
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300706 if (ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200707 xtest_teec_open_session(&session,
708 &os_test_ta_uuid,
709 NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300710 &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200711
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300712 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
713 TEEC_InvokeCommand(&session,
714 TA_OS_TEST_CMD_CLIENT_WITH_TIMEOUT,
715 &op, &ret_orig));
716 TEEC_CloseSession(&session);
717 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200718 }
719 Do_ADBG_EndSubCase(c, "Invoke command with timeout");
720
721 Do_ADBG_BeginSubCase(c, "Create session fail");
722 {
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100723 size_t n = 0;
Jens Wiklanderb7940892015-10-23 16:02:40 +0200724
Pascal Brandc639ac82015-07-02 08:53:34 +0200725 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
726 xtest_teec_open_session(&session_crypt,
727 &create_fail_test_ta_uuid, NULL,
728 &ret_orig));
Pascal Brandc639ac82015-07-02 08:53:34 +0200729 /*
730 * Run this several times to see that there's no memory leakage.
731 */
732 for (n = 0; n < 100; n++) {
733 Do_ADBG_Log("n = %zu", n);
734 (void)ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_GENERIC,
735 xtest_teec_open_session(&session_crypt,
736 &create_fail_test_ta_uuid,
737 NULL, &ret_orig));
738 }
739 }
740 Do_ADBG_EndSubCase(c, "Create session fail");
Jens Wiklanderb7940892015-10-23 16:02:40 +0200741
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100742#ifdef CFG_SECSTOR_TA_MGMT_PTA
Jens Wiklander4441fe22015-10-23 16:53:02 +0200743 Do_ADBG_BeginSubCase(c, "Load corrupt TA");
744 ADBG_EXPECT_TRUE(c,
745 load_corrupt_ta(c, offsetof(struct shdr, magic), 1));
746 ADBG_EXPECT_TRUE(c,
747 load_corrupt_ta(c, offsetof(struct shdr, img_type), 1));
748 ADBG_EXPECT_TRUE(c,
749 load_corrupt_ta(c, offsetof(struct shdr, img_size), 1));
750 ADBG_EXPECT_TRUE(c,
751 load_corrupt_ta(c, offsetof(struct shdr, algo), 1));
752 ADBG_EXPECT_TRUE(c,
753 load_corrupt_ta(c, offsetof(struct shdr, hash_size), 1));
754 ADBG_EXPECT_TRUE(c,
755 load_corrupt_ta(c, offsetof(struct shdr, sig_size), 1));
756 ADBG_EXPECT_TRUE(c,
757 load_corrupt_ta(c, sizeof(struct shdr), 1)); /* hash */
758 ADBG_EXPECT_TRUE(c,
759 load_corrupt_ta(c, sizeof(struct shdr) + 32, 1)); /* sig */
760 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 3000, 1)); /* payload */
Jerome Forissier895c5ca2019-02-28 17:27:46 +0100761 ADBG_EXPECT_TRUE(c, load_corrupt_ta(c, 8000, 1)); /* payload */
Jens Wiklander4441fe22015-10-23 16:53:02 +0200762 Do_ADBG_EndSubCase(c, "Load corrupt TA");
Jens Wiklanderec545fb2017-11-24 16:58:07 +0100763#endif /*CFG_SECSTOR_TA_MGMT_PTA*/
Pascal Brandc639ac82015-07-02 08:53:34 +0200764}
Jens Wiklander14f48872018-06-29 15:30:13 +0200765ADBG_CASE_DEFINE(regression, 1008, xtest_tee_test_1008,
766 "TEE internal client API");
Pascal Brandc639ac82015-07-02 08:53:34 +0200767
Pascal Brandc639ac82015-07-02 08:53:34 +0200768static void *cancellation_thread(void *arg)
769{
770 /*
771 * Sleep 0.5 seconds before cancellation to make sure that the other
772 * thread is in RPC_WAIT.
773 */
774 (void)usleep(500000);
775 TEEC_RequestCancellation(arg);
776 return NULL;
777}
Pascal Brandc639ac82015-07-02 08:53:34 +0200778
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300779static void xtest_tee_test_1009_subcase(ADBG_Case_t *c, const char *subcase,
780 uint32_t timeout, bool cancel)
Pascal Brandc639ac82015-07-02 08:53:34 +0200781{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100782 TEEC_Session session = { };
783 uint32_t ret_orig = 0;
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300784 pthread_t thr;
Pascal Brandc639ac82015-07-02 08:53:34 +0200785
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100786 memset(&thr, 0, sizeof(thr));
787
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300788 Do_ADBG_BeginSubCase(c, "%s", subcase);
Pascal Brandc639ac82015-07-02 08:53:34 +0200789 {
790 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
791
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300792 if (ADBG_EXPECT_TEEC_SUCCESS(c,
793 xtest_teec_open_session(&session, &os_test_ta_uuid,
794 NULL, &ret_orig))) {
Pascal Brandc639ac82015-07-02 08:53:34 +0200795
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300796 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c,
797 TEEC_ORIGIN_TRUSTED_APP,
798 ret_orig);
Pascal Brandc639ac82015-07-02 08:53:34 +0200799
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300800 op.params[0].value.a = timeout;
801 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
802 TEEC_NONE,
803 TEEC_NONE, TEEC_NONE);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300804 if (cancel) {
805 (void)ADBG_EXPECT(c, 0,
806 pthread_create(&thr, NULL,
807 cancellation_thread, &op));
Pascal Brandc639ac82015-07-02 08:53:34 +0200808
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300809 (void)ADBG_EXPECT_TEEC_RESULT(c,
810 TEEC_ERROR_CANCEL,
811 TEEC_InvokeCommand(&session,
812 TA_OS_TEST_CMD_WAIT,
813 &op,
814 &ret_orig));
815 } else
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300816
817 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
818 TEEC_InvokeCommand(&session,
819 TA_OS_TEST_CMD_WAIT,
820 &op,
821 &ret_orig));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300822 if (cancel)
823 (void)ADBG_EXPECT(c, 0, pthread_join(thr, NULL));
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300824
825 TEEC_CloseSession(&session);
826 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200827 }
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300828 Do_ADBG_EndSubCase(c, "%s", subcase);
829}
830
831static void xtest_tee_test_1009(ADBG_Case_t *c)
832{
833 xtest_tee_test_1009_subcase(c, "TEE Wait 0.1s", 100, false);
834 xtest_tee_test_1009_subcase(c, "TEE Wait 0.5s", 500, false);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300835 xtest_tee_test_1009_subcase(c, "TEE Wait 2s cancel", 2000, true);
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300836 xtest_tee_test_1009_subcase(c, "TEE Wait 2s", 2000, false);
Pascal Brandc639ac82015-07-02 08:53:34 +0200837}
Jens Wiklander14f48872018-06-29 15:30:13 +0200838ADBG_CASE_DEFINE(regression, 1009, xtest_tee_test_1009, "TEE Wait");
Pascal Brandc639ac82015-07-02 08:53:34 +0200839
840static void xtest_tee_test_1010(ADBG_Case_t *c)
841{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100842 unsigned int n = 0;
843 unsigned int idx = 0;
Etienne Carriere92c34422018-02-09 13:11:40 +0100844 size_t memref_sz[] = { 1024, 65536 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200845
846 for (n = 1; n <= 5; n++) {
847 Do_ADBG_BeginSubCase(c, "Invalid memory access %u", n);
848 xtest_tee_test_invalid_mem_access(c, n);
849 Do_ADBG_EndSubCase(c, "Invalid memory access %u", n);
850 }
Etienne Carriere92c34422018-02-09 13:11:40 +0100851
852 for (idx = 0; idx < ARRAY_SIZE(memref_sz); idx++) {
853 for (n = 1; n <= 5; n++) {
854 Do_ADBG_BeginSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200855 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100856 n, memref_sz[idx]);
857 xtest_tee_test_invalid_mem_access2(c, n, memref_sz[idx]);
858 Do_ADBG_EndSubCase(c,
Igor Opaniuke8236ac2018-02-12 12:26:25 +0200859 "Invalid memory access %u with %zu bytes memref",
Etienne Carriere92c34422018-02-09 13:11:40 +0100860 n, memref_sz[idx]);
861 }
862 }
Pascal Brandc639ac82015-07-02 08:53:34 +0200863}
Jens Wiklander14f48872018-06-29 15:30:13 +0200864ADBG_CASE_DEFINE(regression, 1010, xtest_tee_test_1010,
865 "Invalid memory access");
Pascal Brandc639ac82015-07-02 08:53:34 +0200866
867static void xtest_tee_test_1011(ADBG_Case_t *c)
868{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100869 TEEC_Session session = { };
870 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200871 struct xtest_crypto_session cs = {
872 c, &session, TA_RPC_CMD_CRYPT_SHA256,
873 TA_RPC_CMD_CRYPT_AES256ECB_ENC,
874 TA_RPC_CMD_CRYPT_AES256ECB_DEC
875 };
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100876 struct xtest_crypto_session cs_privmem = {
877 c, &session,
878 TA_RPC_CMD_CRYPT_PRIVMEM_SHA256,
879 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_ENC,
880 TA_RPC_CMD_CRYPT_PRIVMEM_AES256ECB_DEC
881 };
Pascal Brandc639ac82015-07-02 08:53:34 +0200882 TEEC_UUID uuid = rpc_test_ta_uuid;
883
884 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
885 xtest_teec_open_session(&session, &uuid, NULL, &ret_orig)))
886 return;
887
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100888 Do_ADBG_BeginSubCase(c, "TA-to-TA via non-secure shared memory");
Pascal Brandc639ac82015-07-02 08:53:34 +0200889 /*
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100890 * Run the "complete crypto test suite" using TA-to-TA
891 * communication
Pascal Brandc639ac82015-07-02 08:53:34 +0200892 */
893 xtest_crypto_test(&cs);
Jens Wiklanderf7b9c632017-01-03 17:32:26 +0100894 Do_ADBG_EndSubCase(c, "TA-to-TA via non-secure shared memory");
895
896 Do_ADBG_BeginSubCase(c, "TA-to-TA via TA private memory");
897 /*
898 * Run the "complete crypto test suite" using TA-to-TA
899 * communication via TA private memory.
900 */
901 xtest_crypto_test(&cs_privmem);
902 Do_ADBG_EndSubCase(c, "TA-to-TA via TA private memory");
903
Pascal Brandc639ac82015-07-02 08:53:34 +0200904 TEEC_CloseSession(&session);
905}
Jens Wiklander14f48872018-06-29 15:30:13 +0200906ADBG_CASE_DEFINE(regression, 1011, xtest_tee_test_1011,
907 "Test TA-to-TA features with User Crypt TA");
Pascal Brandc639ac82015-07-02 08:53:34 +0200908
909/*
910 * Note that this test is failing when
911 * - running twice in a raw
912 * - and the user TA is statically linked
913 * This is because the counter is not reseted when opening the first session
914 * in case the TA is statically linked
915 */
916static void xtest_tee_test_1012(ADBG_Case_t *c)
917{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100918 TEEC_Session session1 = { };
919 TEEC_Session session2 = { };
920 uint32_t ret_orig = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200921 TEEC_UUID uuid = sims_test_ta_uuid;
922
923 Do_ADBG_BeginSubCase(c, "Single Instance Multi Session");
924 {
925 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
926 static const uint8_t in[] = {
927 0x5A, 0x6E, 0x04, 0x57, 0x08, 0xFB, 0x71, 0x96,
928 0xF0, 0x2E, 0x55, 0x3D, 0x02, 0xC3, 0xA6, 0x92,
929 0xE9, 0xC3, 0xEF, 0x8A, 0xB2, 0x34, 0x53, 0xE6,
930 0xF0, 0x74, 0x9C, 0xD6, 0x36, 0xE7, 0xA8, 0x8E
931 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +0100932 uint8_t out[32] = { };
933 int i = 0;
Pascal Brandc639ac82015-07-02 08:53:34 +0200934
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300935 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200936 xtest_teec_open_session(&session1, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300937 &ret_orig)))
938 return;
Pascal Brandc639ac82015-07-02 08:53:34 +0200939
940 op.params[0].value.a = 0;
941 op.params[1].tmpref.buffer = (void *)in;
942 op.params[1].tmpref.size = sizeof(in);
943 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
944 TEEC_MEMREF_TEMP_INPUT,
945 TEEC_NONE, TEEC_NONE);
946
947 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
948 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_WRITE, &op,
949 &ret_orig));
950
Jerome Forissier3cc0a422017-12-14 09:53:24 +0100951 for (i = 1; i < 3; i++) {
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300952 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
Pascal Brandc639ac82015-07-02 08:53:34 +0200953 xtest_teec_open_session(&session2, &uuid, NULL,
Volodymyr Babchukae3dcd72016-10-20 16:51:59 +0300954 &ret_orig)))
955 continue;
Pascal Brandc639ac82015-07-02 08:53:34 +0200956
957 op.params[0].value.a = 0;
958 op.params[1].tmpref.buffer = out;
959 op.params[1].tmpref.size = sizeof(out);
960 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
961 TEEC_MEMREF_TEMP_OUTPUT,
962 TEEC_NONE, TEEC_NONE);
963
964 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
965 TEEC_InvokeCommand(&session2, TA_SIMS_CMD_READ,
966 &op, &ret_orig));
967
968 if (!ADBG_EXPECT_BUFFER(c, in, sizeof(in), out,
969 sizeof(out))) {
970 Do_ADBG_Log("in:");
971 Do_ADBG_HexLog(in, sizeof(in), 16);
972 Do_ADBG_Log("out:");
973 Do_ADBG_HexLog(out, sizeof(out), 16);
974 }
975
976 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
977 TEEC_NONE, TEEC_NONE,
978 TEEC_NONE);
979
980 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
981 TEEC_InvokeCommand(&session1,
982 TA_SIMS_CMD_GET_COUNTER,
983 &op, &ret_orig));
984
985 (void)ADBG_EXPECT(c, 0, op.params[0].value.a);
986
987 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
988 TEEC_InvokeCommand(&session2,
989 TA_SIMS_CMD_GET_COUNTER, &op,
990 &ret_orig));
991
992 (void)ADBG_EXPECT(c, i, op.params[0].value.a);
993 TEEC_CloseSession(&session2);
994 }
995
996 memset(out, 0, sizeof(out));
997 op.params[0].value.a = 0;
998 op.params[1].tmpref.buffer = out;
999 op.params[1].tmpref.size = sizeof(out);
1000 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_INPUT,
1001 TEEC_MEMREF_TEMP_OUTPUT,
1002 TEEC_NONE, TEEC_NONE);
1003
1004 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1005 TEEC_InvokeCommand(&session1, TA_SIMS_CMD_READ, &op,
1006 &ret_orig));
1007
1008 if (!ADBG_EXPECT(c, 0, memcmp(in, out, sizeof(in)))) {
1009 Do_ADBG_Log("in:");
1010 Do_ADBG_HexLog(in, sizeof(in), 16);
1011 Do_ADBG_Log("out:");
1012 Do_ADBG_HexLog(out, sizeof(out), 16);
1013 }
1014
1015 TEEC_CloseSession(&session1);
1016 }
Ovidiu Mihalachi3e6183e2019-04-25 10:23:21 +03001017 Do_ADBG_EndSubCase(c, "Single Instance Multi Session");
Pascal Brandc639ac82015-07-02 08:53:34 +02001018}
Jens Wiklander14f48872018-06-29 15:30:13 +02001019ADBG_CASE_DEFINE(regression, 1012, xtest_tee_test_1012,
1020 "Test Single Instance Multi Session features with SIMS TA");
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001021
1022struct test_1013_thread_arg {
Jens Wiklander70672972016-04-06 00:01:45 +02001023 const TEEC_UUID *uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001024 uint32_t cmd;
1025 uint32_t repeat;
1026 TEEC_SharedMemory *shm;
1027 uint32_t error_orig;
1028 TEEC_Result res;
1029 uint32_t max_concurrency;
1030 const uint8_t *in;
1031 size_t in_len;
1032 uint8_t *out;
1033 size_t out_len;
1034};
1035
1036static void *test_1013_thread(void *arg)
1037{
1038 struct test_1013_thread_arg *a = arg;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001039 TEEC_Session session = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001040 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1041 uint8_t p2 = TEEC_NONE;
1042 uint8_t p3 = TEEC_NONE;
1043
Jens Wiklander70672972016-04-06 00:01:45 +02001044 a->res = xtest_teec_open_session(&session, a->uuid, NULL,
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001045 &a->error_orig);
1046 if (a->res != TEEC_SUCCESS)
1047 return NULL;
1048
1049 op.params[0].memref.parent = a->shm;
1050 op.params[0].memref.size = a->shm->size;
1051 op.params[0].memref.offset = 0;
1052 op.params[1].value.a = a->repeat;
1053 op.params[1].value.b = 0;
1054 op.params[2].tmpref.buffer = (void *)a->in;
1055 op.params[2].tmpref.size = a->in_len;
1056 op.params[3].tmpref.buffer = a->out;
1057 op.params[3].tmpref.size = a->out_len;
1058
1059 if (a->in_len)
1060 p2 = TEEC_MEMREF_TEMP_INPUT;
1061 if (a->out_len)
1062 p3 = TEEC_MEMREF_TEMP_OUTPUT;
1063
1064 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INOUT,
1065 TEEC_VALUE_INOUT, p2, p3);
1066
1067 a->res = TEEC_InvokeCommand(&session, a->cmd, &op, &a->error_orig);
1068 a->max_concurrency = op.params[1].value.b;
1069 a->out_len = op.params[3].tmpref.size;
1070 TEEC_CloseSession(&session);
1071 return NULL;
1072}
1073
Pascal Brand4fa35582015-12-17 10:59:12 +01001074#define NUM_THREADS 3
1075
Jens Wiklander70672972016-04-06 00:01:45 +02001076static void xtest_tee_test_1013_single(ADBG_Case_t *c, double *mean_concurrency,
1077 const TEEC_UUID *uuid)
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001078{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001079 size_t nt = 0;
1080 size_t n = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001081 size_t repeat = 1000;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001082 TEEC_SharedMemory shm = { };
1083 size_t max_concurrency = 0;
1084 struct test_1013_thread_arg arg[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001085 static const uint8_t sha256_in[] = { 'a', 'b', 'c' };
1086 static const uint8_t sha256_out[] = {
1087 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
1088 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
1089 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
1090 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad
1091 };
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001092 uint8_t out[32] = { };
1093 pthread_t thr[NUM_THREADS] = { };
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001094
Jens Wiklander70672972016-04-06 00:01:45 +02001095 Do_ADBG_BeginSubCase(c, "Busy loop repeat %zu", repeat * 10);
Pascal Brand4fa35582015-12-17 10:59:12 +01001096 *mean_concurrency = 0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001097
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001098 shm.size = sizeof(struct ta_concurrent_shm);
1099 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1100 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1101 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1102 return;
1103
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001104 memset(shm.buffer, 0, shm.size);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001105 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001106 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001107
1108 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001109 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001110 arg[n].cmd = TA_CONCURRENT_CMD_BUSY_LOOP;
Jens Wiklander70672972016-04-06 00:01:45 +02001111 arg[n].repeat = repeat * 10;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001112 arg[n].shm = &shm;
1113 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1114 test_1013_thread, arg + n)))
1115 nt = n; /* break loop and start cleanup */
1116 }
1117
1118 for (n = 0; n < nt; n++) {
1119 ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL));
1120 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res);
1121 if (arg[n].max_concurrency > max_concurrency)
1122 max_concurrency = arg[n].max_concurrency;
1123 }
1124
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001125 /*
1126 * Concurrency can be limited by several factors, for instance in a
1127 * single CPU system it's dependent on the Preemtion Model used by
1128 * the kernel (Preemptible Kernel (Low-Latency Desktop) gives the
1129 * best result there).
1130 */
1131 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, >, 0);
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001132 (void)ADBG_EXPECT_COMPARE_UNSIGNED(c, max_concurrency, <=, NUM_THREADS);
Pascal Brand4fa35582015-12-17 10:59:12 +01001133 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001134 Do_ADBG_EndSubCase(c, "Busy loop repeat %zu", repeat * 10);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001135
Jens Wiklander70672972016-04-06 00:01:45 +02001136 Do_ADBG_BeginSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001137 memset(shm.buffer, 0, shm.size);
1138 memset(arg, 0, sizeof(arg));
1139 max_concurrency = 0;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001140 nt = NUM_THREADS;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001141
1142 for (n = 0; n < nt; n++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001143 arg[n].uuid = uuid;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001144 arg[n].cmd = TA_CONCURRENT_CMD_SHA256;
Jens Wiklander70672972016-04-06 00:01:45 +02001145 arg[n].repeat = repeat;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001146 arg[n].shm = &shm;
1147 arg[n].in = sha256_in;
1148 arg[n].in_len = sizeof(sha256_in);
1149 arg[n].out = out;
1150 arg[n].out_len = sizeof(out);
1151 if (!ADBG_EXPECT(c, 0, pthread_create(thr + n, NULL,
1152 test_1013_thread, arg + n)))
1153 nt = n; /* break loop and start cleanup */
1154 }
1155
1156 for (n = 0; n < nt; n++) {
1157 if (ADBG_EXPECT(c, 0, pthread_join(thr[n], NULL)) &&
1158 ADBG_EXPECT_TEEC_SUCCESS(c, arg[n].res))
1159 ADBG_EXPECT_BUFFER(c, sha256_out, sizeof(sha256_out),
1160 arg[n].out, arg[n].out_len);
1161 if (arg[n].max_concurrency > max_concurrency)
1162 max_concurrency = arg[n].max_concurrency;
1163 }
Pascal Brand4fa35582015-12-17 10:59:12 +01001164 *mean_concurrency += max_concurrency;
Jens Wiklander70672972016-04-06 00:01:45 +02001165 Do_ADBG_EndSubCase(c, "SHA-256 loop repeat %zu", repeat);
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001166
Pascal Brand4fa35582015-12-17 10:59:12 +01001167 *mean_concurrency /= 2.0;
Jens Wiklanderac27ec12015-07-15 15:23:14 +02001168 TEEC_ReleaseSharedMemory(&shm);
1169}
Pascal Brand4fa35582015-12-17 10:59:12 +01001170
1171static void xtest_tee_test_1013(ADBG_Case_t *c)
1172{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001173 int i = 0;
1174 double mean_concurrency = 0;
1175 double concurrency = 0;
Jens Wiklander70672972016-04-06 00:01:45 +02001176 int nb_loops = 24;
Jens Wiklander6a9d15a2016-02-01 10:29:42 +01001177
1178 if (level == 0)
1179 nb_loops /= 2;
Pascal Brand4fa35582015-12-17 10:59:12 +01001180
Jens Wiklander70672972016-04-06 00:01:45 +02001181 Do_ADBG_BeginSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001182 mean_concurrency = 0;
1183 for (i = 0; i < nb_loops; i++) {
Jens Wiklander70672972016-04-06 00:01:45 +02001184 xtest_tee_test_1013_single(c, &concurrency,
1185 &concurrent_ta_uuid);
Pascal Brand4fa35582015-12-17 10:59:12 +01001186 mean_concurrency += concurrency;
1187 }
1188 mean_concurrency /= nb_loops;
1189
1190 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1191 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
Jens Wiklander70672972016-04-06 00:01:45 +02001192 Do_ADBG_EndSubCase(c, "Using small concurrency TA");
Pascal Brand4fa35582015-12-17 10:59:12 +01001193
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001194#ifndef CFG_PAGED_USER_TA
Jens Wiklander70672972016-04-06 00:01:45 +02001195 Do_ADBG_BeginSubCase(c, "Using large concurrency TA");
1196 mean_concurrency = 0;
1197 for (i = 0; i < nb_loops; i++) {
1198 xtest_tee_test_1013_single(c, &concurrency,
1199 &concurrent_large_ta_uuid);
1200 mean_concurrency += concurrency;
1201 }
1202 mean_concurrency /= nb_loops;
1203
1204 Do_ADBG_Log(" Number of parallel threads: %d", NUM_THREADS);
1205 Do_ADBG_Log(" Mean concurrency: %g", mean_concurrency);
1206 Do_ADBG_EndSubCase(c, "Using large concurrency TA");
Jens Wiklanderc9d7b242016-06-28 18:35:08 +02001207#endif
Jens Wiklander70672972016-04-06 00:01:45 +02001208}
Jens Wiklander14f48872018-06-29 15:30:13 +02001209ADBG_CASE_DEFINE(regression, 1013, xtest_tee_test_1013,
1210 "Test concurency with concurrent TA");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001211
1212#ifdef CFG_SECURE_DATA_PATH
1213static void xtest_tee_test_1014(ADBG_Case_t *c)
1214{
1215 UNUSED(c);
1216
1217 int size = 17000;
1218 int loop = 10;
1219 int ion_heap = DEFAULT_ION_HEAP_TYPE;
1220 int rnd_offset = 1;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001221 int test = 0;
1222 int ret = 0;
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001223
1224 test = TEST_NS_TO_TA;
1225 Do_ADBG_BeginSubCase(c, "SDP: NonSecure client invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001226 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001227 ADBG_EXPECT(c, 0, ret);
1228 Do_ADBG_EndSubCase(c, "SDP: NonSecure client invokes a SDP TA");
1229
1230 test = TEST_TA_TO_TA;
1231 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP TA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001232 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001233 ADBG_EXPECT(c, 0, ret);
1234 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP TA");
1235
1236 test = TEST_TA_TO_PTA;
1237 Do_ADBG_BeginSubCase(c, "SDP: SDP TA invokes a SDP pTA");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001238 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001239 ADBG_EXPECT(c, 0, ret);
1240 Do_ADBG_EndSubCase(c, "SDP: SDP TA invokes a SDP pTA");
1241
1242 test = TEST_NS_TO_PTA;
Etienne Carrierea3198522017-10-26 09:48:55 +02001243 Do_ADBG_BeginSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriereb9a95822017-04-26 15:03:53 +02001244 ret = sdp_basic_test(test, size, loop, ion_heap, rnd_offset, 0);
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001245 ADBG_EXPECT(c, 1, ret);
Etienne Carrierea3198522017-10-26 09:48:55 +02001246 Do_ADBG_EndSubCase(c, "SDP: NSec CA invokes SDP pTA (should fail)");
Etienne Carriere50abf9a2017-03-24 11:33:50 +01001247}
Jens Wiklander14f48872018-06-29 15:30:13 +02001248ADBG_CASE_DEFINE(regression, 1014, xtest_tee_test_1014,
1249 "Test secure data path against SDP TAs and pTAs");
1250#endif /*CFG_SECURE_DATA_PATH*/
Jens Wiklander272d3642017-04-03 13:03:47 +02001251
1252static void xtest_tee_test_1015(ADBG_Case_t *c)
1253{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001254 TEEC_Result res = TEEC_ERROR_GENERIC;
1255 TEEC_Session session = { };
1256 uint32_t ret_orig = 0;
Jens Wiklander272d3642017-04-03 13:03:47 +02001257
Etienne Carriere11093162017-10-26 09:49:04 +02001258 /* Pseudo TA is optional: warn and nicely exit if not found */
Jens Wiklander272d3642017-04-03 13:03:47 +02001259 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1260 &ret_orig);
Etienne Carriere11093162017-10-26 09:49:04 +02001261 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1262 Do_ADBG_Log(" - 1015 - skip test, pseudo TA not found");
Jens Wiklander272d3642017-04-03 13:03:47 +02001263 return;
Etienne Carriere11093162017-10-26 09:49:04 +02001264 }
1265 ADBG_EXPECT_TEEC_SUCCESS(c, res);
Jens Wiklander272d3642017-04-03 13:03:47 +02001266
1267 ADBG_EXPECT_TEEC_SUCCESS(c,
1268 TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_FS_HTREE,
1269 NULL, &ret_orig));
1270 TEEC_CloseSession(&session);
1271}
Jens Wiklander14f48872018-06-29 15:30:13 +02001272ADBG_CASE_DEFINE(regression, 1015, xtest_tee_test_1015,
1273 "FS hash-tree corner cases");
Jerome Forissiere916b102017-06-07 17:55:52 +02001274
1275static void xtest_tee_test_1016(ADBG_Case_t *c)
1276{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001277 TEEC_Session session = { };
Jerome Forissiere916b102017-06-07 17:55:52 +02001278 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001279 uint32_t ret_orig = 0;
Jerome Forissiere916b102017-06-07 17:55:52 +02001280
1281 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1282 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1283 &ret_orig)))
1284 return;
1285
1286 op.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE,
1287 TEEC_NONE);
1288
1289 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1290 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_TA2TA_MEMREF, &op,
1291 &ret_orig));
1292
1293 TEEC_CloseSession(&session);
1294}
Jens Wiklander14f48872018-06-29 15:30:13 +02001295ADBG_CASE_DEFINE(regression, 1016, xtest_tee_test_1016,
1296 "Test TA to TA transfers (in/out/inout memrefs on the stack)");
Jens Wiklander87e81702018-03-20 12:00:00 +08001297
1298static void xtest_tee_test_1017(ADBG_Case_t *c)
1299{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001300 TEEC_Session session = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001301 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001302 uint32_t ret_orig = 0;
1303 TEEC_SharedMemory shm = { };
Jens Wiklander87e81702018-03-20 12:00:00 +08001304 size_t page_size = 4096;
1305
Jens Wiklander87e81702018-03-20 12:00:00 +08001306 shm.size = 8 * page_size;
1307 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1308 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1309 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1310 return;
1311
1312 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1313 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1314 &ret_orig)))
1315 goto out;
1316
1317 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1318 TEEC_MEMREF_PARTIAL_INPUT,
1319 TEEC_MEMREF_PARTIAL_OUTPUT,
1320 TEEC_MEMREF_PARTIAL_OUTPUT);
1321
1322 /*
1323 * The first two memrefs are supposed to be combined into in
1324 * region and the last two memrefs should have one region each
1325 * when the parameters are mapped for the TA.
1326 */
1327 op.params[0].memref.parent = &shm;
1328 op.params[0].memref.size = page_size;
1329 op.params[0].memref.offset = 0;
1330
1331 op.params[1].memref.parent = &shm;
1332 op.params[1].memref.size = page_size;
1333 op.params[1].memref.offset = page_size;
1334
1335 op.params[2].memref.parent = &shm;
1336 op.params[2].memref.size = page_size;
1337 op.params[2].memref.offset = 4 * page_size;
1338
1339 op.params[3].memref.parent = &shm;
1340 op.params[3].memref.size = 2 * page_size;
1341 op.params[3].memref.offset = 6 * page_size;
1342
1343 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1344 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1345 &ret_orig));
1346
1347 TEEC_CloseSession(&session);
1348out:
1349 TEEC_ReleaseSharedMemory(&shm);
1350}
Jens Wiklander14f48872018-06-29 15:30:13 +02001351ADBG_CASE_DEFINE(regression, 1017, xtest_tee_test_1017,
1352 "Test coalescing memrefs");
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001353
1354static void xtest_tee_test_1018(ADBG_Case_t *c)
1355{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001356 TEEC_Session session = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001357 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001358 uint32_t ret_orig = 0;
1359 TEEC_SharedMemory shm = { };
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001360 size_t page_size = 4096;
1361
Jens Wiklanderbb10bcd2018-03-20 12:50:58 +08001362 shm.size = 8 * page_size;
1363 shm.flags = TEEC_MEM_INPUT | TEEC_MEM_OUTPUT;
1364 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1365 TEEC_AllocateSharedMemory(&xtest_teec_ctx, &shm)))
1366 return;
1367
1368 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1369 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1370 &ret_orig)))
1371 goto out;
1372
1373 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_PARTIAL_INPUT,
1374 TEEC_MEMREF_PARTIAL_INPUT,
1375 TEEC_MEMREF_PARTIAL_OUTPUT,
1376 TEEC_MEMREF_PARTIAL_OUTPUT);
1377
1378 /*
1379 * The first two memrefs are supposed to be combined into in
1380 * region and the last two memrefs should have one region each
1381 * when the parameters are mapped for the TA.
1382 */
1383 op.params[0].memref.parent = &shm;
1384 op.params[0].memref.size = page_size;
1385 op.params[0].memref.offset = 0;
1386
1387 op.params[1].memref.parent = &shm;
1388 op.params[1].memref.size = page_size;
1389 op.params[1].memref.offset = page_size;
1390
1391 op.params[2].memref.parent = &shm;
1392 op.params[2].memref.size = page_size;
1393 op.params[2].memref.offset = 4 * page_size;
1394
1395 op.params[3].memref.parent = &shm;
1396 op.params[3].memref.size = 3 * page_size;
1397 op.params[3].memref.offset = 6 * page_size;
1398
1399 /*
1400 * Depending on the tee driver we may have different error codes.
1401 * What's most important is that secure world doesn't panic and
1402 * that someone detects an error.
1403 */
1404 ADBG_EXPECT_NOT(c, TEE_SUCCESS,
1405 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_PARAMS, &op,
1406 &ret_orig));
1407
1408 TEEC_CloseSession(&session);
1409out:
1410 TEEC_ReleaseSharedMemory(&shm);
1411}
Jens Wiklander14f48872018-06-29 15:30:13 +02001412ADBG_CASE_DEFINE(regression, 1018, xtest_tee_test_1018,
1413 "Test memref out of bounds");
Jerome Forissier53bde722018-05-31 09:14:54 +02001414
Victor Chong3ff36f52018-06-07 04:37:00 +01001415#if defined(CFG_TA_DYNLINK)
Jerome Forissier53bde722018-05-31 09:14:54 +02001416static void xtest_tee_test_1019(ADBG_Case_t *c)
1417{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001418 TEEC_Session session = { };
1419 uint32_t ret_orig = 0;
Jerome Forissier53bde722018-05-31 09:14:54 +02001420
1421 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1422 xtest_teec_open_session(&session, &os_test_ta_uuid, NULL,
1423 &ret_orig)))
1424 return;
1425
1426 (void)ADBG_EXPECT_TEEC_SUCCESS(c,
1427 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB, NULL,
1428 &ret_orig));
1429
1430 (void)ADBG_EXPECT_TEEC_RESULT(c,
1431 TEEC_ERROR_TARGET_DEAD,
1432 TEEC_InvokeCommand(&session, TA_OS_TEST_CMD_CALL_LIB_PANIC,
1433 NULL, &ret_orig));
1434
1435 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TEE, ret_orig);
1436
1437 TEEC_CloseSession(&session);
1438}
Jens Wiklander14f48872018-06-29 15:30:13 +02001439ADBG_CASE_DEFINE(regression, 1019, xtest_tee_test_1019,
1440 "Test dynamically linked TA");
1441#endif /*CFG_TA_DYNLINK*/
Jerome Forissier3357f872018-10-05 15:13:44 +02001442
1443static void xtest_tee_test_1020(ADBG_Case_t *c)
1444{
Etienne Carrieree4ec9e42019-03-28 13:30:21 +01001445 TEEC_Result res = TEEC_ERROR_GENERIC;
1446 TEEC_Session session = { };
1447 uint32_t ret_orig = 0;
Jerome Forissier3357f872018-10-05 15:13:44 +02001448
1449 /* Pseudo TA is optional: warn and nicely exit if not found */
1450 res = xtest_teec_open_session(&session, &pta_invoke_tests_ta_uuid, NULL,
1451 &ret_orig);
1452 if (res == TEEC_ERROR_ITEM_NOT_FOUND) {
1453 Do_ADBG_Log(" - 1020 - skip test, pseudo TA not found");
1454 return;
1455 }
1456 ADBG_EXPECT_TEEC_SUCCESS(c, res);
1457
1458 res = TEEC_InvokeCommand(&session, PTA_INVOKE_TESTS_CMD_LOCKDEP,
1459 NULL, &ret_orig);
1460 if (res != TEEC_SUCCESS) {
1461 (void)ADBG_EXPECT_TEEC_ERROR_ORIGIN(c, TEEC_ORIGIN_TRUSTED_APP,
1462 ret_orig);
1463 if (res == TEEC_ERROR_NOT_SUPPORTED) {
1464 Do_ADBG_Log(" - 1020 - skip test, feature not "
1465 "implemented");
1466 goto out;
1467 }
1468 /* Error */
1469 (void)ADBG_EXPECT_TEEC_SUCCESS(c, res);
1470 }
1471out:
1472 TEEC_CloseSession(&session);
1473}
1474ADBG_CASE_DEFINE(regression, 1020, xtest_tee_test_1020,
1475 "Test lockdep algorithm");
Ovidiu Mihalachi15cecff2019-04-02 16:36:31 +03001476
1477static TEEC_Result open_sec_session(TEEC_Session *session,
1478 const TEEC_UUID *uuid)
1479{
1480 TEEC_Result res = TEEC_ERROR_GENERIC;
1481 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1482 uint32_t ret_orig = 0;
1483
1484 op.params[0].tmpref.buffer = (void *)uuid;
1485 op.params[0].tmpref.size = sizeof(*uuid);
1486 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1487 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1488
1489 res = TEEC_InvokeCommand(session, TA_SIMS_OPEN_TA_SESSION,
1490 &op, &ret_orig);
1491 if (res != TEEC_SUCCESS)
1492 return TEEC_ERROR_GENERIC;
1493
1494 return res;
1495}
1496
1497static TEEC_Result sims_get_counter(TEEC_Session *session,
1498 uint32_t *counter)
1499{
1500 TEEC_Result res = TEEC_ERROR_GENERIC;
1501 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1502 uint32_t ret_orig = 0;
1503
1504 op.paramTypes = TEEC_PARAM_TYPES(TEEC_VALUE_OUTPUT,
1505 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1506
1507 res = TEEC_InvokeCommand(session, TA_SIMS_CMD_GET_COUNTER,
1508 &op, &ret_orig);
1509 if (res == TEEC_SUCCESS)
1510 *counter = op.params[0].value.a;
1511
1512 return res;
1513}
1514
1515static TEEC_Result trigger_panic(TEEC_Session *session,
1516 const TEEC_UUID *uuid)
1517{
1518 TEEC_Operation op = TEEC_OPERATION_INITIALIZER;
1519 uint32_t ret_orig = 0;
1520
1521 if (!uuid) {
1522 op.params[0].tmpref.buffer = NULL;
1523 op.params[0].tmpref.size = 0;
1524 } else {
1525 op.params[0].tmpref.buffer = (void *)uuid;
1526 op.params[0].tmpref.size = sizeof(*uuid);
1527 }
1528 op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT,
1529 TEEC_NONE, TEEC_NONE, TEEC_NONE);
1530
1531 return TEEC_InvokeCommand(session, TA_SIMS_CMD_PANIC,
1532 &op, &ret_orig);
1533}
1534
1535static void test_panic_ca_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid,
1536 bool multi_instance)
1537{
1538 TEEC_Result exp_res = TEEC_ERROR_GENERIC;
1539 uint32_t counter = 0;
1540 uint32_t ret_orig = 0;
1541 uint32_t exp_counter = 0;
1542 TEEC_Session cs[3] = { };
1543
1544 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1545 xtest_teec_open_session(&cs[0], uuid, NULL,
1546 &ret_orig)))
1547 return;
1548
1549 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1550 xtest_teec_open_session(&cs[1], uuid, NULL,
1551 &ret_orig)))
1552 goto bail0;
1553
1554 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1555 goto bail1;
1556
1557 if (!ADBG_EXPECT(c, 0, counter))
1558 goto bail1;
1559
1560 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[1], &counter)))
1561 goto bail1;
1562
1563 exp_counter = multi_instance ? 0 : 1;
1564 if (ADBG_EXPECT(c, exp_counter, counter))
1565 goto bail1;
1566
1567 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1568 trigger_panic(&cs[1], NULL)))
1569 goto bail1;
1570
1571 exp_res = multi_instance ? TEEC_SUCCESS : TEEC_ERROR_TARGET_DEAD;
1572 if (!ADBG_EXPECT_TEEC_RESULT(c, exp_res,
1573 sims_get_counter(&cs[0], &counter)))
1574 goto bail1;
1575
1576 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1577 sims_get_counter(&cs[1], &counter)))
1578 goto bail1;
1579
1580 /* Attempt to open a session on panicked context */
1581 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1582 xtest_teec_open_session(&cs[1], uuid, NULL,
1583 &ret_orig)))
1584 goto bail1;
1585
1586 /* Sanity check of still valid TA context */
1587 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1588 xtest_teec_open_session(&cs[2], uuid, NULL,
1589 &ret_orig)))
1590 goto bail1;
1591
1592 /* Sanity check of still valid TA context */
1593 if (multi_instance) {
1594 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1595 sims_get_counter(&cs[0], &counter)))
1596 goto bail2;
1597
1598 if (!ADBG_EXPECT(c, 0, counter))
1599 goto bail2;
1600 }
1601
1602 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[2], &counter)))
1603 goto bail2;
1604
1605 if (!ADBG_EXPECT(c, 0, counter))
1606 goto bail2;
1607
1608bail2:
1609 TEEC_CloseSession(&cs[2]);
1610bail1:
1611 TEEC_CloseSession(&cs[1]);
1612bail0:
1613 TEEC_CloseSession(&cs[0]);
1614}
1615
1616static void test_panic_ta_to_ta(ADBG_Case_t *c, const TEEC_UUID *uuid1,
1617 const TEEC_UUID *uuid2)
1618{
1619 uint32_t ret_orig = 0;
1620 uint32_t counter = 0;
1621 TEEC_Session cs[3] = { };
1622
1623 /* Test pre-conditions */
1624 /* 2.1 - CA opens a session toward TA1 */
1625 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1626 xtest_teec_open_session(&cs[0], uuid1, NULL,
1627 &ret_orig)))
1628 return;
1629
1630 /* 2.2 - CA opens a session toward TA2 */
1631 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1632 xtest_teec_open_session(&cs[1], uuid2, NULL,
1633 &ret_orig)))
1634 goto bail0;
1635
1636 /* 2.3 - TA1 opens a session toward TA2 */
1637 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1638 goto bail1;
1639
1640 /* 2.4 - CA invokes TA2 which panics */
1641 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1642 trigger_panic(&cs[1], NULL)))
1643 goto bail1;
1644
1645 /* Expected results */
1646 /* 2.5 - Expect CA->TA1 session is still alive */
1647 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1648 goto bail1;
1649
1650 /* 2.6 - Expect CA->TA2 session is properly released */
1651 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1652 sims_get_counter(&cs[1], &counter)))
1653 goto bail1;
1654
1655bail1:
1656 TEEC_CloseSession(&cs[1]);
1657bail0:
1658 TEEC_CloseSession(&cs[0]);
1659
1660 memset(cs, 0, sizeof(cs));
1661
1662 /* Test pre-conditions */
1663 /* 2.1 - CA opens a session toward TA1 */
1664 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1665 xtest_teec_open_session(&cs[0], uuid1, NULL,
1666 &ret_orig)))
1667 return;
1668
1669 /* 2.2 - CA opens a session toward TA2 */
1670 if (!ADBG_EXPECT_TEEC_SUCCESS(c,
1671 xtest_teec_open_session(&cs[1], uuid2, NULL,
1672 &ret_orig)))
1673 goto bail2;
1674
1675 /* 2.3 - TA1 opens a session toward TA2 */
1676 if (!ADBG_EXPECT_TEEC_SUCCESS(c, open_sec_session(&cs[0], uuid2)))
1677 goto bail3;
1678
1679 /* 2.4 - CA invokes TA1 which invokes TA2 which panics */
1680 if (!ADBG_EXPECT_TEEC_SUCCESS(c, trigger_panic(&cs[0], uuid2)))
1681 goto bail3;
1682
1683 /* Expected results */
1684 /* 2.5 - Expect CA->TA1 session is still alive */
1685 if (!ADBG_EXPECT_TEEC_SUCCESS(c, sims_get_counter(&cs[0], &counter)))
1686 goto bail3;
1687
1688 /* 2.6 - Expect CA->TA2 session is properly released */
1689 if (!ADBG_EXPECT_TEEC_RESULT(c, TEEC_ERROR_TARGET_DEAD,
1690 sims_get_counter(&cs[1], &counter)))
1691 goto bail3;
1692
1693bail3:
1694 TEEC_CloseSession(&cs[1]);
1695bail2:
1696 TEEC_CloseSession(&cs[0]);
1697}
1698
1699static void xtest_tee_test_1021(ADBG_Case_t *c)
1700{
1701 Do_ADBG_BeginSubCase(c, "Multiple Instances Single Session");
1702 test_panic_ca_to_ta(c, &miss_test_ta_uuid, true);
1703 Do_ADBG_EndSubCase(c, "Multiple Instances Single Session");
1704
1705 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions");
1706 test_panic_ca_to_ta(c, &sims_test_ta_uuid, false);
1707 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions");
1708
1709 Do_ADBG_BeginSubCase(c, "Single Instance Multi Sessions Keep Alive");
1710 test_panic_ca_to_ta(c, &sims_keepalive_test_ta_uuid, false);
1711 Do_ADBG_EndSubCase(c, "Single Instance Multi Sessions Keep Alive");
1712
1713 Do_ADBG_BeginSubCase(c, "Multi Sessions TA to TA");
1714 test_panic_ta_to_ta(c, &sims_test_ta_uuid,
1715 &sims_keepalive_test_ta_uuid);
1716 Do_ADBG_EndSubCase(c, "Multi Sessions TA to TA");
1717}
1718ADBG_CASE_DEFINE(regression, 1021, xtest_tee_test_1021,
1719 "Test panic context release");