Skip to content

Commit 402ffeb

Browse files
authored
Merge pull request swiftlang#32926 from eeckstein/cow-test-enabling
stdlib: enable COW sanity checks with an environment variable
2 parents 24b9ec7 + 9382e7a commit 402ffeb

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-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

test/lit.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,9 @@ config.environment[TARGET_ENV_PREFIX + 'SWIFT_DETERMINISTIC_HASHING'] = '1'
19401940
# Enable malloc scribble during tests by default.
19411941
config.environment[TARGET_ENV_PREFIX + 'SWIFT_DEBUG_ENABLE_MALLOC_SCRIBBLE'] = 'YES'
19421942

1943+
# Enable COW sanity checks in the swift runtime by default.
1944+
config.environment['SWIFT_DEBUG_ENABLE_COW_SANITY_CHECKS'] = 'true'
1945+
19431946
# Run lsb_release on the target to be tested and return the results.
19441947
def linux_get_lsb_release():
19451948
lsb_release_path = '/usr/bin/lsb_release'

0 commit comments

Comments
 (0)