1 //===-- ubsan_handlers_cxx.cc ---------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Error logging entry points for the UBSan runtime, which are only used for C++
11 // compilations. This file is permitted to use language features which require
12 // linking against a C++ ABI library.
14 //===----------------------------------------------------------------------===//
16 #include "ubsan_platform.h"
18 #include "ubsan_handlers_cxx.h"
19 #include "ubsan_diag.h"
20 #include "ubsan_type_hash.h"
22 #include "sanitizer_common/sanitizer_common.h"
23 #include "sanitizer_common/sanitizer_suppressions.h"
25 using namespace __sanitizer;
26 using namespace __ubsan;
29 extern const char *TypeCheckKinds[];
32 static void HandleDynamicTypeCacheMiss(
33 DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash,
35 if (checkDynamicType((void*)Pointer, Data->TypeInfo, Hash))
36 // Just a cache miss. The type matches after all.
39 // Check if error report should be suppressed.
40 DynamicTypeInfo DTI = getDynamicTypeInfoFromObject((void*)Pointer);
41 if (DTI.isValid() && IsVptrCheckSuppressed(DTI.getMostDerivedTypeName()))
44 SourceLocation Loc = Data->Loc.acquire();
48 ScopedReport R(Opts, Loc);
51 "%0 address %1 which does not point to an object of type %2")
52 << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type;
54 // If possible, say what type it actually points to.
56 Diag(Pointer, DL_Note, "object has invalid vptr")
57 << TypeName(DTI.getMostDerivedTypeName())
58 << Range(Pointer, Pointer + sizeof(uptr), "invalid vptr");
59 else if (!DTI.getOffset())
60 Diag(Pointer, DL_Note, "object is of type %0")
61 << TypeName(DTI.getMostDerivedTypeName())
62 << Range(Pointer, Pointer + sizeof(uptr), "vptr for %0");
64 // FIXME: Find the type at the specified offset, and include that
66 Diag(Pointer - DTI.getOffset(), DL_Note,
67 "object is base class subobject at offset %0 within object of type %1")
68 << DTI.getOffset() << TypeName(DTI.getMostDerivedTypeName())
69 << TypeName(DTI.getSubobjectTypeName())
70 << Range(Pointer, Pointer + sizeof(uptr),
71 "vptr for %2 base class of %1");
74 void __ubsan::__ubsan_handle_dynamic_type_cache_miss(
75 DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
76 GET_REPORT_OPTIONS(false);
77 HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts);
79 void __ubsan::__ubsan_handle_dynamic_type_cache_miss_abort(
80 DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
81 GET_REPORT_OPTIONS(true);
82 HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts);
85 static void HandleCFIBadType(CFIBadTypeData *Data, ValueHandle Vtable,
87 SourceLocation Loc = Data->Loc.acquire();
88 ScopedReport R(Opts, Loc);
89 DynamicTypeInfo DTI = getDynamicTypeInfoFromVtable((void*)Vtable);
91 static const char *TypeCheckKinds[] = {
94 "base-to-derived cast",
95 "cast to unrelated type",
98 Diag(Loc, DL_Error, "control flow integrity check for type %0 failed during "
99 "%1 (vtable address %2)")
100 << Data->Type << TypeCheckKinds[Data->TypeCheckKind] << (void *)Vtable;
102 // If possible, say what type it actually points to.
104 Diag(Vtable, DL_Note, "invalid vtable");
106 Diag(Vtable, DL_Note, "vtable is of type %0")
107 << TypeName(DTI.getMostDerivedTypeName());
110 void __ubsan::__ubsan_handle_cfi_bad_type(CFIBadTypeData *Data,
111 ValueHandle Vtable) {
112 GET_REPORT_OPTIONS(false);
113 HandleCFIBadType(Data, Vtable, Opts);
116 void __ubsan::__ubsan_handle_cfi_bad_type_abort(CFIBadTypeData *Data,
117 ValueHandle Vtable) {
118 GET_REPORT_OPTIONS(true);
119 HandleCFIBadType(Data, Vtable, Opts);
122 #endif // CAN_SANITIZE_UB