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

swift_newtype + (Equatable & import Foundation) cause compiler crash on Linux #71086

Open
Kyle-Ye opened this issue Jan 23, 2024 · 3 comments
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels

Comments

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Jan 23, 2024

Description

For a C target symbol imported as Swift struct, Equatable + import Foundation cause a compiler crash on Linux

Reproduction

Bug reproduce step:

  1. Add a C target define uint32 as a struct

    typedef uint32_t CAttribute __attribute((swift_newtype(struct)));
  2. import the C module and use the type in our Swift code. If we add Equatable to our Swift type and have import Foundation statement in the same module, we'll get a compiler crash here.

    import CDemoKit
    import Foundation
    
    public struct Demo {
        var attribute: CAttribute
    }
    extension Demo: Equatable {}

Comment on Line 2 or Line 7 will not crash the compiler.

Or you can just download DemoKit.zip here and run swift build.

DemoKit.zip

Stack dump

swift build
Building for debugging...
error: compile command failed due to signal 6 (use -v to see invocation)
Cannot look up associated type for imported conformance:
(struct_type decl=CDemoKit.(file).CAttribute)
(associated_type_decl "_ObjectiveCType" access=public overridden=)
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend -frontend -c -primary-file /home/kyle/tmp/DemoKit/Sources/DemoKit/DemoKit.swift -emit-dependencies-path /home/kyle/tmp/DemoKit/.build/aarch64-unknown-linux-gnu/debug/DemoKit.build/DemoKit.d -emit-reference-dependencies-path /home/kyle/tmp/DemoKit/.build/aarch64-unknown-linux-gnu/debug/DemoKit.build/DemoKit.swiftdeps -target aarch64-unknown-linux-gnu -Xllvm -aarch64-use-tbi -disable-objc-interop -I /home/kyle/tmp/DemoKit/.build/aarch64-unknown-linux-gnu/debug -color-diagnostics -enable-testing -g -module-cache-path /home/kyle/tmp/DemoKit/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/home/kyle/tmp/DemoKit/.build/aarch64-unknown-linux-gnu/debug/CDemoKit.build/module.modulemap -Xcc -I -Xcc /home/kyle/tmp/DemoKit/Sources/CDemoKit/include -Xcc -fPIC -Xcc -fno-omit-frame-pointer -module-name DemoKit -package-name demokit -plugin-path /home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/lib/swift/host/plugins -plugin-path /home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/local/lib/swift/host/plugins -parse-as-library -o /home/kyle/tmp/DemoKit/.build/aarch64-unknown-linux-gnu/debug/DemoKit.build/DemoKit.swift.o -index-store-path /home/kyle/tmp/DemoKit/.build/aarch64-unknown-linux-gnu/debug/index/store -index-system-modules
1.	Swift version 5.9.2 (swift-5.9.2-RELEASE)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for file "/home/kyle/tmp/DemoKit/Sources/DemoKit/DemoKit.swift")
4.	While generating SIL witness table protocol conformance to '_ObjectiveCBridgeable' (in module 'Swift') for type 'CAttribute'
5.	While completing import of protocol conformance to '_ObjectiveCBridgeable' (in module 'Swift') for type 'CAttribute'
Stack dump without symbol names (ensure you have llvm-symbolizer in your PATH or set the environment var `LLVM_SYMBOLIZER_PATH` to point to it):
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x592f56c)[0xaaaaebe5f56c]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x592d748)[0xaaaaebe5d748]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x592f988)[0xaaaaebe5f988]
linux-vdso.so.1(__kernel_rt_sigreturn+0x0)[0xffff865f17a0]
/lib/aarch64-linux-gnu/libc.so.6(+0x7f200)[0xffff84cbf200]
/lib/aarch64-linux-gnu/libc.so.6(raise+0x1c)[0xffff84c7a67c]
/lib/aarch64-linux-gnu/libc.so.6(abort+0xe4)[0xffff84c67130]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x1d730a0)[0xaaaae82a30a0]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x210638c)[0xaaaae863638c]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x21061d0)[0xaaaae86361d0]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x21068d0)[0xaaaae86368d0]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x11ef9a4)[0xaaaae771f9a4]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x11ed510)[0xaaaae771d510]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x115b83c)[0xaaaae768b83c]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x11e4884)[0xaaaae7714884]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x115d924)[0xaaaae768d924]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0x115b9a8)[0xaaaae768b9a8]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0xc18d84)[0xaaaae7148d84]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0xc1b338)[0xaaaae714b338]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0xc1a4e4)[0xaaaae714a4e4]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0xa95444)[0xaaaae6fc5444]
/lib/aarch64-linux-gnu/libc.so.6(+0x273fc)[0xffff84c673fc]
/lib/aarch64-linux-gnu/libc.so.6(__libc_start_main+0x98)[0xffff84c674cc]
/home/kyle/.swiftbox/toolchain/swift-5.9.2/usr/bin/swift-frontend(+0xa94330)[0xaaaae6fc4330]

