Skip to contentSkip to conversation

hw/scsi/lsi53c895a: Fix guest-triggerable assert() in lsi_do_dma()

PATCH Applied Philippe Mathieu-Daudé 3 participants 6 messages Nov 23, 2021 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

cover hw/scsi/lsi53c895a: Fix guest-triggerable assert() in lsi_do_dma()

Trivial fix for this long due bug.

Philippe Mathieu-Daudé (2):
hw/scsi/lsi53c895a: Do not abort when DMA requested and no data queued tests/qtest: Add fuzz-lsi53c895a-test

 hw/scsi/lsi53c895a.c               |  3 +-
 tests/qtest/fuzz-lsi53c895a-test.c | 52 ++++++++++++++++++++++++++++++
 MAINTAINERS                        |  1 +
 tests/qtest/meson.build            |  1 +
 4 files changed, 55 insertions(+), 2 deletions(-)
 create mode 100644 tests/qtest/fuzz-lsi53c895a-test.c
Signature · 1 line
2.33.1
#1 lore raw

PATCH 1/2 hw/scsi/lsi53c895a: Do not abort when DMA requested and no data queued

If asked for DMA request and no data is available, simply wait for data to be queued, do not abort. This fixes:

  $ cat << EOF | \
    qemu-system-i386 -nographic -M q35,accel=qtest -serial none \
      -monitor none -qtest stdio -trace lsi* \
      -drive if=none,id=drive0,file=null-co://,file.read-zeroes=on,format=raw \
      -device lsi53c895a,id=scsi0 -device scsi-hd,drive=drive0,bus=scsi0.0,channel=0,scsi-id=0,lun=0
  lsi_reset Reset
  lsi_reg_write Write reg DSP2 0x2e = 0xff
  lsi_reg_write Write reg DSP3 0x2f = 0xff
  lsi_execute_script SCRIPTS dsp=0xffff0000 opcode 0x184a3900 arg 0x4a8b2d75
  qemu-system-i386: hw/scsi/lsi53c895a.c:624: lsi_do_dma: Assertion `s->current' failed.

(gdb) bt
#5 0x00007ffff4e8a3a6 in __GI___assert_fail

      (assertion=0x5555560accbc "s->current", file=0x5555560acc28 "hw/scsi/lsi53c895a.c", line=624, function=0x5555560adb18 "lsi_do_dma") at assert.c:101
  #6  0x0000555555aa33b9 in lsi_do_dma (s=0x555557805ac0, out=1) at hw/scsi/lsi53c895a.c:624
  #7  0x0000555555aa5042 in lsi_execute_script (s=0x555557805ac0) at hw/scsi/lsi53c895a.c:1250
  #8  0x0000555555aa757a in lsi_reg_writeb (s=0x555557805ac0, offset=47, val=255 '\377') at hw/scsi/lsi53c895a.c:1984
  #9  0x0000555555aa875b in lsi_mmio_write (opaque=0x555557805ac0, addr=47, val=255, size=1) at hw/scsi/lsi53c895a.c:2095

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/84
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/305
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/552

---
 hw/scsi/lsi53c895a.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/scsi/lsi53c895a.c +1 -2
index 85e907a7854..4c431adb774 100644
@@ -621,8 +621,7 @@ static void lsi_do_dma(LSIState *s, int out)
     dma_addr_t addr;
     SCSIDevice *dev;
 
-    assert(s->current);
-    if (!s->current->dma_len) {
+    if (!s->current || !s->current->dma_len) {
         /* Wait until data is available.  */
         trace_lsi_do_dma_unavailable();
         return;
Signature · 1 line
2.33.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH-for-6.2 1/2] hw/scsi/lsi53c895a: Do not abort when DMA requested and no data queued

Quoted 48 lines · Philippe Mathieu-Daudé

If asked for DMA request and no data is available, simply wait for data to be queued, do not abort. This fixes:

   $ cat << EOF | \
     qemu-system-i386 -nographic -M q35,accel=qtest -serial none \
       -monitor none -qtest stdio -trace lsi* \
       -drive if=none,id=drive0,file=null-co://,file.read-zeroes=on,format=raw \
       -device lsi53c895a,id=scsi0 -device scsi-hd,drive=drive0,bus=scsi0.0,channel=0,scsi-id=0,lun=0
   lsi_reset Reset
   lsi_reg_write Write reg DSP2 0x2e = 0xff
   lsi_reg_write Write reg DSP3 0x2f = 0xff
   lsi_execute_script SCRIPTS dsp=0xffff0000 opcode 0x184a3900 arg 0x4a8b2d75
   qemu-system-i386: hw/scsi/lsi53c895a.c:624: lsi_do_dma: Assertion `s->current' failed.
   (gdb) bt
   #5  0x00007ffff4e8a3a6 in __GI___assert_fail
       (assertion=0x5555560accbc "s->current", file=0x5555560acc28 "hw/scsi/lsi53c895a.c", line=624, function=0x5555560adb18 "lsi_do_dma") at assert.c:101
   #6  0x0000555555aa33b9 in lsi_do_dma (s=0x555557805ac0, out=1) at hw/scsi/lsi53c895a.c:624
   #7  0x0000555555aa5042 in lsi_execute_script (s=0x555557805ac0) at hw/scsi/lsi53c895a.c:1250
   #8  0x0000555555aa757a in lsi_reg_writeb (s=0x555557805ac0, offset=47, val=255 '\377') at hw/scsi/lsi53c895a.c:1984
   #9  0x0000555555aa875b in lsi_mmio_write (opaque=0x555557805ac0, addr=47, val=255, size=1) at hw/scsi/lsi53c895a.c:2095

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/84
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/305
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/552

