Skip to content

[compiler-rt] Replace deprecated os_trace calls on mac #138908

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

DanBlackwell
Copy link

Currently there are deprecation warnings suppressed for os_trace; this patch replaces all uses with os_log_error.

rdar://140295247

Copy link

github-actions bot commented May 7, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Dan Blackwell (DanBlackwell)

Changes

Currently there are deprecation warnings suppressed for os_trace; this patch replaces all uses with os_log_error.

rdar://140295247


Full diff: https://github.com/llvm/llvm-project/pull/138908.diff

1 Files Affected:

  • (modified) compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp (+20-24)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index 0b8a75391136d..f02f78c4e19b4 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
@@ -844,30 +844,26 @@ void LogMessageOnPrintf(const char *str) {
 
 void LogFullErrorReport(const char *buffer) {
 #if !SANITIZER_GO
-  // Log with os_trace. This will make it into the crash log.
-#if SANITIZER_OS_TRACE
-#pragma clang diagnostic push
-// os_trace is deprecated.
-#pragma clang diagnostic ignored "-Wdeprecated-declarations"
-  if (GetMacosAlignedVersion() >= MacosVersion(10, 10)) {
-    // os_trace requires the message (format parameter) to be a string literal.
-    if (internal_strncmp(SanitizerToolName, "AddressSanitizer",
-                         sizeof("AddressSanitizer") - 1) == 0)
-      os_trace("Address Sanitizer reported a failure.");
-    else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer",
-                              sizeof("UndefinedBehaviorSanitizer") - 1) == 0)
-      os_trace("Undefined Behavior Sanitizer reported a failure.");
-    else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer",
-                              sizeof("ThreadSanitizer") - 1) == 0)
-      os_trace("Thread Sanitizer reported a failure.");
-    else
-      os_trace("Sanitizer tool reported a failure.");
-
-    if (common_flags()->log_to_syslog)
-      os_trace("Consult syslog for more information.");
-  }
-#pragma clang diagnostic pop
-#endif
+#  if SANITIZER_OS_TRACE
+   // Log with os_log_error. This will make it into the crash log.
+   if (GetMacosAlignedVersion() >= MacosVersion(10, 10)) {
+     if (internal_strncmp(SanitizerToolName, "AddressSanitizer",
+                          sizeof("AddressSanitizer") - 1) == 0)
+       os_log_error(OS_LOG_DEFAULT, "Address Sanitizer reported a failure.");
+     else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer",
+                               sizeof("UndefinedBehaviorSanitizer") - 1) == 0)
+     os_log_error(OS_LOG_DEFAULT,
+                  "Undefined Behavior Sanitizer reported a failure.");
+     else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer",
+                               sizeof("ThreadSanitizer") - 1) == 0)
+       os_log_error(OS_LOG_DEFAULT, "Thread Sanitizer reported a failure.");
+     else
+       os_log_error(OS_LOG_DEFAULT, "Sanitizer tool reported a failure.");
+
+     if (common_flags()->log_to_syslog)
+       os_log_error(OS_LOG_DEFAULT, "Consult syslog for more information.");
+   }
+#  endif // SANITIZER_OS_TRACE
 
   // Log to syslog.
   // The logging on OS X may call pthread_create so we need the threading

Copy link
Contributor

