2019-07-31 18:01:55 +00:00
|
|
|
//===-- interception_linux.cpp ----------------------------------*- C++ -*-===//
|
2012-02-08 19:52:01 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-02-08 19:52:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Linux-specific interception methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-08-02 11:19:13 +00:00
|
|
|
#include "interception.h"
|
2012-02-08 19:52:01 +00:00
|
|
|
|
[Sanitizers] Basic sanitizer Solaris support (PR 33274)
Summary:
This is the first mostly working version of the Sanitizer port to 32-bit Solaris/x86.
It is currently based on Solaris 11.4 Beta.
This part was initially developed inside libsanitizer in the GCC tree and should apply to
both. Subsequent parts will address changes to clang, the compiler-rt build system
and testsuite.
I'm not yet sure what the right patch granularity is: if it's profitable to split the patch
up, I'd like to get guidance on how to do so.
Most of the changes are probably straightforward with a few exceptions:
* The Solaris syscall interface isn't stable, undocumented and can change within an
OS release. The stable interface is the libc interface, which I'm using here, if possible
using the internal _-prefixed names.
* While the patch primarily target 32-bit x86, I've left a few sparc changes in. They
cannot currently be used with clang due to a backend limitation, but have worked
fine inside the gcc tree.
* Some functions (e.g. largefile versions of functions like open64) only exist in 32-bit
Solaris, so I've introduced a separate SANITIZER_SOLARIS32 to check for that.
The patch (with the subsequent ones to be submitted shortly) was tested
on i386-pc-solaris2.11. Only a few failures remain, some of them analyzed, some
still TBD:
AddressSanitizer-i386-sunos :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos :: TestCases/malloc-no-intercept.c
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/start-deactivated.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/default_options.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/malloc-no-intercept.c
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/MemoryMappingLayout.DumpListOfModules
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/SanitizerCommon.PthreadDestructorIterations
Maybe this is good enough the get the ball rolling.
Reviewers: kcc, alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, jyknight, kubamracek, krytarowski, fedor.sergeev, llvm-commits, #sanitizers
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D40898
llvm-svn: 320740
2017-12-14 20:14:29 +00:00
|
|
|
#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
|
2025-04-11 16:21:00 -04:00
|
|
|
SANITIZER_SOLARIS || SANITIZER_HAIKU
|
2017-12-06 17:02:00 +00:00
|
|
|
|
2014-02-24 08:37:41 +00:00
|
|
|
#include <dlfcn.h> // for dlsym() and dlvsym()
|
2012-02-08 19:52:01 +00:00
|
|
|
|
2019-01-22 00:39:59 +00:00
|
|
|
namespace __interception {
|
|
|
|
|
2017-12-06 17:02:00 +00:00
|
|
|
#if SANITIZER_NETBSD
|
2019-01-22 00:39:59 +00:00
|
|
|
static int StrCmp(const char *s1, const char *s2) {
|
|
|
|
while (true) {
|
|
|
|
if (*s1 != *s2)
|
|
|
|
return false;
|
|
|
|
if (*s1 == 0)
|
|
|
|
return true;
|
|
|
|
s1++;
|
|
|
|
s2++;
|
|
|
|
}
|
|
|
|
}
|
2017-08-08 12:10:08 +00:00
|
|
|
#endif
|
|
|
|
|
2023-05-25 11:07:36 +02:00
|
|
|
static void *GetFuncAddr(const char *name, uptr trampoline) {
|
2017-12-06 17:02:00 +00:00
|
|
|
#if SANITIZER_NETBSD
|
2019-01-22 00:39:59 +00:00
|
|
|
// FIXME: Find a better way to handle renames
|
2019-04-25 17:46:29 +00:00
|
|
|
if (StrCmp(name, "sigaction"))
|
|
|
|
name = "__sigaction14";
|
2017-08-08 12:10:08 +00:00
|
|
|
#endif
|
2019-04-25 17:46:29 +00:00
|
|
|
void *addr = dlsym(RTLD_NEXT, name);
|
|
|
|
if (!addr) {
|
2017-11-10 22:09:37 +00:00
|
|
|
// 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.
|
2019-04-25 17:46:29 +00:00
|
|
|
addr = dlsym(RTLD_DEFAULT, name);
|
2019-07-20 17:44:30 +00:00
|
|
|
|
|
|
|
// In case `name' is not loaded, dlsym ends up finding the actual wrapper.
|
|
|
|
// We don't want to intercept the wrapper and have it point to itself.
|
2023-05-25 11:07:36 +02:00
|
|
|
if ((uptr)addr == trampoline)
|
2019-07-20 17:44:30 +00:00
|
|
|
addr = nullptr;
|
2017-11-10 22:09:37 +00:00
|
|
|
}
|
2019-04-25 17:46:29 +00:00
|
|
|
return addr;
|
2012-02-08 19:52:01 +00:00
|
|
|
}
|
2013-09-02 18:06:28 +00:00
|
|
|
|
2019-05-01 20:57:59 +00:00
|
|
|
bool InterceptFunction(const char *name, uptr *ptr_to_real, uptr func,
|
2023-05-25 11:07:36 +02:00
|
|
|
uptr trampoline) {
|
|
|
|
void *addr = GetFuncAddr(name, trampoline);
|
2019-05-01 20:57:59 +00:00
|
|
|
*ptr_to_real = (uptr)addr;
|
2023-05-25 11:07:36 +02:00
|
|
|
return addr && (func == trampoline);
|
2019-05-01 20:57:59 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 10:55:40 -08:00
|
|
|
// dlvsym is a GNU extension supported by some other platforms.
|
|
|
|
#if SANITIZER_GLIBC || SANITIZER_FREEBSD || SANITIZER_NETBSD
|
2019-05-01 20:57:59 +00:00
|
|
|
static void *GetFuncAddr(const char *name, const char *ver) {
|
2019-04-25 17:46:29 +00:00
|
|
|
return dlvsym(RTLD_NEXT, name, ver);
|
2013-09-02 18:06:28 +00:00
|
|
|
}
|
2019-05-01 20:57:59 +00:00
|
|
|
|
|
|
|
bool InterceptFunction(const char *name, const char *ver, uptr *ptr_to_real,
|
2023-05-25 11:07:36 +02:00
|
|
|
uptr func, uptr trampoline) {
|
2019-05-01 20:57:59 +00:00
|
|
|
void *addr = GetFuncAddr(name, ver);
|
|
|
|
*ptr_to_real = (uptr)addr;
|
2023-05-25 11:07:36 +02:00
|
|
|
return addr && (func == trampoline);
|
2019-05-01 20:57:59 +00:00
|
|
|
}
|
2023-05-25 11:07:36 +02:00
|
|
|
# endif // SANITIZER_GLIBC || SANITIZER_FREEBSD || SANITIZER_NETBSD
|
2013-09-02 18:06:28 +00:00
|
|
|
|
2012-02-08 19:52:01 +00:00
|
|
|
} // namespace __interception
|
|
|
|
|
[Sanitizers] Basic sanitizer Solaris support (PR 33274)
Summary:
This is the first mostly working version of the Sanitizer port to 32-bit Solaris/x86.
It is currently based on Solaris 11.4 Beta.
This part was initially developed inside libsanitizer in the GCC tree and should apply to
both. Subsequent parts will address changes to clang, the compiler-rt build system
and testsuite.
I'm not yet sure what the right patch granularity is: if it's profitable to split the patch
up, I'd like to get guidance on how to do so.
Most of the changes are probably straightforward with a few exceptions:
* The Solaris syscall interface isn't stable, undocumented and can change within an
OS release. The stable interface is the libc interface, which I'm using here, if possible
using the internal _-prefixed names.
* While the patch primarily target 32-bit x86, I've left a few sparc changes in. They
cannot currently be used with clang due to a backend limitation, but have worked
fine inside the gcc tree.
* Some functions (e.g. largefile versions of functions like open64) only exist in 32-bit
Solaris, so I've introduced a separate SANITIZER_SOLARIS32 to check for that.
The patch (with the subsequent ones to be submitted shortly) was tested
on i386-pc-solaris2.11. Only a few failures remain, some of them analyzed, some
still TBD:
AddressSanitizer-i386-sunos :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos :: TestCases/malloc-no-intercept.c
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/concurrent_overflow.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/Posix/start-deactivated.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/default_options.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/init-order-atexit.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/log-path_test.cc
AddressSanitizer-i386-sunos-dynamic :: TestCases/malloc-no-intercept.c
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/MemoryMappingLayout.DumpListOfModules
SanitizerCommon-Unit :: ./Sanitizer-i386-Test/SanitizerCommon.PthreadDestructorIterations
Maybe this is good enough the get the ball rolling.
Reviewers: kcc, alekseyshl
Reviewed By: alekseyshl
Subscribers: srhines, jyknight, kubamracek, krytarowski, fedor.sergeev, llvm-commits, #sanitizers
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D40898
llvm-svn: 320740
2017-12-14 20:14:29 +00:00
|
|
|
#endif // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD ||
|
2025-04-12 00:49:23 -04:00
|
|
|
// SANITIZER_SOLARIS || SANITIZER_HAIKU
|