Skip to content

Commit 648705e

Browse files
authored
Merge pull request swiftlang#72850 from eeckstein/sensitive
Add the experimental attribute `@sensitive` for struct declarations
2 parents deab2fa + 1b1d5ed commit 648705e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+660
-83
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/AST/FeatureAvailability.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ FEATURE(IsolatedAny, (5, 11))
7474
FEATURE(TaskExecutor, FUTURE)
7575
FEATURE(Differentiation, FUTURE)
7676
FEATURE(InitRawStructMetadata, FUTURE)
77+
FEATURE(ClearSensitive, FUTURE)
7778

7879
#undef FEATURE
7980
#undef FUTURE

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/Runtime/Heap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ void *swift_slowAllocTyped(size_t bytes, size_t alignMask, MallocTypeId typeId);
4242
SWIFT_RUNTIME_EXPORT
4343
void swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask);
4444

45+
SWIFT_RUNTIME_EXPORT
46+
void swift_clearSensitive(void *ptr, size_t bytes);
47+
4548
/// Allocate and construct an instance of type \c T.
4649
///
4750
/// \param args The arguments to pass to the constructor for \c T.

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,20 @@ FUNCTION(ExceptionPersonality,
28252825
EFFECT(NoEffect),
28262826
UNKNOWN_MEMEFFECTS)
28272827

2828+
FUNCTION(ClearSensitive, swift_clearSensitive, C_CC, ClearSensitiveAvailability,
2829+
RETURNS(VoidTy),
2830+
ARGS(PtrTy, SizeTy),
2831+
ATTRS(NoUnwind),
2832+
EFFECT(NoEffect),
2833+
UNKNOWN_MEMEFFECTS)
2834+
2835+
FUNCTION(MemsetS, memset_s, C_CC, AlwaysAvailable,
2836+
RETURNS(Int32Ty),
2837+
ARGS(PtrTy, SizeTy, Int32Ty, SizeTy),
2838+
ATTRS(NoUnwind),
2839+
EFFECT(NoEffect),
2840+
UNKNOWN_MEMEFFECTS)
2841+
28282842
#undef RETURNS
28292843
#undef ARGS
28302844
#undef ATTRS

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/ClangImporter/ImportDecl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8075,6 +8075,14 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
80758075
continue;
80768076
}
80778077

8078+
if (swiftAttr->getAttribute() == "sensitive") {
8079+
if (!SwiftContext.LangOpts.hasFeature(Feature::Sensitive))
8080+
continue;
8081+
auto attr = new (SwiftContext) SensitiveAttr(/*implicit=*/true);
8082+
MappedDecl->getAttrs().add(attr);
8083+
continue;
8084+
}
8085+
80788086
// Dig out a buffer with the attribute text.
80798087
unsigned bufferID = getClangSwiftAttrSourceBuffer(
80808088
swiftAttr->getAttribute());

0 commit comments

Comments
 (0)