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