1 //===-- ubsan_type_hash.h ---------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Hashing of types for Clang's undefined behavior checker. 11 // 12 //===----------------------------------------------------------------------===// 13 #ifndef UBSAN_TYPE_HASH_H 14 #define UBSAN_TYPE_HASH_H 15 16 #include "sanitizer_common/sanitizer_common.h" 17 18 namespace __ubsan { 19 20 typedef uptr HashValue; 21 22 /// \brief Information about the dynamic type of an object (extracted from its 23 /// vptr). 24 class DynamicTypeInfo { 25 const char *MostDerivedTypeName; 26 sptr Offset; 27 const char *SubobjectTypeName; 28 29 public: DynamicTypeInfo(const char * MDTN,sptr Offset,const char * STN)30 DynamicTypeInfo(const char *MDTN, sptr Offset, const char *STN) 31 : MostDerivedTypeName(MDTN), Offset(Offset), SubobjectTypeName(STN) {} 32 33 /// Determine whether the object had a valid dynamic type. isValid()34 bool isValid() const { return MostDerivedTypeName; } 35 /// Get the name of the most-derived type of the object. getMostDerivedTypeName()36 const char *getMostDerivedTypeName() const { return MostDerivedTypeName; } 37 /// Get the offset from the most-derived type to this base class. getOffset()38 sptr getOffset() const { return Offset; } 39 /// Get the name of the most-derived type at the specified offset. getSubobjectTypeName()40 const char *getSubobjectTypeName() const { return SubobjectTypeName; } 41 }; 42 43 /// \brief Get information about the dynamic type of an object. 44 DynamicTypeInfo getDynamicTypeInfoFromObject(void *Object); 45 46 /// \brief Get information about the dynamic type of an object from its vtable. 47 DynamicTypeInfo getDynamicTypeInfoFromVtable(void *Vtable); 48 49 /// \brief Check whether the dynamic type of \p Object has a \p Type subobject 50 /// at offset 0. 51 /// \return \c true if the type matches, \c false if not. 52 bool checkDynamicType(void *Object, void *Type, HashValue Hash); 53 54 const unsigned VptrTypeCacheSize = 128; 55 56 /// A sanity check for Vtable. Offsets to top must be reasonably small 57 /// numbers (by absolute value). It's a weak check for Vtable corruption. 58 const int VptrMaxOffsetToTop = 1<<20; 59 60 /// \brief A cache of the results of checkDynamicType. \c checkDynamicType would 61 /// return \c true (modulo hash collisions) if 62 /// \code 63 /// __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] == Hash 64 /// \endcode 65 extern "C" SANITIZER_INTERFACE_ATTRIBUTE 66 HashValue __ubsan_vptr_type_cache[VptrTypeCacheSize]; 67 68 } // namespace __ubsan 69 70 #endif // UBSAN_TYPE_HASH_H 71