From patchwork Tue Aug 20 05:08:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 820602 Delivered-To: patch@linaro.org Received: by 2002:adf:e98a:0:b0:367:895a:4699 with SMTP id h10csp64865wrm; Mon, 19 Aug 2024 22:09:54 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCWssEl6WILpljuMspMiwrzTB9uqRRL+SZdIryIPbRf+e9pEvQ24GjgAVO0W1j6a3xL47QstFEDgmaOY7t1N0/m8 X-Google-Smtp-Source: AGHT+IFcsXKWx4uv4lhO4vI6q+GAUtEnmc12N88aiIliSch0z+HkOZPNeL+7b0nrvmgwuaEg+Rwj X-Received: by 2002:a05:620a:2483:b0:79d:9102:554a with SMTP id af79cd13be357-7a50690223bmr1650875585a.14.1724130593952; Mon, 19 Aug 2024 22:09:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1724130593; cv=none; d=google.com; s=arc-20160816; b=EF5j3CQGFgrjNzUWKpJ8Lb8C+tcSwjyLfNK2wD0ErGhoVn+OAbNxW70s8Bw5pjXwWA sLeHljeI+7ujoUjUX1c+rrXLn4AgbP5maOjjqjg5h4Yb7E4DojQWav0t1VBchSLYINye gehRjQ63wTM1gKamIDkanHzsjbeKwSQQcwTVsqOdw2lxXBhV8zJXBVgVhelyuBTpvxgZ +gqMpSsbjUDFy0nzS9XES4V1f4vI2pn/Ihhu6xZTxTbXZY0eCYQdccbUdzTdwM8GKHf9 XQSi7xaRYcZwxs1qhLTW1i8iIUinf6BU5A9fzpC43ydw0eKypBUbTXYXpYsOO9AxrIuG EwQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=z/V+/zK9f6DneOT0stD1zbRm+cUo+xuW5miljh1QM8Q=; fh=mLXbt1omjVZVJNW4dCxVC9Q4UyGbEQtnA7SM68wWA/g=; b=cg/vXmFoGf2yF5jd26JTmZKyc7BmjBnnX1QP++E0UpTThPo8VQ0eQsoAx/29ISVEYK qMpu6xppyx3ZBUIOD57k+tm44/EPbgczSZY4SsHK8LA/ImWgFUb7x0SNWlOpucyFEJat 3+ywdGIQfxO9DVAoA6/36u7+Gr/hjaXJ3R9ijWAo1swxXqsy8NZwL5rv5lVP+jInkQdY ycKnwCcUdNoX9ebpXsU0v8ij4V5qL9npcHNxNHf3yHQDZYOn8GuQM5t+lK05t5MlJhdF /pZWhMtImYlqzejhlHYsJRAqvy3f19edfqxwJRnEPCU9mwxNke+Tu6A5kanUU1MokjOu cH1Q==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="dvC/ETQR"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7a4ff0e636asi1177087785a.516.2024.08.19.22.09.53 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 19 Aug 2024 22:09:53 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="dvC/ETQR"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sgH7Q-0004PP-7P; Tue, 20 Aug 2024 01:09:20 -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 1sgH7A-0004LU-FU for qemu-devel@nongnu.org; Tue, 20 Aug 2024 01:09:05 -0400 Received: from mail-oa1-x35.google.com ([2001:4860:4864:20::35]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sgH77-0007q0-HT for qemu-devel@nongnu.org; Tue, 20 Aug 2024 01:09:04 -0400 Received: by mail-oa1-x35.google.com with SMTP id 586e51a60fabf-2704d461058so1541068fac.0 for ; Mon, 19 Aug 2024 22:09:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1724130540; x=1724735340; darn=nongnu.org; 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=z/V+/zK9f6DneOT0stD1zbRm+cUo+xuW5miljh1QM8Q=; b=dvC/ETQRSGZ4XlRWPF3Cla6m8ZCxk8XrFRzuq2N2AttOy0JLds608Oaz2L82Qo2FrL gHf8O3UN3E870TlgrrKAiKx1whELzkGg1O8TOtnLf6Q4WYkV2fzZK4hEGXX6zSM3LWHc JPh30gs+VP+Dqt0UbPMV4Hkx0566NUSruOZZXaZ3gmN8US/A38jLmzo9dpUzBj8UiVct rX7lxuRly2sN1GBX786zTloH8ijGMrhrj2dLefBR5+otajQfKgpmNv1GQwHvPVeY3jQx tlLDrYZE9ChbB0cvO09+1EECWBffyPcvCPnRLdsPMZClonP/tecNSrSR0LZmYkRO4qyx XTRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724130540; x=1724735340; 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=z/V+/zK9f6DneOT0stD1zbRm+cUo+xuW5miljh1QM8Q=; b=UxfAfLOi23QUXubUn5LtDAhnNMyYguvDDEEHiCENZheBKQYVoeBcTd7VPSUxhbtZfJ sw1yhk6QZYGQqNLT+YP1xnWIaaQR60aeGcwBxGEGDex2lCmeZj2FNDiiJcQfCi1RseDz 8kQMs83vYHDSVwCkffmifzSddyATO4jX8nMenMDfvfKaingOqf9YaA/xRL9Va0wAW6iT S/pZ3KVswOZNlSWeYxNv3YRMg+I6oPj3OPgTXsf0h6ZbYDOqiDL6gmt40sa1w31QNaJN 6Qt8AHLlUB18i/raIOR2Jjh5Xi8NMFRXliB2PPYH10/MZshi9/dNorcsZHwB4fhi436t pfiQ== X-Gm-Message-State: AOJu0YxmSmm08f6Rg0CLYopCagimQW/VPY6ZuvmkgxjhGZJJggpCH3f/ QjOlG/wj4BFRnlf3+cKuGhHvzzaHB0nIpNSxFIfJ1d0o6jsNi//v57tWM6oMMev8Y9QzWbGGSyF 96CY= X-Received: by 2002:a05:6870:d607:b0:261:1eb4:dd8e with SMTP id 586e51a60fabf-2701c3450e3mr15351560fac.2.1724130539755; Mon, 19 Aug 2024 22:08:59 -0700 (PDT) Received: from stoup.. ([203.30.3.188]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7127af416fbsm7382814b3a.207.2024.08.19.22.08.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Aug 2024 22:08:59 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: gaosong@loongson.cn, philmd@linaro.org, imp@bsdimp.com, qemu-stable@nongnu.org Subject: [PATCH v2 1/2] linux-user: Handle short reads in mmap_h_gt_g Date: Tue, 20 Aug 2024 15:08:47 +1000 Message-ID: <20240820050848.165253-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240820050848.165253-1-richard.henderson@linaro.org> References: <20240820050848.165253-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2001:4860:4864:20::35; envelope-from=richard.henderson@linaro.org; helo=mail-oa1-x35.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, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org In particular, if an image has a large bss, we can hit EOF before reading all host_len bytes of the mapping. Create a helper, mmap_pread to handle the job for both the larger block in mmap_h_gt_g itself, as well as the smaller block in mmap_frag. Cc: qemu-stable@nongnu.org Fixes: eb5027ac618 ("linux-user: Split out mmap_h_gt_g") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2504 Signed-off-by: Richard Henderson --- linux-user/mmap.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 6418e811f6..e4bf5d5f39 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -283,6 +283,40 @@ static int do_munmap(void *addr, size_t len) return munmap(addr, len); } +/* + * Perform a pread on behalf of target_mmap. We can reach EOF, we can be + * interrupted by signals, and in general there's no good error return path. + * If @zero, zero the rest of the block at EOF. + * Return true on success. + */ +static bool mmap_pread(int fd, void *p, size_t len, off_t offset, bool zero) +{ + while (1) { + ssize_t r = pread(fd, p, len, offset); + + if (likely(r == len)) { + /* Complete */ + return true; + } + if (r == 0) { + /* EOF */ + if (zero) { + memset(p, 0, len); + } + return true; + } + if (r > 0) { + /* Short read */ + p += r; + len -= r; + offset += r; + } else if (errno != EINTR) { + /* Error */ + return false; + } + } +} + /* * Map an incomplete host page. * @@ -357,10 +391,9 @@ static bool mmap_frag(abi_ulong real_start, abi_ulong start, abi_ulong last, /* Read or zero the new guest pages. */ if (flags & MAP_ANONYMOUS) { memset(g2h_untagged(start), 0, last - start + 1); - } else { - if (pread(fd, g2h_untagged(start), last - start + 1, offset) == -1) { - return false; - } + } else if (!mmap_pread(fd, g2h_untagged(start), last - start + 1, + offset, true)) { + return false; } /* Put final protection */ @@ -853,8 +886,7 @@ static abi_long mmap_h_gt_g(abi_ulong start, abi_ulong len, } if (misaligned_offset) { - /* TODO: The read could be short. */ - if (pread(fd, p, host_len, offset + real_start - start) != host_len) { + if (!mmap_pread(fd, p, host_len, offset + real_start - start, false)) { do_munmap(p, host_len); return -1; } From patchwork Tue Aug 20 05:08:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 820604 Delivered-To: patch@linaro.org Received: by 2002:adf:e98a:0:b0:367:895a:4699 with SMTP id h10csp64894wrm; Mon, 19 Aug 2024 22:09:59 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCX3vV7jPlQlM5CPT3pAoNuFY4IERfdssokH6PeD5WFxrijXrZ3+a1xxY7rfFO3dBLPpRpw8wg==@linaro.org X-Google-Smtp-Source: AGHT+IHZ/lvljwdosoCqqxWY+XBqM8yQ1RIKZA+ajj92l1P6dbQNhB33pZbrHrDQxdDWMP+brB/t X-Received: by 2002:a05:620a:4621:b0:79d:79cb:2a68 with SMTP id af79cd13be357-7a5069d78cbmr1390514685a.57.1724130599249; Mon, 19 Aug 2024 22:09:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1724130599; cv=none; d=google.com; s=arc-20160816; b=G6WsLTEdSKzSoIRTuivnfvZqx5AjJE7l09nUmZst+XVR+qnb13bnMbj8v8sz1YXul8 S2y9lPNCBF0LJhzLT8kKjfQqEoPbBOu4TWWcJSXK+qK8FLI/4FoPgKbGihff8RWZTFSR jjK9HwaveNR1nNUE5X+uH9Vs1Vp/ynM/+vs7d9Lqhmx8zhAkA4+pPHHjLVW8EZVUZEua wpBWb/mMczzhuB919G8RegDz/TaHaEmS5EqdEFZkInHirHBKIqEIMH/7F67ccLWY8zAf V+yHjOdCu826B5K32ngE6ROLHSAOOKOl3jfr0gCFoxNLIPSw3rL9sm2XD/eWrQgL/r/c c4ZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=6rLh/vfsNw8zpHc+sq9u0/i4PzeonkxDDSgQpULc4qg=; fh=NWeu539VbNHEOpnoTT++NFSrIzOm8wClhCk0uD9nTOM=; b=nkiMl8ZfOd6JosqpVwdfeSTGSNvmlhH1oKBsAoB4E+9zrFUcVSdPOL5OWXrchzznbb ZEMHNYu0Ak47s1kQmIuh3nRtcXwYQ5FFvysQt/E9LviqkHYyNcLL0kXLdfV3/uOX5OPU q3YjgT14dqaHc52V5pX1WVQnts14xOC27kks9MaYxKowPS7dhY+CpfWXSf/Tru4qSPOz GJOyu/khc/C+dLDei+6t/B704cqRchWpqT85p9LCaSJ1xUp0mpDXeJdbxS1YeH6jNnfU QXDjftJBis8+pSw1m7HL/ENhXnaRRnJffBv0fi55peTfzvngoU60amDYzfZBlXfemB17 Es9w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jfRrJBxZ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7a4ff1114a3si1203447685a.624.2024.08.19.22.09.59 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 19 Aug 2024 22:09:59 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=jfRrJBxZ; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sgH7b-0004l1-0v; Tue, 20 Aug 2024 01:09: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 1sgH7C-0004MO-9Y for qemu-devel@nongnu.org; Tue, 20 Aug 2024 01:09:10 -0400 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sgH7A-0007qP-Lv for qemu-devel@nongnu.org; Tue, 20 Aug 2024 01:09:06 -0400 Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-713edc53429so1544579b3a.2 for ; Mon, 19 Aug 2024 22:09:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1724130543; x=1724735343; darn=nongnu.org; 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=6rLh/vfsNw8zpHc+sq9u0/i4PzeonkxDDSgQpULc4qg=; b=jfRrJBxZAf5KGv0XxmmNawA376QCKO/4z7VZkUJTqoB7BFnYe/rVHrMMIeKSoYKEdr Kq8iHt38/kH04KIajcMvhqcDq9yG6mtcjhF/8YYY4uCmgAYem1Nc83mEHggepVpGbRg7 FBxanEW7VimjmgzWIQI0zsojDNdDvsEBZsz1oURTtP41xlbdEm5hRTFxv/RO+UPEixMH OsIorRcMxwuxUx6N7CCLgLE8TO907PUOU59kHHatkIJ4FqX0dQG70yIPB6uFFNvO+n6y CQbtZugCkVHxwi3n94fyuUJfxq+8EdUdQAg5TivQ9il0atUneBjerjt7jwnclgRdQb7u S8sA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724130543; x=1724735343; 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=6rLh/vfsNw8zpHc+sq9u0/i4PzeonkxDDSgQpULc4qg=; b=TIS4KQkKDDeTn8a6rQnb7T7kGNSmbj57DV5RTaNK9qJeaLsyy9WANEv/BL2i4n1/qQ NxHizbEwc/XSqUE3OdLvx/Z4qa1lF/F9F7q14TJrj8Jy+VOz2O+M+B+wFyXqJ0pnZ1lB yi/X2qZGPyXWNMsxf2k4+oTmVnpCMiQxvfMg50WwtGQzZ4hkTeLKRlLNEddxwZ8Ttzv/ Qx8LOGH04gcLXLn1LF57NIuxwLBLDbl1kMnrGboeF1nQ9H8Cee9aBclApjmGl10ld5n4 HzDEOuyHpEH4r+tIb52tBUYaJvNFDU/zQsAKDBfrnbStodO8lSqu0QnWN/wrURMW6In3 PLmA== X-Gm-Message-State: AOJu0Yyr+F10o3XR8UNpdbtRDyU95Isf1lTZG39E+j4r7jUSfbZc0/p2 PAwazpdyD/S+qwqOQRg+nwprHWBLY6eqyLQ8hZTwJ69ZLEpP+U5S9ES4iIAw91H6zmFD18T9/T4 f46Y= X-Received: by 2002:a05:6a00:198d:b0:70d:1e20:1c1c with SMTP id d2e1a72fcca58-713c4ed3913mr12924086b3a.22.1724130542575; Mon, 19 Aug 2024 22:09:02 -0700 (PDT) Received: from stoup.. ([203.30.3.188]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7127af416fbsm7382814b3a.207.2024.08.19.22.09.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Aug 2024 22:09:02 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: gaosong@loongson.cn, philmd@linaro.org, imp@bsdimp.com Subject: [PATCH v2 2/2] bsd-user: Handle short reads in mmap_h_gt_g Date: Tue, 20 Aug 2024 15:08:48 +1000 Message-ID: <20240820050848.165253-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240820050848.165253-1-richard.henderson@linaro.org> References: <20240820050848.165253-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::433; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x433.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, RCVD_IN_DNSWL_NONE=-0.0001, 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org In particular, if an image has a large bss, we can hit EOF before reading all bytes of the mapping. Mirror the similar change to linux-user. Signed-off-by: Richard Henderson --- bsd-user/mmap.c | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c index f3a4f1712d..775e905960 100644 --- a/bsd-user/mmap.c +++ b/bsd-user/mmap.c @@ -128,6 +128,40 @@ error: return ret; } +/* + * Perform a pread on behalf of target_mmap. We can reach EOF, we can be + * interrupted by signals, and in general there's no good error return path. + * If @zero, zero the rest of the block at EOF. + * Return true on success. + */ +static bool mmap_pread(int fd, void *p, size_t len, off_t offset, bool zero) +{ + while (1) { + ssize_t r = pread(fd, p, len, offset); + + if (likely(r == len)) { + /* Complete */ + return true; + } + if (r == 0) { + /* EOF */ + if (zero) { + memset(p, 0, len); + } + return true; + } + if (r > 0) { + /* Short read */ + p += r; + len -= r; + offset += r; + } else if (errno != EINTR) { + /* Error */ + return false; + } + } +} + /* * map an incomplete host page * @@ -190,7 +224,7 @@ static int mmap_frag(abi_ulong real_start, mprotect(host_start, qemu_host_page_size, prot1 | PROT_WRITE); /* read the corresponding file data */ - if (pread(fd, g2h_untagged(start), end - start, offset) == -1) { + if (!mmap_pread(fd, g2h_untagged(start), end - start, offset, true)) { return -1; } @@ -565,7 +599,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int prot, -1, 0); if (retaddr == -1) goto fail; - if (pread(fd, g2h_untagged(start), len, offset) == -1) { + if (!mmap_pread(fd, g2h_untagged(start), len, offset, false)) { goto fail; } if (!(prot & PROT_WRITE)) {