Skip to content

[SR-4363] isa-swizzling causes protocol variable access to crash #46942

@swift-ci

Description

@swift-ci
Previous ID SR-4363
Radar rdar://problem/31275541
Original Reporter vladot (JIRA User)
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Mac OS X 10.11.5 + XCode 8.2.1

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

md5: 4be0928a50bcb0c800fb21ca3baf013c

Issue Description:

I am not sure whether this is an NSObject issue or a Swift issue, but since it seems to me that the general problem is with accessing protocol variables after isa-swizzling, I think it falls more in the Swift domain. Here goes:

I have a protocol and the first steps of its type-erasure like so

import Foundation

protocol A: class { }

class AbstractA: A { }

class DelegatedA<D: A>: AbstractA {
    init(_ d: D) { }
}

class AnyA: DelegatedA<AbstractA> {
    init<D: A>(_ d: D) {
        super.init(DelegatedA<D>(d))
    }
}

extension A {
    var anyA: AnyA {
        return AnyA(self)
    }
}

Now I declare a class that conforms to the protocol and also inherits from NSObject

class B: NSObject, A { }

Next I declare a variable from the class B and upcast it to NSObject

var b = B()
let d: NSObject = b

Casting d to A and type-erasing it works fine

(d as! A).anyA

Now we define an observer of b

class O: NSObject { }
var o = O()
b.addObserver(o, forKeyPath: "xxx", options: [.new], context: nil)

This isa-swizzles b and now the type-erasure of d crashes

(d as! A).anyA

I have attached a snapshot of the top of the stack after the crash. The crash is in _buildDemanglingForNominalType.

Thanks,
Vladimir

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfcrashBug: A crash, i.e., an abnormal termination of softwarerun-time crashBug → crash: Swift code crashed during executionruntimeThe Swift Runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions