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. |
| 44 | DynamicTypeInfo getDynamicTypeInfo(void *Object); |
| 45 | |
Richard Smith | eda8bd0 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 46 | /// \brief Check whether the dynamic type of \p Object has a \p Type subobject |
| 47 | /// at offset 0. |
| 48 | /// \return \c true if the type matches, \c false if not. |
Richard Smith | 26a725f | 2012-10-29 20:54:34 +0000 | [diff] [blame] | 49 | bool checkDynamicType(void *Object, void *Type, HashValue Hash); |
| 50 | |
| 51 | const unsigned VptrTypeCacheSize = 128; |
| 52 | |
| 53 | /// \brief A cache of the results of checkDynamicType. \c checkDynamicType would |
| 54 | /// return \c true (modulo hash collisions) if |
| 55 | /// \code |
| 56 | /// __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] == Hash |
| 57 | /// \endcode |
Will Dietz | 18af39e | 2013-01-17 17:14:12 +0000 | [diff] [blame] | 58 | extern "C" SANITIZER_INTERFACE_ATTRIBUTE |
| 59 | HashValue __ubsan_vptr_type_cache[VptrTypeCacheSize]; |
Richard Smith | eda8bd0 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 60 | |
| 61 | } // namespace __ubsan |
| 62 | |
| 63 | #endif // UBSAN_TYPE_HASH_H |