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