gcc/
[official-gcc.git] / libsanitizer / ubsan / ubsan_handlers_cxx.cc
blobbb43cc75cfc69feff4055b8bbf951f28e71ffb44
1 //===-- ubsan_handlers_cxx.cc ---------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Error logging entry points for the UBSan runtime, which are only used for C++
9 // compilations. This file is permitted to use language features which require
10 // linking against a C++ ABI library.
12 //===----------------------------------------------------------------------===//
14 #include "ubsan_handlers_cxx.h"
15 #include "ubsan_diag.h"
16 #include "ubsan_type_hash.h"
18 #include "sanitizer_common/sanitizer_common.h"
20 using namespace __sanitizer;
21 using namespace __ubsan;
23 namespace __ubsan {
24 extern const char *TypeCheckKinds[];
27 static void HandleDynamicTypeCacheMiss(
28 DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash,
29 bool Abort) {
30 if (checkDynamicType((void*)Pointer, Data->TypeInfo, Hash))
31 // Just a cache miss. The type matches after all.
32 return;
34 SourceLocation Loc = Data->Loc.acquire();
35 if (Loc.isDisabled())
36 return;
38 Diag(Loc, DL_Error,
39 "%0 address %1 which does not point to an object of type %2")
40 << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type;
42 // If possible, say what type it actually points to.
43 DynamicTypeInfo DTI = getDynamicTypeInfo((void*)Pointer);
44 if (!DTI.isValid())
45 Diag(Pointer, DL_Note, "object has invalid vptr")
46 << MangledName(DTI.getMostDerivedTypeName())
47 << Range(Pointer, Pointer + sizeof(uptr), "invalid vptr");
48 else if (!DTI.getOffset())
49 Diag(Pointer, DL_Note, "object is of type %0")
50 << MangledName(DTI.getMostDerivedTypeName())
51 << Range(Pointer, Pointer + sizeof(uptr), "vptr for %0");
52 else
53 // FIXME: Find the type at the specified offset, and include that
54 // in the note.
55 Diag(Pointer - DTI.getOffset(), DL_Note,
56 "object is base class subobject at offset %0 within object of type %1")
57 << DTI.getOffset() << MangledName(DTI.getMostDerivedTypeName())
58 << MangledName(DTI.getSubobjectTypeName())
59 << Range(Pointer, Pointer + sizeof(uptr), "vptr for %2 base class of %1");
61 if (Abort)
62 Die();
65 void __ubsan::__ubsan_handle_dynamic_type_cache_miss(
66 DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
67 HandleDynamicTypeCacheMiss(Data, Pointer, Hash, false);
69 void __ubsan::__ubsan_handle_dynamic_type_cache_miss_abort(
70 DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
71 HandleDynamicTypeCacheMiss(Data, Pointer, Hash, true);