Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress Thread Sanitizer warnings in Xcode from an external library?

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?

like image 990
Brad Larson Avatar asked Jul 07 '16 17:07

Brad Larson


People also ask

What is thread sanitizer in Xcode?

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.

Where is thread sanitizer in Xcode?

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.

What is thread sanitizer?

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.

How do I enable thread sanitizer from the command line?

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.

What are runtime issues in Xcode?

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).

How do I enable the address sanitizer tool?

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.

What is undefined behavior sanitizer in C++?

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.


1 Answers

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.

like image 189
Brad Larson Avatar answered Sep 19 '22 19:09

Brad Larson