diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-12-18 06:30:32 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-12-18 06:30:32 +0000 |
commit | 25ee97fe8ada76755c8bd1087fac9cc3cd03b28c (patch) | |
tree | 7eb8ac2d90d4635d1c727ebd835c7aa4c90d290b /lib/ubsan/ubsan_type_hash.cc | |
parent | 01e9a38b8cfe8967efb259978b754c3a9f0c380c (diff) |
ubsan: When diagnosing something wrong somewhere in memory, emit a note
pointing at the bad location and a snippet of nearby memory values. This is
strictly best-effort; reading these bytes to display the note could lead to a
seg fault, and that's explicitly OK.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@170415 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/ubsan/ubsan_type_hash.cc')
-rw-r--r-- | lib/ubsan/ubsan_type_hash.cc | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/lib/ubsan/ubsan_type_hash.cc b/lib/ubsan/ubsan_type_hash.cc index 1f6a3dbd9..ee17af7d8 100644 --- a/lib/ubsan/ubsan_type_hash.cc +++ b/lib/ubsan/ubsan_type_hash.cc @@ -25,7 +25,7 @@ namespace std { class type_info { public: virtual ~type_info(); - private: + const char *__type_name; }; } @@ -160,8 +160,14 @@ struct VtablePrefix { std::type_info *TypeInfo; }; VtablePrefix *getVtablePrefix(void *Object) { - VtablePrefix **Ptr = reinterpret_cast<VtablePrefix**>(Object); - return *Ptr - 1; + VtablePrefix **VptrPtr = reinterpret_cast<VtablePrefix**>(Object); + if (!*VptrPtr) + return 0; + VtablePrefix *Prefix = *VptrPtr - 1; + if (Prefix->Offset > 0 || !Prefix->TypeInfo) + // This can't possibly be a valid vtable. + return 0; + return Prefix; } } @@ -178,8 +184,7 @@ bool __ubsan::checkDynamicType(void *Object, void *Type, HashValue Hash) { } VtablePrefix *Vtable = getVtablePrefix(Object); - if (Vtable + 1 == 0 || Vtable->Offset > 0) - // This can't possibly be a valid vtable. + if (!Vtable) return false; // Check that this is actually a type_info object for a class type. @@ -197,3 +202,10 @@ bool __ubsan::checkDynamicType(void *Object, void *Type, HashValue Hash) { *Bucket = Hash; return true; } + +__ubsan::DynamicTypeInfo __ubsan::getDynamicTypeInfo(void *Object) { + VtablePrefix *Vtable = getVtablePrefix(Object); + if (!Vtable) + return DynamicTypeInfo(0, 0); + return DynamicTypeInfo(Vtable->TypeInfo->__type_name, -Vtable->Offset); +} |