0

Move scoped_mach_vm to base/apple, leave a forwarding header

Crashpad is not yet updated for the new location, so leave a
forwarding header to be removed later.

Bug: 1444927
Change-Id: I4bb025adc7734c201ebf53c26a22b9a489f7af42
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4784271
Owners-Override: Avi Drissman <avi@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Avi Drissman <avi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1184434}
This commit is contained in:
Avi Drissman
2023-08-16 22:52:13 +00:00
committed by Chromium LUCI CQ
parent 03e999613b
commit 760566e48a
8 changed files with 124 additions and 102 deletions

@ -1963,6 +1963,8 @@ component("base") {
"apple/scoped_dispatch_object.h",
"apple/scoped_mach_port.cc",
"apple/scoped_mach_port.h",
"apple/scoped_mach_vm.cc",
"apple/scoped_mach_vm.h",
"apple/scoped_nsobject.h",
"apple/scoped_objc_class_swizzler.h",
"apple/scoped_objc_class_swizzler.mm",
@ -1972,8 +1974,6 @@ component("base") {
"files/file_util_apple.mm",
"mac/foundation_util.h",
"mac/foundation_util.mm",
"mac/scoped_mach_vm.cc",
"mac/scoped_mach_vm.h",
"mac/scoped_nsautorelease_pool.cc",
"mac/scoped_nsautorelease_pool.h",
"memory/platform_shared_memory_mapper_apple.cc",
@ -3729,12 +3729,12 @@ test("base_unittests") {
"allocator/partition_allocator/shim/allocator_interception_apple_unittest.mm",
"allocator/partition_allocator/shim/malloc_zone_functions_apple_unittest.cc",
"apple/call_with_eh_frame_unittest.mm",
"apple/scoped_mach_vm_unittest.cc",
"apple/scoped_objc_class_swizzler_unittest.mm",
"enterprise_util_mac_unittest.mm",
"mac/launch_application_unittest.mm",
"mac/mac_util_unittest.mm",
"mac/mach_port_rendezvous_unittest.cc",
"mac/scoped_mach_vm_unittest.cc",
"mac/scoped_sending_event_unittest.mm",
"message_loop/message_pump_apple_unittest.mm",
"power_monitor/thermal_state_observer_mac_unittest.mm",

@ -2,11 +2,11 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/mac/scoped_mach_vm.h"
#include "base/apple/scoped_mach_vm.h"
#include "base/apple/mach_logging.h"
namespace base::mac {
namespace base::apple {
void ScopedMachVM::reset(vm_address_t address, vm_size_t size) {
DCHECK_EQ(address % PAGE_SIZE, 0u);
@ -33,4 +33,4 @@ void ScopedMachVM::reset_unaligned(vm_address_t address, vm_size_t size) {
size_ = size;
}
} // namespace base::mac
} // namespace base::apple

100
base/apple/scoped_mach_vm.h Normal file

@ -0,0 +1,100 @@
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_APPLE_SCOPED_MACH_VM_H_
#define BASE_APPLE_SCOPED_MACH_VM_H_
#include <mach/mach.h>
#include <stddef.h>
#include <algorithm>
#include <utility>
#include "base/base_export.h"
#include "base/check_op.h"
// Use ScopedMachVM to supervise ownership of pages in the current process
// through the Mach VM subsystem. Pages allocated with vm_allocate can be
// released when exiting a scope with ScopedMachVM.
//
// The Mach VM subsystem operates on a page-by-page basis, and a single VM
// allocation managed by a ScopedMachVM object may span multiple pages. As far
// as Mach is concerned, allocated pages may be deallocated individually. This
// is in contrast to higher-level allocators such as malloc, where the base
// address of an allocation implies the size of an allocated block.
// Consequently, it is not sufficient to just pass the base address of an
// allocation to ScopedMachVM, it also needs to know the size of the
// allocation. To avoid any confusion, both the base address and size must
// be page-aligned.
//
// When dealing with Mach VM, base addresses will naturally be page-aligned,
// but user-specified sizes may not be. If there's a concern that a size is
// not page-aligned, use the mach_vm_round_page macro to correct it.
//
// Example:
//
// vm_address_t address = 0;
// vm_size_t size = 12345; // This requested size is not page-aligned.
// kern_return_t kr =
// vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
// if (kr != KERN_SUCCESS) {
// return false;
// }
// ScopedMachVM vm_owner(address, mach_vm_round_page(size));
namespace base::apple {
class BASE_EXPORT ScopedMachVM {
public:
explicit ScopedMachVM(vm_address_t address = 0, vm_size_t size = 0)
: address_(address), size_(size) {
DCHECK_EQ(address % PAGE_SIZE, 0u);
DCHECK_EQ(size % PAGE_SIZE, 0u);
}
ScopedMachVM(const ScopedMachVM&) = delete;
ScopedMachVM& operator=(const ScopedMachVM&) = delete;
~ScopedMachVM() {
if (size_) {
vm_deallocate(mach_task_self(), address_, size_);
}
}
// Resets the scoper to manage a new memory region. Both |address| and |size|
// must be page-aligned. If the new region is a smaller subset of the
// existing region (i.e. the new and old regions overlap), the non-
// overlapping part of the old region is deallocated.
void reset(vm_address_t address = 0, vm_size_t size = 0);
// Like reset() but does not DCHECK that |address| and |size| are page-
// aligned.
void reset_unaligned(vm_address_t address, vm_size_t size);
vm_address_t address() const {
return address_;
}
vm_size_t size() const {
return size_;
}
void swap(ScopedMachVM& that) {
std::swap(address_, that.address_);
std::swap(size_, that.size_);
}
void release() {
address_ = 0;
size_ = 0;
}
private:
vm_address_t address_;
vm_size_t size_;
};
} // namespace base::apple
#endif // BASE_APPLE_SCOPED_MACH_VM_H_

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/mac/scoped_mach_vm.h"
#include "base/apple/scoped_mach_vm.h"
#include <mach/mach.h>
@ -17,7 +17,7 @@
// allocation will report being part of the previously-deallocated large region.
// That will cause the GetRegionInfo() expectations to fail.
namespace base::mac {
namespace base::apple {
namespace {
void GetRegionInfo(vm_address_t* region_address, vm_size_t* region_size) {
@ -228,4 +228,4 @@ TEST(ScopedMachVMTest, ResetMustBeAligned) {
#endif // DCHECK_IS_ON()
} // namespace
} // namespace base::mac
} // namespace base::apple

@ -1,99 +1,21 @@
// Copyright 2014 The Chromium Authors
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_MAC_SCOPED_MACH_VM_H_
#define BASE_MAC_SCOPED_MACH_VM_H_
#include <mach/mach.h>
#include <stddef.h>
#include "base/apple/scoped_mach_vm.h"
#include <algorithm>
#include <utility>
// This is a forwarding header so that Crashpad can continue to build correctly
// until mini_chromium and then it are updated and rolled.
#include "base/base_export.h"
#include "base/check_op.h"
// Use ScopedMachVM to supervise ownership of pages in the current process
// through the Mach VM subsystem. Pages allocated with vm_allocate can be
// released when exiting a scope with ScopedMachVM.
//
// The Mach VM subsystem operates on a page-by-page basis, and a single VM
// allocation managed by a ScopedMachVM object may span multiple pages. As far
// as Mach is concerned, allocated pages may be deallocated individually. This
// is in contrast to higher-level allocators such as malloc, where the base
// address of an allocation implies the size of an allocated block.
// Consequently, it is not sufficient to just pass the base address of an
// allocation to ScopedMachVM, it also needs to know the size of the
// allocation. To avoid any confusion, both the base address and size must
// be page-aligned.
//
// When dealing with Mach VM, base addresses will naturally be page-aligned,
// but user-specified sizes may not be. If there's a concern that a size is
// not page-aligned, use the mach_vm_round_page macro to correct it.
//
// Example:
//
// vm_address_t address = 0;
// vm_size_t size = 12345; // This requested size is not page-aligned.
// kern_return_t kr =
// vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
// if (kr != KERN_SUCCESS) {
// return false;
// }
// ScopedMachVM vm_owner(address, mach_vm_round_page(size));
// TODO(https://crbug.com/1444927): Update mini_chromium, update Crashpad, roll
// Crashpad, and then delete this forwarding header.
namespace base::mac {
class BASE_EXPORT ScopedMachVM {
public:
explicit ScopedMachVM(vm_address_t address = 0, vm_size_t size = 0)
: address_(address), size_(size) {
DCHECK_EQ(address % PAGE_SIZE, 0u);
DCHECK_EQ(size % PAGE_SIZE, 0u);
}
ScopedMachVM(const ScopedMachVM&) = delete;
ScopedMachVM& operator=(const ScopedMachVM&) = delete;
~ScopedMachVM() {
if (size_) {
vm_deallocate(mach_task_self(), address_, size_);
}
}
// Resets the scoper to manage a new memory region. Both |address| and |size|
// must be page-aligned. If the new region is a smaller subset of the
// existing region (i.e. the new and old regions overlap), the non-
// overlapping part of the old region is deallocated.
void reset(vm_address_t address = 0, vm_size_t size = 0);
// Like reset() but does not DCHECK that |address| and |size| are page-
// aligned.
void reset_unaligned(vm_address_t address, vm_size_t size);
vm_address_t address() const {
return address_;
}
vm_size_t size() const {
return size_;
}
void swap(ScopedMachVM& that) {
std::swap(address_, that.address_);
std::swap(size_, that.size_);
}
void release() {
address_ = 0;
size_ = 0;
}
private:
vm_address_t address_;
vm_size_t size_;
};
using ScopedMachVM = base::apple::ScopedMachVM;
} // namespace base::mac

@ -7,7 +7,7 @@
#include <mach/vm_map.h>
#include "base/apple/mach_logging.h"
#include "base/mac/scoped_mach_vm.h"
#include "base/apple/scoped_mach_vm.h"
namespace base::subtle {
@ -78,7 +78,7 @@ bool PlatformSharedMemoryRegion::ConvertToReadOnly(void* mapped_addr) {
apple::ScopedMachSendRight handle_copy(handle_.release());
void* temp_addr = mapped_addr;
mac::ScopedMachVM scoped_memory;
apple::ScopedMachVM scoped_memory;
if (!temp_addr) {
// Intentionally lower current prot and max prot to |VM_PROT_READ|.
kern_return_t kr =

@ -16,13 +16,13 @@
#include "base/apple/mach_logging.h"
#include "base/apple/scoped_mach_port.h"
#include "base/apple/scoped_mach_vm.h"
#include "base/containers/buffer_iterator.h"
#include "base/containers/circular_deque.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/mac/scoped_mach_msg_destroy.h"
#include "base/mac/scoped_mach_vm.h"
#include "base/message_loop/message_pump_for_io.h"
#include "base/task/current_thread.h"
#include "base/task/single_thread_task_runner.h"
@ -650,7 +650,7 @@ class ChannelMac : public Channel,
}
base::span<const char> payload;
base::mac::ScopedMachVM ool_memory;
base::apple::ScopedMachVM ool_memory;
if (transfer_message_ool) {
auto* descriptor = buffer.Object<mach_msg_ool_descriptor_t>();
if (descriptor->type != MACH_MSG_OOL_DESCRIPTOR) {
@ -716,7 +716,7 @@ class ChannelMac : public Channel,
std::unique_ptr<audit_token_t> peer_audit_token_;
// IO buffer for receiving Mach messages. Only accessed on |io_task_runner_|.
base::mac::ScopedMachVM receive_buffer_;
base::apple::ScopedMachVM receive_buffer_;
// Handles that were received with a message that are validated and returned
// in GetReadPlatformHandles(). Only accessed on |io_task_runner_|.
@ -732,7 +732,7 @@ class ChannelMac : public Channel,
// shutdown.
bool reject_writes_ GUARDED_BY(write_lock_) = false;
// IO buffer for sending Mach messages.
base::mac::ScopedMachVM send_buffer_ GUARDED_BY(write_lock_);
base::apple::ScopedMachVM send_buffer_ GUARDED_BY(write_lock_);
// If a message timed out during send in MachMessageSendLocked(), this will
// be true to indicate that |send_buffer_| contains a message that must
// be sent. If this is true, then other calls to Write() queue messages onto

@ -9,8 +9,8 @@
#include <stdint.h>
#include "base/apple/scoped_mach_port.h"
#include "base/apple/scoped_mach_vm.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_mach_vm.h"
#include "base/system/sys_info.h"
#include "services/device/compute_pressure/core_times.h"
@ -47,7 +47,7 @@ bool HostProcessorInfoScanner::Update() {
return false;
}
base::mac::ScopedMachVM vm_owner(
base::apple::ScopedMachVM vm_owner(
reinterpret_cast<vm_address_t>(cpu_infos),
mach_vm_round_page(number_of_processors *
sizeof(processor_cpu_load_info)));