Skip to content

[SR-2592] Crash in swift::constraints::ConstraintSystem::solveRec #45197

@aschwaighofer

Description

@aschwaighofer
Previous ID SR-2592
Radar rdar://28209082
Original Reporter @aschwaighofer
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, CompilerCrash
Assignee @slavapestov
Priority Medium

md5: 7beb58e8ade83ff8f30a4b02bdfd7068

Issue Description:

$ cat TypeCheckerBug.swift 
import Foundation

public protocol CacheType {
    associatedtype Key
    associatedtype Value

    /// Retrieves the value for this key.
    func valueForKey(_ key: Key) -> Value?

    /// Sets a value for a key. If `value` is `nil`, it will be removed.
    func setValue(_ value: Value?, forKey key: Key)
}

// MARK: -

/// `CacheType` backed by `NSCache`.
public final class InMemoryCache<K: Hashable, V>: CacheType {
    private typealias NativeCacheType = NSCache<CacheKey<K>, CacheValue<V>>

    private let cache: NativeCacheType

    public init(cacheName: String) {
        self.cache = NativeCacheType()
        self.cache.name = cacheName
    }

    public func valueForKey(_ key: K) -> V? {
        /*let o = cache.object(forKey: CacheKey(value: key))
        if let v = o {
          return v.value
        }
        return nil*/
        return cache.object(forKey: CacheKey(value: key))?.value
    }

    public func setValue(_ value: V?, forKey key: K) {
        let key = CacheKey(value: key)

        if let value = value.map(CacheValue.init) {
            cache.setObject(value, forKey: key)
        } else {
            cache.removeObject(forKey: key)
        }
    }
}

private final class CacheValue<V>: NSObject {
    private let value: V

    init(value: V) {
        self.value = value
    }
}

private final class CacheKey<K: Hashable>: NSObject {
    private let value: K
    private let cachedHash: Int

    init(value: K) {
        self.value = value
        self.cachedHash = value.hashValue

        super.init()
    }

    private override func isEqual(_ object: Any?) -> Bool {
        if let otherData = object as? CacheKey<K> {
            return otherData.value == self.value
        } else {
            return false
        }
    }

    private override var hash: Int {
        return self.cachedHash
    }
}

$ xcrun swiftc -v
Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38)
Target: x86_64-apple-macosx10.9

$ xcrun -sdk iphoneos swiftc -target arm64-apple-ios10.0 TypeCheckerBug.swift 
0  swift                    0x000000010d133b6d PrintStackTraceSignalHandler(void*) + 45
1  swift                    0x000000010d1335b6 SignalHandler(int) + 470
2  libsystem_platform.dylib 0x00007fffe1d6901a _sigtramp + 26
3  libsystem_platform.dylib 0x00007fff55722208 _sigtramp + 1939575304
4  swift                    0x000000010ad5e3d9 swift::constraints::ConstraintSystem::solveRec(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) + 57
5  swift                    0x000000010ad676b4 swift::constraints::ConstraintSystem::solveSimplified(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) + 23892
6  swift                    0x000000010ad5e650 swift::constraints::ConstraintSystem::solveRec(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) + 688
7  swift                    0x000000010ad5df92 swift::constraints::ConstraintSystem::solve(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::FreeTypeVariableBinding) + 66
8  swift                    0x000000010ad5dec5 swift::constraints::ConstraintSystem::solveSingle(swift::FreeTypeVariableBinding) + 69
9  swift                    0x000000010adafd34 swift::TypeChecker::typesSatisfyConstraint(swift::Type, swift::Type, swift::constraints::ConstraintKind, swift::DeclContext*) + 132
10 swift                    0x000000010adafdce swift::TypeChecker::typeCheckCheckedCast(swift::Type, swift::Type, swift::DeclContext*, swift::SourceLoc, swift::SourceRange, swift::SourceRange, std::__1::function<bool (swift::Type)>, bool) + 94
11 swift                    0x000000010ad093e0 swift::constraints::ConstraintSystem::diagnoseFailureForExpr(swift::Expr*) + 20032
12 swift                    0x000000010ad2e82f swift::constraints::ConstraintSystem::salvage(llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::Expr*) + 4367
13 swift                    0x000000010ada7e75 swift::TypeChecker::solveForExpression(swift::Expr*&, swift::DeclContext*, swift::Type, swift::FreeTypeVariableBinding, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem&, llvm::SmallVectorImpl<swift::constraints::Solution>&, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>) + 1157
14 swift                    0x000000010adaa59f swift::TypeChecker::typeCheckExpression(swift::Expr*&, swift::DeclContext*, swift::TypeLoc, swift::ContextualTypePurpose, swift::OptionSet<swift::TypeCheckExprFlags, unsigned int>, swift::ExprTypeCheckListener*, swift::constraints::ConstraintSystem*) + 703
15 swift                    0x000000010ae2cbb2 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 6114
16 swift                    0x000000010ae2b516 swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(swift::Stmt*) + 326
17 swift                    0x000000010ae2a5ad swift::TypeChecker::typeCheckFunctionBodyUntil(swift::FuncDecl*, swift::SourceLoc) + 365
18 swift                    0x000000010ae2ecfc swift::TypeChecker::typeCheckAbstractFunctionBody(swift::AbstractFunctionDecl*) + 188
19 swift                    0x000000010ade364a swift::performTypeChecking(swift::SourceFile&, swift::TopLevelContext&, swift::OptionSet<swift::TypeCheckingFlags, unsigned int>, unsigned int, unsigned int) + 6714
20 swift                    0x000000010aa988ef swift::CompilerInstance::performSema() + 5199
21 swift                    0x000000010a54500d performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 2765
22 swift                    0x000000010a542265 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17029
23 swift                    0x000000010a4ff82d main + 8685
24 libdyld.dylib            0x00007fffe1b5d255 start + 1
Stack dump:
0.  Program arguments: /Users/arnold/Desktop/XCodes/Xcode8.0_GM/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -c -primary-file TypeCheckerBug.swift -target arm64-apple-ios10.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -sdk /Users/arnold/Desktop/XCodes/Xcode8.0_GM/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.0.sdk -color-diagnostics -module-name TypeCheckerBug -o /var/folders/zt/3j4wrck57yn1g30gtl0skzhc0000gn/T/TypeCheckerBug-6026f6.o 
1.  While type-checking 'valueForKey' at TypeCheckerBug.swift:27:9
2.  While type-checking expression at [TypeCheckerBug.swift:33:10 - line:33:54] RangeText="cache.object(forKey: CacheKey(value: key))?.v"
<unknown>:0: error: unable to execute command: Segmentation fault: 11
<unknown>:0: error: compile command failed due to signal (use -v to see invocation)

From: https://bugs.swift.org/browse/SR-1951

If you comment out the commented part invalueForKey(:)” you will see the programmer error that happened here:

$ xcrun -sdk iphoneos swiftc -target arm64-apple-ios10.0 TypeCheckerBug.swift 
TypeCheckerBug.swift:30:18: error: ambiguous reference to member 'value(forKey:)'
          return v.value
                 ^
Foundation.NSObject:4:15: note: found this candidate
    open func value(forKey key: String) -> Any?
              ^
Foundation.NSObject:18:15: note: found this candidate
    open func value(forKeyPath keyPath: String) -> Any?
              ^
Foundation.NSObject:32:15: note: found this candidate
    open func value(forUndefinedKey key: String) -> Any?
              ^

Metadata

Metadata

Assignees

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 software

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions