1 //===-- ubsan_type_hash_itanium.cc ----------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Implementation of type hashing/lookup for Itanium C++ ABI.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common/sanitizer_platform.h"
13 #include "ubsan_platform.h"
14 #if CAN_SANITIZE_UB && !SANITIZER_WINDOWS
15 #include "ubsan_type_hash.h"
17 #include "sanitizer_common/sanitizer_common.h"
19 // The following are intended to be binary compatible with the definitions
20 // given in the Itanium ABI. We make no attempt to be ODR-compatible with
21 // those definitions, since existing ABI implementations aren't.
28 const char *__type_name
;
32 namespace __cxxabiv1
{
34 /// Type info for classes with no bases, and base class for type info for
35 /// classes with bases.
36 class __class_type_info
: public std::type_info
{
37 ~__class_type_info() override
;
40 /// Type info for classes with simple single public inheritance.
41 class __si_class_type_info
: public __class_type_info
{
43 ~__si_class_type_info() override
;
45 const __class_type_info
*__base_type
;
48 class __base_class_type_info
{
50 const __class_type_info
*__base_type
;
53 enum __offset_flags_masks
{
60 /// Type info for classes with multiple, virtual, or non-public inheritance.
61 class __vmi_class_type_info
: public __class_type_info
{
63 ~__vmi_class_type_info() override
;
66 unsigned int base_count
;
67 __base_class_type_info base_info
[1];
72 namespace abi
= __cxxabiv1
;
74 // We implement a simple two-level cache for type-checking results. For each
75 // (vptr,type) pair, a hash is computed. This hash is assumed to be globally
76 // unique; if it collides, we will get false negatives, but:
77 // * such a collision would have to occur on the *first* bad access,
78 // * the probability of such a collision is low (and for a 64-bit target, is
80 // * the vptr, and thus the hash, can be affected by ASLR, so multiple runs
81 // give better coverage.
83 // The first caching layer is a small hash table with no chaining; buckets are
84 // reused as needed. The second caching layer is a large hash table with open
85 // chaining. We can freely evict from either layer since this is just a cache.
87 // FIXME: Make these hash table accesses thread-safe. The races here are benign:
88 // assuming the unsequenced loads and stores don't misbehave too badly,
89 // the worst case is false negatives or poor cache behavior, not false
90 // positives or crashes.
92 /// Find a bucket to store the given hash value in.
93 static __ubsan::HashValue
*getTypeCacheHashTableBucket(__ubsan::HashValue V
) {
94 static const unsigned HashTableSize
= 65537;
95 static __ubsan::HashValue __ubsan_vptr_hash_set
[HashTableSize
];
97 unsigned First
= (V
& 65535) ^ 1;
98 unsigned Probe
= First
;
99 for (int Tries
= 5; Tries
; --Tries
) {
100 if (!__ubsan_vptr_hash_set
[Probe
] || __ubsan_vptr_hash_set
[Probe
] == V
)
101 return &__ubsan_vptr_hash_set
[Probe
];
102 Probe
+= ((V
>> 16) & 65535) + 1;
103 if (Probe
>= HashTableSize
)
104 Probe
-= HashTableSize
;
106 // FIXME: Pick a random entry from the probe sequence to evict rather than
107 // just taking the first.
108 return &__ubsan_vptr_hash_set
[First
];
111 /// \brief Determine whether \p Derived has a \p Base base class subobject at
112 /// offset \p Offset.
113 static bool isDerivedFromAtOffset(const abi::__class_type_info
*Derived
,
114 const abi::__class_type_info
*Base
,
116 if (Derived
->__type_name
== Base
->__type_name
)
119 if (const abi::__si_class_type_info
*SI
=
120 dynamic_cast<const abi::__si_class_type_info
*>(Derived
))
121 return isDerivedFromAtOffset(SI
->__base_type
, Base
, Offset
);
123 const abi::__vmi_class_type_info
*VTI
=
124 dynamic_cast<const abi::__vmi_class_type_info
*>(Derived
);
126 // No base class subobjects.
129 // Look for a base class which is derived from \p Base at the right offset.
130 for (unsigned int base
= 0; base
!= VTI
->base_count
; ++base
) {
131 // FIXME: Curtail the recursion if this base can't possibly contain the
133 sptr OffsetHere
= VTI
->base_info
[base
].__offset_flags
>>
134 abi::__base_class_type_info::__offset_shift
;
135 if (VTI
->base_info
[base
].__offset_flags
&
136 abi::__base_class_type_info::__virtual_mask
)
137 // For now, just punt on virtual bases and say 'yes'.
138 // FIXME: OffsetHere is the offset in the vtable of the virtual base
139 // offset. Read the vbase offset out of the vtable and use it.
141 if (isDerivedFromAtOffset(VTI
->base_info
[base
].__base_type
,
142 Base
, Offset
- OffsetHere
))
149 /// \brief Find the derived-most dynamic base class of \p Derived at offset
151 static const abi::__class_type_info
*findBaseAtOffset(
152 const abi::__class_type_info
*Derived
, sptr Offset
) {
156 if (const abi::__si_class_type_info
*SI
=
157 dynamic_cast<const abi::__si_class_type_info
*>(Derived
))
158 return findBaseAtOffset(SI
->__base_type
, Offset
);
160 const abi::__vmi_class_type_info
*VTI
=
161 dynamic_cast<const abi::__vmi_class_type_info
*>(Derived
);
163 // No base class subobjects.
166 for (unsigned int base
= 0; base
!= VTI
->base_count
; ++base
) {
167 sptr OffsetHere
= VTI
->base_info
[base
].__offset_flags
>>
168 abi::__base_class_type_info::__offset_shift
;
169 if (VTI
->base_info
[base
].__offset_flags
&
170 abi::__base_class_type_info::__virtual_mask
)
171 // FIXME: Can't handle virtual bases yet.
173 if (const abi::__class_type_info
*Base
=
174 findBaseAtOffset(VTI
->base_info
[base
].__base_type
,
175 Offset
- OffsetHere
))
184 struct VtablePrefix
{
185 /// The offset from the vptr to the start of the most-derived object.
186 /// This will only be greater than zero in some virtual base class vtables
187 /// used during object con-/destruction, and will usually be exactly zero.
189 /// The type_info object describing the most-derived class type.
190 std::type_info
*TypeInfo
;
192 VtablePrefix
*getVtablePrefix(void *Vtable
) {
193 VtablePrefix
*Vptr
= reinterpret_cast<VtablePrefix
*>(Vtable
);
196 VtablePrefix
*Prefix
= Vptr
- 1;
197 if (!Prefix
->TypeInfo
)
198 // This can't possibly be a valid vtable.
205 bool __ubsan::checkDynamicType(void *Object
, void *Type
, HashValue Hash
) {
206 // A crash anywhere within this function probably means the vptr is corrupted.
207 // FIXME: Perform these checks more cautiously.
209 // Check whether this is something we've evicted from the cache.
210 HashValue
*Bucket
= getTypeCacheHashTableBucket(Hash
);
211 if (*Bucket
== Hash
) {
212 __ubsan_vptr_type_cache
[Hash
% VptrTypeCacheSize
] = Hash
;
216 void *VtablePtr
= *reinterpret_cast<void **>(Object
);
217 VtablePrefix
*Vtable
= getVtablePrefix(VtablePtr
);
221 // Check that this is actually a type_info object for a class type.
222 abi::__class_type_info
*Derived
=
223 dynamic_cast<abi::__class_type_info
*>(Vtable
->TypeInfo
);
227 abi::__class_type_info
*Base
= (abi::__class_type_info
*)Type
;
228 if (!isDerivedFromAtOffset(Derived
, Base
, -Vtable
->Offset
))
231 // Success. Cache this result.
232 __ubsan_vptr_type_cache
[Hash
% VptrTypeCacheSize
] = Hash
;
237 __ubsan::DynamicTypeInfo
238 __ubsan::getDynamicTypeInfoFromVtable(void *VtablePtr
) {
239 VtablePrefix
*Vtable
= getVtablePrefix(VtablePtr
);
241 return DynamicTypeInfo(0, 0, 0);
242 const abi::__class_type_info
*ObjectType
= findBaseAtOffset(
243 static_cast<const abi::__class_type_info
*>(Vtable
->TypeInfo
),
245 return DynamicTypeInfo(Vtable
->TypeInfo
->__type_name
, -Vtable
->Offset
,
246 ObjectType
? ObjectType
->__type_name
: "<unknown>");
249 #endif // CAN_SANITIZE_UB && !SANITIZER_WINDOWS