Skip to content

Commit a980452

Browse files
committed
Add the experimental attribute @sensitive for struct declarations
The attribute declares that a struct contains "sensitive" data. It enforces that the contents of such a struct value is zeroed out at the end of its lifetime. In other words: the content of such a value is not observable in memory after the value's lifetime. Also add an experimental feature `Sensitive` with which the attribute can be enabled.
1 parent fa1fe8b commit a980452

File tree

8 files changed

+38
-1
lines changed

8 files changed

+38
-1
lines changed

include/swift/AST/DeclAttr.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,15 @@ DECL_ATTR_ALIAS(_disallowFeatureSuppression, AllowFeatureSuppression)
494494
SIMPLE_DECL_ATTR(_preInverseGenerics, PreInverseGenerics,
495495
OnAbstractFunction | OnSubscript | OnVar | OnExtension | UserInaccessible | ABIBreakingToAdd | ABIBreakingToRemove | APIStableToAdd | APIStableToRemove,
496496
158)
497+
498+
// Declares that a struct contains "sensitive" data. It enforces that the contents of such a struct value
499+
// is zeroed out at the end of its lifetime. In other words: the content of such a value is not observable
500+
// in memory after the value's lifetime.
501+
// TODO: enable @sensitive also for other nominal types than structs, e.g. for enums
502+
SIMPLE_DECL_ATTR(sensitive, Sensitive,
503+
OnStruct | UserInaccessible | ABIStableToAdd | ABIStableToRemove | APIBreakingToAdd | APIStableToRemove,
504+
159)
505+
497506
LAST_DECL_ATTR(PreInverseGenerics)
498507

499508
#undef DECL_ATTR_ALIAS

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,10 @@ ERROR(attr_static_exclusive_only_mutating,none,
19831983
ERROR(attr_extractConstantsFromMembers_experimental,none,
19841984
"@extractConstantsFromMembers requires '-enable-experimental-feature ExtractConstantsFromMembers'", ())
19851985

1986+
// @sensitive
1987+
ERROR(attr_sensitive_experimental,none,
1988+
"@sensitive requires '-enable-experimental-feature Sensitive'", ())
1989+
19861990
ERROR(c_func_variadic, none,
19871991
"cannot declare variadic argument %0 in %kind1",
19881992
(DeclName, const ValueDecl *))

include/swift/Basic/Features.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,8 @@ EXPERIMENTAL_FEATURE(ObjCImplementation, true)
384384
// Enable @implementation on @_cdecl functions.
385385
EXPERIMENTAL_FEATURE(CImplementation, true)
386386

387+
// Enable @sensitive attribute.
388+
EXPERIMENTAL_FEATURE(Sensitive, true)
387389

388390
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
389391
#undef EXPERIMENTAL_FEATURE

include/swift/SIL/SILType.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,13 @@ class SILType {
481481
bool hasParameterizedExistential() const {
482482
return getASTType()->hasParameterizedExistential();
483483
}
484-
484+
485+
bool isSensitive() const {
486+
if (auto *nom = getNominalOrBoundGenericNominal())
487+
return nom->getAttrs().hasAttribute<SensitiveAttr>();
488+
return false;
489+
}
490+
485491
/// Returns the representation used by an existential type. If the concrete
486492
/// type is provided, this may return a specialized representation kind that
487493
/// can be used for that type. Otherwise, returns the most general

lib/AST/FeatureSet.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,10 @@ static bool usesFeatureGlobalActorIsolatedTypesUsability(Decl *decl) {
694694
UNINTERESTING_FEATURE(ObjCImplementation)
695695
UNINTERESTING_FEATURE(CImplementation)
696696

697+
static bool usesFeatureSensitive(Decl *decl) {
698+
return decl->getAttrs().hasAttribute<SensitiveAttr>();
699+
}
700+
697701
// ----------------------------------------------------------------------------
698702
// MARK: - FeatureSet
699703
// ----------------------------------------------------------------------------

lib/ASTGen/Sources/ASTGen/DeclAttrs.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ extension ASTGenVisitor {
167167
return self.generateSectionAttr(attribute: node)?.asDeclAttribute
168168
case .semantics:
169169
return self.generateSemanticsAttr(attribute: node)?.asDeclAttribute
170+
case .sensitive:
171+
fatalError("unimplemented")
170172
case .silGenName:
171173
return self.generateSILGenNameAttr(attribute: node)?.asDeclAttribute
172174
case .specialize:

lib/Sema/TypeCheckAttr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ class AttributeChecker : public AttributeVisitor<AttributeChecker> {
343343

344344
void visitExtractConstantsFromMembersAttr(ExtractConstantsFromMembersAttr *attr);
345345

346+
void visitSensitiveAttr(SensitiveAttr *attr);
347+
346348
void visitUnavailableFromAsyncAttr(UnavailableFromAsyncAttr *attr);
347349

348350
void visitUnsafeInheritExecutorAttr(UnsafeInheritExecutorAttr *attr);
@@ -451,6 +453,13 @@ void AttributeChecker::visitExtractConstantsFromMembersAttr(ExtractConstantsFrom
451453
}
452454
}
453455

456+
void AttributeChecker::visitSensitiveAttr(SensitiveAttr *attr) {
457+
if (!Ctx.LangOpts.hasFeature(Feature::Sensitive)) {
458+
diagnoseAndRemoveAttr(attr,
459+
diag::attr_sensitive_experimental);
460+
}
461+
}
462+
454463
void AttributeChecker::visitTransparentAttr(TransparentAttr *attr) {
455464
DeclContext *dc = D->getDeclContext();
456465
// Protocol declarations cannot be transparent.

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,7 @@ namespace {
16271627
UNINTERESTING_ATTR(CompilerInitialized)
16281628
UNINTERESTING_ATTR(AlwaysEmitConformanceMetadata)
16291629
UNINTERESTING_ATTR(ExtractConstantsFromMembers)
1630+
UNINTERESTING_ATTR(Sensitive)
16301631

16311632
UNINTERESTING_ATTR(EagerMove)
16321633
UNINTERESTING_ATTR(NoEagerMove)

0 commit comments

Comments
 (0)