From patchwork Mon Jun 19 09:05:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 109831 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp2863175vqr; Mon, 19 Jun 2023 02:06:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ53HXSBlNYnuKjzBuwln5r1Mye8sWKTHZQfD/W4xsOoiK4NkVE3Rxl2+NhTXwAgkXsW5WJn X-Received: by 2002:a2e:850c:0:b0:2b4:5cad:f23f with SMTP id j12-20020a2e850c000000b002b45cadf23fmr4707113lji.2.1687165606023; Mon, 19 Jun 2023 02:06:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687165605; cv=none; d=google.com; s=arc-20160816; b=mpjniwUtGYvpqqqR5asGq/GFd1Y1PL/43pjJiewxoc0z7p4mcgy99mzUygEop+9Ygx K3fZLU1SWmGJLZjkO79OYvxS43rUjKX2pCUclu8wgy4tcjQNOvpO/MUrG6KV+QFUt+H9 B2+GkmZ0V46hRHbp2HZoDVkr9KT4wMbQKY1+mg9hJQq4kbyWiZoJNfoBOy95tKDYTkvt 3TB4oZCpsEdauIG4I09Kk6odUQUSiPy0Tt9dCXbwkvSZE2RBUKgDa2McpgNaelCmdhRt dhHrMeT+74nrpYShfNagoy0HZgKC8bM108pV5yHRRBHJFbcF2KZhyXwi/NMNmBYrpMAn opKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from: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:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=RS2zpQhxMoCa54hbHxiJFqeZ0RsLXwB1aGBb/WTbLUE=; b=AZYX1uf37qDT1uJJUo3hW/+pQlp9DVD/qsKVQulJBK/y4jXx1RQhoMMb6+wU2R/Hph HwNroL9roS4jiPKGaHmUfFut4u5CvAXTLuuzr9DPdLOgI8kFQzkYP8RY+AVjkgBpcxHw jiFf7R6RoIRAs0asL4dLDPs8AdM8eklNC0oCKutjnODGrASrtYElm9nqAsKB9n3dkLYR 98Fk/bvbonLAaoVX99+SImO1Pl3GB7YvI0CGF7Hw6FI52UulStA75K4qb7yT+th0mBYW cZGeJCybMI/fqK06bgLfdfQ380B7ruonwpAHE+6QMoFVAsuYpRncMQDYkFxBRVH55lA9 GLOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Wmdods80; 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=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id i5-20020a1709061cc500b00987a410ed03si2303587ejh.82.2023.06.19.02.06.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Jun 2023 02:06:45 -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=@gcc.gnu.org header.s=default header.b=Wmdods80; 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=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 82D8A3858C66 for ; Mon, 19 Jun 2023 09:06:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 82D8A3858C66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1687165604; bh=RS2zpQhxMoCa54hbHxiJFqeZ0RsLXwB1aGBb/WTbLUE=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=Wmdods80d9/8E0HW0s7hm2KDWI/EaQX5P5RSTN93bDokpc14huM6at4I2lTP9X0uX Sbl4W8kMrrF7ZzRqxd1c1rjq6qLRguCj+vKCYr1tDBUxcsh2R3HsJDHJnwJvy185O8 NzJA8C10u9g9EHKAnQ18ONiy1dXawr6Mj8mgcJHE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by sourceware.org (Postfix) with ESMTPS id 47EDB3858415 for ; Mon, 19 Jun 2023 09:05:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 47EDB3858415 X-IronPort-AV: E=McAfee;i="6600,9927,10745"; a="362118215" X-IronPort-AV: E=Sophos;i="6.00,254,1681196400"; d="scan'208";a="362118215" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jun 2023 02:05:57 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10745"; a="1043828964" X-IronPort-AV: E=Sophos;i="6.00,254,1681196400"; d="scan'208";a="1043828964" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by fmsmga005.fm.intel.com with ESMTP; 19 Jun 2023 02:05:50 -0700 Received: from pli-ubuntu.sh.intel.com (pli-ubuntu.sh.intel.com [10.239.159.47]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 8B7D01005698; Mon, 19 Jun 2023 17:05:49 +0800 (CST) To: gcc-patches@gcc.gnu.org Cc: juzhe.zhong@rivai.ai, rdapp.gcc@gmail.com, jeffreyalaw@gmail.com, pan2.li@intel.com, yanzhang.wang@intel.com, kito.cheng@gmail.com, rguenther@suse.de, jakub@redhat.com Subject: [PATCH] RISC-V: Fix out of range memory access of machine mode table Date: Mon, 19 Jun 2023 17:05:48 +0800 Message-Id: <20230619090548.1574008-1-pan2.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230619080710.1536456-1-pan2.li@intel.com> References: <20230619080710.1536456-1-pan2.li@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Pan Li via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: pan2.li@intel.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1769121362733071640?= X-GMAIL-MSGID: =?utf-8?q?1769121362733071640?= From: Pan Li We extend the machine mode from 8 to 16 bits already. But there still one placing missing from the tree-streamer. It has one hard coded array for the machine code like size 256. In the lto pass, we memset the array by MAX_MACHINE_MODE count but the value of the MAX_MACHINE_MODE will grow as more and more modes are added. While the machine mode array in tree-streamer still leave 256 as is. Then, when the MAX_MACHINE_MODE is greater than 256, the memset of lto_output_init_mode_table will touch the memory out of range unexpected. This patch would like to take the MAX_MACHINE_MODE as the size of the array in tree-streamer, to make sure there is no potential unexpected memory access in future. Signed-off-by: Pan Li gcc/ChangeLog: * lto-streamer-in.cc (lto_input_mode_table): Use MAX_MACHINE_MODE for memory allocation. * tree-streamer.cc: Use MAX_MACHINE_MODE for array size. * tree-streamer.h (streamer_mode_table): Ditto. (bp_pack_machine_mode): Ditto. (bp_unpack_machine_mode): Ditto. --- gcc/lto-streamer-in.cc | 3 ++- gcc/tree-streamer.cc | 2 +- gcc/tree-streamer.h | 7 ++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/gcc/lto-streamer-in.cc b/gcc/lto-streamer-in.cc index 2cb83406db5..102b7e18526 100644 --- a/gcc/lto-streamer-in.cc +++ b/gcc/lto-streamer-in.cc @@ -1985,7 +1985,8 @@ lto_input_mode_table (struct lto_file_decl_data *file_data) internal_error ("cannot read LTO mode table from %s", file_data->file_name); - unsigned char *table = ggc_cleared_vec_alloc (1 << 8); + unsigned char *table = ggc_cleared_vec_alloc ( + MAX_MACHINE_MODE); file_data->mode_table = table; const struct lto_simple_header_with_strings *header = (const struct lto_simple_header_with_strings *) data; diff --git a/gcc/tree-streamer.cc b/gcc/tree-streamer.cc index ed65a7692e3..a28ef9c7920 100644 --- a/gcc/tree-streamer.cc +++ b/gcc/tree-streamer.cc @@ -35,7 +35,7 @@ along with GCC; see the file COPYING3. If not see During streaming in, we translate the on the disk mode using this table. For normal LTO it is set to identity, for ACCEL_COMPILER depending on the mode_table content. */ -unsigned char streamer_mode_table[1 << 8]; +unsigned char streamer_mode_table[MAX_MACHINE_MODE]; /* Check that all the TS_* structures handled by the streamer_write_* and streamer_read_* routines are exactly ALL the structures defined in diff --git a/gcc/tree-streamer.h b/gcc/tree-streamer.h index 170d61cf20b..be3a1938e76 100644 --- a/gcc/tree-streamer.h +++ b/gcc/tree-streamer.h @@ -75,7 +75,7 @@ void streamer_write_tree_body (struct output_block *, tree); void streamer_write_integer_cst (struct output_block *, tree); /* In tree-streamer.cc. */ -extern unsigned char streamer_mode_table[1 << 8]; +extern unsigned char streamer_mode_table[MAX_MACHINE_MODE]; void streamer_check_handled_ts_structures (void); bool streamer_tree_cache_insert (struct streamer_tree_cache_d *, tree, hashval_t, unsigned *); @@ -108,7 +108,7 @@ inline void bp_pack_machine_mode (struct bitpack_d *bp, machine_mode mode) { streamer_mode_table[mode] = 1; - bp_pack_enum (bp, machine_mode, 1 << 8, mode); + bp_pack_enum (bp, machine_mode, MAX_MACHINE_MODE, mode); } inline machine_mode @@ -116,7 +116,8 @@ bp_unpack_machine_mode (struct bitpack_d *bp) { return (machine_mode) ((class lto_input_block *) - bp->stream)->mode_table[bp_unpack_enum (bp, machine_mode, 1 << 8)]; + bp->stream)->mode_table[bp_unpack_enum (bp, machine_mode, + MAX_MACHINE_MODE)]; } #endif /* GCC_TREE_STREAMER_H */