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