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-5427] Crash in generated code when calling optional closure that throws #48001

Closed
tjw opened this issue Jul 11, 2017 · 7 comments
Closed
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

@tjw
Copy link
Contributor

tjw commented Jul 11, 2017

Previous ID SR-5427
Radar rdar://problem/33227757
Original Reporter @tjw
Type Bug
Status Closed
Resolution Done
Environment

Xcode 9.0 b3 (looks like this crashed back to Xcode 8.3.2 at least, too)

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

md5: b38027066c504d9766b108b13094cd0f

is duplicated by:

  • SR-7551 apply expression is not marked as throwing or non-throwing crash

relates to:

  • SR-9421 Passing an optional throwing block from one rethrowing func to another fails

Issue Description:

This produces a binary that crashes:

struct E : Error {}
func f(op: (() throws -> Void)?) rethrows { try op?() }
try? f(op: { throw E() })

with:

xcrun swift optional-throwing-block.swift
0  swift                    0x00000001041a97fa PrintStackTraceSignalHandler(void*) + 42
1  swift                    0x00000001041a8c36 SignalHandler(int) + 662
2  libsystem_platform.dylib 0x00007fffc5c7ab3a _sigtramp + 26
3  libsystem_platform.dylib 0x5f37457350726f72 _sigtramp + 2326447186
4  swift                    0x000000010173a62f llvm::MCJIT::runFunction(llvm::Function*, llvm::ArrayRef<llvm::GenericValue>) + 655
5  swift                    0x0000000101740893 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*) + 707
6  swift                    0x0000000100b36710 performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*, swift::UnifiedStatsReporter*) + 22720
7  swift                    0x0000000100b2f5e4 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 7332
8  swift                    0x0000000100ae4ee8 main + 12248
9  libdyld.dylib            0x00007fffc5a6b235 start + 1
Stack dump:
0.  Program arguments: /Applications/Xcode-9.0b3.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret optional-throwing-block.swift -enable-objc-interop -sdk /Applications/Xcode-9.0b3.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -color-diagnostics -module-name main 
zsh: bus error  xcrun swift optional-throwing-block.swift

If the `op` is not marked Optional, no crash is generated.

@belkadan
Copy link
Contributor

There's a very suspicious unreachable in the generated SIL here…

// main
sil @main : $@convention(c) (Int32, UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>) -> Int32 {
bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>>):
  // function_ref f(op:)
  %2 = function_ref @_T04main1fyyyKcSg2op_tKF : $@convention(thin) (@owned Optional<@callee_owned () -> @error Error>) -> @error Error // user: %6
  // function_ref closure #&#8203;1 in 
  %3 = function_ref @_T04mainyyKcfU_ : $@convention(thin) () -> @error Error // user: %4
  %4 = thin_to_thick_function %3 : $@convention(thin) () -> @error Error to $@callee_owned () -> @error Error // user: %5
  %5 = enum $Optional<@callee_owned () -> @error Error>, #Optional.some!enumelt.1, %4 : $@callee_owned () -> @error Error // user: %6
  try_apply %2(%5) : $@convention(thin) (@owned Optional<@callee_owned () -> @error Error>) -> @error Error, normal bb1, error bb2 // id: %6

bb1(%7 : $()):                                    // Preds: bb0
  %8 = tuple ()                                   // user: %9
  %9 = enum $Optional<()>, #Optional.some!enumelt.1, %8 : $()
  %10 = integer_literal $Builtin.Int32, 0         // user: %11
  %11 = struct $Int32 (%10 : $Builtin.Int32)      // user: %12
  return %11 : $Int32                             // id: %12

bb2(%13 : $Error):                                // Preds: bb0
  unreachable                                     // id: %14
} // end sil function 'main'

@swift-ci create

@belkadan
Copy link
Contributor

Still occurs, but now a +Asserts build complains in AST verification:

apply expression is not marked as throwing or non-throwing
(call_expr type='()' location=<stdin>:3:6 range=[<stdin>:3:6 - line:3:25] arg_labels=op:
  (declref_expr type='((() throws -> Void)?) throws -> ()' location=<stdin>:3:6 range=[<stdin>:3:6 - line:3:6] decl=main.(file).f(op:)@<stdin>:2:6 function_ref=single)
  (tuple_expr type='(op: (() throws -> Void)?)' location=<stdin>:3:7 range=[<stdin>:3:7 - line:3:25] names=op
    (inject_into_optional implicit type='(() throws -> Void)?' location=<stdin>:3:12 range=[<stdin>:3:12 - line:3:24]
      (closure_expr type='() throws -> Void' location=<stdin>:3:12 range=[<stdin>:3:12 - line:3:24] discriminator=0
        (parameter_list range=[<stdin>:3:12 - line:3:12])
        (brace_stmt range=[<stdin>:3:12 - line:3:24]
          (throw_stmt range=[<stdin>:3:14 - line:3:22]
            (erasure_expr implicit type='Error' location=<stdin>:3:20 range=[<stdin>:3:20 - line:3:22]
              (normal_conformance type=E protocol=Error
                (value req=_domain witness=Swift.(file).Error._domain)
                (value req=_code witness=Swift.(file).Error._code)
                (value req=_userInfo witness=Swift.(file).Error._userInfo)
                (value req=_getEmbeddedNSError() witness=Swift.(file).Error._getEmbeddedNSError()))
              (call_expr type='E' location=<stdin>:3:20 range=[<stdin>:3:20 - line:3:22] nothrow arg_labels=
                (constructor_ref_call_expr type='() -> E' location=<stdin>:3:20 range=[<stdin>:3:20 - line:3:20] nothrow
                  (declref_expr implicit type='(E.Type) -> () -> E' location=<stdin>:3:20 range=[<stdin>:3:20 - line:3:20] decl=main.(file).E.init()@<stdin>:1:8 function_ref=single)
                  (type_expr type='E.Type' location=<stdin>:3:20 range=[<stdin>:3:20 - line:3:20] typerepr='E'))
                (tuple_expr type='()' location=<stdin>:3:21 range=[<stdin>:3:21 - line:3:22])))))))))

cc @jckarter

@DougGregor
Copy link
Member

#17617 and #17622 address this

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Aug 4, 2018

@tjw, Could you verify if the problem is fixed and if so move the JIRA to "Closed"?
Thanks!
Anna

@tjw
Copy link
Contributor Author

tjw commented Aug 4, 2018

This is working for me in Xcode 10.0 b5.

@tjw
Copy link
Contributor Author

tjw commented Aug 4, 2018

Working in Xcode 10.0 b5.

@AnnaZaks
Copy link
Mannequin

AnnaZaks mannequin commented Aug 5, 2018

Great! Thank you.

@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

4 participants