2017-01-22 Sebastian Perta <sebastian.perta@renesas.com>
[official-gcc.git] / libsanitizer / ubsan / ubsan_type_hash.h
blob610fcb44ea7dc27d92e02ff885cdcf9835486476
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
14 #include "sanitizer_common/sanitizer_common.h"
16 namespace __ubsan {
18 typedef uptr HashValue;
20 /// \brief Information about the dynamic type of an object (extracted from its
21 /// vptr).
22 class DynamicTypeInfo {
23 const char *MostDerivedTypeName;
24 sptr Offset;
25 const char *SubobjectTypeName;
27 public:
28 DynamicTypeInfo(const char *MDTN, sptr Offset, const char *STN)
29 : MostDerivedTypeName(MDTN), Offset(Offset), SubobjectTypeName(STN) {}
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; }
41 /// \brief Get information about the dynamic type of an object.
42 DynamicTypeInfo getDynamicTypeInfoFromObject(void *Object);
44 /// \brief Get information about the dynamic type of an object from its vtable.
45 DynamicTypeInfo getDynamicTypeInfoFromVtable(void *Vtable);
47 /// \brief Check whether the dynamic type of \p Object has a \p Type subobject
48 /// at offset 0.
49 /// \return \c true if the type matches, \c false if not.
50 bool checkDynamicType(void *Object, void *Type, HashValue Hash);
52 const unsigned VptrTypeCacheSize = 128;
54 /// A sanity check for Vtable. Offsets to top must be reasonably small
55 /// numbers (by absolute value). It's a weak check for Vtable corruption.
56 const int VptrMaxOffsetToTop = 1<<20;
58 /// \brief A cache of the results of checkDynamicType. \c checkDynamicType would
59 /// return \c true (modulo hash collisions) if
60 /// \code
61 /// __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] == Hash
62 /// \endcode
63 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
64 HashValue __ubsan_vptr_type_cache[VptrTypeCacheSize];
66 } // namespace __ubsan
68 #endif // UBSAN_TYPE_HASH_H