On Mon, 19 Jun 2023, Li, Pan2 wrote: > Add Richard Biener for reviewing, sorry for inconvenient. > > Pan > > -----Original Message----- > From: Li, Pan2 <pan2...@intel.com> > Sent: Monday, June 19, 2023 4:07 PM > To: gcc-patches@gcc.gnu.org > Cc: juzhe.zh...@rivai.ai; rdapp....@gmail.com; jeffreya...@gmail.com; Li, > Pan2 <pan2...@intel.com>; Wang, Yanzhang <yanzhang.w...@intel.com>; > kito.ch...@gmail.com > Subject: [PATCH v1] RISC-V: Fix out of range memory access when lto mode init > > From: Pan Li <pan2...@intel.com> > > 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.
You also have to fix bp_pack_machine_mode/bp_unpack_machine_mode which streams exactly values in [0, 1<<8 - 1]. CCing Jakub who invented this code. Richard. > Signed-off-by: Pan Li <pan2...@intel.com> > > gcc/ChangeLog: > > * tree-streamer.cc (streamer_mode_table): Use MAX_MACHINE_MODE > as array size. > * tree-streamer.h (streamer_mode_table): Ditto. > --- > gcc/tree-streamer.cc | 2 +- > gcc/tree-streamer.h | 2 +- > 2 files changed, 2 insertions(+), 2 deletions(-) > > 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..51a292c8d80 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 *); > -- Richard Biener <rguent...@suse.de> SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)