1//===-- ubsan_type_hash.h ---------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Hashing of types for Clang's undefined behavior checker.
10//
11//===----------------------------------------------------------------------===//
12#ifndef UBSAN_TYPE_HASH_H
13#define UBSAN_TYPE_HASH_H
14
15#include "sanitizer_common/sanitizer_common.h"
16
17namespace __ubsan {
18
19typedef uptr HashValue;
20
21/// \brief Information about the dynamic type of an object (extracted from its
22/// vptr).
23class DynamicTypeInfo {
24 const char *MostDerivedTypeName;
25 sptr Offset;
26 const char *SubobjectTypeName;
27
28public:
29 DynamicTypeInfo(const char *MDTN, sptr Offset, const char *STN)
30 : MostDerivedTypeName(MDTN), Offset(Offset), SubobjectTypeName(STN) {}
31
32 /// Determine whether the object had a valid dynamic type.
33 bool isValid() const { return MostDerivedTypeName; }
34 /// Get the name of the most-derived type of the object.
35 const char *getMostDerivedTypeName() const { return MostDerivedTypeName; }
36 /// Get the offset from the most-derived type to this base class.
37 sptr getOffset() const { return Offset; }
38 /// Get the name of the most-derived type at the specified offset.
39 const char *getSubobjectTypeName() const { return SubobjectTypeName; }
40};
41
42/// \brief Get information about the dynamic type of an object.
43DynamicTypeInfo getDynamicTypeInfoFromObject(void *Object);
44
45/// \brief Get information about the dynamic type of an object from its vtable.
46DynamicTypeInfo getDynamicTypeInfoFromVtable(void *Vtable);
47
48/// \brief Check whether the dynamic type of \p Object has a \p Type subobject
49/// at offset 0.
50/// \return \c true if the type matches, \c false if not.
51bool checkDynamicType(void *Object, void *Type, HashValue Hash);
52
53const unsigned VptrTypeCacheSize = 128;
54
55/// A sanity check for Vtable. Offsets to top must be reasonably small
56/// numbers (by absolute value). It's a weak check for Vtable corruption.
57const int VptrMaxOffsetToTop = 1<<20;
58
59/// \brief A cache of the results of checkDynamicType. \c checkDynamicType would
60/// return \c true (modulo hash collisions) if
61/// \code
62/// __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] == Hash
63/// \endcode
64extern "C" SANITIZER_INTERFACE_ATTRIBUTE
65HashValue __ubsan_vptr_type_cache[VptrTypeCacheSize];
66
67/// \brief Do whatever is required by the ABI to check for std::type_info
68/// equivalence beyond simple pointer comparison.
69bool checkTypeInfoEquality(const void *TypeInfo1, const void *TypeInfo2);
70
71} // namespace __ubsan
72
73#endif // UBSAN_TYPE_HASH_H
74

source code of compiler-rt/lib/ubsan/ubsan_type_hash.h