From patchwork Tue Oct 31 21:16:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Malcolm X-Patchwork-Id: 160351 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:abcd:0:b0:403:3b70:6f57 with SMTP id f13csp27611vqx; Tue, 31 Oct 2023 14:17:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEvAXanc0EwioIKNzFgqcpLItNswiN+sNBtOtYt1mBTfNnJTQRak3KhkPNz2tYrgiJmTFbj X-Received: by 2002:a05:620a:408a:b0:772:4be1:b41e with SMTP id f10-20020a05620a408a00b007724be1b41emr14565157qko.10.1698787042692; Tue, 31 Oct 2023 14:17:22 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698787042; cv=pass; d=google.com; s=arc-20160816; b=EmugPNZyzU8BLc+K+IA63LmWYFue8PbIBKLF3JQ0XU9gyi4i2mS+ht6bu7ajnEDn6q XDXZ33wKi37iH+6iU8wb2IitZtC/ZGJXuWRwGm7C2vFlSaJqLmcKj1VJ0/CTdxzFuagC VmuTOEs8cjqkzwfz/O+IAMpSy+Pn6mfCJdy02SHpipOuYgTk04zgN+fMuOz1/Nso3/XO Qt69HH7Tw9aSO0zXL7rgoLqSjMrrG7YBL7HH6SveAZYXT/ee5V1oYkIePtv8RI4kU3/w yiRMiaL+Jh1vuf6nVWbgiUu4fQMXLvCqCcIVNqBWNs2T0W3e2qETCPS7TNBcOUEiDi8M it0Q== 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=vKAR16Nyb8/MyK+JKgtRVXY7PQfmHyUWDuKIjCdB2Rg=; fh=NXemEfxTRbZtBxUkxR2ehQUaYlcDfMdzPkO8MChVQE4=; b=Bgv19vgeOglL553Ji+9xEAUjh8whmiBbncSQqHWE9/SuvFTI1YvxmSTOiCGr2q1TBg onJtBxNXuyZ8RLBqO9Sz7144h87gQlMGUOyaI5dYZrALlL323Tc41X6qcyfR2Mta5pId ov5QQFFPGMty4uCYaD0l8qzIyfCDnRj8FS/84e1ZNtTJCBj1A1/i/YbIeIHTNoJmyXPh I0zdfKhDei54ecA8kwS2ztxW3PVhNSWrLek6wU0xkh44OfqUqBxP23e17feCcJPKvXUW c9Obzjcfexzyc4f2RuUqa4NaTB/ottDTJsNHWUcnf6yh0mLRbU9xmIGWzlR32hZHbMrR oDIA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UvGl7PLk; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id pb27-20020a05620a839b00b0076da127884csi1902274qkn.223.2023.10.31.14.17.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Oct 2023 14:17:22 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UvGl7PLk; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 7426D3857806 for ; Tue, 31 Oct 2023 21:17:22 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id 78D783858031 for ; Tue, 31 Oct 2023 21:16:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 78D783858031 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 78D783858031 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698786980; cv=none; b=aXmX+a6YIcqjdbpEF6UNJsA8WGhowcgJfoD6KcCxTplYXOz8F/RNrd4O0fynancCV4rk4NmNFipoibc3Gct3UY0L8qADiRnfnuYT2N5QC0U5+AXqLXcxbTwhTs9D96bhJYa713u34gJtnEa15Zww4JlKAToUBjU5IEd5SgGR6Xs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698786980; c=relaxed/simple; bh=cWsssCZ2pNPRelt70IvCNKzYQSE0qr5N3XMgjL5Y4Vc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=ujFSxze9C2yMiv9bRMMJa54s6ihftsWxj2ZNCqL9Xe3mzzO1oefycyJbk7KhMShx2pWnPBjWaNQJBZbo94xwFi3oBgeiatjJ+t0ifqgLzlUZM6Y5DRWQxmO1QZyXRMONZPzUIGnH1J2K1Kh2oU9/f/asEkC6jBtZ8QNDfKkBbqw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698786977; 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=vKAR16Nyb8/MyK+JKgtRVXY7PQfmHyUWDuKIjCdB2Rg=; b=UvGl7PLk21949bUGDsv4uGDtcUlJn5HZ98yBoh6ebI/ACrEGQyy7eAbVI7PkHaNEcIOmmX YmK13hwRysSNswB9jgXBeeTvVkYz/oXKk3qYJHxly3MuRlUw/zGY6VveD/BHf3BoDHt1iY Y82SUqxnzz6iEiwXyxVNOXPtliPNeJQ= 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-56-PodKYLxAPhu9Et-6YhWSfA-1; Tue, 31 Oct 2023 17:16:05 -0400 X-MC-Unique: PodKYLxAPhu9Et-6YhWSfA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 6963E38117EB for ; Tue, 31 Oct 2023 21:16:05 +0000 (UTC) Received: from t14s.localdomain.com (unknown [10.22.8.62]) by smtp.corp.redhat.com (Postfix) with ESMTP id 364682026D4C; Tue, 31 Oct 2023 21:16:05 +0000 (UTC) From: David Malcolm To: gcc-patches@gcc.gnu.org Cc: David Malcolm Subject: [pushed] analyzer: move class record_layout to its own .h/.cc Date: Tue, 31 Oct 2023 17:16:04 -0400 Message-Id: <20231031211604.1907952-1-dmalcolm@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, 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: 1781307321749396543 X-GMAIL-MSGID: 1781307321749396543 No functional change intended. Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu. Pushed to trunk as r14-5050-g37e1634ef1a6f1. gcc/ChangeLog: * Makefile.in (ANALYZER_OBJS): Add analyzer/record-layout.o. gcc/analyzer/ChangeLog: * record-layout.cc: New file, based on material in region-model.cc. * record-layout.h: Likewise. * region-model.cc: Include "analyzer/record-layout.h". (class record_layout): Move to record-layout.cc and .h --- gcc/Makefile.in | 1 + gcc/analyzer/record-layout.cc | 125 ++++++++++++++++++++++++++++++++ gcc/analyzer/record-layout.h | 91 +++++++++++++++++++++++ gcc/analyzer/region-model.cc | 132 +--------------------------------- 4 files changed, 218 insertions(+), 131 deletions(-) create mode 100644 gcc/analyzer/record-layout.cc create mode 100644 gcc/analyzer/record-layout.h diff --git a/gcc/Makefile.in b/gcc/Makefile.in index 91d6bfbea4d..41ed8163cd8 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1324,6 +1324,7 @@ ANALYZER_OBJS = \ analyzer/program-point.o \ analyzer/program-state.o \ analyzer/ranges.o \ + analyzer/record-layout.o \ analyzer/region.o \ analyzer/region-model.o \ analyzer/region-model-asm.o \ diff --git a/gcc/analyzer/record-layout.cc b/gcc/analyzer/record-layout.cc new file mode 100644 index 00000000000..1369bfb5eff --- /dev/null +++ b/gcc/analyzer/record-layout.cc @@ -0,0 +1,125 @@ +/* Implementation of class record_layout. + Copyright (C) 2022-2023 Free Software Foundation, Inc. + Contributed by David Malcolm . + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#include "config.h" +#define INCLUDE_MEMORY +#include "system.h" +#include "coretypes.h" +#include "tree.h" +#include "function.h" +#include "basic-block.h" +#include "gimple.h" +#include "diagnostic.h" +#include "tree-diagnostic.h" +#include "analyzer/analyzer.h" +#include "analyzer/record-layout.h" + +#if ENABLE_ANALYZER + +namespace ana { + +/* class record_layout. */ + +record_layout::record_layout (tree record_type) +{ + gcc_assert (TREE_CODE (record_type) == RECORD_TYPE); + + for (tree iter = TYPE_FIELDS (record_type); iter != NULL_TREE; + iter = DECL_CHAIN (iter)) + { + if (TREE_CODE (iter) == FIELD_DECL) + { + int iter_field_offset = int_bit_position (iter); + bit_size_t size_in_bits; + if (!int_size_in_bits (TREE_TYPE (iter), &size_in_bits)) + size_in_bits = 0; + + maybe_pad_to (iter_field_offset); + + /* Add field. */ + m_items.safe_push (item (bit_range (iter_field_offset, + size_in_bits), + iter, false)); + } + } + + /* Add any trailing padding. */ + bit_size_t size_in_bits; + if (int_size_in_bits (record_type, &size_in_bits)) + maybe_pad_to (size_in_bits); +} + +void +record_layout::dump_to_pp (pretty_printer *pp) const +{ + unsigned i; + item *it; + FOR_EACH_VEC_ELT (m_items, i, it) + { + it->dump_to_pp (pp); + pp_newline (pp); + } +} + +void +record_layout::dump () const +{ + pretty_printer pp; + pp_format_decoder (&pp) = default_tree_printer; + pp.buffer->stream = stderr; + dump_to_pp (&pp); + pp_flush (&pp); +} + +const record_layout::item * +record_layout::get_item_at (bit_offset_t offset) const +{ + unsigned i; + item *it; + FOR_EACH_VEC_ELT (m_items, i, it) + if (it->contains_p (offset)) + return it; + return NULL; +} + +/* Subroutine of ctor. Add padding item to NEXT_OFFSET if necessary. */ + +void +record_layout::maybe_pad_to (bit_offset_t next_offset) +{ + if (m_items.length () > 0) + { + const item &last_item = m_items[m_items.length () - 1]; + bit_offset_t offset_after_last_item + = last_item.get_next_bit_offset (); + if (next_offset > offset_after_last_item) + { + bit_size_t padding_size + = next_offset - offset_after_last_item; + m_items.safe_push (item (bit_range (offset_after_last_item, + padding_size), + last_item.m_field, true)); + } + } +} + +} // namespace ana + +#endif /* #if ENABLE_ANALYZER */ diff --git a/gcc/analyzer/record-layout.h b/gcc/analyzer/record-layout.h new file mode 100644 index 00000000000..b63e7b00e48 --- /dev/null +++ b/gcc/analyzer/record-layout.h @@ -0,0 +1,91 @@ +/* Declaration of class record_layout. + Copyright (C) 2022-2023 Free Software Foundation, Inc. + Contributed by David Malcolm . + +This file is part of GCC. + +GCC is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3, or (at your option) +any later version. + +GCC is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GCC; see the file COPYING3. If not see +. */ + +#ifndef GCC_ANALYZER_RECORD_LAYOUT_H +#define GCC_ANALYZER_RECORD_LAYOUT_H + +#include "analyzer/store.h" + +namespace ana { + +/* Information of the layout of a RECORD_TYPE, capturing it as a vector + of items, where each item is either a field or padding. */ + +class record_layout +{ +public: + /* An item within a record; either a field, or padding after a field. */ + struct item + { + public: + item (const bit_range &br, + tree field, + bool is_padding) + : m_bit_range (br), + m_field (field), + m_is_padding (is_padding) + { + } + + bit_offset_t get_start_bit_offset () const + { + return m_bit_range.get_start_bit_offset (); + } + bit_offset_t get_next_bit_offset () const + { + return m_bit_range.get_next_bit_offset (); + } + + bool contains_p (bit_offset_t offset) const + { + return m_bit_range.contains_p (offset); + } + + void dump_to_pp (pretty_printer *pp) const + { + if (m_is_padding) + pp_printf (pp, "padding after %qD", m_field); + else + pp_printf (pp, "%qD", m_field); + pp_string (pp, ", "); + m_bit_range.dump_to_pp (pp); + } + + bit_range m_bit_range; + tree m_field; + bool m_is_padding; + }; + + record_layout (tree record_type); + + void dump_to_pp (pretty_printer *pp) const; + DEBUG_FUNCTION void dump () const; + + const record_layout::item *get_item_at (bit_offset_t offset) const; + +private: + void maybe_pad_to (bit_offset_t next_offset); + + auto_vec m_items; +}; + +} // namespace ana + +#endif /* GCC_ANALYZER_RECORD_LAYOUT_H */ diff --git a/gcc/analyzer/region-model.cc b/gcc/analyzer/region-model.cc index 067347ef845..9479bcf380c 100644 --- a/gcc/analyzer/region-model.cc +++ b/gcc/analyzer/region-model.cc @@ -78,6 +78,7 @@ along with GCC; see the file COPYING3. If not see #include "analyzer/checker-event.h" #include "analyzer/checker-path.h" #include "analyzer/feasible-graph.h" +#include "analyzer/record-layout.h" #if ENABLE_ANALYZER @@ -6123,137 +6124,6 @@ region_model::unset_dynamic_extents (const region *reg) m_dynamic_extents.remove (reg); } -/* Information of the layout of a RECORD_TYPE, capturing it as a vector - of items, where each item is either a field or padding. */ - -class record_layout -{ -public: - /* An item within a record; either a field, or padding after a field. */ - struct item - { - public: - item (const bit_range &br, - tree field, - bool is_padding) - : m_bit_range (br), - m_field (field), - m_is_padding (is_padding) - { - } - - bit_offset_t get_start_bit_offset () const - { - return m_bit_range.get_start_bit_offset (); - } - bit_offset_t get_next_bit_offset () const - { - return m_bit_range.get_next_bit_offset (); - } - - bool contains_p (bit_offset_t offset) const - { - return m_bit_range.contains_p (offset); - } - - void dump_to_pp (pretty_printer *pp) const - { - if (m_is_padding) - pp_printf (pp, "padding after %qD", m_field); - else - pp_printf (pp, "%qD", m_field); - pp_string (pp, ", "); - m_bit_range.dump_to_pp (pp); - } - - bit_range m_bit_range; - tree m_field; - bool m_is_padding; - }; - - record_layout (tree record_type) - { - gcc_assert (TREE_CODE (record_type) == RECORD_TYPE); - - for (tree iter = TYPE_FIELDS (record_type); iter != NULL_TREE; - iter = DECL_CHAIN (iter)) - { - if (TREE_CODE (iter) == FIELD_DECL) - { - int iter_field_offset = int_bit_position (iter); - bit_size_t size_in_bits; - if (!int_size_in_bits (TREE_TYPE (iter), &size_in_bits)) - size_in_bits = 0; - - maybe_pad_to (iter_field_offset); - - /* Add field. */ - m_items.safe_push (item (bit_range (iter_field_offset, - size_in_bits), - iter, false)); - } - } - - /* Add any trailing padding. */ - bit_size_t size_in_bits; - if (int_size_in_bits (record_type, &size_in_bits)) - maybe_pad_to (size_in_bits); - } - - void dump_to_pp (pretty_printer *pp) const - { - unsigned i; - item *it; - FOR_EACH_VEC_ELT (m_items, i, it) - { - it->dump_to_pp (pp); - pp_newline (pp); - } - } - - DEBUG_FUNCTION void dump () const - { - pretty_printer pp; - pp_format_decoder (&pp) = default_tree_printer; - pp.buffer->stream = stderr; - dump_to_pp (&pp); - pp_flush (&pp); - } - - const record_layout::item *get_item_at (bit_offset_t offset) const - { - unsigned i; - item *it; - FOR_EACH_VEC_ELT (m_items, i, it) - if (it->contains_p (offset)) - return it; - return NULL; - } - -private: - /* Subroutine of ctor. Add padding item to NEXT_OFFSET if necessary. */ - - void maybe_pad_to (bit_offset_t next_offset) - { - if (m_items.length () > 0) - { - const item &last_item = m_items[m_items.length () - 1]; - bit_offset_t offset_after_last_item - = last_item.get_next_bit_offset (); - if (next_offset > offset_after_last_item) - { - bit_size_t padding_size - = next_offset - offset_after_last_item; - m_items.safe_push (item (bit_range (offset_after_last_item, - padding_size), - last_item.m_field, true)); - } - } - } - - auto_vec m_items; -}; - /* A subclass of pending_diagnostic for complaining about uninitialized data being copied across a trust boundary to an untrusted output (e.g. copy_to_user infoleaks in the Linux kernel). */