Expected behavior

Compile normally as the same on iOS and macOS.

Environment

$ swiftc --version
Swift version 5.9.2 (swift-5.9.2-RELEASE)
Target: aarch64-unknown-linux-gnu
$ uname -a
Linux ubuntu-2204 6.5.13-orbstack-00121-ge428743e4e98 #1 SMP Wed Dec 27 10:22:46 UTC 2023 aarch64 aarch64 aarch64 GNU/Linux

Additional information

Forums link: https://forums.swift.org/t/69577

@Kyle-Ye Kyle-Ye added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels labels Jan 23, 2024
Kyle-Ye added a commit to OpenSwiftUIProject/OpenGraph that referenced this issue Jan 23, 2024
Kyle-Ye added a commit to OpenSwiftUIProject/OpenGraph that referenced this issue Jan 23, 2024
@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Mar 24, 2024

Checking the code, it looks like we should not trigger _ObjectiveCBridgeable conformance on non-ObjectiveC platform.

https://github.com/apple/swift/blob/b8ca381d1400942612a4397a14851e223a694c45/stdlib/public/core/NewtypeWrapper.swift#L101-L102

@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Apr 1, 2024

Full backtrace on swift-release-5.10.

export LLVM_SYMBOLIZER_PATH=/home/kyle/swift-project/build/buildbot_linux/llvm-linux-aarch64/bin/llvm-symbolizer

kyle@builder:~/DemoKit$ SWIFT_EXEC=/home/kyle/swift-nightly-install/usr/bin/swiftc swift build
Building for debugging...
error: emit-module command failed due to signal 6 (use -v to see invocation)
error: compile command failed due to signal 6 (use -v to see invocation)
Cannot look up associated type for imported conformance:
(struct_type decl=CDemoKit.(file).CAttribute)
(associated_type_decl "_ObjectiveCType" access=public overridden=)
Please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the crash backtrace.
Stack dump:
0.	Program arguments: /home/kyle/swift-nightly-install/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift -target aarch64-unknown-linux-gnu -Xllvm -aarch64-use-tbi -disable-objc-interop -I /home/kyle/DemoKit/.build/aarch64-unknown-linux-gnu/debug -color-diagnostics -enable-testing -g -module-cache-path /home/kyle/DemoKit/.build/aarch64-unknown-linux-gnu/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /home/kyle/swift-nightly-install/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /home/kyle/swift-nightly-install/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/home/kyle/DemoKit/.build/aarch64-unknown-linux-gnu/debug/CDemoKit.build/module.modulemap -Xcc -I -Xcc /home/kyle/DemoKit/Sources/CDemoKit/include -Xcc -fPIC -Xcc -g -Xcc -fno-omit-frame-pointer -module-name DemoKit -package-name demokit -plugin-path /home/kyle/swift-nightly-install/usr/lib/swift/host/plugins -plugin-path /home/kyle/swift-nightly-install/usr/local/lib/swift/host/plugins -emit-module-doc-path /home/kyle/DemoKit/.build/aarch64-unknown-linux-gnu/debug/DemoKit.swiftdoc -emit-module-source-info-path /home/kyle/DemoKit/.build/aarch64-unknown-linux-gnu/debug/DemoKit.swiftsourceinfo -emit-dependencies-path /home/kyle/DemoKit/.build/aarch64-unknown-linux-gnu/debug/DemoKit.build/DemoKit.emit-module.d -parse-as-library -o /home/kyle/DemoKit/.build/aarch64-unknown-linux-gnu/debug/DemoKit.swiftmodule
1.	Swift version 5.10-dev (LLVM 5dc9d563e5a6cd2, Swift 17ca88c94a34b34)
2.	Compiling with the current language version
3.	While evaluating request ASTLoweringRequest(Lowering AST to SIL for module DemoKit)
4.	While generating SIL witness table protocol conformance to '_ObjectiveCBridgeable' (in module 'Swift') for type 'CAttribute'
5.	While completing import of protocol conformance to '_ObjectiveCBridgeable' (in module 'Swift') for type 'CAttribute'
 #0 0x0000aaaaed669580 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0x5a99580)
 #1 0x0000aaaaed6677a8 llvm::sys::RunSignalHandlers() (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0x5a977a8)
 #2 0x0000aaaaed66999c SignalHandler(int) Signals.cpp:0:0
 #3 0x0000ffff8afbc860 (linux-vdso.so.1+0x860)
 #4 0x0000ffff896cf200 (/lib/aarch64-linux-gnu/libc.so.6+0x7f200)
 #5 0x0000ffff8968a67c gsignal (/lib/aarch64-linux-gnu/libc.so.6+0x3a67c)
 #6 0x0000ffff89677130 abort (/lib/aarch64-linux-gnu/libc.so.6+0x27130)
 #7 0x0000aaaae99938a4 non-virtual thunk to swift::ClangImporter::Implementation::finishNormalConformance(swift::NormalProtocolConformance*, unsigned long) crtstuff.c:0:0
 #8 0x0000aaaae9db5778 swift::NormalProtocolConformance::getTypeWitnessAndDecl(swift::AssociatedTypeDecl*, swift::SubstOptions) const (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0x21e5778)
 #9 0x0000aaaae9db55bc swift::ProtocolConformance::getTypeWitnessAndDecl(swift::AssociatedTypeDecl*, swift::SubstOptions) const (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0x21e55bc)
