diff options
author | Alexey Samsonov <vonosmas@gmail.com> | 2015-12-08 23:29:33 +0000 |
---|---|---|
committer | Alexey Samsonov <vonosmas@gmail.com> | 2015-12-08 23:29:33 +0000 |
commit | fdca75f617fd27f6b866c3f7a873dbfd735b4232 (patch) | |
tree | ddae6d93f3cf335aa84604adb7d6c80d2d038098 | |
parent | 1e005922d5a4b54d03a2a80fc0c72071601bf449 (diff) |
[UBSan] Always calculate ErrorType (kind of UB) before printing a report.
Currently, this is an NFC. However, knowing out the kind of error
report before we bring up all the reporting machinery (implemented in
ScopedReport class) is important once we teach UBSan runtime
suppressions.
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@255074 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/ubsan/ubsan_diag.h | 4 | ||||
-rw-r--r-- | lib/ubsan/ubsan_handlers.cc | 103 |
2 files changed, 65 insertions, 42 deletions
diff --git a/lib/ubsan/ubsan_diag.h b/lib/ubsan/ubsan_diag.h index 2aa62eb74..0bad9a064 100644 --- a/lib/ubsan/ubsan_diag.h +++ b/lib/ubsan/ubsan_diag.h @@ -238,9 +238,7 @@ class ScopedReport { ErrorType Type; public: - ScopedReport(ReportOptions Opts, Location SummaryLoc, - ErrorType Type = ErrorType::GenericUB); - void setErrorType(ErrorType T) { Type = T; } + ScopedReport(ReportOptions Opts, Location SummaryLoc, ErrorType Type); ~ScopedReport(); }; diff --git a/lib/ubsan/ubsan_handlers.cc b/lib/ubsan/ubsan_handlers.cc index ec6b54e43..69bde0dca 100644 --- a/lib/ubsan/ubsan_handlers.cc +++ b/lib/ubsan/ubsan_handlers.cc @@ -51,24 +51,36 @@ static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer, Loc = FallbackLoc; } - ScopedReport R(Opts, Loc); + ErrorType ET; + if (!Pointer) + ET = ErrorType::NullPointerUse; + else if (Data->Alignment && (Pointer & (Data->Alignment - 1))) + ET = ErrorType::MisalignedPointerUse; + else + ET = ErrorType::InsufficientObjectSize; + + ScopedReport R(Opts, Loc, ET); - if (!Pointer) { - R.setErrorType(ErrorType::NullPointerUse); + switch (ET) { + case ErrorType::NullPointerUse: Diag(Loc, DL_Error, "%0 null pointer of type %1") - << TypeCheckKinds[Data->TypeCheckKind] << Data->Type; - } else if (Data->Alignment && (Pointer & (Data->Alignment - 1))) { - R.setErrorType(ErrorType::MisalignedPointerUse); + << TypeCheckKinds[Data->TypeCheckKind] << Data->Type; + break; + case ErrorType::MisalignedPointerUse: Diag(Loc, DL_Error, "%0 misaligned address %1 for type %3, " "which requires %2 byte alignment") - << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer - << Data->Alignment << Data->Type; - } else { - R.setErrorType(ErrorType::InsufficientObjectSize); + << TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer + << Data->Alignment << Data->Type; + break; + case ErrorType::InsufficientObjectSize: Diag(Loc, DL_Error, "%0 address %1 with insufficient space " "for an object of type %2") - << TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type; + << TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer << Data->Type; + break; + default: + UNREACHABLE("unexpected error type!"); } + if (Pointer) Diag(Pointer, DL_Note, "pointer points here"); } @@ -157,19 +169,27 @@ static void handleDivremOverflowImpl(OverflowData *Data, ValueHandle LHS, if (ignoreReport(Loc, Opts)) return; - ScopedReport R(Opts, Loc); - Value LHSVal(Data->Type, LHS); Value RHSVal(Data->Type, RHS); - if (RHSVal.isMinusOne()) { - R.setErrorType(ErrorType::SignedIntegerOverflow); - Diag(Loc, DL_Error, - "division of %0 by -1 cannot be represented in type %1") - << LHSVal << Data->Type; - } else { - R.setErrorType(Data->Type.isIntegerTy() ? ErrorType::IntegerDivideByZero - : ErrorType::FloatDivideByZero); + + ErrorType ET; + if (RHSVal.isMinusOne()) + ET = ErrorType::SignedIntegerOverflow; + else if (Data->Type.isIntegerTy()) + ET = ErrorType::IntegerDivideByZero; + else + ET = ErrorType::FloatDivideByZero; + + ScopedReport R(Opts, Loc, ET); + + switch (ET) { + case ErrorType::SignedIntegerOverflow: + Diag(Loc, DL_Error, "division of %0 by -1 cannot be represented in type %1") + << LHSVal << Data->Type; + break; + default: Diag(Loc, DL_Error, "division by zero"); + break; } } @@ -193,26 +213,31 @@ static void handleShiftOutOfBoundsImpl(ShiftOutOfBoundsData *Data, if (ignoreReport(Loc, Opts)) return; - ScopedReport R(Opts, Loc); - Value LHSVal(Data->LHSType, LHS); Value RHSVal(Data->RHSType, RHS); - if (RHSVal.isNegative()) { - R.setErrorType(ErrorType::InvalidShiftExponent); - Diag(Loc, DL_Error, "shift exponent %0 is negative") << RHSVal; - } else if (RHSVal.getPositiveIntValue() >= - Data->LHSType.getIntegerBitWidth()) { - R.setErrorType(ErrorType::InvalidShiftExponent); - Diag(Loc, DL_Error, "shift exponent %0 is too large for %1-bit type %2") - << RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType; - } else if (LHSVal.isNegative()) { - R.setErrorType(ErrorType::InvalidShiftBase); - Diag(Loc, DL_Error, "left shift of negative value %0") << LHSVal; + + ErrorType ET; + if (RHSVal.isNegative() || + RHSVal.getPositiveIntValue() >= Data->LHSType.getIntegerBitWidth()) + ET = ErrorType::InvalidShiftExponent; + else + ET = ErrorType::InvalidShiftBase; + + ScopedReport R(Opts, Loc, ET); + + if (ET == ErrorType::InvalidShiftExponent) { + if (RHSVal.isNegative()) + Diag(Loc, DL_Error, "shift exponent %0 is negative") << RHSVal; + else + Diag(Loc, DL_Error, "shift exponent %0 is too large for %1-bit type %2") + << RHSVal << Data->LHSType.getIntegerBitWidth() << Data->LHSType; } else { - R.setErrorType(ErrorType::InvalidShiftBase); - Diag(Loc, DL_Error, - "left shift of %0 by %1 places cannot be represented in type %2") - << LHSVal << RHSVal << Data->LHSType; + if (LHSVal.isNegative()) + Diag(Loc, DL_Error, "left shift of negative value %0") << LHSVal; + else + Diag(Loc, DL_Error, + "left shift of %0 by %1 places cannot be represented in type %2") + << LHSVal << RHSVal << Data->LHSType; } } @@ -481,7 +506,7 @@ static void handleCFIBadIcall(CFIBadIcallData *Data, ValueHandle Function, if (ignoreReport(Loc, Opts)) return; - ScopedReport R(Opts, Loc); + ScopedReport R(Opts, Loc, ErrorType::CFIBadType); Diag(Loc, DL_Error, "control flow integrity check for type %0 failed during " "indirect function call") |