xref: /xnu-12377.41.6/tests/exc_guard_helper.h (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2024 Apple 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 
29*bbb1b6f9SApple OSS Distributions /*
30*bbb1b6f9SApple OSS Distributions  * exc_guard_helper.h
31*bbb1b6f9SApple OSS Distributions  *
32*bbb1b6f9SApple OSS Distributions  * Helper functions for userspace tests to test for EXC_GUARD exceptions.
33*bbb1b6f9SApple OSS Distributions  *
34*bbb1b6f9SApple OSS Distributions  * To use these functions in your test you must set additional build options.
35*bbb1b6f9SApple OSS Distributions  * See target `exc_guard_helper_test` in tests/Makefile for an example.
36*bbb1b6f9SApple OSS Distributions  */
37*bbb1b6f9SApple OSS Distributions 
38*bbb1b6f9SApple OSS Distributions #pragma once
39*bbb1b6f9SApple OSS Distributions 
40*bbb1b6f9SApple OSS Distributions #include <stdbool.h>
41*bbb1b6f9SApple OSS Distributions #include <stdint.h>
42*bbb1b6f9SApple OSS Distributions #include <mach/task_info.h>
43*bbb1b6f9SApple OSS Distributions 
44*bbb1b6f9SApple OSS Distributions /*
45*bbb1b6f9SApple OSS Distributions  * Set verbose_exc_helper = true to log exception information with T_LOG().
46*bbb1b6f9SApple OSS Distributions  * The default is true.
47*bbb1b6f9SApple OSS Distributions  */
48*bbb1b6f9SApple OSS Distributions extern bool verbose_exc_helper;
49*bbb1b6f9SApple OSS Distributions 
50*bbb1b6f9SApple OSS Distributions typedef struct {
51*bbb1b6f9SApple OSS Distributions 	/* The number of EXC_GUARD exceptions caught during the block. */
52*bbb1b6f9SApple OSS Distributions 	unsigned catch_count;
53*bbb1b6f9SApple OSS Distributions 
54*bbb1b6f9SApple OSS Distributions 	/*
55*bbb1b6f9SApple OSS Distributions 	 * The remaining fields are only set for the first EXC_GUARD caught.
56*bbb1b6f9SApple OSS Distributions 	 * See kern/exc_guard.h for definitions of these fields.
57*bbb1b6f9SApple OSS Distributions 	 */
58*bbb1b6f9SApple OSS Distributions 	unsigned guard_type;     /* e.g. GUARD_TYPE_VIRT_MEMORY */
59*bbb1b6f9SApple OSS Distributions 	uint32_t guard_flavor;
60*bbb1b6f9SApple OSS Distributions 	uint32_t guard_target;
61*bbb1b6f9SApple OSS Distributions 	uint64_t guard_payload;
62*bbb1b6f9SApple OSS Distributions } exc_guard_helper_info_t;
63*bbb1b6f9SApple OSS Distributions 
64*bbb1b6f9SApple OSS Distributions /*
65*bbb1b6f9SApple OSS Distributions  * Initialize exc_guard_helper's exception handling.
66*bbb1b6f9SApple OSS Distributions  *
67*bbb1b6f9SApple OSS Distributions  * Calling this is optional. The other functions will perform
68*bbb1b6f9SApple OSS Distributions  * initialization if necessary. You may need to call this
69*bbb1b6f9SApple OSS Distributions  * function if that automatic initialization allocates
70*bbb1b6f9SApple OSS Distributions  * memory in address ranges that your test requires to
71*bbb1b6f9SApple OSS Distributions  * be unallocated.
72*bbb1b6f9SApple OSS Distributions  */
73*bbb1b6f9SApple OSS Distributions extern void
74*bbb1b6f9SApple OSS Distributions exc_guard_helper_init(void);
75*bbb1b6f9SApple OSS Distributions 
76*bbb1b6f9SApple OSS Distributions /*
77*bbb1b6f9SApple OSS Distributions  * Sets EXC_GUARD exceptions of the given type (e.g. GUARD_TYPE_VIRT_MEMORY)
78*bbb1b6f9SApple OSS Distributions  * to be enabled and non-fatal in this process.
79*bbb1b6f9SApple OSS Distributions  * Returns the previous guard exception behavior. Pass this value
80*bbb1b6f9SApple OSS Distributions  * to task_set_exc_guard_behavior() to restore the previous behavior.
81*bbb1b6f9SApple OSS Distributions  *
82*bbb1b6f9SApple OSS Distributions  * Fails with T_FAIL if the behavior could not be set; for example:
83*bbb1b6f9SApple OSS Distributions  * - guard exceptions cannot be configured in some processes
84*bbb1b6f9SApple OSS Distributions  * - some guard exception types cannot be set to non-fatal
85*bbb1b6f9SApple OSS Distributions  */
86*bbb1b6f9SApple OSS Distributions extern task_exc_guard_behavior_t
87*bbb1b6f9SApple OSS Distributions enable_exc_guard_of_type(unsigned int guard_type);
88*bbb1b6f9SApple OSS Distributions 
89*bbb1b6f9SApple OSS Distributions /*
90*bbb1b6f9SApple OSS Distributions  * Runs block() and returns true if it raised a non-fatal EXC_GUARD exception
91*bbb1b6f9SApple OSS Distributions  * of the requested type (e.g. GUARD_TYPE_VIRT_MEMORY).
92*bbb1b6f9SApple OSS Distributions  *
93*bbb1b6f9SApple OSS Distributions  * While block() runs, any EXC_GUARD exceptions of the requested
94*bbb1b6f9SApple OSS Distributions  * type are caught and recorded, then execution resumes.
95*bbb1b6f9SApple OSS Distributions  * Information about any caught exception(s) is returned in *out_exc_info.
96*bbb1b6f9SApple OSS Distributions  * If more than one EXC_GUARD exception of the requested type is raised then
97*bbb1b6f9SApple OSS Distributions  * details about all but the first are discarded, other than `catch_count`
98*bbb1b6f9SApple OSS Distributions  * the number of exceptions caught.
99*bbb1b6f9SApple OSS Distributions  *
100*bbb1b6f9SApple OSS Distributions  * Guard exceptions of this type must be enabled and non-fatal.
101*bbb1b6f9SApple OSS Distributions  * enable_exc_guard_of_type() can set this for your process.
102*bbb1b6f9SApple OSS Distributions  *
103*bbb1b6f9SApple OSS Distributions  * Note that block_raised_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY)
104*bbb1b6f9SApple OSS Distributions  * does not work on Rosetta. This function will T_FAIL if you try.
105*bbb1b6f9SApple OSS Distributions  * See block_raised_exc_guard_of_type_ignoring_translated() below
106*bbb1b6f9SApple OSS Distributions  * if you are willing to forgo the guard exception handler in
107*bbb1b6f9SApple OSS Distributions  * translated execution environments like Rosetta.
108*bbb1b6f9SApple OSS Distributions  *
109*bbb1b6f9SApple OSS Distributions  * Example:
110*bbb1b6f9SApple OSS Distributions  *      enable_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY);
111*bbb1b6f9SApple OSS Distributions  *      [...]
112*bbb1b6f9SApple OSS Distributions  *      exc_guard_helper_info_t exc_info;
113*bbb1b6f9SApple OSS Distributions  *      if (block_raised_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY, &exc_info, ^{
114*bbb1b6f9SApple OSS Distributions  *              mach_vm_deallocate(mach_task_self(), addr, size);
115*bbb1b6f9SApple OSS Distributions  *          })) {
116*bbb1b6f9SApple OSS Distributions  *              // EXC_GUARD raised during mach_vm_deallocate, details in exc_info
117*bbb1b6f9SApple OSS Distributions  *      } else {
118*bbb1b6f9SApple OSS Distributions  *              // mach_vm_deallocate did not raise EXC_GUARD
119*bbb1b6f9SApple OSS Distributions  *      }
120*bbb1b6f9SApple OSS Distributions  */
121*bbb1b6f9SApple OSS Distributions typedef void (^exc_guard_helper_block_t)(void);
122*bbb1b6f9SApple OSS Distributions extern bool
123*bbb1b6f9SApple OSS Distributions block_raised_exc_guard_of_type(
124*bbb1b6f9SApple OSS Distributions 	unsigned int guard_type,
125*bbb1b6f9SApple OSS Distributions 	exc_guard_helper_info_t * const out_exc_info,
126*bbb1b6f9SApple OSS Distributions 	exc_guard_helper_block_t block);
127*bbb1b6f9SApple OSS Distributions 
128*bbb1b6f9SApple OSS Distributions /*
129*bbb1b6f9SApple OSS Distributions  * Like block_raised_exc_guard_of_type(), but quietly
130*bbb1b6f9SApple OSS Distributions  * runs the block with no guard exception handler if
131*bbb1b6f9SApple OSS Distributions  * the guard type is GUARD_TYPE_VIRT_MEMORY and we're
132*bbb1b6f9SApple OSS Distributions  * in a translated execution environment like Rosetta.
133*bbb1b6f9SApple OSS Distributions  */
134*bbb1b6f9SApple OSS Distributions extern bool
135*bbb1b6f9SApple OSS Distributions block_raised_exc_guard_of_type_ignoring_translated(
136*bbb1b6f9SApple OSS Distributions 	unsigned int guard_type,
137*bbb1b6f9SApple OSS Distributions 	exc_guard_helper_info_t * const out_exc_info,
138*bbb1b6f9SApple OSS Distributions 	exc_guard_helper_block_t block);
139