| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef __PRIVATE_TYPEINFO_H_ |
| 10 | #define __PRIVATE_TYPEINFO_H_ |
| 11 | |
| 12 | #include "__cxxabi_config.h" |
| 13 | |
| 14 | #include <stddef.h> |
| 15 | #include <typeinfo> |
| 16 | |
| 17 | namespace __cxxabiv1 { |
| 18 | |
| 19 | class _LIBCXXABI_TYPE_VIS __shim_type_info : public std::type_info { |
| 20 | public: |
| 21 | _LIBCXXABI_HIDDEN ~__shim_type_info() override; |
| 22 | |
| 23 | _LIBCXXABI_HIDDEN virtual void noop1() const; |
| 24 | _LIBCXXABI_HIDDEN virtual void noop2() const; |
| 25 | _LIBCXXABI_HIDDEN virtual bool can_catch(const __shim_type_info* thrown_type, void*& adjustedPtr) const = 0; |
| 26 | }; |
| 27 | |
| 28 | class _LIBCXXABI_TYPE_VIS __fundamental_type_info final : public __shim_type_info { |
| 29 | public: |
| 30 | _LIBCXXABI_HIDDEN ~__fundamental_type_info() final; |
| 31 | _LIBCXXABI_HIDDEN bool can_catch(const __shim_type_info*, void*&) const final; |
| 32 | }; |
| 33 | |
| 34 | class _LIBCXXABI_TYPE_VIS __array_type_info final : public __shim_type_info { |
| 35 | public: |
| 36 | _LIBCXXABI_HIDDEN ~__array_type_info() final; |
| 37 | _LIBCXXABI_HIDDEN bool can_catch(const __shim_type_info*, void*&) const final; |
| 38 | }; |
| 39 | |
| 40 | class _LIBCXXABI_TYPE_VIS __function_type_info final : public __shim_type_info { |
| 41 | public: |
| 42 | _LIBCXXABI_HIDDEN ~__function_type_info() final; |
| 43 | _LIBCXXABI_HIDDEN bool can_catch(const __shim_type_info*, void*&) const final; |
| 44 | }; |
| 45 | |
| 46 | class _LIBCXXABI_TYPE_VIS __enum_type_info final : public __shim_type_info { |
| 47 | public: |
| 48 | _LIBCXXABI_HIDDEN ~__enum_type_info() final; |
| 49 | _LIBCXXABI_HIDDEN bool can_catch(const __shim_type_info*, void*&) const final; |
| 50 | }; |
| 51 | |
| 52 | enum class path_kind : uint8_t { |
| 53 | unknown, |
| 54 | public_path, |
| 55 | not_public_path, |
| 56 | }; |
| 57 | |
| 58 | class _LIBCXXABI_TYPE_VIS __class_type_info; |
| 59 | |
| 60 | struct catch_info { |
| 61 | const __class_type_info* static_type; |
| 62 | |
| 63 | // pointer to a dst_type which has (static_ptr, static_type) above it |
| 64 | const void* dst_ptr_leading_to_static_ptr; |
| 65 | // pointer to a dst_type which does not have (static_ptr, static_type) above it |
| 66 | const void* dst_ptr_not_leading_to_static_ptr; |
| 67 | |
| 68 | // access of path from dst_ptr_leading_to_static_ptr to (static_ptr, static_type) |
| 69 | path_kind path_dst_ptr_to_static_ptr; |
| 70 | |
| 71 | // Number of dst_types below (static_ptr, static_type) |
| 72 | int number_to_static_ptr; |
| 73 | |
| 74 | // There is no object (seen when we throw a null pointer to object). |
| 75 | bool have_object; |
| 76 | // Virtual base |
| 77 | const void* vbase_cookie; |
| 78 | }; |
| 79 | |
| 80 | struct _LIBCXXABI_HIDDEN __dynamic_cast_info { |
| 81 | // const data supplied to the search: |
| 82 | |
| 83 | const __class_type_info* dst_type; |
| 84 | const void* static_ptr; |
| 85 | const __class_type_info* static_type; |
| 86 | |
| 87 | // Data that represents the answer: |
| 88 | |
| 89 | // pointer to a dst_type which has (static_ptr, static_type) above it |
| 90 | const void* dst_ptr_leading_to_static_ptr; |
| 91 | // pointer to a dst_type which does not have (static_ptr, static_type) above it |
| 92 | const void* dst_ptr_not_leading_to_static_ptr; |
| 93 | |
| 94 | // access of path from dst_ptr_leading_to_static_ptr to (static_ptr, static_type) |
| 95 | path_kind path_dst_ptr_to_static_ptr; |
| 96 | // access of path from (dynamic_ptr, dynamic_type) to (static_ptr, static_type) |
| 97 | // when there is no dst_type along the path |
| 98 | path_kind path_dynamic_ptr_to_static_ptr; |
| 99 | // access of path from (dynamic_ptr, dynamic_type) to dst_type |
| 100 | // (not used if there is a (static_ptr, static_type) above a dst_type). |
| 101 | path_kind path_dynamic_ptr_to_dst_ptr; |
| 102 | |
| 103 | // Number of dst_types below (static_ptr, static_type) |
| 104 | int number_to_static_ptr; |
| 105 | // Number of dst_types not below (static_ptr, static_type) |
| 106 | int number_to_dst_ptr; |
| 107 | |
| 108 | // Data that helps stop the search before the entire tree is searched: |
| 109 | |
| 110 | bool dst_type_might_be_derived_from_static_type; |
| 111 | // Number of dst_type in tree. If 0, then that means unknown. |
| 112 | int number_of_dst_type; |
| 113 | // communicates to a dst_type node that (static_ptr, static_type) was found |
| 114 | // above it. |
| 115 | bool found_our_static_ptr; |
| 116 | // communicates to a dst_type node that a static_type was found |
| 117 | // above it, but it wasn't (static_ptr, static_type) |
| 118 | bool found_any_static_type; |
| 119 | }; |
| 120 | |
| 121 | // Has no base class |
| 122 | class _LIBCXXABI_TYPE_VIS __class_type_info : public __shim_type_info { |
| 123 | public: |
| 124 | _LIBCXXABI_HIDDEN ~__class_type_info() override; |
| 125 | |
| 126 | _LIBCXXABI_HIDDEN [[nodiscard]] bool |
| 127 | process_static_type_above_dst(__dynamic_cast_info*, const void*, const void*, path_kind) const; |
| 128 | _LIBCXXABI_HIDDEN void process_static_type_below_dst(__dynamic_cast_info*, const void*, path_kind) const; |
| 129 | _LIBCXXABI_HIDDEN [[nodiscard]] bool process_found_base_class(catch_info*, void*, path_kind) const; |
| 130 | |
| 131 | template <bool use_strcmp> |
| 132 | [[nodiscard]] _LIBCXXABI_HIDDEN bool search_above_dst( |
| 133 | __dynamic_cast_info* info, const void* dst_ptr, const void* current_ptr, path_kind path_below) const { |
| 134 | #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST |
| 135 | if constexpr (use_strcmp) |
| 136 | return search_above_dst_strcmp(info, dst_ptr, current_ptr, path_below); |
| 137 | else |
| 138 | return search_above_dst_ptrcmp(info, dst_ptr, current_ptr, path_below); |
| 139 | #else |
| 140 | static_assert(!use_strcmp); |
| 141 | return search_above_dst_ptrcmp(info, dst_ptr, current_ptr, path_below); |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | template <bool use_strcmp> |
| 146 | [[nodiscard]] _LIBCXXABI_HIDDEN bool |
| 147 | search_below_dst(__dynamic_cast_info* info, const void* current_ptr, path_kind path_below) const { |
| 148 | #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST |
| 149 | if constexpr (use_strcmp) |
| 150 | return search_below_dst_strcmp(info, current_ptr, path_below); |
| 151 | else |
| 152 | return search_below_dst_ptrcmp(info, current_ptr, path_below); |
| 153 | #else |
| 154 | static_assert(!use_strcmp); |
| 155 | return search_below_dst_ptrcmp(catch_info: info, current_ptr, path_below); |
| 156 | #endif |
| 157 | } |
| 158 | |
| 159 | template <bool use_strcmp> |
| 160 | _LIBCXXABI_HIDDEN bool search_above_dst_impl( |
| 161 | __dynamic_cast_info* info, const void* dst_ptr, const void* current_ptr, path_kind path_below) const; |
| 162 | |
| 163 | _LIBCXXABI_HIDDEN [[nodiscard]] virtual bool search_above_dst_ptrcmp( |
| 164 | __dynamic_cast_info* info, const void* dst_ptr, const void* current_ptr, path_kind path_below) const { |
| 165 | return search_above_dst_impl<false>(info, dst_ptr, current_ptr, path_below); |
| 166 | } |
| 167 | |
| 168 | #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST |
| 169 | _LIBCXXABI_HIDDEN [[nodiscard]] virtual bool search_above_dst_strcmp( |
| 170 | __dynamic_cast_info* info, const void* dst_ptr, const void* current_ptr, path_kind path_below) const { |
| 171 | return search_above_dst_impl<true>(info, dst_ptr, current_ptr, path_below); |
| 172 | } |
| 173 | #endif |
| 174 | |
| 175 | template <bool use_strcmp> |
| 176 | [[nodiscard]] _LIBCXXABI_HIDDEN bool |
| 177 | search_below_dst_impl(__dynamic_cast_info* info, const void* current_ptr, path_kind path_below) const; |
| 178 | |
| 179 | _LIBCXXABI_HIDDEN [[nodiscard]] virtual bool |
| 180 | search_below_dst_ptrcmp(__dynamic_cast_info* catch_info, const void* current_ptr, path_kind path_below) const { |
| 181 | return search_below_dst_impl<false>(info: catch_info, current_ptr, path_below); |
| 182 | } |
| 183 | |
| 184 | #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST |
| 185 | _LIBCXXABI_HIDDEN [[nodiscard]] virtual bool |
| 186 | search_below_dst_strcmp(__dynamic_cast_info* info, const void* current_ptr, path_kind path_below) const { |
| 187 | return search_below_dst_impl<true>(info, current_ptr, path_below); |
| 188 | } |
| 189 | #endif |
| 190 | |
| 191 | _LIBCXXABI_HIDDEN bool can_catch(const __shim_type_info*, void*&) const final; |
| 192 | _LIBCXXABI_HIDDEN [[nodiscard]] virtual bool has_unambiguous_public_base(catch_info*, void*, path_kind) const; |
| 193 | }; |
| 194 | |
| 195 | // Has one non-virtual public base class at offset zero |
| 196 | class _LIBCXXABI_TYPE_VIS __si_class_type_info final : public __class_type_info { |
| 197 | public: |
| 198 | _LIBCXXABI_DISABLE_POINTER_FIELD_PROTECTION const __class_type_info* __base_type; |
| 199 | |
| 200 | _LIBCXXABI_HIDDEN ~__si_class_type_info() final; |
| 201 | |
| 202 | template <bool use_strcmp> |
| 203 | [[nodiscard]] _LIBCXXABI_HIDDEN bool |
| 204 | search_above_dst_impl(__dynamic_cast_info*, const void*, const void*, path_kind) const; |
| 205 | |
| 206 | _LIBCXXABI_HIDDEN [[nodiscard]] bool search_above_dst_ptrcmp( |
| 207 | __dynamic_cast_info* info, const void* dst_ptr, const void* current_ptr, path_kind path_below) const final { |
| 208 | return search_above_dst_impl<false>(info, dst_ptr, current_ptr, path_below); |
| 209 | } |
| 210 | |
| 211 | #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST |
| 212 | _LIBCXXABI_HIDDEN [[nodiscard]] bool search_above_dst_strcmp( |
| 213 | __dynamic_cast_info* info, const void* dst_ptr, const void* current_ptr, path_kind path_below) const final { |
| 214 | return search_above_dst_impl<true>(info, dst_ptr, current_ptr, path_below); |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | template <bool use_strcmp> |
| 219 | [[nodiscard]] _LIBCXXABI_HIDDEN bool search_below_dst_impl(__dynamic_cast_info*, const void*, path_kind) const; |
| 220 | |
| 221 | _LIBCXXABI_HIDDEN [[nodiscard]] bool |
| 222 | search_below_dst_ptrcmp(__dynamic_cast_info* info, const void* current_ptr, path_kind path_below) const final { |
| 223 | return search_below_dst_impl<false>(info, current_ptr, path_below); |
| 224 | } |
| 225 | |
| 226 | _LIBCXXABI_HIDDEN [[nodiscard]] bool has_unambiguous_public_base(catch_info*, void*, path_kind) const final; |
| 227 | }; |
| 228 | |
| 229 | struct _LIBCXXABI_HIDDEN __base_class_type_info { |
| 230 | public: |
| 231 | const __class_type_info* __base_type; |
| 232 | long __offset_flags; |
| 233 | |
| 234 | enum __offset_flags_masks { |
| 235 | __virtual_mask = 0x1, |
| 236 | __public_mask = 0x2, // base is public |
| 237 | __offset_shift = 8 |
| 238 | }; |
| 239 | |
| 240 | template <bool use_strcmp> |
| 241 | [[nodiscard]] bool search_above_dst(__dynamic_cast_info*, const void*, const void*, path_kind) const; |
| 242 | template <bool use_strcmp> |
| 243 | [[nodiscard]] bool search_below_dst(__dynamic_cast_info*, const void*, path_kind) const; |
| 244 | [[nodiscard]] bool has_unambiguous_public_base(catch_info*, void*, path_kind) const; |
| 245 | }; |
| 246 | |
| 247 | // Has one or more base classes |
| 248 | class _LIBCXXABI_TYPE_VIS __vmi_class_type_info final : public __class_type_info { |
| 249 | public: |
| 250 | unsigned int __flags; |
| 251 | unsigned int __base_count; |
| 252 | __base_class_type_info __base_info[1]; |
| 253 | |
| 254 | enum __flags_masks { |
| 255 | __non_diamond_repeat_mask = 0x1, // has two or more distinct base class |
| 256 | // objects of the same type |
| 257 | __diamond_shaped_mask = 0x2 // has base class object with two or |
| 258 | // more derived objects |
| 259 | }; |
| 260 | |
| 261 | _LIBCXXABI_HIDDEN ~__vmi_class_type_info() final; |
| 262 | |
| 263 | template <bool use_strcmp> |
| 264 | [[nodiscard]] _LIBCXXABI_HIDDEN bool |
| 265 | search_above_dst_impl(__dynamic_cast_info*, const void*, const void*, path_kind) const; |
| 266 | _LIBCXXABI_HIDDEN [[nodiscard]] bool search_above_dst_ptrcmp( |
| 267 | __dynamic_cast_info* info, const void* dst_ptr, const void* current_ptr, path_kind path_below) const final { |
| 268 | return search_above_dst_impl<false>(info, dst_ptr, current_ptr, path_below); |
| 269 | } |
| 270 | #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST |
| 271 | _LIBCXXABI_HIDDEN [[nodiscard]] bool search_above_dst_strcmp( |
| 272 | __dynamic_cast_info* info, const void* dst_ptr, const void* current_ptr, path_kind path_below) const final { |
| 273 | return search_above_dst_impl<true>(info, dst_ptr, current_ptr, path_below); |
| 274 | } |
| 275 | #endif |
| 276 | |
| 277 | template <bool use_strcmp> |
| 278 | [[nodiscard]] _LIBCXXABI_HIDDEN bool search_below_dst_impl(__dynamic_cast_info*, const void*, path_kind) const; |
| 279 | |
| 280 | _LIBCXXABI_HIDDEN [[nodiscard]] bool |
| 281 | search_below_dst_ptrcmp(__dynamic_cast_info* info, const void* current_ptr, path_kind path_below) const final { |
| 282 | return search_below_dst_impl<false>(info, current_ptr, path_below); |
| 283 | } |
| 284 | #ifdef _LIBCXXABI_FORGIVING_DYNAMIC_CAST |
| 285 | _LIBCXXABI_HIDDEN [[nodiscard]] bool |
| 286 | search_below_dst_strcmp(__dynamic_cast_info* info, const void* current_ptr, path_kind path_below) const final { |
| 287 | return search_below_dst_impl<true>(info, current_ptr, path_below); |
| 288 | } |
| 289 | #endif |
| 290 | _LIBCXXABI_HIDDEN [[nodiscard]] bool has_unambiguous_public_base(catch_info*, void*, path_kind) const final; |
| 291 | }; |
| 292 | |
| 293 | class _LIBCXXABI_TYPE_VIS __pbase_type_info : public __shim_type_info { |
| 294 | public: |
| 295 | unsigned int __flags; |
| 296 | _LIBCXXABI_DISABLE_POINTER_FIELD_PROTECTION const __shim_type_info* __pointee; |
| 297 | |
| 298 | enum __masks { |
| 299 | __const_mask = 0x1, |
| 300 | __volatile_mask = 0x2, |
| 301 | __restrict_mask = 0x4, |
| 302 | __incomplete_mask = 0x8, |
| 303 | __incomplete_class_mask = 0x10, |
| 304 | __transaction_safe_mask = 0x20, |
| 305 | // This implements the following proposal from cxx-abi-dev (not yet part of |
| 306 | // the ABI document): |
| 307 | // |
| 308 | // http://sourcerytools.com/pipermail/cxx-abi-dev/2016-October/002986.html |
| 309 | // |
| 310 | // This is necessary for support of http://wg21.link/p0012, which permits |
| 311 | // throwing noexcept function and member function pointers and catching |
| 312 | // them as non-noexcept pointers. |
| 313 | __noexcept_mask = 0x40, |
| 314 | |
| 315 | // Flags that cannot be removed by a standard conversion. |
| 316 | __no_remove_flags_mask = __const_mask | __volatile_mask | __restrict_mask, |
| 317 | // Flags that cannot be added by a standard conversion. |
| 318 | __no_add_flags_mask = __transaction_safe_mask | __noexcept_mask |
| 319 | }; |
| 320 | |
| 321 | _LIBCXXABI_HIDDEN ~__pbase_type_info() override; |
| 322 | _LIBCXXABI_HIDDEN bool can_catch(const __shim_type_info*, void*&) const override; |
| 323 | }; |
| 324 | |
| 325 | class _LIBCXXABI_TYPE_VIS __pointer_type_info final : public __pbase_type_info { |
| 326 | public: |
| 327 | _LIBCXXABI_HIDDEN ~__pointer_type_info() final; |
| 328 | _LIBCXXABI_HIDDEN bool can_catch(const __shim_type_info*, void*&) const final; |
| 329 | _LIBCXXABI_HIDDEN bool can_catch_nested(const __shim_type_info*) const; |
| 330 | }; |
| 331 | |
| 332 | class _LIBCXXABI_TYPE_VIS __pointer_to_member_type_info final : public __pbase_type_info { |
| 333 | public: |
| 334 | _LIBCXXABI_DISABLE_POINTER_FIELD_PROTECTION const __class_type_info* __context; |
| 335 | |
| 336 | _LIBCXXABI_HIDDEN ~__pointer_to_member_type_info() final; |
| 337 | _LIBCXXABI_HIDDEN bool can_catch(const __shim_type_info*, void*&) const final; |
| 338 | _LIBCXXABI_HIDDEN bool can_catch_nested(const __shim_type_info*) const; |
| 339 | }; |
| 340 | |
| 341 | } // namespace __cxxabiv1 |
| 342 | |
| 343 | #endif // __PRIVATE_TYPEINFO_H_ |
| 344 | |