[Sanitizers] Get link map on FreeBSD and NetBSD via documented API

Summary:
Instead of hand-crafting an offset into the structure returned by
dlopen(3) to get at the link map, use the documented API.  This is
described in dlinfo(3): by calling it with `RTLD_DI_LINKMAP`, the
dynamic linker ensures the right address is returned.

This is a recommit of 92e267a94dc4272511be674062f8a3e8897b7083, with
dlinfo(3) expliclity being referenced only for FreeBSD, non-Android
Linux, NetBSD and Solaris.  Other OSes will have to add their own
implementation.

Reviewers: devnexen, emaste, MaskRay, krytarowski

Reviewed By: krytarowski

Subscribers: krytarowski, vitalybuka, #sanitizers, llvm-commits

Tags: #sanitizers, #llvm

Differential Revision: https://reviews.llvm.org/D73990
This commit is contained in:
Dimitry Andric 2020-02-10 23:43:12 +01:00
parent bf65f19bce
commit 52f2df1ecd
11 changed files with 54 additions and 15 deletions

View File

@ -66,6 +66,10 @@ uptr internal_getpid() {
return pid;
}
int internal_dlinfo(void *handle, int request, void *p) {
UNIMPLEMENTED();
}
uptr GetThreadSelf() { return reinterpret_cast<uptr>(thrd_current()); }
tid_t GetTid() { return GetThreadSelf(); }

View File

@ -72,6 +72,8 @@ unsigned int internal_sleep(unsigned int seconds);
uptr internal_getpid();
uptr internal_getppid();
int internal_dlinfo(void *handle, int request, void *p);
// Threading
uptr internal_sched_yield();

View File

@ -735,6 +735,15 @@ uptr internal_getppid() {
return internal_syscall(SYSCALL(getppid));
}
int internal_dlinfo(void *handle, int request, void *p) {
#if SANITIZER_FREEBSD || (SANITIZER_LINUX && !SANITIZER_ANDROID) || \
SANITIZER_SOLARIS
return dlinfo(handle, request, p);
#else
UNIMPLEMENTED();
#endif
}
uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
#if SANITIZER_FREEBSD
return internal_syscall(SYSCALL(getdirentries), fd, (uptr)dirp, count, NULL);

View File

