[PATCH 2/2] kselftest/arm64/mte: Use MTE safe memory accessors

Vladimir Murzin vladimir.murzin at arm.com
Tue Sep 22 03:59:59 PDT 2026


Use MTE safe memory accessors instead of library functions with
arbitrary implementations.

Also, for consistency, use accessors even for byte size accesses.

Signed-off-by: Vladimir Murzin <vladimir.murzin at arm.com>
---
 tools/testing/selftests/arm64/mte/check_buffer_fill.c  | 10 +++++-----
 tools/testing/selftests/arm64/mte/check_child_memory.c |  6 +++---
 .../selftests/arm64/mte/check_hugetlb_options.c        |  4 ++--
 tools/testing/selftests/arm64/mte/check_mmap_options.c | 10 +++++-----
 .../testing/selftests/arm64/mte/check_tags_inclusion.c |  4 ++--
 5 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/tools/testing/selftests/arm64/mte/check_buffer_fill.c b/tools/testing/selftests/arm64/mte/check_buffer_fill.c
index 039b1d7d8566..54e1dcaf52ac 100644
--- a/tools/testing/selftests/arm64/mte/check_buffer_fill.c
+++ b/tools/testing/selftests/arm64/mte/check_buffer_fill.c
@@ -41,7 +41,7 @@ static int check_buffer_by_byte(int mem_type, int mode)
 		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';
+			memset_safe(&ptr[j], '1', 1);
 		mte_wait_after_trig();
 		err = cur_mte_cxt.fault_valid;
 		/* Check the buffer whether it is filled. */
