@@ -153,12 +153,21 @@ static bool isArchetypeValidInFunction(ArchetypeType *A, const SILFunction *F) {
153
153
154
154
namespace {
155
155
156
- // / When resilience is bypassed, direct access is legal, but the decls are still
157
- // / resilient.
156
+ // / When resilience is bypassed or package serialization is enabled,
157
+ // / direct access is legal, but the decls are still resilient.
158
158
template <typename DeclType>
159
159
bool checkResilience (DeclType *D, ModuleDecl *M,
160
- ResilienceExpansion expansion) {
161
- return !D->getModuleContext ()->getBypassResilience () &&
160
+ ResilienceExpansion expansion,
161
+ bool isSerialized) {
162
+ // Check whether the SIL definition referencing decl D is serialized
163
+ // when its defining module M was resiliently built; this means package
164
+ // serialization was enabled and direct access to decl D should be
165
+ // allowed.
166
+ auto serializedInResilientModule = M->isResilient () &&
167
+ isSerialized &&
168
+ D->getModuleContext ()->inSamePackage (M);
169
+ return !serializedInResilientModule &&
170
+ !D->getModuleContext ()->getBypassResilience () &&
162
171
D->isResilient (M, expansion);
163
172
}
164
173
@@ -193,6 +202,7 @@ namespace {
193
202
// / Verify invariants on a key path component.
194
203
void verifyKeyPathComponent (SILModule &M,
195
204
TypeExpansionContext typeExpansionContext,
205
+ bool isSerialized,
196
206
llvm::function_ref<void (bool , StringRef)> require,
197
207
CanType &baseTy,
198
208
CanType leafTy,
@@ -300,7 +310,7 @@ void verifyKeyPathComponent(SILModule &M,
300
310
" property decl should be a member of the base with the same type "
301
311
" as the component" );
302
312
require (property->hasStorage (), " property must be stored" );
303
- require (!checkResilience (property, M.getSwiftModule (), expansion),
313
+ require (!checkResilience (property, M.getSwiftModule (), expansion, isSerialized ),
304
314
" cannot access storage of resilient property" );
305
315
auto propertyTy =
306
316
loweredBaseTy.getFieldType (property, M, typeExpansionContext);
@@ -2469,13 +2479,12 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
2469
2479
SILGlobalVariable *RefG = AGI->getReferencedGlobal ();
2470
2480
if (auto *VD = RefG->getDecl ()) {
2471
2481
require (!checkResilience (VD, F.getModule ().getSwiftModule (),
2472
- F.getResilienceExpansion ()),
2482
+ F.getResilienceExpansion (), F. isSerialized () ),
2473
2483
" cannot access storage of resilient global" );
2474
2484
}
2475
2485
if (F.isSerialized ()) {
2476
- // If it has a package linkage at this point, package CMO must
2477
- // have been enabled, so opt in for visibility.
2478
2486
require (RefG->isSerialized ()
2487
+ // RefG should be visibile if it has package linkage in serialized context.
2479
2488
|| hasPublicOrPackageVisibility (RefG->getLinkage (), /* includePackage*/ true ),
2480
2489
" alloc_global inside fragile function cannot "
2481
2490
" reference a private or hidden symbol" );
@@ -2490,13 +2499,13 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
2490
2499
" global_addr/value must be the type of the variable it references" );
2491
2500
if (auto *VD = RefG->getDecl ()) {
2492
2501
require (!checkResilience (VD, F.getModule ().getSwiftModule (),
2493
- F.getResilienceExpansion ()),
2502
+ F.getResilienceExpansion (), F. isSerialized () ),
2494
2503
" cannot access storage of resilient global" );
2495
2504
}
2505
+
2496
2506
if (F.isSerialized ()) {
2497
- // If it has a package linkage at this point, package CMO must
2498
- // have been enabled, so opt in for visibility.
2499
2507
require (RefG->isSerialized ()
2508
+ // RefG should be visibile if it has package linkage in serialized context.
2500
2509
|| hasPublicOrPackageVisibility (RefG->getLinkage (), /* includePackage*/ true ),
2501
2510
" global_addr/value inside fragile function cannot "
2502
2511
" reference a private or hidden symbol" );
@@ -3415,7 +3424,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
3415
3424
" Cannot build a struct with unreferenceable storage from elements "
3416
3425
" using StructInst" );
3417
3426
require (!checkResilience (structDecl, F.getModule ().getSwiftModule (),
3418
- F.getResilienceExpansion ()),
3427
+ F.getResilienceExpansion (), F. isSerialized () ),
3419
3428
" cannot access storage of resilient struct" );
3420
3429
require (SI->getType ().isObject (),
3421
3430
" StructInst must produce an object" );
@@ -3652,7 +3661,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
3652
3661
require (cd, " Operand of dealloc_ref must be of class type" );
3653
3662
3654
3663
require (!checkResilience (cd, F.getModule ().getSwiftModule (),
3655
- F.getResilienceExpansion ()),
3664
+ F.getResilienceExpansion (), F. isSerialized () ),
3656
3665
" cannot directly deallocate resilient class" );
3657
3666
}
3658
3667
void checkDeallocPartialRefInst (DeallocPartialRefInst *DPRI) {
@@ -3780,7 +3789,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
3780
3789
StructDecl *sd = operandTy.getStructOrBoundGenericStruct ();
3781
3790
require (sd, " must struct_extract from struct" );
3782
3791
require (!checkResilience (sd, F.getModule ().getSwiftModule (),
3783
- F.getResilienceExpansion ()),
3792
+ F.getResilienceExpansion (), F. isSerialized () ),
3784
3793
" cannot access storage of resilient struct" );
3785
3794
require (!EI->getField ()->isStatic (),
3786
3795
" cannot get address of static property with struct_element_addr" );
@@ -3836,7 +3845,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
3836
3845
StructDecl *sd = operandTy.getStructOrBoundGenericStruct ();
3837
3846
require (sd, " struct_element_addr operand must be struct address" );
3838
3847
require (!checkResilience (sd, F.getModule ().getSwiftModule (),
3839
- F.getResilienceExpansion ()),
3848
+ F.getResilienceExpansion (), F. isSerialized () ),
3840
3849
" cannot access storage of resilient struct" );
3841
3850
require (EI->getType ().isAddress (),
3842
3851
" result of struct_element_addr must be address" );
@@ -3877,8 +3886,9 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
3877
3886
SILType operandTy = EI->getOperand ()->getType ();
3878
3887
ClassDecl *cd = operandTy.getClassOrBoundGenericClass ();
3879
3888
require (cd, " ref_element_addr operand must be a class instance" );
3889
+
3880
3890
require (!checkResilience (cd, F.getModule ().getSwiftModule (),
3881
- F.getResilienceExpansion ()),
3891
+ F.getResilienceExpansion (), F. isSerialized () ),
3882
3892
" cannot access storage of resilient class" );
3883
3893
3884
3894
require (EI->getField ()->getDeclContext () ==
@@ -3904,7 +3914,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
3904
3914
ClassDecl *cd = operandTy.getClassOrBoundGenericClass ();
3905
3915
require (cd, " ref_tail_addr operand must be a class instance" );
3906
3916
require (!checkResilience (cd, F.getModule ().getSwiftModule (),
3907
- F.getResilienceExpansion ()),
3917
+ F.getResilienceExpansion (), F. isSerialized () ),
3908
3918
" cannot access storage of resilient class" );
3909
3919
require (cd, " ref_tail_addr operand must be a class instance" );
3910
3920
checkAddressWalkerCanVisitAllTransitiveUses (RTAI);
@@ -3915,7 +3925,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
3915
3925
StructDecl *sd = operandTy.getStructOrBoundGenericStruct ();
3916
3926
require (sd, " must struct_extract from struct" );
3917
3927
require (!checkResilience (sd, F.getModule ().getSwiftModule (),
3918
- F.getResilienceExpansion ()),
3928
+ F.getResilienceExpansion (), F. isSerialized () ),
3919
3929
" cannot access storage of resilient struct" );
3920
3930
if (F.hasOwnership ()) {
3921
3931
// Make sure that all of our destructure results ownership kinds are
@@ -5730,8 +5740,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
5730
5740
hasIndices = false ;
5731
5741
break ;
5732
5742
}
5733
-
5734
- verifyKeyPathComponent (F.getModule (), F.getTypeExpansionContext (),
5743
+
5744
+ verifyKeyPathComponent (F.getModule (), F.getTypeExpansionContext (), F. isSerialized (),
5735
5745
[&](bool reqt, StringRef message) { _require (reqt, message); },
5736
5746
baseTy,
5737
5747
leafTy,
@@ -7205,6 +7215,7 @@ void SILProperty::verify(const SILModule &M) const {
7205
7215
ResilienceExpansion::Maximal);
7206
7216
verifyKeyPathComponent (const_cast <SILModule&>(M),
7207
7217
typeExpansionContext,
7218
+ isSerialized (),
7208
7219
require,
7209
7220
baseTy,
7210
7221
leafTy,
0 commit comments