allocator_shim_internals.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_ALLOCATOR_ALLOCATOR_SHIM_INTERNALS_H_
  5. #define BASE_ALLOCATOR_ALLOCATOR_SHIM_INTERNALS_H_
  6. #include "build/build_config.h"
  7. #if defined(__GNUC__)
  8. #if BUILDFLAG(IS_POSIX)
  9. #include <sys/cdefs.h> // for __THROW
  10. #endif
  11. #ifndef __THROW // Not a glibc system
  12. #ifdef _NOEXCEPT // LLVM libc++ uses noexcept instead
  13. #define __THROW _NOEXCEPT
  14. #else
  15. #define __THROW
  16. #endif // !_NOEXCEPT
  17. #endif
  18. // Shim layer symbols need to be ALWAYS exported, regardless of component build.
  19. //
  20. // If an exported symbol is linked into a DSO, it may be preempted by a
  21. // definition in the main executable. If this happens to an allocator symbol, it
  22. // will mean that the DSO will use the main executable's allocator. This is
  23. // normally relatively harmless -- regular allocations should all use the same
  24. // allocator, but if the DSO tries to hook the allocator it will not see any
  25. // allocations.
  26. //
  27. // However, if LLVM LTO is enabled, the compiler may inline the shim layer
  28. // symbols into callers. The end result is that allocator calls in DSOs may use
  29. // either the main executable's allocator or the DSO's allocator, depending on
  30. // whether the call was inlined. This is arguably a bug in LLVM caused by its
  31. // somewhat irregular handling of symbol interposition (see llvm.org/PR23501).
  32. // To work around the bug we use noinline to prevent the symbols from being
  33. // inlined.
  34. //
  35. // In the long run we probably want to avoid linking the allocator bits into
  36. // DSOs altogether. This will save a little space and stop giving DSOs the false
  37. // impression that they can hook the allocator.
  38. #define SHIM_ALWAYS_EXPORT __attribute__((visibility("default"), noinline))
  39. #elif BUILDFLAG(IS_WIN) // __GNUC__
  40. #define __THROW
  41. #define SHIM_ALWAYS_EXPORT __declspec(noinline)
  42. #endif // __GNUC__
  43. #endif // BASE_ALLOCATOR_ALLOCATOR_SHIM_INTERNALS_H_