| 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 | |
| 16 | #include "ubsan_handlers_cxx.h" |
| 17 | #include "ubsan_diag.h" |
| 18 | #include "ubsan_type_hash.h" |
| 19 | |
| 20 | #include "sanitizer_common/sanitizer_common.h" |
| 21 | |
| 22 | using namespace __sanitizer; |
| 23 | using namespace __ubsan; |
| 24 | |
| 25 | namespace __ubsan { |
| 26 | extern const char *TypeCheckKinds[]; |
| 27 | } |
| 28 | |
| Will Dietz | 2c36c71 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 29 | static void HandleDynamicTypeCacheMiss( |
| Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 30 | DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash, |
| 31 | bool Abort) { |
| Richard Smith | 2f0d7d5 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 32 | if (checkDynamicType((void*)Pointer, Data->TypeInfo, Hash)) |
| 33 | // Just a cache miss. The type matches after all. |
| 34 | return; |
| 35 | |
| Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 36 | Diag(Data->Loc, DL_Error, |
| 37 | "%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] | 38 | << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type; |
| Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 39 | |
| 40 | // If possible, say what type it actually points to. |
| 41 | // FIXME: Demangle the type names. |
| 42 | DynamicTypeInfo DTI = getDynamicTypeInfo((void*)Pointer); |
| 43 | if (!DTI.isValid()) |
| 44 | Diag(Pointer, DL_Note, "object has invalid vptr") |
| Richard Smith | 7e45562 | 2012-12-18 09:30:21 +0000 | [diff] [blame^] | 45 | << MangledName(DTI.getMostDerivedTypeName()) |
| Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 46 | << Range(Pointer, Pointer + sizeof(uptr), "invalid vptr"); |
| 47 | else if (!DTI.getOffset()) |
| 48 | Diag(Pointer, DL_Note, "object is of type %0") |
| Richard Smith | 7e45562 | 2012-12-18 09:30:21 +0000 | [diff] [blame^] | 49 | << MangledName(DTI.getMostDerivedTypeName()) |
| Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 50 | << Range(Pointer, Pointer + sizeof(uptr), "vptr for %0"); |
| 51 | else |
| Richard Smith | 7e45562 | 2012-12-18 09:30:21 +0000 | [diff] [blame^] | 52 | // FIXME: Find the type at the specified offset, and include that |
| 53 | // in the note. |
| Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 54 | Diag(Pointer - DTI.getOffset(), DL_Note, |
| 55 | "object is base class subobject at offset %0 within object of type %1") |
| Richard Smith | 7e45562 | 2012-12-18 09:30:21 +0000 | [diff] [blame^] | 56 | << DTI.getOffset() << MangledName(DTI.getMostDerivedTypeName()) |
| 57 | << MangledName(DTI.getSubobjectTypeName()) |
| 58 | << Range(Pointer, Pointer + sizeof(uptr), "vptr for %2 base class of %1"); |
| Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 59 | |
| 60 | if (Abort) |
| Will Dietz | 2c36c71 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 61 | Die(); |
| 62 | } |
| 63 | |
| 64 | void __ubsan::__ubsan_handle_dynamic_type_cache_miss( |
| Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 65 | DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) { |
| Will Dietz | 2c36c71 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 66 | HandleDynamicTypeCacheMiss(Data, Pointer, Hash, false); |
| 67 | } |
| 68 | void __ubsan::__ubsan_handle_dynamic_type_cache_miss_abort( |
| Richard Smith | cf56ebd | 2012-12-18 06:30:32 +0000 | [diff] [blame] | 69 | DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) { |
| Will Dietz | 2c36c71 | 2012-12-02 19:47:29 +0000 | [diff] [blame] | 70 | HandleDynamicTypeCacheMiss(Data, Pointer, Hash, true); |
| Richard Smith | 2f0d7d5 | 2012-10-25 02:07:02 +0000 | [diff] [blame] | 71 | } |