Skip to content

Commit f97a609

Browse files
author
Julian Lettner
committed
[Darwin] Add and adopt a way to query the Darwin kernel version
This applies the learnings from [1]. What I intended as a simple cleanup made me realize that the compiler-rt version checks have two separate issues: 1) In some places (e.g., mmap flag setting) what matters is the kernel version, not the OS version. 2) OS version checks are implemented by querying the kernel version. This is not necessarily correct inside the simulators if the simulator runtime isn't aligned with the host macOS. This commit tackles 1) by adopting a separate query function for the Darwin kernel version. 2) (and cleanups) will be dealt with in follow-ups. [1] https://reviews.llvm.org/D78942 rdar://63031937 Reviewed By: delcypher Differential Revision: https://reviews.llvm.org/D79965
1 parent 6c27c61 commit f97a609

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -635,11 +635,7 @@ MacosVersion GetMacosVersionInternal() {
635635
case 14: return MACOS_VERSION_YOSEMITE;
636636
case 15: return MACOS_VERSION_EL_CAPITAN;
637637
case 16: return MACOS_VERSION_SIERRA;
638-
case 17:
639-
// Not a typo, 17.5 Darwin Kernel Version maps to High Sierra 10.13.4.
640-
if (minor >= 5)
641-
return MACOS_VERSION_HIGH_SIERRA_DOT_RELEASE_4;
642-
return MACOS_VERSION_HIGH_SIERRA;
638+
case 17: return MACOS_VERSION_HIGH_SIERRA;
643639
case 18: return MACOS_VERSION_MOJAVE;
644640
case 19: return MACOS_VERSION_CATALINA;
645641
default:
@@ -660,6 +656,23 @@ MacosVersion GetMacosVersion() {
660656
return result;
661657
}
662658

659+
DarwinKernelVersion GetDarwinKernelVersion() {
660+
char buf[100];
661+
size_t len = sizeof(buf);
662+
int res = internal_sysctlbyname("kern.osrelease", buf, &len, nullptr, 0);
663+
CHECK_EQ(res, 0);
664+
665+
// Format: <major>.<minor>.<patch>\0
666+
CHECK_GE(len, 6);
667+
const char *p = buf;
668+
u16 major = internal_simple_strtoll(p, &p, /*base=*/10);
669+
CHECK_EQ(*p, '.');
670+
p += 1;
671+
u16 minor = internal_simple_strtoll(p, &p, /*base=*/10);
672+
673+
return DarwinKernelVersion(major, minor);
674+
}
675+
663676
uptr GetRSS() {
664677
struct task_basic_info info;
665678
unsigned count = TASK_BASIC_INFO_COUNT;
@@ -796,10 +809,10 @@ void SignalContext::InitPcSpBp() {
796809
}
797810

798811
void InitializePlatformEarly() {
799-
// Only use xnu_fast_mmap when on x86_64 and the OS supports it.
812+
// Only use xnu_fast_mmap when on x86_64 and the kernel supports it.
800813
use_xnu_fast_mmap =
801814
#if defined(__x86_64__)
802-
GetMacosVersion() >= MACOS_VERSION_HIGH_SIERRA_DOT_RELEASE_4;
815+
GetDarwinKernelVersion() >= DarwinKernelVersion(17, 5);
803816
#else
804817
false;
805818
#endif

compiler-rt/lib/sanitizer_common/sanitizer_mac.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,28 @@ enum MacosVersion {
4040
MACOS_VERSION_EL_CAPITAN,
4141
MACOS_VERSION_SIERRA,
4242
MACOS_VERSION_HIGH_SIERRA,
43-
MACOS_VERSION_HIGH_SIERRA_DOT_RELEASE_4,
4443
MACOS_VERSION_MOJAVE,
4544
MACOS_VERSION_CATALINA,
4645
MACOS_VERSION_UNKNOWN_NEWER
4746
};
4847

48+
struct DarwinKernelVersion {
49+
u16 major;
50+
u16 minor;
51+
52+
DarwinKernelVersion(u16 major, u16 minor) : major(major), minor(minor) {}
53+
54+
bool operator==(const DarwinKernelVersion &other) const {
55+
return major == other.major && minor == other.minor;
56+
}
57+
bool operator>=(const DarwinKernelVersion &other) const {
58+
return major >= other.major ||
59+
(major == other.major && minor >= other.minor);
60+
}
61+
};
62+
4963
MacosVersion GetMacosVersion();
64+
DarwinKernelVersion GetDarwinKernelVersion();
5065

5166
char **GetEnviron();
5267

0 commit comments

Comments
 (0)