@wrotki wrotki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thetruestblue thetruestblue requested review from yln and thetruestblue May 8, 2025 16:48
#endif
# if SANITIZER_OS_TRACE
// Log with os_log_error. This will make it into the crash log.
if (GetMacosAlignedVersion() >= MacosVersion(10, 10)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we test this on 10.10? Is this the proper version for the new call?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've now bumped the check to 10.12, but also the minimum supported macOS for sanitizers looks to be 10.13 anyway:

set(DEFAULT_SANITIZER_MIN_OSX_VERSION 10.13)
.

@thetruestblue
Copy link
Contributor

I think the right thing here may be to find out when the deprecation occurred and call the new calls after that version, but maintain the os_trace calls for the older versions.

Copy link

github-actions bot commented May 9, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff HEAD~1 HEAD --extensions cpp -- compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
View the diff from clang-format here.
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
index c1a4d5ac3..d9ff0c961 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
@@ -844,26 +844,26 @@ void LogMessageOnPrintf(const char *str) {
 
 void LogFullErrorReport(const char *buffer) {
 #if !SANITIZER_GO
-#  if SANITIZER_OS_TRACE
-   // Log with os_log_error. This will make it into the crash log.
-   if (GetMacosAlignedVersion() >= MacosVersion(10, 12)) {
-     if (internal_strncmp(SanitizerToolName, "AddressSanitizer",
-                          sizeof("AddressSanitizer") - 1) == 0)
-       os_log_error(OS_LOG_DEFAULT, "Address Sanitizer reported a failure.");
-     else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer",
-                               sizeof("UndefinedBehaviorSanitizer") - 1) == 0)
-     os_log_error(OS_LOG_DEFAULT,
-                  "Undefined Behavior Sanitizer reported a failure.");
-     else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer",
-                               sizeof("ThreadSanitizer") - 1) == 0)
-       os_log_error(OS_LOG_DEFAULT, "Thread Sanitizer reported a failure.");
-     else
-       os_log_error(OS_LOG_DEFAULT, "Sanitizer tool reported a failure.");
-
-     if (common_flags()->log_to_syslog)
-       os_log_error(OS_LOG_DEFAULT, "Consult syslog for more information.");
-   }
-#  endif // SANITIZER_OS_TRACE
+#    if SANITIZER_OS_TRACE
+  // Log with os_log_error. This will make it into the crash log.
+  if (GetMacosAlignedVersion() >= MacosVersion(10, 12)) {
+    if (internal_strncmp(SanitizerToolName, "AddressSanitizer",
+                         sizeof("AddressSanitizer") - 1) == 0)
+      os_log_error(OS_LOG_DEFAULT, "Address Sanitizer reported a failure.");
+    else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer",
+                              sizeof("UndefinedBehaviorSanitizer") - 1) == 0)
+      os_log_error(OS_LOG_DEFAULT,
+                   "Undefined Behavior Sanitizer reported a failure.");
+    else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer",
+                              sizeof("ThreadSanitizer") - 1) == 0)
+      os_log_error(OS_LOG_DEFAULT, "Thread Sanitizer reported a failure.");
+    else
+      os_log_error(OS_LOG_DEFAULT, "Sanitizer tool reported a failure.");
+
+    if (common_flags()->log_to_syslog)
+      os_log_error(OS_LOG_DEFAULT, "Consult syslog for more information.");
+  }
+#    endif  // SANITIZER_OS_TRACE
 
   // Log to syslog.
   // The logging on OS X may call pthread_create so we need the threading

@DanBlackwell
Copy link
Author

I think the right thing here may be to find out when the deprecation occurred and call the new calls after that version, but maintain the os_trace calls for the older versions.

As noted, it seems that these new os_log_error calls are available in all supported macOS above the minimum version required for sanitizer support:

set(DEFAULT_SANITIZER_MIN_OSX_VERSION 10.13)
.

Copy link
Collaborator

@yln yln left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good cleanup, thanks!

I have a few nits.

// os_trace requires the message (format parameter) to be a string literal.
# if SANITIZER_OS_TRACE
// Log with os_log_error. This will make it into the crash log.
if (GetMacosAlignedVersion() >= MacosVersion(10, 12)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems that these new os_log_error calls are available in all supported macOS above the minimum version required for sanitizer support: ma

Could we completely omit the check then? No point in trying to preserve runtime compatibility with < macOS 10.13.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have removed the check now.

if (GetMacosAlignedVersion() >= MacosVersion(10, 10)) {
// os_trace requires the message (format parameter) to be a string literal.
# if SANITIZER_OS_TRACE
// Log with os_log_error. This will make it into the crash log.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you haven't, please double-check this is still true with the new API.
Is there any difference in crash log format?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behaviour seems identical - note that these changes only affect the single message that gets output on crash. The actual Asan crash report is unaffected.

#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (GetMacosAlignedVersion() >= MacosVersion(10, 10)) {
// os_trace requires the message (format parameter) to be a string literal.
# if SANITIZER_OS_TRACE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now also remove the SANITIZER_OS_TRACE macro and #include <os/trace.h> above.

This file already includes #include <os/log.h>.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@yln yln assigned yln and DanBlackwell and unassigned yln May 12, 2025
@DanBlackwell DanBlackwell requested a review from yln May 13, 2025 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants