Index: lib/ubsan/ubsan_type_hash.h =================================================================== --- lib/ubsan/ubsan_type_hash.h +++ lib/ubsan/ubsan_type_hash.h @@ -53,6 +53,10 @@ const unsigned VptrTypeCacheSize = 128; +/// A sanity check for Vtable. Offsets must be non-negative and +/// no larger than this value. It's a weak check for Vtable corruption. +const int VptrMaxOffset = 1<<20; + /// \brief A cache of the results of checkDynamicType. \c checkDynamicType would /// return \c true (modulo hash collisions) if /// \code Index: lib/ubsan/ubsan_type_hash_itanium.cc =================================================================== --- lib/ubsan/ubsan_type_hash_itanium.cc +++ lib/ubsan/ubsan_type_hash_itanium.cc @@ -11,6 +11,8 @@ // //===----------------------------------------------------------------------===// +#include + #include "sanitizer_common/sanitizer_platform.h" #include "ubsan_platform.h" #if CAN_SANITIZE_UB && !SANITIZER_WINDOWS @@ -221,6 +223,11 @@ VtablePrefix *Vtable = getVtablePrefix(VtablePtr); if (!Vtable) return false; + fprintf(stderr, "Vtable->Offset: %ld\n", Vtable->Offset); + if (Vtable->Offset < -VptrMaxOffset || Vtable->Offset > VptrMaxOffset) { + // Too large or too small offset are signs of Vtable corruption. + return false; + } // Check that this is actually a type_info object for a class type. abi::__class_type_info *Derived = @@ -241,7 +248,8 @@ __ubsan::DynamicTypeInfo __ubsan::getDynamicTypeInfoFromVtable(void *VtablePtr) { VtablePrefix *Vtable = getVtablePrefix(VtablePtr); - if (!Vtable) + if (!Vtable || Vtable->Offset < -VptrMaxOffset || + Vtable->Offset > VptrMaxOffset) return DynamicTypeInfo(0, 0, 0); const abi::__class_type_info *ObjectType = findBaseAtOffset( static_cast(Vtable->TypeInfo), Index: test/ubsan/TestCases/TypeCheck/vptr_itanium.cpp =================================================================== --- /dev/null +++ test/ubsan/TestCases/TypeCheck/vptr_itanium.cpp @@ -0,0 +1,36 @@ +// RUN: %clangxx -frtti -fsanitize=vptr -fno-sanitize-recover=vptr -g %s -O3 -o %t +// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-CORRUPTED-VTABLE --strict-whitespace + +// UNSUPPORTED: win32 +// REQUIRES: stable-runtime, cxxabi +#include + +#include + +struct S { + S() {} + ~S() {} + virtual int v() { return 0; } +}; + +// See the proper definition in ubsan_type_hash_itanium.cc +struct VtablePrefix { + signed long Offset; + std::type_info *TypeInfo; +}; + +int main(int argc, char **argv) { + // Test that we don't crash on corrupted vtable when + // offset is too large or too small. + VtablePrefix Prefix[2]; + Prefix[0].Offset = 1<<21; // Greater than VptrMaxOffset + S Obj; + void *Ptr = &Obj; + // Hack Vtable ptr for Obj. + *reinterpret_cast(Ptr) = static_cast(&Prefix[1]); + + // CHECK-CORRUPTED-VTABLE: vptr_itanium.cpp:[[@LINE+3]]:16: runtime error: member call on address [[PTR:0x[0-9a-f]*]] which does not point to an object of type 'S' + // CHECK-CORRUPTED-VTABLE-NEXT: [[PTR]]: note: object has invalid vptr + S* Ptr2 = reinterpret_cast(Ptr); + return Ptr2->v(); +}