Skip to content

Commit 508bc2e

Browse files
authored
Revert "[ubsan] Display correct runtime messages for negative _BitInt (#93612)"
This reverts commit 49001d5.
1 parent 1ba2768 commit 508bc2e

File tree

5 files changed

+13
-305
lines changed

5 files changed

+13
-305
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
#include "llvm/IR/MatrixBuilder.h"
4242
#include "llvm/Passes/OptimizationLevel.h"
4343
#include "llvm/Support/ConvertUTF.h"
44-
#include "llvm/Support/Endian.h"
4544
#include "llvm/Support/MathExtras.h"
4645
#include "llvm/Support/Path.h"
4746
#include "llvm/Support/SaveAndRestore.h"
@@ -65,22 +64,6 @@ static llvm::cl::opt<bool> ClSanitizeGuardChecks(
6564
"ubsan-guard-checks", llvm::cl::Optional,
6665
llvm::cl::desc("Guard UBSAN checks with `llvm.allow.ubsan.check()`."));
6766

68-
//===--------------------------------------------------------------------===//
69-
// Defines for metadata
70-
//===--------------------------------------------------------------------===//
71-
72-
// Those values are crucial to be the SAME as in ubsan runtime library.
73-
enum VariableTypeDescriptorKind : uint16_t {
74-
/// An integer type.
75-
TK_Integer = 0x0000,
76-
/// A floating-point type.
77-
TK_Float = 0x0001,
78-
/// An _BitInt(N) type.
79-
TK_BitInt = 0x0002,
80-
/// Any other type. The value representation is unspecified.
81-
TK_Unknown = 0xffff
82-
};
83-
8467
//===--------------------------------------------------------------------===//
8568
// Miscellaneous Helper Methods
8669
//===--------------------------------------------------------------------===//
@@ -3315,40 +3298,22 @@ LValue CodeGenFunction::EmitPredefinedLValue(const PredefinedExpr *E) {
33153298
/// { i16 TypeKind, i16 TypeInfo }
33163299
/// \endcode
33173300
///
3318-
/// followed by an array of i8 containing the type name with extra information
3319-
/// for BitInt. TypeKind is TK_Integer(0) for an integer, TK_Float(1) for a
3320-
/// floating point value, TK_BitInt(2) for BitInt and TK_Unknown(0xFFFF) for
3321-
/// anything else.
3301+
/// followed by an array of i8 containing the type name. TypeKind is 0 for an
3302+
/// integer, 1 for a floating point value, and -1 for anything else.
33223303
llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
33233304
// Only emit each type's descriptor once.
33243305
if (llvm::Constant *C = CGM.getTypeDescriptorFromMap(T))
33253306
return C;
33263307

3327-
uint16_t TypeKind = TK_Unknown;
3308+
uint16_t TypeKind = -1;
33283309
uint16_t TypeInfo = 0;
3329-
bool IsBitInt = false;
33303310

33313311
if (T->isIntegerType()) {
3332-
TypeKind = TK_Integer;
3312+
TypeKind = 0;
33333313
TypeInfo = (llvm::Log2_32(getContext().getTypeSize(T)) << 1) |
33343314
(T->isSignedIntegerType() ? 1 : 0);
3335-
// Follow suggestion from https://github.com/llvm/llvm-project/issues/64100
3336-
// So we can write the exact amount of bits in TypeName after '\0'
3337-
// making it <diagnostic-like type name>.'\0'.<32-bit width>.
3338-
if (T->isSignedIntegerType() && T->getAs<BitIntType>()) {
3339-
// Do a sanity checks as we are using 32-bit type to store bit length.
3340-
assert((getContext().getTypeSize(T) > 0) &&
3341-
" non positive amount of bits in __BitInt type");
3342-
assert((getContext().getTypeSize(T) <= 0xFFFFFFFF) &&
3343-
" too many bits in __BitInt type");
3344-
3345-
// Redefine TypeKind with the actual __BitInt type if we have signed
3346-
// BitInt.
3347-
TypeKind = TK_BitInt;
3348-
IsBitInt = true;
3349-
}
33503315
} else if (T->isFloatingType()) {
3351-
TypeKind = TK_Float;
3316+
TypeKind = 1;
33523317
TypeInfo = getContext().getTypeSize(T);
33533318
}
33543319

@@ -3359,20 +3324,6 @@ llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
33593324
DiagnosticsEngine::ak_qualtype, (intptr_t)T.getAsOpaquePtr(), StringRef(),
33603325
StringRef(), std::nullopt, Buffer, std::nullopt);
33613326

