From nobody Thu Nov 28 11:05:40 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1692959022; cv=none; d=zohomail.com; s=zohoarc; b=lq/2l94lyY0KREMHWyjJDJ1rBZxeT4svvaQzQF5Zc8kpUHJHbY/FgCCHXoPvrzFSvvNT4vLb4Mxm+t1Iy/3X3VCEDaFvj+VJF6gZRVURX6c1l//qY78/dDaM3HM3LojoI7/8cw2hnNxwiJAmdtd+la9so8WG+a5DiqzH0ysxfoM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1692959022; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=bID9qI0h3G1/dqYn59S/QrAa2isHzV5tXKIg0NXmPWU=; b=IkcL5D5/gRVLMq40gQOQoKw71GLmCBcWFCGy0lPmjFmlyooKwBuw3enTSf1Buc1raRTq72A9DDonFpwEI0y6QLCcC2aHAICq09D7QfgyBG2o6+xb3bf2rLqZVKYpZrjNtn2UMj5cWxQknPqzFzbTRaB8OJxPEO3xcuIJYkQ/rII= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1692959022572784.9793679754118; Fri, 25 Aug 2023 03:23:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZTxz-00078B-MA; Fri, 25 Aug 2023 06:22:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZTxx-00077k-FV for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:57 -0400 Received: from mail-oi1-x234.google.com ([2607:f8b0:4864:20::234]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qZTxv-0004lm-0Q for qemu-devel@nongnu.org; Fri, 25 Aug 2023 06:22:57 -0400 Received: by mail-oi1-x234.google.com with SMTP id 5614622812f47-3a741f46fadso527227b6e.0 for ; Fri, 25 Aug 2023 03:22:54 -0700 (PDT) Received: from localhost.localdomain ([223.104.40.15]) by smtp.gmail.com with ESMTPSA id c5-20020aa781c5000000b0068a077847c7sm1220954pfn.135.2023.08.25.03.22.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Aug 2023 03:22:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692958974; x=1693563774; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bID9qI0h3G1/dqYn59S/QrAa2isHzV5tXKIg0NXmPWU=; b=lKRU2MRyfvWIgfR9z8/urSCMjKiCgMbhIHW2vx6FCRO/WwQy22KHbLQlzb4Og+0Nde NcVlnN3mMvsSvwpSj6MEUo2gMiUyTcAuU7uAkCT1Jv2eGuLhc7SxTZs77TgKrXS9IHWp TL94oDh58Wehb1bh3l+K5t8ctwmwkGxknisjWTNw2t0KYSpg5NyheTHVQWOglwoa8wIV 842z9QFFugeA+cAa3kLI+DwEtNvlEvy2Y3W8gX4eOLJ4/OjODKyxrWJZaNQ9Me3uxhOC /TvDEBVAMYwTQy9yiYfczsCwWrbqkUTz3MQBEqpg20aiKRPviTbXFeBeasKalnL7ZNDO H2GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692958974; x=1693563774; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bID9qI0h3G1/dqYn59S/QrAa2isHzV5tXKIg0NXmPWU=; b=NoJ7netvQCfk1cucCtwfn6Oa8NxitX97oD47b2khhfPK42F1Qs6278AopsBbuEn9w7 jWcNLcbYZn9gurAs2ldb5Dm+GsyCef7vLKmrBRrJysIq0nYAI8rteGzgW5GrPH0QfUX4 yeKOuLoClv28RG1uhfsJI+4+oVDpwtSt0QWGvGLgCmbQOORVEuHJM/9CljeKnoKdxVQZ 68ncg5cXMelwrc1OfNynjGY6XxAUKHVgYUx5tP1lhnkJ2MPj+tP0O5y+SBATtjaTB4b+ J9iQ6gRsOs1olCEjzG/KODUAJb8EGKfCHJnEoDuKOPxzoCn9hC3oXQeL+6p64wF3hgvS Xvig== X-Gm-Message-State: AOJu0YyGN+Pi+jIp0g1LkKFqmzwGT14tnutuwtz8WfhyvKBtUlnP26pZ d8m6HRRdyJsQ6/eVHH4e16D95ZtQceVwbQ== X-Google-Smtp-Source: AGHT+IGbcoD2kAcB0u8Yczp2FTlOTwz3CuaRM4YOv5othW6rSXgOeUTtJX4f1WscX7gRN1YHJuiZ3w== X-Received: by 2002:a54:4890:0:b0:3a8:1727:5af4 with SMTP id r16-20020a544890000000b003a817275af4mr2171239oic.24.1692958973826; Fri, 25 Aug 2023 03:22:53 -0700 (PDT) From: Yeqi Fu To: alex.bennee@linaro.org Cc: richard.henderson@linaro.org, qemu-devel@nongnu.org, Yeqi Fu , Laurent Vivier Subject: [RFC v5 04/10] linux-user: Implement native-bypass option support Date: Fri, 25 Aug 2023 18:20:02 +0800 Message-Id: <20230825102009.1754699-5-fufuyqqqqqq@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> References: <20230825102009.1754699-1-fufuyqqqqqq@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::234; envelope-from=fufuyqqqqqq@gmail.com; helo=mail-oi1-x234.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HK_RANDOM_ENVFROM=0.001, HK_RANDOM_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1692959023876100001 Content-Type: text/plain; charset="utf-8" This commit implements the -native-bypass support in linux-user. The native_calls_enabled() function can be true only when the '-native-bypass' option is given. Signed-off-by: Yeqi Fu --- include/native/native.h | 9 +++++++++ linux-user/main.c | 38 ++++++++++++++++++++++++++++++++++++++ linux-user/syscall.c | 21 +++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 include/native/native.h diff --git a/include/native/native.h b/include/native/native.h new file mode 100644 index 0000000000..7d1baadfcf --- /dev/null +++ b/include/native/native.h @@ -0,0 +1,9 @@ +/* + * Check if the native bypass feature is enabled. + */ +#if defined(CONFIG_USER_ONLY) && defined(CONFIG_NATIVE_CALL) +extern char *native_lib_path; +#define native_bypass_enabled() (native_lib_path !=3D NULL) +#else +#define native_bypass_enabled() false +#endif diff --git a/linux-user/main.c b/linux-user/main.c index dba67ffa36..5cf02c071b 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -60,6 +60,11 @@ #include "semihosting/semihost.h" #endif =20 +#if defined(CONFIG_NATIVE_CALL) +#include "native/native.h" +char *native_lib_path; +#endif + #ifndef AT_FLAGS_PRESERVE_ARGV0 #define AT_FLAGS_PRESERVE_ARGV0_BIT 0 #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT) @@ -293,6 +298,17 @@ static void handle_arg_set_env(const char *arg) free(r); } =20 +#if defined(CONFIG_NATIVE_CALL) +static void handle_arg_native_bypass(const char *arg) +{ + if (access(arg, F_OK) !=3D 0) { + fprintf(stderr, "native library %s does not exist\n", arg); + exit(EXIT_FAILURE); + } + native_lib_path =3D g_strdup(arg); +} +#endif + static void handle_arg_unset_env(const char *arg) { char *r, *p, *token; @@ -522,6 +538,10 @@ static const struct qemu_argument arg_table[] =3D { "", "Generate a /tmp/perf-${pid}.map file for perf"}, {"jitdump", "QEMU_JITDUMP", false, handle_arg_jitdump, "", "Generate a jit-${pid}.dump file for perf"}, +#if defined(CONFIG_NATIVE_CALL) + {"native-bypass", "QEMU_NATIVE_BYPASS", true, handle_arg_native_bypass, + "", "native bypass for library calls"}, +#endif {NULL, NULL, false, NULL, NULL, NULL} }; =20 @@ -834,6 +854,24 @@ int main(int argc, char **argv, char **envp) } } =20 +#if defined(CONFIG_NATIVE_CALL) + /* Set the library for native bypass */ + if (native_lib_path) { + if (g_file_test(native_lib_path, G_FILE_TEST_IS_REGULAR)) { + GString *lib =3D g_string_new(native_lib_path); + lib =3D g_string_prepend(lib, "LD_PRELOAD=3D"); + if (envlist_appendenv(envlist, g_string_free(lib, false), ":")= ) { + fprintf(stderr, + "failed to append the native library to environment.\n= "); + exit(EXIT_FAILURE); + } + } else { + fprintf(stderr, "native library %s does not exist.\n", + native_lib_path); + exit(EXIT_FAILURE); + } + } +#endif target_environ =3D envlist_to_environ(envlist, NULL); envlist_free(envlist); =20 diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 08162cc966..bd4c3045ff 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -143,6 +143,7 @@ #include "fd-trans.h" #include "tcg/tcg.h" #include "cpu_loop-common.h" +#include "native/native.h" =20 #ifndef CLONE_IO #define CLONE_IO 0x80000000 /* Clone io context */ @@ -8626,6 +8627,7 @@ static int do_execveat(CPUArchState *cpu_env, int dir= fd, abi_ulong addr; char **q; void *p; + unsigned int i; =20 argc =3D 0; =20 @@ -8696,6 +8698,25 @@ static int do_execveat(CPUArchState *cpu_env, int di= rfd, goto execve_efault; } =20 + /* + * An error may occur when executing execv, stating that the + * shared library from LD_PRELOAD cannot be preloaded on a + * different arch. So, we find LD_PRELOAD and remove it from + * envp before executing the execv. + */ + if (native_bypass_enabled()) { + i =3D 0; + while (envp[i] !=3D NULL) { + if (strncmp(envp[i], "LD_PRELOAD=3D", 11) =3D=3D 0) { + for (int j =3D i; envp[j] !=3D NULL; j++) { + envp[j] =3D envp[j + 1]; + } + } else { + i++; + } + } + } + if (is_proc_myself(p, "exe")) { ret =3D get_errno(safe_execveat(dirfd, exec_path, argp, envp, flag= s)); } else { --=20 2.34.1