main.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // main.swift
  3. //
  4. // This file has two purposes, one it provides the entry point for the
  5. // fuzzer, and second, it runs through a battery of tests from the fuzzer
  6. // they are run separately.
  7. //
  8. // Sadly, there does not seem a way of making this file serve two purposes
  9. // at once without editing it every time. If compiled for fuzzing, no
  10. // calls from the toplevel are allowed, but to exercise, you want that call.
  11. //
  12. // Created by Miguel de Icaza on 4/24/20.
  13. //
  14. import Foundation
  15. import SwiftTerm
  16. var queue = DispatchQueue(label: "Runner", qos: .userInteractive, attributes: .concurrent, autoreleaseFrequency: .inherit, target: nil)
  17. // Fuzzer entry point
  18. @_cdecl("LLVMFuzzerTestOneInput") public func fuzzMe(data: UnsafePointer<UInt8>, length: CInt) -> CInt{
  19. let h = HeadlessTerminal (queue: queue) { exitCode in }
  20. let t = h.terminal!
  21. t.silentLog = true
  22. let buffer = UnsafeBufferPointer(start: data, count: Int (length))
  23. let arr = Array(buffer)
  24. t.feed (byteArray: arr)
  25. return 0
  26. }
  27. // For manually testing stuff and use the Xcode debugger
  28. func testInput (d: Data)
  29. {
  30. let h = HeadlessTerminal (queue: queue) { exitCode in }
  31. var data : [UInt8] = []
  32. data.append(contentsOf: d)
  33. let t = h.terminal!
  34. t.silentLog = true
  35. t.feed (byteArray: data)
  36. }
  37. func testCrashes ()
  38. {
  39. let crashes = [
  40. "crash-039a0b21c56b1e3a7a51056dd4f8daa9130c7312",
  41. "crash-36eb1fbfdb3a61e7b17b166d190ffd85ad9c80ab",
  42. "crash-4ba9dc95bc1c5d691fd9e80a4de72d65184e5c56",
  43. "crash-59fb9d3b7ab81c1782d26dfc69a962fae49ec449",
  44. "crash-64300317b2f97db7bfacfd77ba4d879e9726fd68",
  45. "crash-b926cdde789b73ff9680ff9ab643f13fa36c0571",
  46. "crash-c1147059ce893629e13289b43ae2b2ad1edcf44f",
  47. "crash-de2a0b4222547592208f7f85e2cd5b2730194daa",
  48. "crash-e1f2f0f2ef07d6d728316fa1bc336e6d1d699b99",
  49. "crash-ec47d21af677ee8eb18f91e150cdfb5d41d931c1",
  50. ]
  51. for crash in crashes {
  52. let url = URL(fileURLWithPath: "/Users/miguel/cvs/SwiftTerm/\(crash)")
  53. let data: Data
  54. do {
  55. print ("Running test \(crash)")
  56. data = try Data(contentsOf: url)
  57. } catch {
  58. print ("Caught error loading \(crash)")
  59. continue
  60. }
  61. testInput (d: data)
  62. print ("passed crash \(crash)")
  63. }
  64. print ("Happy!")
  65. }
  66. testCrashes()