From patchwork Fri Mar 4 11:21:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 548103 Delivered-To: patch@linaro.org Received: by 2002:ac0:f585:0:0:0:0:0 with SMTP id s5csp1216imq; Fri, 4 Mar 2022 03:23:24 -0800 (PST) X-Google-Smtp-Source: ABdhPJyLudYAyGLGIq/j3b0KdqDx2Ol+yZ/0DCNafhHuqBDHfl9zffRjOO3ATE74KcfQHdrXKRyc X-Received: by 2002:a81:25c7:0:b0:2d0:9d5c:2463 with SMTP id l190-20020a8125c7000000b002d09d5c2463mr40615895ywl.164.1646393004016; Fri, 04 Mar 2022 03:23:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646393004; cv=none; d=google.com; s=arc-20160816; b=kD+5tc+tfVP8V+T1556g/5IgoJS5BuYx0Sj5N8gpU89Ki3ezIWltxyVcYzo1A7WpBa nobfMa/gKG2STVLDFi/UmikeUnJrh8VHUKcLnA1J3Qiu5KP/Yrr+wNNSofVrPIPavzYh 1BpYu+SA2n5HdrT2lu5gDKDePDzohId1nhxUndHeLee8i105aZnMRUJlQoVN7U1ZrKbn wD6tigCv5X+sg0QZnJPX6UC1nBYaVhDff7+S4lXnW5A0x1KFZt+VAbaEeA8ouLTMjfU4 nFkF9TMSF20NT38aNf4zDskacGhNKj7T/MBuesOrkwQswcP31TiYensbB5ylDf/YjQN2 n75w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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=aw1khmsL1b3jjSBXuTHlqqORQZf6pcNTAPrzcIvj+hU=; b=dqup646jERiEHPyjulVl0dY0ItOZcgembHgSnSGUfLV6v+b/2f3i5l9FNX2gLqbqQV oJqQk7zCOAphorWd4ZijMfEFRRtg/T/VjZkt7sXR3oCWLVJt6OjspnQUfSk7z8+sJmw3 OEc7f7buB9uYhWFFi1lJpaV1ZwlgTUlVjr4/7fIEjv0KjQkLnF6JCe/UTlcnCnGcoKFv /Ho3YY/+r9Y9+nOqr/EP5nI5SYCE0ihe0v/O6F7TSA8etkOIX5GX/0JvNKng+qmz0oFd Yn2H2CRocRFR+iKlw0gDWzohekKDa5kxw7e4w0Lt0ADWGre8nDBRO6Rqm/+DD4dbHFo4 0R/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=ft5D32Nt; 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=fail (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 k5-20020a815605000000b002dbfd8c6c80si3828002ywb.540.2022.03.04.03.23.23 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 04 Mar 2022 03:23:24 -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=fail header.i=@linaro.org header.s=google header.b=ft5D32Nt; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:54328 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nQ61r-00034b-GO for patch@linaro.org; Fri, 04 Mar 2022 06:23:23 -0500 Received: from eggs.gnu.org ([209.51.188.92]:54002) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nQ608-0002Zv-GJ for qemu-devel@nongnu.org; Fri, 04 Mar 2022 06:21:36 -0500 Received: from [2a00:1450:4864:20::335] (port=36822 helo=mail-wm1-x335.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nQ606-0004f3-T7 for qemu-devel@nongnu.org; Fri, 04 Mar 2022 06:21:36 -0500 Received: by mail-wm1-x335.google.com with SMTP id r7-20020a1c4407000000b00380f6f473b0so255645wma.1 for ; Fri, 04 Mar 2022 03:21:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=aw1khmsL1b3jjSBXuTHlqqORQZf6pcNTAPrzcIvj+hU=; b=ft5D32Nt5+dR1fEuly+ia2zINtI6Zy15tDDF3gsU+SsCwlgXdERaxwbCm/poOjHx6q mylHzrAts9pCQJGd9rDC1Fd57wt7G68/iNFmcJoBDUfNXnJOvcrO2Tm2XhTV/3FYdJe3 hWqNv7qdC+A74JpuktqyJYTL9YVvXNEPGJZY3SgaLCTXuqKBNGUZWtJnKf5DXOlNRa9P OfvZyB7JkfbBzSDqX2ZeIRDboUXOTuvgR+8rIiyF3xa3urHuvD5kY6NP2ExL+cn8MSRH WpdX6wDYURGWETz30iQAJFBGppyowRj0fCANMF6wywzCZ3tiPff8scliVvWHKKkT+ntH 0Y+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aw1khmsL1b3jjSBXuTHlqqORQZf6pcNTAPrzcIvj+hU=; b=CxtcLPiaH8b9UY6SRSYWEtZ9tU005BywYXzsVlaP77a0ELvSFjS1ERVB/HWibVGPHm S/VaD0L2nTD00DtOmfImMe/Ia1fRxzpw/xDyp/8SN1Tdo9vnVviOtHSsnB7ioxCiNDb5 iNijcw562DBSZLVlsFNcYrf6SqgsnNeMpJoUtoOWOcPN0dS77f7RYal9/7+t0JLHiO+X VsE/29Wsbt8Vby2+s3vm3pISxRgp8QhjIa2hep7ZT9QVI7q6KIYkNSSvYyF1GY5KIHCp 3XhmiLWiKsvVSgkHGt/pkIenuKq0hVDKgWdTlKMMPHyKfk7SuSj1kUxPKzDtzPWrVfAR 3fcQ== X-Gm-Message-State: AOAM530XZKtB9ery62VpXCmsMuGPTuRh7zPEvBE1t4em/e/TzhFz4TIQ 2QWlzjPT3ZXQFx1EIp6xwpPkb3DDkwWFPg== X-Received: by 2002:a1c:ed1a:0:b0:37f:a539:5d5e with SMTP id l26-20020a1ced1a000000b0037fa5395d5emr7412175wmh.37.1646392893522; Fri, 04 Mar 2022 03:21:33 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id u14-20020adfed4e000000b001e3323611e5sm4148651wro.26.2022.03.04.03.21.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 03:21:32 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PATCH v2 4/9] util: Return valid allocation for qemu_try_memalign() with zero size Date: Fri, 4 Mar 2022 11:21:21 +0000 Message-Id: <20220304112126.2261039-5-peter.maydell@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220304112126.2261039-1-peter.maydell@linaro.org> References: <20220304112126.2261039-1-peter.maydell@linaro.org> MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::335 (failed) Received-SPF: pass client-ip=2a00:1450:4864:20::335; envelope-from=peter.maydell@linaro.org; helo=mail-wm1-x335.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: , Cc: Paolo Bonzini , Richard Henderson , qemu-block@nongnu.org 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: Philippe Mathieu-Daudé Reviewed-by: Richard Henderson --- 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;