Skip to content

Commit 666c5e0

Browse files
committed
stdlib: enable COW sanity checks with an environment variable
Instead of doing the sanity checks by default (with an assert-build of the stdlib), only do the checks if the environment variable SWIFT_DEBUG_ENABLE_COW_SANITY_CHECKS is set to true. The checks can give false alarms in case a binary is built against a no-assert stdlib but run with an assert-stdlib. Therefore only do the checks if it's explicitly enabled at runtime. rdar://problem/65475776
1 parent 0ee53f9 commit 666c5e0

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@
1212

1313
import SwiftShims
1414

15+
#if INTERNAL_CHECKS_ENABLED
16+
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
17+
@_silgen_name("swift_COWSanityChecksEnabled")
18+
public func _COWSanityChecksEnabled() -> Bool
19+
20+
@_alwaysEmitIntoClient
21+
internal func doCOWSanityChecks() -> Bool {
22+
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
23+
return _COWSanityChecksEnabled()
24+
}
25+
return false
26+
}
27+
#endif
28+
1529
/// Class used whose sole instance is used as storage for empty
1630
/// arrays. The instance is defined in the runtime and statically
1731
/// initialized. See stdlib/runtime/GlobalObjects.cpp for details.
@@ -454,13 +468,13 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
454468
@_alwaysEmitIntoClient
455469
internal var isImmutable: Bool {
456470
get {
457-
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
471+
if doCOWSanityChecks() {
458472
return capacity == 0 || _swift_isImmutableCOWBuffer(_storage)
459473
}
460474
return true
461475
}
462476
nonmutating set {
463-
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
477+
if doCOWSanityChecks() {
464478
if newValue {
465479
if capacity > 0 {
466480
let wasImmutable = _swift_setImmutableCOWBuffer(_storage, true)
@@ -480,7 +494,7 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
480494

481495
@_alwaysEmitIntoClient
482496
internal var isMutable: Bool {
483-
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
497+
if doCOWSanityChecks() {
484498
return !_swift_isImmutableCOWBuffer(_storage)
485499
}
486500
return true

stdlib/public/runtime/EnvironmentVariables.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,9 @@ void swift::runtime::environment::initialize(void *context) {
188188
"will not be flagged.");
189189
}
190190
#endif
191+
192+
SWIFT_RUNTIME_EXPORT
193+
bool swift_COWSanityChecksEnabled() {
194+
return runtime::environment::SWIFT_DEBUG_ENABLE_COW_SANITY_CHECKS();
195+
}
196+

stdlib/public/runtime/EnvironmentVariables.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,7 @@ VARIABLE(SWIFT_ENABLE_MANGLED_NAME_VERIFICATION, bool, false,
4444
VARIABLE(SWIFT_DEBUG_ENABLE_MALLOC_SCRIBBLE, bool, false,
4545
"Scribble on runtime allocations such as metadata allocations.")
4646

47+
VARIABLE(SWIFT_DEBUG_ENABLE_COW_SANITY_CHECKS, bool, false,
48+
"Enable sanity checks for copy-on-write operations.")
49+
4750
#undef VARIABLE

0 commit comments

Comments
 (0)