From 3a1720af1d7f43edc5b214cde0be11bfb94d077e Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Wed, 23 Oct 2019 17:52:22 +0000 Subject: Vendor import of stripped compiler-rt trunk r375505, the last commit before the upstream Subversion repository was made read-only, and the LLVM project migrated to GitHub: https://llvm.org/svn/llvm-project/compiler-rt/trunk@375505 --- lib/interception/interception.h | 4 +- lib/interception/interception_linux.cc | 83 --- lib/interception/interception_linux.cpp | 83 +++ lib/interception/interception_mac.cc | 18 - lib/interception/interception_mac.cpp | 18 + lib/interception/interception_type_test.cc | 39 - lib/interception/interception_type_test.cpp | 39 + lib/interception/interception_win.cc | 1022 --------------------------- lib/interception/interception_win.cpp | 1022 +++++++++++++++++++++++++++ 9 files changed, 1164 insertions(+), 1164 deletions(-) delete mode 100644 lib/interception/interception_linux.cc create mode 100644 lib/interception/interception_linux.cpp delete mode 100644 lib/interception/interception_mac.cc create mode 100644 lib/interception/interception_mac.cpp delete mode 100644 lib/interception/interception_type_test.cc create mode 100644 lib/interception/interception_type_test.cpp delete mode 100644 lib/interception/interception_win.cc create mode 100644 lib/interception/interception_win.cpp (limited to 'lib/interception') diff --git a/lib/interception/interception.h b/lib/interception/interception.h index dacfa5ede28df..d27a8ccf92a8e 100644 --- a/lib/interception/interception.h +++ b/lib/interception/interception.h @@ -272,9 +272,9 @@ const interpose_substitution substitution_##func_name[] \ // INTERCEPT_FUNCTION macro, only its name. namespace __interception { #if defined(_WIN64) -typedef unsigned long long uptr; // NOLINT +typedef unsigned long long uptr; #else -typedef unsigned long uptr; // NOLINT +typedef unsigned long uptr; #endif // _WIN64 } // namespace __interception diff --git a/lib/interception/interception_linux.cc b/lib/interception/interception_linux.cc deleted file mode 100644 index 4b27102a159c8..0000000000000 --- a/lib/interception/interception_linux.cc +++ /dev/null @@ -1,83 +0,0 @@ -//===-- interception_linux.cc -----------------------------------*- C++ -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// -// -// This file is a part of AddressSanitizer, an address sanity checker. -// -// Linux-specific interception methods. -//===----------------------------------------------------------------------===// - -#include "interception.h" - -#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \ - SANITIZER_OPENBSD || SANITIZER_SOLARIS - -#include // for dlsym() and dlvsym() - -namespace __interception { - -#if SANITIZER_NETBSD -static int StrCmp(const char *s1, const char *s2) { - while (true) { - if (*s1 != *s2) - return false; - if (*s1 == 0) - return true; - s1++; - s2++; - } -} -#endif - -static void *GetFuncAddr(const char *name, uptr wrapper_addr) { -#if SANITIZER_NETBSD - // FIXME: Find a better way to handle renames - if (StrCmp(name, "sigaction")) - name = "__sigaction14"; -#endif - void *addr = dlsym(RTLD_NEXT, name); - if (!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. - addr = dlsym(RTLD_DEFAULT, name); - - // 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. - if ((uptr)addr == wrapper_addr) - addr = nullptr; - } - return addr; -} - -bool InterceptFunction(const char *name, uptr *ptr_to_real, uptr func, - uptr wrapper) { - void *addr = GetFuncAddr(name, wrapper); - *ptr_to_real = (uptr)addr; - return addr && (func == wrapper); -} - -// Android and Solaris do not have dlvsym -#if !SANITIZER_ANDROID && !SANITIZER_SOLARIS && !SANITIZER_OPENBSD -static void *GetFuncAddr(const char *name, const char *ver) { - return dlvsym(RTLD_NEXT, name, ver); -} - -bool InterceptFunction(const char *name, const char *ver, uptr *ptr_to_real, - uptr func, uptr wrapper) { - void *addr = GetFuncAddr(name, ver); - *ptr_to_real = (uptr)addr; - return addr && (func == wrapper); -} -#endif // !SANITIZER_ANDROID - -} // namespace __interception - -#endif // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || - // SANITIZER_OPENBSD || SANITIZER_SOLARIS diff --git a/lib/interception/interception_linux.cpp b/lib/interception/interception_linux.cpp new file mode 100644 index 0000000000000..950cd5126538b --- /dev/null +++ b/lib/interception/interception_linux.cpp @@ -0,0 +1,83 @@ +//===-- interception_linux.cpp ----------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file is a part of AddressSanitizer, an address sanity checker. +// +// Linux-specific interception methods. +//===----------------------------------------------------------------------===// + +#include "interception.h" + +#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \ + SANITIZER_OPENBSD || SANITIZER_SOLARIS + +#include // for dlsym() and dlvsym() + +namespace __interception { + +#if SANITIZER_NETBSD +static int StrCmp(const char *s1, const char *s2) { + while (true) { + if (*s1 != *s2) + return false; + if (*s1 == 0) + return true; + s1++; + s2++; + } +} +#endif + +static void *GetFuncAddr(const char *name, uptr wrapper_addr) { +#if SANITIZER_NETBSD + // FIXME: Find a better way to handle renames + if (StrCmp(name, "sigaction")) + name = "__sigaction14"; +#endif + void *addr = dlsym(RTLD_NEXT, name); + if (!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. + addr = dlsym(RTLD_DEFAULT, name); + + // 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. + if ((uptr)addr == wrapper_addr) + addr = nullptr; + } + return addr; +} + +bool InterceptFunction(const char *name, uptr *ptr_to_real, uptr func, + uptr wrapper) { + void *addr = GetFuncAddr(name, wrapper); + *ptr_to_real = (uptr)addr; + return addr && (func == wrapper); +} + +// Android and Solaris do not have dlvsym +#if !SANITIZER_ANDROID && !SANITIZER_SOLARIS && !SANITIZER_OPENBSD +static void *GetFuncAddr(const char *name, const char *ver) { + return dlvsym(RTLD_NEXT, name, ver); +} + +bool InterceptFunction(const char *name, const char *ver, uptr *ptr_to_real, + uptr func, uptr wrapper) { + void *addr = GetFuncAddr(name, ver); + *ptr_to_real = (uptr)addr; + return addr && (func == wrapper); +} +#endif // !SANITIZER_ANDROID + +} // namespace __interception + +#endif // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || + // SANITIZER_OPENBSD || SANITIZER_SOLARIS diff --git a/lib/interception/interception_mac.cc b/lib/interception/interception_mac.cc deleted file mode 100644 index 5bfc1514d2b87..0000000000000 --- a/lib/interception/interception_mac.cc +++ /dev/null @@ -1,18 +0,0 @@ -//===-- interception_mac.cc -------------------------------------*- C++ -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// -// -// This file is a part of AddressSanitizer, an address sanity checker. -// -// Mac-specific interception methods. -//===----------------------------------------------------------------------===// - -#include "interception.h" - -#if SANITIZER_MAC - -#endif // SANITIZER_MAC diff --git a/lib/interception/interception_mac.cpp b/lib/interception/interception_mac.cpp new file mode 100644 index 0000000000000..fb6eadcff597e --- /dev/null +++ b/lib/interception/interception_mac.cpp @@ -0,0 +1,18 @@ +//===-- interception_mac.cpp ------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file is a part of AddressSanitizer, an address sanity checker. +// +// Mac-specific interception methods. +//===----------------------------------------------------------------------===// + +#include "interception.h" + +#if SANITIZER_MAC + +#endif // SANITIZER_MAC diff --git a/lib/interception/interception_type_test.cc b/lib/interception/interception_type_test.cc deleted file mode 100644 index c00294a9b474a..0000000000000 --- a/lib/interception/interception_type_test.cc +++ /dev/null @@ -1,39 +0,0 @@ -//===-- interception_type_test.cc -------------------------------*- C++ -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// -// -// This file is a part of AddressSanitizer, an address sanity checker. -// -// Compile-time tests of the internal type definitions. -//===----------------------------------------------------------------------===// - -#include "interception.h" - -#if SANITIZER_LINUX || SANITIZER_MAC - -#include -#include -#include - -COMPILER_CHECK(sizeof(::SIZE_T) == sizeof(size_t)); -COMPILER_CHECK(sizeof(::SSIZE_T) == sizeof(ssize_t)); -COMPILER_CHECK(sizeof(::PTRDIFF_T) == sizeof(ptrdiff_t)); -COMPILER_CHECK(sizeof(::INTMAX_T) == sizeof(intmax_t)); - -#if !SANITIZER_MAC -COMPILER_CHECK(sizeof(::OFF64_T) == sizeof(off64_t)); -#endif - -// The following are the cases when pread (and friends) is used instead of -// pread64. In those cases we need OFF_T to match off_t. We don't care about the -// rest (they depend on _FILE_OFFSET_BITS setting when building an application). -# if SANITIZER_ANDROID || !defined _FILE_OFFSET_BITS || \ - _FILE_OFFSET_BITS != 64 -COMPILER_CHECK(sizeof(::OFF_T) == sizeof(off_t)); -# endif - -#endif diff --git a/lib/interception/interception_type_test.cpp b/lib/interception/interception_type_test.cpp new file mode 100644 index 0000000000000..a611604a700cf --- /dev/null +++ b/lib/interception/interception_type_test.cpp @@ -0,0 +1,39 @@ +//===-- interception_type_test.cpp ------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file is a part of AddressSanitizer, an address sanity checker. +// +// Compile-time tests of the internal type definitions. +//===----------------------------------------------------------------------===// + +#include "interception.h" + +#if SANITIZER_LINUX || SANITIZER_MAC + +#include +#include +#include + +COMPILER_CHECK(sizeof(::SIZE_T) == sizeof(size_t)); +COMPILER_CHECK(sizeof(::SSIZE_T) == sizeof(ssize_t)); +COMPILER_CHECK(sizeof(::PTRDIFF_T) == sizeof(ptrdiff_t)); +COMPILER_CHECK(sizeof(::INTMAX_T) == sizeof(intmax_t)); + +#if !SANITIZER_MAC +COMPILER_CHECK(sizeof(::OFF64_T) == sizeof(off64_t)); +#endif + +// The following are the cases when pread (and friends) is used instead of +// pread64. In those cases we need OFF_T to match off_t. We don't care about the +// rest (they depend on _FILE_OFFSET_BITS setting when building an application). +# if SANITIZER_ANDROID || !defined _FILE_OFFSET_BITS || \ + _FILE_OFFSET_BITS != 64 +COMPILER_CHECK(sizeof(::OFF_T) == sizeof(off_t)); +# endif + +#endif diff --git a/lib/interception/interception_win.cc b/lib/interception/interception_win.cc deleted file mode 100644 index 40bde008052ba..0000000000000 --- a/lib/interception/interception_win.cc +++ /dev/null @@ -1,1022 +0,0 @@ -//===-- interception_linux.cc -----------------------------------*- C++ -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// -// -// This file is a part of AddressSanitizer, an address sanity checker. -// -// Windows-specific interception methods. -// -// This file is implementing several hooking techniques to intercept calls -// to functions. The hooks are dynamically installed by modifying the assembly -// code. -// -// The hooking techniques are making assumptions on the way the code is -// generated and are safe under these assumptions. -// -// On 64-bit architecture, there is no direct 64-bit jump instruction. To allow -// arbitrary branching on the whole memory space, the notion of trampoline -// region is used. A trampoline region is a memory space withing 2G boundary -// where it is safe to add custom assembly code to build 64-bit jumps. -// -// Hooking techniques -// ================== -// -// 1) Detour -// -// The Detour hooking technique is assuming the presence of an header with -// padding and an overridable 2-bytes nop instruction (mov edi, edi). The -// nop instruction can safely be replaced by a 2-bytes jump without any need -// to save the instruction. A jump to the target is encoded in the function -// header and the nop instruction is replaced by a short jump to the header. -// -// head: 5 x nop head: jmp -// func: mov edi, edi --> func: jmp short -// [...] real: [...] -// -// This technique is only implemented on 32-bit architecture. -// Most of the time, Windows API are hookable with the detour technique. -// -// 2) Redirect Jump -// -// The redirect jump is applicable when the first instruction is a direct -// jump. The instruction is replaced by jump to the hook. -// -// func: jmp