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

[SR-10444] Xcode 10.2 Generates Crashing Code in particular circumstance involving structs, optionals, and file-scoped arrays #52844

Closed
swift-ci opened this issue Apr 10, 2019 · 10 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software optimized only Flag: An issue whose reproduction requires optimized compilation run-time crash Bug → crash: Swift code crashed during execution

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10444
Radar rdar://problem/49755264
Original Reporter paddlefish (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

$ uname -v

Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64

$ swift --version

Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, OptimizedOnly, RunTimeCrash
Assignee @eeckstein
Priority Medium

md5: c20885eef3ecc58cf89fd73240e1b6c0

Issue Description:

Note: Also reported in Radar 49755264

Summary:
New in Xcode 10.2 when generating Swift 4.2 Release code for ARM (e.g. iPhone 6s or iPhone XR) OR iPhone Simulator, the app containing code shown below crashes.

The same code DOES NOT crash when built by swift shipped in Xcode 10.1

The code requires repeated assignment to an array field inside the optional struct `var` property of a class.

I winnowed this down as much as I could, but as far as I can tell all the weird things in the example code are required : the global scope array, the struct, the optional var instance of the struct, and calling the function "sometime later".

Steps to Reproduce:

Here is the code, or simply unzip attached swift package build-and-run, making sure to select Release configuration for your Run target.

//
 // Crash.swift
 // xcode10_2_codegen_1
 //
 // Created by Andrew Rahn on 4/9/19.
 // Copyright © 2019 ModernLogic. All rights reserved.
 //
// Run with 
 // swift build --configuration release && ./.build/release/xcode_10_2_crash
 // [1/2] Compiling Swift Module 'xcode_10_2_crash' (1 sources)
 // xcode_10_2_crash(15484,0x1004d55c0) malloc: *** error for object 0x7fcec7605990: pointer being freed was not allocated
 // xcode_10_2_crash(15484,0x1004d55c0) malloc: *** set a breakpoint in malloc_error_break to debug
 // /bin/bash: line 1: 15484 Abort trap: 6 ./.build/release/xcode_10_2_crash

import Foundation

let alpha = [ "" ]

public struct Beta {
  var gamma: [String]
}

class Delta {
  var epislon: Beta? = Beta(gamma: [""])

  func main() {
    for _ in 1...100 { 
      crash()
    }
  }

  func crash() { 
    epislon?.gamma = [alpha[0]]
  }
}

Delta().main() // This line crashes
print("Bug fixed") // never reached

Expected Results:

It should print "Bug Fixed"

Actual Results:

It crashes
xcode_10_2_crash(15484,0x1004d55c0) malloc: *** error for object 0x7fcec7605990: pointer being freed was not allocated

Version/Build:
Xcode 10.2 (10E125)
Apple Swift version 5.0 (swiftlang-1001.0.69.5 clang-1001.0.46.3)
Target: x86_64-apple-darwin18.5.0

Configuration:
Xcode 10.2
Swift 4.2 OR Swift 5
Release Build

@theblixguy
Copy link
Collaborator

It no longer crashes for me, on Swift 5.1.

@swift-ci
Copy link
Collaborator Author

Comment by Andrew Rahn (JIRA)

@theblixguy we have verified that the problem still exists with swift-DEVELOPMENT-SNAPSHOT-2019-04-07-a . What build of the toolchain did you use? I'm sorry I'm new to reporting Swift bugs so let me know if there is a separate build I should look at ? Thanks

@theblixguy
Copy link
Collaborator

I didn't compile it with a toolchain, I have an up-to-date copy of the source (I checked out again ~15 mins ago).

I compiled and ran the code with `./swift ~/Desktop/test.swift` (which contains your example code) and it printed "Bug Fixed". I even tried with `./swiftc -emit-executable` and then ran the executable and it worked fine as well.

Maybe something else is at play and is causing the crash for you. Might be worth retrying with the next available snapshot.

@swift-ci
Copy link
Collaborator Author

Comment by Andrew Rahn (JIRA)

Ah, can you try again Suyashi Srijan only use the —configuration option to run a release build? The crash does not happen in debug builds.

@theblixguy
Copy link
Collaborator

I'll give it a try later today, I need to build SwiftPM for that.

@belkadan
Copy link
Contributor

With assertions on in a trunk-like build:

Assertion failed: (getStrongExtraRefCount() >= dec && "releasing reference whose refcount is already zero"), function decrementStrongExtraRefCount, file /Volumes/Data/swift-public/swift/include/swift/Runtime/../../../stdlib/public/SwiftShims/RefCount.h, line 531.
zsh: abort      ./main

So we must be overreleasing something. Yikes!

@eeckstein
Copy link
Member

As a workaround you can add following flags to the compiler: -Xllvm -sil-disable-pass=stack-promotion

@eeckstein
Copy link
Member

Fixed in #23931

@swift-ci
Copy link
Collaborator Author

Comment by Andrew Rahn (JIRA)

Awesome, thank you @eeckstein. FYI I think the PR is #23986 although I did enjoy scratching my head reading 23931 trying to figure out how that could possibly have fixed it 😉

@eeckstein
Copy link
Member

oh, you are right. Thanks for correcting the link!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added the crash Bug: A crash, i.e., an abnormal termination of software label Dec 12, 2022
This issue was closed.
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. compiler The Swift compiler in itself crash Bug: A crash, i.e., an abnormal termination of software optimized only Flag: An issue whose reproduction requires optimized compilation run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

5 participants