Fix few problems on NetBSD that caused crashes.
- Correct handling death of a thread. Stop using the Linux solution with pthread_key_create(3). This does not work on NetBSD, because calling the thread destructor is not the latest operation on a POSIX thread entity. NetBSD's libpthread still calls at least pthread_mutex_lock and pthread_mutex_unlock. Detect _lwp_exit(2) call as it is really the latest operation called from a detaching POSIX thread.
- Add interceptor for _lwp_exit() on NetBSD-only.
- Disable detection of on_exit() on NetBSD. Introduce TSAN_MAYBE_INTERCEPT_ON_EXIT. It looks like this code fixes build for Darwin.
- Correct handling atexit(3). The NetBSD specific implementation of cxa_atexit() does not preserve the 2nd argument if dso is equal to NULL. Split paths of handling intercepted cxa_atexit() and atexit(3). Add a local stack-like structure to hold the __cxa_atexit() context. atexit(3) is documented in the C standard as calling callback from the earliest to the oldest entry. This path also fixes potential ABI problem of passing an argument to a function from the atexit(3) callback mechanism.
Proposal to change the behavior of __cxa_atexit() in NetBSD has been rejected.
- Introduce new internal allocator: InternalReallocArr() that has been modeled after NetBSD's reallocarr(3) extension to libc. This function is handy to resize/free arrays.
With the above changes, TSan/NetBSD can fully bootstrap into operation.
Sponsored by <The NetBSD Foundation>