Skip to contentSkip to conversation

hexagon semihosting: Address coverity finding

PATCH Superseded Brian Cain 2 participants 9 messages Sep 14 – Sep 23

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
author
#3 lore raw

PATCH v2 2/4 target/hexagon: fix semihosting STAT filename handling

In reply to Brian Cain

HEX_SYS_STAT could pass an unterminated local filename buffer to stat() when the guest string filled it. Use lock_user_string() to map the guest string safely.

---
 target/hexagon/hexswi.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
target/hexagon/hexswi.c +7 -7
index 21e5d630a75..d0ba1162a5d 100644
@@ -551,20 +551,20 @@ static void sim_handle_trap0(CPUHexagonState *env)
         struct stat st_buf;
         uint8_t *st_bufptr = (uint8_t *)&sys_stat;
         int rc, err = 0;
-        char filename[BUFSIZ];
+        char *filename;
         target_ulong physical_filename_addr;
         target_ulong statBufferAddr;
         hexagon_read_memory(env, swi_info, 4, &physical_filename_addr, retaddr);
 
         if (what_swi == HEX_SYS_STAT) {
-            int i = 0;
-            do {
-                hexagon_read_memory(env, physical_filename_addr + i, 1,
-                                    &filename[i], retaddr);
-                i++;
-            } while ((i < BUFSIZ) && filename[i - 1]);
+            filename = lock_user_string(physical_filename_addr);
+            if (!filename) {
+                semi_cb(cs, -1, EFAULT);
+                break;
+            }
             rc = stat(filename, &st_buf);
             err = errno;
+            unlock_user(filename, physical_filename_addr, 0);
         } else {
             int fd = physical_filename_addr;
             GuestFD *gf = get_guestfd(fd);
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/4 target/hexagon: fix semihosting OPEN filename handling

In reply to Brian Cain

HEX_SYS_OPEN copied guest bytes into a fixed-size buffer until it found a NUL. A missing terminator could overrun that buffer. Use lock_user() with the ABI filename length and validate its terminating NUL before opening it.

Fixes 7711fdba88b ("target/hexagon: add main arch-specific semihosting operations")
---
 target/hexagon/hexswi.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)
target/hexagon/hexswi.c +16 -18
index 4705e915aea..21e5d630a75 100644
@@ -28,6 +28,7 @@
 #include "semihosting/console.h"
 #include "semihosting/syscalls.h"
 #include "semihosting/guestfd.h"
+#include "semihosting/uaccess.h"
 #include "system/runstate.h"
 
 /* non-arm-compatible semihosting calls */
@@ -461,31 +462,17 @@ static void sim_handle_trap0(CPUHexagonState *env)
 
     case HEX_SYS_OPEN:
     {
-        char filename[BUFSIZ];
+        char *filename;
         target_ulong physical_filename_addr;
         unsigned int filemode;
-        int length;
+        uint32_t filename_len;
+        size_t filename_size;
         int real_openmode;
         int ret, err = 0;
-        int i = 0;
 
         hexagon_read_memory(env, swi_info, 4, &physical_filename_addr, retaddr);
         hexagon_read_memory(env, swi_info + 4, 4, &filemode, retaddr);
-        hexagon_read_memory(env, swi_info + 8, 4, &length, retaddr);
-
-        if (length >= BUFSIZ) {
-            qemu_log_mask(LOG_GUEST_ERROR,
-                          "%s: filename too large (%d)\n",
-                          __func__, length);
-            semi_cb(cs, -1, ENAMETOOLONG);
-            break;
-        }
-
-        do {
-            hexagon_read_memory(env, physical_filename_addr + i, 1,
-                                &filename[i], retaddr);
-            i++;
-        } while (filename[i - 1]);
+        hexagon_read_memory(env, swi_info + 8, 4, &filename_len, retaddr);
 
         /* convert ARM ANGEL filemode into host filemode */
         if (filemode < ARRAY_SIZE(angel_to_host_filemode_table)) {
@@ -498,6 +485,16 @@ static void sim_handle_trap0(CPUHexagonState *env)
             break;
         }
 
+        /* The ABI length excludes the filename's terminating NUL. */
+        filename_size = (size_t)filename_len + 1;
+        filename = lock_user(VERIFY_READ, physical_filename_addr,
+                             filename_size, true);
+        if (!filename || filename[filename_len] != '\0') {
+            unlock_user(filename, physical_filename_addr, 0);
+            semi_cb(cs, -1, EFAULT);
+            break;
+        }
+
         if (strcmp(filename, ":tt") == 0 &&
             qemu_semihosting_console_has_chardev()) {
             ret = alloc_guestfd();
@@ -513,6 +510,7 @@ static void sim_handle_trap0(CPUHexagonState *env)
                 ret = guestfd;
             }
         }
