From patchwork Tue Jul 25 11:52:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 1812490 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=deller@gmx.de header.a=rsa-sha256 header.s=s31663417 header.b=anmAjmIr; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R9FmH3mGNz1yXx for ; Tue, 25 Jul 2023 21:53:35 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qOGac-0006w6-W8; Tue, 25 Jul 2023 07:52:31 -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 1qOGab-0006vI-Cp for qemu-devel@nongnu.org; Tue, 25 Jul 2023 07:52:29 -0400 Received: from mout.gmx.net ([212.227.17.22]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOGaZ-0002Kq-If for qemu-devel@nongnu.org; Tue, 25 Jul 2023 07:52:29 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690285941; x=1690890741; i=deller@gmx.de; bh=fVQFPRK5enCK1ItFwvcU3gz1ISxvjWP9wzrocVMsB/0=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=anmAjmIrfR9TvcxJT5K1u7wnv+p8lDsRtowT2Q1fDPI0EVJU3UCi0IINUAu45s1CG2jPAam xF49HPgIndfPJN9e4eLZFRMTpGfMPH+nQo3Dk428CdobVaiJCtXVSc9yEu7g3HmPZYfQz2xHw KD/s2qyVSbViExSGmJVJphyIdnjCzp4qFsZ5YV9nT4nejQ77TciEsh1HPvSPXqQHqVdySKdg+ cHfm5KmAE3GQVxb6Wny5lw4eW+3RI51NVxmd9x6YNO0CP5WwdOfDpsk18/ZrE6gnHyIJUnMuT GQ9Kbw6UPDSrN9C16z96apXTKRGEd4mBZYDll8BJ1Idg71/xaJDQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.145.136]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1N7QxB-1ppmDg0VhN-017oVS; Tue, 25 Jul 2023 13:52:21 +0200 From: Helge Deller To: Richard Henderson , Michael Tokarev , qemu-devel@nongnu.org, Andreas Schwab , Laurent Vivier Cc: Paolo Bonzini , Helge Deller Subject: [PATCH v3 1/3] linux-user: Show heap address in /proc/pid/maps Date: Tue, 25 Jul 2023 13:52:16 +0200 Message-ID: <20230725115218.52738-2-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230725115218.52738-1-deller@gmx.de> References: <20230725115218.52738-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:4FDIsh9x/zU/eCt5ZyQR6IERFAzFtlB1TOYA4hJ4NLCp+Alld8S E7Tpfv6mU7SZ7RoJUX5WBehq8ctj6FeICCUst424P4jveaBg8HwmeSTZXAUGwBBHsTMdCxI 2Z7IL7i+lXciTWNG9bjDJ67wMyYYFIGmMiVbShTaFgWqcqytpmVvdfzLMBx4ZtbxzpzREWk 0H6cWATcZTCGOUGd46aZQ== UI-OutboundReport: notjunk:1;M01:P0:Anh58Xrp+gg=;wpwPunikxO/mo54W9IEj2lky1z0 XI2h7H4ZDGCH4UtD1ypp7dDFDM3mZDAu8uFVfxzBvrlnapjZmlAK2twDB/Jyvs4dEgIwSQvh2 qtmcXvZh1xbI2rUilPpKcG02Imk7dcuNStS/oqbviyU2/CidNGPvTkUn+JtXEyBMnCP4kAMgb l3DZxUDUvwpaXhW2gw1clKdxILrk5M0rnOqMTA6N2ToFJNiJeIWqXy2nJ+QzoOFQlRafzeNVG ikG63FcW4YPUO7veJrzhId4kGcHD52qcK8VJoPR6ETmMQRRBH16m+5/9lbmLwvnEHgBLZu4qj cl8BNLBs1VyBxeT0UBs2Pfk8ErR2iuLZnePhOEeWAdaPi65EBQyovb3hETxW5mZVXbqo/OEN7 sL8DhR53s3ktkQHsueELTHvHJcuNP3fIa1Pridijgw9b3XR5ehvjuD++I5t5sctJ1/MlZ2iuq SbVT0Ggp6Ay+zDg8zGnr2t2m/yPCsHtbXZ7e+UpsHTUjoIn9nI+rWHGC4L3Qv4QCFMEYc1qmb HZIXr+0ttfhegpuYsQh5lDrSo+tfs3USS4ziXY8krucfRqi4/SqhJotw6TNjhPvLJi0Ehq9ID KRT6lQ8Bu1cszzh3jLMQro9AEiBU98sdkDqyIXHDlNtIbkwV8vlvCfG+kQuE9+dQtiKS2cfRk h2B5vBeApAefsoS8JecusaIqnNG4pA46D+8daZmUjlPsxygnpRJGEpxHq8wIceEq2Q65kWbZJ eorogocbbCfS7W2+0D9bKmjfMvSZmALToS3GG8lFpvJKlyNZINWPFYb5EXYOVr598RpWh6Zvs UT6qg67gcO2VKqjs365G1wDiCde2Xeb2EuttaVACVSOjk8CcbJsTHqm4zLxbQMaTFzFAqLChi Fj4adA3/XoU30BiampMHbEnD+YiWqqjVduCT7aycH7K4zncknGn+DCyQ94pX53TTipT//KAkF kAUwGQ== Received-SPF: pass client-ip=212.227.17.22; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Show the memory location of the heap in the /proc/pid/maps file inside the guest. The heap address will be stored in ts->heap_base, so make that variable visible for all guest architectures, not just architectures for semihosted binaries (arm, m68k, riscv). Show 32- and 64-bit pointers with 8 digits and leading zeros (%08x/%08lx). For 64-bit we could use %16lx, but we mimic the Linux kernel, which shows it with %08lx too. Example: user@machine:/# uname -a Linux paq 5.15.88+ #47 SMP Sun Jan 15 12:53:11 CET 2023 aarch64 GNU/Linux user@machine:/# cat /proc/self/maps 00000000-00009000 r-xp 00000000 08:01 2380521 /usr/bin/cat 00009000-0001f000 ---p 00000000 00:00 0 0001f000-00020000 r--p 0000f000 08:01 2380521 /usr/bin/cat 00020000-00021000 rw-p 00010000 08:01 2380521 /usr/bin/cat 00021000-00042000 rw-p 00000000 00:00 0 [heap] 5500000000-5500001000 ---p 00000000 00:00 0 5500001000-5500801000 rw-p 00000000 00:00 0 [stack] 5500801000-5500827000 r-xp 00000000 08:01 2395258 /usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1 5500827000-550083f000 ---p 00000000 00:00 0 550083f000-5500841000 r--p 0002e000 08:01 2395258 /usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1 5500841000-5500843000 rw-p 00030000 08:01 2395258 /usr/lib/aarch64-linux-gnu/ld-linux-aarch64.so.1 5500843000-5500844000 r-xp 00000000 00:00 0 5500844000-5500846000 rw-p 00000000 00:00 0 5500850000-55009d7000 r-xp 00000000 08:01 2395261 /usr/lib/aarch64-linux-gnu/libc.so.6 55009d7000-55009ed000 ---p 00187000 08:01 2395261 /usr/lib/aarch64-linux-gnu/libc.so.6 55009ed000-55009f0000 r--p 0018d000 08:01 2395261 /usr/lib/aarch64-linux-gnu/libc.so.6 55009f0000-55009f2000 rw-p 00190000 08:01 2395261 /usr/lib/aarch64-linux-gnu/libc.so.6 55009f2000-55009ff000 rw-p 00000000 00:00 0 Signed-off-by: Helge Deller --- include/exec/cpu_ldst.h | 4 ++-- linux-user/main.c | 1 + linux-user/qemu.h | 4 ++-- linux-user/syscall.c | 8 ++++++-- 4 files changed, 11 insertions(+), 6 deletions(-) -- 2.41.0 diff --git a/include/exec/cpu_ldst.h b/include/exec/cpu_ldst.h index 645476f0e5..f1e6f31e88 100644 --- a/include/exec/cpu_ldst.h +++ b/include/exec/cpu_ldst.h @@ -72,10 +72,10 @@ */ #if TARGET_VIRT_ADDR_SPACE_BITS <= 32 typedef uint32_t abi_ptr; -#define TARGET_ABI_FMT_ptr "%x" +#define TARGET_ABI_FMT_ptr "%08x" #else typedef uint64_t abi_ptr; -#define TARGET_ABI_FMT_ptr "%"PRIx64 +#define TARGET_ABI_FMT_ptr "%08"PRIx64 #endif #ifndef TARGET_TAGGED_ADDRESSES diff --git a/linux-user/main.c b/linux-user/main.c index dba67ffa36..12f3d8a93e 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -955,6 +955,7 @@ int main(int argc, char **argv, char **envp) the real value of GUEST_BASE into account. */ tcg_prologue_init(tcg_ctx); + ts->heap_base = info->brk; target_cpu_copy_regs(env, regs); if (gdbstub) { diff --git a/linux-user/qemu.h b/linux-user/qemu.h index 802794db63..7a6adac637 100644 --- a/linux-user/qemu.h +++ b/linux-user/qemu.h @@ -121,11 +121,11 @@ typedef struct TaskState { #ifdef TARGET_M68K abi_ulong tp_value; #endif -#if defined(TARGET_ARM) || defined(TARGET_M68K) || defined(TARGET_RISCV) + /* Extra fields for semihosted binaries. */ abi_ulong heap_base; abi_ulong heap_limit; -#endif + abi_ulong stack_base; int used; /* non zero if used */ struct image_info *info; diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 95727a816a..220c4a04b8 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -8131,14 +8131,18 @@ static int open_self_maps_1(CPUArchState *cpu_env, int fd, bool smaps) continue; } + path = e->path; + + if (ts->heap_base && h2g(min) == ts->heap_base) { + path = "[heap]"; + } + #ifdef TARGET_HPPA if (h2g(max) == ts->info->stack_limit) { #else if (h2g(min) == ts->info->stack_limit) { #endif path = "[stack]"; - } else { - path = e->path; } count = dprintf(fd, TARGET_ABI_FMT_ptr "-" TARGET_ABI_FMT_ptr From patchwork Tue Jul 25 11:52:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 1812491 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=deller@gmx.de header.a=rsa-sha256 header.s=s31663417 header.b=TYd/Pp0N; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R9FmH45Xbz20Fn for ; Tue, 25 Jul 2023 21:53:35 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qOGaf-0006wi-07; Tue, 25 Jul 2023 07:52:33 -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 1qOGad-0006wI-AN for qemu-devel@nongnu.org; Tue, 25 Jul 2023 07:52:31 -0400 Received: from mout.gmx.net ([212.227.17.20]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOGaa-0002Kv-Bp for qemu-devel@nongnu.org; Tue, 25 Jul 2023 07:52:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690285941; x=1690890741; i=deller@gmx.de; bh=RQzwqRdzC4UMPSJzIBjSaM7WEto38+vaHaaILdjhDXs=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=TYd/Pp0Nfo8Q5rVh0b1gAs1OSO1CM6ZrckhfTZSNbbX2MUDRu3IKXKOXrqzoWzJabAqx5yz Yb1O8JWsfOHG32ShmTd9r9JYA/X7lwZlQNFN2gwF6Af8YBdwNQcQyrus4QiGRV0faZCgJka6u BK2tX+Yg127pBz5HD+68iFWLdRxqE3eDlcReol6mezYaSSlaQ/6viGYgoDFGR6zwFFKOztUPs CqN96N9Ypimgfw3iBFdbvIIwovHtkXDj92kugHqPaSP/Ah2biRdL5jCLE10isj6LMkzUvc1Jd nxJQj7JnOuJ5QtucqMWcycSYc/Ag23m4jj2UwbQGU2jQpryKRoJw== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.145.136]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1M7sHy-1qJIVL1LRy-004xjV; Tue, 25 Jul 2023 13:52:21 +0200 From: Helge Deller To: Richard Henderson , Michael Tokarev , qemu-devel@nongnu.org, Andreas Schwab , Laurent Vivier Cc: Paolo Bonzini , Helge Deller Subject: [PATCH v3 2/3] linux-user: Optimize memory layout for static and dynamic executables Date: Tue, 25 Jul 2023 13:52:17 +0200 Message-ID: <20230725115218.52738-3-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230725115218.52738-1-deller@gmx.de> References: <20230725115218.52738-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:Uz3o4L8wIR3yYHYfuex1d5SxZFQ2fbQZNx8kcNx9b0QxY69PwrY xWA/NvGvpiXRB/hYq3D6p4r1R03Zhr3MZgELDAiVXecMyqAT0lY/s2XCSyWceuzFEClOGgb aK///1ZMwWQoGpnbuXnKlZz9qEedbs0FwOVTjbklqMoKUOGUHBryHi4IwUDYAxVfAV8f1my ZuL97WyDTXU9nCUR+Dvhw== UI-OutboundReport: notjunk:1;M01:P0:g3U8MkO5QnY=;6H8ANrZjZ5o59+r9YwQFfNy5rcx y28YXNJagqFlRdTS4U9VRqhsgQIL3fqe4VSvfYv9fv0fmFqZInhosavSZA5BJp6NvkfY13lDu CO7V58sPpR62Auv69pHUHUUOHB2eqDDXxVFDsUb7yrVDvWX57bZsPkJbTUY7ECoYb96Jz8mDW 1wI+7PH2sKfoqVZ6FWUYaO2p+psJqLlcbBr4IXRJtN1HjH52gfjFg00oRHHl2X4g8GoGnvEVC BsvzeQH1GPpOmvT9eLp+O5u/O9eAEXcx9AllylzC6f2Ws3Fi0kaNsZmrXguedrMOsCOSEiKq2 jzsppGJad1hntHMfKgdvCcDNheuU22zRH2tkstUzcLY6vQXscKL25zU+iNuTyMUtyxOzxHgu+ 1Wh9B+HWGOJBYpz2SHM3kiienRcNQ+IQ07jhMFLUnrLjW/oiWmJnp/tQq4qDg4qbTxG0usTOq 7vQGYMY+VgUM2KMbHeYmevGTM3QB2BTk9xkrvqQ8drbTegbToGS5+MOpP2F52nT+kCdmI+e0J E1KH1HfwTxx/XbThwIeheHkhUpjTEc/U3iuX6qkHf3n8PgC83AE22y4W0k0hrCeTtJIdg0sWb sPTSfBpELDOB3LsRdrBrs5puVNrikJt1iowDNOmPa7qWm2y3K+/hGsBhqA2qim0/Fbfym1khm Mcc4LgXZ7i3NXRiy1Afm5WgfBA+a1pGxa+57YChbksgMr1isZ+2GTNR+07VX+qZg3RD9FOI54 GJArIjfoKAwzZrqedNOSOKQOg2px5oOIanc707iGslhPAMBRJyz6m+cqiaKjv+gtQp1Wuur6W tzBipwi8BnG3WnIC/6hiNnL9ChF9dQOhvZj5zzP8q3m6DT6uHhpHVIGacD/MxDGFKCPUN0+W/ Tge2pXoDapKnjPs2gLLdjRC1z5x3Dh401yqtuI5iUiT8CHw64M0xd7dJwK1/c20ZoxTuzJYru zOzFb2hBiTWdg4uyyYqMHsboMbo= Received-SPF: pass client-ip=212.227.17.20; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Organize the emulated memory layout in a way which leaves as much memory as possible for heap for the application. This patch tries to optize the memory layout by loading pie executables into lower memory and shared libs into higher memory (at TASK_UNMAPPED_BASE). This leaves a bigger memory area usable for heap space which will be located directly after the executable. Up to now, pie executable and shared libs were loaded directly behind each other in the area at TASK_UNMAPPED_BASE, which leaves very little space for heap. I tested this change on arm64, armhf and hppa (all in chroot on x86-64), and with a static armhf binary (which is broken without this patch). This patch temporarily breaks the Thread Sanitizer (TSan) application which expects specific boundary definitions for memory mappings on different platforms [1], see commit aab613fb9597 ("linux-user: Update TASK_UNMAPPED_BASE for aarch64") for aarch64. The follow-up patch fixes it again. [1] https://github.com/llvm/llvm-project/blob/master/compiler-rt/lib/tsan/rtl/tsan_platform.h Signed-off-by: Helge Deller --- linux-user/elfload.c | 55 +++++++++++++------------------------------- linux-user/mmap.c | 8 ++++--- 2 files changed, 21 insertions(+), 42 deletions(-) -- 2.41.0 diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 861ec07abc..47a118e430 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3023,6 +3023,7 @@ static void load_elf_image(const char *image_name, int image_fd, abi_ulong load_addr, load_bias, loaddr, hiaddr, error; int i, retval, prot_exec; Error *err = NULL; + bool is_main_executable; /* First of all, some simple consistency checks */ if (!elf_check_ident(ehdr)) { @@ -3106,28 +3107,8 @@ static void load_elf_image(const char *image_name, int image_fd, } } - if (pinterp_name != NULL) { - /* - * This is the main executable. - * - * Reserve extra space for brk. - * We hold on to this space while placing the interpreter - * and the stack, lest they be placed immediately after - * the data segment and block allocation from the brk. - * - * 16MB is chosen as "large enough" without being so large as - * to allow the result to not fit with a 32-bit guest on a - * 32-bit host. However some 64 bit guests (e.g. s390x) - * attempt to place their heap further ahead and currently - * nothing stops them smashing into QEMUs address space. - */ -#if TARGET_LONG_BITS == 64 - info->reserve_brk = 32 * MiB; -#else - info->reserve_brk = 16 * MiB; -#endif - hiaddr += info->reserve_brk; - + is_main_executable = (pinterp_name != NULL); + if (is_main_executable) { if (ehdr->e_type == ET_EXEC) { /* * Make sure that the low address does not conflict with @@ -3136,7 +3117,7 @@ static void load_elf_image(const char *image_name, int image_fd, probe_guest_base(image_name, loaddr, hiaddr); } else { /* - * The binary is dynamic, but we still need to + * The binary is dynamic (pie-executabe), but we still need to * select guest_base. In this case we pass a size. */ probe_guest_base(image_name, 0, hiaddr - loaddr); @@ -3159,7 +3140,7 @@ static void load_elf_image(const char *image_name, int image_fd, */ load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | - (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0), + (is_main_executable ? MAP_FIXED : 0), -1, 0); if (load_addr == -1) { goto exit_mmap; @@ -3194,7 +3175,8 @@ static void load_elf_image(const char *image_name, int image_fd, info->end_code = 0; info->start_data = -1; info->end_data = 0; - info->brk = 0; + /* possible start for brk is behind all sections of this ELF file. */ + info->brk = TARGET_PAGE_ALIGN(hiaddr); info->elf_flags = ehdr->e_flags; prot_exec = PROT_EXEC; @@ -3288,9 +3270,6 @@ static void load_elf_image(const char *image_name, int image_fd, info->end_data = vaddr_ef; } } - if (vaddr_em > info->brk) { - info->brk = vaddr_em; - } #ifdef TARGET_MIPS } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) { Mips_elf_abiflags_v0 abiflags; @@ -3618,6 +3597,15 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) if (elf_interpreter) { load_elf_interp(elf_interpreter, &interp_info, bprm->buf); + /* + * Use brk address of interpreter if it was loaded above the + * executable and leaves less than 16 MB for heap. + * This happens e.g. with static binaries on armhf. + */ + if (interp_info.brk > info->brk && + interp_info.load_bias - info->brk < 16 * MiB) { + info->brk = interp_info.brk; + } /* If the program interpreter is one of these two, then assume an iBCS2 image. Otherwise assume a native linux image. */ @@ -3672,17 +3660,6 @@ int load_elf_binary(struct linux_binprm *bprm, struct image_info *info) bprm->core_dump = &elf_core_dump; #endif - /* - * If we reserved extra space for brk, release it now. - * The implementation of do_brk in syscalls.c expects to be able - * to mmap pages in this space. - */ - if (info->reserve_brk) { - abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk); - abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk); - target_munmap(start_brk, end_brk - start_brk); - } - return 0; } diff --git a/linux-user/mmap.c b/linux-user/mmap.c index a5dfb56545..848d2fd4bb 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -299,14 +299,16 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, #ifdef TARGET_AARCH64 # define TASK_UNMAPPED_BASE 0x5500000000 #else -# define TASK_UNMAPPED_BASE (1ul << 38) +# define TASK_UNMAPPED_BASE 0x4000000000 #endif -#else +#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32 #ifdef TARGET_HPPA # define TASK_UNMAPPED_BASE 0xfa000000 #else -# define TASK_UNMAPPED_BASE 0x40000000 +# define TASK_UNMAPPED_BASE 0xe0000000 #endif +#else /* HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32 */ +# define TASK_UNMAPPED_BASE 0x40000000 #endif abi_ulong mmap_next_start = TASK_UNMAPPED_BASE; From patchwork Tue Jul 25 11:52:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 1812492 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=deller@gmx.de header.a=rsa-sha256 header.s=s31663417 header.b=enKHRRLL; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R9FmH3vDTz1ydq for ; Tue, 25 Jul 2023 21:53:35 +1000 (AEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qOGae-0006wS-As; Tue, 25 Jul 2023 07:52:32 -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 1qOGac-0006w7-Tj for qemu-devel@nongnu.org; Tue, 25 Jul 2023 07:52:30 -0400 Received: from mout.gmx.net ([212.227.17.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qOGaa-0002MP-7S for qemu-devel@nongnu.org; Tue, 25 Jul 2023 07:52:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1690285941; x=1690890741; i=deller@gmx.de; bh=q34b3jO4pz0ToHdtWJOkpF1oDqxSHLwWgPdVBphMzi8=; h=X-UI-Sender-Class:From:To:Cc:Subject:Date:In-Reply-To:References; b=enKHRRLLAjSek/rpyb21T9weALihZ5u/3LOfGdZNqq7AKv+tuR3ycKZE5rgD2ZywGlHZiD4 2QoOiSwooakW/HpizJlg9SJmZavnAdRPnmvt2b8/+Vi37givyl8eQ6MJ5pmVIYyHVmWm/n7ds YuHKnX1vEeRXrlQc8YEVSnGaGPnu7lrOaGUPVbkmMd81e8ScD7Vp8vJSaZy6f1GqwXsWOm0Gj Ixnw9AgA9YMowCjcBjVrkTLd6UwajkGRcMeO7OHqhQ21T1DmlvkiXNDRwfJnI2T9Fw0RKbLUG wKhTGuZuW4Le4GPqrQiDhd5C2S5oUdnsqqpQ9LGppl1OvORqcHzQ== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from p100.fritz.box ([94.134.145.136]) by mail.gmx.net (mrgmx105 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MbAgq-1pmjyn2C1g-00bYex; Tue, 25 Jul 2023 13:52:21 +0200 From: Helge Deller To: Richard Henderson , Michael Tokarev , qemu-devel@nongnu.org, Andreas Schwab , Laurent Vivier Cc: Paolo Bonzini , Helge Deller Subject: [PATCH v3 3/3] linux-user: Load pie executables at upper memory Date: Tue, 25 Jul 2023 13:52:18 +0200 Message-ID: <20230725115218.52738-4-deller@gmx.de> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230725115218.52738-1-deller@gmx.de> References: <20230725115218.52738-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:hyJ06P6rPQLmSYh9PbSMURIh0SjLKBpaCLPvgLy49tZSdfAnloS 6MrbTgPyYeVaUMQnxg075OH8vympgFnWIx661gQsJMc6KnN3FiXTVwmyJ5uZdAmykgTPH+N 3EAHy+gdsTjOQQS0XJtZtnRktukc0PsL4vcD9R/HubswQl8fgdUvviOueMZcGSw09SAWCWD RqXQfH3SAIpWJlAAqJWZg== UI-OutboundReport: notjunk:1;M01:P0:dk/s/8KWZtU=;deXzR3Z7nAvr6qY1V6KcCGF0AjD tdKPpp4AYkEHSrFyrvXQQqchFKN84m7yDRdd1eHtPwgp10n0xPupxT+Gkyhdy4qIp0b7Q2jLb 3CTv9QGamM1aUr8D3v7bkYXstpiZE7fpYxHFXU4mXL/j40htr5/w1IVvWc2KsQSrNBYWOTBwv 3axDUBY+JpXLoO/Urq3xYAozLVJqmNbDfpAD0LsJBtj2l/O4gdD6z6sHxMHtfZUJMFVaX0W47 iHl88cERU6yZHtdXzSZNFTUsH/zM2HWZgMj30LH8XQYEOX9/piXWsVYs+wb7bEk58Jg8hLtsI PVyRdPpI9QJpXTDYdh9wmestb1xcs7Q6xcF5wF3dI0ySQ7j0LdOltqn2jgTuOEdKhcubN/SUC wKjXxhqcpr2y3/3ItqbU9CWy5qdfbohYwVQZN5/6UxYDMcqebQdr4E5R4jmgniul97LePQa9L 4fzC9OwlCuFs+XtJSJJpuDuXS5I5o8eWVSFzkoXpSLDsi637MYkrfPzf8o9gnYCCJGvQRmgZ7 wdvDwr5aNhqcSEHYAJ6ppIReIqS9n2LdnZfyHYBHIA5jQyC+hldlX/d9JiUwz/16IUiSbZqxC EfCE1qtIWXvu/NKSQntIoedBCGDry3tOacsboN1OeVmgYbuuAkxJ5KJh4VW0r7561OnHLTpCb RNDH4bZaYVzhXQVE0q4iaY1hgBJHrJxfmfqgszzU/vX8USGKJmOUVlmuQPWRI2ikVFGyLWPeN BxQyWLrYy8rvGyXZGUcfBlYMRm7DpmgUHccC2rN/quVGC3K1haI0z6Q/am3AX6jbrJRntceLB YSSOU96ysV+y7IAHTiIR1+eReXVcAJSZf+mKQ42BOiy6+tS9pJPyolo2mwUqNFycGG+lQupRn gejwBufNR+Dg1dWenB69+rvgViQflPxG9kZuqlal29LJ4QxWGDRPSTn5G3RHA7ykmUju3j1dJ z+41ZASYZ9QcT4HdYZl2hZFBeZ4= Received-SPF: pass client-ip=212.227.17.21; envelope-from=deller@gmx.de; helo=mout.gmx.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 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, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Adjust the loader to load dynamic pie executables at around: ~ 0x5500000000 for 64-bit guest binaries on 64-bit host, - 0x40000000 for 32-bit guest binaries on 64-bit host, and - 0x00000000 for 32-bit guest binaries on 32-bit host. Reason for this change is to unbreak the Thread Sanitizer (TSan) application again, as it was done in aab613fb9597 ("linux-user: Update TASK_UNMAPPED_BASE for aarch64"). Signed-off-by: Helge Deller --- linux-user/elfload.c | 6 ++++-- linux-user/loader.h | 12 ++++++++++++ linux-user/mmap.c | 16 +--------------- 3 files changed, 17 insertions(+), 17 deletions(-) -- 2.41.0 diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 47a118e430..8f5a79b537 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -3021,6 +3021,7 @@ static void load_elf_image(const char *image_name, int image_fd, struct elfhdr *ehdr = (struct elfhdr *)bprm_buf; struct elf_phdr *phdr; abi_ulong load_addr, load_bias, loaddr, hiaddr, error; + unsigned long load_offset = 0; int i, retval, prot_exec; Error *err = NULL; bool is_main_executable; @@ -3121,6 +3122,7 @@ static void load_elf_image(const char *image_name, int image_fd, * select guest_base. In this case we pass a size. */ probe_guest_base(image_name, 0, hiaddr - loaddr); + load_offset = TASK_UNMAPPED_BASE_PIE; } } @@ -3138,7 +3140,7 @@ static void load_elf_image(const char *image_name, int image_fd, * In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr = target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NONE, + load_addr = target_mmap(loaddr + load_offset, (size_t)hiaddr - loaddr + 1, PROT_NONE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | (is_main_executable ? MAP_FIXED : 0), -1, 0); @@ -3176,7 +3178,7 @@ static void load_elf_image(const char *image_name, int image_fd, info->start_data = -1; info->end_data = 0; /* possible start for brk is behind all sections of this ELF file. */ - info->brk = TARGET_PAGE_ALIGN(hiaddr); + info->brk = TARGET_PAGE_ALIGN(load_offset + hiaddr); info->elf_flags = ehdr->e_flags; prot_exec = PROT_EXEC; diff --git a/linux-user/loader.h b/linux-user/loader.h index 59cbeacf24..3ba41e9a7b 100644 --- a/linux-user/loader.h +++ b/linux-user/loader.h @@ -18,6 +18,18 @@ #ifndef LINUX_USER_LOADER_H #define LINUX_USER_LOADER_H +/* where to map binaries? */ +#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 +# define TASK_UNMAPPED_BASE_PIE 0x5500000000 +# define TASK_UNMAPPED_BASE 0x7000000000 +#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32 +# define TASK_UNMAPPED_BASE_PIE 0x40000000 +# define TASK_UNMAPPED_BASE 0xf0000000 +#else /* HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32 */ +# define TASK_UNMAPPED_BASE_PIE 0x00000000 +# define TASK_UNMAPPED_BASE 0x40000000 +#endif + /* * Read a good amount of data initially, to hopefully get all the * program headers loaded. diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 848d2fd4bb..9434bc805d 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -23,6 +23,7 @@ #include "user-internals.h" #include "user-mmap.h" #include "target_mman.h" +#include "loader.h" static pthread_mutex_t mmap_mutex = PTHREAD_MUTEX_INITIALIZER; static __thread int mmap_lock_count; @@ -295,21 +296,6 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, return true; } -#if HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64 -#ifdef TARGET_AARCH64 -# define TASK_UNMAPPED_BASE 0x5500000000 -#else -# define TASK_UNMAPPED_BASE 0x4000000000 -#endif -#elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32 -#ifdef TARGET_HPPA -# define TASK_UNMAPPED_BASE 0xfa000000 -#else -# define TASK_UNMAPPED_BASE 0xe0000000 -#endif -#else /* HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32 */ -# define TASK_UNMAPPED_BASE 0x40000000 -#endif abi_ulong mmap_next_start = TASK_UNMAPPED_BASE; unsigned long last_brk;