Skip to content

[SE-0458] Adopt strict memory safety in the standard libraries #78372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Feb 27, 2025

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Dec 27, 2024

With the acceptance of SE-0458 "Opt-In Strict Memory Safety Checking" and its enablement in the compiler, adopt strict memory safety through most of the Standard Library. This involves passing the -strict-memory-safety flag to the compiler when building these standard library modules, and adding all of the necessary @safe/@unsafe/unsafe annotations to acknowledge uses of existing unsafe code.

There are a lot of @unsafe and unsafe annotations, hence the large diff. This is, fundamentally, because it's the Swift standard libraries that layer safe abstractions (arrays, dictionaries, strings, etc.) on top of unsafe primitives (raw UTF-8 bytes and such). The annotation here was largely mechanical, driven by compiler Fix-Its with some review and massaging.

// if it was present on the command line.
auto &paths = outputPaths.back();
if (auto fixitPath = Args.getLastArg(options::OPT_emit_fixits_path)) {
if (paths.FixItsOutputPath.empty()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this check? The if (auto ... = Args.getLastArg(options::OPT_emit_fixits_path)) should get just the last entry.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably, the supplementary outputs file could have an entry here, so we're saying that would take precedence over the command-line argument.

@@ -18,7 +18,7 @@ endif()
if(SWIFT_STDLIB_SIL_DEBUGGING)
list(APPEND SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS "-Xfrontend" "-sil-based-debuginfo")
endif()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bleeding whitespace

@@ -18,7 +18,7 @@ endif()
if(SWIFT_STDLIB_SIL_DEBUGGING)
list(APPEND SWIFT_RUNTIME_SWIFT_COMPILE_FLAGS "-Xfrontend" "-sil-based-debuginfo")
endif()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bleeding whitespace

@@ -575,7 +575,7 @@ extension _ArrayBuffer {

@inlinable @_alwaysEmitIntoClient
internal func getAssociatedBuffer() -> _ContiguousArrayBuffer<Element>? {
let getter = unsafeBitCast(
let getter = unsafe unsafeBitCast(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're going to need condfail mitigations in all the fragile functions, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I had an idea on that... since we have a swift-syntax tree for the inlined text, I think we can write a visitor that replaces UnsafeExprSyntax nodes with their argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm glad my SwiftIfConfig rewrite can be useful :)

Allow a conformance to be "isolated", meaning that it stays in the same
isolation domain as the conforming type. Only allow this for
global-actor-isolated types.

When a conformance is isolated, a nonisolated requirement can be
witnessed by a declaration with the same global actor isolation as the
enclosing type.
When a protocol conformance somehow depends on an isolated conformance, it
must itself be isolated to the same global actor as the conformance on
which it depends.
… default

With the acceptance of SE-0458, allow the use of unsafe expressions, the
@safe and @unsafe attributes, and the `unsafe` effect on the for..in loop
in all Swift code.

Introduce the `-strict-memory-safety` flag detailed in the proposal to
enable strict memory safety checking. This enables a new class of
feature, an optional feature (that is *not* upcoming or experimental),
and which can be detected via `hasFeature(StrictMemorySafety)`.
Port over the disambiguation code we already had in the new Swift parser
to the existing C++ parser for the "unsafe" expression and for..in effect.
@DougGregor DougGregor changed the title Adopt strict safety checking in the standard libraries Adopt strict memory safety in the standard libraries Feb 26, 2025
@DougGregor DougGregor changed the title Adopt strict memory safety in the standard libraries [SE-0458] Adopt strict memory safety in the standard libraries Feb 26, 2025
@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2978

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

Note that we have some serious stacking of PRs happening right now. The first five commits are covered by #79645

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2978

@swift-ci please smoke test macOS

@DougGregor
Copy link
Member Author

swiftlang/swift-syntax#2978

@swift-ci please smoke test Windows

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor enabled auto-merge February 27, 2025 02:53
@eeckstein
Copy link
Contributor

Just do double check that this (large) change has no impact on code generation:

@swift-ci apple silicon benchmark

@DougGregor DougGregor merged commit 3001177 into swiftlang:main Feb 27, 2025
4 checks passed
@DougGregor DougGregor deleted the strict-safety-stdlib branch February 27, 2025 07:46
@DougGregor
Copy link
Member Author

@eeckstein There should be zero impact on performance because there's no SIL-level differences for anything related to the strict memory safety checking. I think the results of that performance run look fine, but there are a few's ?'s I don't know how to interpret.

@eeckstein
Copy link
Contributor

Yes, it's completely fine. The "?" results are noise. If there would be a real impact, the results would look much different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants