https://gcc.gnu.org/g:adc647aa9e6aaea6799032f76524543b5336af80
commit r17-5880-gadc647aa9e6aaea6799032f76524543b5336af80 Author: Andrew Stubbs <[email protected]> Date: Mon Nov 2 15:59:03 2026 +0000 amdgcn: Fix stack size calculation overflow [PR128366] The stack sizes and/or offsets were all written assuming they wouldn't exceed UINT_MAX, which is not safe. This patch fixes the libgomp plugin that creates the stack space, the kernel prologue that initializes the stack pointers in each thread, and the builtin that Newlib uses to locate the reent structs. gcc/ChangeLog: PR target/128366 * config/gcn/gcn.cc (gcn_expand_prologue): Use 64-bit to calculate wave_offset and fp_hi. (gcn_expand_builtin_1): Use 64-bit to calculate stack_limit_offset_di. libgomp/ChangeLog: PR target/128366 * config/gcn/team.c (gomp_gcn_enter_kernel): Use size_t to find the start of the team_arena. * plugin/plugin-gcn.c (configure_ephemeral_memories): Use size_t for actual_arena_total_size. Diff: --- gcc/config/gcn/gcn.cc | 23 ++++++++++++----------- libgomp/config/gcn/team.c | 2 +- libgomp/plugin/plugin-gcn.c | 4 ++-- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/gcc/config/gcn/gcn.cc b/gcc/config/gcn/gcn.cc index 9628d8f7282b..a8c3e6cb8c54 100644 --- a/gcc/config/gcn/gcn.cc +++ b/gcc/config/gcn/gcn.cc @@ -3559,6 +3559,11 @@ gcn_expand_prologue () set_mem_addr_space (stack_size_mem, ADDR_SPACE_SCALAR_FLAT); emit_move_insn (wave_offset, stack_size_mem); + /* The total stack size may exceed 4 GiB, so the wave offset needs + 64 bits. NUM_WAVES is dead now, so reuse it for the high part. */ + rtx wave_offset_hi = num_waves; + emit_insn (gen_umulsi3_highpart_reg (wave_offset_hi, wave_offset, + thread_id)); emit_insn (gen_mulsi3 (wave_offset, wave_offset, thread_id)); /* The FLAT_SCRATCH_INIT is not usually needed, but can be enabled @@ -3607,7 +3612,7 @@ gcn_expand_prologue () emit_move_insn (fp, stack_addr_mem); rtx scc = gen_rtx_REG (BImode, SCC_REG); emit_insn (gen_addsi3_scalar_carry (fp_lo, fp_lo, wave_offset, scc)); - emit_insn (gen_addcsi3_scalar_zero (fp_hi, fp_hi, scc)); + emit_insn (gen_addcsi3_scalar (fp_hi, fp_hi, wave_offset_hi, scc, scc)); /* Adding RTX_FRAME_RELATED_P effectively disables splitting, so we use split add explicitly, and specify the DImode add in the note. @@ -5030,18 +5035,14 @@ gcn_expand_builtin_1 (tree exp, rtx target, rtx /*subtarget */ , rtx stack_size = gen_reg_rtx (SImode); emit_move_insn (stack_size, stack_size_mem); - rtx wave_offset = gen_reg_rtx (SImode); - emit_insn (gen_mulsi3 (wave_offset, stack_size, thread_id)); - - rtx stack_limit_offset = gen_reg_rtx (SImode); - emit_insn (gen_addsi3 (stack_limit_offset, wave_offset, - stack_size)); + /* The stack limit is at the end of this wave's stack, at + (thread_id + 1) * stack_size, which may exceed 4 GiB. */ + rtx next_thread_id = gen_reg_rtx (SImode); + emit_insn (gen_addsi3 (next_thread_id, thread_id, const1_rtx)); rtx stack_limit_offset_di = gen_reg_rtx (DImode); - emit_move_insn (gen_rtx_SUBREG (SImode, stack_limit_offset_di, 4), - const0_rtx); - emit_move_insn (gen_rtx_SUBREG (SImode, stack_limit_offset_di, 0), - stack_limit_offset); + emit_insn (gen_umulsidi3 (stack_limit_offset_di, stack_size, + next_thread_id)); rtx stack_addr_mem = gen_rtx_MEM (DImode, gen_rtx_PLUS (DImode, diff --git a/libgomp/config/gcn/team.c b/libgomp/config/gcn/team.c index be148c3f8bf6..83cc0f8f73c3 100644 --- a/libgomp/config/gcn/team.c +++ b/libgomp/config/gcn/team.c @@ -80,7 +80,7 @@ gomp_gcn_enter_kernel (void) struct kernargs_abi *kernargs = (struct kernargs_abi*) __builtin_gcn_kernarg_ptr (); void *team_arena = ((void*)kernargs->arena_ptr - + kernargs->arena_size_per_team * teamid); + + (size_t) kernargs->arena_size_per_team * teamid); void * __lds *arena_start = (void * __lds *)TEAM_ARENA_START; void * __lds *arena_free = (void * __lds *)TEAM_ARENA_FREE; void * __lds *arena_end = (void * __lds *)TEAM_ARENA_END; diff --git a/libgomp/plugin/plugin-gcn.c b/libgomp/plugin/plugin-gcn.c index 612803367360..bfce10ef0426 100644 --- a/libgomp/plugin/plugin-gcn.c +++ b/libgomp/plugin/plugin-gcn.c @@ -1991,9 +1991,9 @@ configure_ephemeral_memories (struct kernel_info *kernel, int actual_arena_size = (kernel->kind == KIND_OPENMP ? team_arena_size : 0); - int actual_arena_total_size = actual_arena_size * num_teams; + size_t actual_arena_total_size = (size_t) actual_arena_size * num_teams; size_t size = (actual_arena_total_size - + num_teams * num_threads * stack_size); + + (size_t) num_teams * num_threads * stack_size); for (item = *next_ptr; item; next_ptr = &item->next, item = item->next) {
