ubsan_handlers_cxx.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //===-- ubsan_handlers_cxx.cpp --------------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  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.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ubsan_platform.h"
  15. #if CAN_SANITIZE_UB
  16. #include "ubsan_handlers.h"
  17. #include "ubsan_handlers_cxx.h"
  18. #include "ubsan_diag.h"
  19. #include "ubsan_type_hash.h"
  20. #include "sanitizer_common/sanitizer_common.h"
  21. #include "sanitizer_common/sanitizer_suppressions.h"
  22. using namespace __sanitizer;
  23. using namespace __ubsan;
  24. namespace __ubsan {
  25. extern const char *TypeCheckKinds[];
  26. }
  27. // Returns true if UBSan has printed an error report.
  28. static bool HandleDynamicTypeCacheMiss(
  29. DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash,
  30. ReportOptions Opts) {
  31. if (checkDynamicType((void*)Pointer, Data->TypeInfo, Hash))
  32. // Just a cache miss. The type matches after all.
  33. return false;
  34. // Check if error report should be suppressed.
  35. DynamicTypeInfo DTI = getDynamicTypeInfoFromObject((void*)Pointer);
  36. if (DTI.isValid() && IsVptrCheckSuppressed(DTI.getMostDerivedTypeName()))
  37. return false;
  38. SourceLocation Loc = Data->Loc.acquire();
  39. ErrorType ET = ErrorType::DynamicTypeMismatch;
  40. if (ignoreReport(Loc, Opts, ET))
  41. return false;
  42. ScopedReport R(Opts, Loc, ET);
  43. Diag(Loc, DL_Error, ET,
  44. "%0 address %1 which does not point to an object of type %2")
  45. << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type;
  46. // If possible, say what type it actually points to.
  47. if (!DTI.isValid()) {
  48. if (DTI.getOffset() < -VptrMaxOffsetToTop || DTI.getOffset() > VptrMaxOffsetToTop) {
  49. Diag(Pointer, DL_Note, ET,
  50. "object has a possibly invalid vptr: abs(offset to top) too big")
  51. << TypeName(DTI.getMostDerivedTypeName())
  52. << Range(Pointer, Pointer + sizeof(uptr), "possibly invalid vptr");
  53. } else {
  54. Diag(Pointer, DL_Note, ET, "object has invalid vptr")
  55. << TypeName(DTI.getMostDerivedTypeName())
  56. << Range(Pointer, Pointer + sizeof(uptr), "invalid vptr");
  57. }
  58. } else if (!DTI.getOffset())
  59. Diag(Pointer, DL_Note, ET, "object is of type %0")
  60. << TypeName(DTI.getMostDerivedTypeName())
  61. << Range(Pointer, Pointer + sizeof(uptr), "vptr for %0");
  62. else
  63. // FIXME: Find the type at the specified offset, and include that
  64. // in the note.
  65. Diag(Pointer - DTI.getOffset(), DL_Note, ET,
  66. "object is base class subobject at offset %0 within object of type %1")
  67. << DTI.getOffset() << TypeName(DTI.getMostDerivedTypeName())
  68. << TypeName(DTI.getSubobjectTypeName())
  69. << Range(Pointer, Pointer + sizeof(uptr),
  70. "vptr for %2 base class of %1");
  71. return true;
  72. }
  73. void __ubsan::__ubsan_handle_dynamic_type_cache_miss(
  74. DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
  75. GET_REPORT_OPTIONS(false);
  76. HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts);
  77. }
  78. void __ubsan::__ubsan_handle_dynamic_type_cache_miss_abort(
  79. DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
  80. // Note: -fsanitize=vptr is always recoverable.
  81. GET_REPORT_OPTIONS(false);
  82. if (HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts))
  83. Die();
  84. }
  85. namespace __ubsan {
  86. void __ubsan_handle_cfi_bad_type(CFICheckFailData *Data, ValueHandle Vtable,
  87. bool ValidVtable, ReportOptions Opts) {
  88. SourceLocation Loc = Data->Loc.acquire();
  89. ErrorType ET = ErrorType::CFIBadType;
  90. if (ignoreReport(Loc, Opts, ET))
  91. return;
  92. ScopedReport R(Opts, Loc, ET);
  93. DynamicTypeInfo DTI = ValidVtable
  94. ? getDynamicTypeInfoFromVtable((void *)Vtable)
  95. : DynamicTypeInfo(0, 0, 0);
  96. const char *CheckKindStr;
  97. switch (Data->CheckKind) {
  98. case CFITCK_VCall:
  99. CheckKindStr = "virtual call";
  100. break;
  101. case CFITCK_NVCall:
  102. CheckKindStr = "non-virtual call";
  103. break;
  104. case CFITCK_DerivedCast:
  105. CheckKindStr = "base-to-derived cast";
  106. break;
  107. case CFITCK_UnrelatedCast:
  108. CheckKindStr = "cast to unrelated type";
  109. break;
  110. case CFITCK_VMFCall:
  111. CheckKindStr = "virtual pointer to member function call";
  112. break;
  113. case CFITCK_ICall:
  114. case CFITCK_NVMFCall:
  115. Die();
  116. }
  117. Diag(Loc, DL_Error, ET,
  118. "control flow integrity check for type %0 failed during "
  119. "%1 (vtable address %2)")
  120. << Data->Type << CheckKindStr << (void *)Vtable;
  121. // If possible, say what type it actually points to.
  122. if (!DTI.isValid())
  123. Diag(Vtable, DL_Note, ET, "invalid vtable");
  124. else
  125. Diag(Vtable, DL_Note, ET, "vtable is of type %0")
  126. << TypeName(DTI.getMostDerivedTypeName());
  127. // If the failure involved different DSOs for the check location and vtable,
  128. // report the DSO names.
  129. const char *DstModule = Symbolizer::GetOrInit()->GetModuleNameForPc(Vtable);
  130. if (!DstModule)
  131. DstModule = "(unknown)";
  132. const char *SrcModule = Symbolizer::GetOrInit()->GetModuleNameForPc(Opts.pc);
  133. if (!SrcModule)
  134. SrcModule = "(unknown)";
  135. if (internal_strcmp(SrcModule, DstModule))
  136. Diag(Loc, DL_Note, ET, "check failed in %0, vtable located in %1")
  137. << SrcModule << DstModule;
  138. }
  139. static bool handleFunctionTypeMismatch(FunctionTypeMismatchData *Data,
  140. ValueHandle Function,
  141. ValueHandle calleeRTTI,
  142. ValueHandle fnRTTI, ReportOptions Opts) {
  143. if (checkTypeInfoEquality(reinterpret_cast<void *>(calleeRTTI),
  144. reinterpret_cast<void *>(fnRTTI)))
  145. return false;
  146. SourceLocation CallLoc = Data->Loc.acquire();
  147. ErrorType ET = ErrorType::FunctionTypeMismatch;
  148. if (ignoreReport(CallLoc, Opts, ET))
  149. return true;
  150. ScopedReport R(Opts, CallLoc, ET);
  151. SymbolizedStackHolder FLoc(getSymbolizedLocation(Function));
  152. const char *FName = FLoc.get()->info.function;
  153. if (!FName)
  154. FName = "(unknown)";
  155. Diag(CallLoc, DL_Error, ET,
  156. "call to function %0 through pointer to incorrect function type %1")
  157. << FName << Data->Type;
  158. Diag(FLoc, DL_Note, ET, "%0 defined here") << FName;
  159. return true;
  160. }
  161. void __ubsan_handle_function_type_mismatch_v1(FunctionTypeMismatchData *Data,
  162. ValueHandle Function,
  163. ValueHandle calleeRTTI,
  164. ValueHandle fnRTTI) {
  165. GET_REPORT_OPTIONS(false);
  166. handleFunctionTypeMismatch(Data, Function, calleeRTTI, fnRTTI, Opts);
  167. }
  168. void __ubsan_handle_function_type_mismatch_v1_abort(
  169. FunctionTypeMismatchData *Data, ValueHandle Function,
  170. ValueHandle calleeRTTI, ValueHandle fnRTTI) {
  171. GET_REPORT_OPTIONS(true);
  172. if (handleFunctionTypeMismatch(Data, Function, calleeRTTI, fnRTTI, Opts))
  173. Die();
  174. }
  175. } // namespace __ubsan
  176. #endif // CAN_SANITIZE_UB