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-8950] Runtime crash accessing memory on 32-bit device after initialising an enum with a large associated struct #51455

Open
liamnichols opened this issue Oct 9, 2018 · 3 comments
Labels
access control Feature → modifiers: Access control and access levels bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.2

Comments

@liamnichols
Copy link

Previous ID SR-8950
Radar rdar://problem/45216973
Original Reporter @liamnichols
Type Bug

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.2Regression
Assignee None
Priority Medium

md5: 95494830e0b32e6a93d1247237241cfe

Issue Description:

Updating to Swift 4.2/Xcode 10 seems to have surfaced a strange memory corruption ( ? ) issue when initialising an extremely large struct inside of an enum using associated types on a 32-bit device.

After a good few hours, I managed to narrow it down to the following sample code:

import UIKit

struct TestStruct {

    // 512 Int32's use 2048 bytes in the memory layout
    let property_1: Int32 = 0
    let property_2: Int32 = 0
    let property_3: Int32 = 0
    ... // removed the additional 506 declarations to shorten this example.
    let property_510: Int32 = 0
    let property_511: Int32 = 0
    let property_512: Int32 = 0

    let string: String = String()
    let date: Date = Date()
}

enum TestEnum {
    case regular
    case associatedType(TestStruct)
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        let result = TestEnum.associatedType(TestStruct())
        print(result)

        return true
    }
}

Using Xcode 10 (10A255) and it's packaged Swift 4.2 compiler, I ran the code on an iPhone 5c (iOS 10.3.3) where I am met with EXC_BAD_ACCESS. I can only seem to reproduce this issue using a 32-bit device. I've attached a sample project as well that you can run yourself.

There is a limit to my knowledge and I'm struggling to understand the actual cause of this issue but below are some points of interest:

  • The crash will only occur if the size of TestStruct is over ~2060 bytes.

  • The crash will only occur if TestStruct contains a Swift String and Date object.

  • The crash will only occur if the TestEnum has an additional case.

  • The crash does not occur in the iOS simulator, when run on macOS, or when run on a 64-bit device (both iOS 10 and iOS 11).

  • In this particular example, it only crashes when running in Debug mode (no compiler optimisations). This is not the case however in our main project but I am assuming that this is related to the fact that the compiler optimised most of the code away in this simple version.

The only workarounds that I've come across are to eliminate one of the above points for the code to execute correctly. Would be great if anybody is able to help me understand things further!

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Oct 12, 2018

@swift-ci create

@aschwaighofer
Copy link
Member

Does this still reproduce with Xcode 10 beta 2?

@liamnichols
Copy link
Author

aschwaighofer@apple.com (JIRA User): I'm sorry but our team since dropped iOS 10 support (and thus 32-bit support) and we no longer have access to the old test devices so I'm not able to verify

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added regression swift 4.2 access control Feature → modifiers: Access control and access levels and removed 4.2 regression labels Nov 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
access control Feature → modifiers: Access control and access levels bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression swift 4.2
Projects
None yet
Development

No branches or pull requests

3 participants