2019-08-01 01:51:05 +08:00
|
|
|
//===-- ubsan_handlers.cpp ------------------------------------------------===//
|
2012-10-10 03:34:32 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2012-10-10 03:34:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Error logging entry points for the UBSan runtime.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-27 01:26:04 +08:00
|
|
|
#include "ubsan_platform.h"
|
|
|
|
#if CAN_SANITIZE_UB
|
2012-10-10 03:34:32 +08:00
|
|
|
#include "ubsan_handlers.h"
|
|
|
|
#include "ubsan_diag.h"
|
2018-06-28 02:24:46 +08:00
|
|
|
#include "ubsan_flags.h"
|
2018-06-23 01:21:17 +08:00
|
|
|
#include "ubsan_monitor.h"
|
2019-12-14 04:59:40 +08:00
|
|
|
#include "ubsan_value.h"
|
2012-10-10 03:34:32 +08:00
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
|
|
|
|
using namespace __sanitizer;
|
|
|
|
using namespace __ubsan;
|
|
|
|
|
2015-12-09 08:12:57 +08:00
|
|
|
namespace __ubsan {
|
2015-12-19 03:56:42 +08:00
|
|
|
bool ignoreReport(SourceLocation SLoc, ReportOptions Opts, ErrorType ET) {
|
2015-12-09 08:12:57 +08:00
|
|
|
// We are not allowed to skip error report: if we are in unrecoverable
|
|
|
|
// handler, we have to terminate the program right now, and therefore
|
|
|
|
// have to print some diagnostic.
|
|
|
|
//
|
|
|
|
// Even if source location is disabled, it doesn't mean that we have
|
|
|
|
// already report an error to the user: some concurrently running
|
|
|
|
// thread could have acquired it, but not yet printed the report.
|
|
|
|
if (Opts.FromUnrecoverableHandler)
|
|
|
|
return false;
|
2015-12-19 03:56:42 +08:00
|
|
|
return SLoc.isDisabled() || IsPCSuppressed(ET, Opts.pc, SLoc.getFilename());
|
2014-09-11 04:43:36 +08:00
|
|
|
}
|
|
|
|
|
2020-02-29 06:09:14 +08:00
|
|
|
/// Situations in which we might emit a check for the suitability of a
|
|
|
|
/// pointer or glvalue. Needs to be kept in sync with CodeGenFunction.h in
|
|
|
|
/// clang.
|
|
|
|
enum TypeCheckKind {
|
|
|
|
/// Checking the operand of a load. Must be suitably sized and aligned.
|
|
|
|
TCK_Load,
|
|
|
|
/// Checking the destination of a store. Must be suitably sized and aligned.
|
|
|
|
TCK_Store,
|
|
|
|
/// Checking the bound value in a reference binding. Must be suitably sized
|
|
|
|
/// and aligned, but is not required to refer to an object (until the
|
|
|
|
/// reference is used), per core issue 453.
|
|
|
|
TCK_ReferenceBinding,
|
|
|
|
/// Checking the object expression in a non-static data member access. Must
|
|
|
|
/// be an object within its lifetime.
|
|
|
|
TCK_MemberAccess,
|
|
|
|
/// Checking the 'this' pointer for a call to a non-static member function.
|
|
|
|
/// Must be an object within its lifetime.
|
|
|
|
TCK_MemberCall,
|
|
|
|
/// Checking the 'this' pointer for a constructor call.
|
|
|
|
TCK_ConstructorCall,
|
|
|
|
/// Checking the operand of a static_cast to a derived pointer type. Must be
|
|
|
|
/// null or an object within its lifetime.
|
|
|
|
TCK_DowncastPointer,
|
|
|
|
/// Checking the operand of a static_cast to a derived reference type. Must
|
|
|
|
/// be an object within its lifetime.
|
|
|
|
TCK_DowncastReference,
|
|
|
|
/// Checking the operand of a cast to a base object. Must be suitably sized
|
|
|
|
/// and aligned.
|
|
|
|
TCK_Upcast,
|
|
|
|
/// Checking the operand of a cast to a virtual base object. Must be an
|
|
|
|
/// object within its lifetime.
|
|
|
|
TCK_UpcastToVirtualBase,
|
|
|
|
/// Checking the value assigned to a _Nonnull pointer. Must not be null.
|
|
|
|
TCK_NonnullAssign,
|
|
|
|
/// Checking the operand of a dynamic_cast or a typeid expression. Must be
|
|
|
|
/// null or an object within its lifetime.
|
|
|
|
TCK_DynamicOperation
|
|
|
|
};
|
|
|
|
|
2014-10-14 07:59:00 +08:00
|
|
|
const char *TypeCheckKinds[] = {
|
2012-10-10 03:34:32 +08:00
|
|
|
"load of", "store to", "reference binding to", "member access within",
|
2014-10-14 07:59:00 +08:00
|
|
|
"member call on", "constructor call on", "downcast of", "downcast of",
|
2017-12-28 20:45:23 +08:00
|
|
|
"upcast of", "cast to virtual base of", "_Nonnull binding to",
|
|
|
|
"dynamic operation on"};
|
2012-10-25 10:07:02 +08:00
|
|
|
}
|
|
|
|
|
2012-12-18 12:23:18 +08:00
|
|
|
static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer,
|
2015-02-11 03:50:20 +08:00
|
|
|
ReportOptions Opts) {
|
2013-01-09 11:40:03 +08:00
|
|
|
Location Loc = Data->Loc.acquire();
|
2012-12-18 12:23:18 +08:00
|
|
|
|
2017-01-06 22:40:28 +08:00
|
|
|
uptr Alignment = (uptr)1 << Data->LogAlignment;
|
2015-12-09 07:29:33 +08:00
|
|
|
ErrorType ET;
|
|
|
|
if (!Pointer)
|
2020-02-29 06:09:14 +08:00
|
|
|
ET = (Data->TypeCheckKind == TCK_NonnullAssign)
|
|
|
|
? ErrorType::NullPointerUseWithNullability
|
|
|
|
: ErrorType::NullPointerUse;
|
2017-01-06 22:40:28 +08:00
|
|
|
else if (Pointer & (Alignment - 1))
|
2015-12-09 07:29:33 +08:00
|
|
|
ET = ErrorType::MisalignedPointerUse;
|
|
|
|
else
|
|
|
|
ET = ErrorType::InsufficientObjectSize;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
// Use the SourceLocation from Data to track deduplication, even if it's
|
|
|
|
// invalid.
|
|
|
|
if (ignoreReport(Loc.getSourceLocation(), Opts, ET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
SymbolizedStackHolder FallbackLoc;
|
|
|
|
if (Data->Loc.isInvalid()) {
|
|
|
|
FallbackLoc.reset(getCallerLocation(Opts.pc));
|
|
|
|
Loc = FallbackLoc;
|
|
|
|
}
|
|
|
|
|
2015-12-09 07:29:33 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-20 02:33:45 +08:00
|
|
|
|
2015-12-09 07:29:33 +08:00
|
|
|
switch (ET) {
|
|
|
|
case ErrorType::NullPointerUse:
|
2020-02-29 06:09:14 +08:00
|
|
|
case ErrorType::NullPointerUseWithNullability:
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "%0 null pointer of type %1")
|
2015-12-09 07:29:33 +08:00
|
|
|
<< TypeCheckKinds[Data->TypeCheckKind] << Data->Type;
|
|
|
|
break;
|
|
|
|
case ErrorType::MisalignedPointerUse:
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "%0 misaligned address %1 for type %3, "
|
2012-12-18 14:30:32 +08:00
|
|
|
"which requires %2 byte alignment")
|
2017-01-06 22:40:28 +08:00
|
|
|
<< TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer << Alignment
|
|
|
|
<< Data->Type;
|
2015-12-09 07:29:33 +08:00
|
|
|
break;
|
|
|
|
case ErrorType::InsufficientObjectSize:
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "%0 address %1 with insufficient space "
|
2012-12-18 14:30:32 +08:00
|
|
|
"for an object of type %2")
|
2015-12-09 07:29:33 +08:00
|
|
|
<< TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer << Data->Type;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE("unexpected error type!");
|
2015-08-25 07:18:49 +08:00
|
|
|
}
|
2015-12-09 07:29:33 +08:00
|
|
|
|
2012-12-18 14:30:32 +08:00
|
|
|
if (Pointer)
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Pointer, DL_Note, ET, "pointer points here");
|
2012-12-03 03:47:29 +08:00
|
|
|
}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2017-01-06 22:40:28 +08:00
|
|
|
void __ubsan::__ubsan_handle_type_mismatch_v1(TypeMismatchData *Data,
|
|
|
|
ValueHandle Pointer) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
2015-02-11 03:50:20 +08:00
|
|
|
handleTypeMismatchImpl(Data, Pointer, Opts);
|
2012-12-18 12:23:18 +08:00
|
|
|
}
|
2017-01-06 22:40:28 +08:00
|
|
|
void __ubsan::__ubsan_handle_type_mismatch_v1_abort(TypeMismatchData *Data,
|
|
|
|
ValueHandle Pointer) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
2015-02-11 03:50:20 +08:00
|
|
|
handleTypeMismatchImpl(Data, Pointer, Opts);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
|
|
|
|
[compiler-rt][UBSan] Sanitization for alignment assumptions.
Summary:
This is the compiler-rt part.
The clang part is D54589.
This is a second commit, the original one was r351106,
which was mass-reverted in r351159 because 2 compiler-rt tests were failing.
Now, i have fundamentally changed the testing approach:
i malloc a few bytes, intentionally mis-align the pointer
(increment it by one), and check that. Also, i have decreased
the expected alignment. This hopefully should be enough to pacify
all the bots. If not, i guess i might just drop the two 'bad' tests.
Reviewers: filcab, vsk, #sanitizers, vitalybuka, rsmith, morehouse
Reviewed By: morehouse
Subscribers: rjmccall, krytarowski, rsmith, kcc, srhines, kubamracek, dberris, llvm-commits
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D54590
llvm-svn: 351178
2019-01-15 17:44:27 +08:00
|
|
|
static void handleAlignmentAssumptionImpl(AlignmentAssumptionData *Data,
|
|
|
|
ValueHandle Pointer,
|
|
|
|
ValueHandle Alignment,
|
|
|
|
ValueHandle Offset,
|
|
|
|
ReportOptions Opts) {
|
|
|
|
Location Loc = Data->Loc.acquire();
|
|
|
|
SourceLocation AssumptionLoc = Data->AssumptionLoc.acquire();
|
|
|
|
|
|
|
|
ErrorType ET = ErrorType::AlignmentAssumption;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc.getSourceLocation(), Opts, ET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScopedReport R(Opts, Loc, ET);
|
|
|
|
|
|
|
|
uptr RealPointer = Pointer - Offset;
|
|
|
|
uptr LSB = LeastSignificantSetBitIndex(RealPointer);
|
|
|
|
uptr ActualAlignment = uptr(1) << LSB;
|
|
|
|
|
|
|
|
uptr Mask = Alignment - 1;
|
|
|
|
uptr MisAlignmentOffset = RealPointer & Mask;
|
|
|
|
|
|
|
|
if (!Offset) {
|
|
|
|
Diag(Loc, DL_Error, ET,
|
|
|
|
"assumption of %0 byte alignment for pointer of type %1 failed")
|
|
|
|
<< Alignment << Data->Type;
|
|
|
|
} else {
|
|
|
|
Diag(Loc, DL_Error, ET,
|
|
|
|
"assumption of %0 byte alignment (with offset of %1 byte) for pointer "
|
|
|
|
"of type %2 failed")
|
|
|
|
<< Alignment << Offset << Data->Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AssumptionLoc.isInvalid())
|
|
|
|
Diag(AssumptionLoc, DL_Note, ET, "alignment assumption was specified here");
|
|
|
|
|
|
|
|
Diag(RealPointer, DL_Note, ET,
|
|
|
|
"%0address is %1 aligned, misalignment offset is %2 bytes")
|
|
|
|
<< (Offset ? "offset " : "") << ActualAlignment << MisAlignmentOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_alignment_assumption(AlignmentAssumptionData *Data,
|
|
|
|
ValueHandle Pointer,
|
|
|
|
ValueHandle Alignment,
|
|
|
|
ValueHandle Offset) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleAlignmentAssumptionImpl(Data, Pointer, Alignment, Offset, Opts);
|
|
|
|
}
|
|
|
|
void __ubsan::__ubsan_handle_alignment_assumption_abort(
|
|
|
|
AlignmentAssumptionData *Data, ValueHandle Pointer, ValueHandle Alignment,
|
|
|
|
ValueHandle Offset) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleAlignmentAssumptionImpl(Data, Pointer, Alignment, Offset, Opts);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2012-11-27 23:01:43 +08:00
|
|
|
/// \brief Common diagnostic emission for various forms of integer overflow.
|
2014-07-30 09:49:19 +08:00
|
|
|
template <typename T>
|
|
|
|
static void handleIntegerOverflowImpl(OverflowData *Data, ValueHandle LHS,
|
2014-08-23 05:42:04 +08:00
|
|
|
const char *Operator, T RHS,
|
|
|
|
ReportOptions Opts) {
|
2013-01-09 11:40:03 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2015-12-19 03:56:42 +08:00
|
|
|
bool IsSigned = Data->Type.isSignedIntegerTy();
|
|
|
|
ErrorType ET = IsSigned ? ErrorType::SignedIntegerOverflow
|
|
|
|
: ErrorType::UnsignedIntegerOverflow;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-01-09 11:40:03 +08:00
|
|
|
return;
|
|
|
|
|
[compiler-rt][UBSan] silence_unsigned_overflow: do *NOT* ignore *fatal* unsigned overflows
Summary:
D48660 / rL335762 added a `silence_unsigned_overflow` env flag for [[ https://github.com/google/oss-fuzz/pull/1717 | oss-fuzz needs ]],
that allows to silence the reports from unsigned overflows.
It makes sense, it is there because `-fsanitize=integer` sanitizer is not enabled on oss-fuzz,
so this allows to still use it as an interestingness signal, without getting the actual reports.
However there is a slight problem here.
All types of unsigned overflows are ignored.
Even if `-fno-sanitize-recover=unsigned` was used (which means the program will die after the report)
there will still be no report, the program will just silently die.
At the moment there are just two projects on oss-fuzz that care:
* [[ https://github.com/google/oss-fuzz/blob/8eeffa627f937040aaf8ba1b7d93f43f77d74fb9/projects/llvm_libcxx/build.sh#L18-L20 | libc++ ]]
* [[ https://github.com/google/oss-fuzz/blob/8eeffa627f937040aaf8ba1b7d93f43f77d74fb9/projects/librawspeed/build.sh | RawSpeed ]] (me)
I suppose this could be overridden there ^, but i really don't think this is intended behavior in any case..
Reviewers: kcc, Dor1s, #sanitizers, filcab, vsk, kubamracek
Reviewed By: Dor1s
Subscribers: dberris, mclow.lists, llvm-commits
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D54771
llvm-svn: 347415
2018-11-22 04:35:43 +08:00
|
|
|
// If this is an unsigned overflow in non-fatal mode, potentially ignore it.
|
|
|
|
if (!IsSigned && !Opts.FromUnrecoverableHandler &&
|
|
|
|
flags()->silence_unsigned_overflow)
|
2018-06-28 02:24:46 +08:00
|
|
|
return;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "%0 integer overflow: "
|
|
|
|
"%1 %2 %3 cannot be represented in type %4")
|
|
|
|
<< (IsSigned ? "signed" : "unsigned") << Value(Data->Type, LHS)
|
|
|
|
<< Operator << RHS << Data->Type;
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
|
|
|
|
2015-12-09 08:12:57 +08:00
|
|
|
#define UBSAN_OVERFLOW_HANDLER(handler_name, op, unrecoverable) \
|
2014-07-30 09:49:19 +08:00
|
|
|
void __ubsan::handler_name(OverflowData *Data, ValueHandle LHS, \
|
|
|
|
ValueHandle RHS) { \
|
2015-12-09 08:12:57 +08:00
|
|
|
GET_REPORT_OPTIONS(unrecoverable); \
|
2014-08-23 05:42:04 +08:00
|
|
|
handleIntegerOverflowImpl(Data, LHS, op, Value(Data->Type, RHS), Opts); \
|
2015-12-09 08:12:57 +08:00
|
|
|
if (unrecoverable) \
|
|
|
|
Die(); \
|
2014-07-30 09:49:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow, "+", false)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_add_overflow_abort, "+", true)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow, "-", false)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_sub_overflow_abort, "-", true)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow, "*", false)
|
|
|
|
UBSAN_OVERFLOW_HANDLER(__ubsan_handle_mul_overflow_abort, "*", true)
|
|
|
|
|
|
|
|
static void handleNegateOverflowImpl(OverflowData *Data, ValueHandle OldVal,
|
2014-08-23 05:42:04 +08:00
|
|
|
ReportOptions Opts) {
|
2013-01-09 11:40:03 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2015-12-19 03:56:42 +08:00
|
|
|
bool IsSigned = Data->Type.isSignedIntegerTy();
|
|
|
|
ErrorType ET = IsSigned ? ErrorType::SignedIntegerOverflow
|
|
|
|
: ErrorType::UnsignedIntegerOverflow;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-01-09 11:40:03 +08:00
|
|
|
return;
|
|
|
|
|
2018-07-14 06:49:06 +08:00
|
|
|
if (!IsSigned && flags()->silence_unsigned_overflow)
|
|
|
|
return;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2015-08-25 07:18:49 +08:00
|
|
|
if (IsSigned)
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
2012-12-31 14:36:44 +08:00
|
|
|
"negation of %0 cannot be represented in type %1; "
|
|
|
|
"cast to an unsigned type to negate this value to itself")
|
2015-08-25 07:18:49 +08:00
|
|
|
<< Value(Data->Type, OldVal) << Data->Type;
|
2012-12-31 14:36:44 +08:00
|
|
|
else
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "negation of %0 cannot be represented in type %1")
|
2015-08-25 07:18:49 +08:00
|
|
|
<< Value(Data->Type, OldVal) << Data->Type;
|
2012-12-03 03:47:29 +08:00
|
|
|
}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_negate_overflow(OverflowData *Data,
|
|
|
|
ValueHandle OldVal) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleNegateOverflowImpl(Data, OldVal, Opts);
|
2014-07-30 09:49:19 +08:00
|
|
|
}
|
2012-12-03 03:47:29 +08:00
|
|
|
void __ubsan::__ubsan_handle_negate_overflow_abort(OverflowData *Data,
|
|
|
|
ValueHandle OldVal) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleNegateOverflowImpl(Data, OldVal, Opts);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 09:49:19 +08:00
|
|
|
static void handleDivremOverflowImpl(OverflowData *Data, ValueHandle LHS,
|
2014-08-23 05:42:04 +08:00
|
|
|
ValueHandle RHS, ReportOptions Opts) {
|
2013-01-09 11:40:03 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2012-10-10 03:34:32 +08:00
|
|
|
Value LHSVal(Data->Type, LHS);
|
|
|
|
Value RHSVal(Data->Type, RHS);
|
2015-12-09 07:29:33 +08:00
|
|
|
|
|
|
|
ErrorType ET;
|
|
|
|
if (RHSVal.isMinusOne())
|
|
|
|
ET = ErrorType::SignedIntegerOverflow;
|
|
|
|
else if (Data->Type.isIntegerTy())
|
|
|
|
ET = ErrorType::IntegerDivideByZero;
|
|
|
|
else
|
|
|
|
ET = ErrorType::FloatDivideByZero;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
|
|
|
return;
|
|
|
|
|
2015-12-09 07:29:33 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
|
|
|
|
|
|
|
switch (ET) {
|
|
|
|
case ErrorType::SignedIntegerOverflow:
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
|
|
|
"division of %0 by -1 cannot be represented in type %1")
|
2015-12-09 07:29:33 +08:00
|
|
|
<< LHSVal << Data->Type;
|
|
|
|
break;
|
|
|
|
default:
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "division by zero");
|
2015-12-09 07:29:33 +08:00
|
|
|
break;
|
2015-08-25 07:18:49 +08:00
|
|
|
}
|
2012-12-03 03:47:29 +08:00
|
|
|
}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_divrem_overflow(OverflowData *Data,
|
|
|
|
ValueHandle LHS, ValueHandle RHS) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleDivremOverflowImpl(Data, LHS, RHS, Opts);
|
2014-07-30 09:49:19 +08:00
|
|
|
}
|
2012-12-03 03:47:29 +08:00
|
|
|
void __ubsan::__ubsan_handle_divrem_overflow_abort(OverflowData *Data,
|
|
|
|
ValueHandle LHS,
|
|
|
|
ValueHandle RHS) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleDivremOverflowImpl(Data, LHS, RHS, Opts);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 09:49:19 +08:00
|
|
|
static void handleShiftOutOfBoundsImpl(ShiftOutOfBoundsData *Data,
|
|
|
|
ValueHandle LHS, ValueHandle RHS,
|
2014-08-23 05:42:04 +08:00
|
|
|
ReportOptions Opts) {
|
2013-01-09 11:40:03 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2012-10-10 03:34:32 +08:00
|
|
|
Value LHSVal(Data->LHSType, LHS);
|
|
|
|
Value RHSVal(Data->RHSType, RHS);
|
2015-12-09 07:29:33 +08:00
|
|
|
|
|
|
|
ErrorType ET;
|
|
|
|
if (RHSVal.isNegative() ||
|
|
|
|
RHSVal.getPositiveIntValue() >= Data->LHSType.getIntegerBitWidth())
|
|
|
|
ET = ErrorType::InvalidShiftExponent;
|
|
|
|
else
|
|
|
|
ET = ErrorType::InvalidShiftBase;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
|
|
|
return;
|
|
|
|
|
2015-12-09 07:29:33 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
|
|
|
|
|
|
|
if (ET == ErrorType::InvalidShiftExponent) {
|
|
|
|
if (RHSVal.isNegative())
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "shift exponent %0 is negative") << RHSVal;
|
2015-12-09 07:29:33 +08:00
|
|
|
else
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
|
|
|
"shift exponent %0 is too large for %1-bit type %2")
|
2015-12-09 07:29:33 +08:00
|
|
|
<< RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType;
|
2015-08-25 07:18:49 +08:00
|
|
|
} else {
|
2015-12-09 07:29:33 +08:00
|
|
|
if (LHSVal.isNegative())
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "left shift of negative value %0") << LHSVal;
|
2015-12-09 07:29:33 +08:00
|
|
|
else
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
2015-12-09 07:29:33 +08:00
|
|
|
"left shift of %0 by %1 places cannot be represented in type %2")
|
|
|
|
<< LHSVal << RHSVal << Data->LHSType;
|
2015-08-25 07:18:49 +08:00
|
|
|
}
|
2012-12-03 03:47:29 +08:00
|
|
|
}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_shift_out_of_bounds(ShiftOutOfBoundsData *Data,
|
|
|
|
ValueHandle LHS,
|
|
|
|
ValueHandle RHS) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
|
2014-07-30 09:49:19 +08:00
|
|
|
}
|
2012-12-03 03:47:29 +08:00
|
|
|
void __ubsan::__ubsan_handle_shift_out_of_bounds_abort(
|
|
|
|
ShiftOutOfBoundsData *Data,
|
|
|
|
ValueHandle LHS,
|
|
|
|
ValueHandle RHS) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleShiftOutOfBoundsImpl(Data, LHS, RHS, Opts);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
|
|
|
|
2014-07-30 09:49:19 +08:00
|
|
|
static void handleOutOfBoundsImpl(OutOfBoundsData *Data, ValueHandle Index,
|
2014-08-23 05:42:04 +08:00
|
|
|
ReportOptions Opts) {
|
2013-02-23 10:40:07 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2015-12-19 03:56:42 +08:00
|
|
|
ErrorType ET = ErrorType::OutOfBoundsIndex;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-02-23 10:40:07 +08:00
|
|
|
return;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2013-02-23 10:40:07 +08:00
|
|
|
Value IndexVal(Data->IndexType, Index);
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "index %0 out of bounds for type %1")
|
2013-02-23 10:40:07 +08:00
|
|
|
<< IndexVal << Data->ArrayType;
|
|
|
|
}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_out_of_bounds(OutOfBoundsData *Data,
|
|
|
|
ValueHandle Index) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleOutOfBoundsImpl(Data, Index, Opts);
|
2014-07-30 09:49:19 +08:00
|
|
|
}
|
2013-02-23 10:40:07 +08:00
|
|
|
void __ubsan::__ubsan_handle_out_of_bounds_abort(OutOfBoundsData *Data,
|
|
|
|
ValueHandle Index) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleOutOfBoundsImpl(Data, Index, Opts);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2013-02-23 10:40:07 +08:00
|
|
|
}
|
|
|
|
|
2014-09-11 04:43:36 +08:00
|
|
|
static void handleBuiltinUnreachableImpl(UnreachableData *Data,
|
|
|
|
ReportOptions Opts) {
|
2018-06-23 01:21:17 +08:00
|
|
|
ErrorType ET = ErrorType::UnreachableCall;
|
|
|
|
ScopedReport R(Opts, Data->Loc, ET);
|
|
|
|
Diag(Data->Loc, DL_Error, ET,
|
|
|
|
"execution reached an unreachable program point");
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
|
|
|
|
2014-09-11 04:43:36 +08:00
|
|
|
void __ubsan::__ubsan_handle_builtin_unreachable(UnreachableData *Data) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
2014-09-11 04:43:36 +08:00
|
|
|
handleBuiltinUnreachableImpl(Data, Opts);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleMissingReturnImpl(UnreachableData *Data, ReportOptions Opts) {
|
2018-06-23 01:21:17 +08:00
|
|
|
ErrorType ET = ErrorType::MissingReturn;
|
|
|
|
ScopedReport R(Opts, Data->Loc, ET);
|
|
|
|
Diag(Data->Loc, DL_Error, ET,
|
2012-12-18 14:30:32 +08:00
|
|
|
"execution reached the end of a value-returning function "
|
|
|
|
"without returning a value");
|
2012-10-10 03:34:32 +08:00
|
|
|
}
|
2012-10-10 09:10:59 +08:00
|
|
|
|
2014-09-11 04:43:36 +08:00
|
|
|
void __ubsan::__ubsan_handle_missing_return(UnreachableData *Data) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleMissingReturnImpl(Data, Opts);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2014-07-30 09:49:19 +08:00
|
|
|
static void handleVLABoundNotPositive(VLABoundData *Data, ValueHandle Bound,
|
2014-08-23 05:42:04 +08:00
|
|
|
ReportOptions Opts) {
|
2013-01-09 11:40:03 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2015-12-19 03:56:42 +08:00
|
|
|
ErrorType ET = ErrorType::NonPositiveVLAIndex;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2013-01-09 11:40:03 +08:00
|
|
|
return;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET, "variable length array bound evaluates to "
|
|
|
|
"non-positive value %0")
|
|
|
|
<< Value(Data->Type, Bound);
|
2012-12-03 03:47:29 +08:00
|
|
|
}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_vla_bound_not_positive(VLABoundData *Data,
|
|
|
|
ValueHandle Bound) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleVLABoundNotPositive(Data, Bound, Opts);
|
2014-07-30 09:49:19 +08:00
|
|
|
}
|
2012-12-03 03:47:29 +08:00
|
|
|
void __ubsan::__ubsan_handle_vla_bound_not_positive_abort(VLABoundData *Data,
|
2014-07-30 09:49:19 +08:00
|
|
|
ValueHandle Bound) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleVLABoundNotPositive(Data, Bound, Opts);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2012-10-10 09:10:59 +08:00
|
|
|
}
|
2012-10-13 06:57:15 +08:00
|
|
|
|
2015-08-11 12:19:24 +08:00
|
|
|
static bool looksLikeFloatCastOverflowDataV1(void *Data) {
|
|
|
|
// First field is either a pointer to filename or a pointer to a
|
|
|
|
// TypeDescriptor.
|
|
|
|
u8 *FilenameOrTypeDescriptor;
|
|
|
|
internal_memcpy(&FilenameOrTypeDescriptor, Data,
|
|
|
|
sizeof(FilenameOrTypeDescriptor));
|
|
|
|
|
|
|
|
// Heuristic: For float_cast_overflow, the TypeKind will be either TK_Integer
|
2015-10-20 16:40:52 +08:00
|
|
|
// (0x0), TK_Float (0x1) or TK_Unknown (0xff). If both types are known,
|
|
|
|
// adding both bytes will be 0 or 1 (for BE or LE). If it were a filename,
|
|
|
|
// adding two printable characters will not yield such a value. Otherwise,
|
|
|
|
// if one of them is 0xff, this is most likely TK_Unknown type descriptor.
|
2015-08-11 12:19:24 +08:00
|
|
|
u16 MaybeFromTypeKind =
|
|
|
|
FilenameOrTypeDescriptor[0] + FilenameOrTypeDescriptor[1];
|
2015-10-20 16:40:52 +08:00
|
|
|
return MaybeFromTypeKind < 2 || FilenameOrTypeDescriptor[0] == 0xff ||
|
|
|
|
FilenameOrTypeDescriptor[1] == 0xff;
|
2015-08-11 12:19:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleFloatCastOverflow(void *DataPtr, ValueHandle From,
|
|
|
|
ReportOptions Opts) {
|
|
|
|
SymbolizedStackHolder CallerLoc;
|
|
|
|
Location Loc;
|
|
|
|
const TypeDescriptor *FromType, *ToType;
|
2015-12-19 03:56:42 +08:00
|
|
|
ErrorType ET = ErrorType::FloatCastOverflow;
|
2015-08-11 12:19:24 +08:00
|
|
|
|
|
|
|
if (looksLikeFloatCastOverflowDataV1(DataPtr)) {
|
|
|
|
auto Data = reinterpret_cast<FloatCastOverflowData *>(DataPtr);
|
|
|
|
CallerLoc.reset(getCallerLocation(Opts.pc));
|
|
|
|
Loc = CallerLoc;
|
|
|
|
FromType = &Data->FromType;
|
|
|
|
ToType = &Data->ToType;
|
|
|
|
} else {
|
|
|
|
auto Data = reinterpret_cast<FloatCastOverflowDataV2 *>(DataPtr);
|
|
|
|
SourceLocation SLoc = Data->Loc.acquire();
|
2015-12-19 03:56:42 +08:00
|
|
|
if (ignoreReport(SLoc, Opts, ET))
|
2015-08-11 12:19:24 +08:00
|
|
|
return;
|
|
|
|
Loc = SLoc;
|
|
|
|
FromType = &Data->FromType;
|
|
|
|
ToType = &Data->ToType;
|
|
|
|
}
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
2017-04-28 04:48:17 +08:00
|
|
|
"%0 is outside the range of representable values of type %2")
|
2015-08-11 12:19:24 +08:00
|
|
|
<< Value(*FromType, From) << *FromType << *ToType;
|
2012-12-03 03:47:29 +08:00
|
|
|
}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2015-08-11 12:19:24 +08:00
|
|
|
void __ubsan::__ubsan_handle_float_cast_overflow(void *Data, ValueHandle From) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleFloatCastOverflow(Data, From, Opts);
|
2014-07-30 09:49:19 +08:00
|
|
|
}
|
2015-08-11 12:19:24 +08:00
|
|
|
void __ubsan::__ubsan_handle_float_cast_overflow_abort(void *Data,
|
|
|
|
ValueHandle From) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleFloatCastOverflow(Data, From, Opts);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2012-10-13 06:57:15 +08:00
|
|
|
}
|
2012-12-13 15:00:14 +08:00
|
|
|
|
2014-07-30 09:49:19 +08:00
|
|
|
static void handleLoadInvalidValue(InvalidValueData *Data, ValueHandle Val,
|
2014-08-23 05:42:04 +08:00
|
|
|
ReportOptions Opts) {
|
2013-10-02 10:29:47 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2015-08-25 07:18:49 +08:00
|
|
|
// This check could be more precise if we used different handlers for
|
|
|
|
// -fsanitize=bool and -fsanitize=enum.
|
2017-05-05 09:35:42 +08:00
|
|
|
bool IsBool = (0 == internal_strcmp(Data->Type.getTypeName(), "'bool'")) ||
|
|
|
|
(0 == internal_strncmp(Data->Type.getTypeName(), "'BOOL'", 6));
|
2015-12-19 03:56:42 +08:00
|
|
|
ErrorType ET =
|
|
|
|
IsBool ? ErrorType::InvalidBoolLoad : ErrorType::InvalidEnumLoad;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
2012-12-18 14:30:32 +08:00
|
|
|
"load of value %0, which is not a valid value for type %1")
|
2018-06-23 01:21:17 +08:00
|
|
|
<< Value(Data->Type, Val) << Data->Type;
|
2012-12-13 15:00:14 +08:00
|
|
|
}
|
2014-07-30 09:49:19 +08:00
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_load_invalid_value(InvalidValueData *Data,
|
|
|
|
ValueHandle Val) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleLoadInvalidValue(Data, Val, Opts);
|
2014-07-30 09:49:19 +08:00
|
|
|
}
|
2012-12-13 15:00:14 +08:00
|
|
|
void __ubsan::__ubsan_handle_load_invalid_value_abort(InvalidValueData *Data,
|
|
|
|
ValueHandle Val) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleLoadInvalidValue(Data, Val, Opts);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2012-12-13 15:00:14 +08:00
|
|
|
}
|
2013-10-21 05:29:46 +08:00
|
|
|
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer truncation - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D48958.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: #sanitizers, samsonov, vsk, rsmith, pcc, eugenis, kcc, filcab
Reviewed By: #sanitizers, vsk, filcab
Subscribers: llvm-commits, eugenis, filcab, kubamracek, dberris, #sanitizers, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D48959
llvm-svn: 338287
2018-07-31 02:58:30 +08:00
|
|
|
static void handleImplicitConversion(ImplicitConversionData *Data,
|
|
|
|
ReportOptions Opts, ValueHandle Src,
|
|
|
|
ValueHandle Dst) {
|
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
|
|
|
ErrorType ET = ErrorType::GenericUB;
|
|
|
|
|
2018-10-11 17:09:52 +08:00
|
|
|
const TypeDescriptor &SrcTy = Data->FromType;
|
|
|
|
const TypeDescriptor &DstTy = Data->ToType;
|
|
|
|
|
|
|
|
bool SrcSigned = SrcTy.isSignedIntegerTy();
|
|
|
|
bool DstSigned = DstTy.isSignedIntegerTy();
|
|
|
|
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer truncation - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D48958.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: #sanitizers, samsonov, vsk, rsmith, pcc, eugenis, kcc, filcab
Reviewed By: #sanitizers, vsk, filcab
Subscribers: llvm-commits, eugenis, filcab, kubamracek, dberris, #sanitizers, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D48959
llvm-svn: 338287
2018-07-31 02:58:30 +08:00
|
|
|
switch (Data->Kind) {
|
2018-10-11 17:09:52 +08:00
|
|
|
case ICCK_IntegerTruncation: { // Legacy, no longer used.
|
|
|
|
// Let's figure out what it should be as per the new types, and upgrade.
|
|
|
|
// If both types are unsigned, then it's an unsigned truncation.
|
|
|
|
// Else, it is a signed truncation.
|
|
|
|
if (!SrcSigned && !DstSigned) {
|
|
|
|
ET = ErrorType::ImplicitUnsignedIntegerTruncation;
|
|
|
|
} else {
|
|
|
|
ET = ErrorType::ImplicitSignedIntegerTruncation;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ICCK_UnsignedIntegerTruncation:
|
|
|
|
ET = ErrorType::ImplicitUnsignedIntegerTruncation;
|
|
|
|
break;
|
|
|
|
case ICCK_SignedIntegerTruncation:
|
|
|
|
ET = ErrorType::ImplicitSignedIntegerTruncation;
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer truncation - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D48958.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: #sanitizers, samsonov, vsk, rsmith, pcc, eugenis, kcc, filcab
Reviewed By: #sanitizers, vsk, filcab
Subscribers: llvm-commits, eugenis, filcab, kubamracek, dberris, #sanitizers, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D48959
llvm-svn: 338287
2018-07-31 02:58:30 +08:00
|
|
|
break;
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer sign change - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D50250.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: vsk, filcab, #sanitizers
Reviewed By: filcab, #sanitizers
Subscribers: mclow.lists, srhines, kubamracek, dberris, rjmccall, rsmith, llvm-commits, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D50251
llvm-svn: 345659
2018-10-31 05:58:54 +08:00
|
|
|
case ICCK_IntegerSignChange:
|
|
|
|
ET = ErrorType::ImplicitIntegerSignChange;
|
|
|
|
break;
|
|
|
|
case ICCK_SignedIntegerTruncationOrSignChange:
|
|
|
|
ET = ErrorType::ImplicitSignedIntegerTruncationOrSignChange;
|
|
|
|
break;
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer truncation - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D48958.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: #sanitizers, samsonov, vsk, rsmith, pcc, eugenis, kcc, filcab
Reviewed By: #sanitizers, vsk, filcab
Subscribers: llvm-commits, eugenis, filcab, kubamracek, dberris, #sanitizers, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D48959
llvm-svn: 338287
2018-07-31 02:58:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScopedReport R(Opts, Loc, ET);
|
|
|
|
|
|
|
|
// FIXME: is it possible to dump the values as hex with fixed width?
|
|
|
|
|
|
|
|
Diag(Loc, DL_Error, ET,
|
|
|
|
"implicit conversion from type %0 of value %1 (%2-bit, %3signed) to "
|
|
|
|
"type %4 changed the value to %5 (%6-bit, %7signed)")
|
|
|
|
<< SrcTy << Value(SrcTy, Src) << SrcTy.getIntegerBitWidth()
|
2018-10-11 17:09:52 +08:00
|
|
|
<< (SrcSigned ? "" : "un") << DstTy << Value(DstTy, Dst)
|
|
|
|
<< DstTy.getIntegerBitWidth() << (DstSigned ? "" : "un");
|
[compiler-rt][ubsan] Implicit Conversion Sanitizer - integer truncation - compiler-rt part
Summary:
This is a compiler-rt part.
The clang part is D48958.
See [[ https://bugs.llvm.org/show_bug.cgi?id=21530 | PR21530 ]], https://github.com/google/sanitizers/issues/940.
Reviewers: #sanitizers, samsonov, vsk, rsmith, pcc, eugenis, kcc, filcab
Reviewed By: #sanitizers, vsk, filcab
Subscribers: llvm-commits, eugenis, filcab, kubamracek, dberris, #sanitizers, regehr
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D48959
llvm-svn: 338287
2018-07-31 02:58:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_implicit_conversion(ImplicitConversionData *Data,
|
|
|
|
ValueHandle Src,
|
|
|
|
ValueHandle Dst) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleImplicitConversion(Data, Opts, Src, Dst);
|
|
|
|
}
|
|
|
|
void __ubsan::__ubsan_handle_implicit_conversion_abort(
|
|
|
|
ImplicitConversionData *Data, ValueHandle Src, ValueHandle Dst) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleImplicitConversion(Data, Opts, Src, Dst);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2017-07-29 08:20:02 +08:00
|
|
|
static void handleInvalidBuiltin(InvalidBuiltinData *Data, ReportOptions Opts) {
|
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
|
|
|
ErrorType ET = ErrorType::InvalidBuiltin;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScopedReport R(Opts, Loc, ET);
|
|
|
|
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
2017-07-29 08:20:02 +08:00
|
|
|
"passing zero to %0, which is not a valid argument")
|
|
|
|
<< ((Data->Kind == BCK_CTZPassedZero) ? "ctz()" : "clz()");
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_invalid_builtin(InvalidBuiltinData *Data) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleInvalidBuiltin(Data, Opts);
|
|
|
|
}
|
|
|
|
void __ubsan::__ubsan_handle_invalid_builtin_abort(InvalidBuiltinData *Data) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleInvalidBuiltin(Data, Opts);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2019-12-14 04:59:40 +08:00
|
|
|
static void handleInvalidObjCCast(InvalidObjCCast *Data, ValueHandle Pointer,
|
|
|
|
ReportOptions Opts) {
|
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
|
|
|
ErrorType ET = ErrorType::InvalidObjCCast;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScopedReport R(Opts, Loc, ET);
|
|
|
|
|
|
|
|
const char *GivenClass = getObjCClassName(Pointer);
|
|
|
|
const char *GivenClassStr = GivenClass ? GivenClass : "<unknown type>";
|
|
|
|
|
|
|
|
Diag(Loc, DL_Error, ET,
|
|
|
|
"invalid ObjC cast, object is a '%0', but expected a %1")
|
|
|
|
<< GivenClassStr << Data->ExpectedType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_invalid_objc_cast(InvalidObjCCast *Data,
|
|
|
|
ValueHandle Pointer) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleInvalidObjCCast(Data, Pointer, Opts);
|
|
|
|
}
|
|
|
|
void __ubsan::__ubsan_handle_invalid_objc_cast_abort(InvalidObjCCast *Data,
|
|
|
|
ValueHandle Pointer) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleInvalidObjCCast(Data, Pointer, Opts);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2017-06-24 05:32:48 +08:00
|
|
|
static void handleNonNullReturn(NonNullReturnData *Data, SourceLocation *LocPtr,
|
|
|
|
ReportOptions Opts, bool IsAttr) {
|
|
|
|
if (!LocPtr)
|
|
|
|
UNREACHABLE("source location pointer is null!");
|
|
|
|
|
|
|
|
SourceLocation Loc = LocPtr->acquire();
|
2020-02-29 06:09:14 +08:00
|
|
|
ErrorType ET = IsAttr ? ErrorType::InvalidNullReturn
|
|
|
|
: ErrorType::InvalidNullReturnWithNullability;
|
2015-12-19 03:56:42 +08:00
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2014-08-13 08:26:40 +08:00
|
|
|
return;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-08-13 08:26:40 +08:00
|
|
|
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
|
|
|
"null pointer returned from function declared to never return null");
|
2014-09-09 04:17:19 +08:00
|
|
|
if (!Data->AttrLoc.isInvalid())
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Data->AttrLoc, DL_Note, ET, "%0 specified here")
|
2017-03-15 00:32:27 +08:00
|
|
|
<< (IsAttr ? "returns_nonnull attribute"
|
|
|
|
: "_Nonnull return type annotation");
|
2014-08-13 08:26:40 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 05:32:48 +08:00
|
|
|
void __ubsan::__ubsan_handle_nonnull_return_v1(NonNullReturnData *Data,
|
|
|
|
SourceLocation *LocPtr) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
2017-06-24 05:32:48 +08:00
|
|
|
handleNonNullReturn(Data, LocPtr, Opts, true);
|
2014-08-13 08:26:40 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 05:32:48 +08:00
|
|
|
void __ubsan::__ubsan_handle_nonnull_return_v1_abort(NonNullReturnData *Data,
|
|
|
|
SourceLocation *LocPtr) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
2017-06-24 05:32:48 +08:00
|
|
|
handleNonNullReturn(Data, LocPtr, Opts, true);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2014-09-09 01:22:45 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 05:32:48 +08:00
|
|
|
void __ubsan::__ubsan_handle_nullability_return_v1(NonNullReturnData *Data,
|
|
|
|
SourceLocation *LocPtr) {
|
2017-03-15 00:32:27 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
2017-06-24 05:32:48 +08:00
|
|
|
handleNonNullReturn(Data, LocPtr, Opts, false);
|
2017-03-15 00:32:27 +08:00
|
|
|
}
|
|
|
|
|
2017-06-24 05:32:48 +08:00
|
|
|
void __ubsan::__ubsan_handle_nullability_return_v1_abort(
|
|
|
|
NonNullReturnData *Data, SourceLocation *LocPtr) {
|
2017-03-15 00:32:27 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
2017-06-24 05:32:48 +08:00
|
|
|
handleNonNullReturn(Data, LocPtr, Opts, false);
|
2017-03-15 00:32:27 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleNonNullArg(NonNullArgData *Data, ReportOptions Opts,
|
|
|
|
bool IsAttr) {
|
2014-09-09 01:22:45 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2020-02-29 06:09:14 +08:00
|
|
|
ErrorType ET = IsAttr ? ErrorType::InvalidNullArgument
|
|
|
|
: ErrorType::InvalidNullArgumentWithNullability;
|
2015-12-19 03:56:42 +08:00
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2014-09-09 01:22:45 +08:00
|
|
|
return;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-09-09 01:22:45 +08:00
|
|
|
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
2017-03-15 00:32:27 +08:00
|
|
|
"null pointer passed as argument %0, which is declared to "
|
|
|
|
"never be null")
|
|
|
|
<< Data->ArgIndex;
|
2014-09-09 01:22:45 +08:00
|
|
|
if (!Data->AttrLoc.isInvalid())
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Data->AttrLoc, DL_Note, ET, "%0 specified here")
|
2017-03-15 00:32:27 +08:00
|
|
|
<< (IsAttr ? "nonnull attribute" : "_Nonnull type annotation");
|
2014-09-09 01:22:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_nonnull_arg(NonNullArgData *Data) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
2017-03-15 00:32:27 +08:00
|
|
|
handleNonNullArg(Data, Opts, true);
|
2014-09-09 01:22:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_nonnull_arg_abort(NonNullArgData *Data) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
2017-03-15 00:32:27 +08:00
|
|
|
handleNonNullArg(Data, Opts, true);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_nullability_arg(NonNullArgData *Data) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handleNonNullArg(Data, Opts, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_nullability_arg_abort(NonNullArgData *Data) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handleNonNullArg(Data, Opts, false);
|
2014-09-11 04:43:36 +08:00
|
|
|
Die();
|
2014-08-13 08:26:40 +08:00
|
|
|
}
|
2015-03-27 01:26:04 +08:00
|
|
|
|
2017-06-02 03:40:59 +08:00
|
|
|
static void handlePointerOverflowImpl(PointerOverflowData *Data,
|
|
|
|
ValueHandle Base,
|
|
|
|
ValueHandle Result,
|
|
|
|
ReportOptions Opts) {
|
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
[UBSan][clang][compiler-rt] Applying non-zero offset to nullptr is undefined behaviour
Summary:
Quote from http://eel.is/c++draft/expr.add#4:
```
4 When an expression J that has integral type is added to or subtracted
from an expression P of pointer type, the result has the type of P.
(4.1) If P evaluates to a null pointer value and J evaluates to 0,
the result is a null pointer value.
(4.2) Otherwise, if P points to an array element i of an array object x with n
elements ([dcl.array]), the expressions P + J and J + P
(where J has the value j) point to the (possibly-hypothetical) array
element i+j of x if 0≤i+j≤n and the expression P - J points to the
(possibly-hypothetical) array element i−j of x if 0≤i−j≤n.
(4.3) Otherwise, the behavior is undefined.
```
Therefore, as per the standard, applying non-zero offset to `nullptr`
(or making non-`nullptr` a `nullptr`, by subtracting pointer's integral value
from the pointer itself) is undefined behavior. (*if* `nullptr` is not defined,
i.e. e.g. `-fno-delete-null-pointer-checks` was *not* specified.)
To make things more fun, in C (6.5.6p8), applying *any* offset to null pointer
is undefined, although Clang front-end pessimizes the code by not lowering
that info, so this UB is "harmless".
Since rL369789 (D66608 `[InstCombine] icmp eq/ne (gep inbounds P, Idx..), null -> icmp eq/ne P, null`)
LLVM middle-end uses those guarantees for transformations.
If the source contains such UB's, said code may now be miscompiled.
Such miscompilations were already observed:
* https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190826/687838.html
* https://github.com/google/filament/pull/1566
Surprisingly, UBSan does not catch those issues
... until now. This diff teaches UBSan about these UB's.
`getelementpointer inbounds` is a pretty frequent instruction,
so this does have a measurable impact on performance;
I've addressed most of the obvious missing folds (and thus decreased the performance impact by ~5%),
and then re-performed some performance measurements using my [[ https://github.com/darktable-org/rawspeed | RawSpeed ]] benchmark:
(all measurements done with LLVM ToT, the sanitizer never fired.)
* no sanitization vs. existing check: average `+21.62%` slowdown
* existing check vs. check after this patch: average `22.04%` slowdown
* no sanitization vs. this patch: average `48.42%` slowdown
Reviewers: vsk, filcab, rsmith, aaron.ballman, vitalybuka, rjmccall, #sanitizers
Reviewed By: rsmith
Subscribers: kristof.beyls, nickdesaulniers, nikic, ychen, dtzWill, xbolva00, dberris, arphaman, rupprecht, reames, regehr, llvm-commits, cfe-commits
Tags: #clang, #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D67122
llvm-svn: 374293
2019-10-10 17:25:02 +08:00
|
|
|
ErrorType ET;
|
|
|
|
|
|
|
|
if (Base == 0 && Result == 0)
|
|
|
|
ET = ErrorType::NullptrWithOffset;
|
|
|
|
else if (Base == 0 && Result != 0)
|
|
|
|
ET = ErrorType::NullptrWithNonZeroOffset;
|
|
|
|
else if (Base != 0 && Result == 0)
|
|
|
|
ET = ErrorType::NullptrAfterNonZeroOffset;
|
|
|
|
else
|
|
|
|
ET = ErrorType::PointerOverflow;
|
2017-06-02 03:40:59 +08:00
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
|
|
|
return;
|
|
|
|
|
|
|
|
ScopedReport R(Opts, Loc, ET);
|
|
|
|
|
[UBSan][clang][compiler-rt] Applying non-zero offset to nullptr is undefined behaviour
Summary:
Quote from http://eel.is/c++draft/expr.add#4:
```
4 When an expression J that has integral type is added to or subtracted
from an expression P of pointer type, the result has the type of P.
(4.1) If P evaluates to a null pointer value and J evaluates to 0,
the result is a null pointer value.
(4.2) Otherwise, if P points to an array element i of an array object x with n
elements ([dcl.array]), the expressions P + J and J + P
(where J has the value j) point to the (possibly-hypothetical) array
element i+j of x if 0≤i+j≤n and the expression P - J points to the
(possibly-hypothetical) array element i−j of x if 0≤i−j≤n.
(4.3) Otherwise, the behavior is undefined.
```
Therefore, as per the standard, applying non-zero offset to `nullptr`
(or making non-`nullptr` a `nullptr`, by subtracting pointer's integral value
from the pointer itself) is undefined behavior. (*if* `nullptr` is not defined,
i.e. e.g. `-fno-delete-null-pointer-checks` was *not* specified.)
To make things more fun, in C (6.5.6p8), applying *any* offset to null pointer
is undefined, although Clang front-end pessimizes the code by not lowering
that info, so this UB is "harmless".
Since rL369789 (D66608 `[InstCombine] icmp eq/ne (gep inbounds P, Idx..), null -> icmp eq/ne P, null`)
LLVM middle-end uses those guarantees for transformations.
If the source contains such UB's, said code may now be miscompiled.
Such miscompilations were already observed:
* https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20190826/687838.html
* https://github.com/google/filament/pull/1566
Surprisingly, UBSan does not catch those issues
... until now. This diff teaches UBSan about these UB's.
`getelementpointer inbounds` is a pretty frequent instruction,
so this does have a measurable impact on performance;
I've addressed most of the obvious missing folds (and thus decreased the performance impact by ~5%),
and then re-performed some performance measurements using my [[ https://github.com/darktable-org/rawspeed | RawSpeed ]] benchmark:
(all measurements done with LLVM ToT, the sanitizer never fired.)
* no sanitization vs. existing check: average `+21.62%` slowdown
* existing check vs. check after this patch: average `22.04%` slowdown
* no sanitization vs. this patch: average `48.42%` slowdown
Reviewers: vsk, filcab, rsmith, aaron.ballman, vitalybuka, rjmccall, #sanitizers
Reviewed By: rsmith
Subscribers: kristof.beyls, nickdesaulniers, nikic, ychen, dtzWill, xbolva00, dberris, arphaman, rupprecht, reames, regehr, llvm-commits, cfe-commits
Tags: #clang, #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D67122
llvm-svn: 374293
2019-10-10 17:25:02 +08:00
|
|
|
if (ET == ErrorType::NullptrWithOffset) {
|
|
|
|
Diag(Loc, DL_Error, ET, "applying zero offset to null pointer");
|
|
|
|
} else if (ET == ErrorType::NullptrWithNonZeroOffset) {
|
|
|
|
Diag(Loc, DL_Error, ET, "applying non-zero offset %0 to null pointer")
|
|
|
|
<< Result;
|
|
|
|
} else if (ET == ErrorType::NullptrAfterNonZeroOffset) {
|
|
|
|
Diag(
|
|
|
|
Loc, DL_Error, ET,
|
|
|
|
"applying non-zero offset to non-null pointer %0 produced null pointer")
|
|
|
|
<< (void *)Base;
|
|
|
|
} else if ((sptr(Base) >= 0) == (sptr(Result) >= 0)) {
|
2017-07-14 04:55:41 +08:00
|
|
|
if (Base > Result)
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
|
|
|
"addition of unsigned offset to %0 overflowed to %1")
|
2017-07-14 04:55:41 +08:00
|
|
|
<< (void *)Base << (void *)Result;
|
|
|
|
else
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
2017-07-14 04:55:41 +08:00
|
|
|
"subtraction of unsigned offset from %0 overflowed to %1")
|
|
|
|
<< (void *)Base << (void *)Result;
|
|
|
|
} else {
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
2017-06-13 02:42:51 +08:00
|
|
|
"pointer index expression with base %0 overflowed to %1")
|
|
|
|
<< (void *)Base << (void *)Result;
|
2017-07-14 04:55:41 +08:00
|
|
|
}
|
2017-06-02 03:40:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_pointer_overflow(PointerOverflowData *Data,
|
|
|
|
ValueHandle Base,
|
|
|
|
ValueHandle Result) {
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
handlePointerOverflowImpl(Data, Base, Result, Opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_pointer_overflow_abort(PointerOverflowData *Data,
|
|
|
|
ValueHandle Base,
|
|
|
|
ValueHandle Result) {
|
|
|
|
GET_REPORT_OPTIONS(true);
|
|
|
|
handlePointerOverflowImpl(Data, Base, Result, Opts);
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2016-01-26 07:34:38 +08:00
|
|
|
static void handleCFIBadIcall(CFICheckFailData *Data, ValueHandle Function,
|
2015-09-10 10:18:02 +08:00
|
|
|
ReportOptions Opts) {
|
2018-06-26 10:15:47 +08:00
|
|
|
if (Data->CheckKind != CFITCK_ICall && Data->CheckKind != CFITCK_NVMFCall)
|
2016-01-26 07:34:38 +08:00
|
|
|
Die();
|
|
|
|
|
2015-09-10 10:18:02 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2015-12-19 03:56:42 +08:00
|
|
|
ErrorType ET = ErrorType::CFIBadType;
|
|
|
|
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2015-09-10 10:18:02 +08:00
|
|
|
return;
|
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2015-09-10 10:18:02 +08:00
|
|
|
|
2018-06-26 10:15:47 +08:00
|
|
|
const char *CheckKindStr = Data->CheckKind == CFITCK_NVMFCall
|
|
|
|
? "non-virtual pointer to member function call"
|
|
|
|
: "indirect function call";
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(Loc, DL_Error, ET,
|
2018-06-26 10:15:47 +08:00
|
|
|
"control flow integrity check for type %0 failed during %1")
|
|
|
|
<< Data->Type << CheckKindStr;
|
2015-09-10 10:18:02 +08:00
|
|
|
|
|
|
|
SymbolizedStackHolder FLoc(getSymbolizedLocation(Function));
|
|
|
|
const char *FName = FLoc.get()->info.function;
|
|
|
|
if (!FName)
|
|
|
|
FName = "(unknown)";
|
2018-06-23 01:21:17 +08:00
|
|
|
Diag(FLoc, DL_Note, ET, "%0 defined here") << FName;
|
2018-06-27 02:51:04 +08:00
|
|
|
|
|
|
|
// If the failure involved different DSOs for the check location and icall
|
|
|
|
// target, report the DSO names.
|
|
|
|
const char *DstModule = FLoc.get()->info.module;
|
|
|
|
if (!DstModule)
|
|
|
|
DstModule = "(unknown)";
|
|
|
|
|
|
|
|
const char *SrcModule = Symbolizer::GetOrInit()->GetModuleNameForPc(Opts.pc);
|
|
|
|
if (!SrcModule)
|
|
|
|
SrcModule = "(unknown)";
|
|
|
|
|
|
|
|
if (internal_strcmp(SrcModule, DstModule))
|
|
|
|
Diag(Loc, DL_Note, ET,
|
|
|
|
"check failed in %0, destination function located in %1")
|
|
|
|
<< SrcModule << DstModule;
|
2015-09-10 10:18:02 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 07:34:38 +08:00
|
|
|
namespace __ubsan {
|
2017-09-16 04:24:12 +08:00
|
|
|
|
2016-01-28 05:15:10 +08:00
|
|
|
#ifdef UBSAN_CAN_USE_CXXABI
|
2017-09-16 04:24:12 +08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
extern "C" void __ubsan_handle_cfi_bad_type_default(CFICheckFailData *Data,
|
|
|
|
ValueHandle Vtable,
|
|
|
|
bool ValidVtable,
|
|
|
|
ReportOptions Opts) {
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
WIN_WEAK_ALIAS(__ubsan_handle_cfi_bad_type, __ubsan_handle_cfi_bad_type_default)
|
|
|
|
#else
|
2016-01-26 07:34:38 +08:00
|
|
|
SANITIZER_WEAK_ATTRIBUTE
|
2017-09-16 04:24:12 +08:00
|
|
|
#endif
|
|
|
|
void __ubsan_handle_cfi_bad_type(CFICheckFailData *Data, ValueHandle Vtable,
|
|
|
|
bool ValidVtable, ReportOptions Opts);
|
|
|
|
|
2016-01-28 05:15:10 +08:00
|
|
|
#else
|
2017-09-19 05:35:49 +08:00
|
|
|
void __ubsan_handle_cfi_bad_type(CFICheckFailData *Data, ValueHandle Vtable,
|
|
|
|
bool ValidVtable, ReportOptions Opts) {
|
2016-01-28 05:15:10 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
#endif
|
2017-09-16 04:24:12 +08:00
|
|
|
|
2016-01-26 07:34:38 +08:00
|
|
|
} // namespace __ubsan
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_cfi_check_fail(CFICheckFailData *Data,
|
2016-02-04 06:19:04 +08:00
|
|
|
ValueHandle Value,
|
|
|
|
uptr ValidVtable) {
|
2015-09-10 10:18:02 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
2018-06-26 10:15:47 +08:00
|
|
|
if (Data->CheckKind == CFITCK_ICall || Data->CheckKind == CFITCK_NVMFCall)
|
2016-01-26 07:34:38 +08:00
|
|
|
handleCFIBadIcall(Data, Value, Opts);
|
|
|
|
else
|
2017-09-16 04:24:12 +08:00
|
|
|
__ubsan_handle_cfi_bad_type(Data, Value, ValidVtable, Opts);
|
2015-09-10 10:18:02 +08:00
|
|
|
}
|
|
|
|
|
2016-01-26 07:34:38 +08:00
|
|
|
void __ubsan::__ubsan_handle_cfi_check_fail_abort(CFICheckFailData *Data,
|
2016-02-04 06:19:04 +08:00
|
|
|
ValueHandle Value,
|
|
|
|
uptr ValidVtable) {
|
2015-09-10 10:18:02 +08:00
|
|
|
GET_REPORT_OPTIONS(true);
|
2018-06-26 10:15:47 +08:00
|
|
|
if (Data->CheckKind == CFITCK_ICall || Data->CheckKind == CFITCK_NVMFCall)
|
2016-01-26 07:34:38 +08:00
|
|
|
handleCFIBadIcall(Data, Value, Opts);
|
|
|
|
else
|
2017-09-16 04:24:12 +08:00
|
|
|
__ubsan_handle_cfi_bad_type(Data, Value, ValidVtable, Opts);
|
2015-09-10 10:18:02 +08:00
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2015-03-27 01:26:04 +08:00
|
|
|
#endif // CAN_SANITIZE_UB
|