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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/arm64/mte/check_buffer_fill.c
Extension
.c
Size
14948 bytes
Lines
479
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 (ptr[j] != '1') {
				err = true;
				ksft_print_msg("Buffer is not filled at index:%d of ptr:0x%p\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, false);
	item = ARRAY_SIZE(sizes);
	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)) &&

Annotation

Implementation Notes