1 //===-- ubsan_handlers_cxx.cc ---------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Error logging entry points for the UBSan runtime, which are only used for C++
10 // compilations. This file is permitted to use language features which require
11 // linking against a C++ ABI library.
13 //===----------------------------------------------------------------------===//
15 #include "ubsan_platform.h"
17 #include "ubsan_handlers.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 // Returns true if UBSan has printed an error report.
33 static bool HandleDynamicTypeCacheMiss(
34 DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash,
36 if (checkDynamicType((void*)Pointer, Data->TypeInfo, Hash))
37 // Just a cache miss. The type matches after all.
40 // Check if error report should be suppressed.
41 DynamicTypeInfo DTI = getDynamicTypeInfoFromObject((void*)Pointer);
42 if (DTI.isValid() && IsVptrCheckSuppressed(DTI.getMostDerivedTypeName()))
45 SourceLocation Loc = Data->Loc.acquire();
46 ErrorType ET = ErrorType::DynamicTypeMismatch;
47 if (ignoreReport(Loc, Opts, ET))
50 ScopedReport R(Opts, Loc, ET);
52 Diag(Loc, DL_Error, ET,
53 "%0 address %1 which does not point to an object of type %2")
54 << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type;
56 // If possible, say what type it actually points to.
58 if (DTI.getOffset() < -VptrMaxOffsetToTop || DTI.getOffset() > VptrMaxOffsetToTop) {
59 Diag(Pointer, DL_Note, ET,
60 "object has a possibly invalid vptr: abs(offset to top) too big")
61 << TypeName(DTI.getMostDerivedTypeName())
62 << Range(Pointer, Pointer + sizeof(uptr), "possibly invalid vptr");
64 Diag(Pointer, DL_Note, ET, "object has invalid vptr")
65 << TypeName(DTI.getMostDerivedTypeName())
66 << Range(Pointer, Pointer + sizeof(uptr), "invalid vptr");
68 } else if (!DTI.getOffset())
69 Diag(Pointer, DL_Note, ET, "object is of type %0")
70 << TypeName(DTI.getMostDerivedTypeName())
71 << Range(Pointer, Pointer + sizeof(uptr), "vptr for %0");
73 // FIXME: Find the type at the specified offset, and include that
75 Diag(Pointer - DTI.getOffset(), DL_Note, ET,
76 "object is base class subobject at offset %0 within object of type %1")
77 << DTI.getOffset() << TypeName(DTI.getMostDerivedTypeName())
78 << TypeName(DTI.getSubobjectTypeName())
79 << Range(Pointer, Pointer + sizeof(uptr),
80 "vptr for %2 base class of %1");
84 void __ubsan::__ubsan_handle_dynamic_type_cache_miss(
85 DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
86 GET_REPORT_OPTIONS(false);
87 HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts);
89 void __ubsan::__ubsan_handle_dynamic_type_cache_miss_abort(
90 DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
91 // Note: -fsanitize=vptr is always recoverable.
92 GET_REPORT_OPTIONS(false);
93 if (HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts))
98 void __ubsan_handle_cfi_bad_type(CFICheckFailData *Data, ValueHandle Vtable,
99 bool ValidVtable, ReportOptions Opts) {
100 SourceLocation Loc = Data->Loc.acquire();
101 ErrorType ET = ErrorType::CFIBadType;
103 if (ignoreReport(Loc, Opts, ET))
106 ScopedReport R(Opts, Loc, ET);
107 DynamicTypeInfo DTI = ValidVtable
108 ? getDynamicTypeInfoFromVtable((void *)Vtable)
109 : DynamicTypeInfo(0, 0, 0);
111 const char *CheckKindStr;
112 switch (Data->CheckKind) {
114 CheckKindStr = "virtual call";
117 CheckKindStr = "non-virtual call";
119 case CFITCK_DerivedCast:
120 CheckKindStr = "base-to-derived cast";
122 case CFITCK_UnrelatedCast:
123 CheckKindStr = "cast to unrelated type";
126 CheckKindStr = "virtual pointer to member function call";
129 case CFITCK_NVMFCall:
133 Diag(Loc, DL_Error, ET,
134 "control flow integrity check for type %0 failed during "
135 "%1 (vtable address %2)")
136 << Data->Type << CheckKindStr << (void *)Vtable;
138 // If possible, say what type it actually points to.
140 Diag(Vtable, DL_Note, ET, "invalid vtable");
142 Diag(Vtable, DL_Note, ET, "vtable is of type %0")
143 << TypeName(DTI.getMostDerivedTypeName());
145 // If the failure involved different DSOs for the check location and vtable,
146 // report the DSO names.
147 const char *DstModule = Symbolizer::GetOrInit()->GetModuleNameForPc(Vtable);
149 DstModule = "(unknown)";
151 const char *SrcModule = Symbolizer::GetOrInit()->GetModuleNameForPc(Opts.pc);
153 SrcModule = "(unknown)";
155 if (internal_strcmp(SrcModule, DstModule))
156 Diag(Loc, DL_Note, ET, "check failed in %0, vtable located in %1")
157 << SrcModule << DstModule;
160 static bool handleFunctionTypeMismatch(FunctionTypeMismatchData *Data,
161 ValueHandle Function,
162 ValueHandle calleeRTTI,
163 ValueHandle fnRTTI, ReportOptions Opts) {
164 if (checkTypeInfoEquality(reinterpret_cast<void *>(calleeRTTI),
165 reinterpret_cast<void *>(fnRTTI)))
168 SourceLocation CallLoc = Data->Loc.acquire();
169 ErrorType ET = ErrorType::FunctionTypeMismatch;
171 if (ignoreReport(CallLoc, Opts, ET))
174 ScopedReport R(Opts, CallLoc, ET);
176 SymbolizedStackHolder FLoc(getSymbolizedLocation(Function));
177 const char *FName = FLoc.get()->info.function;
181 Diag(CallLoc, DL_Error, ET,
182 "call to function %0 through pointer to incorrect function type %1")
183 << FName << Data->Type;
184 Diag(FLoc, DL_Note, ET, "%0 defined here") << FName;
188 void __ubsan_handle_function_type_mismatch_v1(FunctionTypeMismatchData *Data,
189 ValueHandle Function,
190 ValueHandle calleeRTTI,
191 ValueHandle fnRTTI) {
192 GET_REPORT_OPTIONS(false);
193 handleFunctionTypeMismatch(Data, Function, calleeRTTI, fnRTTI, Opts);
196 void __ubsan_handle_function_type_mismatch_v1_abort(
197 FunctionTypeMismatchData *Data, ValueHandle Function,
198 ValueHandle calleeRTTI, ValueHandle fnRTTI) {
199 GET_REPORT_OPTIONS(true);
200 if (handleFunctionTypeMismatch(Data, Function, calleeRTTI, fnRTTI, Opts))
203 } // namespace __ubsan
205 #endif // CAN_SANITIZE_UB