2
0

reorder_fuzzer_qtest_trace.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Use this to convert qtest log info from a generic fuzzer input into a qtest
  5. trace that you can feed into a standard qemu-system process. Example usage:
  6. QEMU_FUZZ_ARGS="-machine q35,accel=qtest" QEMU_FUZZ_OBJECTS="*" \
  7. ./i386-softmmu/qemu-fuzz-i386 --fuzz-target=generic-pci-fuzz
  8. # .. Finds some crash
  9. QTEST_LOG=1 FUZZ_SERIALIZE_QTEST=1 \
  10. QEMU_FUZZ_ARGS="-machine q35,accel=qtest" QEMU_FUZZ_OBJECTS="*" \
  11. ./i386-softmmu/qemu-fuzz-i386 --fuzz-target=generic-pci-fuzz
  12. /path/to/crash 2> qtest_log_output
  13. scripts/oss-fuzz/reorder_fuzzer_qtest_trace.py qtest_log_output > qtest_trace
  14. ./i386-softmmu/qemu-fuzz-i386 -machine q35,accel=qtest \
  15. -qtest stdio < qtest_trace
  16. ### Details ###
  17. Some fuzzer make use of hooks that allow us to populate some memory range, just
  18. before a DMA read from that range. This means that the fuzzer can produce
  19. activity that looks like:
  20. [start] read from mmio addr
  21. [end] read from mmio addr
  22. [start] write to pio addr
  23. [start] fill a DMA buffer just in time
  24. [end] fill a DMA buffer just in time
  25. [start] fill a DMA buffer just in time
  26. [end] fill a DMA buffer just in time
  27. [end] write to pio addr
  28. [start] read from mmio addr
  29. [end] read from mmio addr
  30. We annotate these "nested" DMA writes, so with QTEST_LOG=1 the QTest trace
  31. might look something like:
  32. [R +0.028431] readw 0x10000
  33. [R +0.028434] outl 0xc000 0xbeef # Triggers a DMA read from 0xbeef and 0xbf00
  34. [DMA][R +0.034639] write 0xbeef 0x2 0xAAAA
  35. [DMA][R +0.034639] write 0xbf00 0x2 0xBBBB
  36. [R +0.028431] readw 0xfc000
  37. This script would reorder the above trace so it becomes:
  38. readw 0x10000
  39. write 0xbeef 0x2 0xAAAA
  40. write 0xbf00 0x2 0xBBBB
  41. outl 0xc000 0xbeef
  42. readw 0xfc000
  43. I.e. by the time, 0xc000 tries to read from DMA, those DMA buffers have already
  44. been set up, removing the need for the DMA hooks. We can simply provide this
  45. reordered trace via -qtest stdio to reproduce the input
  46. Note: this won't work for traces where the device tries to read from the same
  47. DMA region twice in between MMIO/PIO commands. E.g:
  48. [R +0.028434] outl 0xc000 0xbeef
  49. [DMA][R +0.034639] write 0xbeef 0x2 0xAAAA
  50. [DMA][R +0.034639] write 0xbeef 0x2 0xBBBB
  51. The fuzzer will annotate suspected double-fetches with [DOUBLE-FETCH]. This
  52. script looks for these tags and warns the users that the resulting trace might
  53. not reproduce the bug.
  54. """
  55. import sys
  56. __author__ = "Alexander Bulekov <alxndr@bu.edu>"
  57. __copyright__ = "Copyright (C) 2020, Red Hat, Inc."
  58. __license__ = "GPL version 2 or (at your option) any later version"
  59. __maintainer__ = "Alexander Bulekov"
  60. __email__ = "alxndr@bu.edu"
  61. def usage():
  62. sys.exit("Usage: {} /path/to/qtest_log_output".format((sys.argv[0])))
  63. def main(filename):
  64. with open(filename, "r") as f:
  65. trace = f.readlines()
  66. # Leave only lines that look like logged qtest commands
  67. trace[:] = [x.strip() for x in trace if "[R +" in x
  68. or "[S +" in x and "CLOSED" not in x]
  69. for i in range(len(trace)):
  70. if i+1 < len(trace):
  71. if "[DMA]" in trace[i+1]:
  72. if "[DOUBLE-FETCH]" in trace[i+1]:
  73. sys.stderr.write("Warning: Likely double fetch on line"
  74. "{}.\n There will likely be problems "
  75. "reproducing behavior with the "
  76. "resulting qtest trace\n\n".format(i+1))
  77. trace[i], trace[i+1] = trace[i+1], trace[i]
  78. for line in trace:
  79. print(line.split("]")[-1].strip())
  80. if __name__ == '__main__':
  81. if len(sys.argv) == 1:
  82. usage()
  83. main(sys.argv[1])