1 //===-- ubsan_type_hash.h ---------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Hashing of types for Clang's undefined behavior checker.
11 //===----------------------------------------------------------------------===//
12 #ifndef UBSAN_TYPE_HASH_H
13 #define UBSAN_TYPE_HASH_H
15 #include "sanitizer_common/sanitizer_common.h"
19 typedef uptr HashValue
;
21 /// \brief Information about the dynamic type of an object (extracted from its
23 class DynamicTypeInfo
{
24 const char *MostDerivedTypeName
;
26 const char *SubobjectTypeName
;
29 DynamicTypeInfo(const char *MDTN
, sptr Offset
, const char *STN
)
30 : MostDerivedTypeName(MDTN
), Offset(Offset
), SubobjectTypeName(STN
) {}
32 /// Determine whether the object had a valid dynamic type.
33 bool isValid() const { return MostDerivedTypeName
; }
34 /// Get the name of the most-derived type of the object.
35 const char *getMostDerivedTypeName() const { return MostDerivedTypeName
; }
36 /// Get the offset from the most-derived type to this base class.
37 sptr
getOffset() const { return Offset
; }
38 /// Get the name of the most-derived type at the specified offset.
39 const char *getSubobjectTypeName() const { return SubobjectTypeName
; }
42 /// \brief Get information about the dynamic type of an object.
43 DynamicTypeInfo
getDynamicTypeInfoFromObject(void *Object
);
45 /// \brief Get information about the dynamic type of an object from its vtable.
46 DynamicTypeInfo
getDynamicTypeInfoFromVtable(void *Vtable
);
48 /// \brief Check whether the dynamic type of \p Object has a \p Type subobject
50 /// \return \c true if the type matches, \c false if not.
51 bool checkDynamicType(void *Object
, void *Type
, HashValue Hash
);
53 const unsigned VptrTypeCacheSize
= 128;
55 /// A sanity check for Vtable. Offsets to top must be reasonably small
56 /// numbers (by absolute value). It's a weak check for Vtable corruption.
57 const int VptrMaxOffsetToTop
= 1<<20;
59 /// \brief A cache of the results of checkDynamicType. \c checkDynamicType would
60 /// return \c true (modulo hash collisions) if
62 /// __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] == Hash
64 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
65 HashValue __ubsan_vptr_type_cache
[VptrTypeCacheSize
];
67 /// \brief Do whatever is required by the ABI to check for std::type_info
68 /// equivalence beyond simple pointer comparison.
69 bool checkTypeInfoEquality(const void *TypeInfo1
, const void *TypeInfo2
);
71 } // namespace __ubsan
73 #endif // UBSAN_TYPE_HASH_H