2012-02-09 03:52:01 +08:00
|
|
|
//===-- interception_linux.cc -----------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Linux-specific interception methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-08-08 20:10:08 +08:00
|
|
|
#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
|
2012-08-02 19:19:13 +08:00
|
|
|
#include "interception.h"
|
2012-02-09 03:52:01 +08:00
|
|
|
|
2014-02-24 16:37:41 +08:00
|
|
|
#include <dlfcn.h> // for dlsym() and dlvsym()
|
2012-02-09 03:52:01 +08:00
|
|
|
|
2017-08-08 20:10:08 +08:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
#endif
|
|
|
|
|
2012-02-09 03:52:01 +08:00
|
|
|
namespace __interception {
|
2012-08-02 19:19:13 +08:00
|
|
|
bool GetRealFunctionAddress(const char *func_name, uptr *func_addr,
|
|
|
|
uptr real, uptr wrapper) {
|
2017-08-08 20:10:08 +08:00
|
|
|
#ifdef __NetBSD__
|
|
|
|
// XXX: Find a better way to handle renames
|
|
|
|
if (internal_strcmp(func_name, "sigaction") == 0) func_name = "__sigaction14";
|
|
|
|
#endif
|
2012-08-02 19:19:13 +08:00
|
|
|
*func_addr = (uptr)dlsym(RTLD_NEXT, func_name);
|
2017-11-11 06:09:37 +08:00
|
|
|
if (!*func_addr) {
|
|
|
|
// If the lookup using RTLD_NEXT failed, the sanitizer runtime library is
|
|
|
|
// later in the library search order than the DSO that we are trying to
|
|
|
|
// intercept, which means that we cannot intercept this function. We still
|
|
|
|
// want the address of the real definition, though, so look it up using
|
|
|
|
// RTLD_DEFAULT.
|
|
|
|
*func_addr = (uptr)dlsym(RTLD_DEFAULT, func_name);
|
|
|
|
}
|
2012-05-24 21:54:31 +08:00
|
|
|
return real == wrapper;
|
2012-02-09 03:52:01 +08:00
|
|
|
}
|
2013-09-03 02:06:28 +08:00
|
|
|
|
2013-09-03 15:53:49 +08:00
|
|
|
#if !defined(__ANDROID__) // android does not have dlvsym
|
2013-09-03 02:06:28 +08:00
|
|
|
void *GetFuncAddrVer(const char *func_name, const char *ver) {
|
|
|
|
return dlvsym(RTLD_NEXT, func_name, ver);
|
|
|
|
}
|
2013-09-03 15:53:49 +08:00
|
|
|
#endif // !defined(__ANDROID__)
|
2013-09-03 02:06:28 +08:00
|
|
|
|
2012-02-09 03:52:01 +08:00
|
|
|
} // namespace __interception
|
|
|
|
|
2017-08-08 20:10:08 +08:00
|
|
|
#endif // __linux__ || __FreeBSD__ || __NetBSD__
|