blob: 2476947dc914899d03f0918639f10340108a8b6c [file] [log] [blame]
Richard Smith6ebe4512012-10-09 19:34:32 +00001//===-- ubsan_diag.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// Diagnostic reporting for the UBSan runtime.
11//
12//===----------------------------------------------------------------------===//
13
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070014#include "ubsan_platform.h"
15#if CAN_SANITIZE_UB
Richard Smith6ebe4512012-10-09 19:34:32 +000016#include "ubsan_diag.h"
Stephen Hines6d186232014-11-26 17:56:19 -080017#include "ubsan_init.h"
18#include "ubsan_flags.h"
Stephen Hines86277eb2015-03-23 12:06:32 -070019#include "sanitizer_common/sanitizer_placement_new.h"
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +000020#include "sanitizer_common/sanitizer_report_decorator.h"
Richard Smith5f116492012-12-18 04:23:18 +000021#include "sanitizer_common/sanitizer_stacktrace.h"
Stephen Hines6d186232014-11-26 17:56:19 -080022#include "sanitizer_common/sanitizer_stacktrace_printer.h"
Stephen Hines86277eb2015-03-23 12:06:32 -070023#include "sanitizer_common/sanitizer_suppressions.h"
Richard Smith5f116492012-12-18 04:23:18 +000024#include "sanitizer_common/sanitizer_symbolizer.h"
Richard Smith6ebe4512012-10-09 19:34:32 +000025#include <stdio.h>
Richard Smith6ebe4512012-10-09 19:34:32 +000026
27using namespace __ubsan;
28
Stephen Hines6d186232014-11-26 17:56:19 -080029static void MaybePrintStackTrace(uptr pc, uptr bp) {
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070030 // We assume that flags are already parsed, as UBSan runtime
Stephen Hines6d186232014-11-26 17:56:19 -080031 // will definitely be called when we print the first diagnostics message.
32 if (!flags()->print_stacktrace)
33 return;
34 // We can only use slow unwind, as we don't have any information about stack
35 // top/bottom.
36 // FIXME: It's better to respect "fast_unwind_on_fatal" runtime flag and
37 // fetch stack top/bottom information if we have it (e.g. if we're running
38 // under ASan).
39 if (StackTrace::WillUseFastUnwind(false))
40 return;
41 BufferedStackTrace stack;
42 stack.Unwind(kStackTraceMax, pc, bp, 0, 0, 0, false);
43 stack.Print();
44}
45
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080046static const char *ConvertTypeToString(ErrorType Type) {
47 switch (Type) {
48#define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \
49 case ErrorType::Name: \
50 return SummaryKind;
51#include "ubsan_checks.inc"
52#undef UBSAN_CHECK
53 }
54 UNREACHABLE("unknown ErrorType!");
55}
56
57static const char *ConvertTypeToFlagName(ErrorType Type) {
58 switch (Type) {
59#define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) \
60 case ErrorType::Name: \
61 return FSanitizeFlagName;
62#include "ubsan_checks.inc"
63#undef UBSAN_CHECK
64 }
65 UNREACHABLE("unknown ErrorType!");
66}
67
68static void MaybeReportErrorSummary(Location Loc, ErrorType Type) {
Stephen Hines6d186232014-11-26 17:56:19 -080069 if (!common_flags()->print_summary)
70 return;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080071 if (!flags()->report_error_type)
72 Type = ErrorType::GenericUB;
73 const char *ErrorKind = ConvertTypeToString(Type);
Stephen Hines6d186232014-11-26 17:56:19 -080074 if (Loc.isSourceLocation()) {
75 SourceLocation SLoc = Loc.getSourceLocation();
76 if (!SLoc.isInvalid()) {
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070077 AddressInfo AI;
78 AI.file = internal_strdup(SLoc.getFilename());
79 AI.line = SLoc.getLine();
80 AI.column = SLoc.getColumn();
81 AI.function = internal_strdup(""); // Avoid printing ?? as function name.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080082 ReportErrorSummary(ErrorKind, AI);
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -070083 AI.Clear();
Stephen Hines6d186232014-11-26 17:56:19 -080084 return;
85 }
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070086 } else if (Loc.isSymbolizedStack()) {
87 const AddressInfo &AI = Loc.getSymbolizedStack()->info;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080088 ReportErrorSummary(ErrorKind, AI);
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070089 return;
Stephen Hines2d1fdb22014-05-28 23:58:16 -070090 }
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080091 ReportErrorSummary(ErrorKind);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070092}
93
Stephen Hines6a211c52014-07-21 00:49:56 -070094namespace {
95class Decorator : public SanitizerCommonDecorator {
96 public:
97 Decorator() : SanitizerCommonDecorator() {}
98 const char *Highlight() const { return Green(); }
99 const char *EndHighlight() const { return Default(); }
100 const char *Note() const { return Black(); }
101 const char *EndNote() const { return Default(); }
102};
103}
104
Stephen Hines86277eb2015-03-23 12:06:32 -0700105SymbolizedStack *__ubsan::getSymbolizedLocation(uptr PC) {
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700106 InitAsStandaloneIfNecessary();
Stephen Hines86277eb2015-03-23 12:06:32 -0700107 return Symbolizer::GetOrInit()->SymbolizePC(PC);
Richard Smith5f116492012-12-18 04:23:18 +0000108}
109
Richard Smith6ebe4512012-10-09 19:34:32 +0000110Diag &Diag::operator<<(const TypeDescriptor &V) {
111 return AddArg(V.getTypeName());
112}
113
114Diag &Diag::operator<<(const Value &V) {
115 if (V.getType().isSignedIntegerTy())
116 AddArg(V.getSIntValue());
117 else if (V.getType().isUnsignedIntegerTy())
118 AddArg(V.getUIntValue());
Richard Smith58561702012-10-12 22:57:15 +0000119 else if (V.getType().isFloatTy())
120 AddArg(V.getFloatValue());
Richard Smith6ebe4512012-10-09 19:34:32 +0000121 else
122 AddArg("<unknown>");
123 return *this;
124}
125
Richard Smith0ad23f72012-12-18 09:30:21 +0000126/// Hexadecimal printing for numbers too large for Printf to handle directly.
Richard Smith6ebe4512012-10-09 19:34:32 +0000127static void PrintHex(UIntMax Val) {
Chandler Carruthba3fde62012-10-13 02:30:10 +0000128#if HAVE_INT128_T
Richard Smithf4932202012-11-13 23:42:05 +0000129 Printf("0x%08x%08x%08x%08x",
Richard Smith6ebe4512012-10-09 19:34:32 +0000130 (unsigned int)(Val >> 96),
131 (unsigned int)(Val >> 64),
132 (unsigned int)(Val >> 32),
133 (unsigned int)(Val));
134#else
135 UNREACHABLE("long long smaller than 64 bits?");
136#endif
137}
138
Richard Smith5f116492012-12-18 04:23:18 +0000139static void renderLocation(Location Loc) {
Alexey Samsonovaed85842013-11-14 09:54:10 +0000140 InternalScopedString LocBuffer(1024);
Richard Smith5f116492012-12-18 04:23:18 +0000141 switch (Loc.getKind()) {
142 case Location::LK_Source: {
143 SourceLocation SLoc = Loc.getSourceLocation();
144 if (SLoc.isInvalid())
Alexey Samsonovaed85842013-11-14 09:54:10 +0000145 LocBuffer.append("<unknown>");
Alexey Samsonov90b0f1e2013-10-04 08:55:03 +0000146 else
Stephen Hines6d186232014-11-26 17:56:19 -0800147 RenderSourceLocation(&LocBuffer, SLoc.getFilename(), SLoc.getLine(),
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700148 SLoc.getColumn(), common_flags()->symbolize_vs_style,
149 common_flags()->strip_path_prefix);
Richard Smith5f116492012-12-18 04:23:18 +0000150 break;
151 }
Richard Smith5f116492012-12-18 04:23:18 +0000152 case Location::LK_Memory:
Alexey Samsonovaed85842013-11-14 09:54:10 +0000153 LocBuffer.append("%p", Loc.getMemoryLocation());
Richard Smith5f116492012-12-18 04:23:18 +0000154 break;
Stephen Hines86277eb2015-03-23 12:06:32 -0700155 case Location::LK_Symbolized: {
156 const AddressInfo &Info = Loc.getSymbolizedStack()->info;
157 if (Info.file) {
158 RenderSourceLocation(&LocBuffer, Info.file, Info.line, Info.column,
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700159 common_flags()->symbolize_vs_style,
Stephen Hines86277eb2015-03-23 12:06:32 -0700160 common_flags()->strip_path_prefix);
161 } else if (Info.module) {
162 RenderModuleLocation(&LocBuffer, Info.module, Info.module_offset,
163 common_flags()->strip_path_prefix);
164 } else {
165 LocBuffer.append("%p", Info.address);
166 }
167 break;
168 }
Richard Smith5f116492012-12-18 04:23:18 +0000169 case Location::LK_Null:
Alexey Samsonovaed85842013-11-14 09:54:10 +0000170 LocBuffer.append("<unknown>");
Richard Smith25ee97f2012-12-18 06:30:32 +0000171 break;
Richard Smith5f116492012-12-18 04:23:18 +0000172 }
Alexey Samsonovaed85842013-11-14 09:54:10 +0000173 Printf("%s:", LocBuffer.data());
Richard Smith5f116492012-12-18 04:23:18 +0000174}
175
Richard Smith25ee97f2012-12-18 06:30:32 +0000176static void renderText(const char *Message, const Diag::Arg *Args) {
Richard Smith6ebe4512012-10-09 19:34:32 +0000177 for (const char *Msg = Message; *Msg; ++Msg) {
Richard Smithf4932202012-11-13 23:42:05 +0000178 if (*Msg != '%') {
179 char Buffer[64];
180 unsigned I;
181 for (I = 0; Msg[I] && Msg[I] != '%' && I != 63; ++I)
182 Buffer[I] = Msg[I];
183 Buffer[I] = '\0';
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +0000184 Printf(Buffer);
Richard Smithf4932202012-11-13 23:42:05 +0000185 Msg += I - 1;
186 } else {
Richard Smith25ee97f2012-12-18 06:30:32 +0000187 const Diag::Arg &A = Args[*++Msg - '0'];
Richard Smith6ebe4512012-10-09 19:34:32 +0000188 switch (A.Kind) {
Richard Smith25ee97f2012-12-18 06:30:32 +0000189 case Diag::AK_String:
Richard Smithf4932202012-11-13 23:42:05 +0000190 Printf("%s", A.String);
Richard Smith6ebe4512012-10-09 19:34:32 +0000191 break;
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800192 case Diag::AK_TypeName: {
193 if (SANITIZER_WINDOWS)
194 // The Windows implementation demangles names early.
195 Printf("'%s'", A.String);
196 else
197 Printf("'%s'", Symbolizer::GetOrInit()->Demangle(A.String));
Richard Smith0ad23f72012-12-18 09:30:21 +0000198 break;
199 }
Richard Smith25ee97f2012-12-18 06:30:32 +0000200 case Diag::AK_SInt:
Richard Smith6ebe4512012-10-09 19:34:32 +0000201 // 'long long' is guaranteed to be at least 64 bits wide.
202 if (A.SInt >= INT64_MIN && A.SInt <= INT64_MAX)
Richard Smithf4932202012-11-13 23:42:05 +0000203 Printf("%lld", (long long)A.SInt);
Richard Smith6ebe4512012-10-09 19:34:32 +0000204 else
205 PrintHex(A.SInt);
206 break;
Richard Smith25ee97f2012-12-18 06:30:32 +0000207 case Diag::AK_UInt:
Richard Smith6ebe4512012-10-09 19:34:32 +0000208 if (A.UInt <= UINT64_MAX)
Richard Smithf4932202012-11-13 23:42:05 +0000209 Printf("%llu", (unsigned long long)A.UInt);
Richard Smith6ebe4512012-10-09 19:34:32 +0000210 else
211 PrintHex(A.UInt);
212 break;
Richard Smith25ee97f2012-12-18 06:30:32 +0000213 case Diag::AK_Float: {
Richard Smithf4932202012-11-13 23:42:05 +0000214 // FIXME: Support floating-point formatting in sanitizer_common's
215 // printf, and stop using snprintf here.
216 char Buffer[32];
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800217#if SANITIZER_WINDOWS
218 sprintf_s(Buffer, sizeof(Buffer), "%Lg", (long double)A.Float);
219#else
Richard Smithf4932202012-11-13 23:42:05 +0000220 snprintf(Buffer, sizeof(Buffer), "%Lg", (long double)A.Float);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800221#endif
Richard Smithf4932202012-11-13 23:42:05 +0000222 Printf("%s", Buffer);
Richard Smith58561702012-10-12 22:57:15 +0000223 break;
Richard Smithf4932202012-11-13 23:42:05 +0000224 }
Richard Smith25ee97f2012-12-18 06:30:32 +0000225 case Diag::AK_Pointer:
Richard Smithc1939532013-01-10 22:39:40 +0000226 Printf("%p", A.Pointer);
Richard Smith6ebe4512012-10-09 19:34:32 +0000227 break;
228 }
229 }
230 }
Richard Smith25ee97f2012-12-18 06:30:32 +0000231}
232
233/// Find the earliest-starting range in Ranges which ends after Loc.
234static Range *upperBound(MemoryLocation Loc, Range *Ranges,
235 unsigned NumRanges) {
236 Range *Best = 0;
237 for (unsigned I = 0; I != NumRanges; ++I)
238 if (Ranges[I].getEnd().getMemoryLocation() > Loc &&
239 (!Best ||
240 Best->getStart().getMemoryLocation() >
241 Ranges[I].getStart().getMemoryLocation()))
242 Best = &Ranges[I];
243 return Best;
244}
245
Stephen Hines6d186232014-11-26 17:56:19 -0800246static inline uptr subtractNoOverflow(uptr LHS, uptr RHS) {
247 return (LHS < RHS) ? 0 : LHS - RHS;
248}
249
250static inline uptr addNoOverflow(uptr LHS, uptr RHS) {
251 const uptr Limit = (uptr)-1;
252 return (LHS > Limit - RHS) ? Limit : LHS + RHS;
253}
254
Richard Smith25ee97f2012-12-18 06:30:32 +0000255/// Render a snippet of the address space near a location.
Stephen Hines6a211c52014-07-21 00:49:56 -0700256static void renderMemorySnippet(const Decorator &Decor, MemoryLocation Loc,
Richard Smith25ee97f2012-12-18 06:30:32 +0000257 Range *Ranges, unsigned NumRanges,
258 const Diag::Arg *Args) {
Richard Smith25ee97f2012-12-18 06:30:32 +0000259 // Show at least the 8 bytes surrounding Loc.
Stephen Hines6d186232014-11-26 17:56:19 -0800260 const unsigned MinBytesNearLoc = 4;
261 MemoryLocation Min = subtractNoOverflow(Loc, MinBytesNearLoc);
262 MemoryLocation Max = addNoOverflow(Loc, MinBytesNearLoc);
263 MemoryLocation OrigMin = Min;
Richard Smith25ee97f2012-12-18 06:30:32 +0000264 for (unsigned I = 0; I < NumRanges; ++I) {
265 Min = __sanitizer::Min(Ranges[I].getStart().getMemoryLocation(), Min);
266 Max = __sanitizer::Max(Ranges[I].getEnd().getMemoryLocation(), Max);
267 }
268
269 // If we have too many interesting bytes, prefer to show bytes after Loc.
Stephen Hines6d186232014-11-26 17:56:19 -0800270 const unsigned BytesToShow = 32;
Richard Smith25ee97f2012-12-18 06:30:32 +0000271 if (Max - Min > BytesToShow)
Stephen Hines6d186232014-11-26 17:56:19 -0800272 Min = __sanitizer::Min(Max - BytesToShow, OrigMin);
273 Max = addNoOverflow(Min, BytesToShow);
274
275 if (!IsAccessibleMemoryRange(Min, Max - Min)) {
276 Printf("<memory cannot be printed>\n");
277 return;
278 }
Richard Smith25ee97f2012-12-18 06:30:32 +0000279
280 // Emit data.
281 for (uptr P = Min; P != Max; ++P) {
Richard Smith25ee97f2012-12-18 06:30:32 +0000282 unsigned char C = *reinterpret_cast<const unsigned char*>(P);
283 Printf("%s%02x", (P % 8 == 0) ? " " : " ", C);
284 }
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +0000285 Printf("\n");
Richard Smith25ee97f2012-12-18 06:30:32 +0000286
287 // Emit highlights.
Stephen Hines6a211c52014-07-21 00:49:56 -0700288 Printf(Decor.Highlight());
Richard Smith25ee97f2012-12-18 06:30:32 +0000289 Range *InRange = upperBound(Min, Ranges, NumRanges);
290 for (uptr P = Min; P != Max; ++P) {
291 char Pad = ' ', Byte = ' ';
292 if (InRange && InRange->getEnd().getMemoryLocation() == P)
293 InRange = upperBound(P, Ranges, NumRanges);
294 if (!InRange && P > Loc)
295 break;
296 if (InRange && InRange->getStart().getMemoryLocation() < P)
297 Pad = '~';
298 if (InRange && InRange->getStart().getMemoryLocation() <= P)
299 Byte = '~';
300 char Buffer[] = { Pad, Pad, P == Loc ? '^' : Byte, Byte, 0 };
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +0000301 Printf((P % 8 == 0) ? Buffer : &Buffer[1]);
Richard Smith25ee97f2012-12-18 06:30:32 +0000302 }
Stephen Hines6a211c52014-07-21 00:49:56 -0700303 Printf("%s\n", Decor.EndHighlight());
Richard Smith25ee97f2012-12-18 06:30:32 +0000304
305 // Go over the line again, and print names for the ranges.
306 InRange = 0;
307 unsigned Spaces = 0;
308 for (uptr P = Min; P != Max; ++P) {
309 if (!InRange || InRange->getEnd().getMemoryLocation() == P)
310 InRange = upperBound(P, Ranges, NumRanges);
311 if (!InRange)
312 break;
313
314 Spaces += (P % 8) == 0 ? 2 : 1;
315
316 if (InRange && InRange->getStart().getMemoryLocation() == P) {
317 while (Spaces--)
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +0000318 Printf(" ");
Richard Smith25ee97f2012-12-18 06:30:32 +0000319 renderText(InRange->getText(), Args);
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +0000320 Printf("\n");
Richard Smith25ee97f2012-12-18 06:30:32 +0000321 // FIXME: We only support naming one range for now!
322 break;
323 }
324
325 Spaces += 2;
326 }
327
328 // FIXME: Print names for anything we can identify within the line:
329 //
330 // * If we can identify the memory itself as belonging to a particular
331 // global, stack variable, or dynamic allocation, then do so.
332 //
333 // * If we have a pointer-size, pointer-aligned range highlighted,
334 // determine whether the value of that range is a pointer to an
335 // entity which we can name, and if so, print that name.
336 //
337 // This needs an external symbolizer, or (preferably) ASan instrumentation.
338}
339
340Diag::~Diag() {
Stephen Hines6d186232014-11-26 17:56:19 -0800341 // All diagnostics should be printed under report mutex.
342 CommonSanitizerReportMutex.CheckLocked();
Stephen Hines6a211c52014-07-21 00:49:56 -0700343 Decorator Decor;
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +0000344 Printf(Decor.Bold());
Richard Smith25ee97f2012-12-18 06:30:32 +0000345
346 renderLocation(Loc);
347
348 switch (Level) {
349 case DL_Error:
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +0000350 Printf("%s runtime error: %s%s",
Stephen Hines6a211c52014-07-21 00:49:56 -0700351 Decor.Warning(), Decor.EndWarning(), Decor.Bold());
Richard Smith25ee97f2012-12-18 06:30:32 +0000352 break;
353
354 case DL_Note:
Stephen Hines6a211c52014-07-21 00:49:56 -0700355 Printf("%s note: %s", Decor.Note(), Decor.EndNote());
Richard Smith25ee97f2012-12-18 06:30:32 +0000356 break;
357 }
358
359 renderText(Message, Args);
360
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +0000361 Printf("%s\n", Decor.Default());
Richard Smith25ee97f2012-12-18 06:30:32 +0000362
363 if (Loc.isMemoryLocation())
Alexey Samsonov8f72f7c2013-02-27 12:58:24 +0000364 renderMemorySnippet(Decor, Loc.getMemoryLocation(), Ranges,
Richard Smithc2424462013-02-12 22:12:10 +0000365 NumRanges, Args);
Richard Smith6ebe4512012-10-09 19:34:32 +0000366}
Stephen Hines6d186232014-11-26 17:56:19 -0800367
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800368ScopedReport::ScopedReport(ReportOptions Opts, Location SummaryLoc,
369 ErrorType Type)
370 : Opts(Opts), SummaryLoc(SummaryLoc), Type(Type) {
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700371 InitAsStandaloneIfNecessary();
Stephen Hines6d186232014-11-26 17:56:19 -0800372 CommonSanitizerReportMutex.Lock();
373}
374
375ScopedReport::~ScopedReport() {
376 MaybePrintStackTrace(Opts.pc, Opts.bp);
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800377 MaybeReportErrorSummary(SummaryLoc, Type);
Stephen Hines6d186232014-11-26 17:56:19 -0800378 CommonSanitizerReportMutex.Unlock();
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800379 if (flags()->halt_on_error)
Stephen Hines6d186232014-11-26 17:56:19 -0800380 Die();
381}
382
Stephen Hines86277eb2015-03-23 12:06:32 -0700383ALIGNED(64) static char suppression_placeholder[sizeof(SuppressionContext)];
384static SuppressionContext *suppression_ctx = nullptr;
385static const char kVptrCheck[] = "vptr_check";
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800386static const char *kSuppressionTypes[] = {
387#define UBSAN_CHECK(Name, SummaryKind, FSanitizeFlagName) FSanitizeFlagName,
388#include "ubsan_checks.inc"
389#undef UBSAN_CHECK
390 kVptrCheck,
391};
Stephen Hines86277eb2015-03-23 12:06:32 -0700392
393void __ubsan::InitializeSuppressions() {
394 CHECK_EQ(nullptr, suppression_ctx);
395 suppression_ctx = new (suppression_placeholder) // NOLINT
396 SuppressionContext(kSuppressionTypes, ARRAY_SIZE(kSuppressionTypes));
397 suppression_ctx->ParseFromFile(flags()->suppressions);
398}
399
400bool __ubsan::IsVptrCheckSuppressed(const char *TypeName) {
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700401 InitAsStandaloneIfNecessary();
Stephen Hines86277eb2015-03-23 12:06:32 -0700402 CHECK(suppression_ctx);
403 Suppression *s;
404 return suppression_ctx->Match(TypeName, kVptrCheck, &s);
Stephen Hines6d186232014-11-26 17:56:19 -0800405}
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700406
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800407bool __ubsan::IsPCSuppressed(ErrorType ET, uptr PC, const char *Filename) {
408 InitAsStandaloneIfNecessary();
409 CHECK(suppression_ctx);
410 const char *SuppType = ConvertTypeToFlagName(ET);
411 // Fast path: don't symbolize PC if there is no suppressions for given UB
412 // type.
413 if (!suppression_ctx->HasSuppressionType(SuppType))
414 return false;
415 Suppression *s = nullptr;
416 // Suppress by file name known to runtime.
417 if (Filename != nullptr && suppression_ctx->Match(Filename, SuppType, &s))
418 return true;
419 // Suppress by module name.
420 if (const char *Module = Symbolizer::GetOrInit()->GetModuleNameForPc(PC)) {
421 if (suppression_ctx->Match(Module, SuppType, &s))
422 return true;
423 }
424 // Suppress by function or source file name from debug info.
425 SymbolizedStackHolder Stack(Symbolizer::GetOrInit()->SymbolizePC(PC));
426 const AddressInfo &AI = Stack.get()->info;
427 return suppression_ctx->Match(AI.function, SuppType, &s) ||
428 suppression_ctx->Match(AI.file, SuppType, &s);
429}
430
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700431#endif // CAN_SANITIZE_UB