From: Richard Henderson <richard.henderson@linaro.org>
To: qemu-devel@nongnu.org
Cc: laurent@vivier.eu
Subject: [Qemu-devel] [PATCH 11/33] linux-user: Split out execve
Date: Fri, 1 Jun 2018 00:30:28 -0700 [thread overview]
Message-ID: <20180601073050.8054-12-richard.henderson@linaro.org> (raw)
In-Reply-To: <20180601073050.8054-1-richard.henderson@linaro.org>
At the same time, fix the repeated re-reading of the argv and env
arrays from guest memory. Instead read into a unified array once.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
linux-user/syscall.c | 203 ++++++++++++++++++++++---------------------
1 file changed, 106 insertions(+), 97 deletions(-)
diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index b0d268dab7..a9b59a8658 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -7998,6 +7998,111 @@ IMPL(close)
return get_errno(close(arg1));
}
+IMPL(execve)
+{
+ abi_ulong *guest_ptrs;
+ char **host_ptrs;
+ int argc, envc, alloc, i;
+ abi_ulong gp;
+ abi_ulong guest_argp = arg2;
+ abi_ulong guest_envp = arg3;
+ char *filename;
+ abi_long ret;
+
+ /* Initial estimate of number of guest pointers required. */
+ alloc = 32;
+ guest_ptrs = g_new(abi_ulong, alloc);
+
+ /* Iterate through argp and envp, counting entries, and
+ * reading guest addresses from the arrays.
+ */
+ for (gp = guest_argp, argc = 0; gp; gp += sizeof(abi_ulong)) {
+ abi_ulong addr;
+ if (get_user_ual(addr, gp)) {
+ return -TARGET_EFAULT;
+ }
+ if (!addr) {
+ break;
+ }
+ if (argc >= alloc) {
+ alloc *= 2;
+ guest_ptrs = g_renew(abi_ulong, guest_ptrs, alloc);
+ }
+ guest_ptrs[argc++] = addr;
+ }
+ for (gp = guest_envp, envc = 0; gp; gp += sizeof(abi_ulong)) {
+ abi_ulong addr;
+ if (get_user_ual(addr, gp)) {
+ return -TARGET_EFAULT;
+ }
+ if (!addr) {
+ break;
+ }
+ if (argc + envc >= alloc) {
+ alloc *= 2;
+ guest_ptrs = g_renew(abi_ulong, guest_ptrs, alloc);
+ }
+ guest_ptrs[argc + envc++] = addr;
+ }
+
+ /* Exact number of host pointers required. */
+ host_ptrs = g_new0(char *, argc + envc + 2);
+
+ /* Iterate through the argp and envp that we already read
+ * and convert the guest pointers to host pointers.
+ */
+ ret = -TARGET_EFAULT;
+ for (i = 0; i < argc; ++i) {
+ char *p = lock_user_string(guest_ptrs[i]);
+ if (!p) {
+ goto fini;
+ }
+ host_ptrs[i] = p;
+ }
+ for (i = 0; i < envc; ++i) {
+ char *p = lock_user_string(guest_ptrs[argc + i]);
+ if (!p) {
+ goto fini;
+ }
+ host_ptrs[argc + 1 + i] = p;
+ }
+
+ /* Read the executable filename. */
+ filename = lock_user_string(arg1);
+ if (!filename) {
+ goto fini;
+ }
+
+ /* Although execve() is not an interruptible syscall it is
+ * a special case where we must use the safe_syscall wrapper:
+ * if we allow a signal to happen before we make the host
+ * syscall then we will 'lose' it, because at the point of
+ * execve the process leaves QEMU's control. So we use the
+ * safe syscall wrapper to ensure that we either take the
+ * signal as a guest signal, or else it does not happen
+ * before the execve completes and makes it the other
+ * program's problem.
+ */
+ ret = get_errno(safe_execve(filename, host_ptrs, host_ptrs + argc + 1));
+ unlock_user(filename, arg1, 0);
+
+ fini:
+ /* Deallocate everything we allocated above. */
+ for (i = 0; i < argc; ++i) {
+ if (host_ptrs[i]) {
+ unlock_user(host_ptrs[i], guest_ptrs[i], 0);
+ }
+ }
+ for (i = 0; i < envc; ++i) {
+ if (host_ptrs[argc + 1 + i]) {
+ unlock_user(host_ptrs[argc + 1 + i], guest_ptrs[argc + i], 0);
+ }
+ }
+ g_free(host_ptrs);
+ g_free(guest_ptrs);
+ return ret;
+}
+
IMPL(exit)
{
CPUState *cpu = ENV_GET_CPU(cpu_env);
@@ -8237,103 +8342,6 @@ IMPL(everything_else)
unlock_user(p, arg2, 0);
return ret;
#endif
- case TARGET_NR_execve:
- {
- char **argp, **envp;
- int argc, envc;
- abi_ulong gp;
- abi_ulong guest_argp;
- abi_ulong guest_envp;
- abi_ulong addr;
- char **q;
- int total_size = 0;
-
- argc = 0;
- guest_argp = arg2;
- for (gp = guest_argp; gp; gp += sizeof(abi_ulong)) {
- if (get_user_ual(addr, gp))
- return -TARGET_EFAULT;
- if (!addr)
- break;
- argc++;
- }
- envc = 0;
- guest_envp = arg3;
- for (gp = guest_envp; gp; gp += sizeof(abi_ulong)) {
- if (get_user_ual(addr, gp))
- return -TARGET_EFAULT;
- if (!addr)
- break;
- envc++;
- }
-
- argp = g_new0(char *, argc + 1);
- envp = g_new0(char *, envc + 1);
-
- for (gp = guest_argp, q = argp; gp;
- gp += sizeof(abi_ulong), q++) {
- if (get_user_ual(addr, gp))
- goto execve_efault;
- if (!addr)
- break;
- if (!(*q = lock_user_string(addr)))
- goto execve_efault;
- total_size += strlen(*q) + 1;
- }
- *q = NULL;
-
- for (gp = guest_envp, q = envp; gp;
- gp += sizeof(abi_ulong), q++) {
- if (get_user_ual(addr, gp))
- goto execve_efault;
- if (!addr)
- break;
- if (!(*q = lock_user_string(addr)))
- goto execve_efault;
- total_size += strlen(*q) + 1;
- }
- *q = NULL;
-
- if (!(p = lock_user_string(arg1)))
- goto execve_efault;
- /* Although execve() is not an interruptible syscall it is
- * a special case where we must use the safe_syscall wrapper:
- * if we allow a signal to happen before we make the host
- * syscall then we will 'lose' it, because at the point of
- * execve the process leaves QEMU's control. So we use the
- * safe syscall wrapper to ensure that we either take the
- * signal as a guest signal, or else it does not happen
- * before the execve completes and makes it the other
- * program's problem.
- */
- ret = get_errno(safe_execve(p, argp, envp));
- unlock_user(p, arg1, 0);
-
- goto execve_end;
-
- execve_efault:
- ret = -TARGET_EFAULT;
-
- execve_end:
- for (gp = guest_argp, q = argp; *q;
- gp += sizeof(abi_ulong), q++) {
- if (get_user_ual(addr, gp)
- || !addr)
- break;
- unlock_user(*q, addr, 0);
- }
- for (gp = guest_envp, q = envp; *q;
- gp += sizeof(abi_ulong), q++) {
- if (get_user_ual(addr, gp)
- || !addr)
- break;
- unlock_user(*q, addr, 0);
- }
-
- g_free(argp);
- g_free(envp);
- }
- return ret;
case TARGET_NR_chdir:
if (!(p = lock_user_string(arg1)))
return -TARGET_EFAULT;
@@ -12916,6 +12924,7 @@ IMPL(everything_else)
static impl_fn * const syscall_table[] = {
[TARGET_NR_brk] = impl_brk,
[TARGET_NR_close] = impl_close,
+ [TARGET_NR_execve] = impl_execve,
[TARGET_NR_exit] = impl_exit,
[TARGET_NR_read] = impl_read,
[TARGET_NR_write] = impl_write,
--
2.17.0
next prev parent reply other threads:[~2018-06-01 7:31 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-01 7:30 [Qemu-devel] [PATCH 00/33] linux-user: Begin splitting do_syscall Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 01/33] linux-user: Split out do_syscall1 Richard Henderson
2018-06-01 7:36 ` Laurent Vivier
2018-06-01 14:00 ` Eric Blake
2018-06-01 14:52 ` Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 02/33] linux-user: Relax single exit from "break" Richard Henderson
2018-06-04 19:29 ` Laurent Vivier
2018-06-01 7:30 ` [Qemu-devel] [PATCH 03/33] linux-user: Propagate goto ebadf to return Richard Henderson
2018-06-04 19:33 ` Laurent Vivier
2018-06-01 7:30 ` [Qemu-devel] [PATCH 04/33] linux-user: Propagate goto efault " Richard Henderson
2018-06-04 19:35 ` Laurent Vivier
2018-06-01 7:30 ` [Qemu-devel] [PATCH 05/33] linux-user: Propagate goto unimplemented_nowarn " Richard Henderson
2018-06-04 19:36 ` Laurent Vivier
2018-06-01 7:30 ` [Qemu-devel] [PATCH 06/33] linux-user: Split out goto unimplemented to do_unimplemented Richard Henderson
2018-06-04 19:38 ` Laurent Vivier
2018-06-01 7:30 ` [Qemu-devel] [PATCH 07/33] linux-user: Propagate goto fail to return Richard Henderson
2018-06-04 19:48 ` Laurent Vivier
2018-06-01 7:30 ` [Qemu-devel] [PATCH 08/33] linux-user: Make syscall number unsigned Richard Henderson
2018-06-04 19:50 ` Laurent Vivier
2018-06-01 7:30 ` [Qemu-devel] [PATCH 09/33] linux-user: Set up infrastructure for table-izing syscalls Richard Henderson
2018-06-04 19:55 ` Laurent Vivier
2018-06-01 7:30 ` [Qemu-devel] [PATCH 10/33] linux-user: Split out brk, close, exit, read, write Richard Henderson
2018-06-04 20:17 ` Laurent Vivier
2018-06-04 21:01 ` Richard Henderson
2018-06-01 7:30 ` Richard Henderson [this message]
2018-06-01 7:30 ` [Qemu-devel] [PATCH 12/33] linux-user: Split out open, openat Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 13/33] linux-user: Split out name_to_handle_at Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 14/33] linux-user: Split out open_to_handle_at Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 15/33] linux-user: Split out creat, fork, waitid, waitpid Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 16/33] linux-user: Split out link, linkat Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 17/33] linux-user: Split out unlink, unlinkat Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 18/33] linux-user: Split out chdir, mknod, mknodat, time, chmod Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 19/33] linux-user: Remove all unimplemented entries Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 20/33] linux-user: Split out getpid, getxpid, lseek Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 21/33] linux-user: Split out mount, umount Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 22/33] linux-user: Split out alarm, pause, stime, utime, utimes Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 23/33] linux-user: Split out access, faccessat, futimesat, kill, nice, sync, syncfs Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 24/33] linux-user: Split out rename, renameat, renameat2 Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 25/33] linux-user: Split out dup, mkdir, mkdirat, rmdir Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 26/33] linux-user: Split out acct, pipe, pipe2, times, umount2 Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 27/33] linux-user: Split out ioctl Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 28/33] linux-user: Split out chroot, dup2, dup3, fcntl, setpgid, umask Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 29/33] linux-user: Split out getpgrp, getppid, setsid Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 30/33] linux-user: Split out rt_sigaction, sigaction Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 31/33] linux-user: Split out rt_sigprocmask, sgetmask, sigprocmask, ssetmask Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 32/33] linux-user: Split out rt_sigpending, rt_sigsuspend, sigpending, sigsuspend Richard Henderson
2018-06-01 7:30 ` [Qemu-devel] [PATCH 33/33] linux-user: Split out rt_sigqueueinfo, rt_sigtimedwait, rt_tgsigqueueinfo Richard Henderson
2018-06-01 7:33 ` [Qemu-devel] [PATCH 00/33] linux-user: Begin splitting do_syscall Richard Henderson
2018-06-01 8:05 ` no-reply
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=20180601073050.8054-12-richard.henderson@linaro.org \
--to=richard.henderson@linaro.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).