This is an archive of the discontinued LLVM Phabricator instance.

[Darwin] Improve runtime OS version checks
AbandonedPublic

Authored by yln on Apr 27 2020, 9:47 AM.

Details

Summary

Use a struct to represent numerical versions instead of encoding release
names in an enumeration. This avoids the need to extend the enumeration
every time there is a new release.

Diff Detail

Event Timeline

yln created this revision.Apr 27 2020, 9:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2020, 9:47 AM
Herald added subscribers: Restricted Project, jfb. · View Herald Transcript
delcypher requested changes to this revision.Apr 28 2020, 3:57 PM

LGTM except for a few minor nits.

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
623

If we're going to use a CHECK here this should be CHECK_EQ so that we get a better error message if the check fails.

This is also a behaviour change. Are we happy with faulting here where previously we would just return MACOS_VERSION_UNKNOWN?

637

Nice cleanup!

compiler-rt/lib/sanitizer_common/sanitizer_mac.h
56

Nit: I'm wondering if we should rename GetMacosVersion() given then we use it on platforms that are not macOS? GetMacosAlignedVersion() would be a more accurate name. I know you didn't change this line but given you're having to touch all call sites now would be a good time to re-consider how this function is named.

This revision now requires changes to proceed.Apr 28 2020, 3:57 PM
yln updated this revision to Diff 260839.Apr 28 2020, 10:37 PM

GetMacosVersion() -> GetMacosAlignedVersion(). Use CHECK_EQ.

yln marked an inline comment as done.Apr 28 2020, 10:48 PM
yln added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
623

This is also a behaviour change. Are we happy with faulting here where previously we would just return MACOS_VERSION_UNKNOWN?

Yes, I think so. This asserts that the version string follows the expected format. Note that we already assert on the system call above CHECK_NE(internal_sysctl(...), -1);. Considering this my best guess is that MACOS_VERSION_UNKNOWN is an artifact of the old implementation via enum+switch: there should be a value for the default case.

kubamracek added inline comments.May 1 2020, 9:42 AM
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
630

This makes it easy(-ier) to make a mistake if for some reason some new code tries to check for 10.13.6 - it won't work (this code claims all 10.13.4+ versions are 10.13.4).

kubamracek added inline comments.May 1 2020, 9:43 AM
compiler-rt/lib/sanitizer_common/sanitizer_mac.h
43

Drop this TODO. There's no "bug" in this code and I don't think we need to make notes for us to adopt new language feature we can't use today.

Overall LGTM - would be great to hear feedback from @eugenis though.

yln edited the summary of this revision. (Show Details)May 1 2020, 10:19 AM
yln marked an inline comment as done.May 1 2020, 10:28 AM
yln added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
630

Good catch! I am assumoing this special mapping should apply for kernel 17.5, as follows:

kernel -> macOS
17.4 -> 10.13.4
17.5 -> 10.13.4
17.6 -> 10.13.6
delcypher added inline comments.May 1 2020, 11:06 AM
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
637

@yln Random thought. Could you check the code that clang generates here? I don't know how clang implements the locking here and I'd like to be sure it doesn't generate code that could be dangerous for us or introduce a new runtime dependency.

yln updated this revision to Diff 261600.May 1 2020, 6:34 PM
yln marked an inline comment as done.
   // Kernel version 17.5 maps to High Sierra 10.13.4.
-  if (major == 17 && minor >= 5)
+  if (major == 17 && minor == 5)

Drop TODO.

yln marked an inline comment as done.May 4 2020, 12:14 PM
yln added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
637

C++ local statics use __cxa_guard_acquire/release/abort [1]. The implementation in the runtime has different locking strategies; currently a global mutex, in the future "futex"es. So there might be an interaction with TSan. There is a test in the Tsan test suite [1] (still passes for me).

[1] https://github.com/llvm/llvm-project/blob/c8ac29ab1d798f16999541cd9f08d58ebaa1b4c1/libcxxabi/src/cxa_guard_impl.h#L546
[2] https://github.com/llvm/llvm-project/blob/master/compiler-rt/test/tsan/cxa_guard_acquire.cpp#L1

yln marked an inline comment as done.May 4 2020, 12:19 PM
yln added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
637

Scrap it. I have decided that this change is not worth the risk (possible TSan interactions). I will revert this part to minimize the risk (and diff) of this patch. Thanks Dan for pointing this out!

yln updated this revision to Diff 261916.May 4 2020, 12:57 PM

Avoid local static var due to potential interactions with TSan.

delcypher requested changes to this revision.May 6 2020, 12:43 PM

LGTM except the issue with 10.13.5.

compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
630

@yln

kernel -> macOS
17.4 -> 10.13.4
17.5 -> 10.13.4
17.6 -> 10.13.6

This mapping sounds odd (mapping two different kernel versions to the same macOS version). Can you verify this is correct? It looks like GetMacosAlignedVersion() cannot return MacosVersion(10, 13, 5) even if the host OS was 10.13.5. That doesn't sound right.

This revision now requires changes to proceed.May 6 2020, 12:43 PM
yln abandoned this revision.May 14 2020, 12:48 PM