Skip to contentSkip to conversation

semihosting: Fix a few semihosting bugs

PATCH Applied Sean Anderson 6 participants 18 messages Oct 18, 2025 – Oct 31, 2025

lore
Share & tools
lore
#0 lore raw

cover semihosting: Fix a few semihosting bugs

While discussing [1], it came to my attention that QEMU does not properly truncate/error SYS_FLEN on 32-bit systems. Fix this, and some other bugs with GDB File I/O that I found while working on this series. That said, GDB File I/O has been substantially broken for two years now, so it makes me wonder if anyone actually uses it! It would certainly simplify the implementation if we didn't have to support it.

[1] https://lore.kernel.org/u-boot/20251017195322.GF6688@bill-the-cat/T/#m493c42570d3103b8c606c5f50faeb78d27719de6

Sean Anderson (3):
gdbstub: Fix %s formatting
semihosting: Fix GDB File-I/O FLEN
semihosting: Check for overflow in FLEN on 32-bit systems

 gdbstub/syscalls.c            |  2 +-
 semihosting/arm-compat-semi.c | 22 +++++++++++++++-------
 2 files changed, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.35.1.1320.gc452695387.dirty
#1 lore raw

PATCH 1/3 gdbstub: Fix %s formatting

The format string for %s has two format characters. This causes it to emit strings like "466f5bd8/6x" instead of "466f5bd8/6". GDB detects this and returns EIO, causing all open File I/O calls to fail.

Fixes 0820a075af ("gdbstub: Adjust gdb_do_syscall to only use uint32_t and uint64_t")

---

 gdbstub/syscalls.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
gdbstub/syscalls.c +1 -1
index e855df21ab..d8bb90cc1c 100644
@@ -127,7 +127,7 @@ void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
             case 's':
                 i64 = va_arg(va, uint64_t);
                 i32 = va_arg(va, uint32_t);
-                p += snprintf(p, p_end - p, "%" PRIx64 "/%x" PRIx32, i64, i32);
+                p += snprintf(p, p_end - p, "%" PRIx64 "/%" PRIx32, i64, i32);
                 break;
             default:
             bad_format:
Signature · 1 line
2.35.1.1320.gc452695387.dirty
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] gdbstub: Fix %s formatting

Quoted 16 lines · Sean Anderson

The format string for %s has two format characters. This causes it to emit strings like "466f5bd8/6x" instead of "466f5bd8/6". GDB detects this and returns EIO, causing all open File I/O calls to fail.

Fixes 0820a075af ("gdbstub: Adjust gdb_do_syscall to only use uint32_t and uint64_t")

---

  gdbstub/syscalls.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
gdbstub/syscalls.c +0 -0
index e855df21ab..d8bb90cc1c 100644

gdbstub/syscalls.c void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...) · line 133

Quoted 8 lines
gdbstub/syscalls.c +1 -1
@@ -127,7 +127,7 @@ void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
              case 's':
                  i64 = va_arg(va, uint64_t);
                  i32 = va_arg(va, uint32_t);
-                p += snprintf(p, p_end - p, "%" PRIx64 "/%x" PRIx32, i64, i32);
+                p += snprintf(p, p_end - p, "%" PRIx64 "/%" PRIx32, i64, i32);
                  break;
              default:
              bad_format:

r~

Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] gdbstub: Fix %s formatting

In reply to Sean Anderson
Quoted 6 lines · Sean Anderson

The format string for %s has two format characters. This causes it to emit strings like "466f5bd8/6x" instead of "466f5bd8/6". GDB detects this and returns EIO, causing all open File I/O calls to fail.

Fixes 0820a075af ("gdbstub: Adjust gdb_do_syscall to only use uint32_t and uint64_t")
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#4 lore raw

PATCH 2/3 semihosting: Fix GDB File-I/O FLEN

In reply to Sean Anderson

fstat returns 0 on success and -1 on error. Since we have already checked for error, ret must be zero. Therefore, any call to fstat on a non-empty file will return -1/EOVERFLOW.

