Segfault when trying to compile my version of standard library

I'm using a compiler based on swift 5.1.5 (built from tag swift-5.1.5-RELEASE) and attempting to build my own simple, cut down standard library (for use on bare metal microcontrollers).

It's going OK, but I keep getting segfaults when compiling the code for UnsafeMutableBufferPointer etc.

Here's an example backtrace...

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x8)
  * frame #0: 0x000000010001cddc swift`swift::TypeBase::isCanonical(this=0x0000000000000000) const at Types.h:432:51
    frame #1: 0x00000001022dd919 swift`swift::TypeBase::getCanonicalType(this=0x0000000000000000) const at Types.h:445:9
    frame #2: 0x00000001027df59e swift`swift::TypeBase::isEqual(this=0x00000001202b3cb8, Other=Type @ 0x00007ffeefbf5ea8) at Type.cpp:125:52
    frame #3: 0x00000001017d5713 swift`swift::constraints::ConstraintSystem::matchTypes(this=0x00007ffeefbf9a30, type1=Type @ 0x00007ffeefbf6ab8, type2=Type @ 0x00007ffeefbf6ab0, kind=ArgumentConversion, flags=(Storage = 1), locator=ConstraintLocatorBuilder @ 0x00007ffeefbf6fb0) at CSSimplify.cpp:2995:28
    frame #4: 0x00000001017f150a swift`swift::constraints::ConstraintSystem::addConstraintImpl(this=0x00007ffeefbf9a30, kind=ArgumentConversion, first=Type @ 0x00007ffeefbf7178, second=Type @ 0x00007ffeefbf7170, locator=ConstraintLocatorBuilder @ 0x00007ffeefbf7360, isFavored=false) at CSSimplify.cpp:6883:12
    frame #5: 0x00000001017d1e9c swift`swift::constraints::ConstraintSystem::addConstraint(this=0x00007ffeefbf9a30, kind=ArgumentConversion, first=Type @ 0x00007ffeefbf73e0, second=Type @ 0x00007ffeefbf73d8, locator=ConstraintLocatorBuilder @ 0x00007ffeefbf7430, isFavored=false) at CSSimplify.cpp:7080:11
    frame #6: 0x00000001017d0855 swift`swift::constraints::matchCallArguments(cs=0x00007ffeefbf9a30, args=ArrayRef<swift::AnyFunctionType::Param> @ 0x00007ffeefbf7810, params=ArrayRef<swift::AnyFunctionType::Param> @ 0x00007ffeefbf7800, locator=ConstraintLocatorBuilder @ 0x00007ffeefbf79c0) at CSSimplify.cpp:1010:10
    frame #7: 0x00000001017ed2b2 swift`swift::constraints::ConstraintSystem::simplifyApplicableFnConstraint(this=0x00007ffeefbf9a30, type1=Type @ 0x00007ffeefbf7c28, type2=Type @ 0x00007ffeefbf7c20, flags=(Storage = 1), locator=ConstraintLocatorBuilder @ 0x00007ffeefbf7d90) at CSSimplify.cpp:5977:9
    frame #8: 0x00000001017f16ff swift`swift::constraints::ConstraintSystem::addConstraintImpl(this=0x00007ffeefbf9a30, kind=ApplicableFunction, first=Type @ 0x00007ffeefbf7f58, second=Type @ 0x00007ffeefbf7f50, locator=ConstraintLocatorBuilder @ 0x00007ffeefbf8140, isFavored=false) at CSSimplify.cpp:6893:12
    frame #9: 0x00000001017d1e9c swift`swift::constraints::ConstraintSystem::addConstraint(this=0x00007ffeefbf9a30, kind=ApplicableFunction, first=Type @ 0x00007ffeefbf81c0, second=Type @ 0x00007ffeefbf81b8, locator=ConstraintLocatorBuilder @ 0x00007ffeefbf8210, isFavored=false) at CSSimplify.cpp:7080:11
    frame #10: 0x00000001017ae3c4 swift`(anonymous namespace)::ConstraintGenerator::visitApplyExpr(this=0x00007ffeefbf8920, expr=0x000000012007df78) at CSGen.cpp:2474:10
    frame #11: 0x00000001017a4518 swift`swift::ASTVisitor<(anonymous namespace)::ConstraintGenerator, swift::Type, void, void, void, void, void>::visitCallExpr(this=0x00007ffeefbf8920, E=0x000000012007df78) at ExprNodes.def:135:3
    frame #12: 0x000000010179dbf0 swift`swift::ASTVisitor<(anonymous namespace)::ConstraintGenerator, swift::Type, void, void, void, void, void>::visit(this=0x00007ffeefbf8920, E=0x000000012007df78) at ExprNodes.def:135:3
    frame #13: 0x000000010179d3d5 swift`(anonymous namespace)::ConstraintWalker::walkToExprPost(this=0x00007ffeefbf8900, expr=0x000000012007df78) at CSGen.cpp:3529:26
    frame #14: 0x0000000102485ed0 swift`(anonymous namespace)::Traversal::doIt(this=0x00007ffeefbf88a8, E=0x000000012007df78) at ASTWalker.cpp:1178:23
    frame #15: 0x0000000102488750 swift`(anonymous namespace)::Traversal::visitRebindSelfInConstructorExpr(this=0x00007ffeefbf88a8, E=0x00000001202984a0) at ASTWalker.cpp:876:17
    frame #16: 0x0000000102486b12 swift`swift::ASTVisitor<(anonymous namespace)::Traversal, swift::Expr*, swift::Stmt*, bool, swift::Pattern*, bool, void>::visit(this=0x00007ffeefbf88a8, E=0x00000001202984a0) at ExprNodes.def:127:1
    frame #17: 0x0000000102486586 swift`(anonymous namespace)::Traversal::visit(this=0x00007ffeefbf88a8, E=0x00000001202984a0) at ASTWalker.cpp:98:23
    frame #18: 0x0000000102485ead swift`(anonymous namespace)::Traversal::doIt(this=0x00007ffeefbf88a8, E=0x00000001202984a0) at ASTWalker.cpp:1175:9
    frame #19: 0x0000000102485e10 swift`swift::Expr::walk(this=0x00000001202984a0, walker=0x00007ffeefbf8900) at ASTWalker.cpp:1834:28
    frame #20: 0x00000001017964a0 swift`swift::constraints::ConstraintSystem::generateConstraints(this=0x00007ffeefbf9a30, expr=0x00000001202984a0, dc=0x0000000000000000) at CSGen.cpp:3616:24
    frame #21: 0x000000010181b868 swift`swift::constraints::ConstraintSystem::solveImpl(this=0x00007ffeefbf9a30, expr=0x00007ffeefbfa458, convertType=Type @ 0x00007ffeefbf8be8, listener=0x00007ffeefbfa248, solutions=0x00007ffeefbf8e88, allowFreeTypeVariables=Disallow) at CSSolver.cpp:1160:28
    frame #22: 0x000000010181b294 swift`swift::constraints::ConstraintSystem::solve(this=0x00007ffeefbf9a30, expr=0x00007ffeefbfa458, convertType=Type @ 0x00007ffeefbf8d10, listener=0x00007ffeefbfa248, solutions=0x00007ffeefbf8e88, allowFreeTypeVariables=Disallow) at CSSolver.cpp:1080:19
    frame #23: 0x0000000101a96711 swift`swift::TypeChecker::typeCheckExpressionImpl(this=0x000000011f81d010, expr=0x00007ffeefbfa458, dc=0x000000012007d918, convertType=TypeLoc @ 0x00007ffeefbf99e0, convertTypePurpose=CTP_Unused, options=(Storage = 33), listener=0x00007ffeefbfa248, baseCS=0x0000000000000000) at TypeCheckConstraints.cpp:2247:10
    frame #24: 0x0000000101a95f47 swift`swift::TypeChecker::typeCheckExpression(this=0x000000011f81d010, expr=0x00007ffeefbfa458, dc=0x000000012007d918, convertType=TypeLoc @ 0x00007ffeefbfa290, convertTypePurpose=CTP_Unused, options=(Storage = 33), listener=0x0000000000000000, baseCS=0x0000000000000000) at TypeCheckConstraints.cpp:2166:10
    frame #25: 0x0000000101c87289 swift`(anonymous namespace)::StmtChecker::visitBraceStmt(this=0x00007ffeefbfa948, BS=0x00000001202b3c48) at TypeCheckStmt.cpp:1765:14
    frame #26: 0x0000000101c86b7c swift`swift::ASTVisitor<(anonymous namespace)::StmtChecker, void, swift::Stmt*, void, void, void, void>::visit(this=0x00007ffeefbfa948, S=0x00000001202b3c48) at StmtNodes.def:47:1
    frame #27: 0x0000000101c80185 swift`bool (anonymous namespace)::StmtChecker::typeCheckStmt<swift::BraceStmt>(this=0x00007ffeefbfa948, S=0x00007ffeefbfa900) at TypeCheckStmt.cpp:415:39
    frame #28: 0x0000000101c7f07f swift`(anonymous namespace)::StmtChecker::typeCheckBody(this=0x00007ffeefbfa948, S=0x00007ffeefbfa900) at TypeCheckStmt.cpp:425:21
    frame #29: 0x0000000101c7e427 swift`swift::TypeChecker::typeCheckConstructorBodyUntil(this=0x000000011f81d010, ctor=0x000000012007d900, EndTypeCheckLoc=SourceLoc @ 0x00007ffeefbfa918) at TypeCheckStmt.cpp:2139:22
    frame #30: 0x0000000101c7dc89 swift`swift::TypeChecker::typeCheckAbstractFunctionBodyUntil(this=0x000000011f81d010, AFD=0x000000012007d900, EndTypeCheckLoc=SourceLoc @ 0x00007ffeefbfaa40) at TypeCheckStmt.cpp:1919:12
    frame #31: 0x0000000101c7edca swift`swift::TypeChecker::typeCheckAbstractFunctionBody(this=0x000000011f81d010, AFD=0x000000012007d900) at TypeCheckStmt.cpp:1944:16
    frame #32: 0x0000000101cd7779 swift`typeCheckFunctionsAndExternalDecls(SF=0x0000000120057300, TC=0x000000011f81d010) at TypeChecker.cpp:323:10
    frame #33: 0x0000000101cd8735 swift`swift::performTypeChecking(SF=0x0000000120057300, TLC=0x00007ffeefbfb470, Options=(Storage = 0), StartElem=0, WarnLongFunctionBodies=0, WarnLongExpressionTypeChecking=0, ExpressionTimeoutThreshold=0, SwitchCheckingInvocationThreshold=0) at TypeChecker.cpp:534:5
    frame #34: 0x00000001006f1900 swift`swift::CompilerInstance::parseAndCheckTypesUpTo(this=0x00007ffeefbfb278, SF=0x0000000120057300)::$_1::operator()(swift::SourceFile&) const at Frontend.cpp:823:5
    frame #35: 0x00000001006f1860 swift`void llvm::function_ref<void (swift::SourceFile&)>::callback_fn<swift::CompilerInstance::parseAndCheckTypesUpTo(callable=140732920738424, params=0x0000000120057300)::$_1>(long, swift::SourceFile&) at STLExtras.h:120:12
    frame #36: 0x00000001006dc78c swift`llvm::function_ref<void (swift::SourceFile&)>::operator(this=0x00007ffeefbfb200, params=0x0000000120057300)(swift::SourceFile&) const at STLExtras.h:137:12
    frame #37: 0x00000001006dbed1 swift`swift::CompilerInstance::forEachFileToTypeCheck(this=0x0000000121005e00, fn=function_ref<void (swift::SourceFile &)> @ 0x00007ffeefbfb200)>) at Frontend.cpp:1001:7
    frame #38: 0x00000001006d9dc4 swift`swift::CompilerInstance::parseAndCheckTypesUpTo(this=0x0000000121005e00, implicitImports=0x00007ffeefbfb4f0, limitStage=TypeChecked) at Frontend.cpp:822:3
    frame #39: 0x00000001006d8ed8 swift`swift::CompilerInstance::performSemaUpTo(this=0x0000000121005e00, LimitStage=TypeChecked) at Frontend.cpp:667:3
    frame #40: 0x00000001006d8f3a swift`swift::CompilerInstance::performSema(this=0x0000000121005e00) at Frontend.cpp:624:3
    frame #41: 0x000000010011ac8e swift`performCompile(Instance=0x0000000121005e00, Invocation=0x00007ffeefbfbee8, Args=ArrayRef<const char *> @ 0x00007ffeefbfbaa0, ReturnValue=0x00007ffeefbfbc1c, observer=0x0000000000000000, Stats=0x0000000000000000) at FrontendTool.cpp:998:14
    frame #42: 0x0000000100119c6a swift`swift::performFrontend(Args=ArrayRef<const char *> @ 0x00007ffeefbfbe58, Argv0="/Users/carlpeto/avr-swift/build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift", MainAddr=0x0000000100002350, observer=0x0000000000000000) at FrontendTool.cpp:1819:5
    frame #43: 0x0000000100004520 swift`run_driver(ExecName=(Data = "swift", Length = 5), argv=const llvm::ArrayRef<const char *> @ 0x00007ffeefbfcd00) at driver.cpp:125:14
    frame #44: 0x0000000100002e1f swift`main(argc_=53, argv_=0x00007ffeefbff3a0) at driver.cpp:270:12
    frame #45: 0x00007fff752ac3d5 libdyld.dylib`start + 1
(lldb) fr s 15
frame #15: 0x0000000102488750 swift`(anonymous namespace)::Traversal::visitRebindSelfInConstructorExpr(this=0x00007ffeefbf88a8, E=0x00000001202984a0) at ASTWalker.cpp:876:17
   861 	  Expr *visitArrowExpr(ArrowExpr *E) {
   862 	    if (Expr *Args = E->getArgsExpr()) {
   863 	      Args = doIt(Args);
   864 	      if (!Args) return nullptr;
   865 	      E->setArgsExpr(Args);
   866 	    }
   867 	    if (Expr *Result = E->getResultExpr()) {
   868 	      Result = doIt(Result);
   869 	      if (!Result) return nullptr;
   870 	      E->setResultExpr(Result);
   871 	    }
   872 	    return E;
   873 	  }
   874 	
   875 	  Expr *visitRebindSelfInConstructorExpr(RebindSelfInConstructorExpr *E) {
-> 876 	    Expr *Sub = doIt(E->getSubExpr());
   877 	    if (!Sub) return nullptr;
   878 	    E->setSubExpr(Sub);
   879 	    
   880 	    return E;
   881 	  }
   882 	  
   883 	  Expr *visitAssignExpr(AssignExpr *AE) {
   884 	    if (Expr *Dest = AE->getDest()) {
   885 	      if (!(Dest = doIt(Dest)))
   886 	        return nullptr;
   887 	      AE->setDest(Dest);
   888 	    }
   889 	
   890 	    if (Expr *Src = AE->getSrc()) {
   891 	      if (!(Src = doIt(AE->getSrc())))
(lldb) e E->dump()
(rebind_self_in_constructor_expr implicit type='()' location=UnsafeBufferPointer.swift:951:10 range=[UnsafeBufferPointer.swift:951:5 - line:951:46]
  (call_expr type='<null>' arg_labels=start:count:
    (unresolved_dot_expr type='<null>' field 'init' function_ref=single
      (declref_expr type='<null>' decl=Swift.(file).UnsafeBufferPointer extension.init(rebasing:).self@UnsafeBufferPointer.swift:949:10 function_ref=unapplied))
    (tuple_expr type='<null>' names=start,count
      (declref_expr type='<null>' decl=Swift.(file).UnsafeBufferPointer extension.init(rebasing:).base@UnsafeBufferPointer.swift:950:9 function_ref=unapplied)
      (unresolved_dot_expr type='<null>' field 'count' function_ref=unapplied
        (declref_expr type='<null>' decl=Swift.(file).UnsafeBufferPointer extension.init(rebasing:).slice@UnsafeBufferPointer.swift:949:24 function_ref=unapplied)))))
(lldb) 

It looks like this is the code it is attempting to digest...

extension UnsafeBufferPointer {
...
  @inlinable // unsafe-performance
  public init(rebasing slice: Slice<UnsafeMutableBufferPointer<Element>>) {
    let base = slice.base.baseAddress?.advanced(by: slice.startIndex)
    self.init(start: base, count: slice.count)
  }

I have stripped out most of the stdlib and added it back to my compile a bit at a time so it's likely that functions, types, etc. are missing but it should cause a well formed error rather than a compiler crash.

My guess is that the compiler is unable to compile this code due to an error, a missing type, etc. but the problem is not trapped because compiling a custom standard library module is such an unusual thing to do.

Can anyone give advice how to start figuring out what the real underlying problem is or, if it's a genuine bug in this version of the compiler, how to track down the bug please?

Without knowing exactly what you changed in your stdlib, it's hard to tell what the problem is, but most likely the compiler is unable to find a declaration of a nominal type that it expects to find, such as Array, Dictionary, Optional, etc.

Thanks so much Slava for helping with my crazy experiments. :slight_smile:

Array and Dictionary have been (temporarily) removed. Optional is still there.

The Swift code for UnsafeBufferPointer doesn’t reference Array or Dictionary either directly Array<> or in sugared form []. So presumably there’s a compiler intrinsic at work.

What is not obvious is how it ties to this type. Is there something deep in the compiler that says “aha, you are attempting to define UnsafeBufferPointer so I will automatically go looking for Array and Dictionary“, fails to find them, leaves a pointer null then borks during constraint resolutions?

I’ve been able to define other types in a very simple minimal standard library before without getting this error.

Is it just the pointer classes? Or the pointer buffer classes? Which bit of Sema has this magic code?

Looks like this is the function causing the crash...

frame #3: 0x00000001017d5713 swiftswift::constraints::ConstraintSystem::matchTypes(this=0x00007ffeefbf8a50, type1=Type @ 0x00007ffeefbf5a08, type2=Type @ 0x00007ffeefbf5a00, kind=ArgumentConversion, flags=(Storage = 1), locator=ConstraintLocatorBuilder @ 0x00007ffeefbf5f00) at CSSimplify.cpp:2995:28`

The code at the crash site is...

(lldb) fr s 3
frame #3: 0x00000001017d5713 swift`swift::constraints::ConstraintSystem::matchTypes(this=0x00007ffeefbf8a50, type1=Type @ 0x00007ffeefbf5a08, type2=Type @ 0x00007ffeefbf5a00, kind=ArgumentConversion, flags=(Storage = 1), locator=ConstraintLocatorBuilder @ 0x00007ffeefbf5f00) at CSSimplify.cpp:2995:28
   2980	              }
   2981	            }
   2982	            // UnsafePointer and UnsafeRawPointer can also be converted from an
   2983	            // array or string value, or a UnsafePointer or
   2984	            // AutoreleasingUnsafeMutablePointer.
   2985	            if (pointerKind == PTK_UnsafePointer
   2986	                || pointerKind == PTK_UnsafeRawPointer) {
   2987	              if (!isAutoClosureArgument) {
   2988	                if (isArrayType(type1)) {
   2989	                  conversionsOrFixes.push_back(
   2990	                      ConversionRestrictionKind::ArrayToPointer);
   2991	                }
   2992	
   2993	                // The pointer can be converted from a string, if the element
   2994	                // type is compatible.
-> 2995	                if (type1->isEqual(TC.getStringType(DC))) {
   2996	                  auto baseTy = getFixedTypeRecursive(pointeeTy, false);
   2997	
   2998	                  if (baseTy->isTypeVariableOrMember() ||
   2999	                      isStringCompatiblePointerBaseType(TC, DC, baseTy))
   3000	                    conversionsOrFixes.push_back(
   3001	                        ConversionRestrictionKind::StringToPointer);
   3002	                }
   3003	              }
   3004	              
   3005	              if (type1IsPointer && optionalityMatches &&
   3006	                  (type1PointerKind == PTK_UnsafePointer ||
   3007	                   type1PointerKind == PTK_AutoreleasingUnsafeMutablePointer)) {
   3008	                conversionsOrFixes.push_back(
   3009	                                   ConversionRestrictionKind::PointerToPointer);
   3010	              }
(lldb) e type1->dump()

TC.getStringType(DC) returns nil because I don't have a String type in my standard library (yet)!

Hence the crash.

p.s. i didn't finish this up, I'm going to submit a minor patch to check for the existence of String

Terms of Service

Privacy Policy

Cookie Policy