@@ -3456,7 +3456,8 @@ llvm::Constant *CodeGenFunction::EmitCheckTypeDescriptor(QualType T) {
3456
3456
return GV;
3457
3457
}
3458
3458
3459
- llvm::Value *CodeGenFunction::EmitCheckValue (llvm::Value *V) {
3459
+ llvm::Value *CodeGenFunction::EmitCheckValue (llvm::Value *V,
3460
+ bool &MayReadFromPtrToInt) {
3460
3461
llvm::Type *TargetTy = IntPtrTy;
3461
3462
3462
3463
if (V->getType () == TargetTy)
@@ -3482,6 +3483,7 @@ llvm::Value *CodeGenFunction::EmitCheckValue(llvm::Value *V) {
3482
3483
Builder.CreateStore (V, Ptr);
3483
3484
V = Ptr.getPointer ();
3484
3485
}
3486
+ MayReadFromPtrToInt = true ;
3485
3487
return Builder.CreatePtrToInt (V, TargetTy);
3486
3488
}
3487
3489
@@ -3587,7 +3589,8 @@ static void emitCheckHandlerCall(CodeGenFunction &CGF,
3587
3589
ArrayRef<llvm::Value *> FnArgs,
3588
3590
SanitizerHandler CheckHandler,
3589
3591
CheckRecoverableKind RecoverKind, bool IsFatal,
3590
- llvm::BasicBlock *ContBB, bool NoMerge) {
3592
+ llvm::BasicBlock *ContBB, bool NoMerge,
3593
+ bool MayReadFromPtrToInt) {
3591
3594
assert (IsFatal || RecoverKind != CheckRecoverableKind::Unrecoverable);
3592
3595
std::optional<ApplyDebugLocation> DL;
3593
3596
if (!CGF.Builder .getCurrentDebugLocation ()) {
@@ -3620,9 +3623,14 @@ static void emitCheckHandlerCall(CodeGenFunction &CGF,
3620
3623
if (CGF.CGM .getCodeGenOpts ().OptimizationLevel > 0 && MayReturn) {
3621
3624
// __ubsan_handle_dynamic_type_cache_miss reads the vtable, which is also
3622
3625
// accessible by the current module.
3623
- if (CheckHandler != SanitizerHandler::DynamicTypeCacheMiss)
3624
- B.addMemoryAttr (llvm::MemoryEffects::argMemOnly (llvm::ModRefInfo::Ref) |
3625
- llvm::MemoryEffects::inaccessibleMemOnly ());
3626
+ if (CheckHandler != SanitizerHandler::DynamicTypeCacheMiss) {
3627
+ llvm::MemoryEffects ME =
3628
+ llvm::MemoryEffects::argMemOnly (llvm::ModRefInfo::Ref) |
3629
+ llvm::MemoryEffects::inaccessibleMemOnly ();
3630
+ if (MayReadFromPtrToInt)
3631
+ ME |= llvm::MemoryEffects::readOnly ();
3632
+ B.addMemoryAttr (ME);
3633
+ }
3626
3634
// If the handler does not return, it must interact with the environment in
3627
3635
// an observable way.
3628
3636
B.addAttribute (llvm::Attribute::MustProgress);
@@ -3723,6 +3731,7 @@ void CodeGenFunction::EmitCheck(
3723
3731
// representing operand values.
3724
3732
SmallVector<llvm::Value *, 4 > Args;
3725
3733
SmallVector<llvm::Type *, 4 > ArgTypes;
3734
+ bool MayReadFromPtrToInt = false ;
3726
3735
if (!CGM.getCodeGenOpts ().SanitizeMinimalRuntime ) {
3727
3736
Args.reserve (DynamicArgs.size () + 1 );
3728
3737
ArgTypes.reserve (DynamicArgs.size () + 1 );
@@ -3742,7 +3751,7 @@ void CodeGenFunction::EmitCheck(
3742
3751
}
3743
3752
3744
3753
for (size_t i = 0 , n = DynamicArgs.size (); i != n; ++i) {
3745
- Args.push_back (EmitCheckValue (DynamicArgs[i]));
3754
+ Args.push_back (EmitCheckValue (DynamicArgs[i], MayReadFromPtrToInt ));
3746
3755
ArgTypes.push_back (IntPtrTy);
3747
3756
}
3748
3757
}
@@ -3754,7 +3763,8 @@ void CodeGenFunction::EmitCheck(
3754
3763
// Simple case: we need to generate a single handler call, either
3755
3764
// fatal, or non-fatal.
3756
3765
emitCheckHandlerCall (*this , FnType, Args, CheckHandler, RecoverKind,
3757
- (FatalCond != nullptr ), Cont, NoMerge);
3766
+ (FatalCond != nullptr ), Cont, NoMerge,
3767
+ MayReadFromPtrToInt);
3758
3768
} else {
3759
3769
// Emit two handler calls: first one for set of unrecoverable checks,
3760
3770
// another one for recoverable.
@@ -3764,10 +3774,10 @@ void CodeGenFunction::EmitCheck(
3764
3774
Builder.CreateCondBr (FatalCond, NonFatalHandlerBB, FatalHandlerBB);
3765
3775
EmitBlock (FatalHandlerBB);
3766
3776
emitCheckHandlerCall (*this , FnType, Args, CheckHandler, RecoverKind, true ,
3767
- NonFatalHandlerBB, NoMerge);
3777
+ NonFatalHandlerBB, NoMerge, MayReadFromPtrToInt );
3768
3778
EmitBlock (NonFatalHandlerBB);
3769
3779
emitCheckHandlerCall (*this , FnType, Args, CheckHandler, RecoverKind, false ,
3770
- Cont, NoMerge);
3780
+ Cont, NoMerge, MayReadFromPtrToInt );
3771
3781
}
3772
3782
3773
3783
EmitBlock (Cont);
0 commit comments