swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.21k stars 10.32k forks source link

Class containing a dictionary `var` crashes on write on older macOS versions #67614

Open CharlesJS opened 1 year ago

CharlesJS commented 1 year ago

Description If a class contains a dictionary var, and the project is compiled for backwards compatibility to macOS 10.13, and the resulting code is run on macOS 10.14.6, a crash will occur when the dictionary variable is set.

Steps to reproduce Copy the following code to a file named crashexample.swift:

class MyObject {
    var dict: [String : String] = [:]
}

print("creating object")
let obj = MyObject() // crashes here
print("created object") // will never be logged
print("object is \(obj)")

Then compile it with the following command:

swiftc -O -target x86_64-apple-macos10.13 crashexample.swift

Now copy the resulting binary to a Mac running macOS 10.14.6 and run it.

The output I see when I run this is:

$ ./crashexample
creating object
crashexample(1527,0x108e5b5c0) malloc: *** error for object 0x7fffa6fbf6e0: pointer being freed was not allocated
crashexample(1527,0x108e5b5c0) malloc: *** set a breakpoint in malloc_error_break to debug
Abort trap: 6

and the backtrace in the crash report looks like:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libsystem_kernel.dylib              0x00007fff70b952c2 __pthread_kill + 10
1   libsystem_pthread.dylib             0x00007fff70c50bf1 pthread_kill + 284
2   libsystem_c.dylib                   0x00007fff70aff6a6 abort + 127
3   libsystem_malloc.dylib              0x00007fff70c0e077 malloc_vreport + 545
4   libsystem_malloc.dylib              0x00007fff70c0de38 malloc_report + 151
5   libswiftCore.dylib                  0x00007fff70471a60 _swift_release_dealloc + 16
6   crashexample                        0x00000001015d9d8a main + 202
7   libdyld.dylib                       0x00007fff70a5a3d5 start + 1

Note that similar behavior occurs if you set the dictionary after initialization, like so:

class MyObject {
    var dict: [String : String]? = nil
}

print("creating object")
let obj = MyObject()
print("created object")
print("object is \(obj)")
print("setting dict")
obj.dict = [:] // Crash!
print("set dict") // Not logged

Expected behavior This code should not crash.

Environment

CharlesJS commented 1 year ago

Still seems to occur with Xcode 15 beta 6.

CharlesJS commented 1 year ago

Still occurring with Xcode 15 beta 7.

matxpa commented 11 months ago

@CharlesJS

Hi

and with 15.0 (15A240d) on Ventura too on macOS 10.13.6 ... Reason: Incompatible library version: Pacifist requires version 45.0.0 or later, but libswiftAppKit.dylib provides version 1.0.0

Regards

nickasd commented 10 months ago

I can confirm that it still happens with Xcode 15.0. I got alerted after receiving a negative review for my App Store app after publishing an update built with Xcode 15.0, which I thought still supported macOS 10.13. Why does it take so long to fix this issue?