main.swift 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. import Foundation
  9. import FuzzCommon
  10. import SwiftProtobuf
  11. @_cdecl("LLVMFuzzerTestOneInput")
  12. public func FuzzDelimited(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
  13. // No decoding options here, a leading zero is actually valid (zero length message),
  14. // so we rely on the other Binary fuzz tester to test options, and just let this
  15. // one focus on issue around framing of the messages on the stream.
  16. let bytes = UnsafeRawBufferPointer(start: start, count: count)
  17. let istream = InputStream(data: Data(bytes))
  18. istream.open()
  19. while true {
  20. let msg: SwiftProtoTesting_Fuzz_Message?
  21. do {
  22. msg = try BinaryDelimited.parse(
  23. messageType: SwiftProtoTesting_Fuzz_Message.self,
  24. from: istream,
  25. extensions: SwiftProtoTesting_Fuzz_FuzzTesting_Extensions
  26. )
  27. } catch {
  28. // Error parsing are to be expected since not all input will be well formed.
  29. break
  30. }
  31. // Test serialization for completeness.
  32. // If a message was parsed, it should not fail to serialize, so assert as such.
  33. if let msg = msg {
  34. // Could use one stream for all messages, but since fuzz tests have
  35. // memory limits, attempt to avoid hitting that limit with a new stream
  36. // for each output attempt.
  37. let ostream = OutputStream.toMemory()
  38. ostream.open()
  39. try! BinaryDelimited.serialize(message: msg, to: ostream)
  40. }
  41. }
  42. return 0
  43. }