Skip to content

Commit

Permalink
mac: Update knowledge of CFAllocator internals for 10.12
Browse files Browse the repository at this point in the history
For each new major OS version, we need to poke at the internals of
CFAllocator to enable the out-of-memory killer for the default
CFAllocator implementations.

This update is for macOS 10.12 ("Sierra"), whose CFAllocator internals
are unchanged from 10.11 (and from 10.9, and in fact from 10.7). It also
updates the base::mac::IsOS*() family of functions for this new OS
version.

BUG=626536,45650
TEST=base_unittests OutOfMemoryDeathTest.CFAllocator*,MacUtilTest.IsOSEllipsis

Review-Url: https://codereview.chromium.org/2129273002
Cr-Commit-Position: refs/heads/master@{#404471}
  • Loading branch information
markmentovai authored and Commit bot committed Jul 8, 2016
1 parent 4d08afd commit fd1cb64
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 37 deletions.
28 changes: 23 additions & 5 deletions base/mac/mac_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,33 @@ BASE_EXPORT bool RemoveQuarantineAttribute(const FilePath& file_path);
// "OrLater" variants to those that check for a specific version, unless you
// know for sure that you need to check for a specific version.

// Mavericks is Mac OS X 10.9, Darwin 13.
// Mavericks is OS X 10.9, Darwin 13.
BASE_EXPORT bool IsOSMavericks();
BASE_EXPORT bool IsOSMavericksOrLater();

// Yosemite is Mac OS X 10.10, Darwin 14.
// Yosemite is OS X 10.10, Darwin 14.
BASE_EXPORT bool IsOSYosemite();
BASE_EXPORT bool IsOSYosemiteOrEarlier();
BASE_EXPORT bool IsOSYosemiteOrLater();

// El Capitan is Mac OS X 10.11, Darwin 15.
// El Capitan is OS X 10.11, Darwin 15.
BASE_EXPORT bool IsOSElCapitan();
BASE_EXPORT bool IsOSElCapitanOrEarlier();
BASE_EXPORT bool IsOSElCapitanOrLater();

// Sierra is macOS 10.12, Darwin 16.
BASE_EXPORT bool IsOSSierra();
BASE_EXPORT bool IsOSSierraOrLater();

// This should be infrequently used. It only makes sense to use this to avoid
// codepaths that are very likely to break on future (unreleased, untested,
// unborn) OS releases, or to log when the OS is newer than any known version.
BASE_EXPORT bool IsOSLaterThanElCapitan_DontCallThis();
BASE_EXPORT bool IsOSLaterThanSierra_DontCallThis();

// Inline functions that are redundant due to version ranges being mutually-
// exclusive.
inline bool IsOSYosemiteOrEarlier() { return !IsOSElCapitanOrLater(); }
inline bool IsOSElCapitanOrEarlier() { return !IsOSSierraOrLater(); }

// When the deployment target is set, the code produced cannot run on earlier
// OS releases. That enables some of the IsOS* family to be implemented as
Expand Down Expand Up @@ -174,7 +180,19 @@ inline bool IsOSElCapitanOrLater() { return true; }
MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_11
#define BASE_MAC_MAC_UTIL_H_INLINED_GT_10_11
inline bool IsOSElCapitan() { return false; }
inline bool IsOSLaterThanElCapitan_DontCallThis() { return true; }
#endif

#if defined(MAC_OS_X_VERSION_10_12) && \
MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
#define BASE_MAC_MAC_UTIL_H_INLINED_GE_10_12
inline bool IsOSSierraOrLater() { return true; }
#endif

#if defined(MAC_OS_X_VERSION_10_12) && \
MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_12
#define BASE_MAC_MAC_UTIL_H_INLINED_GT_10_12
inline bool IsOSSierra() { return false; }
inline bool IsOSLaterThanSierra_DontCallThis() { return true; }
#endif

// Retrieve the system's model identifier string from the IOKit registry:
Expand Down
34 changes: 16 additions & 18 deletions base/mac/mac_util.mm
Original file line number Diff line number Diff line change
Expand Up @@ -456,28 +456,14 @@ int MacOSXMinorVersion() {
}

enum {
SNOW_LEOPARD_MINOR_VERSION = 6,
LION_MINOR_VERSION = 7,
MOUNTAIN_LION_MINOR_VERSION = 8,
MAVERICKS_MINOR_VERSION = 9,
YOSEMITE_MINOR_VERSION = 10,
EL_CAPITAN_MINOR_VERSION = 11,
SIERRA_MINOR_VERSION = 12,
};

} // namespace

#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_8)
bool IsOSMountainLion() {
return MacOSXMinorVersion() == MOUNTAIN_LION_MINOR_VERSION;
}
#endif

#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_8)
bool IsOSMountainLionOrLater() {
return MacOSXMinorVersion() >= MOUNTAIN_LION_MINOR_VERSION;
}
#endif

