tools/testing/selftests/arm64/mte/check_child_memory.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/arm64/mte/check_child_memory.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/arm64/mte/check_child_memory.c
Extension
.c
Size
6789 bytes
Lines
199
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (cur_mte_cxt.fault_valid == true) {
			fault = 1;
			goto check_child_tag_inheritance_err;
		}
		for (i = 0 ; i < size ; i += MT_GRANULE_SIZE) {
			child_tag = MT_FETCH_TAG((uintptr_t)(mte_get_tag_address(ptr + i)));
			if (parent_tag != child_tag) {
				ksft_print_msg("FAIL: child mte tag mismatch\n");
				fault = 1;
				goto check_child_tag_inheritance_err;
			}
		}
		mte_initialize_current_context(mode, (uintptr_t)ptr, -UNDERFLOW);
		memset(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);
		mte_wait_after_trig();
		if (cur_mte_cxt.fault_valid == false) {
			fault = 1;
			goto check_child_tag_inheritance_err;
		}
check_child_tag_inheritance_err:
		_exit(fault);
	}
	/* Wait for child process to terminate */
	wait(&child_status);
	if (WIFEXITED(child_status))
		fault = WEXITSTATUS(child_status);
	else
		fault = 1;
	return (fault) ? KSFT_FAIL : KSFT_PASS;
}

static int check_child_memory_mapping(int mem_type, int mode, int mapping)
{
	char *ptr;
	int run, result;
	int item = ARRAY_SIZE(sizes);

	item = ARRAY_SIZE(sizes);
	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG, false);
	for (run = 0; run < item; run++) {
		ptr = (char *)mte_allocate_memory_tag_range(sizes[run], mem_type, mapping,
							    UNDERFLOW, OVERFLOW);
		if (check_allocated_memory_range(ptr, sizes[run], mem_type,
						 UNDERFLOW, OVERFLOW) != KSFT_PASS)
			return KSFT_FAIL;
		result = check_child_tag_inheritance(ptr, sizes[run], mode);
		mte_free_memory_tag_range((void *)ptr, sizes[run], mem_type, UNDERFLOW, OVERFLOW);
		if (result == KSFT_FAIL)
			return result;
	}
	return KSFT_PASS;
}

static int check_child_file_mapping(int mem_type, int mode, int mapping)
{
	char *ptr, *map_ptr;
	int run, fd, map_size, result = KSFT_PASS;
	int total = ARRAY_SIZE(sizes);

	mte_switch_mode(mode, MTE_ALLOW_NON_ZERO_TAG, false);
	for (run = 0; run < total; run++) {
		fd = create_temp_file();
		if (fd == -1)
			return KSFT_FAIL;

		map_size = sizes[run] + OVERFLOW + UNDERFLOW;
		map_ptr = (char *)mte_allocate_file_memory(map_size, mem_type, mapping, false, fd);
		if (check_allocated_memory(map_ptr, map_size, mem_type, false) != KSFT_PASS) {
			close(fd);
			return KSFT_FAIL;
		}
		ptr = map_ptr + UNDERFLOW;
		mte_initialize_current_context(mode, (uintptr_t)ptr, sizes[run]);
		/* Only mte enabled memory will allow tag insertion */
		ptr = mte_insert_tags((void *)ptr, sizes[run]);
		if (!ptr || cur_mte_cxt.fault_valid == true) {
			ksft_print_msg("FAIL: Insert tags on file based memory\n");
			munmap((void *)map_ptr, map_size);
			close(fd);
			return KSFT_FAIL;
		}
		result = check_child_tag_inheritance(ptr, sizes[run], mode);
		mte_clear_tags((void *)ptr, sizes[run]);

Annotation

Implementation Notes