mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-27 04:06:07 +00:00
[ubsan] Save a ptrtoint when emitting alignment checks
The alignment check emits a ptrtoint instruction which can be reused in the call to the diagnostic handler. llvm-svn: 314749
This commit is contained in:
parent
675cf03f6e
commit
8a7153312b
@ -656,6 +656,7 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint64_t AlignVal = 0;
|
uint64_t AlignVal = 0;
|
||||||
|
llvm::Value *PtrAsInt = nullptr;
|
||||||
|
|
||||||
if (SanOpts.has(SanitizerKind::Alignment) &&
|
if (SanOpts.has(SanitizerKind::Alignment) &&
|
||||||
!SkippedChecks.has(SanitizerKind::Alignment)) {
|
!SkippedChecks.has(SanitizerKind::Alignment)) {
|
||||||
@ -666,9 +667,9 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
|
|||||||
// The glvalue must be suitably aligned.
|
// The glvalue must be suitably aligned.
|
||||||
if (AlignVal > 1 &&
|
if (AlignVal > 1 &&
|
||||||
(!PtrToAlloca || PtrToAlloca->getAlignment() < AlignVal)) {
|
(!PtrToAlloca || PtrToAlloca->getAlignment() < AlignVal)) {
|
||||||
llvm::Value *Align =
|
PtrAsInt = Builder.CreatePtrToInt(Ptr, IntPtrTy);
|
||||||
Builder.CreateAnd(Builder.CreatePtrToInt(Ptr, IntPtrTy),
|
llvm::Value *Align = Builder.CreateAnd(
|
||||||
llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
|
PtrAsInt, llvm::ConstantInt::get(IntPtrTy, AlignVal - 1));
|
||||||
llvm::Value *Aligned =
|
llvm::Value *Aligned =
|
||||||
Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
|
Builder.CreateICmpEQ(Align, llvm::ConstantInt::get(IntPtrTy, 0));
|
||||||
Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment));
|
Checks.push_back(std::make_pair(Aligned, SanitizerKind::Alignment));
|
||||||
@ -683,7 +684,8 @@ void CodeGenFunction::EmitTypeCheck(TypeCheckKind TCK, SourceLocation Loc,
|
|||||||
EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(Ty),
|
EmitCheckSourceLocation(Loc), EmitCheckTypeDescriptor(Ty),
|
||||||
llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2_64(AlignVal) : 1),
|
llvm::ConstantInt::get(Int8Ty, AlignVal ? llvm::Log2_64(AlignVal) : 1),
|
||||||
llvm::ConstantInt::get(Int8Ty, TCK)};
|
llvm::ConstantInt::get(Int8Ty, TCK)};
|
||||||
EmitCheck(Checks, SanitizerHandler::TypeMismatch, StaticData, Ptr);
|
EmitCheck(Checks, SanitizerHandler::TypeMismatch, StaticData,
|
||||||
|
PtrAsInt ? PtrAsInt : Ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If possible, check that the vptr indicates that there is a subobject of
|
// If possible, check that the vptr indicates that there is a subobject of
|
||||||
@ -2599,6 +2601,9 @@ llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
|
|||||||
llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
|
llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
|
||||||
llvm::Type *TargetTy = IntPtrTy;
|
llvm::Type *TargetTy = IntPtrTy;
|
||||||
|
|
||||||
|
if (V->getType() == TargetTy)
|
||||||
|
return V;
|
||||||
|
|
||||||
// Floating-point types which fit into intptr_t are bitcast to integers
|
// Floating-point types which fit into intptr_t are bitcast to integers
|
||||||
// and then passed directly (after zero-extension, if necessary).
|
// and then passed directly (after zero-extension, if necessary).
|
||||||
if (V->getType()->isFloatingPointTy()) {
|
if (V->getType()->isFloatingPointTy()) {
|
||||||
|
@ -59,8 +59,7 @@ int bar(int *a) {
|
|||||||
// CHECK-COMMON-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
|
// CHECK-COMMON-NEXT: %[[MISALIGN:.*]] = and i64 %[[PTRINT]], 3
|
||||||
// CHECK-COMMON-NEXT: icmp eq i64 %[[MISALIGN]], 0
|
// CHECK-COMMON-NEXT: icmp eq i64 %[[MISALIGN]], 0
|
||||||
|
|
||||||
// CHECK-UBSAN: %[[ARG:.*]] = ptrtoint
|
// CHECK-UBSAN: call void @__ubsan_handle_type_mismatch_v1(i8* bitcast ({{.*}} @[[LINE_200]] to i8*), i64 %[[PTRINT]])
|
||||||
// CHECK-UBSAN-NEXT: call void @__ubsan_handle_type_mismatch_v1(i8* bitcast ({{.*}} @[[LINE_200]] to i8*), i64 %[[ARG]])
|
|
||||||
|
|
||||||
// CHECK-TRAP: call void @llvm.trap() [[NR_NUW]]
|
// CHECK-TRAP: call void @llvm.trap() [[NR_NUW]]
|
||||||
// CHECK-TRAP-NEXT: unreachable
|
// CHECK-TRAP-NEXT: unreachable
|
||||||
|
Loading…
x
Reference in New Issue
Block a user