1e9b60476SAmit Daniel Kachhap // SPDX-License-Identifier: GPL-2.0
2e9b60476SAmit Daniel Kachhap // Copyright (C) 2020 ARM Limited
3e9b60476SAmit Daniel Kachhap
4e9b60476SAmit Daniel Kachhap #define _GNU_SOURCE
5e9b60476SAmit Daniel Kachhap
6e9b60476SAmit Daniel Kachhap #include <stddef.h>
7e9b60476SAmit Daniel Kachhap #include <stdio.h>
8e9b60476SAmit Daniel Kachhap #include <string.h>
9e9b60476SAmit Daniel Kachhap
10e9b60476SAmit Daniel Kachhap #include "kselftest.h"
11e9b60476SAmit Daniel Kachhap #include "mte_common_util.h"
12e9b60476SAmit Daniel Kachhap #include "mte_def.h"
13e9b60476SAmit Daniel Kachhap
14e9b60476SAmit Daniel Kachhap #define OVERFLOW_RANGE MT_GRANULE_SIZE
15e9b60476SAmit Daniel Kachhap
16e9b60476SAmit Daniel Kachhap static int sizes[] = {
17e9b60476SAmit Daniel Kachhap 1, 555, 1033, MT_GRANULE_SIZE - 1, MT_GRANULE_SIZE,
18e9b60476SAmit Daniel Kachhap /* page size - 1*/ 0, /* page_size */ 0, /* page size + 1 */ 0
19e9b60476SAmit Daniel Kachhap };
20e9b60476SAmit Daniel Kachhap
21e9b60476SAmit Daniel Kachhap enum mte_block_test_alloc {
22e9b60476SAmit Daniel Kachhap UNTAGGED_TAGGED,
23e9b60476SAmit Daniel Kachhap TAGGED_UNTAGGED,
24e9b60476SAmit Daniel Kachhap TAGGED_TAGGED,
25e9b60476SAmit Daniel Kachhap BLOCK_ALLOC_MAX,
26e9b60476SAmit Daniel Kachhap };
27e9b60476SAmit Daniel Kachhap
check_buffer_by_byte(int mem_type,int mode)28e9b60476SAmit Daniel Kachhap static int check_buffer_by_byte(int mem_type, int mode)
29e9b60476SAmit Daniel Kachhap {
30e9b60476SAmit Daniel Kachhap char *ptr;
31e9b60476SAmit Daniel Kachhap int i, j, item;
32e9b60476SAmit Daniel Kachhap bool err;
33e9b60476SAmit Daniel Kachhap
34e9b60476SAmit Daniel Kachhap mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
35*a0fa0b63SKang Minchul item = ARRAY_SIZE(sizes);
36e9b60476SAmit Daniel Kachhap
37e9b60476SAmit Daniel Kachhap for (i = 0; i < item; i++) {
38e9b60476SAmit Daniel Kachhap ptr = (char *)mte_allocate_memory(sizes[i], mem_type, 0, true);
39e9b60476SAmit Daniel Kachhap if (check_allocated_memory(ptr, sizes[i], mem_type, true) != KSFT_PASS)
40e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
41e9b60476SAmit Daniel Kachhap mte_initialize_current_context(mode, (uintptr_t)ptr, sizes[i]);
42e9b60476SAmit Daniel Kachhap /* Set some value in tagged memory */
43e9b60476SAmit Daniel Kachhap for (j = 0; j < sizes[i]; j++)
44e9b60476SAmit Daniel Kachhap ptr[j] = '1';
45e9b60476SAmit Daniel Kachhap mte_wait_after_trig();
46e9b60476SAmit Daniel Kachhap err = cur_mte_cxt.fault_valid;
47e9b60476SAmit Daniel Kachhap /* Check the buffer whether it is filled. */
48e9b60476SAmit Daniel Kachhap for (j = 0; j < sizes[i] && !err; j++) {
49e9b60476SAmit Daniel Kachhap if (ptr[j] != '1')
50e9b60476SAmit Daniel Kachhap err = true;
51e9b60476SAmit Daniel Kachhap }
52e9b60476SAmit Daniel Kachhap mte_free_memory((void *)ptr, sizes[i], mem_type, true);
53e9b60476SAmit Daniel Kachhap
54e9b60476SAmit Daniel Kachhap if (err)
55e9b60476SAmit Daniel Kachhap break;
56e9b60476SAmit Daniel Kachhap }
57e9b60476SAmit Daniel Kachhap if (!err)
58e9b60476SAmit Daniel Kachhap return KSFT_PASS;
59e9b60476SAmit Daniel Kachhap else
60e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
61e9b60476SAmit Daniel Kachhap }
62e9b60476SAmit Daniel Kachhap
check_buffer_underflow_by_byte(int mem_type,int mode,int underflow_range)63e9b60476SAmit Daniel Kachhap static int check_buffer_underflow_by_byte(int mem_type, int mode,
64e9b60476SAmit Daniel Kachhap int underflow_range)
65e9b60476SAmit Daniel Kachhap {
66e9b60476SAmit Daniel Kachhap char *ptr;
67e9b60476SAmit Daniel Kachhap int i, j, item, last_index;
68e9b60476SAmit Daniel Kachhap bool err;
69e9b60476SAmit Daniel Kachhap char *und_ptr = NULL;
70e9b60476SAmit Daniel Kachhap
71e9b60476SAmit Daniel Kachhap mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
72*a0fa0b63SKang Minchul item = ARRAY_SIZE(sizes);
73e9b60476SAmit Daniel Kachhap for (i = 0; i < item; i++) {
74e9b60476SAmit Daniel Kachhap ptr = (char *)mte_allocate_memory_tag_range(sizes[i], mem_type, 0,
75e9b60476SAmit Daniel Kachhap underflow_range, 0);
76e9b60476SAmit Daniel Kachhap if (check_allocated_memory_range(ptr, sizes[i], mem_type,
77e9b60476SAmit Daniel Kachhap underflow_range, 0) != KSFT_PASS)
78e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
79e9b60476SAmit Daniel Kachhap
80e9b60476SAmit Daniel Kachhap mte_initialize_current_context(mode, (uintptr_t)ptr, -underflow_range);
81e9b60476SAmit Daniel Kachhap last_index = 0;
82e9b60476SAmit Daniel Kachhap /* Set some value in tagged memory and make the buffer underflow */
83e9b60476SAmit Daniel Kachhap for (j = sizes[i] - 1; (j >= -underflow_range) &&
84dd313a26SYANG LI (!cur_mte_cxt.fault_valid); j--) {
85e9b60476SAmit Daniel Kachhap ptr[j] = '1';
86e9b60476SAmit Daniel Kachhap last_index = j;
87e9b60476SAmit Daniel Kachhap }
88e9b60476SAmit Daniel Kachhap mte_wait_after_trig();
89e9b60476SAmit Daniel Kachhap err = false;
90e9b60476SAmit Daniel Kachhap /* Check whether the buffer is filled */
91e9b60476SAmit Daniel Kachhap for (j = 0; j < sizes[i]; j++) {
92e9b60476SAmit Daniel Kachhap if (ptr[j] != '1') {
93e9b60476SAmit Daniel Kachhap err = true;
94e9b60476SAmit Daniel Kachhap ksft_print_msg("Buffer is not filled at index:%d of ptr:0x%lx\n",
95e9b60476SAmit Daniel Kachhap j, ptr);
96e9b60476SAmit Daniel Kachhap break;
97e9b60476SAmit Daniel Kachhap }
98e9b60476SAmit Daniel Kachhap }
99e9b60476SAmit Daniel Kachhap if (err)
100e9b60476SAmit Daniel Kachhap goto check_buffer_underflow_by_byte_err;
101e9b60476SAmit Daniel Kachhap
102e9b60476SAmit Daniel Kachhap switch (mode) {
103e9b60476SAmit Daniel Kachhap case MTE_NONE_ERR:
104e9b60476SAmit Daniel Kachhap if (cur_mte_cxt.fault_valid == true || last_index != -underflow_range) {
105e9b60476SAmit Daniel Kachhap err = true;
106e9b60476SAmit Daniel Kachhap break;
107e9b60476SAmit Daniel Kachhap }
108e9b60476SAmit Daniel Kachhap /* There were no fault so the underflow area should be filled */
109e9b60476SAmit Daniel Kachhap und_ptr = (char *) MT_CLEAR_TAG((size_t) ptr - underflow_range);
110e9b60476SAmit Daniel Kachhap for (j = 0 ; j < underflow_range; j++) {
111e9b60476SAmit Daniel Kachhap if (und_ptr[j] != '1') {
112e9b60476SAmit Daniel Kachhap err = true;
113e9b60476SAmit Daniel Kachhap break;
114e9b60476SAmit Daniel Kachhap }
115e9b60476SAmit Daniel Kachhap }
116e9b60476SAmit Daniel Kachhap break;
117e9b60476SAmit Daniel Kachhap case MTE_ASYNC_ERR:
118e9b60476SAmit Daniel Kachhap /* Imprecise fault should occur otherwise return error */
119e9b60476SAmit Daniel Kachhap if (cur_mte_cxt.fault_valid == false) {
120e9b60476SAmit Daniel Kachhap err = true;
121e9b60476SAmit Daniel Kachhap break;
122e9b60476SAmit Daniel Kachhap }
123e9b60476SAmit Daniel Kachhap /*
124e9b60476SAmit Daniel Kachhap * The imprecise fault is checked after the write to the buffer,
125e9b60476SAmit Daniel Kachhap * so the underflow area before the fault should be filled.
126e9b60476SAmit Daniel Kachhap */
127e9b60476SAmit Daniel Kachhap und_ptr = (char *) MT_CLEAR_TAG((size_t) ptr);
128e9b60476SAmit Daniel Kachhap for (j = last_index ; j < 0 ; j++) {
129e9b60476SAmit Daniel Kachhap if (und_ptr[j] != '1') {
130e9b60476SAmit Daniel Kachhap err = true;
131e9b60476SAmit Daniel Kachhap break;
132e9b60476SAmit Daniel Kachhap }
133e9b60476SAmit Daniel Kachhap }
134e9b60476SAmit Daniel Kachhap break;
135e9b60476SAmit Daniel Kachhap case MTE_SYNC_ERR:
136e9b60476SAmit Daniel Kachhap /* Precise fault should occur otherwise return error */
137e9b60476SAmit Daniel Kachhap if (!cur_mte_cxt.fault_valid || (last_index != (-1))) {
138e9b60476SAmit Daniel Kachhap err = true;
139e9b60476SAmit Daniel Kachhap break;
140e9b60476SAmit Daniel Kachhap }
141e9b60476SAmit Daniel Kachhap /* Underflow area should not be filled */
142e9b60476SAmit Daniel Kachhap und_ptr = (char *) MT_CLEAR_TAG((size_t) ptr);
143e9b60476SAmit Daniel Kachhap if (und_ptr[-1] == '1')
144e9b60476SAmit Daniel Kachhap err = true;
145e9b60476SAmit Daniel Kachhap break;
146e9b60476SAmit Daniel Kachhap default:
147e9b60476SAmit Daniel Kachhap err = true;
148e9b60476SAmit Daniel Kachhap break;
149e9b60476SAmit Daniel Kachhap }
150e9b60476SAmit Daniel Kachhap check_buffer_underflow_by_byte_err:
151e9b60476SAmit Daniel Kachhap mte_free_memory_tag_range((void *)ptr, sizes[i], mem_type, underflow_range, 0);
152e9b60476SAmit Daniel Kachhap if (err)
153e9b60476SAmit Daniel Kachhap break;
154e9b60476SAmit Daniel Kachhap }
155e9b60476SAmit Daniel Kachhap return (err ? KSFT_FAIL : KSFT_PASS);
156e9b60476SAmit Daniel Kachhap }
157e9b60476SAmit Daniel Kachhap
check_buffer_overflow_by_byte(int mem_type,int mode,int overflow_range)158e9b60476SAmit Daniel Kachhap static int check_buffer_overflow_by_byte(int mem_type, int mode,
159e9b60476SAmit Daniel Kachhap int overflow_range)
160e9b60476SAmit Daniel Kachhap {
161e9b60476SAmit Daniel Kachhap char *ptr;
162e9b60476SAmit Daniel Kachhap int i, j, item, last_index;
163e9b60476SAmit Daniel Kachhap bool err;
164e9b60476SAmit Daniel Kachhap size_t tagged_size, overflow_size;
165e9b60476SAmit Daniel Kachhap char *over_ptr = NULL;
166e9b60476SAmit Daniel Kachhap
167e9b60476SAmit Daniel Kachhap mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
168*a0fa0b63SKang Minchul item = ARRAY_SIZE(sizes);
169e9b60476SAmit Daniel Kachhap for (i = 0; i < item; i++) {
170e9b60476SAmit Daniel Kachhap ptr = (char *)mte_allocate_memory_tag_range(sizes[i], mem_type, 0,
171e9b60476SAmit Daniel Kachhap 0, overflow_range);
172e9b60476SAmit Daniel Kachhap if (check_allocated_memory_range(ptr, sizes[i], mem_type,
173e9b60476SAmit Daniel Kachhap 0, overflow_range) != KSFT_PASS)
174e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
175e9b60476SAmit Daniel Kachhap
176e9b60476SAmit Daniel Kachhap tagged_size = MT_ALIGN_UP(sizes[i]);
177e9b60476SAmit Daniel Kachhap
178e9b60476SAmit Daniel Kachhap mte_initialize_current_context(mode, (uintptr_t)ptr, sizes[i] + overflow_range);
179e9b60476SAmit Daniel Kachhap
180e9b60476SAmit Daniel Kachhap /* Set some value in tagged memory and make the buffer underflow */
181e9b60476SAmit Daniel Kachhap for (j = 0, last_index = 0 ; (j < (sizes[i] + overflow_range)) &&
182e9b60476SAmit Daniel Kachhap (cur_mte_cxt.fault_valid == false); j++) {
183e9b60476SAmit Daniel Kachhap ptr[j] = '1';
184e9b60476SAmit Daniel Kachhap last_index = j;
185e9b60476SAmit Daniel Kachhap }
186e9b60476SAmit Daniel Kachhap mte_wait_after_trig();
187e9b60476SAmit Daniel Kachhap err = false;
188e9b60476SAmit Daniel Kachhap /* Check whether the buffer is filled */
189e9b60476SAmit Daniel Kachhap for (j = 0; j < sizes[i]; j++) {
190e9b60476SAmit Daniel Kachhap if (ptr[j] != '1') {
191e9b60476SAmit Daniel Kachhap err = true;
192e9b60476SAmit Daniel Kachhap ksft_print_msg("Buffer is not filled at index:%d of ptr:0x%lx\n",
193e9b60476SAmit Daniel Kachhap j, ptr);
194e9b60476SAmit Daniel Kachhap break;
195e9b60476SAmit Daniel Kachhap }
196e9b60476SAmit Daniel Kachhap }
197e9b60476SAmit Daniel Kachhap if (err)
198e9b60476SAmit Daniel Kachhap goto check_buffer_overflow_by_byte_err;
199e9b60476SAmit Daniel Kachhap
200e9b60476SAmit Daniel Kachhap overflow_size = overflow_range - (tagged_size - sizes[i]);
201e9b60476SAmit Daniel Kachhap
202e9b60476SAmit Daniel Kachhap switch (mode) {
203e9b60476SAmit Daniel Kachhap case MTE_NONE_ERR:
204e9b60476SAmit Daniel Kachhap if ((cur_mte_cxt.fault_valid == true) ||
205e9b60476SAmit Daniel Kachhap (last_index != (sizes[i] + overflow_range - 1))) {
206e9b60476SAmit Daniel Kachhap err = true;
207e9b60476SAmit Daniel Kachhap break;
208e9b60476SAmit Daniel Kachhap }
209e9b60476SAmit Daniel Kachhap /* There were no fault so the overflow area should be filled */
210e9b60476SAmit Daniel Kachhap over_ptr = (char *) MT_CLEAR_TAG((size_t) ptr + tagged_size);
211e9b60476SAmit Daniel Kachhap for (j = 0 ; j < overflow_size; j++) {
212e9b60476SAmit Daniel Kachhap if (over_ptr[j] != '1') {
213e9b60476SAmit Daniel Kachhap err = true;
214e9b60476SAmit Daniel Kachhap break;
215e9b60476SAmit Daniel Kachhap }
216e9b60476SAmit Daniel Kachhap }
217e9b60476SAmit Daniel Kachhap break;
218e9b60476SAmit Daniel Kachhap case MTE_ASYNC_ERR:
219e9b60476SAmit Daniel Kachhap /* Imprecise fault should occur otherwise return error */
220e9b60476SAmit Daniel Kachhap if (cur_mte_cxt.fault_valid == false) {
221e9b60476SAmit Daniel Kachhap err = true;
222e9b60476SAmit Daniel Kachhap break;
223e9b60476SAmit Daniel Kachhap }
224e9b60476SAmit Daniel Kachhap /*
225e9b60476SAmit Daniel Kachhap * The imprecise fault is checked after the write to the buffer,
226e9b60476SAmit Daniel Kachhap * so the overflow area should be filled before the fault.
227e9b60476SAmit Daniel Kachhap */
228e9b60476SAmit Daniel Kachhap over_ptr = (char *) MT_CLEAR_TAG((size_t) ptr);
229e9b60476SAmit Daniel Kachhap for (j = tagged_size ; j < last_index; j++) {
230e9b60476SAmit Daniel Kachhap if (over_ptr[j] != '1') {
231e9b60476SAmit Daniel Kachhap err = true;
232e9b60476SAmit Daniel Kachhap break;
233e9b60476SAmit Daniel Kachhap }
234e9b60476SAmit Daniel Kachhap }
235e9b60476SAmit Daniel Kachhap break;
236e9b60476SAmit Daniel Kachhap case MTE_SYNC_ERR:
237e9b60476SAmit Daniel Kachhap /* Precise fault should occur otherwise return error */
238e9b60476SAmit Daniel Kachhap if (!cur_mte_cxt.fault_valid || (last_index != tagged_size)) {
239e9b60476SAmit Daniel Kachhap err = true;
240e9b60476SAmit Daniel Kachhap break;
241e9b60476SAmit Daniel Kachhap }
242e9b60476SAmit Daniel Kachhap /* Underflow area should not be filled */
243e9b60476SAmit Daniel Kachhap over_ptr = (char *) MT_CLEAR_TAG((size_t) ptr + tagged_size);
244e9b60476SAmit Daniel Kachhap for (j = 0 ; j < overflow_size; j++) {
245e9b60476SAmit Daniel Kachhap if (over_ptr[j] == '1')
246e9b60476SAmit Daniel Kachhap err = true;
247e9b60476SAmit Daniel Kachhap }
248e9b60476SAmit Daniel Kachhap break;
249e9b60476SAmit Daniel Kachhap default:
250e9b60476SAmit Daniel Kachhap err = true;
251e9b60476SAmit Daniel Kachhap break;
252e9b60476SAmit Daniel Kachhap }
253e9b60476SAmit Daniel Kachhap check_buffer_overflow_by_byte_err:
254e9b60476SAmit Daniel Kachhap mte_free_memory_tag_range((void *)ptr, sizes[i], mem_type, 0, overflow_range);
255e9b60476SAmit Daniel Kachhap if (err)
256e9b60476SAmit Daniel Kachhap break;
257e9b60476SAmit Daniel Kachhap }
258e9b60476SAmit Daniel Kachhap return (err ? KSFT_FAIL : KSFT_PASS);
259e9b60476SAmit Daniel Kachhap }
260e9b60476SAmit Daniel Kachhap
check_buffer_by_block_iterate(int mem_type,int mode,size_t size)261e9b60476SAmit Daniel Kachhap static int check_buffer_by_block_iterate(int mem_type, int mode, size_t size)
262e9b60476SAmit Daniel Kachhap {
263e9b60476SAmit Daniel Kachhap char *src, *dst;
264e9b60476SAmit Daniel Kachhap int j, result = KSFT_PASS;
265e9b60476SAmit Daniel Kachhap enum mte_block_test_alloc alloc_type = UNTAGGED_TAGGED;
266e9b60476SAmit Daniel Kachhap
267e9b60476SAmit Daniel Kachhap for (alloc_type = UNTAGGED_TAGGED; alloc_type < (int) BLOCK_ALLOC_MAX; alloc_type++) {
268e9b60476SAmit Daniel Kachhap switch (alloc_type) {
269e9b60476SAmit Daniel Kachhap case UNTAGGED_TAGGED:
270e9b60476SAmit Daniel Kachhap src = (char *)mte_allocate_memory(size, mem_type, 0, false);
271e9b60476SAmit Daniel Kachhap if (check_allocated_memory(src, size, mem_type, false) != KSFT_PASS)
272e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
273e9b60476SAmit Daniel Kachhap
274e9b60476SAmit Daniel Kachhap dst = (char *)mte_allocate_memory(size, mem_type, 0, true);
275e9b60476SAmit Daniel Kachhap if (check_allocated_memory(dst, size, mem_type, true) != KSFT_PASS) {
276e9b60476SAmit Daniel Kachhap mte_free_memory((void *)src, size, mem_type, false);
277e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
278e9b60476SAmit Daniel Kachhap }
279e9b60476SAmit Daniel Kachhap
280e9b60476SAmit Daniel Kachhap break;
281e9b60476SAmit Daniel Kachhap case TAGGED_UNTAGGED:
282e9b60476SAmit Daniel Kachhap dst = (char *)mte_allocate_memory(size, mem_type, 0, false);
283e9b60476SAmit Daniel Kachhap if (check_allocated_memory(dst, size, mem_type, false) != KSFT_PASS)
284e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
285e9b60476SAmit Daniel Kachhap
286e9b60476SAmit Daniel Kachhap src = (char *)mte_allocate_memory(size, mem_type, 0, true);
287e9b60476SAmit Daniel Kachhap if (check_allocated_memory(src, size, mem_type, true) != KSFT_PASS) {
288e9b60476SAmit Daniel Kachhap mte_free_memory((void *)dst, size, mem_type, false);
289e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
290e9b60476SAmit Daniel Kachhap }
291e9b60476SAmit Daniel Kachhap break;
292e9b60476SAmit Daniel Kachhap case TAGGED_TAGGED:
293e9b60476SAmit Daniel Kachhap src = (char *)mte_allocate_memory(size, mem_type, 0, true);
294e9b60476SAmit Daniel Kachhap if (check_allocated_memory(src, size, mem_type, true) != KSFT_PASS)
295e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
296e9b60476SAmit Daniel Kachhap
297e9b60476SAmit Daniel Kachhap dst = (char *)mte_allocate_memory(size, mem_type, 0, true);
298e9b60476SAmit Daniel Kachhap if (check_allocated_memory(dst, size, mem_type, true) != KSFT_PASS) {
299e9b60476SAmit Daniel Kachhap mte_free_memory((void *)src, size, mem_type, true);
300e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
301e9b60476SAmit Daniel Kachhap }
302e9b60476SAmit Daniel Kachhap break;
303e9b60476SAmit Daniel Kachhap default:
304e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
305e9b60476SAmit Daniel Kachhap }
306e9b60476SAmit Daniel Kachhap
307e9b60476SAmit Daniel Kachhap cur_mte_cxt.fault_valid = false;
308e9b60476SAmit Daniel Kachhap result = KSFT_PASS;
309e9b60476SAmit Daniel Kachhap mte_initialize_current_context(mode, (uintptr_t)dst, size);
310e9b60476SAmit Daniel Kachhap /* Set some value in memory and copy*/
311e9b60476SAmit Daniel Kachhap memset((void *)src, (int)'1', size);
312e9b60476SAmit Daniel Kachhap memcpy((void *)dst, (void *)src, size);
313e9b60476SAmit Daniel Kachhap mte_wait_after_trig();
314e9b60476SAmit Daniel Kachhap if (cur_mte_cxt.fault_valid) {
315e9b60476SAmit Daniel Kachhap result = KSFT_FAIL;
316e9b60476SAmit Daniel Kachhap goto check_buffer_by_block_err;
317e9b60476SAmit Daniel Kachhap }
318e9b60476SAmit Daniel Kachhap /* Check the buffer whether it is filled. */
319e9b60476SAmit Daniel Kachhap for (j = 0; j < size; j++) {
320e9b60476SAmit Daniel Kachhap if (src[j] != dst[j] || src[j] != '1') {
321e9b60476SAmit Daniel Kachhap result = KSFT_FAIL;
322e9b60476SAmit Daniel Kachhap break;
323e9b60476SAmit Daniel Kachhap }
324e9b60476SAmit Daniel Kachhap }
325e9b60476SAmit Daniel Kachhap check_buffer_by_block_err:
326e9b60476SAmit Daniel Kachhap mte_free_memory((void *)src, size, mem_type,
327e9b60476SAmit Daniel Kachhap MT_FETCH_TAG((uintptr_t)src) ? true : false);
328e9b60476SAmit Daniel Kachhap mte_free_memory((void *)dst, size, mem_type,
329e9b60476SAmit Daniel Kachhap MT_FETCH_TAG((uintptr_t)dst) ? true : false);
330e9b60476SAmit Daniel Kachhap if (result != KSFT_PASS)
331e9b60476SAmit Daniel Kachhap return result;
332e9b60476SAmit Daniel Kachhap }
333e9b60476SAmit Daniel Kachhap return result;
334e9b60476SAmit Daniel Kachhap }
335e9b60476SAmit Daniel Kachhap
check_buffer_by_block(int mem_type,int mode)336e9b60476SAmit Daniel Kachhap static int check_buffer_by_block(int mem_type, int mode)
337e9b60476SAmit Daniel Kachhap {
338e9b60476SAmit Daniel Kachhap int i, item, result = KSFT_PASS;
339e9b60476SAmit Daniel Kachhap
340e9b60476SAmit Daniel Kachhap mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
341*a0fa0b63SKang Minchul item = ARRAY_SIZE(sizes);
342e9b60476SAmit Daniel Kachhap cur_mte_cxt.fault_valid = false;
343e9b60476SAmit Daniel Kachhap for (i = 0; i < item; i++) {
344e9b60476SAmit Daniel Kachhap result = check_buffer_by_block_iterate(mem_type, mode, sizes[i]);
345e9b60476SAmit Daniel Kachhap if (result != KSFT_PASS)
346e9b60476SAmit Daniel Kachhap break;
347e9b60476SAmit Daniel Kachhap }
348e9b60476SAmit Daniel Kachhap return result;
349e9b60476SAmit Daniel Kachhap }
350e9b60476SAmit Daniel Kachhap
compare_memory_tags(char * ptr,size_t size,int tag)351e9b60476SAmit Daniel Kachhap static int compare_memory_tags(char *ptr, size_t size, int tag)
352e9b60476SAmit Daniel Kachhap {
353e9b60476SAmit Daniel Kachhap int i, new_tag;
354e9b60476SAmit Daniel Kachhap
355e9b60476SAmit Daniel Kachhap for (i = 0 ; i < size ; i += MT_GRANULE_SIZE) {
356e9b60476SAmit Daniel Kachhap new_tag = MT_FETCH_TAG((uintptr_t)(mte_get_tag_address(ptr + i)));
357e9b60476SAmit Daniel Kachhap if (tag != new_tag) {
358e9b60476SAmit Daniel Kachhap ksft_print_msg("FAIL: child mte tag mismatch\n");
359e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
360e9b60476SAmit Daniel Kachhap }
361e9b60476SAmit Daniel Kachhap }
362e9b60476SAmit Daniel Kachhap return KSFT_PASS;
363e9b60476SAmit Daniel Kachhap }
364e9b60476SAmit Daniel Kachhap
check_memory_initial_tags(int mem_type,int mode,int mapping)365e9b60476SAmit Daniel Kachhap static int check_memory_initial_tags(int mem_type, int mode, int mapping)
366e9b60476SAmit Daniel Kachhap {
367e9b60476SAmit Daniel Kachhap char *ptr;
368e9b60476SAmit Daniel Kachhap int run, fd;
369*a0fa0b63SKang Minchul int total = ARRAY_SIZE(sizes);
370e9b60476SAmit Daniel Kachhap
371e9b60476SAmit Daniel Kachhap mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG);
372e9b60476SAmit Daniel Kachhap for (run = 0; run < total; run++) {
373e9b60476SAmit Daniel Kachhap /* check initial tags for anonymous mmap */
374e9b60476SAmit Daniel Kachhap ptr = (char *)mte_allocate_memory(sizes[run], mem_type, mapping, false);
375e9b60476SAmit Daniel Kachhap if (check_allocated_memory(ptr, sizes[run], mem_type, false) != KSFT_PASS)
376e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
377e9b60476SAmit Daniel Kachhap if (compare_memory_tags(ptr, sizes[run], 0) != KSFT_PASS) {
378e9b60476SAmit Daniel Kachhap mte_free_memory((void *)ptr, sizes[run], mem_type, false);
379e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
380e9b60476SAmit Daniel Kachhap }
381e9b60476SAmit Daniel Kachhap mte_free_memory((void *)ptr, sizes[run], mem_type, false);
382e9b60476SAmit Daniel Kachhap
383e9b60476SAmit Daniel Kachhap /* check initial tags for file mmap */
384e9b60476SAmit Daniel Kachhap fd = create_temp_file();
385e9b60476SAmit Daniel Kachhap if (fd == -1)
386e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
387e9b60476SAmit Daniel Kachhap ptr = (char *)mte_allocate_file_memory(sizes[run], mem_type, mapping, false, fd);
388e9b60476SAmit Daniel Kachhap if (check_allocated_memory(ptr, sizes[run], mem_type, false) != KSFT_PASS) {
389e9b60476SAmit Daniel Kachhap close(fd);
390e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
391e9b60476SAmit Daniel Kachhap }
392e9b60476SAmit Daniel Kachhap if (compare_memory_tags(ptr, sizes[run], 0) != KSFT_PASS) {
393e9b60476SAmit Daniel Kachhap mte_free_memory((void *)ptr, sizes[run], mem_type, false);
394e9b60476SAmit Daniel Kachhap close(fd);
395e9b60476SAmit Daniel Kachhap return KSFT_FAIL;
396e9b60476SAmit Daniel Kachhap }
397e9b60476SAmit Daniel Kachhap mte_free_memory((void *)ptr, sizes[run], mem_type, false);
398e9b60476SAmit Daniel Kachhap close(fd);
399e9b60476SAmit Daniel Kachhap }
400e9b60476SAmit Daniel Kachhap return KSFT_PASS;
401e9b60476SAmit Daniel Kachhap }
402e9b60476SAmit Daniel Kachhap
main(int argc,char * argv[])403e9b60476SAmit Daniel Kachhap int main(int argc, char *argv[])
404e9b60476SAmit Daniel Kachhap {
405e9b60476SAmit Daniel Kachhap int err;
406e9b60476SAmit Daniel Kachhap size_t page_size = getpagesize();
407*a0fa0b63SKang Minchul int item = ARRAY_SIZE(sizes);
408e9b60476SAmit Daniel Kachhap
409e9b60476SAmit Daniel Kachhap sizes[item - 3] = page_size - 1;
410e9b60476SAmit Daniel Kachhap sizes[item - 2] = page_size;
411e9b60476SAmit Daniel Kachhap sizes[item - 1] = page_size + 1;
412e9b60476SAmit Daniel Kachhap
413e9b60476SAmit Daniel Kachhap err = mte_default_setup();
414e9b60476SAmit Daniel Kachhap if (err)
415e9b60476SAmit Daniel Kachhap return err;
416e9b60476SAmit Daniel Kachhap
417e9b60476SAmit Daniel Kachhap /* Register SIGSEGV handler */
418e9b60476SAmit Daniel Kachhap mte_register_signal(SIGSEGV, mte_default_handler);
419e9b60476SAmit Daniel Kachhap
4205bc7c115SVincenzo Frascino /* Set test plan */
4215bc7c115SVincenzo Frascino ksft_set_plan(20);
4225bc7c115SVincenzo Frascino
423e9b60476SAmit Daniel Kachhap /* Buffer by byte tests */
424e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_by_byte(USE_MMAP, MTE_SYNC_ERR),
425e9b60476SAmit Daniel Kachhap "Check buffer correctness by byte with sync err mode and mmap memory\n");
426e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_by_byte(USE_MMAP, MTE_ASYNC_ERR),
427e9b60476SAmit Daniel Kachhap "Check buffer correctness by byte with async err mode and mmap memory\n");
428e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_by_byte(USE_MPROTECT, MTE_SYNC_ERR),
429e9b60476SAmit Daniel Kachhap "Check buffer correctness by byte with sync err mode and mmap/mprotect memory\n");
430e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_by_byte(USE_MPROTECT, MTE_ASYNC_ERR),
431e9b60476SAmit Daniel Kachhap "Check buffer correctness by byte with async err mode and mmap/mprotect memory\n");
432e9b60476SAmit Daniel Kachhap
433e9b60476SAmit Daniel Kachhap /* Check buffer underflow with underflow size as 16 */
434e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_SYNC_ERR, MT_GRANULE_SIZE),
435e9b60476SAmit Daniel Kachhap "Check buffer write underflow by byte with sync mode and mmap memory\n");
436e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_ASYNC_ERR, MT_GRANULE_SIZE),
437e9b60476SAmit Daniel Kachhap "Check buffer write underflow by byte with async mode and mmap memory\n");
438e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_NONE_ERR, MT_GRANULE_SIZE),
439e9b60476SAmit Daniel Kachhap "Check buffer write underflow by byte with tag check fault ignore and mmap memory\n");
440e9b60476SAmit Daniel Kachhap
441e9b60476SAmit Daniel Kachhap /* Check buffer underflow with underflow size as page size */
442e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_SYNC_ERR, page_size),
443e9b60476SAmit Daniel Kachhap "Check buffer write underflow by byte with sync mode and mmap memory\n");
444e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_ASYNC_ERR, page_size),
445e9b60476SAmit Daniel Kachhap "Check buffer write underflow by byte with async mode and mmap memory\n");
446e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_underflow_by_byte(USE_MMAP, MTE_NONE_ERR, page_size),
447e9b60476SAmit Daniel Kachhap "Check buffer write underflow by byte with tag check fault ignore and mmap memory\n");
448e9b60476SAmit Daniel Kachhap
449e9b60476SAmit Daniel Kachhap /* Check buffer overflow with overflow size as 16 */
450e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_overflow_by_byte(USE_MMAP, MTE_SYNC_ERR, MT_GRANULE_SIZE),
451e9b60476SAmit Daniel Kachhap "Check buffer write overflow by byte with sync mode and mmap memory\n");
452e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_overflow_by_byte(USE_MMAP, MTE_ASYNC_ERR, MT_GRANULE_SIZE),
453e9b60476SAmit Daniel Kachhap "Check buffer write overflow by byte with async mode and mmap memory\n");
454e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_overflow_by_byte(USE_MMAP, MTE_NONE_ERR, MT_GRANULE_SIZE),
455e9b60476SAmit Daniel Kachhap "Check buffer write overflow by byte with tag fault ignore mode and mmap memory\n");
456e9b60476SAmit Daniel Kachhap
457e9b60476SAmit Daniel Kachhap /* Buffer by block tests */
458e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_by_block(USE_MMAP, MTE_SYNC_ERR),
459e9b60476SAmit Daniel Kachhap "Check buffer write correctness by block with sync mode and mmap memory\n");
460e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_by_block(USE_MMAP, MTE_ASYNC_ERR),
461e9b60476SAmit Daniel Kachhap "Check buffer write correctness by block with async mode and mmap memory\n");
462e9b60476SAmit Daniel Kachhap evaluate_test(check_buffer_by_block(USE_MMAP, MTE_NONE_ERR),
463e9b60476SAmit Daniel Kachhap "Check buffer write correctness by block with tag fault ignore and mmap memory\n");
464e9b60476SAmit Daniel Kachhap
465e9b60476SAmit Daniel Kachhap /* Initial tags are supposed to be 0 */
466e9b60476SAmit Daniel Kachhap evaluate_test(check_memory_initial_tags(USE_MMAP, MTE_SYNC_ERR, MAP_PRIVATE),
467e9b60476SAmit Daniel Kachhap "Check initial tags with private mapping, sync error mode and mmap memory\n");
468e9b60476SAmit Daniel Kachhap evaluate_test(check_memory_initial_tags(USE_MPROTECT, MTE_SYNC_ERR, MAP_PRIVATE),
469e9b60476SAmit Daniel Kachhap "Check initial tags with private mapping, sync error mode and mmap/mprotect memory\n");
470e9b60476SAmit Daniel Kachhap evaluate_test(check_memory_initial_tags(USE_MMAP, MTE_SYNC_ERR, MAP_SHARED),
471e9b60476SAmit Daniel Kachhap "Check initial tags with shared mapping, sync error mode and mmap memory\n");
472e9b60476SAmit Daniel Kachhap evaluate_test(check_memory_initial_tags(USE_MPROTECT, MTE_SYNC_ERR, MAP_SHARED),
473e9b60476SAmit Daniel Kachhap "Check initial tags with shared mapping, sync error mode and mmap/mprotect memory\n");
474e9b60476SAmit Daniel Kachhap
475e9b60476SAmit Daniel Kachhap mte_restore_setup();
476e9b60476SAmit Daniel Kachhap ksft_print_cnts();
477e9b60476SAmit Daniel Kachhap return ksft_get_fail_cnt() == 0 ? KSFT_PASS : KSFT_FAIL;
478e9b60476SAmit Daniel Kachhap }
479