Skip to content

C++ interop crash adding Strings of > 15 bytes to Swift Array. #69372

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

Closed
efroemling opened this issue Oct 24, 2023 · 2 comments · Fixed by #69473
Closed

C++ interop crash adding Strings of > 15 bytes to Swift Array. #69372

efroemling opened this issue Oct 24, 2023 · 2 comments · Fixed by #69473
Assignees
Labels
Array Area → standard library: The `Array` type bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software ownership Feature: Ownership modifiers and semantics swift to c++ Feature → c++ interop: swift to c++ swift 5.10

Comments

@efroemling
Copy link

efroemling commented Oct 24, 2023

Description
When creating a Swift Array of Strings from C++ code (as described in the guide), adding strings with a size greater than 15 bytes causes a crash.

Steps to reproduce
The following steps reproduce the crash for me using XCode 15.0.1 (15A507) on an M1 Max MBP running Sonoma 14.1:

  • Create a new ‘Command Line Tool’ project named “TestCommand”.
  • Create a new C++ file named ‘test’ (with header). Click ‘Create Bridging Header’ when it asks.
  • In TestCommand-Bridging-Header.h, add:
    #include "test.hpp"
  • In test.hpp, add:
    void doTest();
  • In test.cpp, add:
    #include <TestCommand-Swift.h>
    
    void doTest() {
      // Pass an array back to our Swift code to print.
      auto array = swift::Array<swift::String>::init();
      array.append("foo");
      TestCommand::printStrings(array);
    }
  • In main.swift, add:
    // Expose a print call for C++ to use.
    public func printStrings(_ strings: [String]) {
      for s in strings {
        print("GOT STRING", s)
      }
      print("DONE PRINTING.")
    }
    
    // Call our c++ code.
    doTest()
  • Under build settings for the TestCommand target, set C++ and Objective-C Interoperability to “C++/Objective C++”
  • You should now be able to build and run the project, which should give the following output:
    Hello, World!
    GOT STRING foo
    DONE PRINTING.
    Program ended with exit code: 0
    
  • Now, to trigger the crash, simply change the "foo" in test.cpp to "123456789ABCDEFG" and run again. You should see the following output followed by an EXC_BAD_ACCESS crash at the end of the doTest function.
    Hello, World!
    GOT STRING ����������������
    DONE PRINTING.
    <crashes here in swift::Array<swift::String>::~Array()>
    

As per the discussion of this crash on the swift forums, it appears that the threshold for causing a crash is strings > 15 characters which corresponds to String's inline storage size.

Environment

  • Swift compiler version info: swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)

  • Xcode version info: Xcode 15.0.1 Build version 15A507

  • Deployment target: macOS Sonoma 14.1 (23B73)

@efroemling efroemling added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Oct 24, 2023
@efroemling efroemling changed the title C++ interop crash adding Strings with > 15 chars to Swift Array. C++ interop crash adding Strings of > 15 bytes to Swift Array. Oct 24, 2023
@hyp hyp self-assigned this Oct 24, 2023
@hyp hyp added the c++ interop Feature: Interoperability with C++ label Oct 24, 2023
@hyp
Copy link
Contributor

hyp commented Oct 26, 2023

Problem with @owned parameter convention here.

@hyp
Copy link
Contributor

hyp commented Oct 27, 2023

I have a fix on hand, just testing it.

hyp added a commit to hyp/swift that referenced this issue Oct 28, 2023
hyp added a commit to hyp/swift that referenced this issue Oct 30, 2023
@AnthonyLatsis AnthonyLatsis added swift to c++ Feature → c++ interop: swift to c++ crash Bug: A crash, i.e., an abnormal termination of software compiler The Swift compiler itself textual interfaces ownership Feature: Ownership modifiers and semantics Array Area → standard library: The `Array` type and removed textual interfaces triage needed This issue needs more specific labels labels Oct 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Array Area → standard library: The `Array` type bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ compiler The Swift compiler itself crash Bug: A crash, i.e., an abnormal termination of software ownership Feature: Ownership modifiers and semantics swift to c++ Feature → c++ interop: swift to c++ swift 5.10
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants