[compiler-rt] [sanitizer-common] Improve mach_vm_region_recurse error handling (PR #158670)
Mariusz Borsa via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 16 17:01:20 PDT 2025
================
@@ -1265,17 +1265,34 @@ uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
kr = mach_vm_region_recurse(mach_task_self(), &address, &vmsize, &depth,
(vm_region_info_t)&vminfo, &count);
- // There are cases where going beyond the processes' max vm does
- // not return KERN_INVALID_ADDRESS so we check for going beyond that
- // max address as well.
- if (kr == KERN_INVALID_ADDRESS || address > max_vm_address) {
+ if (kr == KERN_SUCCESS) {
+ // There are cases where going beyond the processes' max vm does
+ // not return KERN_INVALID_ADDRESS so we check for going beyond that
+ // max address as well.
+ if (address > max_vm_address) {
+ address = max_vm_address;
+ kr = -1; // break after this iteration.
+ }
+
+ if (max_occupied_addr)
+ *max_occupied_addr = address + vmsize;
+ } else if (kr == KERN_INVALID_ADDRESS) {
// No more regions beyond "address", consider the gap at the end of VM.
address = max_vm_address;
- vmsize = 0;
- kr = -1; // break after this iteration.
+
+ // We will break after this iteration anyway since kr != KERN_SUCCESS
+ } else if (kr == KERN_DENIED) {
----------------
wrotki wrote:
I would move this part up, to follow the 'early return/exit/Die' principle, otherwise I find it a bit harder to read (else as applied to what condition(s)? Once KERN_DENIED handling is out of the way, the following happy path logic is easier to read (there's one less } else if () {
https://github.com/llvm/llvm-project/pull/158670
More information about the llvm-commits
mailing list