2012-02-09 03:52:01 +08:00
|
|
|
//===-- interception.h ------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of AddressSanitizer, an address sanity checker.
|
|
|
|
//
|
|
|
|
// Machinery for providing replacements/wrappers for system functions.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef INTERCEPTION_H
|
|
|
|
#define INTERCEPTION_H
|
|
|
|
|
|
|
|
#if !defined(__linux__) && !defined(__APPLE__) && !defined(_WIN32)
|
|
|
|
# error "Interception doesn't work on this operating system."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// How to use this library:
|
|
|
|
// 1) Include this header to define your own interceptors
|
|
|
|
// (see details below).
|
|
|
|
// 2) Build all *.cc files and link against them.
|
|
|
|
// On Mac you will also need to:
|
|
|
|
// 3) Provide your own implementation for the following functions:
|
|
|
|
// mach_error_t __interception::allocate_island(void **ptr,
|
|
|
|
// size_t size,
|
|
|
|
// void *hint);
|
|
|
|
// mach_error_t __interception::deallocate_island(void *ptr);
|
|
|
|
// See "interception_mac.h" for more details.
|
|
|
|
|
|
|
|
// How to add an interceptor:
|
|
|
|
// Suppose you need to wrap/replace system function (generally, from libc):
|
|
|
|
// int foo(const char *bar, double baz);
|
|
|
|
// You'll need to:
|
|
|
|
// 1) define INTERCEPTOR(int, foo, const char *bar, double baz) { ... } in
|
|
|
|
// your source file.
|
|
|
|
// 2) Call "INTERCEPT_FUNCTION(foo)" prior to the first call of "foo".
|
|
|
|
// INTERCEPT_FUNCTION(foo) evaluates to "true" iff the function was
|
|
|
|
// intercepted successfully.
|
|
|
|
// You can access original function by calling REAL(foo)(bar, baz).
|
|
|
|
// By default, REAL(foo) will be visible only inside your interceptor, and if
|
|
|
|
// you want to use it in other parts of RTL, you'll need to:
|
2012-06-28 16:27:24 +08:00
|
|
|
// 3a) add DECLARE_REAL(int, foo, const char*, double) to a
|
2012-02-09 03:52:01 +08:00
|
|
|
// header file.
|
|
|
|
// However, if the call "INTERCEPT_FUNCTION(foo)" and definition for
|
|
|
|
// INTERCEPTOR(..., foo, ...) are in different files, you'll instead need to:
|
2012-06-28 16:27:24 +08:00
|
|
|
// 3b) add DECLARE_REAL_AND_INTERCEPTOR(int, foo, const char*, double)
|
2012-02-09 03:52:01 +08:00
|
|
|
// to a header file.
|
|
|
|
|
|
|
|
// Notes: 1. Things may not work properly if macro INTERCEPT(...) {...} or
|
2012-06-28 16:27:24 +08:00
|
|
|
// DECLARE_REAL(...) are located inside namespaces.
|
2012-02-09 03:52:01 +08:00
|
|
|
// 2. On Mac you can also use: "OVERRIDE_FUNCTION(foo, zoo);" to
|
|
|
|
// effectively redirect calls from "foo" to "zoo". In this case
|
|
|
|
// you aren't required to implement
|
2012-06-28 16:27:24 +08:00
|
|
|
// INTERCEPTOR(int, foo, const char *bar, double baz) {...}
|
2012-02-09 03:52:01 +08:00
|
|
|
// but instead you'll have to add
|
2012-06-28 16:27:24 +08:00
|
|
|
// DEFINE_REAL(int, foo, const char *bar, double baz) in your
|
2012-02-09 03:52:01 +08:00
|
|
|
// source file (to define a pointer to overriden function).
|
|
|
|
|
|
|
|
// How it works:
|
2012-05-24 21:54:31 +08:00
|
|
|
// To replace system functions on Linux we just need to declare functions
|
2012-02-09 03:52:01 +08:00
|
|
|
// with same names in our library and then obtain the real function pointers
|
2012-05-24 21:54:31 +08:00
|
|
|
// using dlsym().
|
|
|
|
// There is one complication. A user may also intercept some of the functions
|
2012-05-28 15:47:35 +08:00
|
|
|
// we intercept. To resolve this we declare our interceptors with __interceptor_
|
|
|
|
// prefix, and then make actual interceptors weak aliases to __interceptor_
|
2012-05-24 21:54:31 +08:00
|
|
|
// functions.
|
|
|
|
// This is not so on Mac OS, where the two-level namespace makes
|
2012-02-09 03:52:01 +08:00
|
|
|
// our replacement functions invisible to other libraries. This may be overcomed
|
|
|
|
// using the DYLD_FORCE_FLAT_NAMESPACE, but some errors loading the shared
|
2012-05-24 21:54:31 +08:00
|
|
|
// libraries in Chromium were noticed when doing so. Instead we use
|
|
|
|
// mach_override, a handy framework for patching functions at runtime.
|
|
|
|
// To avoid possible name clashes, our replacement functions have
|
2012-02-09 03:52:01 +08:00
|
|
|
// the "wrap_" prefix on Mac.
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
# define WRAP(x) wrap_##x
|
|
|
|
# define WRAPPER_NAME(x) "wrap_"#x
|
|
|
|
# define INTERCEPTOR_ATTRIBUTE
|
2012-05-24 21:54:31 +08:00
|
|
|
# define DECLARE_WRAPPER(ret_type, convention, func, ...)
|
2012-02-09 03:52:01 +08:00
|
|
|
#elif defined(_WIN32)
|
2012-03-12 19:45:09 +08:00
|
|
|
# if defined(_DLL) // DLL CRT
|
|
|
|
# define WRAP(x) x
|
|
|
|
# define WRAPPER_NAME(x) #x
|
|
|
|
# define INTERCEPTOR_ATTRIBUTE
|
|
|
|
# else // Static CRT
|
|
|
|
# define WRAP(x) wrap_##x
|
|
|
|
# define WRAPPER_NAME(x) "wrap_"#x
|
|
|
|
# define INTERCEPTOR_ATTRIBUTE
|
|
|
|
# endif
|
2012-05-24 21:54:31 +08:00
|
|
|
# define DECLARE_WRAPPER(ret_type, convention, func, ...)
|
2012-02-09 03:52:01 +08:00
|
|
|
#else
|
2012-05-28 15:47:35 +08:00
|
|
|
# define WRAP(x) __interceptor_ ## x
|
|
|
|
# define WRAPPER_NAME(x) "__interceptor_" #x
|
2012-02-09 03:52:01 +08:00
|
|
|
# define INTERCEPTOR_ATTRIBUTE __attribute__((visibility("default")))
|
2012-05-24 21:54:31 +08:00
|
|
|
# define DECLARE_WRAPPER(ret_type, convention, func, ...) \
|
|
|
|
extern "C" ret_type convention func(__VA_ARGS__) \
|
2012-06-28 16:07:19 +08:00
|
|
|
__attribute__((weak, alias("__interceptor_" #func), visibility("default")));
|
2012-02-09 03:52:01 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PTR_TO_REAL(x) real_##x
|
|
|
|
#define REAL(x) __interception::PTR_TO_REAL(x)
|
|
|
|
#define FUNC_TYPE(x) x##_f
|
|
|
|
|
2012-06-25 14:53:10 +08:00
|
|
|
#define DECLARE_REAL(ret_type, func, ...) \
|
2012-02-09 03:52:01 +08:00
|
|
|
typedef ret_type (*FUNC_TYPE(func))(__VA_ARGS__); \
|
|
|
|
namespace __interception { \
|
|
|
|
extern FUNC_TYPE(func) PTR_TO_REAL(func); \
|
|
|
|
}
|
|
|
|
|
2012-06-25 14:53:10 +08:00
|
|
|
#define DECLARE_REAL_AND_INTERCEPTOR(ret_type, func, ...) \
|
2012-06-28 16:07:19 +08:00
|
|
|
DECLARE_REAL(ret_type, func, ##__VA_ARGS__) \
|
2012-02-09 03:52:01 +08:00
|
|
|
extern "C" ret_type WRAP(func)(__VA_ARGS__);
|
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
// FIXME(timurrrr): We might need to add DECLARE_REAL_EX etc to support
|
|
|
|
// different calling conventions later.
|
|
|
|
|
2012-06-25 14:53:10 +08:00
|
|
|
#define DEFINE_REAL_EX(ret_type, convention, func, ...) \
|
2012-02-24 23:28:43 +08:00
|
|
|
typedef ret_type (convention *FUNC_TYPE(func))(__VA_ARGS__); \
|
|
|
|
namespace __interception { \
|
|
|
|
FUNC_TYPE(func) PTR_TO_REAL(func); \
|
|
|
|
}
|
|
|
|
|
2012-02-09 03:52:01 +08:00
|
|
|
// Generally, you don't need to use DEFINE_REAL by itself, as INTERCEPTOR
|
|
|
|
// macros does its job. In exceptional cases you may need to call REAL(foo)
|
|
|
|
// without defining INTERCEPTOR(..., foo, ...). For example, if you override
|
|
|
|
// foo with an interceptor for other function.
|
2012-02-24 23:28:43 +08:00
|
|
|
#define DEFAULT_CONVENTION
|
|
|
|
|
2012-06-25 14:53:10 +08:00
|
|
|
#define DEFINE_REAL(ret_type, func, ...) \
|
2012-06-28 16:07:19 +08:00
|
|
|
DEFINE_REAL_EX(ret_type, DEFAULT_CONVENTION, func, __VA_ARGS__)
|
2012-02-09 03:52:01 +08:00
|
|
|
|
2012-02-24 23:28:43 +08:00
|
|
|
#define INTERCEPTOR_EX(ret_type, convention, func, ...) \
|
2012-06-25 14:53:10 +08:00
|
|
|
DEFINE_REAL_EX(ret_type, convention, func, __VA_ARGS__) \
|
2012-06-28 16:07:19 +08:00
|
|
|
DECLARE_WRAPPER(ret_type, convention, func, __VA_ARGS__) \
|
2012-02-09 03:52:01 +08:00
|
|
|
extern "C" \
|
|
|
|
INTERCEPTOR_ATTRIBUTE \
|
2012-02-24 23:28:43 +08:00
|
|
|
ret_type convention WRAP(func)(__VA_ARGS__)
|
|
|
|
|
|
|
|
#define INTERCEPTOR(ret_type, func, ...) \
|
|
|
|
INTERCEPTOR_EX(ret_type, DEFAULT_CONVENTION, func, __VA_ARGS__)
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# define INTERCEPTOR_WINAPI(ret_type, func, ...) \
|
|
|
|
INTERCEPTOR_EX(ret_type, __stdcall, func, __VA_ARGS__)
|
|
|
|
#endif
|
2012-02-09 03:52:01 +08:00
|
|
|
|
2012-08-02 19:19:13 +08:00
|
|
|
// ISO C++ forbids casting between pointer-to-function and pointer-to-object,
|
|
|
|
// so we use casting via an integral type __interception::uptr,
|
|
|
|
// assuming that system is POSIX-compliant. Using other hacks seem
|
|
|
|
// challenging, as we don't even pass function type to
|
|
|
|
// INTERCEPT_FUNCTION macro, only its name.
|
|
|
|
namespace __interception {
|
|
|
|
typedef unsigned long uptr; // NOLINT
|
|
|
|
} // namespace __interception
|
|
|
|
|
2012-02-09 03:52:01 +08:00
|
|
|
#define INCLUDED_FROM_INTERCEPTION_LIB
|
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
# include "interception_linux.h"
|
|
|
|
# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_LINUX(func)
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
# include "interception_mac.h"
|
|
|
|
# define OVERRIDE_FUNCTION(old_func, new_func) \
|
|
|
|
OVERRIDE_FUNCTION_MAC(old_func, new_func)
|
|
|
|
# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_MAC(func)
|
|
|
|
#else // defined(_WIN32)
|
2012-02-22 21:59:49 +08:00
|
|
|
# include "interception_win.h"
|
|
|
|
# define INTERCEPT_FUNCTION(func) INTERCEPT_FUNCTION_WIN(func)
|
2012-02-09 03:52:01 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#undef INCLUDED_FROM_INTERCEPTION_LIB
|
|
|
|
|
|
|
|
#endif // INTERCEPTION_H
|