1 /* 2 * Copyright (c) 2024 Apple 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 29 #pragma once 30 31 /* 32 * try_read_write.h 33 * 34 * Helper functions for userspace tests to read or write memory and 35 * verify that EXC_BAD_ACCESS is or is not generated by that operation. 36 * 37 * To use these functions in your test you must set additional build options. 38 * See target `try_read_write_test` in tests/Makefile for an example. 39 */ 40 41 #include <stdint.h> 42 #include <stdbool.h> 43 #include <mach/mach_types.h> 44 45 /* 46 * Set verbose_exc_helper = true to log exception information with T_LOG(). 47 * The default is true. 48 */ 49 extern bool verbose_exc_helper; 50 51 /* 52 * Tries to read a single byte from an address. 53 * Returns true if the read succeeded. 54 * Aborts if an exception other than EXC_BAD_ACCESS is generated. 55 * On exit: 56 * *out_byte is the value read, or an indeterminate value if the read failed. 57 * *out_error is the EXC_BAD_ACCESS error code 58 * (typically KERN_PROTECTION_FAILURE or KERN_INVALID_ADDRESS) 59 * or 0 if the read succeeded. 60 * 61 * To use this function in your test you must set additional build options. 62 * See target `try_read_write_test` in tests/Makefile for an example. 63 */ 64 extern bool 65 try_read_byte( 66 mach_vm_address_t addr, 67 uint8_t * const out_byte, 68 kern_return_t * const out_error); 69 70 /* 71 * Tries to write a single byte to an address. 72 * Returns true if the write succeeded. 73 * Aborts if an exception other than EXC_BAD_ACCESS is generated. 74 * On exit: 75 * *out_error is the EXC_BAD_ACCESS error code 76 * (typically KERN_PROTECTION_FAILURE or KERN_INVALID__ADDRESS) 77 * or 0 if the write succeeded. 78 * 79 * To use this function in your test you must set additional build options. 80 * See target `try_read_write_test` in tests/Makefile for an example. 81 */ 82 extern bool 83 try_write_byte( 84 mach_vm_address_t addr, 85 uint8_t byte, 86 kern_return_t * const out_error); 87