Runtime crash in optimised build wrapping C++ library

I'm trying to diagnose a runtime crash that occurs only in release builds.

I've extracted the issue into swift package that reproduces it within as small a project as I could manage.

I'm not sure which layer the issue occurs. It could be any of the following:

  • Swift DuckDB wrapper (for which I wrote most of the code)
  • DuckDB C++ lib
  • Swift compiler
  • LLVM

As it occurs only in Swift 5.10 and later, and only in release builds, my guess is it's the swift compiler but it's totally possible the fault lies in the wrapper code. There's lots of pointer management where I can imagine issues could easily go unseen.

The project uses the DuckDB swift library, which is a thin wrapper around the DuckDB C++ project which itself exposes a C interface.

The error I get is:

libc++abi: terminating due to uncaught exception of type duckdb::InternalException: {"exception_type":"INTERNAL","exception_message":"Attempted to dereference unique_ptr that is NULL!"}
[1] 16630 abort xcrun swift run -c release

To Reproduce

  1. Run xcrun swift --version and verify you're using Swift version 5.10
  2. Unzip the small swift package reproducer DuckConnCrash.zip and cd into the uncompressed directory.
  3. Run xcrun swift run -c release to see the crash

If you xcrun swift run, you'll see the program exit normally, so it seems to be something specific to optimised builds.

Question

From the above, does anyone have any insights as to the likely origin of the issue and whether or not I should create an issue report in the Swift project? Or if it's more likely at one of the higher levels?