[NFC][Sanitizer] Extract GetFuncAddr from GetRealFunctionAddress

Summary:
Hopefully, this will enable cleanup/removal of GetRealFunctionAddress in
follow-up commits.

Subscribers: #sanitizers, llvm-commits

Tags: #sanitizers, #llvm

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

llvm-svn: 359213
This commit is contained in:
Julian Lettner
2019-04-25 17:46:29 +00:00
parent c19f4f8069
commit 8b36610bfa
3 changed files with 20 additions and 9 deletions

View File

@@ -35,27 +35,32 @@ static int StrCmp(const char *s1, const char *s2) {
bool GetRealFunctionAddress(const char *func_name, uptr *func_addr, bool GetRealFunctionAddress(const char *func_name, uptr *func_addr,
uptr real, uptr wrapper) { uptr real, uptr wrapper) {
*func_addr = (uptr)GetFuncAddr(func_name);
return real == wrapper;
}
void *GetFuncAddr(const char *name) {
#if SANITIZER_NETBSD #if SANITIZER_NETBSD
// FIXME: Find a better way to handle renames // FIXME: Find a better way to handle renames
if (StrCmp(func_name, "sigaction")) if (StrCmp(name, "sigaction"))
func_name = "__sigaction14"; name = "__sigaction14";
#endif #endif
*func_addr = (uptr)dlsym(RTLD_NEXT, func_name); void *addr = dlsym(RTLD_NEXT, name);
if (!*func_addr) { if (!addr) {
// If the lookup using RTLD_NEXT failed, the sanitizer runtime library is // 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 // 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 // intercept, which means that we cannot intercept this function. We still
// want the address of the real definition, though, so look it up using // want the address of the real definition, though, so look it up using
// RTLD_DEFAULT. // RTLD_DEFAULT.
*func_addr = (uptr)dlsym(RTLD_DEFAULT, func_name); addr = dlsym(RTLD_DEFAULT, name);
} }
return real == wrapper; return addr;
} }
// Android and Solaris do not have dlvsym // Android and Solaris do not have dlvsym
#if !SANITIZER_ANDROID && !SANITIZER_SOLARIS && !SANITIZER_OPENBSD #if !SANITIZER_ANDROID && !SANITIZER_SOLARIS && !SANITIZER_OPENBSD
void *GetFuncAddrVer(const char *func_name, const char *ver) { void *GetFuncAddrVer(const char *name, const char *ver) {
return dlvsym(RTLD_NEXT, func_name, ver); return dlvsym(RTLD_NEXT, name, ver);
} }
#endif // !SANITIZER_ANDROID #endif // !SANITIZER_ANDROID

View File

@@ -25,7 +25,8 @@ namespace __interception {
// returns true if a function with the given name was found. // returns true if a function with the given name was found.
bool GetRealFunctionAddress(const char *func_name, uptr *func_addr, bool GetRealFunctionAddress(const char *func_name, uptr *func_addr,
uptr real, uptr wrapper); uptr real, uptr wrapper);
void *GetFuncAddrVer(const char *func_name, const char *ver); void *GetFuncAddr(const char *name);
void *GetFuncAddrVer(const char *name, const char *ver);
} // namespace __interception } // namespace __interception
#define INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func) \ #define INTERCEPT_FUNCTION_LINUX_OR_FREEBSD(func) \

View File

@@ -44,6 +44,11 @@ TEST(Interception, GetRealFunctionAddress) {
EXPECT_EQ(0U, dummy_address); EXPECT_EQ(0U, dummy_address);
} }
TEST(Interception, GetFuncAddr) {
EXPECT_NE(GetFuncAddr("malloc"), nullptr);
EXPECT_EQ(GetFuncAddr("does_not_exist"), nullptr);
}
TEST(Interception, Basic) { TEST(Interception, Basic) {
ASSERT_TRUE(INTERCEPT_FUNCTION(isdigit)); ASSERT_TRUE(INTERCEPT_FUNCTION(isdigit));