2012-10-25 10:07:02 +08:00
|
|
|
//===-- ubsan_handlers_cxx.cc ---------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Error logging entry points for the UBSan runtime, which are only used for C++
|
|
|
|
// compilations. This file is permitted to use language features which require
|
|
|
|
// linking against a C++ ABI library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-27 01:26:04 +08:00
|
|
|
#include "ubsan_platform.h"
|
|
|
|
#if CAN_SANITIZE_UB
|
2016-01-26 07:34:38 +08:00
|
|
|
#include "ubsan_handlers.h"
|
2012-10-25 10:07:02 +08:00
|
|
|
#include "ubsan_handlers_cxx.h"
|
|
|
|
#include "ubsan_diag.h"
|
|
|
|
#include "ubsan_type_hash.h"
|
|
|
|
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2014-08-05 09:24:22 +08:00
|
|
|
#include "sanitizer_common/sanitizer_suppressions.h"
|
2012-10-25 10:07:02 +08:00
|
|
|
|
|
|
|
using namespace __sanitizer;
|
|
|
|
using namespace __ubsan;
|
|
|
|
|
|
|
|
namespace __ubsan {
|
|
|
|
extern const char *TypeCheckKinds[];
|
|
|
|
}
|
|
|
|
|
2015-12-09 08:12:57 +08:00
|
|
|
// Returns true if UBSan has printed an error report.
|
|
|
|
static bool HandleDynamicTypeCacheMiss(
|
2012-12-18 14:30:32 +08:00
|
|
|
DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash,
|
2014-08-23 05:42:04 +08:00
|
|
|
ReportOptions Opts) {
|
2012-10-25 10:07:02 +08:00
|
|
|
if (checkDynamicType((void*)Pointer, Data->TypeInfo, Hash))
|
|
|
|
// Just a cache miss. The type matches after all.
|
2015-12-09 08:12:57 +08:00
|
|
|
return false;
|
2012-10-25 10:07:02 +08:00
|
|
|
|
2014-08-05 09:24:22 +08:00
|
|
|
// Check if error report should be suppressed.
|
2015-06-19 09:52:55 +08:00
|
|
|
DynamicTypeInfo DTI = getDynamicTypeInfoFromObject((void*)Pointer);
|
2015-02-21 01:41:59 +08:00
|
|
|
if (DTI.isValid() && IsVptrCheckSuppressed(DTI.getMostDerivedTypeName()))
|
2015-12-09 08:12:57 +08:00
|
|
|
return false;
|
2014-08-05 09:24:22 +08:00
|
|
|
|
2013-01-09 11:40:03 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2015-12-19 03:56:42 +08:00
|
|
|
ErrorType ET = ErrorType::DynamicTypeMismatch;
|
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2015-12-09 08:12:57 +08:00
|
|
|
return false;
|
2013-01-09 11:40:03 +08:00
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2014-07-30 09:49:19 +08:00
|
|
|
|
2013-01-09 11:40:03 +08:00
|
|
|
Diag(Loc, DL_Error,
|
2012-12-18 14:30:32 +08:00
|
|
|
"%0 address %1 which does not point to an object of type %2")
|
2012-10-25 10:07:02 +08:00
|
|
|
<< TypeCheckKinds[Data->TypeCheckKind] << (void*)Pointer << Data->Type;
|
2012-12-18 14:30:32 +08:00
|
|
|
|
|
|
|
// If possible, say what type it actually points to.
|
2016-06-03 02:36:12 +08:00
|
|
|
if (!DTI.isValid()) {
|
|
|
|
if (DTI.getOffset() < -VptrMaxOffsetToTop || DTI.getOffset() > VptrMaxOffsetToTop) {
|
|
|
|
Diag(Pointer, DL_Note, "object has a possibly invalid vptr: abs(offset to top) too big")
|
|
|
|
<< TypeName(DTI.getMostDerivedTypeName())
|
|
|
|
<< Range(Pointer, Pointer + sizeof(uptr), "possibly invalid vptr");
|
|
|
|
} else {
|
|
|
|
Diag(Pointer, DL_Note, "object has invalid vptr")
|
|
|
|
<< TypeName(DTI.getMostDerivedTypeName())
|
|
|
|
<< Range(Pointer, Pointer + sizeof(uptr), "invalid vptr");
|
|
|
|
}
|
|
|
|
} else if (!DTI.getOffset())
|
2012-12-18 14:30:32 +08:00
|
|
|
Diag(Pointer, DL_Note, "object is of type %0")
|
2015-07-09 06:10:34 +08:00
|
|
|
<< TypeName(DTI.getMostDerivedTypeName())
|
2014-08-05 09:24:22 +08:00
|
|
|
<< Range(Pointer, Pointer + sizeof(uptr), "vptr for %0");
|
2012-12-18 14:30:32 +08:00
|
|
|
else
|
2012-12-18 17:30:21 +08:00
|
|
|
// FIXME: Find the type at the specified offset, and include that
|
|
|
|
// in the note.
|
2012-12-18 14:30:32 +08:00
|
|
|
Diag(Pointer - DTI.getOffset(), DL_Note,
|
|
|
|
"object is base class subobject at offset %0 within object of type %1")
|
2015-07-09 06:10:34 +08:00
|
|
|
<< DTI.getOffset() << TypeName(DTI.getMostDerivedTypeName())
|
|
|
|
<< TypeName(DTI.getSubobjectTypeName())
|
2014-08-05 09:24:22 +08:00
|
|
|
<< Range(Pointer, Pointer + sizeof(uptr),
|
|
|
|
"vptr for %2 base class of %1");
|
2015-12-09 08:12:57 +08:00
|
|
|
return true;
|
2012-12-03 03:47:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __ubsan::__ubsan_handle_dynamic_type_cache_miss(
|
2012-12-18 14:30:32 +08:00
|
|
|
DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
|
2014-08-23 05:42:04 +08:00
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts);
|
2012-12-03 03:47:29 +08:00
|
|
|
}
|
|
|
|
void __ubsan::__ubsan_handle_dynamic_type_cache_miss_abort(
|
2012-12-18 14:30:32 +08:00
|
|
|
DynamicTypeCacheMissData *Data, ValueHandle Pointer, ValueHandle Hash) {
|
2015-12-09 08:12:57 +08:00
|
|
|
// Note: -fsanitize=vptr is always recoverable.
|
|
|
|
GET_REPORT_OPTIONS(false);
|
|
|
|
if (HandleDynamicTypeCacheMiss(Data, Pointer, Hash, Opts))
|
|
|
|
Die();
|
2012-10-25 10:07:02 +08:00
|
|
|
}
|
2015-03-27 01:26:04 +08:00
|
|
|
|
2016-01-26 07:34:38 +08:00
|
|
|
namespace __ubsan {
|
|
|
|
void HandleCFIBadType(CFICheckFailData *Data, ValueHandle Vtable,
|
2016-02-04 06:19:04 +08:00
|
|
|
bool ValidVtable, ReportOptions Opts) {
|
2015-06-19 09:52:55 +08:00
|
|
|
SourceLocation Loc = Data->Loc.acquire();
|
2015-12-19 03:56:42 +08:00
|
|
|
ErrorType ET = ErrorType::CFIBadType;
|
2015-12-09 08:12:57 +08:00
|
|
|
|
2015-12-19 03:56:42 +08:00
|
|
|
if (ignoreReport(Loc, Opts, ET))
|
2015-12-09 08:12:57 +08:00
|
|
|
return;
|
2015-12-19 03:56:42 +08:00
|
|
|
|
|
|
|
ScopedReport R(Opts, Loc, ET);
|
2016-02-04 06:19:04 +08:00
|
|
|
DynamicTypeInfo DTI = ValidVtable
|
|
|
|
? getDynamicTypeInfoFromVtable((void *)Vtable)
|
|
|
|
: DynamicTypeInfo(0, 0, 0);
|
2016-01-26 07:34:38 +08:00
|
|
|
|
|
|
|
const char *CheckKindStr;
|
|
|
|
switch (Data->CheckKind) {
|
|
|
|
case CFITCK_VCall:
|
|
|
|
CheckKindStr = "virtual call";
|
|
|
|
break;
|
|
|
|
case CFITCK_NVCall:
|
|
|
|
CheckKindStr = "non-virtual call";
|
|
|
|
break;
|
|
|
|
case CFITCK_DerivedCast:
|
|
|
|
CheckKindStr = "base-to-derived cast";
|
|
|
|
break;
|
|
|
|
case CFITCK_UnrelatedCast:
|
|
|
|
CheckKindStr = "cast to unrelated type";
|
|
|
|
break;
|
|
|
|
case CFITCK_ICall:
|
|
|
|
Die();
|
|
|
|
}
|
2015-06-19 09:52:55 +08:00
|
|
|
|
|
|
|
Diag(Loc, DL_Error, "control flow integrity check for type %0 failed during "
|
|
|
|
"%1 (vtable address %2)")
|
2016-01-26 07:34:38 +08:00
|
|
|
<< Data->Type << CheckKindStr << (void *)Vtable;
|
2015-06-19 09:52:55 +08:00
|
|
|
|
|
|
|
// If possible, say what type it actually points to.
|
2016-02-04 06:19:04 +08:00
|
|
|
if (!DTI.isValid()) {
|
|
|
|
const char *module = Symbolizer::GetOrInit()->GetModuleNameForPc(Vtable);
|
|
|
|
if (module)
|
|
|
|
Diag(Vtable, DL_Note, "invalid vtable in module %0") << module;
|
|
|
|
else
|
|
|
|
Diag(Vtable, DL_Note, "invalid vtable");
|
|
|
|
} else {
|
2015-06-19 09:52:55 +08:00
|
|
|
Diag(Vtable, DL_Note, "vtable is of type %0")
|
2015-07-09 06:10:34 +08:00
|
|
|
<< TypeName(DTI.getMostDerivedTypeName());
|
2016-02-04 06:19:04 +08:00
|
|
|
}
|
2015-06-19 09:52:55 +08:00
|
|
|
}
|
2016-01-26 07:34:38 +08:00
|
|
|
} // namespace __ubsan
|
2015-06-19 09:52:55 +08:00
|
|
|
|
2016-01-26 07:34:38 +08:00
|
|
|
#endif // CAN_SANITIZE_UB
|