Options.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 SwiftProtobuf
  10. public enum FuzzOption<T: SupportsFuzzOptions> {
  11. case boolean(WritableKeyPath<T, Bool>)
  12. case byte(WritableKeyPath<T, Int>, mod: UInt8 = .max)
  13. }
  14. public protocol SupportsFuzzOptions {
  15. static var fuzzOptionsList: [FuzzOption<Self>] { get }
  16. init()
  17. }
  18. extension SupportsFuzzOptions {
  19. public static func extractOptions(
  20. _ start: UnsafeRawPointer,
  21. _ count: Int
  22. ) -> (Self, UnsafeRawBufferPointer)? {
  23. var start = start
  24. let initialCount = count
  25. var count = count
  26. var options = Self()
  27. let reportInfo = ProcessInfo.processInfo.environment["DUMP_DECODE_INFO"] == "1"
  28. // No format can start with zero (invalid tag, not really UTF-8), so use that to
  29. // indicate there are decoding options. The one case that can start with a zero
  30. // would be length delimited binary, but since that's a zero length message, we
  31. // can go ahead and use that one also.
  32. guard count >= 2, start.loadUnaligned(as: UInt8.self) == 0 else {
  33. if reportInfo {
  34. print("No options to decode")
  35. }
  36. return (options, UnsafeRawBufferPointer(start: start, count: count))
  37. }
  38. // Step over the zero
  39. start += 1
  40. count -= 1
  41. var optionsBits = start.loadUnaligned(as: UInt8.self)
  42. start += 1
  43. count -= 1
  44. var bit = 0
  45. for opt in fuzzOptionsList {
  46. var isSet = optionsBits & (1 << bit) != 0
  47. if bit == 7 {
  48. // About the use the last bit of this byte, to allow more options in
  49. // the future, use this bit to indicate reading another byte.
  50. guard isSet else {
  51. // No continuation, just return whatever we got.
  52. bit = 8
  53. break
  54. }
  55. guard count >= 1 else {
  56. return nil // No data left to read bits
  57. }
  58. optionsBits = start.loadUnaligned(as: UInt8.self)
  59. start += 1
  60. count -= 1
  61. bit = 0
  62. isSet = optionsBits & (1 << bit) != 0
  63. }
  64. switch opt {
  65. case .boolean(let keypath):
  66. options[keyPath: keypath] = isSet
  67. case .byte(let keypath, let mod):
  68. assert(mod >= 1 && mod <= UInt8.max)
  69. if isSet {
  70. guard count >= 1 else {
  71. return nil // No more bytes to get a value, fail
  72. }
  73. let value = start.loadUnaligned(as: UInt8.self)
  74. start += 1
  75. count -= 1
  76. options[keyPath: keypath] = Int(value % mod)
  77. }
  78. }
  79. bit += 1
  80. }
  81. // Ensure the any remaining bits are zero so they can be used in the future
  82. while bit < 8 {
  83. if optionsBits & (1 << bit) != 0 {
  84. return nil
  85. }
  86. bit += 1
  87. }
  88. if reportInfo {
  89. print("\(initialCount - count) bytes consumed off front for options: \(options)")
  90. }
  91. return (options, UnsafeRawBufferPointer(start: start, count: count))
  92. }
  93. }
  94. extension BinaryDecodingOptions: SupportsFuzzOptions {
  95. public static var fuzzOptionsList: [FuzzOption<Self>] {
  96. [
  97. // NOTE: Do not reorder these in the future as it invalidates all
  98. // existing cases.
  99. // The default depth is 100, so limit outselves to modding by 8 to
  100. // avoid allowing larger depths that could timeout.
  101. .byte(\.messageDepthLimit, mod: 8),
  102. .boolean(\.discardUnknownFields),
  103. ]
  104. }
  105. }
  106. extension JSONDecodingOptions: SupportsFuzzOptions {
  107. public static var fuzzOptionsList: [FuzzOption<Self>] {
  108. [
  109. // NOTE: Do not reorder these in the future as it invalidates all
  110. // existing cases.
  111. // The default depth is 100, so limit outselves to modding by 8 to
  112. // avoid allowing larger depths that could timeout.
  113. .byte(\.messageDepthLimit, mod: 8),
  114. .boolean(\.ignoreUnknownFields),
  115. ]
  116. }
  117. }
  118. extension TextFormatDecodingOptions: SupportsFuzzOptions {
  119. public static var fuzzOptionsList: [FuzzOption<Self>] {
  120. [
  121. // NOTE: Do not reorder these in the future as it invalidates all
  122. // existing cases.
  123. // The default depth is 100, so limit outselves to modding by 8 to
  124. // avoid allowing larger depths that could timeout.
  125. .byte(\.messageDepthLimit, mod: 8),
  126. .boolean(\.ignoreUnknownFields),
  127. .boolean(\.ignoreUnknownExtensionFields),
  128. ]
  129. }
  130. }