Correct handling of the TLS/NetBSD block of the main program

Summary:
Include <sys/tls.h> for:

 - struct tls_tcb - thread control block structure
 - __HAVE___LWP_GETTCB_FAST - __lwp_gettcb_fast() is available
 - __HAVE___LWP_GETPRIVATE_FAST -  __lwp_getprivate_fast() is available
 - __HAVE_TLS_VARIANT_I - TLS Variant I for this architecture
 - __HAVE_TLS_VARIANT_II - TLS Variant II for this architecture

Rename ThreadSelfSegbase() to ThreadSelfTlsTcb and switch it
to retrieve in a portable way TCB.

Switch ThreadSelf() to retrieve pthread from struct tcb_tls.

Use dl_iterate_phdr() to find out the size of TLS block of
the main program.

Correct the index of the  TLS block of the main program
(dlpi_tls_modid); it's 1, not 2.

New NetBSD code is now CPU (NetBSD port) agnostic.

Stop sharing the same code with FreeBSD.

Sponsored by <The NetBSD Foundation>

Reviewers: dvyukov, joerg, vitalybuka

Reviewed By: vitalybuka

Subscribers: emaste, kubamracek, llvm-commits, #sanitizers

Tags: #sanitizers

Differential Revision: https://reviews.llvm.org/D40159

llvm-svn: 320234
This commit is contained in:
Kamil Rytarowski 2017-12-09 02:23:33 +00:00
parent ababedd0b0
commit f6510a01a6
1 changed files with 47 additions and 3 deletions

View File

@ -43,6 +43,7 @@
#if SANITIZER_NETBSD
#include <sys/sysctl.h>
#include <sys/tls.h>
#endif
#if SANITIZER_LINUX
@ -327,7 +328,7 @@ uptr ThreadSelf() {
}
#endif // (x86_64 || i386 || MIPS) && SANITIZER_LINUX
#if SANITIZER_FREEBSD || SANITIZER_NETBSD
#if SANITIZER_FREEBSD
static void **ThreadSelfSegbase() {
void **segbase = 0;
# if defined(__i386__)
@ -345,7 +346,36 @@ static void **ThreadSelfSegbase() {
uptr ThreadSelf() {
return (uptr)ThreadSelfSegbase()[2];
}
#endif // SANITIZER_FREEBSD || SANITIZER_NETBSD
#endif // SANITIZER_FREEBSD
#if SANITIZER_NETBSD
static struct tls_tcb * ThreadSelfTlsTcb() {
struct tls_tcb * tcb;
# ifdef __HAVE___LWP_GETTCB_FAST
tcb = (struct tls_tcb *)__lwp_gettcb_fast();
# elif defined(__HAVE___LWP_GETPRIVATE_FAST)
tcb = (struct tls_tcb *)__lwp_getprivate_fast();
# endif
return tcb;
}
uptr ThreadSelf() {
return (uptr)ThreadSelfTlsTcb()->tcb_pthread;
}
int GetSizeFromHdr(struct dl_phdr_info *info, size_t size, void *data) {
const Elf_Phdr *hdr = info->dlpi_phdr;
const Elf_Phdr *last_hdr = hdr + info->dlpi_phnum;
for (; hdr != last_hdr; ++hdr) {
if (hdr->p_type == PT_TLS && info->dlpi_tls_modid == 1) {
*(uptr*)data = hdr->p_memsz;
break;
}
}
return 0;
}
#endif // SANITIZER_NETBSD
#if !SANITIZER_GO
static void GetTls(uptr *addr, uptr *size) {
@ -363,7 +393,7 @@ static void GetTls(uptr *addr, uptr *size) {
*addr = 0;
*size = 0;
# endif
#elif SANITIZER_FREEBSD || SANITIZER_NETBSD
#elif SANITIZER_FREEBSD
void** segbase = ThreadSelfSegbase();
*addr = 0;
*size = 0;
@ -376,6 +406,20 @@ static void GetTls(uptr *addr, uptr *size) {
*addr = (uptr) dtv[2];
*size = (*addr == 0) ? 0 : ((uptr) segbase[0] - (uptr) dtv[2]);
}
#elif SANITIZER_NETBSD
struct tls_tcb * const tcb = ThreadSelfTlsTcb();
*addr = 0;
*size = 0;
if (tcb != 0) {
// Find size (p_memsz) of dlpi_tls_modid 1 (TLS block of the main program).
// ld.elf_so hardcodes the index 1.
dl_iterate_phdr(GetSizeFromHdr, size);
if (*size != 0) {
// The block has been found and tcb_dtv[1] contains the base address
*addr = (uptr)tcb->tcb_dtv[1];
}
}
#elif SANITIZER_ANDROID
*addr = 0;
*size = 0;