interception_linux.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===-- interception_linux.cpp ----------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is a part of AddressSanitizer, an address sanity checker.
  10. //
  11. // Linux-specific interception methods.
  12. //===----------------------------------------------------------------------===//
  13. #include "interception.h"
  14. #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
  15. SANITIZER_SOLARIS
  16. #include <dlfcn.h> // for dlsym() and dlvsym()
  17. namespace __interception {
  18. #if SANITIZER_NETBSD
  19. static int StrCmp(const char *s1, const char *s2) {
  20. while (true) {
  21. if (*s1 != *s2)
  22. return false;
  23. if (*s1 == 0)
  24. return true;
  25. s1++;
  26. s2++;
  27. }
  28. }
  29. #endif
  30. static void *GetFuncAddr(const char *name, uptr wrapper_addr) {
  31. #if SANITIZER_NETBSD
  32. // FIXME: Find a better way to handle renames
  33. if (StrCmp(name, "sigaction"))
  34. name = "__sigaction14";
  35. #endif
  36. void *addr = dlsym(RTLD_NEXT, name);
  37. if (!addr) {
  38. // If the lookup using RTLD_NEXT failed, the sanitizer runtime library is
  39. // later in the library search order than the DSO that we are trying to
  40. // intercept, which means that we cannot intercept this function. We still
  41. // want the address of the real definition, though, so look it up using
  42. // RTLD_DEFAULT.
  43. addr = dlsym(RTLD_DEFAULT, name);
  44. // In case `name' is not loaded, dlsym ends up finding the actual wrapper.
  45. // We don't want to intercept the wrapper and have it point to itself.
  46. if ((uptr)addr == wrapper_addr)
  47. addr = nullptr;
  48. }
  49. return addr;
  50. }
  51. bool InterceptFunction(const char *name, uptr *ptr_to_real, uptr func,
  52. uptr wrapper) {
  53. void *addr = GetFuncAddr(name, wrapper);
  54. *ptr_to_real = (uptr)addr;
  55. return addr && (func == wrapper);
  56. }
  57. // dlvsym is a GNU extension supported by some other platforms.
  58. #if SANITIZER_GLIBC || SANITIZER_FREEBSD || SANITIZER_NETBSD
  59. static void *GetFuncAddr(const char *name, const char *ver) {
  60. return dlvsym(RTLD_NEXT, name, ver);
  61. }
  62. bool InterceptFunction(const char *name, const char *ver, uptr *ptr_to_real,
  63. uptr func, uptr wrapper) {
  64. void *addr = GetFuncAddr(name, ver);
  65. *ptr_to_real = (uptr)addr;
  66. return addr && (func == wrapper);
  67. }
  68. #endif // SANITIZER_GLIBC || SANITIZER_FREEBSD || SANITIZER_NETBSD
  69. } // namespace __interception
  70. #endif // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD ||
  71. // SANITIZER_SOLARIS