xtensa: Prevent out-of-bounds access in xt_true_regnum
Fixes a bug in Xtensa's register renumbering to prevent crashes during local register allocation.
A bug in the Xtensa backend’s xt_true_regnum function has been fixed to prevent out-of-bounds reads from the reg_renumber array. This function is used during instruction emission for moves. The fix ensures that reg_renumber is only accessed after the Local Register Allocation (LRA) pass has resized it to accommodate new pseudos, or after reload has completed, thus avoiding crashes.
In Details
The xt_true_regnum function in config/xtensa/xtensa.cc is invoked by move expanders and instruction recognizers, often via emit_move_insn and recog_memoized, during the instruction scheduling and register allocation phases. Previously, it could be called before reg_renumber was properly sized by LRA, leading to out-of-bounds access. This commit restricts calls to xt_true_regnum to only occur when LRA is in progress or has finished, or when reload is complete, safeguarding against invalid memory accesses.
- LRA
- Local Register Allocation, a compiler optimization pass that assigns physical registers to virtual registers within a basic block or function.
- reg_renumber
- An internal data structure used by the Xtensa backend to map virtual or pseudo registers to physical registers during register allocation.
- move expanders
- Compiler components responsible for generating machine instructions to move data between registers or memory locations.
- insn conditions
- Predicates or rules used by the compiler to determine when a particular machine instruction can be used.