OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process/memory.h" | 5 #include "base/process/memory.h" |
6 | 6 |
7 #include <CoreFoundation/CoreFoundation.h> | 7 #include <CoreFoundation/CoreFoundation.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <mach/mach.h> | 9 #include <mach/mach.h> |
10 #include <mach/mach_vm.h> | 10 #include <mach/mach_vm.h> |
11 #include <malloc/malloc.h> | 11 #include <malloc/malloc.h> |
12 #import <objc/runtime.h> | 12 #import <objc/runtime.h> |
13 | 13 |
14 #include <new> | 14 #include <new> |
15 | 15 |
16 #include "base/lazy_instance.h" | 16 #include "base/lazy_instance.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/mac/mac_util.h" | 18 #include "base/mac/mac_util.h" |
| 19 #include "base/mac/mach_logging.h" |
19 #include "base/scoped_clear_errno.h" | 20 #include "base/scoped_clear_errno.h" |
20 #include "third_party/apple_apsl/CFBase.h" | 21 #include "third_party/apple_apsl/CFBase.h" |
21 #include "third_party/apple_apsl/malloc.h" | 22 #include "third_party/apple_apsl/malloc.h" |
22 | 23 |
23 #if ARCH_CPU_32_BITS | 24 #if ARCH_CPU_32_BITS |
24 #include <dlfcn.h> | 25 #include <dlfcn.h> |
25 #include <mach-o/nlist.h> | 26 #include <mach-o/nlist.h> |
26 | 27 |
27 #include "base/threading/thread_local.h" | 28 #include "base/threading/thread_local.h" |
28 #include "third_party/mach_override/mach_override.h" | 29 #include "third_party/mach_override/mach_override.h" |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 struct vm_region_basic_info_64 info; | 216 struct vm_region_basic_info_64 info; |
216 mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64; | 217 mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64; |
217 kern_return_t result = | 218 kern_return_t result = |
218 mach_vm_region(mach_task_self(), | 219 mach_vm_region(mach_task_self(), |
219 reprotection_start, | 220 reprotection_start, |
220 reprotection_length, | 221 reprotection_length, |
221 VM_REGION_BASIC_INFO_64, | 222 VM_REGION_BASIC_INFO_64, |
222 reinterpret_cast<vm_region_info_t>(&info), | 223 reinterpret_cast<vm_region_info_t>(&info), |
223 &count, | 224 &count, |
224 &unused); | 225 &unused); |
225 CHECK(result == KERN_SUCCESS); | 226 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_region"; |
226 | 227 |
227 result = mach_port_deallocate(mach_task_self(), unused); | 228 // The kernel always returns a null object for VM_REGION_BASIC_INFO_64, but |
228 CHECK(result == KERN_SUCCESS); | 229 // balance it with a deallocate in case this ever changes. See 10.9.2 |
| 230 // xnu-2422.90.20/osfmk/vm/vm_map.c vm_map_region. |
| 231 mach_port_deallocate(mach_task_self(), unused); |
229 | 232 |
230 // Does the region fully enclose the zone pointers? Possibly unwarranted | 233 // Does the region fully enclose the zone pointers? Possibly unwarranted |
231 // simplification used: using the size of a full version 8 malloc zone rather | 234 // simplification used: using the size of a full version 8 malloc zone rather |
232 // than the actual smaller size if the passed-in zone is not version 8. | 235 // than the actual smaller size if the passed-in zone is not version 8. |
233 CHECK(*reprotection_start <= | 236 CHECK(*reprotection_start <= |
234 reinterpret_cast<mach_vm_address_t>(default_zone)); | 237 reinterpret_cast<mach_vm_address_t>(default_zone)); |
235 mach_vm_size_t zone_offset = reinterpret_cast<mach_vm_size_t>(default_zone) - | 238 mach_vm_size_t zone_offset = reinterpret_cast<mach_vm_size_t>(default_zone) - |
236 reinterpret_cast<mach_vm_size_t>(*reprotection_start); | 239 reinterpret_cast<mach_vm_size_t>(*reprotection_start); |
237 CHECK(zone_offset + sizeof(ChromeMallocZone) <= *reprotection_length); | 240 CHECK(zone_offset + sizeof(ChromeMallocZone) <= *reprotection_length); |
238 | 241 |
239 if (info.protection & VM_PROT_WRITE) { | 242 if (info.protection & VM_PROT_WRITE) { |
240 // No change needed; the zone is already writable. | 243 // No change needed; the zone is already writable. |
241 *reprotection_start = 0; | 244 *reprotection_start = 0; |
242 *reprotection_length = 0; | 245 *reprotection_length = 0; |
243 *reprotection_value = VM_PROT_NONE; | 246 *reprotection_value = VM_PROT_NONE; |
244 } else { | 247 } else { |
245 *reprotection_value = info.protection; | 248 *reprotection_value = info.protection; |
246 result = mach_vm_protect(mach_task_self(), | 249 result = mach_vm_protect(mach_task_self(), |
247 *reprotection_start, | 250 *reprotection_start, |
248 *reprotection_length, | 251 *reprotection_length, |
249 false, | 252 false, |
250 info.protection | VM_PROT_WRITE); | 253 info.protection | VM_PROT_WRITE); |
251 CHECK(result == KERN_SUCCESS); | 254 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect"; |
252 } | 255 } |
253 } | 256 } |
254 | 257 |
255 // === C malloc/calloc/valloc/realloc/posix_memalign === | 258 // === C malloc/calloc/valloc/realloc/posix_memalign === |
256 | 259 |
257 typedef void* (*malloc_type)(struct _malloc_zone_t* zone, | 260 typedef void* (*malloc_type)(struct _malloc_zone_t* zone, |
258 size_t size); | 261 size_t size); |
259 typedef void* (*calloc_type)(struct _malloc_zone_t* zone, | 262 typedef void* (*calloc_type)(struct _malloc_zone_t* zone, |
260 size_t num_items, | 263 size_t num_items, |
261 size_t size); | 264 size_t size); |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
639 } | 642 } |
640 | 643 |
641 // Restore protection if it was active. | 644 // Restore protection if it was active. |
642 | 645 |
643 if (default_reprotection_start) { | 646 if (default_reprotection_start) { |
644 kern_return_t result = mach_vm_protect(mach_task_self(), | 647 kern_return_t result = mach_vm_protect(mach_task_self(), |
645 default_reprotection_start, | 648 default_reprotection_start, |
646 default_reprotection_length, | 649 default_reprotection_length, |
647 false, | 650 false, |
648 default_reprotection_value); | 651 default_reprotection_value); |
649 CHECK(result == KERN_SUCCESS); | 652 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect"; |
650 } | 653 } |
651 | 654 |
652 if (purgeable_reprotection_start) { | 655 if (purgeable_reprotection_start) { |
653 kern_return_t result = mach_vm_protect(mach_task_self(), | 656 kern_return_t result = mach_vm_protect(mach_task_self(), |
654 purgeable_reprotection_start, | 657 purgeable_reprotection_start, |
655 purgeable_reprotection_length, | 658 purgeable_reprotection_length, |
656 false, | 659 false, |
657 purgeable_reprotection_value); | 660 purgeable_reprotection_value); |
658 CHECK(result == KERN_SUCCESS); | 661 MACH_CHECK(result == KERN_SUCCESS, result) << "mach_vm_protect"; |
659 } | 662 } |
660 #endif | 663 #endif |
661 | 664 |
662 // === C malloc_zone_batch_malloc === | 665 // === C malloc_zone_batch_malloc === |
663 | 666 |
664 // batch_malloc is omitted because the default malloc zone's implementation | 667 // batch_malloc is omitted because the default malloc zone's implementation |
665 // only supports batch_malloc for "tiny" allocations from the free list. It | 668 // only supports batch_malloc for "tiny" allocations from the free list. It |
666 // will fail for allocations larger than "tiny", and will only allocate as | 669 // will fail for allocations larger than "tiny", and will only allocate as |
667 // many blocks as it's able to from the free list. These factors mean that it | 670 // many blocks as it's able to from the free list. These factors mean that it |
668 // can return less than the requested memory even in a non-out-of-memory | 671 // can return less than the requested memory even in a non-out-of-memory |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
737 @selector(allocWithZone:)); | 740 @selector(allocWithZone:)); |
738 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( | 741 g_old_allocWithZone = reinterpret_cast<allocWithZone_t>( |
739 method_getImplementation(orig_method)); | 742 method_getImplementation(orig_method)); |
740 CHECK(g_old_allocWithZone) | 743 CHECK(g_old_allocWithZone) |
741 << "Failed to get allocWithZone allocation function."; | 744 << "Failed to get allocWithZone allocation function."; |
742 method_setImplementation(orig_method, | 745 method_setImplementation(orig_method, |
743 reinterpret_cast<IMP>(oom_killer_allocWithZone)); | 746 reinterpret_cast<IMP>(oom_killer_allocWithZone)); |
744 } | 747 } |
745 | 748 |
746 } // namespace base | 749 } // namespace base |
OLD | NEW |