+        unlock_user(filename, physical_filename_addr, 0);
         semi_cb(cs, ret, err);
     }
     break;
Signature · 1 line
2.34.1

Brian Cain

author
#0 lore raw

v2 cover hexagon semihosting: Address coverity finding

These fixes are to mitigate the issue(s) reported by Coverity and Peter Maydell.

In v2:
- HEX_SYS_OPEN switched to lock_user() from lock_user_string()
- pulled in {open,read,close}dir from the a prev patch, changes since that v2:
* Replaces POSIX DIR, opendir(), readdir(), and closedir() with portable GLib GDir APIs. {un,}lock_user
* Replaces the fixed BUFSIZ path buffer and byte-by-byte guest reads with lock_user_string()/unlock_user().
* Adds SemihostingDir, which stores the GDir plus enumeration state
* Synthesizes . and .., omitted by glib
* Writes inode zero instead of host d_ino, which GLib does not expose portably.
* Limits returned names to 254 characters plus NUL, rather than depending on host struct dirent::d_name size. Prev opendir v2: https://lore.kernel.org/qemu-devel/[email protected]/

v1: https://lore.kernel.org/qemu-devel/[email protected]/

Brian Cain (3):
target/hexagon: fix semihosting OPEN filename handling target/hexagon: fix semihosting STAT filename handling target/hexagon: fix semihosting ACCESS filename handling

Matheus Tavares Bernardino (1):
target/hexagon: add directory semihosting operations

 include/hw/hexagon/hexagon.h              |  14 +-
 hw/hexagon/hex-subsys.c                   |  32 ++--
 target/hexagon/hexswi.c                   | 177 +++++++++++++++++-----
 tests/functional/hexagon/test_systests.py |  11 ++
 4 files changed, 186 insertions(+), 48 deletions(-)
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v2 3/4 target/hexagon: fix semihosting ACCESS filename handling

HEX_SYS_ACCESS wrote a NUL byte past its fixed-size filename buffer when the guest string filled it. Use lock_user_string() to avoid the out-of-bounds write.

Fixes 7711fdba88b ("target/hexagon: add main arch-specific semihosting operations")

Resolves: Coverity CID 1685942

---
 target/hexagon/hexswi.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)
target/hexagon/hexswi.c +10 -11
index d0ba1162a5d..e56a60f2f06 100644
@@ -610,25 +610,24 @@ static void sim_handle_trap0(CPUHexagonState *env)
 
     case HEX_SYS_ACCESS:
     {
-        char filename[BUFSIZ];
+        char *filename;
         uint32_t FileNameAddr;
         uint32_t BufferMode;
-        int rc;
-
-        int i = 0;
+        int rc, err;
 
         hexagon_read_memory(env, swi_info, 4, &FileNameAddr, retaddr);
-        do {
-            hexagon_read_memory(env, FileNameAddr + i, 1, &filename[i],
-                                retaddr);
-            i++;
-        } while ((i < BUFSIZ) && (filename[i - 1]));
-        filename[i] = 0;
+        filename = lock_user_string(FileNameAddr);
+        if (!filename) {
+            semi_cb(cs, -1, EFAULT);
+            break;
+        }
 
         hexagon_read_memory(env, swi_info + 4, 4, &BufferMode, retaddr);
 
         rc = access(filename, BufferMode);
-        semi_cb(cs, rc,  rc == 0 ? 0 : errno);
+        err = errno;
+        unlock_user(filename, FileNameAddr, 0);
+        semi_cb(cs, rc,  rc == 0 ? 0 : err);
     }
     break;
 
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v2 4/4 target/hexagon: add directory semihosting operations

