[lwp] 修复信号顺序与 ELF 段保护 (#11895)

修正信号处理顺序和 ELF 段的内存保护设置。
This commit is contained in:
Bernard Xiong authored and GitHub committed 2026-11-05 11:16:02 +08:00
1 parent 2b69e18fc3
commit f14ab20efd
3 files changed
+41 -5

No files matched your search

@@ -109,6 +109,8 @@ arch_ret_to_user_impl:
// TODO: we don't support kernel gdb server in risc-v yet
// so we don't check debug state here and handle debugging bussiness
/* Signal handling takes sleeping locks and must remain schedulable. */
OPEN_INTERRUPT
call lwp_check_exit_request
beqz a0, 1f
mv a0, x0
@@ -121,6 +123,7 @@ arch_ret_to_user_impl:
call lwp_thread_signal_catch
ret_to_user_exit:
CLOSE_INTERRUPT
RESTORE_ALL
// `RESTORE_ALL` also reset sp to user sp, and setup sscratch
sret
@@ -130,8 +133,10 @@ ret_to_user_exit:
* And handle pending signals;
*/
arch_signal_quit:
OPEN_INTERRUPT
LOAD a0, FRAME_OFF_SP(sp)
call arch_signal_ucontext_restore
CLOSE_INTERRUPT
/* reset kernel sp to the stack */
addi sp, sp, CTX_REG_NR * REGBYTES
@@ -167,6 +172,7 @@ arch_thread_signal_enter:
LOAD t0, FRAME_OFF_SP(a2)
mv a3, t0
call arch_signal_ucontext_save
CLOSE_INTERRUPT
/** restore kernel sp */
addi sp, s3, CTX_REG_NR * REGBYTES
+32 -3
View File
@@ -599,6 +599,31 @@ static int elf_zero_bss(struct rt_lwp *lwp, int fd, const Elf_Phdr *phdr, rt_uba
return RT_EOK;
}
/**
* @brief Derive the memory protection flags from an ELF segment's p_flags.
*
* @param p_flags ELF program header flags (PF_R, PF_W, PF_X).
*
* @return Protection flags suitable for elf_map().
*/
static size_t elf_prot_from_pflags(uint32_t p_flags)
{
size_t prot = PROT_NONE;
if (p_flags & PF_R)
{
prot |= PROT_READ;
}
if (p_flags & PF_W)
{
prot |= PROT_WRITE;
}
if (p_flags & PF_X)
{
prot |= PROT_EXEC;
}
return prot;
}
/**
* @brief Map ELF segments into memory.
*
@@ -626,7 +651,6 @@ static int elf_file_mmap(elf_load_info_t *load_info, elf_info_t *elf_info, rt_ub
const Elf_Phdr *tmp_phdr = phdr; /* Current program header */
int fd = elf_info->fd; /* File descriptor for ELF file */
rt_ubase_t load_addr; /* Calculated load address */
size_t prot = PROT_READ | PROT_WRITE; /* Memory protection flags */
size_t flags = MAP_FIXED | MAP_PRIVATE; /* Memory mapping flags */
/* Iterate through all program headers */
@@ -659,8 +683,13 @@ static int elf_file_mmap(elf_load_info_t *load_info, elf_info_t *elf_info, rt_ub
flags &= ~MAP_FIXED;
}
/* Map the segment into memory */
map_va = elf_map(load_info->lwp, tmp_phdr, fd, load_addr, prot, flags, map_size);
/*
* Map the segment with the permissions described by p_flags. When
* LWP_USING_MPROTECT is disabled, lwp_user_mm_attr_to_kernel() ignores
* the protection and always maps the segment as MMU_MAP_U_RWCB, so
* this behaves exactly as before until MPROTECT is enabled.
*/
map_va = elf_map(load_info->lwp, tmp_phdr, fd, load_addr, elf_prot_from_pflags(tmp_phdr->p_flags), flags, map_size);
if (!map_va)
{
LOG_E("%s : elf_map failed", __func__);
+3 -2
View File
@@ -255,9 +255,10 @@ static void sigqueue_enqueue(lwp_sigqueue_t sigqueue, lwp_siginfo_t siginfo)
rt_bool_t inserted = RT_FALSE;
rt_list_for_each_entry(idx, &sigqueue->siginfo_list, node)
{
if (idx->ksiginfo.signo >= siginfo->ksiginfo.signo)
/* Keep ascending signal order and FIFO order for equal signals. */
if (idx->ksiginfo.signo > siginfo->ksiginfo.signo)
{
rt_list_insert_after(&idx->node, &siginfo->node);
rt_list_insert_before(&idx->node, &siginfo->node);
inserted = RT_TRUE;
break;
}