1eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith//===-- ubsan_type_hash.h ---------------------------------------*- C++ -*-===//
2eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith//
3eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith//                     The LLVM Compiler Infrastructure
4eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith//
5eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith// This file is distributed under the University of Illinois Open Source
6eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith// License. See LICENSE.TXT for details.
7eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith//
8eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith//===----------------------------------------------------------------------===//
9eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith//
10eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith// Hashing of types for Clang's undefined behavior checker.
11eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith//
12eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith//===----------------------------------------------------------------------===//
13eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith#ifndef UBSAN_TYPE_HASH_H
14eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith#define UBSAN_TYPE_HASH_H
15eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith
16eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith#include "sanitizer_common/sanitizer_common.h"
17eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith
18eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smithnamespace __ubsan {
19eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith
20eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smithtypedef uptr HashValue;
21eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith
2225ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith/// \brief Information about the dynamic type of an object (extracted from its
2325ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith/// vptr).
2425ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smithclass DynamicTypeInfo {
2525ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith  const char *MostDerivedTypeName;
2625ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith  sptr Offset;
270ad23f7f860e27b8b9a889be665cfaea830503ceRichard Smith  const char *SubobjectTypeName;
2825ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith
2925ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smithpublic:
300ad23f7f860e27b8b9a889be665cfaea830503ceRichard Smith  DynamicTypeInfo(const char *MDTN, sptr Offset, const char *STN)
310ad23f7f860e27b8b9a889be665cfaea830503ceRichard Smith    : MostDerivedTypeName(MDTN), Offset(Offset), SubobjectTypeName(STN) {}
3225ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith
3325ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith  /// Determine whether the object had a valid dynamic type.
3425ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith  bool isValid() const { return MostDerivedTypeName; }
3525ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith  /// Get the name of the most-derived type of the object.
3625ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith  const char *getMostDerivedTypeName() const { return MostDerivedTypeName; }
3725ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith  /// Get the offset from the most-derived type to this base class.
3825ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith  sptr getOffset() const { return Offset; }
390ad23f7f860e27b8b9a889be665cfaea830503ceRichard Smith  /// Get the name of the most-derived type at the specified offset.
400ad23f7f860e27b8b9a889be665cfaea830503ceRichard Smith  const char *getSubobjectTypeName() const { return SubobjectTypeName; }
4125ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith};
4225ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith
4325ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith/// \brief Get information about the dynamic type of an object.
44799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga NainarDynamicTypeInfo getDynamicTypeInfoFromObject(void *Object);
45799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar
46799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga Nainar/// \brief Get information about the dynamic type of an object from its vtable.
47799172d60d32feb1acba1a6867f3a9c39a999e5cPirama Arumuga NainarDynamicTypeInfo getDynamicTypeInfoFromVtable(void *Vtable);
4825ee97fe8ada76755c8bd1087fac9cc3cd03b28cRichard Smith
49eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith/// \brief Check whether the dynamic type of \p Object has a \p Type subobject
50eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith/// at offset 0.
51eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith/// \return \c true if the type matches, \c false if not.
5226a725f0b2fd30b98adae6353b432d0c955e10bdRichard Smithbool checkDynamicType(void *Object, void *Type, HashValue Hash);
5326a725f0b2fd30b98adae6353b432d0c955e10bdRichard Smith
5426a725f0b2fd30b98adae6353b432d0c955e10bdRichard Smithconst unsigned VptrTypeCacheSize = 128;
5526a725f0b2fd30b98adae6353b432d0c955e10bdRichard Smith
56c58a43648cd6121c51a2e795a28e2ef90d7813e6Pirama Arumuga Nainar/// A sanity check for Vtable. Offsets to top must be reasonably small
57c58a43648cd6121c51a2e795a28e2ef90d7813e6Pirama Arumuga Nainar/// numbers (by absolute value). It's a weak check for Vtable corruption.
58c58a43648cd6121c51a2e795a28e2ef90d7813e6Pirama Arumuga Nainarconst int VptrMaxOffsetToTop = 1<<20;
59c58a43648cd6121c51a2e795a28e2ef90d7813e6Pirama Arumuga Nainar
6026a725f0b2fd30b98adae6353b432d0c955e10bdRichard Smith/// \brief A cache of the results of checkDynamicType. \c checkDynamicType would
6126a725f0b2fd30b98adae6353b432d0c955e10bdRichard Smith/// return \c true (modulo hash collisions) if
6226a725f0b2fd30b98adae6353b432d0c955e10bdRichard Smith/// \code
6326a725f0b2fd30b98adae6353b432d0c955e10bdRichard Smith///   __ubsan_vptr_type_cache[Hash % VptrTypeCacheSize] == Hash
6426a725f0b2fd30b98adae6353b432d0c955e10bdRichard Smith/// \endcode
6518af39e73d651f1903f2d46ac51b3842b378763fWill Dietzextern "C" SANITIZER_INTERFACE_ATTRIBUTE
6618af39e73d651f1903f2d46ac51b3842b378763fWill DietzHashValue __ubsan_vptr_type_cache[VptrTypeCacheSize];
67eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith
68eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith} // namespace __ubsan
69eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith
70eda8bd0fc07df35c9ad7de5b698bb717b063e7afRichard Smith#endif // UBSAN_TYPE_HASH_H
71