Restore the original logic that just did a byteswap. I don't really know what the intention of the fixed commit was.

Fixes a6300ed6b7 ("semihosting: Split out semihost_sys_flen")

---

 semihosting/arm-compat-semi.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
semihosting/arm-compat-semi.c +1 -4
index 6100126796..c5a07cb947 100644
@@ -316,10 +316,7 @@ common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err)
                                 &size, 8, 0)) {
             ret = -1, err = EFAULT;
         } else {
-            size = be64_to_cpu(size);
-            if (ret != size) {
-                ret = -1, err = EOVERFLOW;
-            }
+            ret = be64_to_cpu(size);
         }
     }
     common_semi_cb(cs, ret, err);
Signature · 1 line
2.35.1.1320.gc452695387.dirty
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] semihosting: Fix GDB File-I/O FLEN

Quoted 20 lines · Sean Anderson

fstat returns 0 on success and -1 on error. Since we have already checked for error, ret must be zero. Therefore, any call to fstat on a non-empty file will return -1/EOVERFLOW.

Restore the original logic that just did a byteswap. I don't really know what the intention of the fixed commit was.

Fixes a6300ed6b7 ("semihosting: Split out semihost_sys_flen")

---

 semihosting/arm-compat-semi.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
semihosting/arm-compat-semi.c +0 -0
index 6100126796..c5a07cb947 100644
@@ -316,10 +316,7 @@ common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err)
                                 &size, 8, 0)) {

semihosting/arm-compat-semi.c common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err) · line 322

Quoted 10 lines
semihosting/arm-compat-semi.c +1 -4
             ret = -1, err = EFAULT;
         } else {
-            size = be64_to_cpu(size);
-            if (ret != size) {
-                ret = -1, err = EOVERFLOW;
-            }
+            ret = be64_to_cpu(size);
         }
     }
     common_semi_cb(cs, ret, err);

well this sent me on a rat hole to figure out the be64_to_cpu. Really I think the callback function should be renamed to gdb_semi_flen_fstat_cb because it is only called for the GDB File I/O implementation.

Otherwise the logic looks fine:

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#6 lore raw

PATCH 3/3 semihosting: Check for overflow in FLEN on 32-bit systems

In reply to Sean Anderson

When semihosting 32-bit systems, the return value of FLEN will be stored in a 32-bit integer. To prevent wraparound, return -1 and set EOVERFLOW. This matches the behavior of stat(2). Static files don't need to be checked, since are always small.

---

 semihosting/arm-compat-semi.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
semihosting/arm-compat-semi.c +14 -3
index c5a07cb947..57453ca6be 100644
@@ -305,8 +305,19 @@ static uint64_t common_semi_flen_buf(CPUState *cs)
     return sp - 64;
 }
 
+static void common_semi_flen_cb(CPUState *cs, uint64_t ret, int err)
+{
+    CPUArchState *env = cpu_env(cs);
+
+    if (!err && !is_64bit_semihosting(env) && ret > INT32_MAX) {
+            ret = -1, err = EOVERFLOW;
+    }
+    common_semi_cb(cs, ret, err);
+}
+
+
 static void
-common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err)
+common_semi_flen_gdb_cb(CPUState *cs, uint64_t ret, int err)
 {
     if (!err) {
         /* The size is always stored in big-endian order, extract the value. */
@@ -319,7 +330,7 @@ common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err)
             ret = be64_to_cpu(size);
         }
     }
-    common_semi_cb(cs, ret, err);
+    common_semi_flen_cb(cs, ret, err);
 }
 
 static void
@@ -517,7 +528,7 @@ void do_common_semihosting(CPUState *cs)
 
     case TARGET_SYS_FLEN:
         GET_ARG(0);