@ -208,6 +208,10 @@ uptr internal_getpid() {
return getpid();
}
int internal_dlinfo(void *handle, int request, void *p) {
UNIMPLEMENTED();
}
int internal_sigaction(int signum, const void *act, void *oldact) {
return sigaction(signum,
(const struct sigaction *)act, (struct sigaction *)oldact);

View File

@ -265,6 +265,11 @@ uptr internal_getppid() {
return _REAL(getppid);
}
int internal_dlinfo(void *handle, int request, void *p) {
DEFINE__REAL(int, dlinfo, void *a, int b, void *c);
return _REAL(dlinfo, handle, request, p);
}
uptr internal_getdents(fd_t fd, void *dirp, unsigned int count) {
DEFINE__REAL(int, __getdents30, int a, void *b, size_t c);
return _REAL(__getdents30, fd, dirp, count);

View File

@ -52,6 +52,7 @@
#include <netinet/ip_mroute.h>
//
#include <dirent.h>
#include <dlfcn.h>
#include <fstab.h>
#include <fts.h>
#include <glob.h>
@ -86,9 +87,15 @@
// Include these after system headers to avoid name clashes and ambiguities.
#include "sanitizer_internal_defs.h"
#include "sanitizer_libc.h"
#include "sanitizer_platform_limits_freebsd.h"
namespace __sanitizer {
void *__sanitizer_get_link_map_by_dlopen_handle(void *handle) {
void *p = nullptr;
return internal_dlinfo(handle, RTLD_DI_LINKMAP, &p) == 0 ? p : nullptr;
}
unsigned struct_cap_rights_sz = sizeof(cap_rights_t);
unsigned struct_utsname_sz = sizeof(struct utsname);
unsigned struct_stat_sz = sizeof(struct stat);

View File

@ -20,15 +20,15 @@
#include "sanitizer_platform.h"
#include "sanitizer_platform_limits_posix.h"
// FreeBSD's dlopen() returns a pointer to an Obj_Entry structure that
// incorporates the map structure.
#define GET_LINK_MAP_BY_DLOPEN_HANDLE(handle) \
((link_map *)((handle) == nullptr ? nullptr : ((char *)(handle) + 560)))
// Get sys/_types.h, because that tells us whether 64-bit inodes are
// used in struct dirent below.
#include <sys/_types.h>
namespace __sanitizer {
void *__sanitizer_get_link_map_by_dlopen_handle(void *handle);
#define GET_LINK_MAP_BY_DLOPEN_HANDLE(handle) \
(link_map *)__sanitizer_get_link_map_by_dlopen_handle(handle)
extern unsigned struct_utsname_sz;
extern unsigned struct_stat_sz;
#if defined(__powerpc64__)

View File

@ -213,6 +213,7 @@ struct urio_command {
#include <dev/wscons/wsdisplay_usl_io.h>
#include <fs/autofs/autofs_ioctl.h>
#include <dirent.h>
#include <dlfcn.h>
#include <glob.h>
#include <grp.h>
#include <ifaddrs.h>
@ -258,9 +259,15 @@ struct urio_command {
// Include these after system headers to avoid name clashes and ambiguities.
#include "sanitizer_internal_defs.h"
#include "sanitizer_libc.h"
#include "sanitizer_platform_limits_netbsd.h"
namespace __sanitizer {
void *__sanitizer_get_link_map_by_dlopen_handle(void* handle) {
void *p = nullptr;
return internal_dlinfo(handle, RTLD_DI_LINKMAP, &p) == 0 ? p : nullptr;
}
unsigned struct_utsname_sz = sizeof(struct utsname);
unsigned struct_stat_sz = sizeof(struct stat);
unsigned struct_rusage_sz = sizeof(struct rusage);

View File

@ -19,18 +19,11 @@
#include "sanitizer_internal_defs.h"
#include "sanitizer_platform.h"
#define _GET_LINK_MAP_BY_DLOPEN_HANDLE(handle, shift) \
((link_map *)((handle) == nullptr ? nullptr : ((char *)(handle) + (shift))))
#if defined(__x86_64__)
#define GET_LINK_MAP_BY_DLOPEN_HANDLE(handle) \
_GET_LINK_MAP_BY_DLOPEN_HANDLE(handle, 264)
#elif defined(__i386__)
#define GET_LINK_MAP_BY_DLOPEN_HANDLE(handle) \
_GET_LINK_MAP_BY_DLOPEN_HANDLE(handle, 136)
#endif
namespace __sanitizer {
void *__sanitizer_get_link_map_by_dlopen_handle(void *handle);
# define GET_LINK_MAP_BY_DLOPEN_HANDLE(handle) \
(link_map *)__sanitizer_get_link_map_by_dlopen_handle(handle)
extern unsigned struct_utsname_sz;
extern unsigned struct_stat_sz;
extern unsigned struct_rusage_sz;

View File

@ -49,6 +49,10 @@ uptr internal_getpid() {
return getpid();
}
int internal_dlinfo(void *handle, int request, void *p) {
UNIMPLEMENTED();
}
bool FileExists(const char *filename) {
struct stat st;
if (stat(filename, &st))

View File

@ -94,6 +94,10 @@ uptr internal_getpid() {
return GetProcessId(GetCurrentProcess());
}
int internal_dlinfo(void *handle, int request, void *p) {
UNIMPLEMENTED();
}
// In contrast to POSIX, on Windows GetCurrentThreadId()
// returns a system-unique identifier.
tid_t GetTid() {