From patchwork Tue Jan 30 13:31:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 194176 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2087:b0:106:209c:c626 with SMTP id gs7csp1220134dyb; Tue, 30 Jan 2024 05:32:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IGeq5AfdDvNspexEKwRSQCReeypapEAHjloRCwwbmoj8YCgvaLEM/FL+1GAbjv8FANFkxCj X-Received: by 2002:ad4:5f8b:0:b0:68c:53eb:720 with SMTP id jp11-20020ad45f8b000000b0068c53eb0720mr3475593qvb.23.1706621547702; Tue, 30 Jan 2024 05:32:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706621547; cv=pass; d=google.com; s=arc-20160816; b=uK4cFsSUY4B50b4sn9d8Cy/cIub5w4RAEyQXgZiLruKR/GE/tOQ1aM00ZWb1nPbsqH V1kZzvchnvxgJxWNAnTTSis0A68RWwiWL2z2IE47NI7s0XFvVRGnyXWDW4ikA9qo4iTs ENksx6llpfN3LdSZMtYhgEhfkAzOnh32auQqqZP75dk4Sa/ezUuTe1ADO7v+dVO3Mj51 C0O0bvkoHLqUnypyC0N81EhcHETwuOorLhf+70EMWIip/3t1tN77EpMG7GlCN9E4haMM zuSSLTRNOIv0cwE+7S/qNJjLte0voRr7BFJWnmzCLwTGHbMf4XtK0OVJ2Ayl5Eb6vtg2 B6RQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=AXj2CnzpTcoXrB2lCVjdR2uBUHQVQwWn57PI/xShUxM=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=ULs+nZXAm83oW/qFmjqH3Kx2qLL2fyZEExHoGeIkGyekttaNJryYPa1MMHbugcb3Ia A4st70j016JO9jNwyJJ84+N6y26i4k2RT1Js3ESCs2f8wgZ1Tsgjq9VISB/RR3jOlrfV NNnRo2/7oSB40vEVW0BQnLdScOnL9KWfHXKNYSLKbbztfIH2KwpfX2hmuL6ZhI1g04A3 3n7rmgoYuRbyoUMTFllF3PqKGYLtCOJ8uEiI0PSUkKC4BsirJBfUfXhW1qGe5+85O5z8 pK8Do1qUfAv4oxRbX2eapUEQXzJ2Ix1U7+xCddxXNJyWtD8Ly8udTMl3TSYQVXK644uX eljA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=c1EMUe3Q; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dw15-20020a0562140a0f00b006816cfaa6ebsi3901025qvb.371.2024.01.30.05.32.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 05:32:27 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=c1EMUe3Q; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 22F453857C5A for ; Tue, 30 Jan 2024 13:32:21 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 757E6385801A for ; Tue, 30 Jan 2024 13:31:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 757E6385801A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 757E6385801A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706621489; cv=none; b=q+tp/mBXjwCe29m7wTG73rW1Halr9iXbSj4h5AZZXsT5vftaVZfA/VqSGZGlG4ttGg2RzmWh1H8w3ERsHPu6PwZU+TBCejFGNQaVxMFASGoXuBG99oToZwZoKXH2MagEuZbsmOYvdv2MXHlo6902etVQKOKA7Ocash/acKbP+Cg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706621489; c=relaxed/simple; bh=MnjDgPQ3kf6Ml2jUOxWSPLt8H/RnL3lDUto9PVZ4vAw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=MPBXYkA/+/p1RAUrgYaBN4SnHFe47Wuv9vFz00WPxouZm4qko3cwMCKz7IXO/8NEISS2cCi3lEbW/y/kxrZAzCql3GZXbcI6uCPRiKIJm8M5YDa/Ps89agO4bRzmk+6SH6GH8piqdAi+NDEYkKVlopG8ecj8I82f3XVOZusoCYw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1706621487; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=AXj2CnzpTcoXrB2lCVjdR2uBUHQVQwWn57PI/xShUxM=; b=c1EMUe3QTmIbBlOz7Wj9T8MM3N9V1guSW0BQuNdvgiW2OfzN51AaJJPZScKp6FxFHlxNAm k+LlLxRrLNwms3Mw8unL83MkFXqljsm+RmVU+8D0CiJ/XEorYTBvExXDWQxjZOagvXKeA7 yZ9TSHflh+80EnyUxiOQiDf/YTAZcqs= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-213-Gnox3pjDP8ihz9sbr8LDpw-1; Tue, 30 Jan 2024 08:31:25 -0500 X-MC-Unique: Gnox3pjDP8ihz9sbr8LDpw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BAA3E3CBD4E2 for ; Tue, 30 Jan 2024 13:31:24 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.32.139]) by smtp.corp.redhat.com (Postfix) with ESMTP id 91E2D2B7; Tue, 30 Jan 2024 13:31:24 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: fix -Wanalyzer-allocation-size false +ve on Linux kernel's round_up macro [PR113654] Date: Tue, 30 Jan 2024 08:31:23 -0500 Message-Id: <20240130133123.1875809-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789522396013632964 X-GMAIL-MSGID: 1789522396013632964 Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Successful run of analyzer integration tests on x86_64-pc-linux-gnu. Pushed to trunk as r14-8627-g9f382376660069. gcc/analyzer/ChangeLog: PR analyzer/113654 * region-model.cc (is_round_up): New. (is_multiple_p): New. (is_dubious_capacity): New. (region_model::check_region_size): Move usage of size_visitor into is_dubious_capacity. gcc/testsuite/ChangeLog: PR analyzer/113654 * c-c++-common/analyzer/allocation-size-pr113654-1.c: New test. Signed-off-by: David Malcolm --- gcc/analyzer/region-model.cc | 75 ++++++++++++++++++- .../analyzer/allocation-size-pr113654-1.c | 52 +++++++++++++ 2 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/analyzer/allocation-size-pr113654-1.c diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index ba82f46c1887..082972f9d294 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -3349,6 +3349,76 @@ private: svalue_set result_set; /* Used as a mapping of svalue*->bool. */ }; +/* Return true if SIZE_CST is a power of 2, and we have + CAPACITY_SVAL == ((X | (Y - 1) ) + 1), since it is then a multiple + of SIZE_CST, as used by Linux kernel's round_up macro. */ + +static bool +is_round_up (tree size_cst, + const svalue *capacity_sval) +{ + if (!integer_pow2p (size_cst)) + return false; + const binop_svalue *binop_sval = capacity_sval->dyn_cast_binop_svalue (); + if (!binop_sval) + return false; + if (binop_sval->get_op () != PLUS_EXPR) + return false; + tree rhs_cst = binop_sval->get_arg1 ()->maybe_get_constant (); + if (!rhs_cst) + return false; + if (!integer_onep (rhs_cst)) + return false; + + /* We have CAPACITY_SVAL == (LHS + 1) for some LHS expression. */ + + const binop_svalue *lhs_binop_sval + = binop_sval->get_arg0 ()->dyn_cast_binop_svalue (); + if (!lhs_binop_sval) + return false; + if (lhs_binop_sval->get_op () != BIT_IOR_EXPR) + return false; + + tree inner_rhs_cst = lhs_binop_sval->get_arg1 ()->maybe_get_constant (); + if (!inner_rhs_cst) + return false; + + if (wi::to_widest (inner_rhs_cst) + 1 != wi::to_widest (size_cst)) + return false; + return true; +} + +/* Return true if CAPACITY_SVAL is known to be a multiple of SIZE_CST. */ + +static bool +is_multiple_p (tree size_cst, + const svalue *capacity_sval) +{ + if (const svalue *sval = capacity_sval->maybe_undo_cast ()) + return is_multiple_p (size_cst, sval); + + if (is_round_up (size_cst, capacity_sval)) + return true; + + return false; +} + +/* Return true if we should emit a dubious_allocation_size warning + on assigning a region of capacity CAPACITY_SVAL bytes to a pointer + of type with size SIZE_CST, where CM expresses known constraints. */ + +static bool +is_dubious_capacity (tree size_cst, + const svalue *capacity_sval, + constraint_manager *cm) +{ + if (is_multiple_p (size_cst, capacity_sval)) + return false; + size_visitor v (size_cst, capacity_sval, cm); + return v.is_dubious_capacity (); +} + + /* Return true if a struct or union either uses the inheritance pattern, where the first field is a base struct, or the flexible array member pattern, where the last field is an array without a specified size. */ @@ -3456,8 +3526,9 @@ region_model::check_region_size (const region *lhs_reg, const svalue *rhs_sval, { if (!is_struct) { - size_visitor v (pointee_size_tree, capacity, m_constraints); - if (v.is_dubious_capacity ()) + if (is_dubious_capacity (pointee_size_tree, + capacity, + m_constraints)) { tree expr = get_representative_tree (capacity); ctxt->warn (make_unique (lhs_reg, diff --git a/gcc/testsuite/c-c++-common/analyzer/allocation-size-pr113654-1.c b/gcc/testsuite/c-c++-common/analyzer/allocation-size-pr113654-1.c new file mode 100644 index 000000000000..b7bfc5fec72a --- /dev/null +++ b/gcc/testsuite/c-c++-common/analyzer/allocation-size-pr113654-1.c @@ -0,0 +1,52 @@ +/* Adapted from include/linux/math.h */ +#define __round_mask(x, y) ((__typeof__(x))((y)-1)) +#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) + +/* Reduced from Linux kernel's drivers/gpu/drm/i915/display/intel_bios.c */ +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long __kernel_size_t; +typedef __kernel_size_t size_t; + +extern __attribute__((__alloc_size__(1))) __attribute__((__malloc__)) +void* kzalloc(size_t size); + +typedef struct +{ + u32 reg; +} i915_reg_t; +struct intel_uncore; +struct intel_uncore_funcs +{ + u32 (*mmio_readl)(struct intel_uncore* uncore, i915_reg_t r); +}; +struct intel_uncore +{ + void* regs; + struct intel_uncore_funcs funcs; +}; +static inline __attribute__((__gnu_inline__)) __attribute__((__unused__)) +__attribute__((no_instrument_function)) u32 +intel_uncore_read(struct intel_uncore* uncore, i915_reg_t reg) +{ + return uncore->funcs.mmio_readl(uncore, reg); +} +struct drm_i915_private +{ + struct intel_uncore uncore; +}; +struct vbt_header* +spi_oprom_get_vbt(struct drm_i915_private* i915) +{ + u16 vbt_size; + u32* vbt; + vbt_size = + intel_uncore_read(&i915->uncore, ((const i915_reg_t){ .reg = (0x102040) })); + vbt_size &= 0xffff; + vbt = (u32*)kzalloc(round_up (vbt_size, 4)); /* { dg-bogus "allocated buffer size is not a multiple of the pointee's size" "PR analyzer/113654" } */ + if (!vbt) + goto err_not_found; + return (struct vbt_header*)vbt; +err_not_found: + return ((struct vbt_header*)0); +}