mirror of
https://github.com/apple/swift-crypto
synced 2026-11-24 04:37:48 +00:00
We've long since used `@_implementationOnly import` (IOI) for our vendored C modules (BoringSSL and libXKCP) in an attempt to not leak our dependency into downstream builds. This is important because leaking might cause symbol collisions (build failures) or, worse, layout mismatches (latent runtime failures). IOI was intended to be used with library evolution, which this project does not support, and use of IOI without library evolution has been soft-deprecated. Users are encouraged to move to `internal import` (II), which is usually the right choice for Swift packages. But II does _not_ provide the same guarantees as IOI and, for this project, we really need the protections given by IOI so we will continue to use it, even with library evolution disabled. One of the reasons for the soft-deprecation was the implementation of IOI used an incomplete checker, that won't catch all interface leaks without library evolution. However, Swift has also introduced a new feature, `CheckImplementationOnly` (CIO), which enables the stricter interface checking that we need. Enabling CIO for this project today actually exposes a number of places where we _are_ leaking the C types into our interface in ways that could cause build- or runtime problems in downstream projects, and rightly causes the build to fail. These types will need to have their C struct members boxed behind an `OpaquePointer`. This patch enables CIO, and adds the required boxing to the offending types. The public API is unchanged. Additional refactoring is also done to ensure a single designated initializer, responsible for the allocation, forcing all convenience initializers through an `init(takingOwnershipOf:)` spelling, to reduce the chances of introducing future double free bugs in complex initializers. This patch also adds some benchmarks for some of the paths that would be implicated by these changes. While they show the expected increase in allocations, there's only a modest-to-negligible performance regression, since the majority of the compute is dominated by the time spent doing the crypto routines in the C library themselves.