#10 0x0000aaaae9db5c4c swift::ProtocolConformance::getTypeWitness(swift::AssociatedTypeDecl*, swift::SubstOptions) const (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0x21e5c4c)
#11 0x0000aaaae8e1799c swift::SILWitnessVisitor<(anonymous namespace)::SILGenConformance>::visitProtocolDecl(swift::ProtocolDecl*) SILGenType.cpp:0:0
#12 0x0000aaaae8e155a0 swift::Lowering::SILGenModule::getWitnessTable(swift::NormalProtocolConformance*) crtstuff.c:0:0
#13 0x0000aaaae8d7f9a0 swift::ASTLoweringRequest::evaluate(swift::Evaluator&, swift::ASTLoweringDescriptor) const (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0x11af9a0)
#14 0x0000aaaae8e0ad48 std::unique_ptr<swift::SILModule, std::default_delete<swift::SILModule>> swift::SimpleRequest<swift::ASTLoweringRequest, std::unique_ptr<swift::SILModule, std::default_delete<swift::SILModule>> (swift::ASTLoweringDescriptor), (swift::RequestFlags)9>::callDerived<0ul>(swift::Evaluator&, std::integer_sequence<unsigned long, 0ul>) const crtstuff.c:0:0
#15 0x0000aaaae8d81c68 llvm::Expected<swift::ASTLoweringRequest::OutputType> swift::Evaluator::getResultUncached<swift::ASTLoweringRequest>(swift::ASTLoweringRequest const&) crtstuff.c:0:0
#16 0x0000aaaae8d7fa70 swift::performASTLowering(swift::ModuleDecl*, swift::Lowering::TypeConverter&, swift::SILOptions const&, swift::IRGenOptions const*) (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0x11afa70)
#17 0x0000aaaae881f4c8 swift::performCompileStepsPostSema(swift::CompilerInstance&, int&, swift::FrontendObserver*) (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0xc4f4c8)
#18 0x0000aaaae8822190 performCompile(swift::CompilerInstance&, int&, swift::FrontendObserver*) FrontendTool.cpp:0:0
#19 0x0000aaaae8820d00 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0xc50d00)
#20 0x0000aaaae86f7ccc swift::mainEntry(int, char const**) (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0xb27ccc)
#21 0x0000ffff896773fc (/lib/aarch64-linux-gnu/libc.so.6+0x273fc)
#22 0x0000ffff896774cc __libc_start_main (/lib/aarch64-linux-gnu/libc.so.6+0x274cc)
#23 0x0000aaaae86f6c30 _start (/home/kyle/swift-nightly-install/usr/bin/swift-frontend+0xb26c30)

