Xcode 8 incorporates the Thread Sanitizer, a tool for detecting race conditions and other threading-related issues.
I'm trying to run this against a project of mine, and am detecting many issues with a third-party binary library. These issues are overwhelming any ones in my own code, and I can't replace the binary library until the vendor fixes it.
How can I suppress these Thread Sanitizer warnings in Xcode for a third-party library?
The Thread Sanitizer, also known as TSan, is an LLVM based tool to audit threading issues in your Swift and C language written code. It was first introduced in Xcode 8 and can be a great tool to find less visible bugs in your code, like data races.
You enable them at build time using the Xcode scheme editor. Select the appropriate scheme for your project and choose Product > Scheme > Edit Scheme to display the scheme editor. Select the Run or Test schemes, navigate to the Diagnostics section, and select the sanitizers you want to run.
Thread Sanitizer (TSan) is a fast data race detector for C/C++ and Rust programs. It uses a compile-time instrumentation to check all non-race-free memory access at runtime.
To enable TSan from the command line, use the following flags: The Thread Sanitizer tool inserts diagnostics into your code to record each memory read or write operation. These diagnostics generate a timestamp for each operation, as well as its location in memory.
Xcode Runtime Issues are reports of programming errors found at run time. Issues can be found by variety of tools, including Address Sanitizer (ASan), Main Thread Checker (MTC), Thread Sanitizer (TSan), and Undefined Behavior Sanitizer (UBSan).
Accessing memory improperly can introduce unexpected issues into your code, and even pose a security threat. The Address Sanitizer tool detects memory-access attempts that don’t belong to an allocated block. To enable this tool, select Address Sanitizer from the Diagnostics section of the appropriate scheme.
Undefined Behavior Sanitizer—The UBSan tool detects divide-by-zero errors, attempts to access memory using a misaligned pointer, and other undefined behaviors. These are LLVM-based tools that add specific checks to your code.
Thread Sanitizer can use suppression files to selectively turn off reporting for problems it detects in libraries outside of your code. To use these with Xcode, first create a file named TSan.supp
(or something similar) and put lines into it like the following:
mutex:Purge
mutex:ProcessBulkInData
mutex:EventDestroy
I was encountering issues with bad mutexes in several internal functions within a particular library, so I suppressed the mutex warnings (the mutex:
part of the above) by providing a substring from the function names that appeared in the Thread Sanitizer stack trace.
Once you have the suppression file done, edit your Run scheme in Xcode and go to the Arguments tab. Under Environment Variables, add the name TSAN_OPTIONS
and give it a value of suppressions=[path_to_TSan.supp]
. The path will need to be relative to your application's binary file in your derived data location.
You may need to run Thread Sanitizer a few times and edit your suppression file to add each of the items from the library you wish to suppress.
The file format and other options of this suppression file can be found on this wiki page. For posterity, these are
thread: suppresses reports related to threads (leaks)
mutex: suppresses reports related to mutexes (destruction of a locked mutex)
signal: suppresses reports related to signal handlers (handler calls malloc())
deadlock: suppresses lock inversion reports
called_from_lib: suppresses all interceptors in a particular library
Thanks go to the anonymous Apple engineer who explained this process in response to a recent bug report.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With