[PATCH] linux-user: Clean up arg_start/arg_end confusion

Richard Henderson posted 1 patch 2 years, 1 month ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20220315224519.498463-1-richard.henderson@linaro.org
Test checkpatch passed
Maintainers: Laurent Vivier <laurent@vivier.eu>, "Alex Bennée" <alex.bennee@linaro.org>
There is a newer version of this series
linux-user/qemu.h             | 12 ++++++++----
linux-user/elfload.c          | 10 ++++++----
linux-user/linuxload.c        | 18 ++++++++++--------
linux-user/main.c             |  5 ++---
semihosting/arm-compat-semi.c |  4 ++--
5 files changed, 28 insertions(+), 21 deletions(-)
[PATCH] linux-user: Clean up arg_start/arg_end confusion
Posted by Richard Henderson 2 years, 1 month ago
We had two sets of variables: arg_start/arg_end, and
arg_strings/env_strings.  In linuxload.c, we set the
first pair to the bounds of the argv strings, but in
elfload.c, we set the first pair to the bounds of the
argv pointers and the second pair to the bounds of
the argv strings.

Remove arg_start/arg_end, replacing them with the standard
argc/argv/envc/envp values.  Retain arg_strings/env_strings.
Update linuxload.c, elfload.c, and arm-compat-semi.c to match.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/714
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 linux-user/qemu.h             | 12 ++++++++----
 linux-user/elfload.c          | 10 ++++++----
 linux-user/linuxload.c        | 18 ++++++++++--------
 linux-user/main.c             |  5 ++---
 semihosting/arm-compat-semi.c |  4 ++--
 5 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/linux-user/qemu.h b/linux-user/qemu.h
index 98dfbf2096..3ac39793e1 100644
--- a/linux-user/qemu.h
+++ b/linux-user/qemu.h
@@ -40,15 +40,19 @@ struct image_info {
         abi_ulong       data_offset;
         abi_ulong       saved_auxv;
         abi_ulong       auxv_len;
-        abi_ulong       arg_start;
-        abi_ulong       arg_end;
-        abi_ulong       arg_strings;
-        abi_ulong       env_strings;
+        abi_ulong       argc;
+        abi_ulong       argv;
+        abi_ulong       envc;
+        abi_ulong       envp;
         abi_ulong       file_string;
         uint32_t        elf_flags;
         int             personality;
         abi_ulong       alignment;
 
+        /* Generic semihosting knows about these pointers. */
+        abi_ulong       arg_strings;   /* strings for argv */
+        abi_ulong       env_strings;   /* strings for envp; ends arg_strings */
+
         /* The fields below are used in FDPIC mode.  */
         abi_ulong       loadmap_addr;
         uint16_t        nsegs;
diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index 9628a38361..828ac2d8db 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -1517,8 +1517,8 @@ static inline void init_thread(struct target_pt_regs *regs,
     regs->iaoq[0] = infop->entry;
     regs->iaoq[1] = infop->entry + 4;
     regs->gr[23] = 0;
-    regs->gr[24] = infop->arg_start;
-    regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
+    regs->gr[24] = infop->argv;
+    regs->gr[25] = infop->argc;
     /* The top-of-stack contains a linkage buffer.  */
     regs->gr[30] = infop->start_stack + 64;
     regs->gr[31] = infop->entry;
@@ -2121,8 +2121,10 @@ static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
     u_envp = u_argv + (argc + 1) * n;
     u_auxv = u_envp + (envc + 1) * n;
     info->saved_auxv = u_auxv;
-    info->arg_start = u_argv;
-    info->arg_end = u_argv + argc * n;
+    info->argc = argc;
+    info->envc = envc;
+    info->argv = u_argv;
+    info->envp = u_envp;
 
     /* This is correct because Linux defines
      * elf_addr_t as Elf32_Off / Elf64_Off
diff --git a/linux-user/linuxload.c b/linux-user/linuxload.c
index 2ed5fc45ed..eb010b0109 100644
--- a/linux-user/linuxload.c
+++ b/linux-user/linuxload.c
@@ -92,33 +92,35 @@ abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
     envp = sp;
     sp -= (argc + 1) * n;
     argv = sp;
+    ts->info->envp = envp;
+    ts->info->envc = envc;
+    ts->info->argv = argv;
+    ts->info->argc = argc;
+
     if (push_ptr) {
-        /* FIXME - handle put_user() failures */
         sp -= n;
         put_user_ual(envp, sp);
         sp -= n;
         put_user_ual(argv, sp);
     }
+
     sp -= n;
-    /* FIXME - handle put_user() failures */
     put_user_ual(argc, sp);
-    ts->info->arg_start = stringp;
+
+    ts->info->arg_strings = stringp;
     while (argc-- > 0) {
-        /* FIXME - handle put_user() failures */
         put_user_ual(stringp, argv);
         argv += n;
         stringp += target_strlen(stringp) + 1;
     }
-    ts->info->arg_end = stringp;
-    /* FIXME - handle put_user() failures */
     put_user_ual(0, argv);
+
+    ts->info->env_strings = stringp;
     while (envc-- > 0) {
-        /* FIXME - handle put_user() failures */
         put_user_ual(stringp, envp);
         envp += n;
         stringp += target_strlen(stringp) + 1;
     }
-    /* FIXME - handle put_user() failures */
     put_user_ual(0, envp);
 
     return sp;
diff --git a/linux-user/main.c b/linux-user/main.c
index fbc9bcfd5f..8995379aa3 100644
--- a/linux-user/main.c
+++ b/linux-user/main.c
@@ -869,9 +869,8 @@ int main(int argc, char **argv, char **envp)
         qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", info->start_stack);
         qemu_log("brk         0x" TARGET_ABI_FMT_lx "\n", info->brk);
         qemu_log("entry       0x" TARGET_ABI_FMT_lx "\n", info->entry);
-        qemu_log("argv_start  0x" TARGET_ABI_FMT_lx "\n", info->arg_start);
-        qemu_log("env_start   0x" TARGET_ABI_FMT_lx "\n",
-                 info->arg_end + (abi_ulong)sizeof(abi_ulong));
+        qemu_log("argv_start  0x" TARGET_ABI_FMT_lx "\n", info->argv);
+        qemu_log("env_start   0x" TARGET_ABI_FMT_lx "\n", info->envp);
         qemu_log("auxv_start  0x" TARGET_ABI_FMT_lx "\n", info->saved_auxv);
     }
 
diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c
index 7a51fd0737..b6ddaf863a 100644
--- a/semihosting/arm-compat-semi.c
+++ b/semihosting/arm-compat-semi.c
@@ -1106,7 +1106,7 @@ target_ulong do_common_semihosting(CPUState *cs)
 #else
             unsigned int i;
 
-            output_size = ts->info->arg_end - ts->info->arg_start;
+            output_size = ts->info->env_strings - ts->info->arg_strings;
             if (!output_size) {
                 /*
                  * We special-case the "empty command line" case (argc==0).
@@ -1146,7 +1146,7 @@ target_ulong do_common_semihosting(CPUState *cs)
                 goto out;
             }
 
-            if (copy_from_user(output_buffer, ts->info->arg_start,
+            if (copy_from_user(output_buffer, ts->info->arg_strings,
                                output_size)) {
                 errno = EFAULT;
                 status = set_swi_errno(cs, -1);
-- 
2.25.1
Re: [PATCH] linux-user: Clean up arg_start/arg_end confusion
Posted by Peter Maydell 2 years, 1 month ago
On Tue, 15 Mar 2022 at 22:47, Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> We had two sets of variables: arg_start/arg_end, and
> arg_strings/env_strings.  In linuxload.c, we set the
> first pair to the bounds of the argv strings, but in
> elfload.c, we set the first pair to the bounds of the
> argv pointers and the second pair to the bounds of
> the argv strings.
>
> Remove arg_start/arg_end, replacing them with the standard
> argc/argv/envc/envp values.  Retain arg_strings/env_strings.

"Retain arg_strings/env_strings with the meaning we were using
in elfload.c" ? (ie linuxload.c changes to that definition).

> Update linuxload.c, elfload.c, and arm-compat-semi.c to match.
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/714
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> --- a/linux-user/linuxload.c
> +++ b/linux-user/linuxload.c
> @@ -92,33 +92,35 @@ abi_ulong loader_build_argptr(int envc, int argc, abi_ulong sp,
>      envp = sp;
>      sp -= (argc + 1) * n;
>      argv = sp;
> +    ts->info->envp = envp;
> +    ts->info->envc = envc;
> +    ts->info->argv = argv;
> +    ts->info->argc = argc;
> +
>      if (push_ptr) {
> -        /* FIXME - handle put_user() failures */

Why are you deleting all these FIXME comments? That seems like an
unrelated change.

Otherwise
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

thanks
-- PMM
Re: [PATCH] linux-user: Clean up arg_start/arg_end confusion
Posted by Richard Henderson 2 years, 1 month ago
On 3/18/22 04:41, Peter Maydell wrote:
> On Tue, 15 Mar 2022 at 22:47, Richard Henderson
> <richard.henderson@linaro.org> wrote:
>>
>> We had two sets of variables: arg_start/arg_end, and
>> arg_strings/env_strings.  In linuxload.c, we set the
>> first pair to the bounds of the argv strings, but in
>> elfload.c, we set the first pair to the bounds of the
>> argv pointers and the second pair to the bounds of
>> the argv strings.
>>
>> Remove arg_start/arg_end, replacing them with the standard
>> argc/argv/envc/envp values.  Retain arg_strings/env_strings.
> 
> "Retain arg_strings/env_strings with the meaning we were using
> in elfload.c" ? (ie linuxload.c changes to that definition).

Ah, yes, quite.

>> -        /* FIXME - handle put_user() failures */
> 
> Why are you deleting all these FIXME comments? That seems like an
> unrelated change.

Indeed.  Instead of just removing these, I should convert to lock the entire buffer and 
use __put_user instead.


r~