Path: blob/main/contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_handlers.h
35233 views
//===-- ubsan_handlers.h ----------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Entry points to the runtime library for Clang's undefined behavior sanitizer.9//10//===----------------------------------------------------------------------===//11#ifndef UBSAN_HANDLERS_H12#define UBSAN_HANDLERS_H1314#include "ubsan_value.h"1516namespace __ubsan {1718struct TypeMismatchData {19SourceLocation Loc;20const TypeDescriptor &Type;21unsigned char LogAlignment;22unsigned char TypeCheckKind;23};2425#define UNRECOVERABLE(checkname, ...) \26extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \27void __ubsan_handle_ ## checkname( __VA_ARGS__ );2829#define RECOVERABLE(checkname, ...) \30extern "C" SANITIZER_INTERFACE_ATTRIBUTE \31void __ubsan_handle_ ## checkname( __VA_ARGS__ ); \32extern "C" SANITIZER_INTERFACE_ATTRIBUTE NORETURN \33void __ubsan_handle_ ## checkname ## _abort( __VA_ARGS__ );3435/// \brief Handle a runtime type check failure, caused by either a misaligned36/// pointer, a null pointer, or a pointer to insufficient storage for the37/// type.38RECOVERABLE(type_mismatch_v1, TypeMismatchData *Data, ValueHandle Pointer)3940struct AlignmentAssumptionData {41SourceLocation Loc;42SourceLocation AssumptionLoc;43const TypeDescriptor &Type;44};4546/// \brief Handle a runtime alignment assumption check failure,47/// caused by a misaligned pointer.48RECOVERABLE(alignment_assumption, AlignmentAssumptionData *Data,49ValueHandle Pointer, ValueHandle Alignment, ValueHandle Offset)5051struct OverflowData {52SourceLocation Loc;53const TypeDescriptor &Type;54};5556/// \brief Handle an integer addition overflow.57RECOVERABLE(add_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)5859/// \brief Handle an integer subtraction overflow.60RECOVERABLE(sub_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)6162/// \brief Handle an integer multiplication overflow.63RECOVERABLE(mul_overflow, OverflowData *Data, ValueHandle LHS, ValueHandle RHS)6465/// \brief Handle a signed integer overflow for a unary negate operator.66RECOVERABLE(negate_overflow, OverflowData *Data, ValueHandle OldVal)6768/// \brief Handle an INT_MIN/-1 overflow or division by zero.69RECOVERABLE(divrem_overflow, OverflowData *Data,70ValueHandle LHS, ValueHandle RHS)7172struct ShiftOutOfBoundsData {73SourceLocation Loc;74const TypeDescriptor &LHSType;75const TypeDescriptor &RHSType;76};7778/// \brief Handle a shift where the RHS is out of bounds or a left shift where79/// the LHS is negative or overflows.80RECOVERABLE(shift_out_of_bounds, ShiftOutOfBoundsData *Data,81ValueHandle LHS, ValueHandle RHS)8283struct OutOfBoundsData {84SourceLocation Loc;85const TypeDescriptor &ArrayType;86const TypeDescriptor &IndexType;87};8889/// \brief Handle an array index out of bounds error.90RECOVERABLE(out_of_bounds, OutOfBoundsData *Data, ValueHandle Index)9192struct UnreachableData {93SourceLocation Loc;94};9596/// \brief Handle a __builtin_unreachable which is reached.97UNRECOVERABLE(builtin_unreachable, UnreachableData *Data)98/// \brief Handle reaching the end of a value-returning function.99UNRECOVERABLE(missing_return, UnreachableData *Data)100101struct VLABoundData {102SourceLocation Loc;103const TypeDescriptor &Type;104};105106/// \brief Handle a VLA with a non-positive bound.107RECOVERABLE(vla_bound_not_positive, VLABoundData *Data, ValueHandle Bound)108109// Keeping this around for binary compatibility with (sanitized) programs110// compiled with older compilers.111struct FloatCastOverflowData {112const TypeDescriptor &FromType;113const TypeDescriptor &ToType;114};115116struct FloatCastOverflowDataV2 {117SourceLocation Loc;118const TypeDescriptor &FromType;119const TypeDescriptor &ToType;120};121122/// Handle overflow in a conversion to or from a floating-point type.123/// void *Data is one of FloatCastOverflowData* or FloatCastOverflowDataV2*124RECOVERABLE(float_cast_overflow, void *Data, ValueHandle From)125126struct InvalidValueData {127SourceLocation Loc;128const TypeDescriptor &Type;129};130131/// \brief Handle a load of an invalid value for the type.132RECOVERABLE(load_invalid_value, InvalidValueData *Data, ValueHandle Val)133134/// Known implicit conversion check kinds.135/// Keep in sync with the enum of the same name in CGExprScalar.cpp136enum ImplicitConversionCheckKind : unsigned char {137ICCK_IntegerTruncation = 0, // Legacy, was only used by clang 7.138ICCK_UnsignedIntegerTruncation = 1,139ICCK_SignedIntegerTruncation = 2,140ICCK_IntegerSignChange = 3,141ICCK_SignedIntegerTruncationOrSignChange = 4,142};143144struct ImplicitConversionData {145SourceLocation Loc;146const TypeDescriptor &FromType;147const TypeDescriptor &ToType;148/* ImplicitConversionCheckKind */ unsigned char Kind;149unsigned int BitfieldBits;150};151152/// \brief Implict conversion that changed the value.153RECOVERABLE(implicit_conversion, ImplicitConversionData *Data, ValueHandle Src,154ValueHandle Dst)155156/// Known builtin check kinds.157/// Keep in sync with the enum of the same name in CodeGenFunction.h158enum BuiltinCheckKind : unsigned char {159BCK_CTZPassedZero,160BCK_CLZPassedZero,161};162163struct InvalidBuiltinData {164SourceLocation Loc;165unsigned char Kind;166};167168/// Handle a builtin called in an invalid way.169RECOVERABLE(invalid_builtin, InvalidBuiltinData *Data)170171struct InvalidObjCCast {172SourceLocation Loc;173const TypeDescriptor &ExpectedType;174};175176/// Handle an invalid ObjC cast.177RECOVERABLE(invalid_objc_cast, InvalidObjCCast *Data, ValueHandle Pointer)178179struct NonNullReturnData {180SourceLocation AttrLoc;181};182183/// \brief Handle returning null from function with the returns_nonnull184/// attribute, or a return type annotated with _Nonnull.185RECOVERABLE(nonnull_return_v1, NonNullReturnData *Data, SourceLocation *Loc)186RECOVERABLE(nullability_return_v1, NonNullReturnData *Data, SourceLocation *Loc)187188struct NonNullArgData {189SourceLocation Loc;190SourceLocation AttrLoc;191int ArgIndex;192};193194/// \brief Handle passing null pointer to a function parameter with the nonnull195/// attribute, or a _Nonnull type annotation.196RECOVERABLE(nonnull_arg, NonNullArgData *Data)197RECOVERABLE(nullability_arg, NonNullArgData *Data)198199struct PointerOverflowData {200SourceLocation Loc;201};202203RECOVERABLE(pointer_overflow, PointerOverflowData *Data, ValueHandle Base,204ValueHandle Result)205206/// \brief Known CFI check kinds.207/// Keep in sync with the enum of the same name in CodeGenFunction.h208enum CFITypeCheckKind : unsigned char {209CFITCK_VCall,210CFITCK_NVCall,211CFITCK_DerivedCast,212CFITCK_UnrelatedCast,213CFITCK_ICall,214CFITCK_NVMFCall,215CFITCK_VMFCall,216};217218struct CFICheckFailData {219CFITypeCheckKind CheckKind;220SourceLocation Loc;221const TypeDescriptor &Type;222};223224/// \brief Handle control flow integrity failures.225RECOVERABLE(cfi_check_fail, CFICheckFailData *Data, ValueHandle Function,226uptr VtableIsValid)227228struct ReportOptions;229230extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __ubsan_handle_cfi_bad_type(231CFICheckFailData *Data, ValueHandle Vtable, bool ValidVtable,232ReportOptions Opts);233234struct FunctionTypeMismatchData {235SourceLocation Loc;236const TypeDescriptor &Type;237};238239extern "C" SANITIZER_INTERFACE_ATTRIBUTE void240__ubsan_handle_function_type_mismatch(FunctionTypeMismatchData *Data,241ValueHandle Val);242extern "C" SANITIZER_INTERFACE_ATTRIBUTE void243__ubsan_handle_function_type_mismatch_abort(FunctionTypeMismatchData *Data,244ValueHandle Val);245}246247#endif // UBSAN_HANDLERS_H248249250