summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/arm64/mte/check_buffer_fill.c
blob: 242635d79035a78e71f1f67a44ae3fe507bdce66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2020 ARM Limited

#define _GNU_SOURCE

#include <stddef.h>
#include <stdio.h>
#include <string.h>

#include "kselftest.h"
#include "mte_common_util.h"
#include "mte_def.h"

#define OVERFLOW_RANGE MT_GRANULE_SIZE

static int sizes[] = {
	1, 555, 1033, MT_GRANULE_SIZE - 1, MT_GRANULE_SIZE,
	/* page size - 1*/ 0, /* page_size */ 0, /* page size + 1 */ 0
};

enum mte_block_test_alloc {
	UNTAGGED_TAGGED,
	TAGGED_UNTAGGED,
	TAGGED_TAGGED,
	BLOCK_ALLOC_MAX,
};

static int check_buffer_by_byte(int mem_type, int mode)
{
	char *ptr;
	int i, j, item;
	bool err;

	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
	item = sizeof(sizes)/sizeof(int);

	for (i = 0; i < item; i++) {
		ptr = (char *)mte_allocate_memory(sizes[i], mem_type, 0, true);
		if (check_allocated_memory(ptr, sizes[i], mem_type, true) != KSFT_PASS)
			return KSFT_FAIL;
		mte_initialize_current_context(mode, (uintptr_t)ptr, sizes[i]);
		/* Set some value in tagged memory */
		for (j = 0; j < sizes[i]; j++)
			ptr[j] = '1';
		mte_wait_after_trig();
		err = cur_mte_cxt.fault_valid;
		/* Check the buffer whether it is filled. */
		for (j = 0; j < sizes[i] && !err; j++) {
			if (ptr[j] != '1')
				err = true;
		}
		mte_free_memory((void *)ptr, sizes[i], mem_type, true);

		if (err)
			break;
	}
	if (!err)
		return KSFT_PASS;
	else
		return KSFT_FAIL;
}

static int check_buffer_underflow_by_byte(int mem_type, int mode,
					  int underflow_range)
{
	char *ptr;
	int i, j, item, last_index;
	bool err;
	char *und_ptr = NULL;

	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
	item = sizeof(sizes)/sizeof(int);
	for (i = 0; i < item; i++) {
		ptr = (char *)mte_allocate_memory_tag_range(sizes[i], mem_type, 0,
							    underflow_range, 0);
		if (check_allocated_memory_range(ptr, sizes[i], mem_type,
					       underflow_range, 0) != KSFT_PASS)
			return KSFT_FAIL;

		mte_initialize_current_context(mode, (uintptr_t)ptr, -underflow_range);
		last_index = 0;
		/* Set some value in tagged memory and make the buffer underflow */
		for (j = sizes[i] - 1; (j >= -underflow_range) &&
				       (cur_mte_cxt.fault_valid == false); j--) {
			ptr[j] = '1';
			last_index = j;
		}
		mte_wait_after_trig();
		err = false;
		/* Check whether the buffer is filled */
		for (j = 0; j < sizes[i]; j++) {
			if (ptr[j] != '1') {
				err = true;
				ksft_print_msg("Buffer is not filled at index:%d of ptr:0x%lx\n",
						j, ptr);
				break;
			}
		}
		if (err)
			goto check_buffer_underflow_by_byte_err;

		switch (mode) {
		case MTE_NONE_ERR:
			if (cur_mte_cxt.fault_valid == true || last_index != -underflow_range) {
				err = true;
				break;
			}
			/* There were no fault so the underflow area should be filled */
			und_ptr = (char *) MT_CLEAR_TAG((size_t) ptr - underflow_range);
			for (j = 0 ; j < underflow_range; j++) {
				if (und_ptr[j] != '1') {
					err = true;
					break;
				}
			}
			break;
		case MTE_ASYNC_ERR:
			/* Imprecise fault should occur otherwise return error */
			if (cur_mte_cxt.fault_valid == false) {
				err = true;
				break;
			}
			/*
			 * The imprecise fault is checked after the write to the buffer,
			 * so the underflow area before the fault should be filled.
			 */
			und_ptr = (char *) MT_CLEAR_TAG((size_t) ptr);
			for (j = last_index ; j < 0 ; j++) {
				if (und_ptr[j] != '1') {
					err = true;
					break;
				}
			}
			break;
		case MTE_SYNC_ERR:
			/* Precise fault should occur otherwise return error */
			if (!cur_mte_cxt.fault_valid || (last_index != (-1))) {
				err = true;
				break;
			}
			/* Underflow area should not be filled */
			und_ptr = (char *) MT_CLEAR_TAG((size_t) ptr);
			if (und_ptr[-1] == '1')
				err = true;
			break;
		default:
			err = true;
		break;
		}
check_buffer_underflow_by_byte_err:
		mte_free_memory_tag_range((void *)ptr, sizes[i], mem_type, underflow_range, 0);
		if (err)
			break;
	}
	return (err ? KSFT_FAIL : KSFT_PASS);
}