In reply to Brian Cain

From: Matheus Tavares Bernardino <[email protected]>

Bare-metal Hexagon programs use OPENDIR, READDIR, and CLOSEDIR semihosting calls to enumerate host directories. Directory handles are shared by CPUs in a cluster, allowing guest indices to resolve across calls.

Use lock_user_string()/unlock_user() to read the OPENDIR path from guest memory instead of copying it into a fixed-size buffer.

CLOSEDIR clears its slot in the directory list, so a stale index reports EBADF instead of dereferencing a freed pointer.

Also add a functional test for the new semihosting operations.

---
 include/hw/hexagon/hexagon.h              |  14 ++-
 hw/hexagon/hex-subsys.c                   |  32 ++++---
 target/hexagon/hexswi.c                   | 108 ++++++++++++++++++++++
 tests/functional/hexagon/test_systests.py |  11 +++
 4 files changed, 153 insertions(+), 12 deletions(-)
include/hw/hexagon/hexagon.h +13 -1
index 62398eeb359..58bdcabe87e 100644
@@ -11,6 +11,7 @@
 
 #include "system/memory.h"
 #include "hw/core/boards.h"
+#include "hw/cpu/cluster.h"
 
 struct hexagon_board_boot_info {
     uint64_t ram_size;
@@ -159,6 +160,17 @@ struct hexagon_machine_config {
     union hexagon_config_table cfgtable;
 };
 
+#define TYPE_HEXAGON_CLUSTER_STATE "hexagon-cluster-state"
+OBJECT_DECLARE_SIMPLE_TYPE(HexagonClusterState, HEXAGON_CLUSTER_STATE)
+
+struct HexagonClusterState {
+    CPUClusterState parent_obj;
+
+    struct {
+        GList *dir_list;
+    } semihosting;
+};
+
 #define TYPE_HEXAGON_COMMON_MACHINE "hexagon-common-machine"
 OBJECT_DECLARE_SIMPLE_TYPE(HexagonCommonMachineState, HEXAGON_COMMON_MACHINE)
 
@@ -168,7 +180,7 @@ struct HexagonCommonMachineState {
     MemoryRegion ram;
     MemoryRegion cfgtable_rom;
     MemoryRegion vtcm;
-    DeviceState *cluster;
+    HexagonClusterState cluster;
     DeviceState *l2vic;
     DeviceState *qtimer;
     DeviceState *glob_regs;
hw/hexagon/hex-subsys.c +21 -11
index 4e3a418340e..f76f803dfb0 100644
@@ -98,14 +98,11 @@ static DeviceState *tlb_create(HexagonCommonMachineState *hms,
     return tlb;
 }
 
-static DeviceState *cluster_create(HexagonCommonMachineState *hms)
+static void cluster_create(HexagonCommonMachineState *hms)
 {
-    DeviceState *cluster = qdev_new(TYPE_CPU_CLUSTER);
-
-    object_property_add_child(OBJECT(hms), "cluster", OBJECT(cluster));
-    qdev_prop_set_uint32(cluster, "cluster-id", 0);
-
-    return cluster;
+    object_initialize_child(OBJECT(hms), "cluster", &hms->cluster,
+                            TYPE_HEXAGON_CLUSTER_STATE);
+    qdev_prop_set_uint32(DEVICE(&hms->cluster), "cluster-id", 0);
 }
 
 void hex_subsys_create(HexagonCommonMachineState *hms,
@@ -135,7 +132,7 @@ void hex_subsys_create(HexagonCommonMachineState *hms,
                                     &hms->vtcm);
     }
 
-    hms->cluster = cluster_create(hms);
+    cluster_create(hms);
     hms->l2vic = l2vic_create(hms, m_cfg);
     hms->qtimer = qtimer_create(hms, m_cfg);
     hms->glob_regs = globalreg_create(hms, m_cfg, rev);
@@ -144,7 +141,7 @@ void hex_subsys_create(HexagonCommonMachineState *hms,
 
 void hex_subsys_add_cpu(HexagonCommonMachineState *hms, DeviceState *cpu)
 {
-    object_property_add_child(OBJECT(hms->cluster), "cpu[*]", OBJECT(cpu));
+    object_property_add_child(OBJECT(&hms->cluster), "cpu[*]", OBJECT(cpu));
     object_property_set_link(OBJECT(cpu), "global-regs",
                              OBJECT(hms->glob_regs), &error_fatal);
     object_property_set_link(OBJECT(cpu), "tlb", OBJECT(hms->tlb),
@@ -158,10 +155,10 @@ void hex_subsys_realize_cluster(HexagonCommonMachineState *hms)
     /*
      * The cluster must be realized after its CPUs have been parented into it
      * (see hex_subsys_add_cpu()) but before any CPU is itself realized, since
-     * qdev_realize_and_unref() on a CPU latches cluster_index into the TCG
+     * qdev_realize() on a CPU latches cluster_index into the TCG
      * cflags at that point.
      */
-    qdev_realize_and_unref(hms->cluster, NULL, &error_fatal);
+    qdev_realize(DEVICE(&hms->cluster), NULL, &error_fatal);
 }
 
 void hex_subsys_realize_cpu(HexagonCommonMachineState *hms, DeviceState *cpu,
@@ -173,3 +170,16 @@ void hex_subsys_realize_cpu(HexagonCommonMachineState *hms, DeviceState *cpu,
         l2vic_connect_cpu(hms->l2vic, cpu);
     }
 }
+
+static const TypeInfo hexagon_cluster_type_info = {
+    .name = TYPE_HEXAGON_CLUSTER_STATE,
+    .parent = TYPE_CPU_CLUSTER,
+    .instance_size = sizeof(HexagonClusterState),
+};
+
+static void hexagon_cluster_register_types(void)
+{
+    type_register_static(&hexagon_cluster_type_info);
+}
+
+type_init(hexagon_cluster_register_types)
target/hexagon/hexswi.c +108 -0

132 lines not on this page · raw message

tests/functional/hexagon/test_systests.py +11 -0
index 2779efa9172..a1080f7b974 100755
@@ -7,6 +7,7 @@
 import re
 import time
 import unittest
+from pathlib import Path
 
 from qemu_test import QemuSystemTest, Asset, wait_for_console_pattern
 
@@ -99,5 +100,15 @@ def test_mmu_multi_tlb(self):
     def test_timer_reg(self):
         self.run_exit_zero("timer_reg")
 
+    def test_dirent(self):
+        testdir = Path(self.scratch_file("_testdir_dirent"))
+        testdir.mkdir()
+        files = ["file1", "file2"]
+        for f in files:
+            testdir.joinpath(f).touch()
+        expected = ". .. " + " ".join(files)
+        self.run_console_pattern("dirent", expected, "-append",
+                                 str(testdir))
+
 if __name__ == "__main__":
     QemuSystemTest.main()
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/4] target/hexagon: fix semihosting OPEN filename handling

In reply to Brian Cain
Quoted 48 lines · Brian Cain

HEX_SYS_OPEN copied guest bytes into a fixed-size buffer until it found a NUL. A missing terminator could overrun that buffer. Use lock_user() with the ABI filename length and validate its terminating NUL before opening it.

Fixes 7711fdba88b ("target/hexagon: add main arch-specific semihosting operations")
---
 target/hexagon/hexswi.c | 34 ++++++++++++++++------------------
 1 file changed, 16 insertions(+), 18 deletions(-)
target/hexagon/hexswi.c +4 -9
index 4705e915aea..21e5d630a75 100644
@@ -28,6 +28,7 @@
 #include "semihosting/console.h"
 #include "semihosting/syscalls.h"
 #include "semihosting/guestfd.h"
+#include "semihosting/uaccess.h"
 #include "system/runstate.h"
 
 /* non-arm-compatible semihosting calls */
@@ -461,31 +462,17 @@ static void sim_handle_trap0(CPUHexagonState *env)
 
     case HEX_SYS_OPEN:
     {
-        char filename[BUFSIZ];
+        char *filename;
         target_ulong physical_filename_addr;
         unsigned int filemode;
-        int length;
+        uint32_t filename_len;
+        size_t filename_size;
         int real_openmode;
         int ret, err = 0;
-        int i = 0;
 
         hexagon_read_memory(env, swi_info, 4, &physical_filename_addr, retaddr);
         hexagon_read_memory(env, swi_info + 4, 4, &filemode, retaddr);
-        hexagon_read_memory(env, swi_info + 8, 4, &length, retaddr);
-
-        if (length >= BUFSIZ) {
-            qemu_log_mask(LOG_GUEST_ERROR,
-                          "%s: filename too large (%d)\n",
-                          __func__, length);

target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 475

Quoted 10 lines
target/hexagon/hexswi.c +1 -9
-            semi_cb(cs, -1, ENAMETOOLONG);
-            break;
-        }
-
-        do {
-            hexagon_read_memory(env, physical_filename_addr + i, 1,
-                                &filename[i], retaddr);
-            i++;
-        } while (filename[i - 1]);
+        hexagon_read_memory(env, swi_info + 8, 4, &filename_len, retaddr);

This looks like an endianness bug -- we read 4 bytes of guest-endianness length into filename_len and then treat it as a host value without doing any byteswapping. Could we use get_user_u32() here ? That helper does the memory load and any necessary byteswapping for you.

(This seems to be a problem with a lot of the code in this file that's using hexagon_read_memory() to read from guest memory into host variables. If you made it use the uaccess.h helpers instead you would fix all those big-endian-host bugs and you might find that you don't need hexagon_read_memory() at all once you've done that.)

Quoted 6 lines
target/hexagon/hexswi.c +0 -0
         /* convert ARM ANGEL filemode into host filemode */
         if (filemode < ARRAY_SIZE(angel_to_host_filemode_table)) {
@@ -498,6 +485,16 @@ static void sim_handle_trap0(CPUHexagonState *env)
             break;
         }
 

target/hexagon/hexswi.c static void sim_handle_trap0(CPUHexagonState *env) · line 497

Quoted 10 lines
target/hexagon/hexswi.c +10 -0
+        /* The ABI length excludes the filename's terminating NUL. */
+        filename_size = (size_t)filename_len + 1;
+        filename = lock_user(VERIFY_READ, physical_filename_addr,
+                             filename_size, true);
+        if (!filename || filename[filename_len] != '\0') {
+            unlock_user(filename, physical_filename_addr, 0);
+            semi_cb(cs, -1, EFAULT);
+            break;
+        }
+

This splits the two parts of handling the filename argument so that the "check and convert the filemode argument" code is in the middle of it. Could we put all the filename-argument code in one place?

-- PMM

#6 lore raw

Re: [PATCH v2 4/4] target/hexagon: add directory semihosting operations

In reply to Brian Cain
Quoted 40 lines · Brian Cain

From: Matheus Tavares Bernardino <[email protected]>

Bare-metal Hexagon programs use OPENDIR, READDIR, and CLOSEDIR semihosting calls to enumerate host directories. Directory handles are shared by CPUs in a cluster, allowing guest indices to resolve across calls.

Use lock_user_string()/unlock_user() to read the OPENDIR path from guest memory instead of copying it into a fixed-size buffer.

CLOSEDIR clears its slot in the directory list, so a stale index reports EBADF instead of dereferencing a freed pointer.

Also add a functional test for the new semihosting operations.

---
 include/hw/hexagon/hexagon.h              |  14 ++-
 hw/hexagon/hex-subsys.c                   |  32 ++++---
 target/hexagon/hexswi.c                   | 108 ++++++++++++++++++++++
 tests/functional/hexagon/test_systests.py |  11 +++
 4 files changed, 153 insertions(+), 12 deletions(-)
include/hw/hexagon/hexagon.h +1 -0
index 62398eeb359..58bdcabe87e 100644
@@ -11,6 +11,7 @@
 
 #include "system/memory.h"
 #include "hw/core/boards.h"
+#include "hw/cpu/cluster.h"
 
 struct hexagon_board_boot_info {
     uint64_t ram_size;
@@ -159,6 +160,17 @@ struct hexagon_machine_config {
     union hexagon_config_table cfgtable;
 };
 

include/hw/hexagon/hexagon.h struct hexagon_machine_config { · line 172

Quoted 10 lines
include/hw/hexagon/hexagon.h +10 -0
+#define TYPE_HEXAGON_CLUSTER_STATE "hexagon-cluster-state"
+OBJECT_DECLARE_SIMPLE_TYPE(HexagonClusterState, HEXAGON_CLUSTER_STATE)
+
+struct HexagonClusterState {
+    CPUClusterState parent_obj;
+
+    struct {
+        GList *dir_list;
+    } semihosting;
+};

This looks very odd. Why do we need to track open directory handles in this special per-cluster structure, but we don't need to do that for e.g. open file handles returned by SYS_OPEN ? I can't see anything in the semihosting specification that indicates that SYS_OPENDIR has special behaviour like that.

thanks
-- PMM

author
#7 lore raw

Re: [PATCH v2 4/4] target/hexagon: add directory semihosting operations

Quoted 40 lines · Peter Maydell
Quoted 39 lines · Brian Cain

From: Matheus Tavares Bernardino <[email protected]>

Bare-metal Hexagon programs use OPENDIR, READDIR, and CLOSEDIR semihosting calls to enumerate host directories. Directory handles are shared by CPUs in a cluster, allowing guest indices to resolve across calls.

Use lock_user_string()/unlock_user() to read the OPENDIR path from guest memory instead of copying it into a fixed-size buffer.

CLOSEDIR clears its slot in the directory list, so a stale index reports EBADF instead of dereferencing a freed pointer.

Also add a functional test for the new semihosting operations.

---
  include/hw/hexagon/hexagon.h              |  14 ++-
  hw/hexagon/hex-subsys.c                   |  32 ++++---
  target/hexagon/hexswi.c                   | 108 ++++++++++++++++++++++
  tests/functional/hexagon/test_systests.py |  11 +++
  4 files changed, 153 insertions(+), 12 deletions(-)
include/hw/hexagon/hexagon.h +1 -0
index 62398eeb359..58bdcabe87e 100644
@@ -11,6 +11,7 @@
 
  #include "system/memory.h"
  #include "hw/core/boards.h"
+#include "hw/cpu/cluster.h"
 
  struct hexagon_board_boot_info {
      uint64_t ram_size;
@@ -159,6 +160,17 @@ struct hexagon_machine_config {
      union hexagon_config_table cfgtable;
  };
 
Quoted 15 lines

include/hw/hexagon/hexagon.h struct hexagon_machine_config { · line 172

Quoted 10 lines
include/hw/hexagon/hexagon.h +10 -0
+#define TYPE_HEXAGON_CLUSTER_STATE "hexagon-cluster-state"
+OBJECT_DECLARE_SIMPLE_TYPE(HexagonClusterState, HEXAGON_CLUSTER_STATE)
+
+struct HexagonClusterState {
+    CPUClusterState parent_obj;
+
+    struct {
+        GList *dir_list;
+    } semihosting;
+};

This looks very odd. Why do we need to track open directory handles in this special per-cluster structure, but we don't need to do that for e.g. open file handles returned by SYS_OPEN ? I can't see anything in the semihosting specification that indicates that SYS_OPENDIR has special behaviour like that.

Here's where it was previously discussed:

https://lore.kernel.org/qemu-devel/[email protected]/

It's my understanding that unlike with file descriptors, we can't have a host-portable way to provide directory entries spanning distinct semihosting calls without keeping state like this.  If all host OSs kept this state, we wouldn't need to.

Quoted 3 lines

thanks
-- PMM

author
#8 lore raw

Re: [PATCH v2 4/4] target/hexagon: add directory semihosting operations

Quoted 59 lines · Brian Cain
Quoted 41 lines · Peter Maydell
Quoted 40 lines · Brian Cain

From: Matheus Tavares Bernardino <[email protected]>

Bare-metal Hexagon programs use OPENDIR, READDIR, and CLOSEDIR semihosting calls to enumerate host directories. Directory handles are shared by CPUs in a cluster, allowing guest indices to resolve across calls.

Use lock_user_string()/unlock_user() to read the OPENDIR path from guest memory instead of copying it into a fixed-size buffer.

CLOSEDIR clears its slot in the directory list, so a stale index reports EBADF instead of dereferencing a freed pointer.

Also add a functional test for the new semihosting operations.

---
  include/hw/hexagon/hexagon.h              |  14 ++- hw/hexagon/hex-subsys.c                   |  32 ++++--- target/hexagon/hexswi.c                   | 108
++++++++++++++++++++++
  tests/functional/hexagon/test_systests.py |  11 +++

  4 files changed, 153 insertions(+), 12 deletions(-)
include/hw/hexagon/hexagon.h +1 -0
index 62398eeb359..58bdcabe87e 100644
@@ -11,6 +11,7 @@
 
   #include "system/memory.h"
   #include "hw/core/boards.h"
+#include "hw/cpu/cluster.h"
 
   struct hexagon_board_boot_info {
       uint64_t ram_size;
@@ -159,6 +160,17 @@ struct hexagon_machine_config {
       union hexagon_config_table cfgtable;
   };
 
Quoted 15 lines

include/hw/hexagon/hexagon.h struct hexagon_machine_config { · line 172

Quoted 10 lines
include/hw/hexagon/hexagon.h +10 -0
+#define TYPE_HEXAGON_CLUSTER_STATE "hexagon-cluster-state"
+OBJECT_DECLARE_SIMPLE_TYPE(HexagonClusterState, HEXAGON_CLUSTER_STATE)
+
+struct HexagonClusterState {
+    CPUClusterState parent_obj;
+
+    struct {
+        GList *dir_list;
+    } semihosting;
+};

This looks very odd. Why do we need to track open directory handles in this special per-cluster structure, but we don't need to do that for e.g. open file handles returned by SYS_OPEN ? I can't see anything in the semihosting specification that indicates that SYS_OPENDIR has special behaviour like that.

Quoted 7 lines

Here's where it was previously discussed:

https://lore.kernel.org/qemu-devel/[email protected]/

It's my understanding that unlike with file descriptors, we can't have a host-portable way to provide directory entries spanning distinct semihosting calls without keeping state like this.  If all host OSs kept this state, we wouldn't need to.

If there's concerns/objections to the design here then I'll drop this patch from the series so it can focus on mitigating just the coverity finding(s).  And then I'd put the {open,read,close}dir revisions in its own series.

Quoted 4 lines
Quoted 4 lines

thanks
-- PMM