---
  hw/scsi/lsi53c895a.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
hw/scsi/lsi53c895a.c +0 -0
index 85e907a7854..4c431adb774 100644

hw/scsi/lsi53c895a.c static void lsi_do_dma(LSIState *s, int out) · line 627

Quoted 9 lines
hw/scsi/lsi53c895a.c +1 -2
@@ -621,8 +621,7 @@ static void lsi_do_dma(LSIState *s, int out)
      dma_addr_t addr;
      SCSIDevice *dev;
 
-    assert(s->current);
-    if (!s->current->dma_len) {
+    if (!s->current || !s->current->dma_len) {
          /* Wait until data is available.  */
          trace_lsi_do_dma_unavailable();
          return;
#3 lore raw

PATCH 2/2 tests/qtest: Add fuzz-lsi53c895a-test

In reply to Philippe Mathieu-Daudé

Without the previous commit, this test triggers:

  $ make check-qtest-x86_64
  [...]
  Running test qtest-x86_64/fuzz-lsi53c895a-test
  qemu-system-x86_64: hw/scsi/lsi53c895a.c:624: lsi_do_dma: Assertion `s->current' failed.
  ERROR qtest-x86_64/fuzz-lsi53c895a-test - too few tests run (expected 1, got 0)
---
 tests/qtest/fuzz-lsi53c895a-test.c | 52 ++++++++++++++++++++++++++++++
 MAINTAINERS                        |  1 +
 tests/qtest/meson.build            |  1 +
 3 files changed, 54 insertions(+)
 create mode 100644 tests/qtest/fuzz-lsi53c895a-test.c
tests/qtest/fuzz-lsi53c895a-test.c new file +52 -0
new file mode 100644
index 00000000000..ba5d468970c
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * QTest fuzzer-generated testcase for LSI53C895A device
+ *
+ * Copyright (c) Red Hat
+ */
+
+#include "qemu/osdep.h"
+#include "libqos/libqtest.h"
+
+/*
+ * This used to trigger the assert in lsi_do_dma()
+ * https://bugs.launchpad.net/qemu/+bug/697510
+ * https://bugs.launchpad.net/qemu/+bug/1905521
+ * https://bugs.launchpad.net/qemu/+bug/1908515
+ */
+static void test_lsi_do_dma_empty_queue(void)
+{
+    QTestState *s;
+
+    s = qtest_init("-M q35 -nographic -monitor none -serial none "
+                   "-drive if=none,id=drive0,"
+                            "file=null-co://,file.read-zeroes=on,format=raw "
+                   "-device lsi53c895a,id=scsi0 "
+                   "-device scsi-hd,drive=drive0,"
+                            "bus=scsi0.0,channel=0,scsi-id=0,lun=0");
+    qtest_outl(s, 0xcf8, 0x80001814);
+    qtest_outl(s, 0xcfc, 0xe1068000);
+    qtest_outl(s, 0xcf8, 0x80001818);
+    qtest_outl(s, 0xcf8, 0x80001804);
+    qtest_outw(s, 0xcfc, 0x7);
+    qtest_outl(s, 0xcf8, 0x80002010);
+
+    qtest_writeb(s, 0xe106802e, 0xff); /* Fill DSP bits 16-23 */
+    qtest_writeb(s, 0xe106802f, 0xff); /* Fill DSP bits 24-31: trigger SCRIPT */
+
+    qtest_quit(s);
+}
+
+int main(int argc, char **argv)
+{
+    const char *arch = qtest_get_arch();
+
+    g_test_init(&argc, &argv, NULL);
+
+    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
+        qtest_add_func("fuzz/lsi53c895a/lsi_do_dma_empty_queue",
+                       test_lsi_do_dma_empty_queue);
+    }
+
+    return g_test_run();
+}
MAINTAINERS +1 -0
index d3879aa3c12..3c0f76b7801 100644
@@ -1825,6 +1825,7 @@ F: hw/scsi/*
 F: tests/qtest/virtio-scsi-test.c
 F: tests/qtest/fuzz-virtio-scsi-test.c
 F: tests/qtest/am53c974-test.c
+F: tests/qtest/fuzz-lsi53c895a-test.c
 T: git https://github.com/bonzini/qemu.git scsi-next
 
 SSI
tests/qtest/meson.build +1 -0
index c9d8458062f..d2ce20d3047 100644
@@ -19,6 +19,7 @@
 
 qtests_generic = \
   (config_all_devices.has_key('CONFIG_MEGASAS_SCSI_PCI') ? ['fuzz-megasas-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_LSI_SCSI_PCI') ? ['fuzz-lsi53c895a-test'] : []) + \
   (config_all_devices.has_key('CONFIG_VIRTIO_SCSI') ? ['fuzz-virtio-scsi-test'] : []) + \
   (config_all_devices.has_key('CONFIG_SB16') ? ['fuzz-sb16-test'] : []) + \
   (config_all_devices.has_key('CONFIG_SDHCI_PCI') ? ['fuzz-sdcard-test'] : []) + \
Signature · 1 line
2.33.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH-for-6.2 2/2] tests/qtest: Add fuzz-lsi53c895a-test

Quoted 93 lines · Philippe Mathieu-Daudé

Without the previous commit, this test triggers:

   $ make check-qtest-x86_64
   [...]
   Running test qtest-x86_64/fuzz-lsi53c895a-test
   qemu-system-x86_64: hw/scsi/lsi53c895a.c:624: lsi_do_dma: Assertion `s->current' failed.
   ERROR qtest-x86_64/fuzz-lsi53c895a-test - too few tests run (expected 1, got 0)
---
  tests/qtest/fuzz-lsi53c895a-test.c | 52 ++++++++++++++++++++++++++++++
  MAINTAINERS                        |  1 +
  tests/qtest/meson.build            |  1 +
  3 files changed, 54 insertions(+)

create mode 100644 tests/qtest/fuzz-lsi53c895a-test.c

tests/qtest/fuzz-lsi53c895a-test.c new file +52 -0
new file mode 100644
index 00000000000..ba5d468970c
@@ -0,0 +1,52 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * QTest fuzzer-generated testcase for LSI53C895A device
+ *
+ * Copyright (c) Red Hat
+ */
+
+#include "qemu/osdep.h"
+#include "libqos/libqtest.h"
+
+/*
+ * This used to trigger the assert in lsi_do_dma()
+ * https://bugs.launchpad.net/qemu/+bug/697510
+ * https://bugs.launchpad.net/qemu/+bug/1905521
+ * https://bugs.launchpad.net/qemu/+bug/1908515
+ */
+static void test_lsi_do_dma_empty_queue(void)
+{
+    QTestState *s;
+
+    s = qtest_init("-M q35 -nographic -monitor none -serial none "
+                   "-drive if=none,id=drive0,"
+                            "file=null-co://,file.read-zeroes=on,format=raw "
+                   "-device lsi53c895a,id=scsi0 "
+                   "-device scsi-hd,drive=drive0,"
+                            "bus=scsi0.0,channel=0,scsi-id=0,lun=0");
+    qtest_outl(s, 0xcf8, 0x80001814);
+    qtest_outl(s, 0xcfc, 0xe1068000);
+    qtest_outl(s, 0xcf8, 0x80001818);
+    qtest_outl(s, 0xcf8, 0x80001804);
+    qtest_outw(s, 0xcfc, 0x7);
+    qtest_outl(s, 0xcf8, 0x80002010);
+
+    qtest_writeb(s, 0xe106802e, 0xff); /* Fill DSP bits 16-23 */
+    qtest_writeb(s, 0xe106802f, 0xff); /* Fill DSP bits 24-31: trigger SCRIPT */
+
+    qtest_quit(s);
+}
+
+int main(int argc, char **argv)
+{
+    const char *arch = qtest_get_arch();
+
+    g_test_init(&argc, &argv, NULL);
+
+    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
+        qtest_add_func("fuzz/lsi53c895a/lsi_do_dma_empty_queue",
+                       test_lsi_do_dma_empty_queue);
+    }
+
+    return g_test_run();
+}
MAINTAINERS +1 -0
index d3879aa3c12..3c0f76b7801 100644
@@ -1825,6 +1825,7 @@ F: hw/scsi/*
  F: tests/qtest/virtio-scsi-test.c
  F: tests/qtest/fuzz-virtio-scsi-test.c
  F: tests/qtest/am53c974-test.c
+F: tests/qtest/fuzz-lsi53c895a-test.c
  T: git https://github.com/bonzini/qemu.git scsi-next
 
  SSI
tests/qtest/meson.build +0 -0
index c9d8458062f..d2ce20d3047 100644

tests/qtest/meson.build line 25

Quoted 7 lines
tests/qtest/meson.build +1 -0
@@ -19,6 +19,7 @@
 
  qtests_generic = \
    (config_all_devices.has_key('CONFIG_MEGASAS_SCSI_PCI') ? ['fuzz-megasas-test'] : []) + \
+  (config_all_devices.has_key('CONFIG_LSI_SCSI_PCI') ? ['fuzz-lsi53c895a-test'] : []) + \
    (config_all_devices.has_key('CONFIG_VIRTIO_SCSI') ? ['fuzz-virtio-scsi-test'] : []) + \
    (config_all_devices.has_key('CONFIG_SB16') ? ['fuzz-sb16-test'] : []) + \
    (config_all_devices.has_key('CONFIG_SDHCI_PCI') ? ['fuzz-sdcard-test'] : []) + \
#5 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 6 lines · Philippe Mathieu-Daudé

Trivial fix for this long due bug.

Philippe Mathieu-Daudé (2):
hw/scsi/lsi53c895a: Do not abort when DMA requested and no data queued tests/qtest: Add fuzz-lsi53c895a-test

Quoted 7 lines
  hw/scsi/lsi53c895a.c               |  3 +-
  tests/qtest/fuzz-lsi53c895a-test.c | 52 ++++++++++++++++++++++++++++++
  MAINTAINERS                        |  1 +
  tests/qtest/meson.build            |  1 +
  4 files changed, 55 insertions(+), 2 deletions(-)

create mode 100644 tests/qtest/fuzz-lsi53c895a-test.c

Queued, thanks.

Paolo