A newbie on debugging Swift compiler on Linux. Hope someone can help investigate the issue.

@Kyle-Ye Kyle-Ye changed the title Equatable + import Foundation cause compiler crash on Linux swift_newtype + (Equatable & import Foundation) cause compiler crash on Linux Apr 1, 2024
@Kyle-Ye
Copy link
Contributor Author

Kyle-Ye commented Apr 3, 2024

Following the Debugging The Compiler.md doc, I first run -dump-ast to get the AST. And it looks normal.

(source_file "/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift"
  (import_decl range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:1:1 - line:1:8] 'CDemoKit')
  (import_decl range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:2:1 - line:2:8] 'Foundation')
  (struct_decl range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:4:8 - line:6:1] "Demo" interface type='Demo.Type' access=public non-resilient
    (pattern_binding_decl range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:4 - line:5:19]
      (pattern_typed type='CAttribute'
        (pattern_named type='CAttribute' 'attribute')
        (type_ident id='CAttribute' bind=CDemoKit.(file).CAttribute)))
    (var_decl range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 - line:5:8] "attribute" interface type='CAttribute' access=internal readImpl=stored writeImpl=stored readWriteImpl=stored
      (accessor_decl implicit range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 - line:5:8] 'anonname=0xaaab1be09da8' interface type='(Demo) -> () -> CAttribute' access=internal get_for=attribute
        (parameter "self" interface type='Demo')
        (parameter_list)
        (brace_stmt implicit range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 - line:5:8]
          (return_stmt implicit
            (member_ref_expr implicit type='CAttribute' decl=a.(file).Demo.attribute@/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 direct_to_storage
              (declref_expr implicit type='Demo' decl=a.(file).Demo.<anonymous>.self@/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 function_ref=unapplied)))))
      (accessor_decl implicit range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 - line:5:8] 'anonname=0xaaab1be0a018' interface type='(inout Demo) -> (CAttribute) -> ()' access=internal set_for=attribute
        (parameter "self" interface type='Demo')
        (parameter_list range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 - line:5:8]
          (parameter "value" interface type='CAttribute'))
        (brace_stmt implicit range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 - line:5:8]
          (assign_expr implicit type='()'
            (member_ref_expr implicit type='@lvalue CAttribute' decl=a.(file).Demo.attribute@/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 direct_to_storage
              (declref_expr implicit type='@lvalue Demo' decl=a.(file).Demo.<anonymous>.self@/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 function_ref=unapplied))
            (declref_expr implicit type='CAttribute' decl=a.(file).Demo.<anonymous>.value@/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 function_ref=unapplied))))
      (accessor_decl implicit range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 - line:5:8] 'anonname=0xaaab1be0a1e8' interface type='(inout Demo) -> () -> ()' access=internal _modify_for=attribute
        (parameter "self" interface type='Demo')
        (parameter_list)
        (brace_stmt implicit range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 - line:5:8]
          (yield_stmt implicit range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 - line:5:8]
            (inout_expr implicit type='inout CAttribute'
              (member_ref_expr implicit type='@lvalue CAttribute' decl=a.(file).Demo.attribute@/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 direct_to_impl
                (declref_expr implicit type='@lvalue Demo' decl=a.(file).Demo.<anonymous>.self@/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:5:8 function_ref=unapplied)))))))
    (constructor_decl implicit range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:4:15 - line:4:15] "init(attribute:)" interface type='(Demo.Type) -> (CAttribute) -> Demo' access=internal designated
      (parameter "self")
      (parameter_list range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:4:15 - line:4:15]
        (parameter "attribute" apiName=attribute interface type='CAttribute'))))
  (extension_decl range=[/home/kyle/DemoKit/Sources/DemoKit/DemoKit.swift:7:1 - line:7:28] Demo inherits: Equatable
    (func_decl implicit "__derived_struct_equals(_:_:)" interface type='(Demo.Type) -> (Demo, Demo) -> Bool' access=public type
      (parameter "self")
      (parameter_list
        (parameter "a" interface type='Demo')
        (parameter "b" interface type='Demo')))))

Then I run -emit-silgen and it crashed for the same error output.

Then the issue is happening on ast to sil

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. crash Bug: A crash, i.e., an abnormal termination of software triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant