@@ -1195,13 +1195,14 @@ uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
1195
1195
const uptr left_padding =
1196
1196
Max<uptr>(granularity, 1ULL << min_shadow_base_alignment);
1197
1197
1198
- uptr space_size = shadow_size_bytes + left_padding ;
1198
+ uptr space_size = shadow_size_bytes;
1199
1199
1200
1200
uptr largest_gap_found = 0 ;
1201
1201
uptr max_occupied_addr = 0 ;
1202
+
1202
1203
VReport (2 , " FindDynamicShadowStart, space_size = %p\n " , (void *)space_size);
1203
1204
uptr shadow_start =
1204
- FindAvailableMemoryRange (space_size, alignment, granularity ,
1205
+ FindAvailableMemoryRange (space_size, alignment, left_padding ,
1205
1206
&largest_gap_found, &max_occupied_addr);
1206
1207
// If the shadow doesn't fit, restrict the address space to make it fit.
1207
1208
if (shadow_start == 0 ) {
@@ -1221,9 +1222,9 @@ uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
1221
1222
}
1222
1223
RestrictMemoryToMaxAddress (new_max_vm);
1223
1224
high_mem_end = new_max_vm - 1 ;
1224
- space_size = (high_mem_end >> shadow_scale) + left_padding ;
1225
+ space_size = (high_mem_end >> shadow_scale);
1225
1226
VReport (2 , " FindDynamicShadowStart, space_size = %p\n " , (void *)space_size);
1226
- shadow_start = FindAvailableMemoryRange (space_size, alignment, granularity ,
1227
+ shadow_start = FindAvailableMemoryRange (space_size, alignment, left_padding ,
1227
1228
nullptr , nullptr );
1228
1229
if (shadow_start == 0 ) {
1229
1230
Report (" Unable to find a memory range after restricting VM.\n " );
@@ -1264,10 +1265,15 @@ uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
1264
1265
mach_msg_type_number_t count = kRegionInfoSize ;
1265
1266
kr = mach_vm_region_recurse (mach_task_self (), &address, &vmsize, &depth,
1266
1267
(vm_region_info_t )&vminfo, &count);
1267
- if (kr == KERN_INVALID_ADDRESS) {
1268
+
1269
+ // There are cases where going beyond the processes' max vm does
1270
+ // not return KERN_INVALID_ADDRESS so we check for going beyond that
1271
+ // max address as well.
1272
+ if (kr == KERN_INVALID_ADDRESS || address > max_vm_address) {
1268
1273
// No more regions beyond "address", consider the gap at the end of VM.
1269
1274
address = max_vm_address;
1270
1275
vmsize = 0 ;
1276
+ kr = -1 ; // break after this iteration.
1271
1277
} else {
1272
1278
if (max_occupied_addr) *max_occupied_addr = address + vmsize;
1273
1279
}
0 commit comments