14#include "KittyUtils.hpp"
35 RegionInfo(uintptr_t s, uintptr_t e,
bool r,
bool w,
bool x)
36 : start(s), end(e), readable(r), writable(w), executable(x)
40 inline bool canMergeWith(
const RegionInfo &other)
const
42 return end == other.start && readable == other.readable && writable == other.writable &&
43 executable == other.executable;
47 std::vector<RegionInfo> cachedRegions_;
48 const mach_port_t task_;
49 const size_t page_size_;
51 size_t last_region_index_;
53 bool _findRegion(uintptr_t addr, RegionInfo *region);
57 : task_(mach_task_self()), page_size_(sysconf(_SC_PAGESIZE)), use_cache_(false), last_region_index_(0)
67 KittyPtrValidator(mach_port_t task,
bool use_cache)
68 : task_(task), page_size_(sysconf(_SC_PAGESIZE)), use_cache_(use_cache), last_region_index_(0)
81 use_cache_ = use_cache;
84 cachedRegions_.clear();
85 last_region_index_ = 0;
100 bool isPtrReadable(uintptr_t ptr,
size_t len =
sizeof(
void *));
109 bool isPtrWritable(uintptr_t ptr,
size_t len =
sizeof(
void *));
130 RegionInfo region(0, 0,
false,
false,
false);
131 return _findRegion(ptr, ®ion);
134 inline bool isPtrReadable(
const void *ptr,
size_t len =
sizeof(
void *))
138 inline bool isPtrWritable(
const void *ptr,
size_t len =
sizeof(
void *))
142 inline bool isPtrExecutable(
const void *ptr,
size_t len =
sizeof(
void *))
156 cachedRegions_.clear();
157 last_region_index_ = 0;
172 return cachedRegions_;
183class KittyPtrValidator
194 RegionInfo(uintptr_t s, uintptr_t e,
bool r,
bool w,
bool x)
195 : start(s), end(e), readable(r), writable(w), executable(x)
199 inline bool canMergeWith(
const RegionInfo &other)
const
201 return end == other.start && readable == other.readable && writable == other.writable &&
202 executable == other.executable;
206 std::vector<RegionInfo> cachedRegions_;
208 const size_t page_size_;
210 size_t last_region_index_;
212 bool _parseMapsLine(
const char *line, RegionInfo *region);
214 bool _findRegion(uintptr_t addr, RegionInfo *region);
217 KittyPtrValidator() : pid_(getpid()), page_size_(sysconf(_SC_PAGESIZE)), use_cache_(
false), last_region_index_(0)
228 : pid_(pid), page_size_(sysconf(_SC_PAGESIZE)), use_cache_(use_cache), last_region_index_(0)
241 use_cache_ = use_cache;
244 cachedRegions_.clear();
245 last_region_index_ = 0;
260 cachedRegions_.clear();
261 last_region_index_ = 0;
308 RegionInfo region(0, 0,
false,
false,
false);
309 return _findRegion(ptr, ®ion);
312 inline bool isPtrReadable(
const void *ptr,
size_t len =
sizeof(
void *))
316 inline bool isPtrWritable(
const void *ptr,
size_t len =
sizeof(
void *))
320 inline bool isPtrExecutable(
const void *ptr,
size_t len =
sizeof(
void *))
334 cachedRegions_.clear();
335 last_region_index_ = 0;
350 return cachedRegions_;
Validates the memory access rights for a given address range.
Definition KittyPtrValidator.hpp:184
bool isPtrReadable(uintptr_t ptr, size_t len=sizeof(void *))
Checks if a pointer is readable.
void clearCache()
Clears the cached region information.
Definition KittyPtrValidator.hpp:332
void setUseCache(bool use_cache)
Sets whether to use cached region information.
Definition KittyPtrValidator.hpp:239
void refreshRegionCache()
Refreshes the cached region information.
std::vector< RegionInfo > cachedRegions() const
Retrieves the cached region information.
Definition KittyPtrValidator.hpp:348
bool isPtrWritable(uintptr_t ptr, size_t len=sizeof(void *))
Checks if a pointer is writable.
void setPID(pid_t pid)
Sets the process ID to query memory regions.
Definition KittyPtrValidator.hpp:258
bool isPtrExecutable(uintptr_t ptr, size_t len=sizeof(void *))
Checks if a pointer is executable.
bool isPtrInAddressSpace(uintptr_t ptr)
Checks if a pointer is within the address space of the current process.
Definition KittyPtrValidator.hpp:303
KittyPtrValidator(pid_t pid, bool use_cache)
Constructs a KittyPtrValidator object with the specified process ID and whether to use a cache.
Definition KittyPtrValidator.hpp:227
uintptr_t untagPointer(uintptr_t ptr)
Removes AArch64 top-byte pointer tags from an address.
Definition KittyUtils.hpp:474