From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1687165558; x=1718701558; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Xqf8ma20wEYdzJZil0TcYvsTo0GrhomQRmYQiRfZ8oo=; b=JMQ+p4Pz8rsf/VF77R4TM1FgMztkTnTGvpqaZGj32/6oHDtbPg1DaBvI +XckoLi8dswU64r4gD3wrepq+JTt3FQd7HeH4EDSOASf4dfV6a6fqX8cj +ILHGgyvlxYCvabMRnlMya9p0GEyVG3HAr1aQL8NQa9aB13sh1T/Q8Gg5 n/GdP/ungvam2oG9twn8i9uH6B4GeRMsEJMRUa/gXT9dtnxyi68GYypDc yXAxqnFz7cDHkaRBOIriSTjh8lng3AmxSLfWHWrk0FWW74irYAuYfdh2T v77hKmhzSj3GfKtpPP0/913aKhDBcG6z2ayegKtlyndvFOUUVG7pPT3Q5 w==; 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) From: pan2.li@intel.com 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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 */ -- 2.34.1