Skip to content

Commit 6a275de

Browse files
authored
Revert "[UBSan] Improve error message when a misalignment is due to t… (llvm#166197)
…arget de…" This reverts commit 47c54d5.
1 parent a85ecfa commit 6a275de

File tree

6 files changed

+12
-86
lines changed

6 files changed

+12
-86
lines changed

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
#include "ConstantEmitter.h"
1919
#include "TargetInfo.h"
2020
#include "clang/Basic/CodeGenOptions.h"
21-
#include "clang/Basic/Sanitizers.h"
22-
#include "clang/Basic/SourceLocation.h"
23-
#include "clang/Basic/SourceManager.h"
2421
#include "clang/CodeGen/CGFunctionInfo.h"
2522
#include "llvm/IR/Intrinsics.h"
2623

@@ -1752,27 +1749,17 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
17521749
allocator->isReservedGlobalPlacementOperator())
17531750
result = Builder.CreateLaunderInvariantGroup(result);
17541751

1755-
// Check the default alignment of the type and why. Users may incorrectly
1756-
// return misaligned memory from a replaced operator new without knowing
1757-
// about default alignment.
1758-
TypeCheckKind checkKind = CodeGenFunction::TCK_ConstructorCall;
1759-
const TargetInfo &TI = getContext().getTargetInfo();
1760-
unsigned DefaultTargetAlignment = TI.getNewAlign() / TI.getCharWidth();
1761-
if (SanOpts.has(SanitizerKind::Alignment) &&
1762-
(DefaultTargetAlignment >
1763-
CGM.getContext().getTypeAlignInChars(allocType).getQuantity()))
1764-
checkKind = CodeGenFunction::TCK_ConstructorCallMinimumAlign;
1765-
17661752
// Emit sanitizer checks for pointer value now, so that in the case of an
17671753
// array it was checked only once and not at each constructor call. We may
17681754
// have already checked that the pointer is non-null.
17691755
// FIXME: If we have an array cookie and a potentially-throwing allocator,
17701756
// we'll null check the wrong pointer here.
17711757
SanitizerSet SkippedChecks;
17721758
SkippedChecks.set(SanitizerKind::Null, nullCheck);
1773-
EmitTypeCheck(
1774-
checkKind, E->getAllocatedTypeSourceInfo()->getTypeLoc().getBeginLoc(),
1775-
result, allocType, result.getAlignment(), SkippedChecks, numElements);
1759+
EmitTypeCheck(CodeGenFunction::TCK_ConstructorCall,
1760+
E->getAllocatedTypeSourceInfo()->getTypeLoc().getBeginLoc(),
1761+
result, allocType, result.getAlignment(), SkippedChecks,
1762+
numElements);
17761763

17771764
EmitNewInitializer(*this, E, allocType, elementTy, result, numElements,
17781765
allocSizeWithoutCookie);

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3296,10 +3296,7 @@ class CodeGenFunction : public CodeGenTypeCache {
32963296
TCK_NonnullAssign,
32973297
/// Checking the operand of a dynamic_cast or a typeid expression. Must be
32983298
/// null or an object within its lifetime.
3299-
TCK_DynamicOperation,
3300-
/// Checking the 'this' poiner for a constructor call, including that the
3301-
/// alignment is greater or equal to the targets minimum alignment
3302-
TCK_ConstructorCallMinimumAlign
3299+
TCK_DynamicOperation
33033300
};
33043301

33053302
/// Determine whether the pointer type check \p TCK permits null pointers.

compiler-rt/lib/ubsan/ubsan_checks.inc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ UBSAN_CHECK(NullptrAfterNonZeroOffset, "nullptr-after-nonzero-offset",
2828
UBSAN_CHECK(PointerOverflow, "pointer-overflow", "pointer-overflow")
2929
UBSAN_CHECK(MisalignedPointerUse, "misaligned-pointer-use", "alignment")
3030
UBSAN_CHECK(AlignmentAssumption, "alignment-assumption", "alignment")
31-
UBSAN_CHECK(MinumumAssumedAlignment, "minimum-assumed-alignment", "alignment")
3231
UBSAN_CHECK(InsufficientObjectSize, "insufficient-object-size", "object-size")
3332
UBSAN_CHECK(SignedIntegerOverflow, "signed-integer-overflow",
3433
"signed-integer-overflow")

compiler-rt/lib/ubsan/ubsan_handlers.cpp

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,14 @@ enum TypeCheckKind {
7373
TCK_NonnullAssign,
7474
/// Checking the operand of a dynamic_cast or a typeid expression. Must be
7575
/// null or an object within its lifetime.
76-
TCK_DynamicOperation,
77-
/// Checking the 'this' poiner for a constructor call, including that the
78-
/// alignment is greater or equal to the targets minimum alignment
79-
TCK_ConstructorCallMinimumAlign
76+
TCK_DynamicOperation
8077
};
8178

8279
extern const char *const TypeCheckKinds[] = {
83-
"load of",
84-
"store to",
85-
"reference binding to",
86-
"member access within",
87-
"member call on",
88-
"constructor call on",
89-
"downcast of",
90-
"downcast of",
91-
"upcast of",
92-
"cast to virtual base of",
93-
"_Nonnull binding to",
94-
"dynamic operation on",
95-
"constructor call with pointer from operator new on"};
80+
"load of", "store to", "reference binding to", "member access within",
81+
"member call on", "constructor call on", "downcast of", "downcast of",
82+
"upcast of", "cast to virtual base of", "_Nonnull binding to",
83+
"dynamic operation on"};
9684
}
9785

9886
static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer,
@@ -106,9 +94,7 @@ static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer,
10694
? ErrorType::NullPointerUseWithNullability
10795
: ErrorType::NullPointerUse;
10896
else if (Pointer & (Alignment - 1))
109-
ET = (Data->TypeCheckKind == TCK_ConstructorCallMinimumAlign)
110-
? ErrorType::MinumumAssumedAlignment
111-
: ErrorType::MisalignedPointerUse;
97+
ET = ErrorType::MisalignedPointerUse;
11298
else
11399
ET = ErrorType::InsufficientObjectSize;
114100

@@ -131,13 +117,6 @@ static void handleTypeMismatchImpl(TypeMismatchData *Data, ValueHandle Pointer,
131117
Diag(Loc, DL_Error, ET, "%0 null pointer of type %1")
132118
<< TypeCheckKinds[Data->TypeCheckKind] << Data->Type;
133119
break;
134-
case ErrorType::MinumumAssumedAlignment:
135-
Diag(Loc, DL_Error, ET,
136-
"%0 misaligned address %1 for type %2, "
137-
"which requires target minimum assumed %3 byte alignment")
138-
<< TypeCheckKinds[Data->TypeCheckKind] << (void *)Pointer << Data->Type
139-
<< Alignment;
140-
break;
141120
case ErrorType::MisalignedPointerUse:
142121
Diag(Loc, DL_Error, ET, "%0 misaligned address %1 for type %3, "
143122
"which requires %2 byte alignment")

compiler-rt/test/ubsan/TestCases/TypeCheck/minimum-alignment.cpp

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

compiler-rt/test/ubsan/TestCases/TypeCheck/misaligned.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int main(int, char **argv) {
101101
return s->f() && 0;
102102

103103
case 'n':
104-
// CHECK-NEW: misaligned.cpp:[[@LINE+4]]{{(:21)?}}: runtime error: constructor call with pointer from operator new on misaligned address [[PTR:0x[0-9a-f]*]] for type 'S', which requires target minimum assumed 4 byte alignment
104+
// CHECK-NEW: misaligned.cpp:[[@LINE+4]]{{(:21)?}}: runtime error: constructor call on misaligned address [[PTR:0x[0-9a-f]*]] for type 'S', which requires 4 byte alignment
105105
// CHECK-NEW-NEXT: [[PTR]]: note: pointer points here
106106
// CHECK-NEW-NEXT: {{^ 00 00 00 01 02 03 04 05}}
107107
// CHECK-NEW-NEXT: {{^ \^}}

0 commit comments

Comments
 (0)