All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: Noah Goldstein <goldstein.w.n@gmail.com>
Cc: qemu-devel@nongnu.org,  laurent@vivier.eu
Subject: Re: [PATCH v1] linux-user: Add option to run `execve`d programs through QEMU
Date: Tue, 29 Oct 2024 15:23:33 +0000	[thread overview]
Message-ID: <87iktb6ih6.fsf@draig.linaro.org> (raw)
In-Reply-To: <20240830223601.2796327-1-goldstein.w.n@gmail.com> (Noah Goldstein's message of "Fri, 30 Aug 2024 15:36:01 -0700")

Noah Goldstein <goldstein.w.n@gmail.com> writes:

> The new option '-qemu-children' makes it so that on `execve` the child
> process will be launch by the same `qemu` executable that is currently
> running along with its current commandline arguments.
>
> The motivation for the change is to make it so that plugins running
> through `qemu` can continue to run on children.  Why not just
> `binfmt`?: Plugins can be desirable regardless of system/architecture
> emulation, and can sometimes be useful for elf files that can run
> natively. Enabling `binfmt` for all natively runnable elf files may
> not be desirable.

Broadly I don't see anything wrong with the patch. However my principle
concern is how test it. Currently nothing in check-tcg ensures this
mechanism works and stresses the argument copying.

>
> Signed-off-by: Noah Goldstein <goldstein.w.n@gmail.com>
> ---
>  linux-user/main.c           | 22 ++++++++++++++++++++++
>  linux-user/syscall.c        | 20 +++++++++++++++-----
>  linux-user/user-internals.h |  4 ++++
>  3 files changed, 41 insertions(+), 5 deletions(-)
>
> diff --git a/linux-user/main.c b/linux-user/main.c
> index 8143a0d4b0..dfb303a1f2 100644
> --- a/linux-user/main.c
> +++ b/linux-user/main.c
> @@ -81,6 +81,10 @@ unsigned long mmap_min_addr;
>  uintptr_t guest_base;
>  bool have_guest_base;
>  
> +bool qemu_dup_for_children;
> +int qemu_argc;
> +char ** qemu_argv;
> +
>  /*
>   * Used to implement backwards-compatibility for the `-strace`, and
>   * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
> @@ -451,6 +455,11 @@ static void handle_arg_jitdump(const char *arg)
>      perf_enable_jitdump();
>  }
>  
> +static void handle_arg_qemu_children(const char *arg)
> +{
> +    qemu_dup_for_children = true;
> +}
> +
>  static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
>  
>  #ifdef CONFIG_PLUGIN
> @@ -526,6 +535,10 @@ static const struct qemu_argument arg_table[] = {
>       "",           "Generate a /tmp/perf-${pid}.map file for perf"},
>      {"jitdump",    "QEMU_JITDUMP",     false, handle_arg_jitdump,
>       "",           "Generate a jit-${pid}.dump file for perf"},
> +    {"qemu-children",
> +                   "QEMU_CHILDREN",    false, handle_arg_qemu_children,
> +     "",           "Run child processes (created with execve) with qemu "
> +                   "(as instantiated for the parent)"},
>      {NULL, NULL, false, NULL, NULL, NULL}
>  };
>  
> @@ -729,6 +742,15 @@ int main(int argc, char **argv, char **envp)
>  
>      optind = parse_args(argc, argv);
>  
> +    if (qemu_dup_for_children) {
> +        int i;
> +        qemu_argc = optind;
> +        qemu_argv = g_new0(char *, qemu_argc);
> +        for (i = 0; i < optind; ++i) {
> +            qemu_argv[i] = strdup(argv[i]);
> +        }
> +    }
> +
>      qemu_set_log_filename_flags(last_log_filename,
>                                  last_log_mask | (enable_strace * LOG_STRACE),
>                                  &error_fatal);
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 9d5415674d..732ef89054 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -8459,13 +8459,14 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
>                      abi_long pathname, abi_long guest_argp,
>                      abi_long guest_envp, int flags, bool is_execveat)
>  {
> -    int ret;
> +    int ret, argp_offset;
>      char **argp, **envp;
>      int argc, envc;
>      abi_ulong gp;
>      abi_ulong addr;
>      char **q;
>      void *p;
> +    bool through_qemu = !is_execveat && qemu_dup_for_children;
>  
>      argc = 0;
>  
> @@ -8489,10 +8490,11 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
>          envc++;
>      }
>  
> -    argp = g_new0(char *, argc + 1);
> +    argp_offset = through_qemu ? qemu_argc : 0;
> +    argp = g_new0(char *, argc + argp_offset + 1);
>      envp = g_new0(char *, envc + 1);
>  
> -    for (gp = guest_argp, q = argp; gp; gp += sizeof(abi_ulong), q++) {
> +    for (gp = guest_argp, q = argp + argp_offset; gp; gp += sizeof(abi_ulong), q++) {
>          if (get_user_ual(addr, gp)) {
>              goto execve_efault;
>          }
> @@ -8537,9 +8539,17 @@ static int do_execv(CPUArchState *cpu_env, int dirfd,
>      }
>  
>      const char *exe = p;
> -    if (is_proc_myself(p, "exe")) {
> +    if (through_qemu) {
> +        int i;
> +        for (i = 0; i < argp_offset; ++i) {
> +            argp[i] = qemu_argv[i];
> +        }
> +        exe = qemu_argv[0];
> +    }
> +    else if (is_proc_myself(p, "exe")) {
>          exe = exec_path;
>      }
> +
>      ret = is_execveat
>          ? safe_execveat(dirfd, exe, argp, envp, flags)
>          : safe_execve(exe, argp, envp);
> @@ -8553,7 +8563,7 @@ execve_efault:
>      ret = -TARGET_EFAULT;
>  
>  execve_end:
> -    for (gp = guest_argp, q = argp; *q; gp += sizeof(abi_ulong), q++) {
> +    for (gp = guest_argp, q = argp + argp_offset; *q; gp += sizeof(abi_ulong), q++) {
>          if (get_user_ual(addr, gp) || !addr) {
>              break;
>          }
> diff --git a/linux-user/user-internals.h b/linux-user/user-internals.h
> index 5c7f173ceb..0719e65ff4 100644
> --- a/linux-user/user-internals.h
> +++ b/linux-user/user-internals.h
> @@ -30,6 +30,10 @@ void stop_all_tasks(void);
>  extern const char *qemu_uname_release;
>  extern unsigned long mmap_min_addr;
>  
> +extern bool qemu_dup_for_children;
> +extern int qemu_argc;
> +extern char ** qemu_argv;
> +
>  typedef struct IOCTLEntry IOCTLEntry;
>  
>  typedef abi_long do_ioctl_fn(const IOCTLEntry *ie, uint8_t *buf_temp,

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


  parent reply	other threads:[~2024-10-29 15:23 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-30 22:36 [PATCH v1] linux-user: Add option to run `execve`d programs through QEMU Noah Goldstein
2024-08-30 22:37 ` Noah Goldstein
2024-09-10 22:06   ` Noah Goldstein
2024-09-24 14:43     ` Noah Goldstein
2024-10-02  8:08 ` Ilya Leoshkevich
2024-10-02 14:05   ` Noah Goldstein
2024-10-02 16:39     ` Ilya Leoshkevich
2024-10-02 16:42       ` Noah Goldstein
2024-10-11 18:14         ` Noah Goldstein
2024-10-22 22:06           ` Noah Goldstein
2024-10-29 14:51             ` Noah Goldstein
2024-10-02 14:08   ` Laurent Vivier
2024-10-02 14:25     ` Ilya Leoshkevich
2024-10-02 14:44       ` Noah Goldstein
2024-10-02 14:53         ` Ilya Leoshkevich
2024-10-02 15:10           ` Noah Goldstein
2024-10-02 16:14             ` Ilya Leoshkevich
2024-10-02 16:24               ` Noah Goldstein
2024-10-02 16:35                 ` Ilya Leoshkevich
2024-10-02 16:36                   ` Noah Goldstein
2024-10-02 15:59           ` Laurent Vivier
2024-10-02 14:50 ` [PATCH v2] " Noah Goldstein
2024-10-29 15:23 ` Alex Bennée [this message]
2024-10-29 15:27   ` [PATCH v1] " Noah Goldstein
2024-10-30 14:10 ` Noah Goldstein
2024-10-30 14:11   ` Noah Goldstein
2024-11-05 11:37   ` Richard Henderson
2024-11-05 23:48     ` Noah Goldstein
2024-11-05 23:54       ` Noah Goldstein
2024-11-06  9:38         ` Richard Henderson
2024-11-06 17:03           ` Noah Goldstein
2024-11-06 17:25             ` Richard Henderson
2024-11-06 17:53               ` Noah Goldstein
2024-11-06 18:13                 ` Noah Goldstein
2024-11-06 21:10                   ` Richard Henderson
2024-11-06 21:30                     ` Noah Goldstein
2024-11-06 23:49                       ` Noah Goldstein
2024-11-07  9:42                         ` Richard Henderson
2024-11-07  9:29                       ` Richard Henderson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87iktb6ih6.fsf@draig.linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=goldstein.w.n@gmail.com \
    --cc=laurent@vivier.eu \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.