[compiler-rt] 44e7d44 - [Darwin][compiler-rt] kern.osproductversion unavailable pre macOS 10.13

Julian Lettner via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 29 17:22:18 PDT 2020


Author: Julian Lettner
Date: 2020-07-29T17:18:10-07:00
New Revision: 44e7d449973948ddaa5f7c84a56dd79f8bb82e43

URL: https://github.com/llvm/llvm-project/commit/44e7d449973948ddaa5f7c84a56dd79f8bb82e43
DIFF: https://github.com/llvm/llvm-project/commit/44e7d449973948ddaa5f7c84a56dd79f8bb82e43.diff

LOG: [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

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index 5b06fee62c13..a10ba774b955 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));
 }


        


More information about the llvm-commits mailing list