Skip to content

Commit

Permalink
PA: Migrate //base/mac/mach_loggin.* into partition_alloc_base/
Browse files Browse the repository at this point in the history
Migrates //base/mac/mach_logging.* into PA's base, and also
removes the deps to //base/mac/mach_logging.* in allocator shims.

PS1 contains pure copies of //base/mac/mach_logging.* into
.../partition_alloc_base/mac/.

Bug: 1337681
Change-Id: I5e2489ca354bc6b1872d06cc1b111112d51c89bb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4587482
Reviewed-by: Takashi Sakamoto <tasak@google.com>
Commit-Queue: Yuki Shiino <yukishiino@chromium.org>
Reviewed-by: Bartek Nowierski <bartekn@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1153694}
  • Loading branch information
yuki3 authored and Chromium LUCI CQ committed Jun 6, 2023
1 parent 8e087a6 commit b83957f
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 9 deletions.
2 changes: 2 additions & 0 deletions base/allocator/partition_allocator/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ component("partition_alloc") {
sources += [
"partition_alloc_base/mac/foundation_util.h",
"partition_alloc_base/mac/foundation_util.mm",
"partition_alloc_base/mac/mach_logging.cc",
"partition_alloc_base/mac/mach_logging.h",
"partition_alloc_base/mac/scoped_cftyperef.h",
"partition_alloc_base/mac/scoped_typeref.h",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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.

#include "base/allocator/partition_allocator/partition_alloc_base/mac/mach_logging.h"

#include <iomanip>
#include <string>

#include "base/allocator/partition_allocator/partition_alloc_base/strings/stringprintf.h"
#include "build/build_config.h"

namespace {

std::string FormatMachErrorNumber(mach_error_t mach_err) {
// For the os/kern subsystem, give the error number in decimal as in
// <mach/kern_return.h>. Otherwise, give it in hexadecimal to make it easier
// to visualize the various bits. See <mach/error.h>.
if (mach_err >= 0 && mach_err < KERN_RETURN_MAX) {
return partition_alloc::internal::base::TruncatingStringPrintf(" (%d)",
mach_err);
}
return partition_alloc::internal::base::TruncatingStringPrintf(" (0x%08x)",
mach_err);
}

} // namespace

namespace partition_alloc::internal::logging {

MachLogMessage::MachLogMessage(const char* file_path,
int line,
LogSeverity severity,
mach_error_t mach_err)
: LogMessage(file_path, line, severity), mach_err_(mach_err) {}

MachLogMessage::~MachLogMessage() {
stream() << ": " << mach_error_string(mach_err_)
<< FormatMachErrorNumber(mach_err_);
}

} // namespace partition_alloc::internal::logging
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_MAC_MACH_LOGGING_H_
#define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_MAC_MACH_LOGGING_H_

#include <mach/mach.h>

#include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
#include "base/allocator/partition_allocator/partition_alloc_base/debug/debugging_buildflags.h"
#include "base/allocator/partition_allocator/partition_alloc_base/logging.h"
#include "build/build_config.h"

// Use the PA_MACH_LOG family of macros along with a mach_error_t
// (kern_return_t) containing a Mach error. The error value will be decoded so
// that logged messages explain the error.
//
// Examples:
//
// kern_return_t kr = mach_timebase_info(&info);
// if (kr != KERN_SUCCESS) {
// PA_MACH_LOG(ERROR, kr) << "mach_timebase_info";
// }
//
// kr = vm_deallocate(task, address, size);
// PA_MACH_DCHECK(kr == KERN_SUCCESS, kr) << "vm_deallocate";

namespace partition_alloc::internal::logging {

class PA_COMPONENT_EXPORT(PARTITION_ALLOC) MachLogMessage
: public partition_alloc::internal::logging::LogMessage {
public:
MachLogMessage(const char* file_path,
int line,
LogSeverity severity,
mach_error_t mach_err);

MachLogMessage(const MachLogMessage&) = delete;
MachLogMessage& operator=(const MachLogMessage&) = delete;

~MachLogMessage() override;

private:
mach_error_t mach_err_;
};

} // namespace partition_alloc::internal::logging

#if BUILDFLAG(PA_DCHECK_IS_ON)
#define PA_MACH_DVLOG_IS_ON(verbose_level) PA_VLOG_IS_ON(verbose_level)
#else
#define PA_MACH_DVLOG_IS_ON(verbose_level) 0
#endif

#define PA_MACH_LOG_STREAM(severity, mach_err) \
PA_COMPACT_GOOGLE_LOG_EX_##severity(MachLogMessage, mach_err).stream()
#define PA_MACH_VLOG_STREAM(verbose_level, mach_err) \
::partition_alloc::internal::logging::MachLogMessage( \
__FILE__, __LINE__, -verbose_level, mach_err) \
.stream()

#define PA_MACH_LOG(severity, mach_err) \
PA_LAZY_STREAM(PA_MACH_LOG_STREAM(severity, mach_err), PA_LOG_IS_ON(severity))
#define PA_MACH_LOG_IF(severity, condition, mach_err) \
PA_LAZY_STREAM(PA_MACH_LOG_STREAM(severity, mach_err), \
PA_LOG_IS_ON(severity) && (condition))

#define PA_MACH_VLOG(verbose_level, mach_err) \
PA_LAZY_STREAM(PA_MACH_VLOG_STREAM(verbose_level, mach_err), \
PA_VLOG_IS_ON(verbose_level))
#define PA_MACH_VLOG_IF(verbose_level, condition, mach_err) \
PA_LAZY_STREAM(PA_MACH_VLOG_STREAM(verbose_level, mach_err), \
PA_VLOG_IS_ON(verbose_level) && (condition))

#define PA_MACH_CHECK(condition, mach_err) \
PA_LAZY_STREAM(PA_MACH_LOG_STREAM(FATAL, mach_err), !(condition)) \
<< "Check failed: " #condition << ". "

#define PA_MACH_DLOG(severity, mach_err) \
PA_LAZY_STREAM(PA_MACH_LOG_STREAM(severity, mach_err), \
PA_DLOG_IS_ON(severity))
#define PA_MACH_DLOG_IF(severity, condition, mach_err) \
PA_LAZY_STREAM(PA_MACH_LOG_STREAM(severity, mach_err), \
PA_DLOG_IS_ON(severity) && (condition))

#define PA_MACH_DVLOG(verbose_level, mach_err) \
PA_LAZY_STREAM(PA_MACH_VLOG_STREAM(verbose_level, mach_err), \
PA_MACH_DVLOG_IS_ON(verbose_level))
#define PA_MACH_DVLOG_IF(verbose_level, condition, mach_err) \
PA_LAZY_STREAM(PA_MACH_VLOG_STREAM(verbose_level, mach_err), \
PA_MACH_DVLOG_IS_ON(verbose_level) && (condition))

#define PA_MACH_DCHECK(condition, mach_err) \
PA_LAZY_STREAM(PA_MACH_LOG_STREAM(FATAL, mach_err), \
BUILDFLAG(PA_DCHECK_IS_ON) && !(condition)) \
<< "Check failed: " #condition << ". "

#endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_ALLOC_BASE_MAC_MACH_LOGGING_H_
1 change: 0 additions & 1 deletion base/allocator/partition_allocator/shim/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ include_rules = [
"+base/allocator/partition_allocator/partition_alloc_base",
"+base/base_export.h",
"+base/logging.h",
"+base/mac/mach_logging.h",
"+base/synchronization/lock.h",
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@

#include "base/allocator/partition_allocator/oom.h"
#include "base/allocator/partition_allocator/partition_alloc_base/bits.h"
#include "base/allocator/partition_allocator/partition_alloc_base/mac/mach_logging.h"
#include "base/allocator/partition_allocator/partition_alloc_buildflags.h"
#include "base/allocator/partition_allocator/partition_alloc_check.h"
#include "base/allocator/partition_allocator/shim/malloc_zone_functions_mac.h"
#include "base/allocator/partition_allocator/third_party/apple_apsl/CFBase.h"
#include "base/logging.h"
#include "base/mac/mach_logging.h"
#include "build/build_config.h"

#if BUILDFLAG(IS_IOS)
Expand Down Expand Up @@ -79,7 +79,7 @@ bool DeprotectMallocZone(ChromeMallocZone* default_zone,
VM_REGION_BASIC_INFO_64,
reinterpret_cast<vm_region_info_t>(&info), &count, &unused);
if (result != KERN_SUCCESS) {
MACH_LOG(ERROR, result) << "vm_region_64";
PA_MACH_LOG(ERROR, result) << "vm_region_64";
return false;
}

Expand Down Expand Up @@ -114,7 +114,7 @@ bool DeprotectMallocZone(ChromeMallocZone* default_zone,
vm_protect(mach_task_self(), *reprotection_start, *reprotection_length,
false, info.protection | VM_PROT_WRITE);
if (result != KERN_SUCCESS) {
MACH_LOG(ERROR, result) << "vm_protect";
PA_MACH_LOG(ERROR, result) << "vm_protect";
return false;
}
}
Expand Down Expand Up @@ -617,7 +617,7 @@ void ReplaceZoneFunctions(ChromeMallocZone* zone,
kern_return_t result =
vm_protect(mach_task_self(), reprotection_start, reprotection_length,
false, reprotection_value);
MACH_DCHECK(result == KERN_SUCCESS, result) << "vm_protect";
PA_MACH_DCHECK(result == KERN_SUCCESS, result) << "vm_protect";
}
}

Expand Down
4 changes: 2 additions & 2 deletions base/allocator/partition_allocator/shim/allocator_shim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
#if BUILDFLAG(IS_APPLE)
#include <malloc/malloc.h>

#include "base/allocator/partition_allocator/partition_alloc_base/mac/mach_logging.h"
#include "base/allocator/partition_allocator/shim/allocator_interception_mac.h"
#include "base/mac/mach_logging.h"
#endif

#if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
Expand Down Expand Up @@ -135,7 +135,7 @@ void TryFreeDefaultFallbackToFindZoneAndFree(void* ptr) {
vm_address_t* zones = nullptr;
kern_return_t result =
malloc_get_all_zones(mach_task_self(), nullptr, &zones, &zone_count);
MACH_CHECK(result == KERN_SUCCESS, result) << "malloc_get_all_zones";
PA_MACH_CHECK(result == KERN_SUCCESS, result) << "malloc_get_all_zones";

// "find_zone_and_free" expected by try_free_default.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "base/allocator/early_zone_registration_mac.h"
#include "base/allocator/partition_allocator/partition_alloc_base/bits.h"
#include "base/allocator/partition_allocator/partition_alloc_base/mac/mach_logging.h"
#include "base/allocator/partition_allocator/partition_alloc_constants.h"
#include "base/logging.h"

Expand Down Expand Up @@ -215,7 +216,7 @@ malloc_zone_t* GetDefaultMallocZone() {
vm_address_t* zones = nullptr;
kern_return_t result =
malloc_get_all_zones(mach_task_self(), nullptr, &zones, &zone_count);
MACH_CHECK(result == KERN_SUCCESS, result) << "malloc_get_all_zones";
PA_MACH_CHECK(result == KERN_SUCCESS, result) << "malloc_get_all_zones";
return reinterpret_cast<malloc_zone_t*>(zones[0]);
}

Expand Down Expand Up @@ -251,7 +252,7 @@ bool IsAlreadyRegistered() {
// (in libmalloc).
kern_return_t result =
malloc_get_all_zones(mach_task_self(), nullptr, &zones, &zone_count);
MACH_CHECK(result == KERN_SUCCESS, result) << "malloc_get_all_zones";
PA_MACH_CHECK(result == KERN_SUCCESS, result) << "malloc_get_all_zones";
// Checking all the zones, in case someone registered their own zone on top of
// our own.
for (unsigned int i = 0; i < zone_count; i++) {
Expand Down

0 comments on commit b83957f

Please sign in to comment.