Path: blob/main/contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_value.h
35233 views
//===-- ubsan_value.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// Representation of data which is passed from the compiler-generated calls into9// the ubsan runtime.10//11//===----------------------------------------------------------------------===//12#ifndef UBSAN_VALUE_H13#define UBSAN_VALUE_H1415#include "sanitizer_common/sanitizer_atomic.h"16#include "sanitizer_common/sanitizer_common.h"1718// FIXME: Move this out to a config header.19#if __SIZEOF_INT128__20__extension__ typedef __int128 s128;21__extension__ typedef unsigned __int128 u128;22#define HAVE_INT128_T 123#else24#define HAVE_INT128_T 025#endif2627namespace __ubsan {2829/// \brief Largest integer types we support.30#if HAVE_INT128_T31typedef s128 SIntMax;32typedef u128 UIntMax;33#else34typedef s64 SIntMax;35typedef u64 UIntMax;36#endif3738/// \brief Largest floating-point type we support.39typedef long double FloatMax;4041/// \brief A description of a source location. This corresponds to Clang's42/// \c PresumedLoc type.43class SourceLocation {44const char *Filename;45u32 Line;46u32 Column;4748public:49SourceLocation() : Filename(), Line(), Column() {}50SourceLocation(const char *Filename, unsigned Line, unsigned Column)51: Filename(Filename), Line(Line), Column(Column) {}5253/// \brief Determine whether the source location is known.54bool isInvalid() const { return !Filename; }5556/// \brief Atomically acquire a copy, disabling original in-place.57/// Exactly one call to acquire() returns a copy that isn't disabled.58SourceLocation acquire() {59u32 OldColumn = __sanitizer::atomic_exchange(60(__sanitizer::atomic_uint32_t *)&Column, ~u32(0),61__sanitizer::memory_order_relaxed);62return SourceLocation(Filename, Line, OldColumn);63}6465/// \brief Determine if this Location has been disabled.66/// Disabled SourceLocations are invalid to use.67bool isDisabled() {68return Column == ~u32(0);69}7071/// \brief Get the presumed filename for the source location.72const char *getFilename() const { return Filename; }73/// \brief Get the presumed line number.74unsigned getLine() const { return Line; }75/// \brief Get the column within the presumed line.76unsigned getColumn() const { return Column; }77};787980/// \brief A description of a type.81class TypeDescriptor {82/// A value from the \c Kind enumeration, specifying what flavor of type we83/// have.84u16 TypeKind;8586/// A \c Type-specific value providing information which allows us to87/// interpret the meaning of a ValueHandle of this type.88u16 TypeInfo;8990/// The name of the type follows, in a format suitable for including in91/// diagnostics.92char TypeName[1];9394public:95enum Kind {96/// An integer type. Lowest bit is 1 for a signed value, 0 for an unsigned97/// value. Remaining bits are log_2(bit width). The value representation is98/// the integer itself if it fits into a ValueHandle, and a pointer to the99/// integer otherwise.100TK_Integer = 0x0000,101/// A floating-point type. Low 16 bits are bit width. The value102/// representation is that of bitcasting the floating-point value to an103/// integer type.104TK_Float = 0x0001,105/// Any other type. The value representation is unspecified.106TK_Unknown = 0xffff107};108109const char *getTypeName() const { return TypeName; }110111Kind getKind() const {112return static_cast<Kind>(TypeKind);113}114115bool isIntegerTy() const { return getKind() == TK_Integer; }116bool isSignedIntegerTy() const {117return isIntegerTy() && (TypeInfo & 1);118}119bool isUnsignedIntegerTy() const {120return isIntegerTy() && !(TypeInfo & 1);121}122unsigned getIntegerBitWidth() const {123CHECK(isIntegerTy());124return 1 << (TypeInfo >> 1);125}126127bool isFloatTy() const { return getKind() == TK_Float; }128unsigned getFloatBitWidth() const {129CHECK(isFloatTy());130return TypeInfo;131}132};133134/// \brief An opaque handle to a value.135typedef uptr ValueHandle;136137/// Returns the class name of the given ObjC object, or null if the name138/// cannot be found.139const char *getObjCClassName(ValueHandle Pointer);140141/// \brief Representation of an operand value provided by the instrumented code.142///143/// This is a combination of a TypeDescriptor (which is emitted as constant data144/// as an operand to a handler function) and a ValueHandle (which is passed at145/// runtime when a check failure occurs).146class Value {147/// The type of the value.148const TypeDescriptor &Type;149/// The encoded value itself.150ValueHandle Val;151152/// Is \c Val a (zero-extended) integer?153bool isInlineInt() const {154CHECK(getType().isIntegerTy());155const unsigned InlineBits = sizeof(ValueHandle) * 8;156const unsigned Bits = getType().getIntegerBitWidth();157return Bits <= InlineBits;158}159160/// Is \c Val a (zero-extended) integer representation of a float?161bool isInlineFloat() const {162CHECK(getType().isFloatTy());163const unsigned InlineBits = sizeof(ValueHandle) * 8;164const unsigned Bits = getType().getFloatBitWidth();165return Bits <= InlineBits;166}167168public:169Value(const TypeDescriptor &Type, ValueHandle Val) : Type(Type), Val(Val) {}170171const TypeDescriptor &getType() const { return Type; }172173/// \brief Get this value as a signed integer.174SIntMax getSIntValue() const;175176/// \brief Get this value as an unsigned integer.177UIntMax getUIntValue() const;178179/// \brief Decode this value, which must be a positive or unsigned integer.180UIntMax getPositiveIntValue() const;181182/// Is this an integer with value -1?183bool isMinusOne() const {184return getType().isSignedIntegerTy() && getSIntValue() == -1;185}186187/// Is this a negative integer?188bool isNegative() const {189return getType().isSignedIntegerTy() && getSIntValue() < 0;190}191192/// \brief Get this value as a floating-point quantity.193FloatMax getFloatValue() const;194};195196} // namespace __ubsan197198#endif // UBSAN_VALUE_H199200201