Coverage Report

Created: 2026-11-12 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/swift-protobuf/FuzzTesting/Sources/FuzzBinaryDelimited/main.swift
Line
Count
Source
1
// Copyright (c) 2014 - 2024 Apple Inc. and the project authors
2
// Licensed under Apache License v2.0 with Runtime Library Exception
3
//
4
// See LICENSE.txt for license information:
5
// https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
6
//
7
// -----------------------------------------------------------------------------
8
9
import Foundation
10
import FuzzCommon
11
import SwiftProtobuf
12
13
// Put a limit on how many messages will be read because oss-fuzz seems
14
// occational timeouts on really large input sets, likely because there is just
15
// too much garbage so things just keep failing to parse. Just need to exercise
16
// things enough to read a few messages in a row.
17
private let kMaxMessages = 50
18
19
@_cdecl("LLVMFuzzerTestOneInput")
20
11.9k
public func FuzzDelimited(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
21
11.9k
    // No decoding options here, a leading zero is actually valid (zero length message),
22
11.9k
    // so we rely on the other Binary fuzz tester to test options, and just let this
23
11.9k
    // one focus on issue around framing of the messages on the stream.
24
11.9k
    let bytes = UnsafeRawBufferPointer(start: start, count: count)
25
11.9k
    let istream = InputStream(data: Data(bytes))
26
11.9k
    istream.open()
27
11.9k
    var msgCount = 0
28
56.4k
    while msgCount < kMaxMessages {
29
56.4k
        let msg: SwiftProtoTesting_Fuzz_Message?
30
56.4k
        do {
31
56.4k
            msg = try BinaryDelimited.parse(
32
56.4k
                messageType: SwiftProtoTesting_Fuzz_Message.self,
33
56.4k
                from: istream,
34
56.4k
                extensions: SwiftProtoTesting_Fuzz_FuzzTesting_Extensions
35
56.4k
            )
36
44.4k
            msgCount += 1
37
44.4k
        } catch {
38
11.9k
            // Error parsing are to be expected since not all input will be well formed.
39
11.9k
            break
40
44.4k
        }
41
44.4k
        // Test serialization for completeness.
42
44.4k
        // If a message was parsed, it should not fail to serialize, so assert as such.
43
44.4k
        if let msg = msg {
44
44.4k
            // Could use one stream for all messages, but since fuzz tests have
45
44.4k
            // memory limits, attempt to avoid hitting that limit with a new stream
46
44.4k
            // for each output attempt.
47
44.4k
            let ostream = OutputStream.toMemory()
48
44.4k
            ostream.open()
49
44.4k
            try! BinaryDelimited.serialize(message: msg, to: ostream)
50
44.4k
        }
51
44.4k
    }
52
11.9k
53
11.9k
    return 0
54
11.9k
}