Skip to content

stdlib: fix availability for internal COW checks. #37945

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 3 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,12 +346,18 @@ internal func _class_getInstancePositiveExtentSize(_ theClass: AnyClass) -> Int
}

#if INTERNAL_CHECKS_ENABLED
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
// "9999" means: enable if linked with a built library, but not when linked with
// the OS libraries.
// Note: this must not be changed to a "real" OS version.
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
@usableFromInline
@_silgen_name("_swift_isImmutableCOWBuffer")
internal func _swift_isImmutableCOWBuffer(_ object: AnyObject) -> Bool

@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
// "9999" means: enable if linked with a built library, but not when linked with
// the OS libraries.
// Note: this must not be changed to a "real" OS version.
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
@usableFromInline
@_silgen_name("_swift_setImmutableCOWBuffer")
internal func _swift_setImmutableCOWBuffer(_ object: AnyObject, _ immutable: Bool) -> Bool
Expand Down
20 changes: 16 additions & 4 deletions stdlib/public/core/ContiguousArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import SwiftShims

#if INTERNAL_CHECKS_ENABLED
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
// "9999" means: enable if linked with a built library, but not when linked with
// the OS libraries.
// Note: this must not be changed to a "real" OS version.
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
@_silgen_name("swift_COWChecksEnabled")
public func _COWChecksEnabled() -> Bool
#endif
Expand Down Expand Up @@ -460,15 +463,21 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
@_alwaysEmitIntoClient
internal var isImmutable: Bool {
get {
if #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) {
// "9999" means: enable if linked with a built library, but not when
// linked with the OS libraries.
// Note: this must not be changed to a "real" OS version.
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
if (_COWChecksEnabled()) {
return capacity == 0 || _swift_isImmutableCOWBuffer(_storage)
}
}
return true
}
nonmutating set {
if #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) {
// "9999" means: enable if linked with a built library, but not when
// linked with the OS libraries.
// Note: this must not be changed to a "real" OS version.
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
if (_COWChecksEnabled()) {
// Make sure to not modify the empty array singleton (which has a
// capacity of 0).
Expand All @@ -489,7 +498,10 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {

@_alwaysEmitIntoClient
internal var isMutable: Bool {
if #available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) {
// "9999" means: enable if linked with a built library, but not when
// linked with the OS libraries.
// Note: this must not be changed to a "real" OS version.
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
if (_COWChecksEnabled()) {
return !_swift_isImmutableCOWBuffer(_storage)
}
Expand Down
10 changes: 7 additions & 3 deletions test/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,13 @@ config.substitutions.append(('%raw-FileCheck', shell_quote(config.filecheck)))
config.substitutions.append(('%import-libdispatch', getattr(config, 'import_libdispatch', '')))
config.substitutions.append(('%import-static-libdispatch', getattr(config, 'import_libdispatch_static', '')))

# Disabe COW sanity checks in the swift runtime by default.
# (But it's required to set this environment variable to something)
config.environment['SWIFT_DEBUG_ENABLE_COW_CHECKS'] = 'false'

# Add this to the command which runs an executable to enable COW checks in the swift runtime.
config.substitutions.append(('%enable-cow-checking', TARGET_ENV_PREFIX + 'SWIFT_DEBUG_ENABLE_COW_CHECKS=true;'))

if config.lldb_build_root != "":
lldb_python_path = get_lldb_python_path(config.lldb_build_root)
lldb_python_interpreter = get_lldb_python_interpreter(config.lldb_build_root)
Expand All @@ -2210,9 +2217,6 @@ config.environment[TARGET_ENV_PREFIX + 'SWIFT_DETERMINISTIC_HASHING'] = '1'
# Enable malloc scribble during tests by default.
config.environment[TARGET_ENV_PREFIX + 'SWIFT_DEBUG_ENABLE_MALLOC_SCRIBBLE'] = 'YES'

# Enable COW sanity checks in the swift runtime by default.
config.environment['SWIFT_DEBUG_ENABLE_COW_CHECKS'] = 'true'

# Run lsb_release on the target to be tested and return the results.
def linux_get_lsb_release():
lsb_release_path = '/usr/bin/lsb_release'
Expand Down
Loading