Skip to content

Commit 02cffe2

Browse files
committed
[libcxx] Use alias for detecting overriden function
This mechanism is preferable in environments like embedded since it doesn't require special handling of the custom section.
1 parent 0b8fec6 commit 02cffe2

File tree

2 files changed

+58
-77
lines changed

2 files changed

+58
-77
lines changed

libcxx/src/include/overridable_function.h

Lines changed: 48 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
// This is a low-level utility which does not work on all platforms, since it needs
3030
// to make assumptions about the object file format in use. Furthermore, it requires
3131
// the "base definition" of the function (the one we want to check whether it has been
32-
// overridden) to be annotated with the _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.
32+
// overridden) to be defined using the _LIBCPP_OVERRIDABLE_FUNCTION macro.
3333
//
3434
// This currently works with Mach-O files (used on Darwin) and with ELF files (used on Linux
3535
// and others). On platforms where we know how to implement this detection, the macro
@@ -42,93 +42,74 @@
4242
// -------------------
4343
//
4444
// Let's say we want to check whether a weak function `f` has been overridden by the user.
45-
// The general mechanism works by placing `f`'s definition (in the libc++ built library)
46-
// inside a special section, which we do using the `__section__` attribute via the
47-
// _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE macro.
45+
// The general mechanism works by defining a symbol `f_impl__` and a weak alias `f` via the
46+
// _LIBCPP_OVERRIDABLE_FUNCTION macro.
4847
//
4948
// Then, when comes the time to check whether the function has been overridden, we take
50-
// the address of the function and we check whether it falls inside the special function
51-
// we created. This can be done by finding pointers to the start and the end of the section
52-
// (which is done differently for ELF and Mach-O), and then checking whether `f` falls
53-
// within those bounds. If it falls within those bounds, then `f` is still inside the
54-
// special section and so it is the version we defined in the libc++ built library, i.e.
55-
// it was not overridden. Otherwise, it was overridden by the user because it falls
56-
// outside of the section.
49+
// the address of the function `f` and we check whether it is different from `f_impl__`.
50+
// If so it means the function was overriden by the user.
5751
//
5852
// Important note
5953
// --------------
6054
//
61-
// This mechanism should never be used outside of the libc++ built library. In particular,
62-
// attempting to use this within the libc++ headers will not work at all because we don't
63-
// want to be defining special sections inside user's executables which use our headers.
55+
// This mechanism should never be used outside of the libc++ built library.
6456
//
6557

6658
#if defined(_LIBCPP_OBJECT_FORMAT_MACHO)
6759

68-
# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
69-
# define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE \
70-
__attribute__((__section__("__TEXT,__lcxx_override,regular,pure_instructions")))
71-
7260
_LIBCPP_BEGIN_NAMESPACE_STD
73-
template <class _Ret, class... _Args>
74-
_LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {
75-
// Declare two dummy bytes and give them these special `__asm` values. These values are
76-
// defined by the linker, which means that referring to `&__lcxx_override_start` will
77-
// effectively refer to the address where the section starts (and same for the end).
78-
extern char __lcxx_override_start __asm("section$start$__TEXT$__lcxx_override");
79-
extern char __lcxx_override_end __asm("section$end$__TEXT$__lcxx_override");
80-
81-
// Now get a uintptr_t out of these locations, and out of the function pointer.
82-
uintptr_t __start = reinterpret_cast<uintptr_t>(&__lcxx_override_start);
83-
uintptr_t __end = reinterpret_cast<uintptr_t>(&__lcxx_override_end);
84-
uintptr_t __ptr = reinterpret_cast<uintptr_t>(__fptr);
85-
86-
# if __has_feature(ptrauth_calls)
87-
// We must pass a void* to ptrauth_strip since it only accepts a pointer type. Also, in particular,
88-
// we must NOT pass a function pointer, otherwise we will strip the function pointer, and then attempt
89-
// to authenticate and re-sign it when casting it to a uintptr_t again, which will fail because we just
90-
// stripped the function pointer. See rdar://122927845.
91-
__ptr = reinterpret_cast<uintptr_t>(ptrauth_strip(reinterpret_cast<void*>(__ptr), ptrauth_key_function_pointer));
92-
# endif
93-
94-
// Finally, the function was overridden if it falls outside of the section's bounds.
95-
return __ptr < __start || __ptr > __end;
96-
}
97-
_LIBCPP_END_NAMESPACE_STD
9861

99-
// The NVPTX linker cannot create '__start/__stop' sections.
100-
#elif defined(_LIBCPP_OBJECT_FORMAT_ELF) && !defined(__NVPTX__)
62+
template <typename _Func>
63+
constexpr _Func* __overload_of(_Func* f) { return f; }
10164

