@@ -188,16 +188,14 @@ static llvm::Constant *buildBlockDescriptor(CodeGenModule &CGM,
188188 // Optional copy/dispose helpers.
189189 bool hasInternalHelper = false ;
190190 if (blockInfo.NeedsCopyDispose ) {
191- auto &schema =
192- CGM.getCodeGenOpts ().PointerAuth .BlockHelperFunctionPointers ;
193-
191+ auto &Schema = CGM.getCodeGenOpts ().PointerAuth .BlockHelperFunctionPointers ;
194192 // copy_func_helper_decl
195193 llvm::Constant *copyHelper = buildCopyHelper (CGM, blockInfo);
196- elements.addSignedPointer (copyHelper, schema , GlobalDecl (), QualType ());
194+ elements.addSignedPointer (copyHelper, Schema , GlobalDecl (), QualType ());
197195
198196 // destroy_func_decl
199197 llvm::Constant *disposeHelper = buildDisposeHelper (CGM, blockInfo);
200- elements.addSignedPointer (disposeHelper, schema , GlobalDecl (), QualType ());
198+ elements.addSignedPointer (disposeHelper, Schema , GlobalDecl (), QualType ());
201199
202200 if (cast<llvm::Function>(copyHelper->stripPointerCasts ())
203201 ->hasInternalLinkage () ||
@@ -570,9 +568,8 @@ static void computeBlockInfo(CodeGenModule &CGM, CodeGenFunction *CGF,
570568 llvm::StructType::get (CGM.getLLVMContext (), elementTypes, true );
571569 info.CanBeGlobal = true ;
572570 return ;
573- }
574- else if (C.getLangOpts ().ObjC &&
575- CGM.getLangOpts ().getGC () == LangOptions::NonGC)
571+ } else if (C.getLangOpts ().ObjC &&
572+ CGM.getLangOpts ().getGC () == LangOptions::NonGC)
576573 info.HasCapturedVariableLayout = true ;
577574
578575 if (block->doesNotEscape ())
@@ -786,7 +783,7 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) {
786783
787784llvm::Value *CodeGenFunction::EmitBlockLiteral (const CGBlockInfo &blockInfo) {
788785 bool IsOpenCL = CGM.getContext ().getLangOpts ().OpenCL ;
789- auto GenVoidPtrTy =
786+ llvm::PointerType * GenVoidPtrTy =
790787 IsOpenCL ? CGM.getOpenCLRuntime ().getGenericVoidPointerType () : VoidPtrTy;
791788 LangAS GenVoidPtrAddr = IsOpenCL ? LangAS::opencl_generic : LangAS::Default;
792789 auto GenVoidPtrSize = CharUnits::fromQuantity (
@@ -820,9 +817,6 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
820817 : CGM.getNSConcreteStackBlock ();
821818 isa = blockISA;
822819
823- // Build the block descriptor.
824- descriptor = buildBlockDescriptor (CGM, blockInfo);
825-
826820 // Compute the initial on-stack block flags.
827821 if (!CGM.getCodeGenOpts ().DisableBlockSignatureString )
828822 flags = BLOCK_HAS_SIGNATURE;
@@ -836,6 +830,9 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
836830 flags |= BLOCK_USE_STRET;
837831 if (blockInfo.NoEscape )
838832 flags |= BLOCK_IS_NOESCAPE | BLOCK_IS_GLOBAL;
833+
834+ // Build the block descriptor.
835+ descriptor = buildBlockDescriptor (CGM, blockInfo);
839836 }
840837
841838 auto projectField = [&](unsigned index, const Twine &name) -> Address {
@@ -888,21 +885,20 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
888885 }
889886
890887 if (!IsOpenCL) {
891- llvm::Value *blockFnPtr = llvm::ConstantExpr::getBitCast (InvokeFn, VoidPtrTy);
892- auto blockFnPtrAddr = projectField (index, " block.invoke" );
893- if (auto &schema =
894- CGM.getCodeGenOpts ().PointerAuth .BlockInvocationFunctionPointers ) {
895- QualType type = blockInfo.getBlockExpr ()->getType ()
896- ->castAs <BlockPointerType>()->getPointeeType ();
897- auto authInfo = EmitPointerAuthInfo (
898- schema, blockFnPtrAddr.emitRawPointer (*this ), GlobalDecl (), type);
899- blockFnPtr = EmitPointerAuthSign (authInfo, blockFnPtr);
900- }
901- Builder.CreateStore (blockFnPtr, blockFnPtrAddr);
902- offset += getPointerSize ();
903- index++;
888+ llvm::Value *blockFnPtr =
889+ llvm::ConstantExpr::getBitCast (InvokeFn, VoidPtrTy);
890+ QualType type = blockInfo.getBlockExpr ()
891+ ->getType ()
892+ ->castAs <BlockPointerType>()
893+ ->getPointeeType ();
894+ addSignedHeaderField (
895+ blockFnPtr,
896+ CGM.getCodeGenOpts ().PointerAuth .BlockInvocationFunctionPointers ,
897+ GlobalDecl (), type, getPointerSize (), " block.invoke" );
904898
905- addHeaderField (descriptor, getPointerSize (), " block.descriptor" );
899+ addSignedHeaderField (
900+ descriptor, CGM.getCodeGenOpts ().PointerAuth .BlockDescriptorPointers ,
901+ GlobalDecl (), type, getPointerSize (), " block.descriptor" );
906902 } else if (auto *Helper =
907903 CGM.getTargetCodeGenInfo ().getTargetOpenCLBlockHelper ()) {
908904 addHeaderField (blockFn, GenVoidPtrSize, " block.invoke" );
@@ -1234,11 +1230,12 @@ RValue CodeGenFunction::EmitBlockCallExpr(const CallExpr *E,
12341230 // Prepare the callee.
12351231 CGPointerAuthInfo PointerAuth;
12361232 if (auto &AuthSchema =
1237- CGM.getCodeGenOpts ().PointerAuth .BlockInvocationFunctionPointers ) {
1233+ CGM.getCodeGenOpts ().PointerAuth .BlockInvocationFunctionPointers ) {
12381234 assert (FuncPtr != nullptr && " Missing function pointer for AuthInfo" );
1239- PointerAuth = EmitPointerAuthInfo (AuthSchema, FuncPtr,
1240- GlobalDecl (), FnType);
1235+ PointerAuth =
1236+ EmitPointerAuthInfo (AuthSchema, FuncPtr, GlobalDecl (), FnType);
12411237 }
1238+
12421239 CGCallee Callee (CGCalleeInfo (), Func, PointerAuth);
12431240
12441241 // And call the block.
@@ -1323,14 +1320,15 @@ static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
13231320
13241321 bool IsOpenCL = CGM.getLangOpts ().OpenCL ;
13251322 bool IsWindows = CGM.getTarget ().getTriple ().isOSWindows ();
1323+ auto &CGOPointerAuth = CGM.getCodeGenOpts ().PointerAuth ;
13261324 if (!IsOpenCL) {
13271325 // isa
13281326 if (IsWindows)
13291327 fields.addNullPointer (CGM.Int8PtrPtrTy );
13301328 else
13311329 fields.addSignedPointer (CGM.getNSConcreteGlobalBlock (),
1332- CGM. getCodeGenOpts (). PointerAuth . ObjCIsaPointers ,
1333- GlobalDecl (), QualType ());
1330+ CGOPointerAuth. ObjCIsaPointers , GlobalDecl () ,
1331+ QualType ());
13341332
13351333 // __flags
13361334 BlockFlags flags = BLOCK_IS_GLOBAL;
@@ -1343,28 +1341,26 @@ static llvm::Constant *buildGlobalBlock(CodeGenModule &CGM,
13431341
13441342 // Reserved
13451343 fields.addInt (CGM.IntTy , 0 );
1346-
1347- // Function
1348- if (auto &schema =
1349- CGM.getCodeGenOpts ().PointerAuth .BlockInvocationFunctionPointers ) {
1350- QualType fnType = blockInfo.getBlockExpr ()
1351- ->getType ()
1352- ->castAs <BlockPointerType>()
1353- ->getPointeeType ();
1354- fields.addSignedPointer (blockFn, schema, GlobalDecl (), fnType);
1355- } else {
1356- fields.add (blockFn);
1357- }
13581344 } else {
13591345 fields.addInt (CGM.IntTy , blockInfo.BlockSize .getQuantity ());
13601346 fields.addInt (CGM.IntTy , blockInfo.BlockAlign .getQuantity ());
1361- // Function
1362- fields.add (blockFn);
13631347 }
13641348
1349+ // Function
1350+ if (auto &Schema = CGOPointerAuth.BlockInvocationFunctionPointers ) {
1351+ QualType FnType = blockInfo.getBlockExpr ()
1352+ ->getType ()
1353+ ->castAs <BlockPointerType>()
1354+ ->getPointeeType ();
1355+ fields.addSignedPointer (blockFn, Schema, GlobalDecl (), FnType);
1356+ } else
1357+ fields.add (blockFn);
1358+
13651359 if (!IsOpenCL) {
13661360 // Descriptor
1367- fields.add (buildBlockDescriptor (CGM, blockInfo));
1361+ llvm::Constant *Descriptor = buildBlockDescriptor (CGM, blockInfo);
1362+ fields.addSignedPointer (Descriptor, CGOPointerAuth.BlockDescriptorPointers ,
1363+ GlobalDecl (), QualType ());
13681364 } else if (auto *Helper =
13691365 CGM.getTargetCodeGenInfo ().getTargetOpenCLBlockHelper ()) {
13701366 for (auto *I : Helper->getCustomFieldValues (CGM, blockInfo)) {
@@ -2034,8 +2030,8 @@ CodeGenFunction::GenerateCopyHelperFunction(const CGBlockInfo &blockInfo) {
20342030 // it. It's not quite worth the annoyance to avoid creating it in the
20352031 // first place.
20362032 if (!needsEHCleanup (captureType.isDestructedType ()))
2037- if (auto *I =
2038- cast_or_null<llvm::Instruction>( dstField.getBasePointer ()))
2033+ if (auto *I = cast_or_null<llvm::Instruction>(
2034+ dstField.getPointerIfNotSigned ()))
20392035 I->eraseFromParent ();
20402036 }
20412037 break ;
@@ -2772,11 +2768,11 @@ void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
27722768 const Twine &name, bool isFunction = false ) {
27732769 auto fieldAddr = Builder.CreateStructGEP (addr, nextHeaderIndex, name);
27742770 if (isFunction) {
2775- if (auto &schema = CGM.getCodeGenOpts (). PointerAuth
2776- .BlockByrefHelperFunctionPointers ) {
2777- auto pointerAuth = EmitPointerAuthInfo (
2778- schema , fieldAddr.emitRawPointer (*this ), GlobalDecl (), QualType ());
2779- value = EmitPointerAuthSign (pointerAuth , value);
2771+ if (auto &Schema = CGM.getCodeGenOpts ()
2772+ . PointerAuth .BlockByrefHelperFunctionPointers ) {
2773+ auto PointerAuth = EmitPointerAuthInfo (
2774+ Schema , fieldAddr.emitRawPointer (*this ), GlobalDecl (), QualType ());
2775+ value = EmitPointerAuthSign (PointerAuth , value);
27802776 }
27812777 }
27822778 Builder.CreateStore (value, fieldAddr);
@@ -2861,10 +2857,10 @@ void CodeGenFunction::emitByrefStructureInit(const AutoVarEmission &emission) {
28612857 storeHeaderField (V, getIntSize (), " byref.size" );
28622858
28632859 if (helpers) {
2864- storeHeaderField (helpers->CopyHelper , getPointerSize (),
2865- " byref.copyHelper " , /* function */ true );
2860+ storeHeaderField (helpers->CopyHelper , getPointerSize (), " byref.copyHelper " ,
2861+ /* isFunction= */ true );
28662862 storeHeaderField (helpers->DisposeHelper , getPointerSize (),
2867- " byref.disposeHelper" , /* function */ true );
2863+ " byref.disposeHelper" , /* isFunction= */ true );
28682864 }
28692865
28702866 if (ByRefHasLifetime && HasByrefExtendedLayout) {
0 commit comments