-        semihost_sys_flen(cs, common_semi_flen_fstat_cb, common_semi_cb,
+        semihost_sys_flen(cs, common_semi_flen_gdb_cb, common_semi_flen_cb,
                           arg0, common_semi_flen_buf(cs));
         break;
 
Signature · 1 line
2.35.1.1320.gc452695387.dirty
#7 lore raw

Re: [PATCH 3/3] semihosting: Check for overflow in FLEN on 32-bit systems

Quoted 16 lines · Sean Anderson

When semihosting 32-bit systems, the return value of FLEN will be stored in a 32-bit integer. To prevent wraparound, return -1 and set EOVERFLOW. This matches the behavior of stat(2). Static files don't need to be checked, since are always small.

---

  semihosting/arm-compat-semi.c | 17 ++++++++++++++---
  1 file changed, 14 insertions(+), 3 deletions(-)
semihosting/arm-compat-semi.c +0 -0
index c5a07cb947..57453ca6be 100644

semihosting/arm-compat-semi.c static uint64_t common_semi_flen_buf(CPUState *cs) · line 312

Quoted 8 lines
semihosting/arm-compat-semi.c +5 -0
@@ -305,8 +305,19 @@ static uint64_t common_semi_flen_buf(CPUState *cs)
      return sp - 64;
  }
 
+static void common_semi_flen_cb(CPUState *cs, uint64_t ret, int err)
+{
+    CPUArchState *env = cpu_env(cs);
+
+    if (!err && !is_64bit_semihosting(env) && ret > INT32_MAX) {

The issue with the current implementation is that files with file sizes over 4 GiB will be reported as file size < 4 -GiB on 32bit systems. Thanks for addressing this.

But unfortunately with your change you are additionally dropping support for file sizes 2 GiB to 4 GiB on 32bit devices. This should be avoided.

The semihosting specification specifies that the value returned in r0 should be -1 if an error occurs. So on 32 bit systems 0xffffffff should be returned.

As file sizes cannot be negative there is not reason to assume that the value in r0 has to be interpreted by semihosting clients as signed.

Please, change your commit to check against 0xffffffff.

It might make sense to contact ARM to make their specification clearer.

Best regards

Heinrich

Quoted 30 lines
semihosting/arm-compat-semi.c +9 -3
+            ret = -1, err = EOVERFLOW;
+    }
+    common_semi_cb(cs, ret, err);
+}
+
+
  static void
-common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err)
+common_semi_flen_gdb_cb(CPUState *cs, uint64_t ret, int err)
  {
      if (!err) {
          /* The size is always stored in big-endian order, extract the value. */
@@ -319,7 +330,7 @@ common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err)
              ret = be64_to_cpu(size);
          }
      }
-    common_semi_cb(cs, ret, err);
+    common_semi_flen_cb(cs, ret, err);
  }
 
  static void
@@ -517,7 +528,7 @@ void do_common_semihosting(CPUState *cs)
 
      case TARGET_SYS_FLEN:
          GET_ARG(0);
-        semihost_sys_flen(cs, common_semi_flen_fstat_cb, common_semi_cb,
+        semihost_sys_flen(cs, common_semi_flen_gdb_cb, common_semi_flen_cb,
                            arg0, common_semi_flen_buf(cs));
          break;
 
#8 lore raw

Re: [PATCH 3/3] semihosting: Check for overflow in FLEN on 32-bit systems

Quoted 30 lines · Heinrich Schuchardt
Quoted 16 lines · Sean Anderson

When semihosting 32-bit systems, the return value of FLEN will be stored in a 32-bit integer. To prevent wraparound, return -1 and set EOVERFLOW. This matches the behavior of stat(2). Static files don't need to be checked, since are always small.

---

  semihosting/arm-compat-semi.c | 17 ++++++++++++++---

  1 file changed, 14 insertions(+), 3 deletions(-)
semihosting/arm-compat-semi.c +0 -0
index c5a07cb947..57453ca6be 100644

semihosting/arm-compat-semi.c static uint64_t common_semi_flen_buf(CPUState *cs) · line 311

Quoted 7 lines
semihosting/arm-compat-semi.c +4 -0
@@ -305,8 +305,19 @@ static uint64_t common_semi_flen_buf(CPUState *cs)
       return sp - 64;
   }
   +static void common_semi_flen_cb(CPUState *cs, uint64_t ret, int err)
