1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
11#define _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
12
13#include <__config>
14
15#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16# pragma GCC system_header
17#endif
18
19//
20// This file provides the std::__is_function_overridden utility, which allows checking
21// whether an overridable function (typically a weak symbol) like `operator new`
22// has been overridden by a user or not.
23//
24// This is a low-level utility which does not work on all platforms, since it needs
25// to make assumptions about the object file format in use. Furthermore, it requires
26// the "base definition" of the function (the one we want to check whether it has been
27// overridden) to be defined using the OVERRIDABLE_FUNCTION macro.
28//
29// This currently works with Mach-O files (used on Darwin) and with ELF files (used on Linux
30// and others). On platforms where we know how to implement this detection, the macro
31// _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION is defined to 1, and it is defined to 0 on
32// other platforms. The OVERRIDABLE_FUNCTION macro is defined to perform a normal
33// function definition on unsupported platforms so that it can be used to define functions
34// regardless of whether detection is actually supported.
35//
36// How does this work?
37// -------------------
38//
39// Let's say we want to check whether a weak function `f` has been overridden by the user.
40// The general mechanism works by defining a local symbol `__impl_ref<f>::__impl_` with
41// the same address as `f` as a constant expression using direct PC-relative
42// materialization thus pointing at the symbol defined in the same TU. At runtime, it
43// compares the address of `__impl_ref<f>::__impl_` with the address of `f` loaded from
44// GOT: if `f` was overridden by the user in another TU, the addresses will be different.
45//
46// When pointer authentication is used, the above mechanism doesn't work (yet) so we use
47// a different strategy placing `f`'s definition (in the libc++ built library) inside
48// a special section, which we do using the `__section__` attribute via the
49// OVERRIDABLE_FUNCTION macro. Then, when comes the time to check whether the function has
50// been overridden, we take the address of the function and we check whether it falls inside
51// the special section we created. This can be done by finding pointers to the start and
52// the end of the section, and then checking whether `f` falls within those bounds.
53//
54// Important note
55// --------------
56//
57// This mechanism should never be used outside of the libc++ built library. In particular,
58// attempting to use this within the libc++ headers will not work at all because we don't
59// want to be defining special sections inside user's executables which use our headers.
60//
61
62#if defined(_LIBCPP_OBJECT_FORMAT_MACHO) || (defined(_LIBCPP_OBJECT_FORMAT_ELF) && !defined(__NVPTX__))
63
64# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
65
66# if !__has_feature(ptrauth_calls)
67
68# define OVERRIDABLE_FUNCTION [[gnu::weak]]
69
70_LIBCPP_BEGIN_NAMESPACE_STD
71
72namespace {
73
74// This is used to prevent TBAA from optimizing away the function pointer comparison.
75template <typename T>
76[[nodiscard]] inline _LIBCPP_HIDE_FROM_ABI T* __launder_function_pointer(T* __ptr) noexcept {
77 __asm__ volatile("" : "+r"(__ptr));
78 return __ptr;
79}
80
81} // namespace
82
83template <auto* _Func>
84struct __impl_ref;
85
86// __impl_ref<...>::__impl_ is expected to be defined elsewhere, so the compiler emits
87// assembly references to the mangled symbol with no definition. This template saves us
88// the trouble of providing manual declarations for overloads with some other local name
89// for each function name being overloaded (operator new, operator new[], etc.).
90template <typename _Ret, typename... _Args, _Ret (*_Func)(_Args...)>
91struct __impl_ref<_Func> {
92 [[gnu::visibility("hidden")]] static _Ret __impl_(_Args...);
93};
94
95// This takes a function type template argument first so that the second non-type template
96// argument (pointer to the public function) gets the benefit of type-aware overload
97// resolution, rather than having to use a static_cast.
98template <typename T, T* _Func>
99_LIBCPP_HIDE_FROM_ABI inline bool __is_function_overridden() noexcept {
100# if !defined(_LIBCPP_CLANG_VER) || _LIBCPP_CLANG_VER >= 2101
101 __asm__("%cc0 = %cc1" : : "X"(__impl_ref<_Func>::__impl_), "X"(_Func));
102# else
103 __asm__("%c0 = %c1" : : "X"(__impl_ref<_Func>::__impl_), "X"(_Func));
104# endif
105 // This just has the compiler compare the two symbols. For PIC mode, this will do a
106 // direct PC-relative materialization for __impl_ref<...>::__impl_ and a GOT load for
107 // the _Func symbol. The compiler thinks __impl_ref<...>::__impl_ is defined elsewhere
108 // at link time and will be an undefined symbol. It doesn't know that the __asm__ tells
109 // the assembler to define it as a local symbol.
110 return __launder_function_pointer(_Func) != __impl_ref<_Func>::__impl_;
111}
112
113_LIBCPP_END_NAMESPACE_STD
114
115# else // __has_feature(ptrauth_calls)
116
117# include <ptrauth.h>
118
119# if defined(_LIBCPP_OBJECT_FORMAT_MACHO)
120# define OVERRIDABLE_FUNCTION [[gnu::weak, gnu::section("__TEXT,__lcxx_override,regular,pure_instructions")]]
121// Declare two dummy bytes and give them these special `__asm` values. These values are
122// defined by the linker, which means that referring to `&__lcxx_override_start` will
123// effectively refer to the address where the section starts (and same for the end).
124extern char __start___lcxx_override __asm("section$start$__TEXT$__lcxx_override");
125extern char __stop___lcxx_override __asm("section$end$__TEXT$__lcxx_override");
126# elif defined(_LIBCPP_OBJECT_FORMAT_ELF)
127// This is very similar to what we do for Mach-O above. The ELF linker will implicitly define
128// variables with those names corresponding to the start and the end of the section.
129//
130// See https://stackoverflow.com/questions/16552710/how-do-you-get-the-start-and-end-addresses-of-a-custom-elf-section
131# define OVERRIDABLE_FUNCTION [[gnu::weak, gnu::section("__lcxx_override")]]
132extern char __start___lcxx_override;
133extern char __stop___lcxx_override;
134# endif
135
136_LIBCPP_BEGIN_NAMESPACE_STD
137template <typename T, T* _Func>
138_LIBCPP_HIDE_FROM_ABI inline bool __is_function_overridden() noexcept {
139 uintptr_t __start = reinterpret_cast<uintptr_t>(&__start___lcxx_override);
140 uintptr_t __end = reinterpret_cast<uintptr_t>(&__stop___lcxx_override);
141 uintptr_t __ptr = reinterpret_cast<uintptr_t>(_Func);
142
143 // We must pass a void* to ptrauth_strip since it only accepts a pointer type. Also, in particular,
144 // we must NOT pass a function pointer, otherwise we will strip the function pointer, and then attempt
145 // to authenticate and re-sign it when casting it to a uintptr_t again, which will fail because we just
146 // stripped the function pointer. See rdar://122927845.
147 __ptr = reinterpret_cast<uintptr_t>(ptrauth_strip(reinterpret_cast<void*>(__ptr), ptrauth_key_function_pointer));
148
149 // Finally, the function was overridden if it falls outside of the section's bounds.
150 return __ptr < __start || __ptr > __end;
151}
152_LIBCPP_END_NAMESPACE_STD
153
154# endif // __has_feature(ptrauth_calls)
155
156#else // defined(_LIBCPP_OBJECT_FORMAT_MACHO) || (defined(_LIBCPP_OBJECT_FORMAT_ELF) && !defined(__NVPTX__))
157
158# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 0
159# define OVERRIDABLE_FUNCTION [[gnu::weak]]
160
161#endif // defined(_LIBCPP_OBJECT_FORMAT_MACHO) || (defined(_LIBCPP_OBJECT_FORMAT_ELF) && !defined(__NVPTX__))
162
163#endif // _LIBCPP_SRC_INCLUDE_OVERRIDABLE_FUNCTION_H
164