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 getDynamicTypeInfoFromObject(void *Object);
43
44/// \brief Get information about the dynamic type of an object from its vtable.
45DynamicTypeInfo getDynamicTypeInfoFromVtable(void *Vtable);
46
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.
50bool checkDynamicType(void *Object, void *Type, HashValue Hash);
51
52const unsigned VptrTypeCacheSize = 128;
53
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.
56const int VptrMaxOffsetToTop = 1<<20;
57
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
63extern "C" SANITIZER_INTERFACE_ATTRIBUTE
64HashValue __ubsan_vptr_type_cache[VptrTypeCacheSize];
65
66} // namespace __ubsan
67
68#endif // UBSAN_TYPE_HASH_H
69