Home | History | Annotate | Line # | Download | only in interception
      1 //===-- interception_linux.cc -----------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file is a part of AddressSanitizer, an address sanity checker.
     11 //
     12 // Linux-specific interception methods.
     13 //===----------------------------------------------------------------------===//
     14 
     15 #include "interception.h"
     16 
     17 #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
     18     SANITIZER_OPENBSD || SANITIZER_SOLARIS
     19 
     20 #include <dlfcn.h>   // for dlsym() and dlvsym()
     21 
     22 #if SANITIZER_NETBSD
     23 #include "sanitizer_common/sanitizer_libc.h"
     24 #endif
     25 
     26 namespace __interception {
     27 bool GetRealFunctionAddress(const char *func_name, uptr *func_addr,
     28     uptr real, uptr wrapper) {
     29 #if SANITIZER_NETBSD
     30   // XXX: Find a better way to handle renames
     31   // XXX: Do we have to handle the old __sigaction14 name here too?
     32   if (internal_strcmp(func_name, "sigaction") == 0) func_name = "__sigaction_siginfo";
     33 #endif
     34   *func_addr = (uptr)dlsym(RTLD_NEXT, func_name);
     35   if (!*func_addr) {
     36     // If the lookup using RTLD_NEXT failed, the sanitizer runtime library is
     37     // later in the library search order than the DSO that we are trying to
     38     // intercept, which means that we cannot intercept this function. We still
     39     // want the address of the real definition, though, so look it up using
     40     // RTLD_DEFAULT.
     41     *func_addr = (uptr)dlsym(RTLD_DEFAULT, func_name);
     42   }
     43   return real == wrapper;
     44 }
     45 
     46 // Android and Solaris do not have dlvsym
     47 #if !SANITIZER_ANDROID && !SANITIZER_SOLARIS && !SANITIZER_OPENBSD
     48 void *GetFuncAddrVer(const char *func_name, const char *ver) {
     49   return dlvsym(RTLD_NEXT, func_name, ver);
     50 }
     51 #endif  // !SANITIZER_ANDROID
     52 
     53 }  // namespace __interception
     54 
     55 #endif  // SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD ||
     56         // SANITIZER_OPENBSD || SANITIZER_SOLARIS
     57