static int check_buffer_overflow_by_byte(int mem_type, int mode,
					  int overflow_range)
{
	char *ptr;
	int i, j, item, last_index;
	bool err;
	size_t tagged_size, overflow_size;
	char *over_ptr = NULL;

	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
	item = sizeof(sizes)/sizeof(int);
	for (i = 0; i < item; i++) {
		ptr = (char *)mte_allocate_memory_tag_range(sizes[i], mem_type, 0,
							    0, overflow_range);
		if (check_allocated_memory_range(ptr, sizes[i], mem_type,
						 0, overflow_range) != KSFT_PASS)
			return KSFT_FAIL;

		tagged_size = MT_ALIGN_UP(sizes[i]);

		mte_initialize_current_context(mode, (uintptr_t)ptr, sizes[i] + overflow_range);

		/* Set some value in tagged memory and make the buffer underflow */
		for (j = 0, last_index = 0 ; (j < (sizes[i] + overflow_range)) &&
					     (cur_mte_cxt.fault_valid == false); j++) {
			ptr[j] = '1';
			last_index = j;
		}
		mte_wait_after_trig();
		err = false;
		/* Check whether the buffer is filled */
		for (j = 0; j < sizes[i]; j++) {
			if (ptr[j] != '1') {
				err = true;
				ksft_print_msg("Buffer is not filled at index:%d of ptr:0x%lx\n",
						j, ptr);
				break;
			}
		}
		if (err)
			goto check_buffer_overflow_by_byte_err;

		overflow_size = overflow_range - (tagged_size - sizes[i]);

		switch (mode) {
		case MTE_NONE_ERR:
			if ((cur_mte_cxt.fault_valid == true) ||
			    (last_index != (sizes[i] + overflow_range - 1))) {
				err = true;
				break;
			}
			/* There were no fault so the overflow area should be filled */
			over_ptr = (char *) MT_CLEAR_TAG((size_t) ptr + tagged_size);
			for (j = 0 ; j < overflow_size; j++) {
				if (over_ptr[j] != '1') {
					err = true;
					break;
				}
			}
			break;
		case MTE_ASYNC_ERR:
			/* Imprecise fault should occur otherwise return error */
			if (cur_mte_cxt.fault_valid == false) {
				err = true;
				break;
			}
			/*
			 * The imprecise fault is checked after the write to the buffer,
			 * so the overflow area should be filled before the fault.
			 */
			over_ptr = (char *) MT_CLEAR_TAG((size_t) ptr);
			for (j = tagged_size ; j < last_index; j++) {
				if (over_ptr[j] != '1') {
					err = true;
					break;
				}
			}
			break;
		case MTE_SYNC_ERR:
			/* Precise fault should occur otherwise return error */
			if (!cur_mte_cxt.fault_valid || (last_index != tagged_size)) {
				err = true;
				break;
			}
			/* Underflow area should not be filled */
			over_ptr = (char *) MT_CLEAR_TAG((size_t) ptr + tagged_size);
			for (j = 0 ; j < overflow_size; j++) {
				if (over_ptr[j] == '1')
					err = true;
			}
			break;
		default:
			err = true;
		break;
		}
check_buffer_overflow_by_byte_err:
		mte_free_memory_tag_range((void *)ptr, sizes[i], mem_type, 0, overflow_range);
		if (err)
			break;
	}
	return (err ? KSFT_FAIL : KSFT_PASS);
}

