]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/ubsan/ubsan_handlers_cxx.cc
Update libucl to git version 8d3b186
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / ubsan / ubsan_handlers_cxx.cc
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 #include "sanitizer_common/sanitizer_suppressions.h"
22
23 using namespace __sanitizer;
24 using namespace __ubsan;
25
26 namespace __ubsan {
27   extern const char *TypeCheckKinds[];
28 }
29
30 static void HandleDynamicTypeCacheMiss(
31     DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash,
32     ReportOptions Opts) {
33   if (checkDynamicType((void*)Pointer, Data->TypeInfo, Hash))
34     // Just a cache miss. The type matches after all.
35     return;
36
37   // Check if error report should be suppressed.
38   DynamicTypeInfo DTI = getDynamicTypeInfo((void*)Pointer);
39   if (DTI.isValid() &&
40       MatchSuppression(DTI.getMostDerivedTypeName(), SuppressionVptrCheck))
41     return;
42
43   SourceLocation Loc = Data->Loc.acquire();
44   if (Loc.isDisabled())
45     return;
46
47   ScopedReport R(Opts, Loc);
48
49   Diag(Loc, DL_Error,
50        "%0 address %1 which does not point to an object of type %2")
51     << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type;
52
53   // If possible, say what type it actually points to.
54   if (!DTI.isValid())
55     Diag(Pointer, DL_Note, "object has invalid vptr")
56         << MangledName(DTI.getMostDerivedTypeName())
57         << Range(Pointer, Pointer + sizeof(uptr), "invalid vptr");
58   else if (!DTI.getOffset())
59     Diag(Pointer, DL_Note, "object is of type %0")
60         << MangledName(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,
66          "object is base class subobject at offset %0 within object of type %1")
67         << DTI.getOffset() << MangledName(DTI.getMostDerivedTypeName())
68         << MangledName(DTI.getSubobjectTypeName())
69         << Range(Pointer, Pointer + sizeof(uptr),
70                  "vptr for %2 base class of %1");
71 }
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   GET_REPORT_OPTIONS(true);
81   HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts);
82 }