diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.h b/compiler-rt/lib/sanitizer_common/sanitizer_mac.h --- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.h @@ -40,13 +40,28 @@ MACOS_VERSION_EL_CAPITAN, MACOS_VERSION_SIERRA, MACOS_VERSION_HIGH_SIERRA, - MACOS_VERSION_HIGH_SIERRA_DOT_RELEASE_4, MACOS_VERSION_MOJAVE, MACOS_VERSION_CATALINA, MACOS_VERSION_UNKNOWN_NEWER }; +struct DarwinKernelVersion { + u16 major; + u16 minor; + + DarwinKernelVersion(u16 major, u16 minor) : major(major), minor(minor) {} + + bool operator==(const DarwinKernelVersion &other) const { + return major == other.major && minor == other.minor; + } + bool operator>=(const DarwinKernelVersion &other) const { + return major >= other.major || + (major == other.major && minor >= other.minor); + } +}; + MacosVersion GetMacosVersion(); +DarwinKernelVersion GetDarwinKernelVersion(); char **GetEnviron(); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp --- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp @@ -635,11 +635,7 @@ case 14: return MACOS_VERSION_YOSEMITE; case 15: return MACOS_VERSION_EL_CAPITAN; case 16: return MACOS_VERSION_SIERRA; - case 17: - // Not a typo, 17.5 Darwin Kernel Version maps to High Sierra 10.13.4. - if (minor >= 5) - return MACOS_VERSION_HIGH_SIERRA_DOT_RELEASE_4; - return MACOS_VERSION_HIGH_SIERRA; + case 17: return MACOS_VERSION_HIGH_SIERRA; case 18: return MACOS_VERSION_MOJAVE; case 19: return MACOS_VERSION_CATALINA; default: @@ -660,6 +656,23 @@ return result; } +DarwinKernelVersion GetDarwinKernelVersion() { + char buf[100]; + size_t len = sizeof(buf); + int res = internal_sysctlbyname("kern.osrelease", buf, &len, nullptr, 0); + CHECK_EQ(res, 0); + + // Format: ..\0 + CHECK_GE(len, 6); + const char *p = buf; + u16 major = internal_simple_strtoll(p, &p, /*base=*/10); + CHECK_EQ(*p, '.'); + p += 1; + u16 minor = internal_simple_strtoll(p, &p, /*base=*/10); + + return DarwinKernelVersion(major, minor); +} + uptr GetRSS() { struct task_basic_info info; unsigned count = TASK_BASIC_INFO_COUNT; @@ -796,10 +809,10 @@ } void InitializePlatformEarly() { - // Only use xnu_fast_mmap when on x86_64 and the OS supports it. + // Only use xnu_fast_mmap when on x86_64 and the kernel supports it. use_xnu_fast_mmap = #if defined(__x86_64__) - GetMacosVersion() >= MACOS_VERSION_HIGH_SIERRA_DOT_RELEASE_4; + GetDarwinKernelVersion() >= DarwinKernelVersion(17, 5); #else false; #endif