static int check_buffer_by_block_iterate(int mem_type, int mode, size_t size)
{
	char *src, *dst;
	int j, result = KSFT_PASS;
	enum mte_block_test_alloc alloc_type = UNTAGGED_TAGGED;

	for (alloc_type = UNTAGGED_TAGGED; alloc_type < (int) BLOCK_ALLOC_MAX; alloc_type++) {
		switch (alloc_type) {
		case UNTAGGED_TAGGED:
			src = (char *)mte_allocate_memory(size, mem_type, 0, false);
			if (check_allocated_memory(src, size, mem_type, false) != KSFT_PASS)
				return KSFT_FAIL;

			dst = (char *)mte_allocate_memory(size, mem_type, 0, true);
			if (check_allocated_memory(dst, size, mem_type, true) != KSFT_PASS) {
				mte_free_memory((void *)src, size, mem_type, false);
				return KSFT_FAIL;
			}

			break;
		case TAGGED_UNTAGGED:
			dst = (char *)mte_allocate_memory(size, mem_type, 0, false);
			if (check_allocated_memory(dst, size, mem_type, false) != KSFT_PASS)
				return KSFT_FAIL;

			src = (char *)mte_allocate_memory(size, mem_type, 0, true);
			if (check_allocated_memory(src, size, mem_type, true) != KSFT_PASS) {
				mte_free_memory((void *)dst, size, mem_type, false);
				return KSFT_FAIL;
			}
			break;
		case TAGGED_TAGGED:
			src = (char *)mte_allocate_memory(size, mem_type, 0, true);
			if (check_allocated_memory(src, size, mem_type, true) != KSFT_PASS)
				return KSFT_FAIL;

			dst = (char *)mte_allocate_memory(size, mem_type, 0, true);
			if (check_allocated_memory(dst, size, mem_type, true) != KSFT_PASS) {
				mte_free_memory((void *)src, size, mem_type, true);
				return KSFT_FAIL;
			}
			break;
		default:
			return KSFT_FAIL;
		}

		cur_mte_cxt.fault_valid = false;
		result = KSFT_PASS;
		mte_initialize_current_context(mode, (uintptr_t)dst, size);
		/* Set some value in memory and copy*/
		memset((void *)src, (int)'1', size);
		memcpy((void *)dst, (void *)src, size);
		mte_wait_after_trig();
		if (cur_mte_cxt.fault_valid) {
			result = KSFT_FAIL;
			goto check_buffer_by_block_err;
		}
		/* Check the buffer whether it is filled. */
		for (j = 0; j < size; j++) {
			if (src[j] != dst[j] || src[j] != '1') {
				result = KSFT_FAIL;
				break;
			}
		}
check_buffer_by_block_err:
		mte_free_memory((void *)src, size, mem_type,
				MT_FETCH_TAG((uintptr_t)src) ? true : false);
		mte_free_memory((void *)dst, size, mem_type,
				MT_FETCH_TAG((uintptr_t)dst) ? true : false);
		if (result != KSFT_PASS)
			return result;
	}
	return result;
}

static int check_buffer_by_block(int mem_type, int mode)
{
	int i, item, result = KSFT_PASS;

	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
	item = sizeof(sizes)/sizeof(int);
	cur_mte_cxt.fault_valid = false;
	for (i = 0; i < item; i++) {
		result = check_buffer_by_block_iterate(mem_type, mode, sizes[i]);
		if (result != KSFT_PASS)
			break;
	}
	return result;
}

static int compare_memory_tags(char *ptr, size_t size, int tag)
{
	int i, new_tag;

	for (i = 0 ; i < size ; i += MT_GRANULE_SIZE) {
		new_tag = MT_FETCH_TAG((uintptr_t)(mte_get_tag_address(ptr + i)));
		if (tag != new_tag) {
			ksft_print_msg("FAIL: child mte tag mismatch\n");
			return KSFT_FAIL;
		}
	}
	return KSFT_PASS;
}

static int check_memory_initial_tags(int mem_type, int mode, int mapping)
{
	char *ptr;
	int run, fd;
	int total = sizeof(sizes)/sizeof(int);

	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
	for (run = 0; run < total; run++) {
		/* check initial tags for anonymous mmap */
		ptr = (char *)mte_allocate_memory(sizes[run], mem_type, mapping, false);
		if (check_allocated_memory(ptr, sizes[run], mem_type, false) != KSFT_PASS)
			return KSFT_FAIL;
		if (compare_memory_tags(ptr, sizes[run], 0) != KSFT_PASS) {
			mte_free_memory((void *)ptr, sizes[run], mem_type, false);
			return KSFT_FAIL;
		}
		mte_free_memory((void *)ptr, sizes[run], mem_type, false);

		/* check initial tags for file mmap */
		fd = create_temp_file();
		if (fd == -1)
			return KSFT_FAIL;
		ptr = (char *)mte_allocate_file_memory(sizes[run], mem_type, mapping, false, fd);
		if (check_allocated_memory(ptr, sizes[run], mem_type, false) != KSFT_PASS) {
			close(fd);
			return KSFT_FAIL;
		}
		if (compare_memory_tags(ptr, sizes[run], 0) != KSFT_PASS) {
			mte_free_memory((void *)ptr, sizes[run], mem_type, false);
			close(fd);
			return KSFT_FAIL;
		}
		mte_free_memory((void *)ptr, sizes[run], mem_type, false);
		close(fd);
	}
	return KSFT_PASS;
}

