From patchwork Fri Jul 7 20:40:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 700050 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp4065882wrs; Fri, 7 Jul 2023 13:43:46 -0700 (PDT) X-Google-Smtp-Source: APBJJlHpUBTXpqDp42RPhCfnP5dMarWameKam8YPi9RVqh7XJl1WSpgxSDuaa96fOmrQWbMDX+ib X-Received: by 2002:a0c:e0d3:0:b0:635:f921:de18 with SMTP id x19-20020a0ce0d3000000b00635f921de18mr5056572qvk.53.1688762625822; Fri, 07 Jul 2023 13:43:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688762625; cv=none; d=google.com; s=arc-20160816; b=VTPwMISOL6bi78L+hrIE9jCSp18M4ugV6s/QSCuOBuz4SSCS6KnVhc/Fm4MhofCOyz hozNvdefgnQcOYdFoHGKWXyiOigwT/dhqWnvg//OD2F8OJJH87U1THzdENky5cTiU8PA KaMgKZI4gkCYU/3bEv7WkZp8EPD48/K6s7Bn7xEcfUe++4zB5/FNb9XDQld7UXog2N5B 7CXiPivEscceSXn1XMePHbfUdj2sKQPeJhywo679bGoy3ciYuAEoebsPdbWKcYyHhW8S nn170FFnAUYT1+cApYSBxVKaKvl8FRPh4NWgAcBlDOKJl495RkGOk0LONeuYV5u171Q/ gdCw== 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=VDUsgzcY3wT2DD7oU5Y2Go2253hS4srcoNjY2t7BE68=; fh=jVqwU2n6uDjg7kXqsBiuuwMdTrtXxwX52l5dkNb4FWE=; b=L1PK8iGW+3CLJ42CwNk/1UISDkz2dlv/50sjch9cPPxH+pdfhc1pZAhk9IXSWp8sF6 akZlktcwKSJihWIGE8EtQK/ABWQU66OUVx7+ATvEsrMM0OR0/KgJCajlahd1L7dWL2yW 3zaQ24dN3QnLMhmiCtaHYulc+DI6UQ+ncs6iMeXbPwL02nsy1OEwDNNfCt8d9HQRctNd 2Awyft1Uah2R46i32hSjv8m8KOhT2CvLSJw2G0RZC4zXixiLLBZVGazsi2NOwEVwOolF He+qbyVLpYaBz2bPpzhY7Pj/3uKclpE6Oe6VFUqrdcts5K1OUf1lYyNKMz1k1XJ1L9eR HFGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="XG/IaR+R"; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id e20-20020a0ce3d4000000b00571bb7c3c01si3126651qvl.337.2023.07.07.13.43.45 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 07 Jul 2023 13:43:45 -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="XG/IaR+R"; 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 Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qHsGR-0004qd-S4; Fri, 07 Jul 2023 16:41:15 -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 1qHsGN-0004pY-K4 for qemu-devel@nongnu.org; Fri, 07 Jul 2023 16:41:11 -0400 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qHsGL-0004nm-PB for qemu-devel@nongnu.org; Fri, 07 Jul 2023 16:41:11 -0400 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-3fbc5d5742eso25639645e9.3 for ; Fri, 07 Jul 2023 13:41:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688762468; x=1691354468; 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=VDUsgzcY3wT2DD7oU5Y2Go2253hS4srcoNjY2t7BE68=; b=XG/IaR+R2DTf+Lf3VhSuFhaAxFEtyJhmQ+KvpGwEhQN19ikfHnvH6NVwhGFN5ZmBQf 9IlImciC6Sg2+DArbiLuDnPrXlQWntpe6CvFEtVMrZq0oJUeSWy0/hxUcJyBc/tJdiHe dEaRSb6XNCvcrjJ8sowQJmD1+s8rKYo3k+jF0uBglhgx0ejq5Uv+lXohKl6CyAbE/9xf o/CioblQrkPdddnKLgLsTcRWvhDBECz1Lo6xo5ZKtQKK4AzQmkOeyRgcvgp3KHvelkT4 tw0ykVbfBPPyvGQnQCNrW6oLhXMVfU7OJQeYoMvPNe5BJDd+LR3aAPcTk+nYJRQy7WzY 8Kyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688762468; x=1691354468; 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=VDUsgzcY3wT2DD7oU5Y2Go2253hS4srcoNjY2t7BE68=; b=ODTNT4/rJykFeH4ovohHuTtueaFEsrYQ+YPLcyxrAch84uBa+QLKvOuiEZq1ZK3pEE dgAb8dDX2RutoM0WGAnoc4Bqrf/vi/SDGUtqOBwklSn0g6cJU/rYk7qyfBo4CGdR3lpo 3xaeZL7mTbG8cXJPaPvm5hbx19aV0HlgEnCbCEsv2NZ+7cwBxf0IoW6jCRkCsMro8RWZ vGT8YrTNmiXPuhECiIYH8/yP/y1LXAXNVGhhXh8KXvae3m2ZxDPN5hHbZwryWfryPYLS UAeVDIqeqxhBns7YvQcKJeCC8FKRnskZNNiGdp+tP4vLjnjoxK4xfBUwvCKDbtZhyIji yVgw== X-Gm-Message-State: ABy/qLamqotYwtEYubhULWTjHMx2G5EP7u7wp6q/vvQXvRKEhY3AzPk+ +QNmRdzMvTPfwT9oWriIUxdjqedbYXZaYCWE0DWRdA== X-Received: by 2002:a05:600c:b45:b0:3fb:bc6d:41f1 with SMTP id k5-20020a05600c0b4500b003fbbc6d41f1mr4633186wmr.17.1688762468502; Fri, 07 Jul 2023 13:41:08 -0700 (PDT) Received: from localhost.localdomain (host-92-23-208-66.as13285.net. [92.23.208.66]) by smtp.gmail.com with ESMTPSA id q10-20020a1ce90a000000b003fbb25da65bsm3337736wmc.30.2023.07.07.13.41.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jul 2023 13:41:08 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, mjt@tls.msk.ru Subject: [PATCH v2 17/24] linux-user: Use 'last' instead of 'end' in target_mmap Date: Fri, 7 Jul 2023 21:40:47 +0100 Message-Id: <20230707204054.8792-20-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230707204054.8792-1-richard.henderson@linaro.org> References: <20230707204054.8792-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::336; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x336.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 Complete the transition within the mmap functions to a formulation that does not overflow at the end of the address space. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- linux-user/mmap.c | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 6ecdf9e56d..67a117823f 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -455,8 +455,8 @@ abi_ulong mmap_find_vma(abi_ulong start, abi_ulong size, abi_ulong align) abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, int flags, int fd, off_t offset) { - abi_ulong ret, end, real_start, real_end, retaddr, host_len, - passthrough_start = -1, passthrough_end = -1; + abi_ulong ret, last, real_start, real_last, retaddr, host_len; + abi_ulong passthrough_start = -1, passthrough_last = 0; int page_flags; off_t host_offset; @@ -580,29 +580,30 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, host_start += offset - host_offset; } start = h2g(host_start); + last = start + len - 1; passthrough_start = start; - passthrough_end = start + len; + passthrough_last = last; } else { if (start & ~TARGET_PAGE_MASK) { errno = EINVAL; goto fail; } - end = start + len; - real_end = HOST_PAGE_ALIGN(end); + last = start + len - 1; + real_last = HOST_PAGE_ALIGN(last) - 1; /* * Test if requested memory area fits target address space * It can fail only on 64-bit host with 32-bit target. * On any other target/host host mmap() handles this error correctly. */ - if (end < start || !guest_range_valid_untagged(start, len)) { + if (last < start || !guest_range_valid_untagged(start, len)) { errno = ENOMEM; goto fail; } /* Validate that the chosen range is empty. */ if ((flags & MAP_FIXED_NOREPLACE) - && !page_check_range_empty(start, end - 1)) { + && !page_check_range_empty(start, last)) { errno = EEXIST; goto fail; } @@ -642,9 +643,9 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, /* handle the start of the mapping */ if (start > real_start) { - if (real_end == real_start + qemu_host_page_size) { + if (real_last == real_start + qemu_host_page_size - 1) { /* one single host page */ - if (!mmap_frag(real_start, start, end - 1, + if (!mmap_frag(real_start, start, last, target_prot, flags, fd, offset)) { goto fail; } @@ -658,18 +659,18 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, real_start += qemu_host_page_size; } /* handle the end of the mapping */ - if (end < real_end) { - if (!mmap_frag(real_end - qemu_host_page_size, - real_end - qemu_host_page_size, end - 1, + if (last < real_last) { + abi_ulong real_page = real_last - qemu_host_page_size + 1; + if (!mmap_frag(real_page, real_page, last, target_prot, flags, fd, - offset + real_end - qemu_host_page_size - start)) { + offset + real_page - start)) { goto fail; } - real_end -= qemu_host_page_size; + real_last -= qemu_host_page_size; } /* map the middle (easier) */ - if (real_start < real_end) { + if (real_start < real_last) { void *p; off_t offset1; @@ -678,13 +679,13 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, } else { offset1 = offset + real_start - start; } - p = mmap(g2h_untagged(real_start), real_end - real_start, + p = mmap(g2h_untagged(real_start), real_last - real_start + 1, target_to_host_prot(target_prot), flags, fd, offset1); if (p == MAP_FAILED) { goto fail; } passthrough_start = real_start; - passthrough_end = real_end; + passthrough_last = real_last; } } the_end1: @@ -692,16 +693,16 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, int target_prot, page_flags |= PAGE_ANON; } page_flags |= PAGE_RESET; - if (passthrough_start == passthrough_end) { - page_set_flags(start, start + len - 1, page_flags); + if (passthrough_start > passthrough_last) { + page_set_flags(start, last, page_flags); } else { if (start < passthrough_start) { page_set_flags(start, passthrough_start - 1, page_flags); } - page_set_flags(passthrough_start, passthrough_end - 1, + page_set_flags(passthrough_start, passthrough_last, page_flags | PAGE_PASSTHROUGH); - if (passthrough_end < start + len) { - page_set_flags(passthrough_end, start + len - 1, page_flags); + if (passthrough_last < last) { + page_set_flags(passthrough_last + 1, last, page_flags); } } the_end: