From patchwork Mon Mar 7 16:46:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 548874 Delivered-To: patch@linaro.org Received: by 2002:adf:80eb:0:0:0:0:0 with SMTP id 98csp1094010wrl; Mon, 7 Mar 2022 08:52:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJwgnK2RIO8VsUz1gXWb0GrYJIQiH2t6lYBDx8JOQQaTtowWwZWsmo7P8jIi8TPIbBExKLJC X-Received: by 2002:a81:e0b:0:b0:2ca:287c:6be1 with SMTP id 11-20020a810e0b000000b002ca287c6be1mr9247655ywo.134.1646671972525; Mon, 07 Mar 2022 08:52:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646671972; cv=none; d=google.com; s=arc-20160816; b=0Er6wS3dZv3sfSjmro6P5iBJXlA5voweSKnINeob5MjIS7YSj/BlhUM/r75VFQardO FNmfWZpOjShMRPou5wfRCOg70LFRJ5GTbH4BOS0oJUckSYfexHCe+feh71dSy1I5EmqR tzNRVYGt1CWhfQkDtZ2Y7pDhbnQw6DYhFeAdiTkTWRKUAlEYxjV4ndtxxfySrMiv6vZT 4kxb/2VYmH+vBjUvfH9u4AXANEF5e/c9ZhO3+qtKarQ/d8GglLIPFzi+xnaE5jLbR2Rr fGEpnDFdDJPNo5oGFonG/NitnfGxba4/M4nY3h/MmkQvq2MXwyJBQMU3Uxq6svS8m9kz pdOw== 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:to:from :dkim-signature; bh=wNLlmiJKgcgevH7l2nRJUQn8/Ms/TerlHklY+uOxLsA=; b=a53geF9thFoTOKgMPOUszkedSPAngIfHZKzakaMnCO8dMD/izWa+FDlRHN2J5cN5U8 AEhmlsO2dsj0hApNgdIVSRcLNdYKgpcyJpplmBeTtoXB9/MDgXZ4kC1C9Si51Vlu29rW xXfkSVKkxP4YN33E+MNYue4xZNb6PMWiOTRRVeORhUkQWp0Syaei+0XK1pY6mkj6LMTJ OCzoT2io2T9Oxh65ncyglFVq4LO2Q3TPgovO65/+3Zm1pUyzzSN9x9gdIYxPsp85RgHn BwjzecCC3vNsLAwe93ZLOcOVa0PNznwOq+MeADfvbN8OpgkHx/gTMnoWbKdyuawbI2rk 4g4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=xTyjjVL2; 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 g67-20020a813946000000b002d035ccf9cfsi7890390ywa.115.2022.03.07.08.52.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 Mar 2022 08:52:52 -0800 (PST) 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=xTyjjVL2; 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]:48532 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nRGbL-0003ha-VT for patch@linaro.org; Mon, 07 Mar 2022 11:52:51 -0500 Received: from eggs.gnu.org ([209.51.188.92]:33910) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nRGVx-0001N2-H9 for qemu-devel@nongnu.org; Mon, 07 Mar 2022 11:47:17 -0500 Received: from [2a00:1450:4864:20::42d] (port=39515 helo=mail-wr1-x42d.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nRGVv-0007iz-Qv for qemu-devel@nongnu.org; Mon, 07 Mar 2022 11:47:17 -0500 Received: by mail-wr1-x42d.google.com with SMTP id n15so10165596wra.6 for ; Mon, 07 Mar 2022 08:47:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=wNLlmiJKgcgevH7l2nRJUQn8/Ms/TerlHklY+uOxLsA=; b=xTyjjVL2OXeg6EXRwId/5u2w32ZmV6gDAzGVwLT/AKmz4mcTs608VSAqj0yAeczzUc K4nGm7aHCDGHZOiPlc38ZXUFMco9IOjpsUy9J1evcJ8VWFZBusSi2jc6k/EVzMp3gUE4 mGT+vZW50G38wefn7vvcv78q0AmzD7g0RFh/E5Am2Mbfr2leCOogZ7sf9XL3RIy5TCi6 HofSJdoCHwXWN/FGsD+DY3DYBOIByevQ1h+Ky3qRnCcxwFwfX1tNuvOoqLuehyc73vMd esIQ7153KkeRMJcBY/y/kA0StG9GhLBxGMCKmux4Dgq59Kc7GpEiKMYXuYhzsu3XAFWH K5gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wNLlmiJKgcgevH7l2nRJUQn8/Ms/TerlHklY+uOxLsA=; b=uLnQUqg87uNdRqRVdf4+ChFf8j1JF/TF85pmUUL/+JhUgqlNIfULQE2VUB+fts9+p9 ZCtGHiATM34rFYF4cYJvKH2KfP9pWkLgWbO1dME/vC37I9DX/RYtHCTJ/RRdsO0G2TI3 Zqp4WW/zH/Wsj4Q/J55Q2LeSoCUWZ2k5D+9xVe3P0BtjVBokOe4DLAyRBPZKfWiMVbdH W8htKSO2O1t/9NFUaTqZUOyycKISMfOhKqQhCJAj7D7D5QCAWxVk1S0lKu+XnMjR3j/T MAvc0KL10yYvDK4ojSDLx/vxjsOxU++KknAZ2zZtu2VRfgGM+rEs0qPLbyEHzqPfShFL /mmQ== X-Gm-Message-State: AOAM533PtyegshhGmt2tl1gGN2Caim2yx3aKJ3cWyaVyzVQ6EOvWJ+ei Vhki7P4qW2B2hlGomGqo6dz6rCE8bqN9Mw== X-Received: by 2002:a05:6000:15cb:b0:1f1:e283:fcc0 with SMTP id y11-20020a05600015cb00b001f1e283fcc0mr8029596wry.18.1646671634444; Mon, 07 Mar 2022 08:47:14 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id v188-20020a1cacc5000000b00384b71a50d5sm13806652wme.24.2022.03.07.08.47.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Mar 2022 08:47:14 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 03/18] util: Return valid allocation for qemu_try_memalign() with zero size Date: Mon, 7 Mar 2022 16:46:54 +0000 Message-Id: <20220307164709.2503250-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220307164709.2503250-1-peter.maydell@linaro.org> References: <20220307164709.2503250-1-peter.maydell@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::42d (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::42d; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x42d.google.com X-Spam_score_int: -6 X-Spam_score: -0.7 X-Spam_bar: / X-Spam_report: (-0.7 / 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, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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" Currently qemu_try_memalign()'s behaviour if asked to allocate 0 bytes is rather variable: * on Windows, we will assert * on POSIX platforms, we get the underlying behaviour of the posix_memalign() or equivalent function, which may be either "return a valid non-NULL pointer" or "return NULL" Explictly check for 0 byte allocations, so we get consistent behaviour across platforms. We handle them by incrementing the size so that we return a valid non-NULL pointer that can later be passed to qemu_vfree(). This is permitted behaviour for the posix_memalign() API and is the most usual way that underlying malloc() etc implementations handle a zero-sized allocation request, because it won't trip up calling code that assumes NULL means an error. (This includes our own qemu_memalign(), which will abort on NULL.) This change is a preparation for sharing the qemu_try_memalign() code between Windows and POSIX. Signed-off-by: Peter Maydell Reviewed-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- util/oslib-posix.c | 3 +++ util/oslib-win32.c | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 0278902ee79..f7e22f4ff9b 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -209,6 +209,9 @@ void *qemu_try_memalign(size_t alignment, size_t size) g_assert(is_power_of_2(alignment)); } + if (size == 0) { + size++; + } #if defined(CONFIG_POSIX_MEMALIGN) int ret; ret = posix_memalign(&ptr, alignment, size); diff --git a/util/oslib-win32.c b/util/oslib-win32.c index 05857414695..8c28d70904d 100644 --- a/util/oslib-win32.c +++ b/util/oslib-win32.c @@ -48,12 +48,14 @@ void *qemu_try_memalign(size_t alignment, size_t size) { void *ptr; - g_assert(size != 0); if (alignment < sizeof(void *)) { alignment = sizeof(void *); } else { g_assert(is_power_of_2(alignment)); } + if (size == 0) { + size++; + } ptr = _aligned_malloc(size, alignment); trace_qemu_memalign(alignment, size, ptr); return ptr;