16

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?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • Here we have two cases; 1) Thread Sanitizer spawns stacks of useless warnings in which case the tool should be tossed; or 2) the warnings are real, in which case there are again two cases 2a) you can place the appropriate mutexes and therefore should do so, or 2b) *in which case the library must be tossed if the vendor won't fix it*. Race conditions in a library that you can't guard against means that library is not fit for multithreaded code at all. – Joshua Aug 01 '16 at 02:04
  • Do not ignore race condition or potential heap corruption issues because you can't replace the library. That way lies utter madness. – Joshua Aug 01 '16 at 02:05

1 Answers1

21

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.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571