Skip to content

Commit ce33d47

Browse files
committed
stdlib: add the swift_clearSensitive runtime function
1 parent 3097cce commit ce33d47

File tree

8 files changed

+43
-0
lines changed

8 files changed

+43
-0
lines changed

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/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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,13 @@ 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+
28282835
#undef RETURNS
28292836
#undef ARGS
28302837
#undef ATTRS

lib/IRGen/IRGenModule.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,14 @@ namespace RuntimeConstants {
969969
return RuntimeAvailability::AlwaysAvailable;
970970
}
971971

972+
RuntimeAvailability ClearSensitiveAvailability(ASTContext &Context) {
973+
auto featureAvailability = Context.getClearSensitiveAvailability();
974+
if (!isDeploymentAvailabilityContainedIn(Context, featureAvailability)) {
975+
return RuntimeAvailability::ConditionallyAvailable;
976+
}
977+
return RuntimeAvailability::AlwaysAvailable;
978+
}
979+
972980
} // namespace RuntimeConstants
973981

974982
// We don't use enough attributes to justify generalizing the

stdlib/public/core/EmbeddedRuntime.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,16 @@ func arc4random_buf(buf: UnsafeMutableRawPointer, nbytes: Int)
338338
public func swift_stdlib_random(_ buf: UnsafeMutableRawPointer, _ nbytes: Int) {
339339
arc4random_buf(buf: buf, nbytes: nbytes)
340340
}
341+
342+
@_cdecl("swift_clearSensitive")
343+
@inline(never)
344+
public func swift_clearSensitive(buf: UnsafeMutableRawPointer, nbytes: Int) {
345+
// TODO: use memset_s if available
346+
// Though, it shouldn't make too much difference because the `@inline(never)` should prevent
347+
// the optimizer from removing the loop below.
348+
let bytePtr = buf.assumingMemoryBound(to: UInt8.self)
349+
for i in 0..<nbytes {
350+
bytePtr[i] = 0
351+
}
352+
}
353+

stdlib/public/runtime/Heap.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "swift/shims/RuntimeShims.h"
2222
#include <algorithm>
2323
#include <stdlib.h>
24+
#include <string.h>
2425
#if defined(__APPLE__) && SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
2526
#include "swift/Basic/Lazy.h"
2627
#include <malloc/malloc.h>
@@ -146,3 +147,10 @@ static void swift_slowDeallocImpl(void *ptr, size_t alignMask) {
146147
void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
147148
swift_slowDeallocImpl(ptr, alignMask);
148149
}
150+
151+
void swift::swift_clearSensitive(void *ptr, size_t bytes) {
152+
// TODO: use memset_s if available
153+
// Though, it shouldn't make too much difference because the optimizer cannot remove
154+
// the following memset without inlining this library function.
155+
memset(ptr, 0, bytes);
156+
}

test/abi/macOS/arm64/stdlib.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ Added: __swift_exceptionPersonality
562562
Added: _swift_willThrowTypedImpl
563563
Added: __swift_willThrowTypedImpl
564564
Added: __swift_enableSwizzlingOfAllocationAndRefCountingFunctions_forInstrumentsOnly
565+
Added: _swift_clearSensitive
565566

566567
// Runtime bincompat functions for Concurrency runtime to detect legacy mode
567568
Added: _swift_bincompat_useLegacyNonCrashingExecutorChecks

test/abi/macOS/x86_64/stdlib-asserts.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,5 @@ Added: _OBJC_CLASS_$__TtCs20__StaticArrayStorage
6060
Added: _OBJC_METACLASS_$__TtCs20__StaticArrayStorage
6161

6262
// Runtime Symbols
63+
Added: _swift_clearSensitive
64+

0 commit comments

Comments
 (0)