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