3362-
if (IsBitInt) {
3363-
// The Structure is: 0 to end the string, 32 bit unsigned integer in target
3364-
// endianness, zero.
3365-
char S[6] = {'\0', '\0', '\0', '\0', '\0', '\0'};
3366-
const auto *EIT = T->castAs<BitIntType>();
3367-
uint32_t Bits = EIT->getNumBits();
3368-
llvm::support::endian::write32(S + 1, Bits,
3369-
getTarget().isBigEndian()
3370-
? llvm::endianness::big
3371-
: llvm::endianness::little);
3372-
StringRef str = StringRef(S, sizeof(S) / sizeof(decltype(S[0])));
3373-
Buffer.append(str);
3374-
}
3375-
33763327
llvm::Constant *Components[] = {
33773328
Builder.getInt16(TypeKind), Builder.getInt16(TypeInfo),
33783329
llvm::ConstantDataArray::getString(getLLVMContext(), Buffer)

compiler-rt/lib/ubsan/ubsan_value.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,18 @@ const char *__ubsan::getObjCClassName(ValueHandle Pointer) {
6767

6868
SIntMax Value::getSIntValue() const {
6969
CHECK(getType().isSignedIntegerTy());
70-
// Val was zero-extended to ValueHandle. Sign-extend from original width
71-
// to SIntMax.
72-
const unsigned ExtraBits =
73-
sizeof(SIntMax) * 8 - getType().getIntegerBitCount();
7470
if (isInlineInt()) {
71+
// Val was zero-extended to ValueHandle. Sign-extend from original width
72+
// to SIntMax.
73+
const unsigned ExtraBits =
74+
sizeof(SIntMax) * 8 - getType().getIntegerBitWidth();
7575
return SIntMax(UIntMax(Val) << ExtraBits) >> ExtraBits;
7676
}
77-
if (getType().getIntegerBitWidth() == 64) {
78-
return SIntMax(UIntMax(*reinterpret_cast<s64 *>(Val)) << ExtraBits) >>
79-
ExtraBits;
80-
}
77+
if (getType().getIntegerBitWidth() == 64)
78+
return *reinterpret_cast<s64*>(Val);
8179
#if HAVE_INT128_T
8280
if (getType().getIntegerBitWidth() == 128)
83-
return SIntMax(UIntMax(*reinterpret_cast<s128 *>(Val)) << ExtraBits) >>
84-
ExtraBits;
81+
return *reinterpret_cast<s128*>(Val);
8582
#else
8683
if (getType().getIntegerBitWidth() == 128)
8784
UNREACHABLE("libclang_rt.ubsan was built without __int128 support");

compiler-rt/lib/ubsan/ubsan_value.h

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,6 @@ class TypeDescriptor {
103103
/// representation is that of bitcasting the floating-point value to an
104104
/// integer type.
105105
TK_Float = 0x0001,
106-
/// An _BitInt(N) type. Lowest bit is 1 for a signed value, 0 for an
107-
/// unsigned value. Remaining bits are log_2(bit_width). The value
108-
/// representation is the integer itself if it fits into a ValueHandle, and
109-
/// a pointer to the integer otherwise. TypeName contains the true width
110-
/// of the type for the signed _BitInt(N) type stored after zero bit after
111-
/// TypeName as 32-bit unsigned integer.
112-
TK_BitInt = 0x0002,
113106
/// Any other type. The value representation is unspecified.
114107
TK_Unknown = 0xffff
115108
};
@@ -120,15 +113,10 @@ class TypeDescriptor {
120113
return static_cast<Kind>(TypeKind);
121114
}
122115

123-
bool isIntegerTy() const {
124-
return getKind() == TK_Integer || getKind() == TK_BitInt;
125-
}
126-
bool isBitIntTy() const { return getKind() == TK_BitInt; }
127-
116+
bool isIntegerTy() const { return getKind() == TK_Integer; }
128117
bool isSignedIntegerTy() const {
129118
return isIntegerTy() && (TypeInfo & 1);
130119
}
131-
bool isSignedBitIntTy() const { return isBitIntTy() && (TypeInfo & 1); }
132120
bool isUnsignedIntegerTy() const {
133121
return isIntegerTy() && !(TypeInfo & 1);
134122
}
@@ -137,26 +125,6 @@ class TypeDescriptor {
137125
return 1 << (TypeInfo >> 1);
138126
}
139127

140-
const char *getBitIntBitCountPointer() const {
141-
CHECK(isBitIntTy());
142-
CHECK(isSignedBitIntTy());
143-
// Scan Name for zero and return the next address
144-
const char *p = getTypeName();
145-
while (*p != '\0') {
146-
++p;
147-
}
148-
// Return the next address
149-
return p + 1;
150-
}
151-
152-
unsigned getIntegerBitCount() const {
153-
CHECK(isIntegerTy());
154-
if (isSignedBitIntTy())
155-
return *reinterpret_cast<const u32 *>(getBitIntBitCountPointer());
156-
else
157-
return getIntegerBitWidth();
158-
}
159-
160128
bool isFloatTy() const { return getKind() == TK_Float; }
161129
unsigned getFloatBitWidth() const {
162130
CHECK(isFloatTy());

compiler-rt/test/ubsan/TestCases/Integer/bit-int-pass.c

Lines changed: 0 additions & 39 deletions
This file was deleted.

compiler-rt/test/ubsan/TestCases/Integer/bit-int.c

Lines changed: 0 additions & 169 deletions
This file was deleted.

0 commit comments

Comments
 (0)