1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2020 ARM Limited
10 #include "kselftest.h"
11 #include "mte_common_util.h"
14 #define OVERFLOW_RANGE MT_GRANULE_SIZE
16 static int sizes[] = {
17 1, 555, 1033, MT_GRANULE_SIZE - 1, MT_GRANULE_SIZE,
18 /* page size - 1*/ 0, /* page_size */ 0, /* page size + 1 */ 0
21 enum mte_block_test_alloc {
28 static int check_buffer_by_byte(int mem_type, int mode)
34 mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
35 item = sizeof(sizes)/sizeof(int);
37 for (i = 0; i < item; i++) {
38 ptr = (char *)mte_allocate_memory(sizes[i], mem_type, 0, true);
39 if (check_allocated_memory(ptr, sizes[i], mem_type, true) != KSFT_PASS)
41 mte_initialize_current_context(mode, (uintptr_t)ptr, sizes[i]);
42 /* Set some value in tagged memory */
43 for (j = 0; j < sizes[i]; j++)
45 mte_wait_after_trig();
46 err = cur_mte_cxt.fault_valid;
47 /* Check the buffer whether it is filled. */
48 for (j = 0; j < sizes[i] && !err; j++) {
52 mte_free_memory((void *)ptr, sizes[i], mem_type, true);
63 static int check_buffer_underflow_by_byte(int mem_type, int mode,
67 int i, j, item, last_index;
71 mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
72 item = sizeof(sizes)/sizeof(int);
73 for (i = 0; i < item; i++) {
74 ptr = (char *)mte_allocate_memory_tag_range(sizes[i], mem_type, 0,
76 if (check_allocated_memory_range(ptr, sizes[i], mem_type,
77 underflow_range, 0) != KSFT_PASS)
80 mte_initialize_current_context(mode, (uintptr_t)ptr, -underflow_range);
82 /* Set some value in tagged memory and make the buffer underflow */
83 for (j = sizes[i] - 1; (j >= -underflow_range) &&
84 (cur_mte_cxt.fault_valid == false); j--) {
88 mte_wait_after_trig();
90 /* Check whether the buffer is filled */
91 for (j = 0; j < sizes[i]; j++) {
94 ksft_print_msg("Buffer is not filled at index:%d of ptr:0x%lx\n",
100 goto check_buffer_underflow_by_byte_err;
104 if (cur_mte_cxt.fault_valid == true || last_index != -underflow_range) {
108 /* There were no fault so the underflow area should be filled */
109 und_ptr = (char *) MT_CLEAR_TAG((size_t) ptr - underflow_range);
110 for (j = 0 ; j < underflow_range; j++) {
111 if (und_ptr[j] != '1') {
118 /* Imprecise fault should occur otherwise return error */
119 if (cur_mte_cxt.fault_valid == false) {
124 * The imprecise fault is checked after the write to the buffer,
125 * so the underflow area before the fault should be filled.
127 und_ptr = (char *) MT_CLEAR_TAG((size_t) ptr);
128 for (j = last_index ; j < 0 ; j++) {
129 if (und_ptr[j] != '1') {
136 /* Precise fault should occur otherwise return error */
137 if (!cur_mte_cxt.fault_valid || (last_index != (-1))) {
141 /* Underflow area should not be filled */
142 und_ptr = (char *) MT_CLEAR_TAG((size_t) ptr);
143 if (und_ptr[-1] == '1')
150 check_buffer_underflow_by_byte_err:
151 mte_free_memory_tag_range((void *)ptr, sizes[i], mem_type, underflow_range, 0);
155 return (err ? KSFT_FAIL : KSFT_PASS);
158 static int check_buffer_overflow_by_byte(int mem_type, int mode,
162 int i, j, item, last_index;
164 size_t tagged_size, overflow_size;
165 char *over_ptr = NULL;
167 mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
168 item = sizeof(sizes)/sizeof(int);
169 for (i = 0; i < item; i++) {
170 ptr = (char *)mte_allocate_memory_tag_range(sizes[i], mem_type, 0,
172 if (check_allocated_memory_range(ptr, sizes[i], mem_type,
173 0, overflow_range) != KSFT_PASS)
176 tagged_size = MT_ALIGN_UP(sizes[i]);
178 mte_initialize_current_context(mode, (uintptr_t)ptr, sizes[i] + overflow_range);
180 /* Set some value in tagged memory and make the buffer underflow */
181 for (j = 0, last_index = 0 ; (j < (sizes[i] + overflow_range)) &&
182 (cur_mte_cxt.fault_valid == false); j++) {
186 mte_wait_after_trig();
188 /* Check whether the buffer is filled */
189 for (j = 0; j < sizes[i]; j++) {
192 ksft_print_msg("Buffer is not filled at index:%d of ptr:0x%lx\n",
198 goto check_buffer_overflow_by_byte_err;
200 overflow_size = overflow_range - (tagged_size - sizes[i]);
204 if ((cur_mte_cxt.fault_valid == true) ||
205 (last_index != (sizes[i] + overflow_range - 1))) {
209 /* There were no fault so the overflow area should be filled */
210 over_ptr = (char *) MT_CLEAR_TAG((size_t) ptr + tagged_size);
211 for (j = 0 ; j < overflow_size; j++) {
212 if (over_ptr[j] != '1') {
219 /* Imprecise fault should occur otherwise return error */
220 if (cur_mte_cxt.fault_valid == false) {
225 * The imprecise fault is checked after the write to the buffer,
226 * so the overflow area should be filled before the fault.
228 over_ptr = (char *) MT_CLEAR_TAG((size_t) ptr);
229 for (j = tagged_size ; j < last_index; j++) {
230 if (over_ptr[j] != '1') {
237 /* Precise fault should occur otherwise return error */
238 if (!cur_mte_cxt.fault_valid || (last_index != tagged_size)) {
242 /* Underflow area should not be filled */
243 over_ptr = (char *) MT_CLEAR_TAG((size_t) ptr + tagged_size);
244 for (j = 0 ; j < overflow_size; j++) {
245 if (over_ptr[j] == '1')
253 check_buffer_overflow_by_byte_err:
254 mte_free_memory_tag_range((void *)ptr, sizes[i], mem_type, 0, overflow_range);
258 return (err ? KSFT_FAIL : KSFT_PASS);
261 static int check_buffer_by_block_iterate(int mem_type, int mode, size_t size)
264 int j, result = KSFT_PASS;
265 enum mte_block_test_alloc alloc_type = UNTAGGED_TAGGED;
267 for (alloc_type = UNTAGGED_TAGGED; alloc_type < (int) BLOCK_ALLOC_MAX; alloc_type++) {
268 switch (alloc_type) {
269 case UNTAGGED_TAGGED:
270 src = (char *)mte_allocate_memory(size, mem_type, 0, false);
271 if (check_allocated_memory(src, size, mem_type, false) != KSFT_PASS)
274 dst = (char *)mte_allocate_memory(size, mem_type, 0, true);
275 if (check_allocated_memory(dst, size, mem_type, true) != KSFT_PASS) {
276 mte_free_memory((void *)src, size, mem_type, false);
281 case TAGGED_UNTAGGED:
282 dst = (char *)mte_allocate_memory(size, mem_type, 0, false);
283 if (check_allocated_memory(dst, size, mem_type, false) != KSFT_PASS)
286 src = (char *)mte_allocate_memory(size, mem_type, 0, true);
287 if (check_allocated_memory(src, size, mem_type, true) != KSFT_PASS) {
288 mte_free_memory((void *)dst, size, mem_type, false);
293 src = (char *)mte_allocate_memory(size, mem_type, 0, true);
294 if (check_allocated_memory(src, size, mem_type, true) != KSFT_PASS)
297 dst = (char *)mte_allocate_memory(size, mem_type, 0, true);
298 if (check_allocated_memory(dst, size, mem_type, true) != KSFT_PASS) {
299 mte_free_memory((void *)src, size, mem_type, true);
307 cur_mte_cxt.fault_valid = false;
309 mte_initialize_current_context(mode, (uintptr_t)dst, size);
310 /* Set some value in memory and copy*/
311 memset((void *)src, (int)'1', size);
312 memcpy((void *)dst, (void *)src, size);
313 mte_wait_after_trig();
314 if (cur_mte_cxt.fault_valid) {
316 goto check_buffer_by_block_err;
318 /* Check the buffer whether it is filled. */
319 for (j = 0; j < size; j++) {
320 if (src[j] != dst[j] || src[j] != '1') {
325 check_buffer_by_block_err:
326 mte_free_memory((void *)src, size, mem_type,
327 MT_FETCH_TAG((uintptr_t)src) ? true : false);
328 mte_free_memory((void *)dst, size, mem_type,
329 MT_FETCH_TAG((uintptr_t)dst) ? true : false);
330 if (result != KSFT_PASS)
336 static int check_buffer_by_block(int mem_type, int mode)
338 int i, item, result = KSFT_PASS;
340 mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
341 item = sizeof(sizes)/sizeof(int);
342 cur_mte_cxt.fault_valid = false;
343 for (i = 0; i < item; i++) {
344 result = check_buffer_by_block_iterate(mem_type, mode, sizes[i]);
345 if (result != KSFT_PASS)
351 static int compare_memory_tags(char *ptr, size_t size, int tag)
355 for (i = 0 ; i < size ; i += MT_GRANULE_SIZE) {
356 new_tag = MT_FETCH_TAG((uintptr_t)(mte_get_tag_address(ptr + i)));
357 if (tag != new_tag) {
358 ksft_print_msg("FAIL: child mte tag mismatch\n");
365 static int check_memory_initial_tags(int mem_type, int mode, int mapping)
369 int total = sizeof(sizes)/sizeof(int);
371 mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
372 for (run = 0; run < total; run++) {
373 /* check initial tags for anonymous mmap */
374 ptr = (char *)mte_allocate_memory(sizes[run], mem_type, mapping, false);
375 if (check_allocated_memory(ptr, sizes[run], mem_type, false) != KSFT_PASS)
377 if (compare_memory_tags(ptr, sizes[run], 0) != KSFT_PASS) {
378 mte_free_memory((void *)ptr, sizes[run], mem_type, false);
381 mte_free_memory((void *)ptr, sizes[run], mem_type, false);
383 /* check initial tags for file mmap */
384 fd = create_temp_file();
387 ptr = (char *)mte_allocate_file_memory(sizes[run], mem_type, mapping, false, fd);
388 if (check_allocated_memory(ptr, sizes[run], mem_type, false) != KSFT_PASS) {
392 if (compare_memory_tags(ptr, sizes[run], 0) != KSFT_PASS) {
393 mte_free_memory((void *)ptr, sizes[run], mem_type, false);
397 mte_free_memory((void *)ptr, sizes[run], mem_type, false);
403 int main(int argc, char *argv[])
406 size_t page_size = getpagesize();
407 int item = sizeof(sizes)/sizeof(int);
409 sizes[item - 3] = page_size - 1;
410 sizes[item - 2] = page_size;
411 sizes[item - 1] = page_size + 1;
413 err = mte_default_setup();
417 /* Register SIGSEGV handler */
418 mte_register_signal(SIGSEGV, mte_default_handler);
420 /* Buffer by byte tests */
421 evaluate_test(check_buffer_by_byte(USE_MMAP, MTE_SYNC_ERR),
422 "Check buffer correctness by byte with sync err mode and mmap memory\n");
423 evaluate_test(check_buffer_by_byte(USE_MMAP, MTE_ASYNC_ERR),
424 "Check buffer correctness by byte with async err mode and mmap memory\n");
425 evaluate_test(check_buffer_by_byte(USE_MPROTECT, MTE_SYNC_ERR),
426 "Check buffer correctness by byte with sync err mode and mmap/mprotect memory\n");
427 evaluate_test(check_buffer_by_byte(USE_MPROTECT, MTE_ASYNC_ERR),
428 "Check buffer correctness by byte with async err mode and mmap/mprotect memory\n");
430 /* Check buffer underflow with underflow size as 16 */
431 evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_SYNC_ERR, MT_GRANULE_SIZE),
432 "Check buffer write underflow by byte with sync mode and mmap memory\n");
433 evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_ASYNC_ERR, MT_GRANULE_SIZE),
434 "Check buffer write underflow by byte with async mode and mmap memory\n");
435 evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_NONE_ERR, MT_GRANULE_SIZE),
436 "Check buffer write underflow by byte with tag check fault ignore and mmap memory\n");
438 /* Check buffer underflow with underflow size as page size */
439 evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_SYNC_ERR, page_size),
440 "Check buffer write underflow by byte with sync mode and mmap memory\n");
441 evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_ASYNC_ERR, page_size),
442 "Check buffer write underflow by byte with async mode and mmap memory\n");
443 evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_NONE_ERR, page_size),
444 "Check buffer write underflow by byte with tag check fault ignore and mmap memory\n");
446 /* Check buffer overflow with overflow size as 16 */
447 evaluate_test(check_buffer_overflow_by_byte(USE_MMAP, MTE_SYNC_ERR, MT_GRANULE_SIZE),
448 "Check buffer write overflow by byte with sync mode and mmap memory\n");
449 evaluate_test(check_buffer_overflow_by_byte(USE_MMAP, MTE_ASYNC_ERR, MT_GRANULE_SIZE),
450 "Check buffer write overflow by byte with async mode and mmap memory\n");
451 evaluate_test(check_buffer_overflow_by_byte(USE_MMAP, MTE_NONE_ERR, MT_GRANULE_SIZE),
452 "Check buffer write overflow by byte with tag fault ignore mode and mmap memory\n");
454 /* Buffer by block tests */
455 evaluate_test(check_buffer_by_block(USE_MMAP, MTE_SYNC_ERR),
456 "Check buffer write correctness by block with sync mode and mmap memory\n");
457 evaluate_test(check_buffer_by_block(USE_MMAP, MTE_ASYNC_ERR),
458 "Check buffer write correctness by block with async mode and mmap memory\n");
459 evaluate_test(check_buffer_by_block(USE_MMAP, MTE_NONE_ERR),
460 "Check buffer write correctness by block with tag fault ignore and mmap memory\n");
462 /* Initial tags are supposed to be 0 */
463 evaluate_test(check_memory_initial_tags(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE),
464 "Check initial tags with private mapping, sync error mode and mmap memory\n");
465 evaluate_test(check_memory_initial_tags(USE_MPROTECT, MTE_SYNC_ERR, MAP_PRIVATE),
466 "Check initial tags with private mapping, sync error mode and mmap/mprotect memory\n");
467 evaluate_test(check_memory_initial_tags(USE_MMAP, MTE_SYNC_ERR, MAP_SHARED),
468 "Check initial tags with shared mapping, sync error mode and mmap memory\n");
469 evaluate_test(check_memory_initial_tags(USE_MPROTECT, MTE_SYNC_ERR, MAP_SHARED),
470 "Check initial tags with shared mapping, sync error mode and mmap/mprotect memory\n");
474 return ksft_get_fail_cnt() == 0 ? KSFT_PASS : KSFT_FAIL;