Skip to contentSkip to conversation

fuzz: map all BARs and enable PCI devices

PATCH In review Alexander Bulekov 3 participants 3 messages Dec 21, 2020 – Mar 14, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 fuzz: map all BARs and enable PCI devices

Prior to this patch, the fuzzer found inputs to map PCI device BARs and enable the device. While it is nice that the fuzzer can do this, it added significant overhead, since the fuzzer needs to map all the BARs (regenerating the memory topology), at the start of each input. With this patch, we do this once, before fuzzing, mitigating some of this overhead.

---

v3: Plug the memory-leak pointed out by Thomas:

    https://gitlab.com/huth/qemu/-/jobs/920543745#L309
 tests/qtest/fuzz/generic_fuzz.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
tests/qtest/fuzz/generic_fuzz.c +24 -0
index 07ad690683..be76d47d2d 100644
@@ -16,6 +16,7 @@
 
 #include "hw/core/cpu.h"
 #include "tests/qtest/libqos/libqtest.h"
+#include "tests/qtest/libqos/pci-pc.h"
 #include "fuzz.h"
 #include "fork_fuzz.h"
 #include "exec/address-spaces.h"
@@ -762,10 +763,29 @@ static int locate_fuzz_objects(Object *child, void *opaque)
     return 0;
 }
 
+
+static void pci_enum(gpointer pcidev, gpointer bus)
+{
+    PCIDevice *dev = pcidev;
+    QPCIDevice *qdev;
+    int i;
+
+    qdev = qpci_device_find(bus, dev->devfn);
+    g_assert(qdev != NULL);
+    for (i = 0; i < 6; i++) {
+        if (dev->io_regions[i].size) {
+            qpci_iomap(qdev, i, NULL);
+        }
+    }
+    qpci_device_enable(qdev);
+    g_free(qdev);
+}
+
 static void generic_pre_fuzz(QTestState *s)
 {
     GHashTableIter iter;
     MemoryRegion *mr;
+    QPCIBus *pcibus;
     char **result;
 
     if (!getenv("QEMU_FUZZ_OBJECTS")) {
@@ -810,6 +830,10 @@ static void generic_pre_fuzz(QTestState *s)
         exit(1);
     }
 
+    pcibus = qpci_new_pc(s, NULL);
+    g_ptr_array_foreach(fuzzable_pci_devices, pci_enum, pcibus);
+    qpci_free_pc(pcibus);
+
     counter_shm_init();
 }
 
Signature · 1 line
2.29.2
Reviewed-by on v3 1/1
#1 lore raw
Quoted 61 lines · Alexander Bulekov

Prior to this patch, the fuzzer found inputs to map PCI device BARs and enable the device. While it is nice that the fuzzer can do this, it added significant overhead, since the fuzzer needs to map all the BARs (regenerating the memory topology), at the start of each input. With this patch, we do this once, before fuzzing, mitigating some of this overhead.

---

v3: Plug the memory-leak pointed out by Thomas:

     https://gitlab.com/huth/qemu/-/jobs/920543745#L309
  tests/qtest/fuzz/generic_fuzz.c | 24 ++++++++++++++++++++++++
  1 file changed, 24 insertions(+)
tests/qtest/fuzz/generic_fuzz.c +20 -0
index 07ad690683..be76d47d2d 100644
@@ -16,6 +16,7 @@
 
  #include "hw/core/cpu.h"
  #include "tests/qtest/libqos/libqtest.h"
+#include "tests/qtest/libqos/pci-pc.h"
  #include "fuzz.h"
  #include "fork_fuzz.h"
  #include "exec/address-spaces.h"
@@ -762,10 +763,29 @@ static int locate_fuzz_objects(Object *child, void *opaque)
      return 0;
  }
 
+
+static void pci_enum(gpointer pcidev, gpointer bus)
+{
+    PCIDevice *dev = pcidev;
+    QPCIDevice *qdev;
+    int i;
+
+    qdev = qpci_device_find(bus, dev->devfn);
+    g_assert(qdev != NULL);
+    for (i = 0; i < 6; i++) {
+        if (dev->io_regions[i].size) {
+            qpci_iomap(qdev, i, NULL);
+        }
+    }
+    qpci_device_enable(qdev);
+    g_free(qdev);
+}
+
  static void generic_pre_fuzz(QTestState *s)
  {
      GHashTableIter iter;
      MemoryRegion *mr;
+    QPCIBus *pcibus;
      char **result;
 
      if (!getenv("QEMU_FUZZ_OBJECTS")) {

tests/qtest/fuzz/generic_fuzz.c static void generic_pre_fuzz(QTestState *s) · line 839

Quoted 10 lines
tests/qtest/fuzz/generic_fuzz.c +4 -0
@@ -810,6 +830,10 @@ static void generic_pre_fuzz(QTestState *s)
          exit(1);
      }
 
+    pcibus = qpci_new_pc(s, NULL);
+    g_ptr_array_foreach(fuzzable_pci_devices, pci_enum, pcibus);
+    qpci_free_pc(pcibus);
+
      counter_shm_init();
  }
 
#2 lore raw
In reply to Alexander Bulekov
Quoted 60 lines · Alexander Bulekov

Prior to this patch, the fuzzer found inputs to map PCI device BARs and enable the device. While it is nice that the fuzzer can do this, it added significant overhead, since the fuzzer needs to map all the BARs (regenerating the memory topology), at the start of each input. With this patch, we do this once, before fuzzing, mitigating some of this overhead.

---

v3: Plug the memory-leak pointed out by Thomas:

    https://gitlab.com/huth/qemu/-/jobs/920543745#L309
 tests/qtest/fuzz/generic_fuzz.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
tests/qtest/fuzz/generic_fuzz.c +20 -0
index 07ad690683..be76d47d2d 100644
@@ -16,6 +16,7 @@
 
 #include "hw/core/cpu.h"
 #include "tests/qtest/libqos/libqtest.h"
+#include "tests/qtest/libqos/pci-pc.h"
 #include "fuzz.h"
 #include "fork_fuzz.h"
 #include "exec/address-spaces.h"
@@ -762,10 +763,29 @@ static int locate_fuzz_objects(Object *child, void *opaque)
     return 0;
 }
 
+
+static void pci_enum(gpointer pcidev, gpointer bus)
+{
+    PCIDevice *dev = pcidev;
+    QPCIDevice *qdev;
+    int i;
+
+    qdev = qpci_device_find(bus, dev->devfn);
+    g_assert(qdev != NULL);
+    for (i = 0; i < 6; i++) {
+        if (dev->io_regions[i].size) {
+            qpci_iomap(qdev, i, NULL);
+        }
+    }
+    qpci_device_enable(qdev);
+    g_free(qdev);
+}
+
 static void generic_pre_fuzz(QTestState *s)
 {
     GHashTableIter iter;
     MemoryRegion *mr;
+    QPCIBus *pcibus;
     char **result;
 
     if (!getenv("QEMU_FUZZ_OBJECTS")) {

tests/qtest/fuzz/generic_fuzz.c static void generic_pre_fuzz(QTestState *s) · line 833

Quoted 4 lines
tests/qtest/fuzz/generic_fuzz.c +1 -0
@@ -810,6 +830,10 @@ static void generic_pre_fuzz(QTestState *s)
         exit(1);
     }
 
+    pcibus = qpci_new_pc(s, NULL);

FYI this patch restricted the "generic" fuzzer to the x86 arch.

Quoted 7 lines
tests/qtest/fuzz/generic_fuzz.c +3 -0
+    g_ptr_array_foreach(fuzzable_pci_devices, pci_enum, pcibus);
+    qpci_free_pc(pcibus);
+
     counter_shm_init();
 }