@@ -5750,7 +5750,6 @@ void IRGenSILFunction::visitBeginAccessInst(BeginAccessInst *access) {
5750
5750
5751
5751
case SILAccessEnforcement::Static:
5752
5752
case SILAccessEnforcement::Unsafe:
5753
- case SILAccessEnforcement::Signed:
5754
5753
// nothing to do
5755
5754
setLoweredAddress (access, addr);
5756
5755
return ;
@@ -5773,6 +5772,41 @@ void IRGenSILFunction::visitBeginAccessInst(BeginAccessInst *access) {
5773
5772
setLoweredDynamicallyEnforcedAddress (access, addr, scratch);
5774
5773
return ;
5775
5774
}
5775
+ case SILAccessEnforcement::Signed: {
5776
+ auto &ti = getTypeInfo (access->getType ());
5777
+ auto *sea = cast<StructElementAddrInst>(access->getOperand ());
5778
+ auto *Int64PtrTy = llvm::Type::getInt64PtrTy (IGM.getLLVMContext ());
5779
+ auto *Int64PtrPtrTy = Int64PtrTy->getPointerTo ();
5780
+ if (access->getAccessKind () == SILAccessKind::Read) {
5781
+ // When we see a signed read access, generate code to:
5782
+ // authenticate the signed pointer, and store the authenticated value to a
5783
+ // shadow stack location. Set the lowered address of the access to this
5784
+ // stack location.
5785
+ auto pointerAuthQual = sea->getField ()->getPointerAuthQualifier ();
5786
+ auto *pointerToSignedFptr = getLoweredAddress (sea).getAddress ();
5787
+ auto *pointerToIntPtr =
5788
+ Builder.CreateBitCast (pointerToSignedFptr, Int64PtrPtrTy);
5789
+ auto *signedFptr = Builder.CreateLoad (pointerToIntPtr, Int64PtrTy,
5790
+ IGM.getPointerAlignment ());
5791
+ auto *unsignedFptr = emitPointerAuthAuth (
5792
+ *this , signedFptr, PointerAuthInfo::emit (IGM, pointerAuthQual));
5793
+ auto temp = ti.allocateStack (*this , access->getType (), " ptrauth.temp" );
5794
+ auto *tempAddressToIntPtr =
5795
+ Builder.CreateBitCast (temp.getAddressPointer (), Int64PtrPtrTy);
5796
+ Builder.CreateStore (unsignedFptr, tempAddressToIntPtr,
5797
+ IGM.getPointerAlignment ());
5798
+ setLoweredAddress (access, temp.getAddress ());
5799
+ return ;
5800
+ }
5801
+ if (access->getAccessKind () == SILAccessKind::Modify) {
5802
+ // When we see a signed modify access, create a shadow stack location and
5803
+ // set the lowered address of the access to this stack location.
5804
+ auto temp = ti.allocateStack (*this , access->getType (), " ptrauth.temp" );
5805
+ setLoweredAddress (access, temp.getAddress ());
5806
+ return ;
5807
+ }
5808
+ llvm_unreachable (" Incompatible access kind with begin_access [signed]" );
5809
+ }
5776
5810
}
5777
5811
llvm_unreachable (" bad access enforcement" );
5778
5812
}
@@ -5837,7 +5871,6 @@ void IRGenSILFunction::visitEndAccessInst(EndAccessInst *i) {
5837
5871
5838
5872
case SILAccessEnforcement::Static:
5839
5873
case SILAccessEnforcement::Unsafe:
5840
- case SILAccessEnforcement::Signed:
5841
5874
// nothing to do
5842
5875
return ;
5843
5876
@@ -5854,6 +5887,35 @@ void IRGenSILFunction::visitEndAccessInst(EndAccessInst *i) {
5854
5887
Builder.CreateLifetimeEnd (scratch);
5855
5888
return ;
5856
5889
}
5890
+
5891
+ case SILAccessEnforcement::Signed: {
5892
+ if (access->getAccessKind () != SILAccessKind::Modify) {
5893
+ // nothing to do.
5894
+ return ;
5895
+ }
5896
+ // When we see a signed modify access, get the lowered address of the
5897
+ // access which is the shadow stack slot, sign the value and write back to
5898
+ // the struct field.
5899
+ auto *Int64PtrTy = llvm::Type::getInt64PtrTy (IGM.getLLVMContext ());
5900
+ auto *Int64PtrPtrTy = Int64PtrTy->getPointerTo ();
5901
+ auto pointerAuthQual = cast<StructElementAddrInst>(access->getOperand ())
5902
+ ->getField ()
5903
+ ->getPointerAuthQualifier ();
5904
+ auto tempAddress = getLoweredAddress (access);
5905
+ auto *tempAddressToIntPtr =
5906
+ Builder.CreateBitCast (tempAddress.getAddress (), Int64PtrPtrTy);
5907
+ auto *tempAddressValue = Builder.CreateLoad (tempAddressToIntPtr, Int64PtrTy,
5908
+ IGM.getPointerAlignment ());
5909
+ auto *signedFptr = emitPointerAuthSign (
5910
+ *this , tempAddressValue, PointerAuthInfo::emit (IGM, pointerAuthQual));
5911
+
5912
+ auto *pointerToSignedFptr =
5913
+ getLoweredAddress (access->getOperand ()).getAddress ();
5914
+ auto *pointerToIntPtr =
5915
+ Builder.CreateBitCast (pointerToSignedFptr, Int64PtrPtrTy);
5916
+ Builder.CreateStore (signedFptr, pointerToIntPtr, IGM.getPointerAlignment ());
5917
+ return ;
5918
+ }
5857
5919
}
5858
5920
llvm_unreachable (" bad access enforcement" );
5859
5921
}
0 commit comments