102-
# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
103-
# define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE __attribute__((__section__("__lcxx_override")))
65+
template <auto _Func>
66+
constexpr bool __is_function_overridden();
10467

105-
// This is very similar to what we do for Mach-O above. The ELF linker will implicitly define
106-
// variables with those names corresponding to the start and the end of the section.
107-
//
108-
// See https://stackoverflow.com/questions/16552710/how-do-you-get-the-start-and-end-addresses-of-a-custom-elf-section
109-
extern char __start___lcxx_override;
110-
extern char __stop___lcxx_override;
68+
_LIBCPP_END_NAMESPACE_STD
69+
70+
# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
71+
# define _LIBCPP_OVERRIDABLE_FUNCTION(symbol, type, name, arglist) \
72+
extern "C" type symbol##_impl__ arglist; \
73+
__asm__(".globl _" _LIBCPP_TOSTRING(symbol)); \
74+
__asm__(".set _" _LIBCPP_TOSTRING(symbol) ", _" _LIBCPP_TOSTRING(symbol##_impl__)); \
75+
extern __typeof(symbol##_impl__) name __attribute__((weak_import)); \
76+
_LIBCPP_BEGIN_NAMESPACE_STD \
77+
template <> \
78+
constexpr bool __is_function_overridden<__overload_of<type arglist>(name)>() { \
79+
return __overload_of<type arglist>(name) != symbol##_impl__; \
80+
} \
81+
_LIBCPP_END_NAMESPACE_STD \
82+
type symbol##_impl__ arglist
83+
84+
#elif defined(_LIBCPP_OBJECT_FORMAT_ELF)
11185

11286
_LIBCPP_BEGIN_NAMESPACE_STD
113-
template <class _Ret, class... _Args>
114-
_LIBCPP_HIDE_FROM_ABI bool __is_function_overridden(_Ret (*__fptr)(_Args...)) noexcept {
115-
uintptr_t __start = reinterpret_cast<uintptr_t>(&__start___lcxx_override);
116-
uintptr_t __end = reinterpret_cast<uintptr_t>(&__stop___lcxx_override);
117-
uintptr_t __ptr = reinterpret_cast<uintptr_t>(__fptr);
118-
119-
# if __has_feature(ptrauth_calls)
120-
// We must pass a void* to ptrauth_strip since it only accepts a pointer type. See full explanation above.
121-
__ptr = reinterpret_cast<uintptr_t>(ptrauth_strip(reinterpret_cast<void*>(__ptr), ptrauth_key_function_pointer));
122-
# endif
123-
124-
return __ptr < __start || __ptr > __end;
125-
}
87+
88+
template <typename _Func>
89+
constexpr _Func* __overload_of(_Func* f) { return f; }
90+
91+
template <auto _Func>
92+
constexpr bool __is_function_overridden();
93+
12694
_LIBCPP_END_NAMESPACE_STD
12795

96+
# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 1
97+
# define _LIBCPP_OVERRIDABLE_FUNCTION(symbol, type, name, arglist) \
98+
extern "C" type symbol##_impl__ arglist; \
99+
[[gnu::weak, gnu::alias(_LIBCPP_TOSTRING(symbol##_impl__))]] type name arglist; \
100+
_LIBCPP_BEGIN_NAMESPACE_STD \
101+
template <> \
102+
constexpr bool __is_function_overridden<__overload_of<type arglist>(name)>() { \
103+
return __overload_of<type arglist>(name) != symbol##_impl__; \
104+
} \
105+
_LIBCPP_END_NAMESPACE_STD \
106+
type symbol##_impl__ arglist
107+
128108
#else
129109

130110
# define _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION 0
131-
# define _LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE /* nothing */
111+
# define _LIBCPP_OVERRIDABLE_FUNCTION(symbol, type, name, arglist) \
112+
_LIBCPP_WEAK type name arglist
132113

133114
#endif
134115

libcxx/src/new.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static void* operator_new_impl(std::size_t size) {
4343
return p;
4444
}
4545

46-
_LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK void* operator new(std::size_t size) _THROW_BAD_ALLOC {
46+
_LIBCPP_OVERRIDABLE_FUNCTION(_Znmw, void *, operator new, (std::size_t size)) _THROW_BAD_ALLOC {
4747
void* p = operator_new_impl(size);
4848
if (p == nullptr)
4949
__throw_bad_alloc_shim();
@@ -54,7 +54,7 @@ _LIBCPP_WEAK void* operator new(size_t size, const std::nothrow_t&) noexcept {
5454
# ifdef _LIBCPP_HAS_NO_EXCEPTIONS
5555
# if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
5656
_LIBCPP_ASSERT_SHIM(
57-
!std::__is_function_overridden(static_cast<void* (*)(std::size_t)>(&operator new)),
57+
!std::__is_function_overridden<std::__overload_of<void* (std::size_t)>(&operator new)>(),
5858
"libc++ was configured with exceptions disabled and `operator new(size_t)` has been overridden, "
5959
"but `operator new(size_t, nothrow_t)` has not been overridden. This is problematic because "
6060
"`operator new(size_t, nothrow_t)` must call `operator new(size_t)`, which will terminate in case "
@@ -74,15 +74,15 @@ _LIBCPP_WEAK void* operator new(size_t size, const std::nothrow_t&) noexcept {
7474
# endif
7575
}
7676

77-
_LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK void* operator new[](size_t size) _THROW_BAD_ALLOC {
77+
_LIBCPP_OVERRIDABLE_FUNCTION(_Znam, void*, operator new[], (size_t size)) _THROW_BAD_ALLOC {
7878
return ::operator new(size);
7979
}
8080

8181
_LIBCPP_WEAK void* operator new[](size_t size, const std::nothrow_t&) noexcept {
8282
# ifdef _LIBCPP_HAS_NO_EXCEPTIONS
8383
# if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
8484
_LIBCPP_ASSERT_SHIM(
85-
!std::__is_function_overridden(static_cast<void* (*)(std::size_t)>(&operator new[])),
85+
!std::__is_function_overridden<std::__overload_of<void* (std::size_t)>(&operator new[])>(),
8686
"libc++ was configured with exceptions disabled and `operator new[](size_t)` has been overridden, "
8787
"but `operator new[](size_t, nothrow_t)` has not been overridden. This is problematic because "
8888
"`operator new[](size_t, nothrow_t)` must call `operator new[](size_t)`, which will terminate in case "
@@ -136,8 +136,9 @@ static void* operator_new_aligned_impl(std::size_t size, std::align_val_t alignm
136136
return p;
137137
}
138138

139-
_LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK void*
140-
operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
139+
//_LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK void*
140+
//operator new(std::size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
141+
_LIBCPP_OVERRIDABLE_FUNCTION(_ZnwmSt11align_val_t, void*, operator new, (std::size_t size, std::align_val_t alignment)) _THROW_BAD_ALLOC {
141142
void* p = operator_new_aligned_impl(size, alignment);
142143
if (p == nullptr)
143144
__throw_bad_alloc_shim();
@@ -148,7 +149,7 @@ _LIBCPP_WEAK void* operator new(size_t size, std::align_val_t alignment, const s
148149
# ifdef _LIBCPP_HAS_NO_EXCEPTIONS
149150
# if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
150151
_LIBCPP_ASSERT_SHIM(
151-
!std::__is_function_overridden(static_cast<void* (*)(std::size_t, std::align_val_t)>(&operator new)),
152+
!std::__is_function_overridden<std::__overload_of<void* (std::size_t, std::align_val_t)>(&operator new)>(),
152153
"libc++ was configured with exceptions disabled and `operator new(size_t, align_val_t)` has been overridden, "
153154
"but `operator new(size_t, align_val_t, nothrow_t)` has not been overridden. This is problematic because "
154155
"`operator new(size_t, align_val_t, nothrow_t)` must call `operator new(size_t, align_val_t)`, which will "
@@ -168,16 +169,15 @@ _LIBCPP_WEAK void* operator new(size_t size, std::align_val_t alignment, const s
168169
# endif
169170
}
170171

171-
_LIBCPP_MAKE_OVERRIDABLE_FUNCTION_DETECTABLE _LIBCPP_WEAK void*
172-
operator new[](size_t size, std::align_val_t alignment) _THROW_BAD_ALLOC {
172+
_LIBCPP_OVERRIDABLE_FUNCTION(_ZnamSt11align_val_t, void *, operator new[], (size_t size, std::align_val_t alignment)) _THROW_BAD_ALLOC {
173173
return ::operator new(size, alignment);
174174
}
175175

176176
_LIBCPP_WEAK void* operator new[](size_t size, std::align_val_t alignment, const std::nothrow_t&) noexcept {
177177
# ifdef _LIBCPP_HAS_NO_EXCEPTIONS
178178
# if _LIBCPP_CAN_DETECT_OVERRIDDEN_FUNCTION
179179
_LIBCPP_ASSERT_SHIM(
180-
!std::__is_function_overridden(static_cast<void* (*)(std::size_t, std::align_val_t)>(&operator new[])),
180+
!std::__is_function_overridden<std::__overload_of<void* (std::size_t, std::align_val_t)>(&operator new[])>(),
181181
"libc++ was configured with exceptions disabled and `operator new[](size_t, align_val_t)` has been overridden, "
182182
"but `operator new[](size_t, align_val_t, nothrow_t)` has not been overridden. This is problematic because "
183183
"`operator new[](size_t, align_val_t, nothrow_t)` must call `operator new[](size_t, align_val_t)`, which will "

0 commit comments

Comments
 (0)