Debian Codegraph @

build/unix/stdc++compat/stdc++compat.cpp

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include <ostream>
6 #include <istream>
7 #include <sstream>
8 #include <memory>
9 #include <string>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <mozilla/Assertions.h>
13 #include <cxxabi.h>
14
15 /*
16 GLIBCXX_3.4.19 is from gcc 4.8.1 (199309)
17 GLIBCXX_3.4.20 is from gcc 4.9.0 (199307)
18 GLIBCXX_3.4.21 is from gcc 5.0 (210290)
19 GLIBCXX_3.4.22 is from gcc 6.0 (222482)
20 GLIBCXX_3.4.23 is from gcc 7
21 GLIBCXX_3.4.24 is from gcc 8
22 GLIBCXX_3.4.25 is from gcc 8
23 GLIBCXX_3.4.26 is from gcc 9
24 GLIBCXX_3.4.27 is from gcc 10
25 GLIBCXX_3.4.28 is from gcc 10
26 GLIBCXX_3.4.29 is from gcc 11
27
28 This file adds the necessary compatibility tricks to avoid symbols with
29 version GLIBCXX_3.4.20 and bigger, keeping binary compatibility with
30 libstdc++ 4.8.1.
31
32 WARNING: all symbols from this file must be defined weak when they
33 overlap with libstdc++.
34 */
35
36 namespace std {
37
38 /* We shouldn't be throwing exceptions at all, but it sadly turns out
39 we call STL (inline) functions that do. This avoids the GLIBCXX_3.4.20
40 symbol version. */
41 void __attribute__((weak)) __throw_out_of_range_fmt(char const* fmt, ...) {
42 va_list ap;
43 char buf[1024]; // That should be big enough.
44
45 va_start(ap, fmt);
46 vsnprintf(buf, sizeof(buf), fmt, ap);
47 buf[sizeof(buf) - 1] = 0;
48 va_end(ap);
49
50 __throw_range_error(buf);
51 }
52
53 } // namespace std
54
55 namespace __cxxabiv1 {
56 /* Calls to this function are added by the compiler itself on `new Class[n]`
57 * calls. This avoids the GLIBCXX_3.4.20 symbol version. */
58 extern "C" void __attribute__((weak)) __cxa_throw_bad_array_new_length() {
59 MOZ_CRASH();
60 }
61 } // namespace __cxxabiv1
62
63 #if _GLIBCXX_RELEASE >= 11
64 namespace std {
65
66 /* This avoids the GLIBCXX_3.4.29 symbol version. */
67 void __attribute__((weak)) __throw_bad_array_new_length() { MOZ_CRASH(); }
68
69 } // namespace std
70 #endif
71
72 /* While we generally don't build with exceptions, we have some host tools
73 * that do use them. libstdc++ from GCC 5.0 added exception constructors with
74 * char const* argument. Older versions only have a constructor with
75 * std::string. This avoids the GLIBCXX_3.4.21 symbol version. */
76 namespace std {
77 __attribute__((weak)) runtime_error::runtime_error(char const* s)
78 : runtime_error(std::string(s)) {}
79 __attribute__((weak)) out_of_range::out_of_range(char const* s)
80 : out_of_range(std::string(s)) {}
81 __attribute__((weak)) invalid_argument::invalid_argument(char const* s)
82 : invalid_argument(std::string(s)) {}
83 } // namespace std
84
85 /* Expose the definitions for the old ABI, allowing us to call its functions */
86 #define _GLIBCXX_THREAD_ABI_COMPAT 1
87 #include <thread>
88
89 namespace std {
90 /* The old ABI has a thread::_M_start_thread(shared_ptr<_Impl_base>),
91 * while the new has thread::_M_start_thread(unique_ptr<_State>, void(*)()).
92 * There is an intermediate ABI at version 3.4.21, with
93 * thread::_M_start_thread(shared_ptr<_Impl_base>, void(*)()).
94 * The void(*)() parameter is only there to keep a reference to pthread_create
95 * on the caller side, and is unused in the implementation
96 * We're creating an entry point for the new and intermediate ABIs, and make
97 * them call the old ABI. This avoids the GLIBCXX_3.4.21 symbol version. */
98 __attribute__((weak)) void thread::_M_start_thread(shared_ptr<_Impl_base> impl,
99 void (*)()) {
100 _M_start_thread(std::move(impl));
101 }
102
103 /* We need a _Impl_base-derived class wrapping a _State to call the old ABI
104 * from what we got by diverting the new API. This avoids the GLIBCXX_3.4.22
105 * symbol version. */
106 struct StateWrapper : public thread::_Impl_base {
107 unique_ptr<thread::_State> mState;
108
109 StateWrapper(unique_ptr<thread::_State> aState) : mState(std::move(aState)) {}
110
111 void _M_run() override { mState->_M_run(); }
112 };
113
114 /* This avoids the GLIBCXX_3.4.22 symbol version. */
115 __attribute__((weak)) void thread::_M_start_thread(unique_ptr<_State> aState,
116 void (*)()) {
117 auto impl = std::make_shared<StateWrapper>(std::move(aState));
118 _M_start_thread(std::move(impl));
119 }
120
121 /* For some reason this is a symbol exported by new versions of libstdc++,
122 * even though the destructor is default there too. This avoids the
123 * GLIBCXX_3.4.22 symbol version. */
124 __attribute__((weak)) thread::_State::~_State() = default;
125
126 #if _GLIBCXX_RELEASE >= 9
127 // Ideally we'd define
128 // bool _Sp_make_shared_tag::_S_eq(const type_info& ti) noexcept
129 // but we wouldn't be able to change its visibility because of the existing
130 // definition in C++ headers. We do need to change its visibility because we
131 // don't want it to be shadowing the one provided by libstdc++ itself, because
132 // it doesn't support RTTI. Not supporting RTTI doesn't matter for Firefox
133 // itself because it's built with RTTI disabled.
134 // So we define via the mangled symbol.
135 // This avoids the GLIBCXX_3.4.26 symbol version.
136 extern "C" __attribute__((visibility("hidden"))) bool
137 _ZNSt19_Sp_make_shared_tag5_S_eqERKSt9type_info(const type_info*) noexcept {
138 return false;
139 }
140 #endif
141
142 } // namespace std
143
144 namespace std {
145 /* Instantiate this template to avoid GLIBCXX_3.4.21 symbol versions
146 * depending on optimization level */
147 template basic_ios<char, char_traits<char>>::operator bool() const;
148 } // namespace std
149
150 #if !defined(MOZ_ASAN) && !defined(MOZ_TSAN)
151 /* operator delete with size is only available in CXXAPI_1.3.9, equivalent to
152 * GLIBCXX_3.4.21. */
153 void operator delete(void* ptr, size_t size) noexcept(true) {
154 ::operator delete(ptr);
155 }
156 #endif
157
158 namespace std {
159 /* Instantiate this template to avoid GLIBCXX_3.4.23 symbol versions
160 * depending on optimization level */
161 template basic_string<char, char_traits<char>, allocator<char>>::basic_string(
162 const basic_string&, size_t, const allocator<char>&);
163
164 #if _GLIBCXX_RELEASE >= 9
165 // This avoids the GLIBCXX_3.4.26 symbol version.
166 template basic_stringstream<char, char_traits<char>,
167 allocator<char>>::basic_stringstream();
168
169 template basic_ostringstream<char, char_traits<char>,
170 allocator<char>>::basic_ostringstream();
171 #endif
172
173 #if _GLIBCXX_RELEASE >= 11
174 // This avoids the GLIBCXX_3.4.29 symbol version.
175 template void basic_string<char, char_traits<char>, allocator<char>>::reserve();
176
177 template void
178 basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>>::reserve();
179 #endif
180
181 } // namespace std
182
183 /* The __cxa_thread_atexit_impl symbol is only available on GLIBC 2.18, but we
184 * want things to keep working on 2.17. It's not actually used directly from
185 * C++ code, but through __cxa_thead_atexit in libstdc++. The problem we have,
186 * though, is that rust's libstd also uses it, introducing a dependency we
187 * don't actually want. Fortunately, we can fall back to libstdc++'s wrapper
188 * (which, on systems without __cxa_thread_atexit_impl, has its own compatible
189 * implementation).
190 * The __cxa_thread_atexit symbol itself is marked CXXABI_1.3.7, which is
191 * equivalent to GLIBCXX_3.4.18.
192 */
193 extern "C" int __cxa_thread_atexit_impl(void (*dtor)(void*), void* obj,
194 void* dso_handle) {
195 return __cxxabiv1::__cxa_thread_atexit(dtor, obj, dso_handle);
196 }