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
18namespace __ubsan {
19
20typedef uptr HashValue;
21
22/// \brief Information about the dynamic type of an object (extracted from its
23/// vptr).
24class DynamicTypeInfo {
25  const char *MostDerivedTypeName;
26  sptr Offset;
27  const char *SubobjectTypeName;
28
29public:
30  DynamicTypeInfo(const char *MDTN, sptr Offset, const char *STN)
31    : MostDerivedTypeName(MDTN), Offset(Offset), SubobjectTypeName(STN) {}
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; }
39  /// Get the name of the most-derived type at the specified offset.
40  const char *getSubobjectTypeName() const { return SubobjectTypeName; }
41};
42
43/// \brief Get information about the dynamic type of an object.
44DynamicTypeInfo getDynamicTypeInfo(void *Object);
45
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.
49bool checkDynamicType(void *Object, void *Type, HashValue Hash);
50
51const 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
58extern "C" SANITIZER_INTERFACE_ATTRIBUTE
59HashValue __ubsan_vptr_type_cache[VptrTypeCacheSize];
60
61} // namespace __ubsan
62
63#endif // UBSAN_TYPE_HASH_H
64