@@ -82,7 +82,7 @@ static int check_buffer_underflow_by_byte(int mem_type, int mode,
 		/* Set some value in tagged memory and make the buffer underflow */
 		for (j = sizes[i] - 1; (j >= -underflow_range) &&
 				       (!cur_mte_cxt.fault_valid); j--) {
-			ptr[j] = '1';
+			memset_safe(&ptr[j], '1', 1);
 			last_index = j;
 		}
 		mte_wait_after_trig();
@@ -180,7 +180,7 @@ static int check_buffer_overflow_by_byte(int mem_type, int mode,
 		/* 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';
+			memset_safe(&ptr[j], '1', 1);
 			last_index = j;
 		}
 		mte_wait_after_trig();
@@ -308,8 +308,8 @@ static int check_buffer_by_block_iterate(int mem_type, int mode, size_t size)
 		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);
+		memset_safe((void *)src, (int)'1', size);
+		memcpy_safe((void *)dst, (void *)src, size);
 		mte_wait_after_trig();
 		if (cur_mte_cxt.fault_valid) {
 			result = KSFT_FAIL;
diff --git a/tools/testing/selftests/arm64/mte/check_child_memory.c b/tools/testing/selftests/arm64/mte/check_child_memory.c
index e6a8acca2a94..5c62359e5682 100644
--- a/tools/testing/selftests/arm64/mte/check_child_memory.c
+++ b/tools/testing/selftests/arm64/mte/check_child_memory.c
@@ -41,7 +41,7 @@ static int check_child_tag_inheritance(char *ptr, int size, int mode)
 	} else if (child == 0) {
 		mte_initialize_current_context(mode, (uintptr_t)ptr, size);
 		/* Do copy on write */
-		memset(ptr, '1', size);
+		memset_safe(ptr, '1', size);
 		mte_wait_after_trig();
 		if (cur_mte_cxt.fault_valid == true) {
 			fault = 1;
@@ -56,14 +56,14 @@ static int check_child_tag_inheritance(char *ptr, int size, int mode)
 			}
 		}
 		mte_initialize_current_context(mode, (uintptr_t)ptr, -UNDERFLOW);
-		memset(ptr - UNDERFLOW, '2', UNDERFLOW);
+		memset_safe(ptr - UNDERFLOW, '2', UNDERFLOW);
 		mte_wait_after_trig();
 		if (cur_mte_cxt.fault_valid == false) {
 			fault = 1;
 			goto check_child_tag_inheritance_err;
 		}
 		mte_initialize_current_context(mode, (uintptr_t)ptr, size + OVERFLOW);
-		memset(ptr + size, '3', OVERFLOW);
+		memset_safe(ptr + size, '3', OVERFLOW);
 		mte_wait_after_trig();
 		if (cur_mte_cxt.fault_valid == false) {
 			fault = 1;
diff --git a/tools/testing/selftests/arm64/mte/check_hugetlb_options.c b/tools/testing/selftests/arm64/mte/check_hugetlb_options.c
index 23e4a7a9950c..85f1574b88be 100644
--- a/tools/testing/selftests/arm64/mte/check_hugetlb_options.c
+++ b/tools/testing/selftests/arm64/mte/check_hugetlb_options.c
@@ -106,7 +106,7 @@ static int check_child_tag_inheritance(char *ptr, int size, int mode)
 	} else if (child == 0) {
 		mte_initialize_current_context(mode, (uintptr_t)ptr, size);
 		/* Do copy on write */
-		memset(ptr, '1', size);
+		memset_safe(ptr, '1', size);
 		mte_wait_after_trig();
 		if (cur_mte_cxt.fault_valid == true) {
 			fault = 1;
@@ -135,7 +135,7 @@ static int check_child_tag_inheritance(char *ptr, int size, int mode)
 static int check_mte_memory(char *ptr, int size, int mode, int tag_check)
 {
 	mte_initialize_current_context(mode, (uintptr_t)ptr, size);
-	memset(ptr, '1', size);
+	memset_safe(ptr, '1', size);
 	mte_wait_after_trig();
 	if (cur_mte_cxt.fault_valid == true)
 		return KSFT_FAIL;
diff --git a/tools/testing/selftests/arm64/mte/check_mmap_options.c b/tools/testing/selftests/arm64/mte/check_mmap_options.c
index 492f2cd41f43..0f4d43fe80f2 100644
--- a/tools/testing/selftests/arm64/mte/check_mmap_options.c
+++ b/tools/testing/selftests/arm64/mte/check_mmap_options.c
@@ -72,13 +72,13 @@ static int check_mte_memory(char *ptr, int size, int mode,
 		ptr = mte_insert_atag(ptr);
 
 	mte_initialize_current_context(mode, (uintptr_t)ptr, size);
-	memset(ptr, '1', size);
+	memset_safe(ptr, '1', size);
 	mte_wait_after_trig();
 	if (cur_mte_cxt.fault_valid == true)
 		return KSFT_FAIL;
 
 	mte_initialize_current_context(mode, (uintptr_t)ptr, -UNDERFLOW);
-	memset(ptr - UNDERFLOW, '2', UNDERFLOW);
+	memset_safe(ptr - UNDERFLOW, '2', UNDERFLOW);
 	mte_wait_after_trig();
 	if (cur_mte_cxt.fault_valid == false && tag_check == TAG_CHECK_ON)
 		return KSFT_FAIL;
@@ -86,7 +86,7 @@ static int check_mte_memory(char *ptr, int size, int mode,
 		return KSFT_FAIL;
 
 	mte_initialize_current_context(mode, (uintptr_t)ptr, size + OVERFLOW);
-	memset(ptr + size, '3', OVERFLOW);
+	memset_safe(ptr + size, '3', OVERFLOW);
 	mte_wait_after_trig();
 	if (cur_mte_cxt.fault_valid == false && tag_check == TAG_CHECK_ON)
 		return KSFT_FAIL;
@@ -95,13 +95,13 @@ static int check_mte_memory(char *ptr, int size, int mode,
 
 	if (tag_op == TAG_OP_STONLY) {
 		mte_initialize_current_context(mode, (uintptr_t)ptr, -UNDERFLOW);
-		memcpy(buf, ptr - UNDERFLOW, MT_GRANULE_SIZE);
+		memcpy_safe(buf, ptr - UNDERFLOW, MT_GRANULE_SIZE);
 		mte_wait_after_trig();
 		if (cur_mte_cxt.fault_valid == true)
 			return KSFT_FAIL;
 
 		mte_initialize_current_context(mode, (uintptr_t)ptr, size + OVERFLOW);
-		memcpy(buf, ptr + size, MT_GRANULE_SIZE);
+		memcpy_safe(buf, ptr + size, MT_GRANULE_SIZE);
 		mte_wait_after_trig();
 		if (cur_mte_cxt.fault_valid == true)
 			return KSFT_FAIL;
diff --git a/tools/testing/selftests/arm64/mte/check_tags_inclusion.c b/tools/testing/selftests/arm64/mte/check_tags_inclusion.c
index 6b4fa6705d7c..bc39be231ae2 100644
--- a/tools/testing/selftests/arm64/mte/check_tags_inclusion.c
+++ b/tools/testing/selftests/arm64/mte/check_tags_inclusion.c
@@ -23,7 +23,7 @@ static int verify_mte_pointer_validity(char *ptr, int mode)
 {
 	mte_initialize_current_context(mode, (uintptr_t)ptr, BUFFER_SIZE);
 	/* Check the validity of the tagged pointer */
-	memset(ptr, '1', BUFFER_SIZE);
+	memset_safe(ptr, '1', BUFFER_SIZE);
 	mte_wait_after_trig();
 	if (cur_mte_cxt.fault_valid) {
 		ksft_print_msg("Unexpected fault recorded for %p-%p in mode %x\n",
@@ -159,7 +159,7 @@ static int check_none_included_tags(int mem_type, int mode)
 		}
 		mte_initialize_current_context(mode, (uintptr_t)ptr, BUFFER_SIZE);
 		/* Check the write validity of the untagged pointer */
-		memset(ptr, '1', BUFFER_SIZE);
+		memset_safe(ptr, '1', BUFFER_SIZE);
 		mte_wait_after_trig();
 		if (cur_mte_cxt.fault_valid)
 			break;
-- 
2.34.1




More information about the linux-arm-kernel mailing list