Richard Smith | 2f0d7d5 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 1 | //===-- ubsan_handlers_cxx.cc ---------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 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. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Alexey Samsonov | 0781e98 | 2015-03-26 17:26:04 +0000 | [diff] [blame] | 16 | #include "ubsan_platform.h" |
| 17 | #if CAN_SANITIZE_UB |
Evgeniy Stepanov | 2269652 | 2016-01-25 23:34:38 +0000 | [diff] [blame] | 18 | #include "ubsan_handlers.h" |
Richard Smith | 2f0d7d5 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 19 | #include "ubsan_handlers_cxx.h" |
| 20 | #include "ubsan_diag.h" |
| 21 | #include "ubsan_type_hash.h" |
| 22 | |
| 23 | #include "sanitizer_common/sanitizer_common.h" |
Alexey Samsonov | ff24fd2 | 2014-08-05 01:24:22 +0000 | [diff] [blame] | 24 | #include "sanitizer_common/sanitizer_suppressions.h" |
Richard Smith | 2f0d7d5 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace __sanitizer; |
| 27 | using namespace __ubsan; |
| 28 | |
| 29 | namespace __ubsan { |
| 30 | extern const char *TypeCheckKinds[]; |
| 31 | } |
| 32 | |
Alexey Samsonov | aff20ac | 2015-12-09 00:12:57 +0000 | [diff] [blame] | 33 | // Returns true if UBSan has printed an error report. |
| 34 | static bool HandleDynamicTypeCacheMiss( |
Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 35 | DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash, |
Alexey Samsonov | 2ccbc62 | 2014-08-22 21:42:04 +0000 | [diff] [blame] | 36 | ReportOptions Opts) { |
Richard Smith | 2f0d7d5 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 37 | if (checkDynamicType((void*)Pointer, Data->TypeInfo, Hash)) |
| 38 | // Just a cache miss. The type matches after all. |
Alexey Samsonov | aff20ac | 2015-12-09 00:12:57 +0000 | [diff] [blame] | 39 | return false; |
Richard Smith | 2f0d7d5 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 40 | |
Alexey Samsonov | ff24fd2 | 2014-08-05 01:24:22 +0000 | [diff] [blame] | 41 | // Check if error report should be suppressed. |
Peter Collingbourne | 175d633 | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 42 | DynamicTypeInfo DTI = getDynamicTypeInfoFromObject((void*)Pointer); |
Alexey Samsonov | d1c3186 | 2015-02-20 17:41:59 +0000 | [diff] [blame] | 43 | if (DTI.isValid() && IsVptrCheckSuppressed(DTI.getMostDerivedTypeName())) |
Alexey Samsonov | aff20ac | 2015-12-09 00:12:57 +0000 | [diff] [blame] | 44 | return false; |
Alexey Samsonov | ff24fd2 | 2014-08-05 01:24:22 +0000 | [diff] [blame] | 45 | |
Will Dietz | 765c266 | 2013-01-09 03:40:03 +0000 | [diff] [blame] | 46 | SourceLocation Loc = Data->Loc.acquire(); |
Alexey Samsonov | 081a24e | 2015-12-18 19:56:42 +0000 | [diff] [blame] | 47 | ErrorType ET = ErrorType::DynamicTypeMismatch; |
| 48 | if (ignoreReport(Loc, Opts, ET)) |
Alexey Samsonov | aff20ac | 2015-12-09 00:12:57 +0000 | [diff] [blame] | 49 | return false; |
Will Dietz | 765c266 | 2013-01-09 03:40:03 +0000 | [diff] [blame] | 50 | |
Alexey Samsonov | 081a24e | 2015-12-18 19:56:42 +0000 | [diff] [blame] | 51 | ScopedReport R(Opts, Loc, ET); |
Alexey Samsonov | 96591cd | 2014-07-30 01:49:19 +0000 | [diff] [blame] | 52 | |
Will Dietz | 765c266 | 2013-01-09 03:40:03 +0000 | [diff] [blame] | 53 | Diag(Loc, DL_Error, |
Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 54 | "%0 address %1 which does not point to an object of type %2") |
Richard Smith | 2f0d7d5 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 55 | << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type; |
Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 56 | |
| 57 | // If possible, say what type it actually points to. |
Ivan Krasin | 048155c | 2016-06-02 18:36:12 +0000 | [diff] [blame] | 58 | if (!DTI.isValid()) { |
| 59 | if (DTI.getOffset() < -VptrMaxOffsetToTop || DTI.getOffset() > VptrMaxOffsetToTop) { |
| 60 | Diag(Pointer, DL_Note, "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"); |
| 63 | } else { |
| 64 | Diag(Pointer, DL_Note, "object has invalid vptr") |
| 65 | << TypeName(DTI.getMostDerivedTypeName()) |
| 66 | << Range(Pointer, Pointer + sizeof(uptr), "invalid vptr"); |
| 67 | } |
| 68 | } else if (!DTI.getOffset()) |
Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 69 | Diag(Pointer, DL_Note, "object is of type %0") |
Peter Collingbourne | 702548d | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 70 | << TypeName(DTI.getMostDerivedTypeName()) |
Alexey Samsonov | ff24fd2 | 2014-08-05 01:24:22 +0000 | [diff] [blame] | 71 | << Range(Pointer, Pointer + sizeof(uptr), "vptr for %0"); |
Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 72 | else |
Richard Smith | 7e45562 | 2012-12-18 09:30:21 +0000 | [diff] [blame] | 73 | // FIXME: Find the type at the specified offset, and include that |
| 74 | // in the note. |
Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 75 | Diag(Pointer - DTI.getOffset(), DL_Note, |
| 76 | "object is base class subobject at offset %0 within object of type %1") |
Peter Collingbourne | 702548d | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 77 | << DTI.getOffset() << TypeName(DTI.getMostDerivedTypeName()) |
| 78 | << TypeName(DTI.getSubobjectTypeName()) |
Alexey Samsonov | ff24fd2 | 2014-08-05 01:24:22 +0000 | [diff] [blame] | 79 | << Range(Pointer, Pointer + sizeof(uptr), |
| 80 | "vptr for %2 base class of %1"); |
Alexey Samsonov | aff20ac | 2015-12-09 00:12:57 +0000 | [diff] [blame] | 81 | return true; |
Will Dietz | 2c36c71 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void __ubsan::__ubsan_handle_dynamic_type_cache_miss( |
Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 85 | DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) { |
Alexey Samsonov | 2ccbc62 | 2014-08-22 21:42:04 +0000 | [diff] [blame] | 86 | GET_REPORT_OPTIONS(false); |
| 87 | HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts); |
Will Dietz | 2c36c71 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 88 | } |
| 89 | void __ubsan::__ubsan_handle_dynamic_type_cache_miss_abort( |
Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 90 | DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) { |
Alexey Samsonov | aff20ac | 2015-12-09 00:12:57 +0000 | [diff] [blame] | 91 | // Note: -fsanitize=vptr is always recoverable. |
| 92 | GET_REPORT_OPTIONS(false); |
| 93 | if (HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts)) |
| 94 | Die(); |
Richard Smith | 2f0d7d5 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 95 | } |
Alexey Samsonov | 0781e98 | 2015-03-26 17:26:04 +0000 | [diff] [blame] | 96 | |
Evgeniy Stepanov | 2269652 | 2016-01-25 23:34:38 +0000 | [diff] [blame] | 97 | namespace __ubsan { |
| 98 | void HandleCFIBadType(CFICheckFailData *Data, ValueHandle Vtable, |
Evgeniy Stepanov | 73583d5 | 2016-02-03 22:19:04 +0000 | [diff] [blame] | 99 | bool ValidVtable, ReportOptions Opts) { |
Peter Collingbourne | 175d633 | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 100 | SourceLocation Loc = Data->Loc.acquire(); |
Alexey Samsonov | 081a24e | 2015-12-18 19:56:42 +0000 | [diff] [blame] | 101 | ErrorType ET = ErrorType::CFIBadType; |
Alexey Samsonov | aff20ac | 2015-12-09 00:12:57 +0000 | [diff] [blame] | 102 | |
Alexey Samsonov | 081a24e | 2015-12-18 19:56:42 +0000 | [diff] [blame] | 103 | if (ignoreReport(Loc, Opts, ET)) |
Alexey Samsonov | aff20ac | 2015-12-09 00:12:57 +0000 | [diff] [blame] | 104 | return; |
Alexey Samsonov | 081a24e | 2015-12-18 19:56:42 +0000 | [diff] [blame] | 105 | |
| 106 | ScopedReport R(Opts, Loc, ET); |
Evgeniy Stepanov | 73583d5 | 2016-02-03 22:19:04 +0000 | [diff] [blame] | 107 | DynamicTypeInfo DTI = ValidVtable |
| 108 | ? getDynamicTypeInfoFromVtable((void *)Vtable) |
| 109 | : DynamicTypeInfo(0, 0, 0); |
Peter Collingbourne | 175d633 | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 110 | |
Evgeniy Stepanov | 2269652 | 2016-01-25 23:34:38 +0000 | [diff] [blame] | 111 | const char *CheckKindStr; |
| 112 | switch (Data->CheckKind) { |
| 113 | case CFITCK_VCall: |
| 114 | CheckKindStr = "virtual call"; |
| 115 | break; |
| 116 | case CFITCK_NVCall: |
| 117 | CheckKindStr = "non-virtual call"; |
| 118 | break; |
| 119 | case CFITCK_DerivedCast: |
| 120 | CheckKindStr = "base-to-derived cast"; |
| 121 | break; |
| 122 | case CFITCK_UnrelatedCast: |
| 123 | CheckKindStr = "cast to unrelated type"; |
| 124 | break; |
| 125 | case CFITCK_ICall: |
| 126 | Die(); |
| 127 | } |
Peter Collingbourne | 175d633 | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 128 | |
| 129 | Diag(Loc, DL_Error, "control flow integrity check for type %0 failed during " |
| 130 | "%1 (vtable address %2)") |
Evgeniy Stepanov | 2269652 | 2016-01-25 23:34:38 +0000 | [diff] [blame] | 131 | << Data->Type << CheckKindStr << (void *)Vtable; |
Peter Collingbourne | 175d633 | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 132 | |
| 133 | // If possible, say what type it actually points to. |
Evgeniy Stepanov | 73583d5 | 2016-02-03 22:19:04 +0000 | [diff] [blame] | 134 | if (!DTI.isValid()) { |
| 135 | const char *module = Symbolizer::GetOrInit()->GetModuleNameForPc(Vtable); |
| 136 | if (module) |
| 137 | Diag(Vtable, DL_Note, "invalid vtable in module %0") << module; |
| 138 | else |
| 139 | Diag(Vtable, DL_Note, "invalid vtable"); |
| 140 | } else { |
Peter Collingbourne | 175d633 | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 141 | Diag(Vtable, DL_Note, "vtable is of type %0") |
Peter Collingbourne | 702548d | 2015-07-08 22:10:34 +0000 | [diff] [blame] | 142 | << TypeName(DTI.getMostDerivedTypeName()); |
Evgeniy Stepanov | 73583d5 | 2016-02-03 22:19:04 +0000 | [diff] [blame] | 143 | } |
Peter Collingbourne | 175d633 | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 144 | } |
Evgeniy Stepanov | 2269652 | 2016-01-25 23:34:38 +0000 | [diff] [blame] | 145 | } // namespace __ubsan |
Peter Collingbourne | 175d633 | 2015-06-19 01:52:55 +0000 | [diff] [blame] | 146 | |
Evgeniy Stepanov | 2269652 | 2016-01-25 23:34:38 +0000 | [diff] [blame] | 147 | #endif // CAN_SANITIZE_UB |