Path: blob/main/contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_value.cpp
35233 views
//===-- ubsan_value.cpp ---------------------------------------------------===//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 a runtime value, as marshaled from the generated code to9// the ubsan runtime.10//11//===----------------------------------------------------------------------===//1213#include "ubsan_platform.h"14#if CAN_SANITIZE_UB15#include "ubsan_value.h"16#include "sanitizer_common/sanitizer_common.h"17#include "sanitizer_common/sanitizer_libc.h"18#include "sanitizer_common/sanitizer_mutex.h"1920#if SANITIZER_APPLE21#include <dlfcn.h>22#endif2324using namespace __ubsan;2526typedef const char *(*ObjCGetClassNameTy)(void *);2728const char *__ubsan::getObjCClassName(ValueHandle Pointer) {29#if SANITIZER_APPLE30// We need to query the ObjC runtime for some information, but do not want31// to introduce a static dependency from the ubsan runtime onto ObjC. Try to32// grab a handle to the ObjC runtime used by the process.33static bool AttemptedDlopen = false;34static void *ObjCHandle = nullptr;35static void *ObjCObjectGetClassName = nullptr;3637// Prevent threads from racing to dlopen().38static __sanitizer::StaticSpinMutex Lock;39{40__sanitizer::SpinMutexLock Guard(&Lock);4142if (!AttemptedDlopen) {43ObjCHandle = dlopen(44"/usr/lib/libobjc.A.dylib",45RTLD_LAZY // Only bind symbols when used.46| RTLD_LOCAL // Only make symbols available via the handle.47| RTLD_NOLOAD // Do not load the dylib, just grab a handle if the48// image is already loaded.49| RTLD_FIRST // Only search the image pointed-to by the handle.50);51AttemptedDlopen = true;52if (!ObjCHandle)53return nullptr;54ObjCObjectGetClassName = dlsym(ObjCHandle, "object_getClassName");55}56}5758if (!ObjCObjectGetClassName)59return nullptr;6061return ObjCGetClassNameTy(ObjCObjectGetClassName)((void *)Pointer);62#else63return nullptr;64#endif65}6667SIntMax Value::getSIntValue() const {68CHECK(getType().isSignedIntegerTy());69if (isInlineInt()) {70// Val was zero-extended to ValueHandle. Sign-extend from original width71// to SIntMax.72const unsigned ExtraBits =73sizeof(SIntMax) * 8 - getType().getIntegerBitWidth();74return SIntMax(UIntMax(Val) << ExtraBits) >> ExtraBits;75}76if (getType().getIntegerBitWidth() == 64)77return *reinterpret_cast<s64*>(Val);78#if HAVE_INT128_T79if (getType().getIntegerBitWidth() == 128)80return *reinterpret_cast<s128*>(Val);81#else82if (getType().getIntegerBitWidth() == 128)83UNREACHABLE("libclang_rt.ubsan was built without __int128 support");84#endif85UNREACHABLE("unexpected bit width");86}8788UIntMax Value::getUIntValue() const {89CHECK(getType().isUnsignedIntegerTy());90if (isInlineInt())91return Val;92if (getType().getIntegerBitWidth() == 64)93return *reinterpret_cast<u64*>(Val);94#if HAVE_INT128_T95if (getType().getIntegerBitWidth() == 128)96return *reinterpret_cast<u128*>(Val);97#else98if (getType().getIntegerBitWidth() == 128)99UNREACHABLE("libclang_rt.ubsan was built without __int128 support");100#endif101UNREACHABLE("unexpected bit width");102}103104UIntMax Value::getPositiveIntValue() const {105if (getType().isUnsignedIntegerTy())106return getUIntValue();107SIntMax Val = getSIntValue();108CHECK(Val >= 0);109return Val;110}111112/// Get the floating-point value of this object, extended to a long double.113/// These are always passed by address (our calling convention doesn't allow114/// them to be passed in floating-point registers, so this has little cost).115FloatMax Value::getFloatValue() const {116CHECK(getType().isFloatTy());117if (isInlineFloat()) {118switch (getType().getFloatBitWidth()) {119#if 0120// FIXME: OpenCL / NEON 'half' type. LLVM can't lower the conversion121// from '__fp16' to 'long double'.122case 16: {123__fp16 Value;124internal_memcpy(&Value, &Val, 4);125return Value;126}127#endif128case 32: {129float Value;130#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__131// For big endian the float value is in the last 4 bytes.132// On some targets we may only have 4 bytes so we count backwards from133// the end of Val to account for both the 32-bit and 64-bit cases.134internal_memcpy(&Value, ((const char*)(&Val + 1)) - 4, 4);135#else136internal_memcpy(&Value, &Val, 4);137#endif138return Value;139}140case 64: {141double Value;142internal_memcpy(&Value, &Val, 8);143return Value;144}145}146} else {147switch (getType().getFloatBitWidth()) {148case 64: return *reinterpret_cast<double*>(Val);149case 80: return *reinterpret_cast<long double*>(Val);150case 96: return *reinterpret_cast<long double*>(Val);151case 128: return *reinterpret_cast<long double*>(Val);152}153}154UNREACHABLE("unexpected floating point bit width");155}156157#endif // CAN_SANITIZE_UB158159160