#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_9)
bool IsOSMavericks() {
return MacOSXMinorVersion() == MAVERICKS_MINOR_VERSION;
Expand Down Expand Up @@ -514,9 +500,21 @@ bool IsOSElCapitanOrLater() {
}
#endif

#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_11)
bool IsOSLaterThanElCapitan_DontCallThis() {
return MacOSXMinorVersion() > EL_CAPITAN_MINOR_VERSION;
#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_12)
bool IsOSSierra() {
return MacOSXMinorVersion() == SIERRA_MINOR_VERSION;
}
#endif

#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GE_10_12)
bool IsOSSierraOrLater() {
return MacOSXMinorVersion() >= SIERRA_MINOR_VERSION;
}
#endif

#if !defined(BASE_MAC_MAC_UTIL_H_INLINED_GT_10_12)
bool IsOSLaterThanSierra_DontCallThis() {
return MacOSXMinorVersion() > SIERRA_MINOR_VERSION;
}
#endif

Expand Down
29 changes: 25 additions & 4 deletions base/mac/mac_util_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -152,28 +152,49 @@
EXPECT_TRUE(IsOSYosemiteOrEarlier());
EXPECT_FALSE(IsOSYosemiteOrLater());
EXPECT_FALSE(IsOSElCapitan());
EXPECT_TRUE(IsOSElCapitanOrEarlier());
EXPECT_FALSE(IsOSElCapitanOrLater());
EXPECT_FALSE(IsOSLaterThanElCapitan_DontCallThis());
EXPECT_FALSE(IsOSSierra());
EXPECT_FALSE(IsOSSierraOrLater());
EXPECT_FALSE(IsOSLaterThanSierra_DontCallThis());
} else if (minor == 10) {
EXPECT_FALSE(IsOSMavericks());
EXPECT_TRUE(IsOSMavericksOrLater());
EXPECT_TRUE(IsOSYosemite());
EXPECT_TRUE(IsOSYosemiteOrEarlier());
EXPECT_TRUE(IsOSYosemiteOrLater());
EXPECT_FALSE(IsOSElCapitan());
EXPECT_TRUE(IsOSElCapitanOrEarlier());
EXPECT_FALSE(IsOSElCapitanOrLater());
EXPECT_FALSE(IsOSLaterThanElCapitan_DontCallThis());
EXPECT_FALSE(IsOSSierra());
EXPECT_FALSE(IsOSSierraOrLater());
EXPECT_FALSE(IsOSLaterThanSierra_DontCallThis());
} else if (minor == 11) {
EXPECT_FALSE(IsOSMavericks());
EXPECT_TRUE(IsOSMavericksOrLater());
EXPECT_FALSE(IsOSYosemite());
EXPECT_FALSE(IsOSYosemiteOrEarlier());
EXPECT_TRUE(IsOSYosemiteOrLater());
EXPECT_TRUE(IsOSElCapitan());
EXPECT_TRUE(IsOSElCapitanOrEarlier());
EXPECT_TRUE(IsOSElCapitanOrLater());
EXPECT_FALSE(IsOSLaterThanElCapitan_DontCallThis());
EXPECT_FALSE(IsOSSierra());
EXPECT_FALSE(IsOSSierraOrLater());
EXPECT_FALSE(IsOSLaterThanSierra_DontCallThis());
} else if (minor == 12) {
EXPECT_FALSE(IsOSMavericks());
EXPECT_TRUE(IsOSMavericksOrLater());
EXPECT_FALSE(IsOSYosemite());
EXPECT_FALSE(IsOSYosemiteOrEarlier());
EXPECT_TRUE(IsOSYosemiteOrLater());
EXPECT_FALSE(IsOSElCapitan());
EXPECT_FALSE(IsOSElCapitanOrEarlier());
EXPECT_TRUE(IsOSElCapitanOrLater());
EXPECT_TRUE(IsOSSierra());
EXPECT_TRUE(IsOSSierraOrLater());
EXPECT_FALSE(IsOSLaterThanSierra_DontCallThis());
} else {
// Not six, seven, eight, nine, ten, or eleven. Ah, ah, ah.
// Not nine, ten, eleven, or twelve. Ah, ah, ah.
EXPECT_TRUE(false);
}
} else {
Expand Down
15 changes: 5 additions & 10 deletions base/process/memory_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -248,19 +248,14 @@ void oom_killer_new() {
// === Core Foundation CFAllocators ===

bool CanGetContextForCFAllocator() {
return !base::mac::IsOSLaterThanElCapitan_DontCallThis();
return !base::mac::IsOSLaterThanSierra_DontCallThis();
}

CFAllocatorContext* ContextForCFAllocator(CFAllocatorRef allocator) {
if (base::mac::IsOSMavericks() || base::mac::IsOSYosemite() ||
base::mac::IsOSElCapitan()) {
ChromeCFAllocatorLions* our_allocator =
const_cast<ChromeCFAllocatorLions*>(
reinterpret_cast<const ChromeCFAllocatorLions*>(allocator));
return &our_allocator->_context;
} else {
return NULL;
}
ChromeCFAllocatorLions* our_allocator =
const_cast<ChromeCFAllocatorLions*>(
reinterpret_cast<const ChromeCFAllocatorLions*>(allocator));
return &our_allocator->_context;
}

CFAllocatorAllocateCallBack g_old_cfallocator_system_default;
Expand Down

0 comments on commit fd1cb64

Please sign in to comment.