mirror of
https://github.com/apple/swift-nio-imap
synced 2026-11-27 05:04:23 +00:00
The handlers stage reads in an IMAPInputBuffer (IMAPInputProcessor) instead of NIOSingleStepByteToMessageProcessor's coalesced ByteBuffer, so a FETCH/APPEND literal piece of at least 1 KiB within one read of at least 1 KiB is delivered as a reference to the received bytes. Shorter pieces are copied; bytes from smaller reads are copied twice, into a tail on arrival and out of it on delivery. The decoders keep their ByteBuffer entry point for ByteToMessageHandler and the decoder verifier; that path copies. A decoder error or PayloadTooLargeError is final: the buffer is dropped and later reads rethrow it without decoding (ByteToMessageHandler also stops decoding, but fires dataReceivedInErrorState). Keeping the unparsed bytes would make every later read append, fail on the same bytes and copy all of them into IMAPDecoderError.buffer: unbounded memory and quadratic copying, past the maximumBufferSize check. Errors thrown by the message receiver (e.g. UnexpectedResponse) pass through and are not final. Reads under 1 KiB are copied into IMAPInputBuffer's tails, so 1-byte reads cost a region per KiB and pin no receive buffers. A larger read still pins its whole receive buffer, which maximumBufferSize does not count. Review focus: IMAPInputProcessor. Behavior changes: - A stream whose input failed to parse stays failed. - IMAPDecoderError.buffer holds the bytes waiting to be parsed when the error was thrown. - Leftover literal bytes followed by a read that starts a new region reach the handler's consumer as two .streamingBytes events, not one. Every read of at least 1 KiB starts a region; a smaller one does when the last region is a retained read, or a tail it does not fit or that is not uniquely referenced. ParseInput.extractBuffer(upTo:) takes bytes from the current region only, where the old code took min(remaining, readable) across the whole buffer. Legal, but observable. A caller passing a multi-region buffer to the stream parsers directly sees this from the previous commit on.
393 lines
19 KiB
Swift
393 lines
19 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// Bytes the parser can read from, in one or more contiguous runs.
|
|
///
|
|
/// Internal: ``IMAPInputBuffer`` is the only public parse input. This protocol is what the parser's
|
|
/// gather is written against, so that the library's own input buffer and the package's tests (which
|
|
/// parse out of arrays) share one code path.
|
|
///
|
|
/// A conformer describes its bytes as an ordered collection of ``ByteRegion``s and says how many
|
|
/// bytes there are in total. Everything else has a default implementation.
|
|
/// ``ConsumableParserInputBuffer`` adds "the parser may discard what it consumed", which the stream
|
|
/// parsers rely on, and ``StableRegionParserInputBuffer`` adds "my region pointers stay valid for a
|
|
/// borrow of me", which buys a single-pass gather and copy-free extraction of large payloads.
|
|
/// ``IMAPInputBuffer`` is all three.
|
|
///
|
|
/// - Important: ``byteRegions`` must begin at the read position, and its regions must add up to
|
|
/// ``count``. A conformer that keeps a read head as an offset has to *slice* its first region
|
|
/// rather than hand over the whole thing and expect the parser to skip the prefix — the parser
|
|
/// reports how much it consumed relative to what it was shown, so a skipped prefix gets counted
|
|
/// twice and bytes go missing. Both halves are asserted in debug builds.
|
|
///
|
|
/// ## Cost
|
|
///
|
|
/// The parser's per-region overhead is small but not free — each move from one region to
|
|
/// the next costs something, and bulk scans restart at every boundary. That is invisible for
|
|
/// network-sized reads and dominant for pathologically small ones: one-byte regions parse far slower
|
|
/// per byte than contiguous bytes do.
|
|
internal protocol ParserInputBuffer {
|
|
/// The collection of contiguous byte runs this buffer is made of.
|
|
associatedtype Regions: Collection where Regions.Element: ByteRegion
|
|
|
|
/// The buffer's bytes, in order, starting at the read position.
|
|
var byteRegions: Regions { get }
|
|
|
|
/// The total number of bytes, across every region.
|
|
var count: Int { get }
|
|
|
|
/// How many regions ``byteRegions`` holds.
|
|
///
|
|
/// The default implementation counts ``byteRegions``. Override it when the count is cheaper to
|
|
/// come by than the regions themselves — `DispatchData` allocates an array to present its
|
|
/// regions, and the gather only needs the number in order to size its table.
|
|
var regionCount: Int { get }
|
|
|
|
/// Reads through the buffer's bytes.
|
|
///
|
|
/// This is the gather: it collects the buffer's regions into one flat table and hands `body` a
|
|
/// cursor over them. Nothing about the cursor may outlive the call.
|
|
///
|
|
/// The default implementation walks ``byteRegions``, holding each region's accessor open for as
|
|
/// long as the cursor exists. ``StableRegionParserInputBuffer`` conformers replace it with a
|
|
/// single pass, and must do so with an explicit one-line witness of their own — see that
|
|
/// protocol.
|
|
///
|
|
/// - Parameters:
|
|
/// - limit: The most bytes the parser may consume. Bytes past it are invisible to it, which is
|
|
/// how a caller parses a prefix of what it holds without slicing first.
|
|
/// - body: A closure that reads through the cursor.
|
|
/// - Returns: Whatever `body` returns.
|
|
func withParseInput<R>(limit: Int, _ body: (inout ParseInput) throws -> R) rethrows -> R
|
|
}
|
|
|
|
/// A ``ParserInputBuffer`` the parser can also consume from, so that a caller does not have to trim
|
|
/// it by hand.
|
|
///
|
|
/// ``ConsumableParserInputBuffer/parse(limit:_:)`` is the entry point that trims: whatever the
|
|
/// parser consumed is discarded when it returns, including when it throws.
|
|
///
|
|
/// - Important: ``discardFirst(_:)`` must be O(1) amortised in the number of bytes discarded. A
|
|
/// conformer that moves the remaining bytes down instead makes a caller that trims after every
|
|
/// message quadratic over the life of a connection. That is why the array types are deliberately
|
|
/// *not* consumable here, even though removing a prefix from them is one line: the one line is the slow
|
|
/// one.
|
|
internal protocol ConsumableParserInputBuffer: ParserInputBuffer {
|
|
/// Discards the first `k` bytes.
|
|
///
|
|
/// - Parameter k: How many bytes to discard. Must not exceed ``ParserInputBuffer/count``.
|
|
mutating func discardFirst(_ k: Int)
|
|
}
|
|
|
|
/// A ``ParserInputBuffer`` whose region pointers stay valid for as long as the buffer is borrowed.
|
|
///
|
|
/// **This is one of the two unsafe seams on the parser's read side, and conforming to it is an
|
|
/// assertion the compiler cannot check.** ``ByteRegion`` promises only that a pointer is valid for the
|
|
/// duration of a ``ByteRegion/withUnsafeBytes(_:)`` call. A conformer here promises more: that the
|
|
/// pointers it pushes into a ``RegionSink`` stay valid for as long as a borrow of `self` lasts, so the
|
|
/// gather can collect them in one pass instead of nesting one accessor scope per region.
|
|
///
|
|
/// What that buys is not mainly speed. The generic gather recurses once per region, so it needs a
|
|
/// depth cap and has to coalesce the tail beyond it; a single pass needs neither, at any region
|
|
/// count.
|
|
///
|
|
/// It is also where zero-copy extraction comes from. A conformer that owns its bytes can pass an
|
|
/// ``IMAPByteStorage`` to ``RegionSink/append(_:owner:offset:)``, and a large payload extracted from
|
|
/// that region is then handed to the caller as a reference to the input rather than as a copy — if it
|
|
/// is at least a third of what the storage keeps alive (see ``StableAddressByteRegion/pinnedByteCount``).
|
|
/// Passing `nil` is always correct and means such an extraction copies.
|
|
internal protocol StableRegionParserInputBuffer: ParserInputBuffer {
|
|
/// Pushes every region, in order, into `sink`.
|
|
///
|
|
/// - Parameter sink: The table to fill. The only thing that can be done with it is appending.
|
|
borrowing func withStableRegions(into sink: inout RegionSink)
|
|
}
|
|
|
|
/// The table a ``StableRegionParserInputBuffer`` fills, one region at a time.
|
|
///
|
|
/// The library owns the storage and validates every push, so a conformer cannot get the bookkeeping
|
|
/// wrong: it cannot overrun the table, and it cannot describe a range that lies outside the owner it
|
|
/// names. It is neither copyable nor escapable, so it cannot outlive the gather that created it.
|
|
public struct RegionSink: ~Copyable, ~Escapable {
|
|
/// Where the entries go. Sized from ``ParserInputBuffer/regionCount``.
|
|
private let table: UnsafeMutableBufferPointer<RegionEntry>
|
|
|
|
/// How many entries have been pushed.
|
|
private(set) var filled: Int
|
|
|
|
/// How many bytes have been pushed, for the conformance assertion in the gather.
|
|
private(set) var byteCount: Int
|
|
|
|
@_lifetime(immortal)
|
|
internal init(table: UnsafeMutableBufferPointer<RegionEntry>) {
|
|
self.table = table
|
|
self.filled = 0
|
|
self.byteCount = 0
|
|
}
|
|
|
|
/// Adds one region.
|
|
///
|
|
/// - Parameters:
|
|
/// - bytes: The region's bytes. Must stay valid for as long as `self` and the borrow of the
|
|
/// buffer that produced it — this is the promise
|
|
/// ``StableRegionParserInputBuffer`` makes.
|
|
/// - owner: Who keeps `bytes` alive, if the buffer can vouch for them, so that a large
|
|
/// extraction can refer to the input rather than copying it. `nil` means an extraction
|
|
/// copies, which is always correct.
|
|
/// - offset: Where `bytes` begins inside `owner`. Zero when the region is all of the owner;
|
|
/// the head offset when the region is the unconsumed tail of one.
|
|
@_lifetime(self: copy self)
|
|
public mutating func append(_ bytes: UnsafeBufferPointer<UInt8>, owner: IMAPByteStorage?, offset: Int) {
|
|
precondition(
|
|
filled < table.count,
|
|
"a ParserInputBuffer pushed more than the \(table.count) regions its regionCount promised"
|
|
)
|
|
if let owner {
|
|
precondition(
|
|
offset >= 0 && offset + bytes.count <= owner.count,
|
|
"a region of \(bytes.count) bytes at offset \(offset) does not fit its owner's \(owner.count)"
|
|
)
|
|
}
|
|
table[filled] = RegionEntry(
|
|
bytes: bytes,
|
|
owner: owner.map { Unmanaged.passUnretained($0) },
|
|
ownerOffset: offset
|
|
)
|
|
filled += 1
|
|
byteCount += bytes.count
|
|
}
|
|
|
|
/// The entries pushed so far.
|
|
internal var filledTable: ChunkTable {
|
|
ChunkTable(rebasing: table[..<filled])
|
|
}
|
|
}
|
|
|
|
/// `RegionSink` is deliberately **not** `Sendable`.
|
|
///
|
|
/// It points at a table on the gather's stack, and the pointers pushed into it are valid only for that
|
|
/// gather. Being neither copyable nor escapable already keeps it there; this says why.
|
|
@available(*, unavailable)
|
|
extension RegionSink: Sendable {}
|
|
|
|
// MARK: - The generic gather
|
|
|
|
extension ParserInputBuffer {
|
|
/// The most frames the recursive gather may use.
|
|
///
|
|
/// One region costs one frame, and a frame here is not small, especially in a debug build, because a
|
|
/// generic region accessor plus its closure is several physical frames. A Swift concurrency task
|
|
/// thread has a limited stack, and the parser recurses below the gather on top of that — so 64
|
|
/// frames is the budget. It is deliberately conservative: 256 was tried first and overflowed a test thread's
|
|
/// stack, and a stack overflow cannot be caught, so "the caller passed a lot of regions" would
|
|
/// otherwise be a killed process.
|
|
///
|
|
/// Regions past the cap are coalesced rather than dropped, so the input stays unbounded and only the
|
|
/// stack is bounded. Types that really do carry many regions conform to
|
|
/// ``StableRegionParserInputBuffer`` and never come here — that path has no recursion and no cap.
|
|
internal static var maximumGatherDepth: Int { 64 }
|
|
|
|
internal var regionCount: Int { byteRegions.count }
|
|
|
|
internal func withParseInput<R>(limit: Int, _ body: (inout ParseInput) throws -> R) rethrows -> R {
|
|
let regions = byteRegions
|
|
assertRegionsAddUp(regions)
|
|
// One entry per region up to the cap, plus one for a coalesced tail.
|
|
let capacity = Swift.min(regionCount, Self.maximumGatherDepth) + 1
|
|
return try withUnsafeTemporaryAllocation(of: RegionEntry.self, capacity: capacity) { table in
|
|
try Self.descend(regions, regions.startIndex, 0, 0, table, limit, body)
|
|
}
|
|
}
|
|
|
|
/// Reads through the buffer's bytes, with no limit on how many the parser may consume.
|
|
///
|
|
/// - Parameter body: A closure that reads through the cursor.
|
|
/// - Returns: Whatever `body` returns.
|
|
///
|
|
/// - Note: This is deliberately a *different* signature from
|
|
/// ``ParserInputBuffer/withParseInput(limit:_:)`` rather than a default argument on it. A
|
|
/// default argument here would shadow a conformer's own witness, so a one-pass conformer would
|
|
/// silently take the generic path whenever the caller left the limit out — which is the common
|
|
/// case, and the failure is invisible.
|
|
public func withParseInput<R>(_ body: (inout ParseInput) throws -> R) rethrows -> R {
|
|
try withParseInput(limit: .max, body)
|
|
}
|
|
|
|
/// Fills the table one region deeper per frame.
|
|
///
|
|
/// The nesting is forced: a cursor cannot be threaded *through* a chain of accessor scopes, so
|
|
/// the descent writes pointers into a flat table on the way down and builds the cursor at the
|
|
/// innermost frame, where every scope is still open. Do not restructure this into a loop that
|
|
/// opens and closes one region at a time.
|
|
private static func descend<R>(
|
|
_ regions: Regions,
|
|
_ index: Regions.Index,
|
|
_ depth: Int,
|
|
_ filled: Int,
|
|
_ table: UnsafeMutableBufferPointer<RegionEntry>,
|
|
_ limit: Int,
|
|
_ body: (inout ParseInput) throws -> R
|
|
) rethrows -> R {
|
|
guard index != regions.endIndex else {
|
|
var input = ParseInput(table: ChunkTable(rebasing: table[..<filled]), owner: regions, limit: limit)
|
|
return try body(&input)
|
|
}
|
|
// Coalescing at the cap, or when the table is full because `regionCount` under-reported.
|
|
guard depth < maximumGatherDepth, filled < table.count - 1 else {
|
|
return try coalescing(regions, index, filled, table, limit, body)
|
|
}
|
|
let next = regions.index(after: index)
|
|
return try regions[index].withUnsafeBytes { raw -> R in
|
|
guard !raw.isEmpty else {
|
|
// An empty region carries no bytes, so it needs no entry — but it does cost a frame.
|
|
return try descend(regions, next, depth + 1, filled, table, limit, body)
|
|
}
|
|
table[filled] = RegionEntry(bytes: raw.bindMemory(to: UInt8.self))
|
|
return try descend(regions, next, depth + 1, filled + 1, table, limit, body)
|
|
}
|
|
}
|
|
|
|
/// Copies every region from `index` onwards into one buffer, and parses that as a single region.
|
|
///
|
|
/// This is the pathological path: a caller that hands over more regions than the recursion can
|
|
/// safely descend gets a copy of the tail rather than a stack overflow. Nothing is dropped, and
|
|
/// the copy is bounded by the bytes the caller passed.
|
|
private static func coalescing<R>(
|
|
_ regions: Regions,
|
|
_ index: Regions.Index,
|
|
_ filled: Int,
|
|
_ table: UnsafeMutableBufferPointer<RegionEntry>,
|
|
_ limit: Int,
|
|
_ body: (inout ParseInput) throws -> R
|
|
) rethrows -> R {
|
|
var tail = [UInt8]()
|
|
for region in regions[index...] {
|
|
region.withUnsafeBytes { tail.append(contentsOf: $0) }
|
|
}
|
|
return try tail.withUnsafeBytes { raw -> R in
|
|
var count = filled
|
|
if !raw.isEmpty {
|
|
table[filled] = RegionEntry(bytes: raw.bindMemory(to: UInt8.self))
|
|
count += 1
|
|
}
|
|
var input = ParseInput(table: ChunkTable(rebasing: table[..<count]), owner: tail, limit: limit)
|
|
return try body(&input)
|
|
}
|
|
}
|
|
|
|
/// Checks the conformance requirement that the regions add up to `count`.
|
|
///
|
|
/// A buffer whose regions do not start at its read position usually still parses — it parses the
|
|
/// bytes it already handed out a second time — so the symptom is corrupted output far from the
|
|
/// cause. Failing here instead means a bad conformer fails in its first test.
|
|
private func assertRegionsAddUp(_ regions: Regions) {
|
|
assert(
|
|
regions.reduce(0) { $0 + $1.withUnsafeBytes { $0.count } } == count,
|
|
"\(Self.self)'s regions hold \(regions.reduce(0) { $0 + $1.withUnsafeBytes { $0.count } }) bytes "
|
|
+ "but its count is \(count). Regions must begin at the read position and add up to count."
|
|
)
|
|
}
|
|
}
|
|
|
|
// MARK: - The one-pass gather
|
|
|
|
extension StableRegionParserInputBuffer {
|
|
/// Reads through the buffer's bytes in a single pass, with no recursion and no depth cap.
|
|
///
|
|
/// Conformers forward to this from their own ``ParserInputBuffer/withParseInput(limit:_:)``:
|
|
///
|
|
/// ```swift
|
|
/// func withParseInput<R>(limit: Int, _ body: (inout ParseInput) throws -> R) rethrows -> R {
|
|
/// try _stableWithParseInput(limit: limit, body)
|
|
/// }
|
|
/// ```
|
|
///
|
|
/// The forward has to be written out like that rather than left to a default implementation on
|
|
/// this protocol. A default here satisfies the *base* protocol's requirement, and which of the
|
|
/// two the compiler picks depends on where the call is made from — so `parse` would take the
|
|
/// generic path while a direct call took this one, with nothing failing to announce it.
|
|
///
|
|
/// - Parameters:
|
|
/// - limit: The most bytes the parser may consume.
|
|
/// - body: A closure that reads through the cursor.
|
|
/// - Returns: Whatever `body` returns.
|
|
internal func _stableWithParseInput<R>(
|
|
limit: Int,
|
|
_ body: (inout ParseInput) throws -> R
|
|
) rethrows -> R {
|
|
try withUnsafeTemporaryAllocation(
|
|
of: RegionEntry.self,
|
|
capacity: Swift.max(regionCount, 1)
|
|
) { table in
|
|
var sink = RegionSink(table: table)
|
|
withStableRegions(into: &sink)
|
|
assert(
|
|
sink.byteCount == count,
|
|
"\(Self.self) pushed \(sink.byteCount) bytes but its count is \(count). Regions must "
|
|
+ "begin at the read position and add up to count."
|
|
)
|
|
var input = ParseInput(table: sink.filledTable, owner: self, limit: limit)
|
|
return try body(&input)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Consuming
|
|
|
|
extension ConsumableParserInputBuffer {
|
|
/// Reads through the buffer's bytes and discards whatever the parser consumed.
|
|
///
|
|
/// This is the entry point a connection uses: the caller cannot forget to trim, and cannot trim
|
|
/// by the wrong amount.
|
|
///
|
|
/// Trimming happens on every exit path, a thrown error included, because the streaming literal
|
|
/// modes legitimately make progress and *then* report that they need more bytes. A parser that
|
|
/// wants its position back on failure gets that from the cursor's own backtracking instead, which
|
|
/// leaves nothing consumed to trim.
|
|
///
|
|
/// - Parameters:
|
|
/// - limit: The most bytes the parser may consume.
|
|
/// - body: A closure that reads through the cursor.
|
|
/// - Returns: Whatever `body` returns.
|
|
public mutating func parse<R>(
|
|
limit: Int = .max,
|
|
_ body: (inout ParseInput) throws -> R
|
|
) rethrows -> R {
|
|
var consumed = 0
|
|
defer { if consumed > 0 { self.discardFirst(consumed) } }
|
|
// `limit:` is passed explicitly so that this binds a conformer's own witness rather than the
|
|
// no-limit convenience overload, which would send a one-pass conformer down the generic path.
|
|
return try withParseInput(limit: limit) { input in
|
|
defer { consumed = input.consumed }
|
|
return try body(&input)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Stdlib conformances
|
|
|
|
// For the package's tests, which parse out of arrays. Read-only on purpose: removing a prefix from an
|
|
// array moves the rest of it.
|
|
extension Array: ParserInputBuffer where Element == UInt8 {
|
|
var byteRegions: CollectionOfOne<[UInt8]> { CollectionOfOne(self) }
|
|
}
|
|
|
|
extension ArraySlice: ParserInputBuffer where Element == UInt8 {
|
|
var byteRegions: CollectionOfOne<ArraySlice<UInt8>> { CollectionOfOne(self) }
|
|
}
|
|
|
|
extension ContiguousArray: ParserInputBuffer where Element == UInt8 {
|
|
var byteRegions: CollectionOfOne<ContiguousArray<UInt8>> { CollectionOfOne(self) }
|
|
}
|