xref: /xnu-12377.81.4/tests/exc_guard_helper.h (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions /*
2*043036a2SApple OSS Distributions  * Copyright (c) 2024 Apple Inc. All rights reserved.
3*043036a2SApple OSS Distributions  *
4*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*043036a2SApple OSS Distributions  *
6*043036a2SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*043036a2SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*043036a2SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*043036a2SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*043036a2SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*043036a2SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*043036a2SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*043036a2SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*043036a2SApple OSS Distributions  *
15*043036a2SApple OSS Distributions  * Please obtain a copy of the License at
16*043036a2SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*043036a2SApple OSS Distributions  *
18*043036a2SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*043036a2SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*043036a2SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*043036a2SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*043036a2SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*043036a2SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*043036a2SApple OSS Distributions  * limitations under the License.
25*043036a2SApple OSS Distributions  *
26*043036a2SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*043036a2SApple OSS Distributions  */
28*043036a2SApple OSS Distributions 
29*043036a2SApple OSS Distributions /*
30*043036a2SApple OSS Distributions  * exc_guard_helper.h
31*043036a2SApple OSS Distributions  *
32*043036a2SApple OSS Distributions  * Helper functions for userspace tests to test for EXC_GUARD exceptions.
33*043036a2SApple OSS Distributions  *
34*043036a2SApple OSS Distributions  * To use these functions in your test you must set additional build options.
35*043036a2SApple OSS Distributions  * See target `exc_guard_helper_test` in tests/Makefile for an example.
36*043036a2SApple OSS Distributions  */
37*043036a2SApple OSS Distributions 
38*043036a2SApple OSS Distributions #pragma once
39*043036a2SApple OSS Distributions 
40*043036a2SApple OSS Distributions #include <stdbool.h>
41*043036a2SApple OSS Distributions #include <stdint.h>
42*043036a2SApple OSS Distributions #include <mach/task_info.h>
43*043036a2SApple OSS Distributions 
44*043036a2SApple OSS Distributions /*
45*043036a2SApple OSS Distributions  * Set verbose_exc_helper = true to log exception information with T_LOG().
46*043036a2SApple OSS Distributions  * The default is true.
47*043036a2SApple OSS Distributions  */
48*043036a2SApple OSS Distributions extern bool verbose_exc_helper;
49*043036a2SApple OSS Distributions 
50*043036a2SApple OSS Distributions typedef struct {
51*043036a2SApple OSS Distributions 	/* The number of EXC_GUARD exceptions caught during the block. */
52*043036a2SApple OSS Distributions 	unsigned catch_count;
53*043036a2SApple OSS Distributions 
54*043036a2SApple OSS Distributions 	/*
55*043036a2SApple OSS Distributions 	 * The remaining fields are only set for the first EXC_GUARD caught.
56*043036a2SApple OSS Distributions 	 * See kern/exc_guard.h for definitions of these fields.
57*043036a2SApple OSS Distributions 	 */
58*043036a2SApple OSS Distributions 	unsigned guard_type;     /* e.g. GUARD_TYPE_VIRT_MEMORY */
59*043036a2SApple OSS Distributions 	uint32_t guard_flavor;
60*043036a2SApple OSS Distributions 	uint32_t guard_target;
61*043036a2SApple OSS Distributions 	uint64_t guard_payload;
62*043036a2SApple OSS Distributions } exc_guard_helper_info_t;
63*043036a2SApple OSS Distributions 
64*043036a2SApple OSS Distributions /*
65*043036a2SApple OSS Distributions  * Initialize exc_guard_helper's exception handling.
66*043036a2SApple OSS Distributions  *
67*043036a2SApple OSS Distributions  * Calling this is optional. The other functions will perform
68*043036a2SApple OSS Distributions  * initialization if necessary. You may need to call this
69*043036a2SApple OSS Distributions  * function if that automatic initialization allocates
70*043036a2SApple OSS Distributions  * memory in address ranges that your test requires to
71*043036a2SApple OSS Distributions  * be unallocated.
72*043036a2SApple OSS Distributions  */
73*043036a2SApple OSS Distributions extern void
74*043036a2SApple OSS Distributions exc_guard_helper_init(void);
75*043036a2SApple OSS Distributions 
76*043036a2SApple OSS Distributions /*
77*043036a2SApple OSS Distributions  * Sets EXC_GUARD exceptions of the given type (e.g. GUARD_TYPE_VIRT_MEMORY)
78*043036a2SApple OSS Distributions  * to be enabled and non-fatal in this process.
79*043036a2SApple OSS Distributions  * Returns the previous guard exception behavior. Pass this value
80*043036a2SApple OSS Distributions  * to task_set_exc_guard_behavior() to restore the previous behavior.
81*043036a2SApple OSS Distributions  *
82*043036a2SApple OSS Distributions  * Fails with T_FAIL if the behavior could not be set; for example:
83*043036a2SApple OSS Distributions  * - guard exceptions cannot be configured in some processes
84*043036a2SApple OSS Distributions  * - some guard exception types cannot be set to non-fatal
85*043036a2SApple OSS Distributions  */
86*043036a2SApple OSS Distributions extern task_exc_guard_behavior_t
87*043036a2SApple OSS Distributions enable_exc_guard_of_type(unsigned int guard_type);
88*043036a2SApple OSS Distributions 
89*043036a2SApple OSS Distributions /*
90*043036a2SApple OSS Distributions  * Runs block() and returns true if it raised a non-fatal EXC_GUARD exception
91*043036a2SApple OSS Distributions  * of the requested type (e.g. GUARD_TYPE_VIRT_MEMORY).
92*043036a2SApple OSS Distributions  *
93*043036a2SApple OSS Distributions  * While block() runs, any EXC_GUARD exceptions of the requested
94*043036a2SApple OSS Distributions  * type are caught and recorded, then execution resumes.
95*043036a2SApple OSS Distributions  * Information about any caught exception(s) is returned in *out_exc_info.
96*043036a2SApple OSS Distributions  * If more than one EXC_GUARD exception of the requested type is raised then
97*043036a2SApple OSS Distributions  * details about all but the first are discarded, other than `catch_count`
98*043036a2SApple OSS Distributions  * the number of exceptions caught.
99*043036a2SApple OSS Distributions  *
100*043036a2SApple OSS Distributions  * Guard exceptions of this type must be enabled and non-fatal.
101*043036a2SApple OSS Distributions  * enable_exc_guard_of_type() can set this for your process.
102*043036a2SApple OSS Distributions  *
103*043036a2SApple OSS Distributions  * Note that block_raised_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY)
104*043036a2SApple OSS Distributions  * does not work on Rosetta. This function will T_FAIL if you try.
105*043036a2SApple OSS Distributions  * See block_raised_exc_guard_of_type_ignoring_translated() below
106*043036a2SApple OSS Distributions  * if you are willing to forgo the guard exception handler in
107*043036a2SApple OSS Distributions  * translated execution environments like Rosetta.
108*043036a2SApple OSS Distributions  *
109*043036a2SApple OSS Distributions  * Example:
110*043036a2SApple OSS Distributions  *      enable_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY);
111*043036a2SApple OSS Distributions  *      [...]
112*043036a2SApple OSS Distributions  *      exc_guard_helper_info_t exc_info;
113*043036a2SApple OSS Distributions  *      if (block_raised_exc_guard_of_type(GUARD_TYPE_VIRT_MEMORY, &exc_info, ^{
114*043036a2SApple OSS Distributions  *              mach_vm_deallocate(mach_task_self(), addr, size);
115*043036a2SApple OSS Distributions  *          })) {
116*043036a2SApple OSS Distributions  *              // EXC_GUARD raised during mach_vm_deallocate, details in exc_info
117*043036a2SApple OSS Distributions  *      } else {
118*043036a2SApple OSS Distributions  *              // mach_vm_deallocate did not raise EXC_GUARD
119*043036a2SApple OSS Distributions  *      }
120*043036a2SApple OSS Distributions  */
121*043036a2SApple OSS Distributions typedef void (^exc_guard_helper_block_t)(void);
122*043036a2SApple OSS Distributions extern bool
123*043036a2SApple OSS Distributions block_raised_exc_guard_of_type(
124*043036a2SApple OSS Distributions 	unsigned int guard_type,
125*043036a2SApple OSS Distributions 	exc_guard_helper_info_t * const out_exc_info,
126*043036a2SApple OSS Distributions 	exc_guard_helper_block_t block);
127*043036a2SApple OSS Distributions 
128*043036a2SApple OSS Distributions /*
129*043036a2SApple OSS Distributions  * Like block_raised_exc_guard_of_type(), but quietly
130*043036a2SApple OSS Distributions  * runs the block with no guard exception handler if
131*043036a2SApple OSS Distributions  * the guard type is GUARD_TYPE_VIRT_MEMORY and we're
132*043036a2SApple OSS Distributions  * in a translated execution environment like Rosetta.
133*043036a2SApple OSS Distributions  */
134*043036a2SApple OSS Distributions extern bool
135*043036a2SApple OSS Distributions block_raised_exc_guard_of_type_ignoring_translated(
136*043036a2SApple OSS Distributions 	unsigned int guard_type,
137*043036a2SApple OSS Distributions 	exc_guard_helper_info_t * const out_exc_info,
138*043036a2SApple OSS Distributions 	exc_guard_helper_block_t block);
139