| 3 | #ifdef __APPLE__ |
| 4 | |
| 5 | bool KittyPtrValidator::_findRegion(uintptr_t addr, RegionInfo *region) |
| 6 | { |
| 7 | if (!use_cache_) |
| 8 | { |
| 9 | vm_address_t address = addr & ~(page_size_ - 1); |
| 10 | vm_size_t size = 0; |
| 11 | natural_t nesting_depth = 0; |
| 12 | vm_region_submap_short_info_data_64_t info{}; |
| 13 | mach_msg_type_number_t info_count = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64; |
| 14 | kern_return_t kret = vm_region_recurse_64(task_, |
| 15 | &address, |
| 16 | &size, |
| 17 | &nesting_depth, |
| 18 | (vm_region_recurse_info_t)&info, |
| 19 | &info_count); |
| 20 | if (kret != KERN_SUCCESS) |
| 21 | return false; |
| 22 | |
| 23 | bool readable = (info.protection & VM_PROT_READ) != 0; |
| 24 | bool writable = (info.protection & VM_PROT_WRITE) != 0; |
| 25 | bool executable = (info.protection & VM_PROT_EXECUTE) != 0; |
| 26 | *region = RegionInfo(address, address + size, readable, writable, executable); |
| 27 | return address <= addr && addr < address + size; |
| 28 | } |
| 29 | |
| 30 | if (!cachedRegions_.empty()) |
| 31 | { |
| 32 | if (last_region_index_ < cachedRegions_.size() && cachedRegions_[last_region_index_].start <= addr && |
| 33 | addr < cachedRegions_[last_region_index_].end) |
| 34 | { |
| 35 | *region = cachedRegions_[last_region_index_]; |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | size_t left = 0; |
| 40 | size_t right = cachedRegions_.size(); |
| 41 | size_t best_match = right; |
| 42 | |
| 43 | while (left < right) |
| 44 | { |
| 45 | size_t mid = left + (right - left) / 2; |
| 46 | if (cachedRegions_[mid].end <= addr) |
| 47 | { |
| 48 | left = mid + 1; |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | best_match = mid; |
| 53 | right = mid; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if (best_match < cachedRegions_.size() && cachedRegions_[best_match].start <= addr && |
| 58 | addr < cachedRegions_[best_match].end) |
| 59 | { |
| 60 | last_region_index_ = best_match; |
| 61 | *region = cachedRegions_[best_match]; |
| 62 | return true; |
nothing calls this directly
no test coverage detected