1 //===-- ubsan_type_hash.cc ------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of a hash table for fast checking of inheritance
11 // relationships. This file is only linked into C++ compilations, and is
12 // permitted to use language features which require a C++ ABI library.
14 //===----------------------------------------------------------------------===//
16 #include "ubsan_type_hash.h"
18 #include "sanitizer_common/sanitizer_common.h"
20 // The following are intended to be binary compatible with the definitions
21 // given in the Itanium ABI. We make no attempt to be ODR-compatible with
22 // those definitions, since existing ABI implementations aren't.
29 const char *__type_name;
33 namespace __cxxabiv1 {
35 /// Type info for classes with no bases, and base class for type info for
36 /// classes with bases.
37 class __class_type_info : public std::type_info {
38 virtual ~__class_type_info();
41 /// Type info for classes with simple single public inheritance.
42 class __si_class_type_info : public __class_type_info {
44 virtual ~__si_class_type_info();
46 const __class_type_info *__base_type;
49 class __base_class_type_info {
51 const __class_type_info *__base_type;
54 enum __offset_flags_masks {
61 /// Type info for classes with multiple, virtual, or non-public inheritance.
62 class __vmi_class_type_info : public __class_type_info {
64 virtual ~__vmi_class_type_info();
67 unsigned int base_count;
68 __base_class_type_info base_info[1];
73 namespace abi = __cxxabiv1;
75 // We implement a simple two-level cache for type-checking results. For each
76 // (vptr,type) pair, a hash is computed. This hash is assumed to be globally
77 // unique; if it collides, we will get false negatives, but:
78 // * such a collision would have to occur on the *first* bad access,
79 // * the probability of such a collision is low (and for a 64-bit target, is
81 // * the vptr, and thus the hash, can be affected by ASLR, so multiple runs
82 // give better coverage.
84 // The first caching layer is a small hash table with no chaining; buckets are
85 // reused as needed. The second caching layer is a large hash table with open
86 // chaining. We can freely evict from either layer since this is just a cache.
88 // FIXME: Make these hash table accesses thread-safe. The races here are benign:
89 // assuming the unsequenced loads and stores don't misbehave too badly,
90 // the worst case is false negatives or poor cache behavior, not false
91 // positives or crashes.
93 /// Find a bucket to store the given hash value in.
94 static __ubsan::HashValue *getTypeCacheHashTableBucket(__ubsan::HashValue V) {
95 static const unsigned HashTableSize = 65537;
96 static __ubsan::HashValue __ubsan_vptr_hash_set[HashTableSize];
98 unsigned First = (V & 65535) ^ 1;
99 unsigned Probe = First;
100 for (int Tries = 5; Tries; --Tries) {
101 if (!__ubsan_vptr_hash_set[Probe] || __ubsan_vptr_hash_set[Probe] == V)
102 return &__ubsan_vptr_hash_set[Probe];
103 Probe += ((V >> 16) & 65535) + 1;
104 if (Probe >= HashTableSize)
105 Probe -= HashTableSize;
107 // FIXME: Pick a random entry from the probe sequence to evict rather than
108 // just taking the first.
109 return &__ubsan_vptr_hash_set[First];
112 /// A cache of recently-checked hashes. Mini hash table with "random" evictions.
114 __ubsan::__ubsan_vptr_type_cache[__ubsan::VptrTypeCacheSize];
116 /// \brief Determine whether \p Derived has a \p Base base class subobject at
117 /// offset \p Offset.
118 static bool isDerivedFromAtOffset(sptr Object,
119 const abi::__class_type_info *Derived,
120 const abi::__class_type_info *Base,
122 if (Derived->__type_name == Base->__type_name)
125 if (const abi::__si_class_type_info *SI =
126 dynamic_cast<const abi::__si_class_type_info*>(Derived))
127 return isDerivedFromAtOffset(Object, SI->__base_type, Base, Offset);
129 const abi::__vmi_class_type_info *VTI =
130 dynamic_cast<const abi::__vmi_class_type_info*>(Derived);
132 // No base class subobjects.
135 // Look for a base class which is derived from \p Base at the right offset.
136 for (unsigned int base = 0; base != VTI->base_count; ++base) {
137 // FIXME: Curtail the recursion if this base can't possibly contain the
139 sptr OffsetHere = VTI->base_info[base].__offset_flags >>
140 abi::__base_class_type_info::__offset_shift;
141 if (VTI->base_info[base].__offset_flags &
142 abi::__base_class_type_info::__virtual_mask) {
143 sptr VTable = *reinterpret_cast<const sptr *>(Object);
144 OffsetHere = *reinterpret_cast<const sptr *>(VTable + OffsetHere);
146 if (isDerivedFromAtOffset(Object + OffsetHere,
147 VTI->base_info[base].__base_type, Base,
148 Offset - OffsetHere))
155 /// \brief Find the derived-most dynamic base class of \p Derived at offset
157 static const abi::__class_type_info *
158 findBaseAtOffset(sptr Object, const abi::__class_type_info *Derived,
163 if (const abi::__si_class_type_info *SI =
164 dynamic_cast<const abi::__si_class_type_info*>(Derived))
165 return findBaseAtOffset(Object, SI->__base_type, Offset);
167 const abi::__vmi_class_type_info *VTI =
168 dynamic_cast<const abi::__vmi_class_type_info*>(Derived);
170 // No base class subobjects.
173 for (unsigned int base = 0; base != VTI->base_count; ++base) {
174 sptr OffsetHere = VTI->base_info[base].__offset_flags >>
175 abi::__base_class_type_info::__offset_shift;
176 if (VTI->base_info[base].__offset_flags &
177 abi::__base_class_type_info::__virtual_mask) {
178 sptr VTable = *reinterpret_cast<const sptr *>(Object);
179 OffsetHere = *reinterpret_cast<const sptr *>(VTable + OffsetHere);
181 if (const abi::__class_type_info *Base = findBaseAtOffset(
182 Object + OffsetHere, VTI->base_info[base].__base_type,
183 Offset - OffsetHere))
192 struct VtablePrefix {
193 /// The offset from the vptr to the start of the most-derived object.
194 /// This should never be greater than zero, and will usually be exactly
197 /// The type_info object describing the most-derived class type.
198 std::type_info *TypeInfo;
200 VtablePrefix *getVtablePrefix(void *Object) {
201 VtablePrefix **VptrPtr = reinterpret_cast<VtablePrefix**>(Object);
204 VtablePrefix *Prefix = *VptrPtr - 1;
205 if (Prefix->Offset > 0 || !Prefix->TypeInfo)
206 // This can't possibly be a valid vtable.
213 bool __ubsan::checkDynamicType(void *Object, void *Type, HashValue Hash) {
214 // A crash anywhere within this function probably means the vptr is corrupted.
215 // FIXME: Perform these checks more cautiously.
217 // Check whether this is something we've evicted from the cache.
218 HashValue *Bucket = getTypeCacheHashTableBucket(Hash);
219 if (*Bucket == Hash) {
220 __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] = Hash;
224 VtablePrefix *Vtable = getVtablePrefix(Object);
228 // Check that this is actually a type_info object for a class type.
229 abi::__class_type_info *Derived =
230 dynamic_cast<abi::__class_type_info*>(Vtable->TypeInfo);
234 abi::__class_type_info *Base = (abi::__class_type_info*)Type;
235 if (!isDerivedFromAtOffset(reinterpret_cast<sptr>(Object), Derived, Base,
239 // Success. Cache this result.
240 __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] = Hash;
245 __ubsan::DynamicTypeInfo __ubsan::getDynamicTypeInfo(void *Object) {
246 VtablePrefix *Vtable = getVtablePrefix(Object);
248 return DynamicTypeInfo(0, 0, 0);
249 const abi::__class_type_info *ObjectType = findBaseAtOffset(
250 reinterpret_cast<sptr>(Object),
251 static_cast<const abi::__class_type_info *>(Vtable->TypeInfo),
253 return DynamicTypeInfo(Vtable->TypeInfo->__type_name, -Vtable->Offset,
254 ObjectType ? ObjectType->__type_name : "<unknown>");