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-5036] Segfault using KeyPath with NSObject #47612

Closed
mdiep opened this issue May 28, 2017 · 9 comments
Closed

[SR-5036] Segfault using KeyPath with NSObject #47612

mdiep opened this issue May 28, 2017 · 9 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 run-time crash Bug → crash: Swift code crashed during execution

Comments

@mdiep
Copy link
Contributor

mdiep commented May 28, 2017

Previous ID SR-5036
Radar rdar://problem/32488871
Original Reporter @mdiep
Type Bug
Status Closed
Resolution Done
Environment

macOS 10.12.5
Swift Development Snapshot 5/27/17 (org.swift.3020170527a)

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

md5: 1f1bde21728e8936374b59d119c4e7b8

Issue Description:

import Foundation

class A: NSObject {
    let b: String
    
    init(b: String) {
        self.b = b
    }
}

let a = A(b: "b")
print(a[keyPath: \A.b])

crashes with

0  swift                    0x0000000105365898 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40
1  swift                    0x0000000105364866 llvm::sys::RunSignalHandlers() + 86
2  swift                    0x0000000105365e59 SignalHandler(int) + 361
3  libsystem_platform.dylib 0x00007fffc0cebb3a _sigtramp + 26
4  libsystem_platform.dylib 0x00000001110ff0cb _sigtramp + 1346450859
5  libsystem_c.dylib        0x00007fffc0b514d2 __sfvwrite + 592
6  libsystem_c.dylib        0x00007fffc0b5188d fwrite + 153
7  libswiftCore.dylib       0x000000010af4d99e _T0s7_StdoutVs16TextOutputStreamssACP5writeySSFTW + 14
8  libswiftCore.dylib       0x000000010b05ba4a _T0s10_TeeStreamV5writeySSFTfq4xn_n + 106
9  libswiftCore.dylib       0x000000010af4dda9 _T0s10_TeeStreamV5writeySSF + 9
10 libswiftCore.dylib       0x000000010af4de49 _T0s10_TeeStreamVyxq_Gs010TextOutputB0ssADRzsADR_r0_lsADP5writeySSFTW + 9
11 libswiftCore.dylib       0x000000010b05baaa _T0SS5writeyxz2to_ts16TextOutputStreamRzlFTfq4nx_n + 74
12 libswiftCore.dylib       0x000000010af4d779 _T0SS5writeyxz2to_ts16TextOutputStreamRzlF + 9
13 libswiftCore.dylib       0x000000010af4da37 _T0SSs20TextOutputStreamablessAAP5writeyqd__z2to_ts0aB6StreamRd__lFTW + 71
14 libswiftCore.dylib       0x000000010b06254a _T0s15_print_unlockedyx_q_zts16TextOutputStreamR_r0_lFTfq4gn_n + 602
15 libswiftCore.dylib       0x000000010b0b6b3c _T0s6_printySayypG_SS9separatorSS10terminatorxz2tots16TextOutputStreamRzlFs04_TeeG0VySSs7_StdoutVG_Tgq5Tfq4ggXgXn_n + 204
16 libswiftCore.dylib       0x000000010b0b7508 _T0s5printySayypGd_SS9separatorSS10terminatortFTfq4ggXgX_nTm + 200
17 libswiftCore.dylib       0x000000010b0b6e1d _T0s5printySayypGd_SS9separatorSS10terminatortFTfq4ggXgX_n + 45
18 libswiftCore.dylib       0x000000010af544dd _T0s5printySayypGd_SS9separatorSS10terminatortF + 29
19 libswiftCore.dylib       0x000000010876c141 _T0s5printySayypGd_SS9separatorSS10terminatortF + 4253121665
20 swift                    0x0000000102b4d32f llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 927
21 swift                    0x0000000102b503bc llvm::ExecutionEngine::runFunctionAsMain(llvm::Function*, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, char const* const*) + 1180
22 swift                    0x0000000102179281 swift::RunImmediately(swift::CompilerInstance&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, swift::IRGenOptions&, swift::SILOptions const&) + 3073
23 swift                    0x000000010214ce19 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 11433
24 swift                    0x000000010214909a swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 2650
25 swift                    0x0000000102105b70 main + 3312
26 libdyld.dylib            0x00007fffc0adc235 start + 1
Stack dump:
0.  Program arguments: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2017-05-27-a.xctoolchain/usr/bin/swift -frontend -interpret test.swift -enable-objc-interop -sdk /Applications/Xcode8.3.2.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -module-name test 
zsh: segmentation fault  xcrun --toolchain org.swift.3020170527a swift test.swift

If A doesn't inherit from NSObject, then this works as expected.

@mdiep
Copy link
Contributor Author

mdiep commented May 28, 2017

This seems like it might be an issue with bridged types.

If I use an Int instead of a String, it doesn't crash, but I get the wrong answer.

import Foundation

class A: NSObject {
    let b: Int
    
    init(b: Int) {
        self.b = b
    }
}

let a = A(b: 5)
// This prints 7310525559249068368 when A is an NSObject.
// Remove inheritance from NSObject and it correctly prints 5.
print(a[keyPath: \A.b])

@belkadan
Copy link
Contributor

@jckarter?

@jckarter
Copy link
Member

@swift-ci create

@jckarter
Copy link
Member

PR for master: #10302
PR for 4.0: #10303

@tkrajacic
Copy link

@jckarter is this in Xcode beta 3?

Because I have a similar crasher when using the older #keyPath(…) with NSObject subclasses. (If the keypath is 'malformed' and contains `Self` for example)

Without NSObject inheritance, I get a correct compiler error about the mistake – with it only a segfault.

I can open a new bugreport if you like for it.

// Remove NSObject inheritance and it doesn't crash
class Muppet: NSObject {
    var identifier: String = "Kermit"
    
    static func predicateForIdentifier(_ id: String) -> NSPredicate {
        return NSPredicate(format: "%K = %@", #keyPath(Self.identifier), id)
    }
}

@jckarter
Copy link
Member

The string-based #keyPath syntax is unrelated. You should file another bug.

@tkrajacic
Copy link

There you go SR-5454

@jckarter
Copy link
Member

This should be fixed in the 4.0 branch now.

@mdiep
Copy link
Contributor Author

mdiep commented Aug 15, 2017

Verified with Xcode 9.0.0 ß5.

@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 run-time crash Bug → crash: Swift code crashed during execution
Projects
None yet
Development

No branches or pull requests

5 participants