xref: /xnu-12377.81.4/tests/arm_mte_debugger_helper.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2024 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #include <arm_acle.h>
29 #include <fcntl.h>
30 #include <mach/mach.h>
31 #include <mach/mach_vm.h>
32 #include <libproc.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/mman.h>
36 #include <unistd.h>
37 
38 #include "arm_mte_utilities.h"
39 
40 static void
assert_posix_success(int ret,char * msg)41 assert_posix_success(int ret, char *msg)
42 {
43 	if (ret == -1) {
44 		fprintf(stderr, "error in process being debugged: %s\n", msg);
45 		exit(1);
46 	}
47 }
48 
49 /*
50  * This program is used by the mte_debugger_untagged_copyio test in
51  * arm_mte_unentitled.c. In this setup, the test is the "debugger" and this code
52  * is the "program being debugged".
53  */
54 int
main(int argc,char * argv[])55 main(int argc, char *argv[])
56 {
57 	if (argc != 2) {
58 		fprintf(stderr, "usage: arm_mte_debugger_helper shm_name\n");
59 		fprintf(stderr, "this is intended to be called from test code\n");
60 		exit(1);
61 	}
62 	int ret;
63 	struct proc_bsdinfo bsd_info;
64 	ret = proc_pidinfo(getpid(), PROC_PIDTBSDINFO, 0, &bsd_info, sizeof(bsd_info));
65 	if (ret != sizeof(bsd_info)) {
66 		fprintf(stderr, "PROC_PIDTBSDINFO");
67 		exit(1);
68 	}
69 	if (!(bsd_info.pbi_flags & PROC_FLAG_SEC_ENABLED)) {
70 		fprintf(stderr, "arm_mte_debugger_helper launched without MTE enabled");
71 		exit(1);
72 	}
73 
74 	char *shm_name = argv[1];
75 	int fd = shm_open(shm_name, O_RDWR);
76 	assert_posix_success(fd, "shm_open");
77 
78 	vm_address_t *shm = mmap(NULL, sizeof(vm_address_t), PROT_READ | PROT_WRITE,
79 	    MAP_SHARED, fd, 0);
80 	if ((void*) shm == MAP_FAILED) {
81 		fprintf(stderr, "mmap");
82 		exit(1);
83 	}
84 
85 	/* the test will tell us how much memory to allocate via the shared memory */
86 	const vm_size_t ALLOC_SIZE = (mach_vm_size_t) *shm;
87 
88 	/* allocate some tagged memory */
89 	vm_address_t addr = 0;
90 	kern_return_t kr = vm_allocate(mach_task_self(), &addr, ALLOC_SIZE,
91 	    VM_FLAGS_ANYWHERE | VM_FLAGS_MTE);
92 	if (kr != KERN_SUCCESS) {
93 		fprintf(stderr, "vm_allocate");
94 		exit(1);
95 	}
96 
97 	/* write some tags and data */
98 	uint64_t mask = __arm_mte_exclude_tag((void*) addr, 0);
99 	uint8_t *tagged_ptr = __arm_mte_create_random_tag((void*) addr, mask);
100 	for (vm_size_t i = 0; i < ALLOC_SIZE; i += MTE_GRANULE_SIZE) {
101 		__arm_mte_set_tag(tagged_ptr + i);
102 	}
103 	memset(tagged_ptr, 'A', ALLOC_SIZE);
104 
105 	/* send the tagged memory's address back to the "debugger" so it can remap it */
106 	*shm = addr;
107 	msync((void*) shm, sizeof(vm_address_t), MS_SYNC | MS_INVALIDATE);
108 
109 	ret = close(fd);
110 	assert_posix_success(ret, "close");
111 
112 	/* when the parent process dies, the child will be given to launchd (pid=1) */
113 	while (getppid() != 1) {
114 		/* we need to keep the process alive so the memory doesn't go away */
115 		sleep(1);
116 	}
117 }
118