+{
+    CPUArchState *env = cpu_env(cs);
+
+    if (!err && !is_64bit_semihosting(env) && ret > INT32_MAX) {

The issue with the current implementation is that files with file sizes over 4 GiB will be reported as file size < 4 -GiB on 32bit systems. Thanks for addressing this.

But unfortunately with your change you are additionally dropping support for file sizes 2 GiB to 4 GiB on 32bit devices. This should be avoided.

Quoted 7 lines

The semihosting specification specifies that the value returned in r0 should be -1 if an error occurs. So on 32 bit systems 0xffffffff should be returned.

As file sizes cannot be negative there is not reason to assume that the value in r0 has to be interpreted by semihosting clients as signed.

Please, change your commit to check against 0xffffffff.

It might make sense to contact ARM to make their specification clearer.

stat(2) will return -1/EOVERFLOW on 32-bit hosts for files over 2 GiB. I believe we should be consistent.

--Sean

#9 lore raw

Re: [PATCH 3/3] semihosting: Check for overflow in FLEN on 32-bit systems

Sean Anderson <[email protected]> schrieb am Mo., 20. Okt. 2025, 16:21:

Quoted 41 lines · Heinrich Schuchardt
Quoted 7 lines · Sean Anderson
Quoted 2 lines

When semihosting 32-bit systems, the return value of FLEN will be stored in a 32-bit integer. To prevent wraparound, return -1 and set EOVERFLOW.

This matches the behavior of stat(2). Static files don't need to be

Quoted 2 lines

checked, since are always small.

Quoted 7 lines

Review

Quoted 7 lines

---

  semihosting/arm-compat-semi.c | 17 ++++++++++++++---
  1 file changed, 14 insertions(+), 3 deletions(-)
(quoted diff) +0 -0

b/semihosting/arm-compat-semi.c

Quoted 6 lines
Quoted 6 lines

index c5a07cb947..57453ca6be 100644

semihosting/arm-compat-semi.c +0 -0
@@ -305,8 +305,19 @@ static uint64_t common_semi_flen_buf(CPUState *cs)
      return sp - 64;
  }
Quoted 8 lines

semihosting/arm-compat-semi.c static uint64_t common_semi_flen_buf(CPUState *cs) · line 312

Quoted 5 lines
semihosting/arm-compat-semi.c +4 -0
  +static void common_semi_flen_cb(CPUState *cs, uint64_t ret, int err)
+{
+    CPUArchState *env = cpu_env(cs);
+
+    if (!err && !is_64bit_semihosting(env) && ret > INT32_MAX) {

The issue with the current implementation is that files with file sizes

over 4 GiB will be reported as file size < 4 -GiB on 32bit systems. Thanks for addressing this.

Quoted 2 lines

But unfortunately with your change you are additionally dropping support

for file sizes 2 GiB to 4 GiB on 32bit devices. This should be avoided.

Quoted 2 lines

The semihosting specification specifies that the value returned in r0

should be -1 if an error occurs. So on 32 bit systems 0xffffffff should be returned.

Quoted 2 lines

As file sizes cannot be negative there is not reason to assume that the

Quoted 9 lines

value in r0 has to be interpreted by semihosting clients as signed.

Quoted 4 lines

Please, change your commit to check against 0xffffffff.

It might make sense to contact ARM to make their specification clearer.

stat(2) will return -1/EOVERFLOW on 32-bit hosts for files over 2 GiB. I believe we should be consistent.

That may have been true historically.

Current 32-bit Linux supports 64-bit file systems and reports the length of files beyond 2 GiB without error.

Best regards

Heinrich

#10 lore raw

Re: [PATCH 3/3] semihosting: Check for overflow in FLEN on 32-bit systems

Quoted 47 lines · Heinrich Schuchardt

Sean Anderson <[email protected] <mailto:[email protected]>> schrieb am Mo., 20. Okt. 2025, 16:21:

    On 10/18/25 03:21, Heinrich Schuchardt wrote:
    > On 10/17/25 23:35, Sean Anderson wrote:
    >> When semihosting 32-bit systems, the return value of FLEN will be stored
    >> in a 32-bit integer. To prevent wraparound, return -1 and set EOVERFLOW.

    >> This matches the behavior of stat(2). Static files don't need to be
    >> checked, since are always small.
    >>
    >> Signed-off-by: Sean Anderson <[email protected] <mailto:[email protected]>>
    >> ---
    >>
    >>   semihosting/arm-compat-semi.c | 17 ++++++++++++++---
    >>   1 file changed, 14 insertions(+), 3 deletions(-)
    >>
    >> diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c
    >> index c5a07cb947..57453ca6be 100644
    >> --- a/semihosting/arm-compat-semi.c
    >> +++ b/semihosting/arm-compat-semi.c
    >> @@ -305,8 +305,19 @@ static uint64_t common_semi_flen_buf(CPUState *cs)
    >>       return sp - 64;
    >>   }
    >>   +static void common_semi_flen_cb(CPUState *cs, uint64_t ret, int err)
    >> +{
    >> +    CPUArchState *env = cpu_env(cs);
    >> +
    >> +    if (!err && !is_64bit_semihosting(env) && ret > INT32_MAX) {
    >
    >
    > The issue with the current implementation is that files with file sizes over 4 GiB will be reported as file size < 4 -GiB on 32bit systems. Thanks for addressing this.
    >
    > But unfortunately with your change you are additionally dropping support for file sizes 2 GiB to 4 GiB on 32bit devices. This should be avoided.
    >
    > The semihosting specification specifies that the value returned in r0 should be -1 if an error occurs. So on 32 bit systems 0xffffffff should be returned.
    >
    > As file sizes cannot be negative there is not reason to assume that the value in r0 has to be interpreted by semihosting clients as signed.
    >
    > Please, change your commit to check against 0xffffffff.
    >
    > It might make sense to contact ARM to make their specification clearer.

    stat(2) will return -1/EOVERFLOW on 32-bit hosts for files over 2 GiB. I believe we should be consistent.
Quoted 3 lines

That may have been true historically.

Current 32-bit Linux supports 64-bit file systems and reports the length of files beyond 2 GiB without error.

Yes, but 32-bit semihosting targets only support 32-bit file lengths. So I believe we should behave the same way as if the host had a 32-bit off_t.

And as I've mentioned elsewhere, I think that virtio is a much better way to transfer large files.

--Sean

#11 lore raw

Re: [PATCH 3/3] semihosting: Check for overflow in FLEN on 32-bit systems

In reply to Sean Anderson
Quoted 34 lines · Sean Anderson
Quoted 32 lines · Heinrich Schuchardt
Quoted 16 lines · Sean Anderson

When semihosting 32-bit systems, the return value of FLEN will be stored in a 32-bit integer. To prevent wraparound, return -1 and set EOVERFLOW. This matches the behavior of stat(2). Static files don't need to be checked, since are always small.

---

  semihosting/arm-compat-semi.c | 17 ++++++++++++++---
  1 file changed, 14 insertions(+), 3 deletions(-)
semihosting/arm-compat-semi.c +0 -0
index c5a07cb947..57453ca6be 100644

semihosting/arm-compat-semi.c static uint64_t common_semi_flen_buf(CPUState *cs) · line 311

Quoted 7 lines
semihosting/arm-compat-semi.c +4 -0
@@ -305,8 +305,19 @@ static uint64_t common_semi_flen_buf(CPUState *cs)
      return sp - 64;
  }
  +static void common_semi_flen_cb(CPUState *cs, uint64_t ret, int err)
+{
+    CPUArchState *env = cpu_env(cs);
+
+    if (!err && !is_64bit_semihosting(env) && ret > INT32_MAX) {

The issue with the current implementation is that files with file sizes over 4 GiB will be reported as file size < 4 -GiB on 32bit systems. Thanks for addressing this.

But unfortunately with your change you are additionally dropping support for file sizes 2 GiB to 4 GiB on 32bit devices. This should be avoided.

The semihosting specification specifies that the value returned in r0 should be -1 if an error occurs. So on 32 bit systems 0xffffffff should be returned.

Quoted 7 lines
Quoted 5 lines

As file sizes cannot be negative there is not reason to assume that the value in r0 has to be interpreted by semihosting clients as signed.

Please, change your commit to check against 0xffffffff.

It might make sense to contact ARM to make their specification clearer.

stat(2) will return -1/EOVERFLOW on 32-bit hosts for files over 2 GiB. I believe we should be consistent.

I can see both sides of this one -- the semihosting spec is pretty old and was designed (to the extent it was designed) back in an era when 2GB of RAM or a 2GB file was pretty implausible sounding. (And today there's not much appetite for making updates to it for AArch32, because 32-bit is the past, not the future, and in any case you have to deal with all the existing implementations of the spec so changes are super painful to try to promulgate.)

Our QEMU implementation of SYS_ISERROR() says "anything that's a negative 32-bit integer is an error number" for 32-bit hosts, which I suppose you might count on the side of "check against INT32_MAX".

I think overall that if we think that anybody is or might be using semihosting with files in that 2..4GB size, we should err on the side of preserving that functionality. Otherwise somebody will report it as a bug and we'll want to fix it as a regression. And it doesn't seem impossible that somebody out there is doing so.

If we report 2..4GB file sizes as if the file size value was an unsigned integer, then clients that expect that will work, and clients that treat any negative 32-bit value as an error will also work in the sense that they'll handle it as an error in the same way they would have done for -1. So that is the safest approach from a back-compat point of view, and I think that's what I lean towards doing.

thanks
-- PMM

#12 lore raw

Re: [PATCH 3/3] semihosting: Check for overflow in FLEN on 32-bit systems

Quoted 64 lines · Peter Maydell
Quoted 34 lines · Sean Anderson
Quoted 32 lines · Heinrich Schuchardt
Quoted 16 lines · Sean Anderson

When semihosting 32-bit systems, the return value of FLEN will be stored in a 32-bit integer. To prevent wraparound, return -1 and set EOVERFLOW. This matches the behavior of stat(2). Static files don't need to be checked, since are always small.

---

  semihosting/arm-compat-semi.c | 17 ++++++++++++++---
  1 file changed, 14 insertions(+), 3 deletions(-)
semihosting/arm-compat-semi.c +0 -0
index c5a07cb947..57453ca6be 100644

semihosting/arm-compat-semi.c static uint64_t common_semi_flen_buf(CPUState *cs) · line 311

Quoted 7 lines
semihosting/arm-compat-semi.c +4 -0
@@ -305,8 +305,19 @@ static uint64_t common_semi_flen_buf(CPUState *cs)
      return sp - 64;
  }
  +static void common_semi_flen_cb(CPUState *cs, uint64_t ret, int err)
+{
+    CPUArchState *env = cpu_env(cs);
+
+    if (!err && !is_64bit_semihosting(env) && ret > INT32_MAX) {

The issue with the current implementation is that files with file sizes over 4 GiB will be reported as file size < 4 -GiB on 32bit systems. Thanks for addressing this.

But unfortunately with your change you are additionally dropping support for file sizes 2 GiB to 4 GiB on 32bit devices. This should be avoided.

The semihosting specification specifies that the value returned in r0 should be -1 if an error occurs. So on 32 bit systems 0xffffffff should be returned.

Quoted 7 lines
Quoted 5 lines

As file sizes cannot be negative there is not reason to assume that the value in r0 has to be interpreted by semihosting clients as signed.

Please, change your commit to check against 0xffffffff.

It might make sense to contact ARM to make their specification clearer.

stat(2) will return -1/EOVERFLOW on 32-bit hosts for files over 2 GiB. I believe we should be consistent.

I can see both sides of this one -- the semihosting spec is pretty old and was designed (to the extent it was designed) back in an era when 2GB of RAM or a 2GB file was pretty implausible sounding. (And today there's not much appetite for making updates to it for AArch32, because 32-bit is the past, not the future, and in any case you have to deal with all the existing implementations of the spec so changes are super painful to try to promulgate.)

Our QEMU implementation of SYS_ISERROR() says "anything that's a negative 32-bit integer is an error number" for 32-bit hosts, which I suppose you might count on the side of "check against INT32_MAX".

I think overall that if we think that anybody is or might be using semihosting with files in that 2..4GB size, we should err on the side of preserving that functionality. Otherwise somebody will report it as a bug and we'll want to fix it as a regression. And it doesn't seem impossible that somebody out there is doing so.

Quoted 7 lines

If we report 2..4GB file sizes as if the file size value was an unsigned integer, then clients that expect that will work, and clients that treat any negative 32-bit value as an error will also work in the sense that they'll handle it as an error in the same way they would have done for -1. So that is the safest approach from a back-compat point of view, and I think that's what I lean towards doing.

Actually, some existing targets don't handle "negative" file sizes properly. In particular, newlib generally treats the result as an int or an off_t, which is a long (except on cygwin). Both of those types are 32 bits or smaller on (I)LP32. So if you return a 3 GiB size it will be treated as -1 GiB. This will break lseek with SEEK_END, since newlib uses the signed result of flen to recompute a new absolute offset.

IMO the spec is unclear, and this is reflected in the varying semantics of differing implementations. I think returning any negative number other than -1 is just inviting bugs.

--Sean

#13 lore raw
In reply to Sean Anderson
Quoted 2 lines · Sean Anderson

While discussing [1], it came to my attention that QEMU does not properly truncate/error SYS_FLEN on 32-bit systems.

TIL that semihostingfs was a thing!

Quoted 4 lines

Fix this, and some
other bugs with GDB File I/O that I found while working on this series. That said, GDB File I/O has been substantially broken for two years now, so it makes me wonder if anyone actually uses it!

I suspect this is at the upper end of things to use semihosting for as its real purpose is to help bootstrap things on the barest of metal until you have enough bits going to selfhost. In QEMU land it is a convenient way to do host calls for test cases.

We don't have much actual testing of semihosting in the tree although I do run Peter's semihosting tests from time to time:

https://git.linaro.org/people/peter.maydell/semihosting-tests.git/

the tests do include flen() but obviously don't cover the extreme filesize cases or overflow.

Quoted 2 lines

It would certainly
simplify the implementation if we didn't have to support it.

While semihosting does have the concept of optional extensions SYS_FLEN is not one of them.

Quoted 12 lines

[1] https://lore.kernel.org/u-boot/20251017195322.GF6688@bill-the-cat/T/#m493c42570d3103b8c606c5f50faeb78d27719de6

Sean Anderson (3):
gdbstub: Fix %s formatting
semihosting: Fix GDB File-I/O FLEN
semihosting: Check for overflow in FLEN on 32-bit systems

 gdbstub/syscalls.c            |  2 +-
 semihosting/arm-compat-semi.c | 22 +++++++++++++++-------
 2 files changed, 16 insertions(+), 8 deletions(-)
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#14 lore raw
Quoted 22 lines · Alex Bennée
Quoted 2 lines · Sean Anderson

While discussing [1], it came to my attention that QEMU does not properly truncate/error SYS_FLEN on 32-bit systems.

TIL that semihostingfs was a thing!

Quoted 4 lines

Fix this, and some
other bugs with GDB File I/O that I found while working on this series. That said, GDB File I/O has been substantially broken for two years now, so it makes me wonder if anyone actually uses it!

I suspect this is at the upper end of things to use semihosting for as its real purpose is to help bootstrap things on the barest of metal until you have enough bits going to selfhost. In QEMU land it is a convenient way to do host calls for test cases.

We don't have much actual testing of semihosting in the tree although I do run Peter's semihosting tests from time to time:

https://git.linaro.org/people/peter.maydell/semihosting-tests.git/

Quoted 8 lines

the tests do include flen() but obviously don't cover the extreme filesize cases or overflow.

Quoted 2 lines

It would certainly
simplify the implementation if we didn't have to support it.

While semihosting does have the concept of optional extensions SYS_FLEN is not one of them.

The above comments refer to GDB File I/O, which currently cannot open files due to the problem fixed in patch 1/3. FLEN is not really broken for most use-cases (as long as the user doesn't access large files).

--Sean

Applied
#15 lore raw
In reply to Sean Anderson
Quoted 9 lines · Sean Anderson

While discussing [1], it came to my attention that QEMU does not properly truncate/error SYS_FLEN on 32-bit systems. Fix this, and some other bugs with GDB File I/O that I found while working on this series. That said, GDB File I/O has been substantially broken for two years now, so it makes me wonder if anyone actually uses it! It would certainly simplify the implementation if we didn't have to support it.

[1] https://lore.kernel.org/u-boot/20251017195322.GF6688@bill-the-cat/T/#m493c42570d3103b8c606c5f50faeb78d27719de6

Queued 1-2 to maintainer/8.2-softfreeze, thanks.

As there is some discussion on patch 3 I'll leave that for now. We can still merge bug fixes after softfreeze but it doesn't seem like its a critical problem.

Quoted 9 lines

Sean Anderson (3):
gdbstub: Fix %s formatting
semihosting: Fix GDB File-I/O FLEN
semihosting: Check for overflow in FLEN on 32-bit systems

 gdbstub/syscalls.c            |  2 +-
 semihosting/arm-compat-semi.c | 22 +++++++++++++++-------
 2 files changed, 16 insertions(+), 8 deletions(-)
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#16 lore raw
In reply to Sean Anderson
Quoted 7 lines · Sean Anderson

While discussing [1], it came to my attention that QEMU does not properly truncate/error SYS_FLEN on 32-bit systems. Fix this, and some other bugs with GDB File I/O that I found while working on this series. That said, GDB File I/O has been substantially broken for two years now, so it makes me wonder if anyone actually uses it! It would certainly simplify the implementation if we didn't have to support it.

Quoted 6 lines

[1] https://lore.kernel.org/u-boot/20251017195322.GF6688@bill-the-cat/T/#m493c42570d3103b8c606c5f50faeb78d27719de6

Sean Anderson (3):
gdbstub: Fix %s formatting
semihosting: Fix GDB File-I/O FLEN

Is this qemu-stable material (for 10.0 & 10.1)?
Please let me know if it isn't.

Thanks,

/mjt

#17 lore raw
Quoted 13 lines · Michael Tokarev
Quoted 11 lines · Sean Anderson

While discussing [1], it came to my attention that QEMU does not properly truncate/error SYS_FLEN on 32-bit systems. Fix this, and some other bugs with GDB File I/O that I found while working on this series. That said, GDB File I/O has been substantially broken for two years now, so it makes me wonder if anyone actually uses it! It would certainly simplify the implementation if we didn't have to support it.
[1]
https://lore.kernel.org/u-boot/20251017195322.GF6688@bill-the-cat/T/#m493c42570d3103b8c606c5f50faeb78d27719de6
Sean Anderson (3):
gdbstub: Fix %s formatting
semihosting: Fix GDB File-I/O FLEN

Quoted 1 line

Is this qemu-stable material (for 10.0 & 10.1)?

Yeah they are simple fixes and should backport easily.

Quoted 5 lines

Please let me know if it isn't.

Thanks,

/mjt

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro