Richard Smith | eda8bd0 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 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 | |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 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; |
Richard Smith | 0ad23f7 | 2012-12-18 09:30:21 +0000 | [diff] [blame] | 27 | const char *SubobjectTypeName; |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 28 | |
| 29 | public: |
Richard Smith | 0ad23f7 | 2012-12-18 09:30:21 +0000 | [diff] [blame] | 30 | DynamicTypeInfo(const char *MDTN, sptr Offset, const char *STN) |
| 31 | : MostDerivedTypeName(MDTN), Offset(Offset), SubobjectTypeName(STN) {} |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 32 | |
| 33 | /// Determine whether the object had a valid dynamic type. |
| 34 | bool isValid() const { return MostDerivedTypeName; } |
| 35 | /// Get the name of the most-derived type of the object. |
| 36 | const char *getMostDerivedTypeName() const { return MostDerivedTypeName; } |
| 37 | /// Get the offset from the most-derived type to this base class. |
| 38 | sptr getOffset() const { return Offset; } |
Richard Smith | 0ad23f7 | 2012-12-18 09:30:21 +0000 | [diff] [blame] | 39 | /// Get the name of the most-derived type at the specified offset. |
| 40 | const char *getSubobjectTypeName() const { return SubobjectTypeName; } |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /// \brief Get information about the dynamic type of an object. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 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); |
Richard Smith | 25ee97f | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 48 | |
Richard Smith | eda8bd0 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 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. |
Richard Smith | 26a725f | 2012-10-29 20:54:34 +0000 | [diff] [blame] | 52 | bool checkDynamicType(void *Object, void *Type, HashValue Hash); |
| 53 | |
| 54 | const unsigned VptrTypeCacheSize = 128; |
| 55 | |
| 56 | /// \brief A cache of the results of checkDynamicType. \c checkDynamicType would |
| 57 | /// return \c true (modulo hash collisions) if |
| 58 | /// \code |
| 59 | /// __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] == Hash |
| 60 | /// \endcode |
Will Dietz | 18af39e | 2013-01-17 17:14:12 +0000 | [diff] [blame] | 61 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE |
| 62 | HashValue __ubsan_vptr_type_cache[VptrTypeCacheSize]; |
Richard Smith | eda8bd0 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 63 | |
| 64 | } // namespace __ubsan |
| 65 | |
| 66 | #endif // UBSAN_TYPE_HASH_H |