Skip to content

[IRGen] Fix ObjC method signatures for .cxx_construct and .cxx_destruct #17884

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

Merged
merged 3 commits into from
Jul 14, 2018

Conversation

fay59
Copy link
Contributor

@fay59 fay59 commented Jul 11, 2018

I'm starting the PR against 4.2, but I actually have no idea what I'm doing. I suspect that you have to get someone's blessing to do this, but it didn't seem to be explicitly mentioned in the contribution guidelines. Let me know if this should be rebased on another branch.

Explanation

For aeons past, the Swift compiler has emitted incorrect method signatures for .cxx_construct and .cxx_destruct methods. The signature was hard-coded to "@?", which is the Objective-C type signature for a block. Type signatures and method signatures are different, and this is not a valid method signature.

Scope

Only Swift classes that inherit from NSObject get .cxx_{construct,destruct} methods. It wouldn't be possible to call a Swift .cxx_construct or .cxx_destruct with NSInvocation, although I'm not entirely sure at which point that would fail. This bug is essentially annoying only to people who look at binaries.

Issue

https://bugs.swift.org/browse/SR-8203

Risk

Negligible. We know from experience that you can put garbage in the method signature of .cxx_destruct and it'll go unnoticed for years. In the unlikely, worst-case scenario in which I got it wrong, the garbage will have changed and someone else will step up in 2023 to fix it without anyone else ever noticing that there was a problem.

Testing

Added a test in test/IRGen/objc_methods.swift.

Reviewed By

You?

@CodaFi
Copy link
Contributor

CodaFi commented Jul 11, 2018

I'm starting the PR against 4.2

The usual strategy is to get things into master and cherry-pick them over. I believe GitHub will let you change your target branch without opening another PR.

I suspect that you have to get someone's blessing to do this

We can nominate this for you, just please cherry-pick.

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty reasonable to me! Our commit process is to first put a change on master and then cherry-pick it to a release branch, so can you retarget the PR there first?

@objc func takesString(_ x: String) -> String { return x }
@objc func takesArray(_ x: [AnyObject]) -> [AnyObject] { return x }
@objc func takesDict(_ x: [NSObject: AnyObject]) -> [NSObject: AnyObject] { return x }
@objc func takesSet(_ x: Set<NSObject>) -> Set<NSObject> { return x }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: can you avoid changing whitespace elsewhere in the file? (We also generally try to clear out trailing whitespace even in indented blocks, but even without that it's better if the diff is as small as possible.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I can change that. I originally had my editor set to clear trailing whitespace, but then found that lib/IRGen/GenObjC.cpp has trailing whitespace and reversed course.

}
}

// CHECK: [[IMPLICIT_PARAMS_SIGNATURE:@.*]] = private unnamed_addr constant [8 x i8] c"v16@0:8\00"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: this should probably be named something like DESTRUCT_SIGNATURE.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it a non-descriptive name because that signature is shared by two tested methods (-[ObjcDestructible .cxx_destruct] and -[Foo baz]) that don't really have anything in common. Do you have a name in mind that suits both?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I get it. Maybe NO_ARGS_SIGNATURE, then?

@jrose-apple
Copy link
Contributor

Also, thanks for the note that the branch policy isn't mentioned on the "Contributing Code" page. We should tweak that.

fay59 added 3 commits July 11, 2018 11:40
"@?" is not a method signature. An acceptable format is "vM@0:N", where
N is the pointer size and M is twice the pointer size. The numeric
values don't matter anymore on Apple platforms because the runtime does
not rely on them (they used to be stack offsets from the base pointer),
but given that the first (self) and second (_cmd) parameters are
pointers, the expected value is easily obtained and it is probably good
style to include it.

Fixes https://bugs.swift.org/browse/SR-8203
@fay59 fay59 force-pushed the cxx-destruct-signature branch from 4b870ab to 917ad05 Compare July 11, 2018 18:42
@fay59 fay59 changed the base branch from swift-4.2-branch to master July 11, 2018 18:42
@fay59 fay59 changed the title [4.2] [IRGen] Fix ObjC method signatures for .cxx_construct and .cxx_destruct [IRGen] Fix ObjC method signatures for .cxx_construct and .cxx_destruct Jul 11, 2018
@fay59
Copy link
Contributor Author

fay59 commented Jul 11, 2018

The PR base has been updated. I might need some more hand-holding: should I be the one to kick off automated tests?

@CodaFi
Copy link
Contributor

CodaFi commented Jul 11, 2018

should I be the one to kick off automated tests?

Because you are a first-time contributor, you will not be able to invoke CI. See Commit Access.

@swift-ci please smoke test

@CodaFi
Copy link
Contributor

CodaFi commented Jul 11, 2018

(And to the outside observer, neither can I!)

Tests happening here

@CodaFi
Copy link
Contributor

CodaFi commented Jul 11, 2018

Hmph, looks like swift-ci might not yet support branch-switching - these failures look like it's still trying to build 4.2.

ping: @shahmishal

In the mean time please leave this open and open a separate pull request against master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants