Skip to content

Commit 4e32b7b

Browse files
committed
Ensure we pass the right alignment to the error, and fixing failing tests
1 parent e9d3243 commit 4e32b7b

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

clang/lib/CodeGen/CGExprCXX.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "CodeGenFunction.h"
1818
#include "ConstantEmitter.h"
1919
#include "TargetInfo.h"
20+
#include "clang/AST/CharUnits.h"
2021
#include "clang/Basic/CodeGenOptions.h"
2122
#include "clang/Basic/Sanitizers.h"
2223
#include "clang/Basic/SourceLocation.h"
@@ -1756,12 +1757,15 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
17561757
// return misaligned memory from a replaced operator new without knowing
17571758
// about default alignment.
17581759
TypeCheckKind checkKind = CodeGenFunction::TCK_ConstructorCall;
1760+
CharUnits checkAlignment = result.getAlignment();
17591761
const TargetInfo &TI = getContext().getTargetInfo();
17601762
unsigned DefaultTargetAlignment = TI.getNewAlign() / TI.getCharWidth();
17611763
if (SanOpts.has(SanitizerKind::Alignment) &&
17621764
(DefaultTargetAlignment >
1763-
CGM.getContext().getTypeAlignInChars(allocType).getQuantity()))
1765+
CGM.getContext().getTypeAlignInChars(allocType).getQuantity())){
17641766
checkKind = CodeGenFunction::TCK_ConstructorCallMinimumAlign;
1767+
checkAlignment = CharUnits::fromQuantity(DefaultTargetAlignment);
1768+
}
17651769

17661770
// Emit sanitizer checks for pointer value now, so that in the case of an
17671771
// array it was checked only once and not at each constructor call. We may
@@ -1772,7 +1776,7 @@ llvm::Value *CodeGenFunction::EmitCXXNewExpr(const CXXNewExpr *E) {
17721776
SkippedChecks.set(SanitizerKind::Null, nullCheck);
17731777
EmitTypeCheck(
17741778
checkKind, E->getAllocatedTypeSourceInfo()->getTypeLoc().getBeginLoc(),
1775-
result, allocType, result.getAlignment(), SkippedChecks, numElements);
1779+
result, allocType, checkAlignment, SkippedChecks, numElements);
17761780

17771781
EmitNewInitializer(*this, E, allocType, elementTy, result, numElements,
17781782
allocSizeWithoutCookie);

clang/test/CodeGenCXX/ubsan-new-checks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ S3 *func_07() {
7575
// CHECK-LABEL: define {{.*}} @_Z7func_07v
7676
// CHECK: and i64 %{{.*}}, 31, !nosanitize
7777
// CHECK: icmp eq i64 %{{.*}}, 0, !nosanitize
78-
// CHECK: and i64 %{{.*}}, 3, !nosanitize
78+
// CHECK: and i64 %{{.*}}, 15, !nosanitize
7979
// CHECK: icmp eq i64 %{{.*}}, 0, !nosanitize
8080
// CHECK: ret ptr
8181
return new S3;
@@ -85,7 +85,7 @@ S3 *func_08() {
8585
// CHECK-LABEL: define {{.*}} @_Z7func_08v
8686
// CHECK: and i64 %{{.*}}, 31, !nosanitize
8787
// CHECK: icmp eq i64 %{{.*}}, 0, !nosanitize
88-
// CHECK: and i64 %{{.*}}, 3, !nosanitize
88+
// CHECK: and i64 %{{.*}}, 15, !nosanitize
8989
// CHECK: icmp eq i64 %{{.*}}, 0, !nosanitize
9090
// CHECK: ret ptr
9191
return new S3[10];

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// RUN: %clangxx %gmlt -fsanitize=alignment %s -o %t
1+
// RUN: %clangxx %gmlt -std=c++17 -m64 -fsanitize=alignment %s -o %t
22
// RUN: %run %t 2>&1 | FileCheck %s
33

4-
// UNSUPPORTED: i386
4+
// UNSUPPORTED: i386, i686
55
// UNSUPPORTED: armv7l
66

77
// These sanitizers already overload the new operator so won't compile this test
@@ -10,6 +10,7 @@
1010

1111
#include <cassert>
1212
#include <cstdlib>
13+
#include <cstddef>
1314

1415
void *operator new(std::size_t count) {
1516
constexpr const size_t offset = 8;

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{{( with pointer from operator new)?}} on misaligned address [[PTR:0x[0-9a-f]*]] for type 'S', which requires {{(4|(target minimum assumed (8|(16))))}} 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)