Skip to content
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

Noncopyable deinit causes compiler crash or miscompile with tsan #74042

Closed
WFT opened this issue May 31, 2024 · 1 comment · Fixed by #74064
Closed

Noncopyable deinit causes compiler crash or miscompile with tsan #74042

WFT opened this issue May 31, 2024 · 1 comment · Fixed by #74064
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels

Comments

@WFT
Copy link

WFT commented May 31, 2024

Description

This copied from this forum post: https://forums.swift.org/t/noncopyable-type-has-deinit-called-before-init/72152

I have a test case where a noncopyable struct’s deinit is called twice, leading to memory corruption. This only happens when compiled with thread sanitizer.

On 5.10 this causes a miscompile on 6.0 nightly forum replies say that it crashes (I haven’t tried).

Reproduction

// Run this with swiftc -sanitize=thread test.swift && ./test
import Accelerate

struct Testable: ~Copyable {
    var foo = 999
    init() { print("Initing...") }
    func honk() {
        print(foo)
    }
    deinit { print("Deinit!") }
}

struct Thing: ~Copyable {
    var buffer: UnsafeMutableBufferPointer<Float>
    var t: Testable

    init() {
        buffer = .allocate(capacity: 100)
        vDSP.clear(&buffer)
        t = Testable()
    }

    func test() {
        t.honk()
    }

    deinit {
        buffer.deallocate()
    }
}


Thing().test()

Stack dump

error: compile command failed due to signal 6 (use -v to see invocation)
Found a write classified as a liveness use?!
Use:   %18 = builtin "tsanInoutAccess"(%17 : $*UnsafeMutableBufferPointer<Float>) : $()
standard failure
UNREACHABLE executed at /Users/ec2-user/jenkins/workspace/oss-swift-6.0-package-macos/swift/lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerUtils.cpp:2730!
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.

Expected behavior

I expected it to print this:

Initing...
999
Deinit!

On swift 5.10 it prints Deinit once before Initing (unexpected) AND once after (expected). On swift 6.0 nightly it apparently crashes (see forum replies for this).

Environment

Version with miscompile:
$ swiftc --version
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Version with crash:
Apple Swift version 6.0-dev (LLVM 4b0b1f9e1a69523, Swift 91a3abc)

Additional information

No response

@WFT WFT added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels May 31, 2024
@tbkka
Copy link
Contributor

tbkka commented May 31, 2024

CC: @kavon @jckarter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants