From 44e7d449973948ddaa5f7c84a56dd79f8bb82e43 Mon Sep 17 00:00:00 2001 From: Julian Lettner Date: Wed, 29 Jul 2020 12:50:49 -0700 Subject: [PATCH] [Darwin][compiler-rt] kern.osproductversion unavailable pre macOS 10.13 Add a fallback for `sysctl kern.osproductversion` for XNU 17 (macOS 10.13) and below, which do not provide this property. Unfortunately, this means we have to take the detour via Darwin kernel version again (at least for the fallback). Reviewed By: delcypher Differential Revision: https://reviews.llvm.org/D84892 --- compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp index 5b06fee..a10ba77 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp +++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp @@ -606,6 +606,14 @@ HandleSignalMode GetHandleSignalMode(int signum) { return result; } +// Offset example: +// XNU 17 -- macOS 10.13 -- iOS 11 -- tvOS 11 -- watchOS 4 +constexpr u16 GetOSMajorKernelOffset() { + if (TARGET_OS_OSX) return 4; + if (SANITIZER_IOS || SANITIZER_TVOS) return 6; + if (SANITIZER_WATCHOS) return 13; +} + using VersStr = char[64]; static void GetOSVersion(VersStr vers) { @@ -621,7 +629,18 @@ static void GetOSVersion(VersStr vers) { } else { int res = internal_sysctlbyname("kern.osproductversion", vers, &len, nullptr, 0); - CHECK_EQ(res, 0); + if (res) { + // Fallback for XNU 17 (macOS 10.13) and below that do not provide the + // `kern.osproductversion` property. + u16 kernel_major = GetDarwinKernelVersion().major; + u16 offset = GetOSMajorKernelOffset(); + CHECK_LE(kernel_major, 17); + CHECK_GE(kernel_major, offset); + u16 os_major = kernel_major - offset; + + auto format = TARGET_OS_OSX ? "10.%d" : "%d.0"; + len = internal_snprintf(vers, len, format, os_major); + } } CHECK_LT(len, sizeof(VersStr)); } -- 2.7.4