int main(int argc, char *argv[])
{
	int err;
	size_t page_size = getpagesize();
	int item = sizeof(sizes)/sizeof(int);

	sizes[item - 3] = page_size - 1;
	sizes[item - 2] = page_size;
	sizes[item - 1] = page_size + 1;

	err = mte_default_setup();
	if (err)
		return err;

	/* Register SIGSEGV handler */
	mte_register_signal(SIGSEGV, mte_default_handler);

	/* Buffer by byte tests */
	evaluate_test(check_buffer_by_byte(USE_MMAP, MTE_SYNC_ERR),
	"Check buffer correctness by byte with sync err mode and mmap memory\n");
	evaluate_test(check_buffer_by_byte(USE_MMAP, MTE_ASYNC_ERR),
	"Check buffer correctness by byte with async err mode and mmap memory\n");
	evaluate_test(check_buffer_by_byte(USE_MPROTECT, MTE_SYNC_ERR),
	"Check buffer correctness by byte with sync err mode and mmap/mprotect memory\n");
	evaluate_test(check_buffer_by_byte(USE_MPROTECT, MTE_ASYNC_ERR),
	"Check buffer correctness by byte with async err mode and mmap/mprotect memory\n");

	/* Check buffer underflow with underflow size as 16 */
	evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_SYNC_ERR, MT_GRANULE_SIZE),
	"Check buffer write underflow by byte with sync mode and mmap memory\n");
	evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_ASYNC_ERR, MT_GRANULE_SIZE),
	"Check buffer write underflow by byte with async mode and mmap memory\n");
	evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_NONE_ERR, MT_GRANULE_SIZE),
	"Check buffer write underflow by byte with tag check fault ignore and mmap memory\n");

	/* Check buffer underflow with underflow size as page size */
	evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_SYNC_ERR, page_size),
	"Check buffer write underflow by byte with sync mode and mmap memory\n");
	evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_ASYNC_ERR, page_size),
	"Check buffer write underflow by byte with async mode and mmap memory\n");
	evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_NONE_ERR, page_size),
	"Check buffer write underflow by byte with tag check fault ignore and mmap memory\n");

	/* Check buffer overflow with overflow size as 16 */
	evaluate_test(check_buffer_overflow_by_byte(USE_MMAP, MTE_SYNC_ERR, MT_GRANULE_SIZE),
	"Check buffer write overflow by byte with sync mode and mmap memory\n");
	evaluate_test(check_buffer_overflow_by_byte(USE_MMAP, MTE_ASYNC_ERR, MT_GRANULE_SIZE),
	"Check buffer write overflow by byte with async mode and mmap memory\n");
	evaluate_test(check_buffer_overflow_by_byte(USE_MMAP, MTE_NONE_ERR, MT_GRANULE_SIZE),
	"Check buffer write overflow by byte with tag fault ignore mode and mmap memory\n");

	/* Buffer by block tests */
	evaluate_test(check_buffer_by_block(USE_MMAP, MTE_SYNC_ERR),
	"Check buffer write correctness by block with sync mode and mmap memory\n");
	evaluate_test(check_buffer_by_block(USE_MMAP, MTE_ASYNC_ERR),
	"Check buffer write correctness by block with async mode and mmap memory\n");
	evaluate_test(check_buffer_by_block(USE_MMAP, MTE_NONE_ERR),
	"Check buffer write correctness by block with tag fault ignore and mmap memory\n");

	/* Initial tags are supposed to be 0 */
	evaluate_test(check_memory_initial_tags(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE),
	"Check initial tags with private mapping, sync error mode and mmap memory\n");
	evaluate_test(check_memory_initial_tags(USE_MPROTECT, MTE_SYNC_ERR, MAP_PRIVATE),
	"Check initial tags with private mapping, sync error mode and mmap/mprotect memory\n");
	evaluate_test(check_memory_initial_tags(USE_MMAP, MTE_SYNC_ERR, MAP_SHARED),
	"Check initial tags with shared mapping, sync error mode and mmap memory\n");
	evaluate_test(check_memory_initial_tags(USE_MPROTECT, MTE_SYNC_ERR, MAP_SHARED),
	"Check initial tags with shared mapping, sync error mode and mmap/mprotect memory\n");

	mte_restore_setup();
	ksft_print_cnts();
	return ksft_get_fail_cnt() == 0 ? KSFT_PASS : KSFT_FAIL;
}