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 @@ -607,8 +607,45 @@ return result; } +struct os_system_version { + unsigned int major; + unsigned int minor; + unsigned int patch; +}; + +// Available from macOS 10.12 +extern "C" SANITIZER_WEAK_ATTRIBUTE +int os_system_version_get_current_version(os_system_version *vers); + +#if TARGET_OS_OSX +// Format: 10.. +# define MACOS_ALIGNED_VERSION(vers) vers.minor +#else +// Format: .. +# define MACOS_ALIGNED_VERSION(vers) (vers.major - MACOS_VERSION_OFFSET) +# if TARGET_OS_IOS || TARGET_OS_TV +# define MACOS_VERSION_OFFSET 2 +# elif TARGET_OS_WATCH +# define MACOS_VERSION_OFFSET 9 +# else +# error "Unsupported platform" +# endif +#endif + static MacosVersion GetMacosAlignedVersionInternal() { - u16 macos_major = GetDarwinKernelVersion().major - 4; + if (!os_system_version_get_current_version) { + // Approximate OS version by querying the kernel version. This is not + // correct when executing inside the simulators if the simulator runtime is + // not aligned with the host macOS version. + u16 macos_major = GetDarwinKernelVersion().major - 4; + return MacosVersion(10, macos_major); + } + + os_system_version os_version; + int res = os_system_version_get_current_version(&os_version); + CHECK_EQ(res, 0); + + u16 macos_major = MACOS_ALIGNED_VERSION(os_version); return MacosVersion(10, macos_major); }