Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | /* * Copyright (c) 2015 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <mach/mach.h> kern_return_t thread_get_register_pointer_values(thread_t thread, uintptr_t *sp, size_t *length, uintptr_t *values) { if (!length) { return KERN_INVALID_ARGUMENT; } if (*length > 0 && values == NULL) { return KERN_INVALID_ARGUMENT; } size_t in_length = *length; size_t out_length = 0; #if defined(__i386__) i386_thread_state_t state = {}; thread_state_flavor_t flavor = x86_THREAD_STATE32; mach_msg_type_number_t count = i386_THREAD_STATE_COUNT; #elif defined(__x86_64__) x86_thread_state64_t state = {}; thread_state_flavor_t flavor = x86_THREAD_STATE64; mach_msg_type_number_t count = x86_THREAD_STATE64_COUNT; #elif defined(__arm__) arm_thread_state_t state = {}; thread_state_flavor_t flavor = ARM_THREAD_STATE; mach_msg_type_number_t count = ARM_THREAD_STATE_COUNT; #elif defined(__arm64__) arm_thread_state64_t state = {}; thread_state_flavor_t flavor = ARM_THREAD_STATE64; mach_msg_type_number_t count = ARM_THREAD_STATE64_COUNT; #else #error thread_get_register_pointer_values not defined for this architecture #endif kern_return_t ret = thread_get_state(thread, flavor, (thread_state_t)&state, &count); if (ret != KERN_SUCCESS) { return ret; } // If the provided pointer value is > PAGE_SIZE, add it to the output array // if there's available space. (Values between 0 and PAGE_SIZE are the NULL page // and not valid pointers.) #define push_register_value(p) do { \ if ((uintptr_t)p > PAGE_SIZE) { \ if (out_length < in_length && values) \ values[out_length] = p; \ out_length++; \ } } while (0) #if defined(__i386__) if (sp) { *sp = state.__esp; } push_register_value(state.__eax); push_register_value(state.__ebx); push_register_value(state.__ecx); push_register_value(state.__edx); push_register_value(state.__edi); push_register_value(state.__esi); push_register_value(state.__ebp); #elif defined(__x86_64__) if (sp) { if (state.__rsp > 128) { *sp = state.__rsp - 128 /* redzone */; } else { *sp = 0; } } push_register_value(state.__rax); push_register_value(state.__rbx); push_register_value(state.__rcx); push_register_value(state.__rdx); push_register_value(state.__rdi); push_register_value(state.__rbp); push_register_value(state.__r8); push_register_value(state.__r9); push_register_value(state.__r10); push_register_value(state.__r11); push_register_value(state.__r12); push_register_value(state.__r13); push_register_value(state.__r14); push_register_value(state.__r15); #elif defined(__arm__) if (sp) { *sp = state.__sp; } push_register_value(state.__lr); for (int i = 0; i < 13; i++) { push_register_value(state.__r[i]); } #elif defined(__arm64__) if (sp) { uintptr_t __sp = arm_thread_state64_get_sp(state); if (__sp > 128) { *sp = __sp - 128 /* redzone */; } else { *sp = 0; } } push_register_value(arm_thread_state64_get_lr(state)); for (int i = 0; i < 29; i++) { push_register_value(state.__x[i]); } #else #error thread_get_register_pointer_values not defined for this architecture #endif *length = out_length; if (in_length == 0 || out_length > in_length) { return KERN_INSUFFICIENT_BUFFER_SIZE; } return KERN_SUCCESS; } |