blob: 695fed905a738fa0f2395c5b0f5911b816fc79dd [file] [log] [blame]
Richard Smitheda8bd02012-10-25 02:07:02 +00001//===-- 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
Richard Smith25ee97f2012-12-18 06:30:32 +000022/// \brief Information about the dynamic type of an object (extracted from its
23/// vptr).
24class DynamicTypeInfo {
25 const char *MostDerivedTypeName;
26 sptr Offset;
Richard Smith0ad23f72012-12-18 09:30:21 +000027 const char *SubobjectTypeName;
Richard Smith25ee97f2012-12-18 06:30:32 +000028
29public:
Richard Smith0ad23f72012-12-18 09:30:21 +000030 DynamicTypeInfo(const char *MDTN, sptr Offset, const char *STN)
31 : MostDerivedTypeName(MDTN), Offset(Offset), SubobjectTypeName(STN) {}
Richard Smith25ee97f2012-12-18 06:30:32 +000032
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 Smith0ad23f72012-12-18 09:30:21 +000039 /// Get the name of the most-derived type at the specified offset.
40 const char *getSubobjectTypeName() const { return SubobjectTypeName; }
Richard Smith25ee97f2012-12-18 06:30:32 +000041};
42
43/// \brief Get information about the dynamic type of an object.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080044DynamicTypeInfo getDynamicTypeInfoFromObject(void *Object);
45
46/// \brief Get information about the dynamic type of an object from its vtable.
47DynamicTypeInfo getDynamicTypeInfoFromVtable(void *Vtable);
Richard Smith25ee97f2012-12-18 06:30:32 +000048
Richard Smitheda8bd02012-10-25 02:07:02 +000049/// \brief Check whether the dynamic type of \p Object has a \p Type subobject
50/// at offset 0.
51/// \return \c true if the type matches, \c false if not.
Richard Smith26a725f2012-10-29 20:54:34 +000052bool checkDynamicType(void *Object, void *Type, HashValue Hash);
53
54const unsigned VptrTypeCacheSize = 128;
55
56/// \brief A cache of the results of checkDynamicType. \c checkDynamicType would
57/// return \c true (modulo hash collisions) if
58/// \code
59/// __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] == Hash
60/// \endcode
Will Dietz18af39e2013-01-17 17:14:12 +000061extern "C" SANITIZER_INTERFACE_ATTRIBUTE
62HashValue __ubsan_vptr_type_cache[VptrTypeCacheSize];
Richard Smitheda8bd02012-10-25 02:07:02 +000063
64} // namespace __ubsan
65
66#endif // UBSAN_TYPE_HASH_H