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