From 89d6abf3dfcf0bec86ed10ec9494995b6f3b41b4 Mon Sep 17 00:00:00 2001 From: Gao Jiawei Date: Tue, 23 Jul 2024 18:05:00 +0800 Subject: [PATCH] setjmp: fix setjmp returns 0 when calling longjmp with 0 as the second argument Signed-off-by: Gao Jiawei --- libs/libc/machine/arm/gnu/arch_setjmp.S | 16 +++++++++++++++- libs/libc/machine/sim/arch_setjmp_x86.S | 6 ++++-- libs/libc/machine/sim/arch_setjmp_x86_64.S | 5 ++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/libs/libc/machine/arm/gnu/arch_setjmp.S b/libs/libc/machine/arm/gnu/arch_setjmp.S index c4a26fcb935..1183f230d1d 100644 --- a/libs/libc/machine/arm/gnu/arch_setjmp.S +++ b/libs/libc/machine/arm/gnu/arch_setjmp.S @@ -170,7 +170,21 @@ longjmp: vmsr fpscr, r2 /* Restore the FPSCR */ #endif /* CONFIG_ARCH_FPU */ - mov r0, r1 /* return val */ + /* Check and substitute the given return value to 1 if it's 0 */ + + movs r0, r1 +#ifdef CONFIG_ARCH_ARMV6M + /* ARMv6-M only supports branching with condition + * So we fall back to not use IT blocks in that case + */ + + bne 1f + movs r0, #1 +1: +#else + it eq + moveq r0, #1 +#endif bx lr .size longjmp, .-longjmp diff --git a/libs/libc/machine/sim/arch_setjmp_x86.S b/libs/libc/machine/sim/arch_setjmp_x86.S index 439e21331a0..ea31516ffbf 100644 --- a/libs/libc/machine/sim/arch_setjmp_x86.S +++ b/libs/libc/machine/sim/arch_setjmp_x86.S @@ -85,9 +85,11 @@ SYMBOL(setjmp): SYMBOL(longjmp): movl 4(%esp), %ecx /* jmpbuf in %ecx. */ movl 8(%esp), %eax /* Second argument is return value. */ - + testl %eax, %eax + jnz 1f + incl %eax /* Save the return address now. */ - +1: movl (JB_PC)(%ecx), %edx /* Restore registers. */ diff --git a/libs/libc/machine/sim/arch_setjmp_x86_64.S b/libs/libc/machine/sim/arch_setjmp_x86_64.S index 62c15ff3552..896851e10fd 100644 --- a/libs/libc/machine/sim/arch_setjmp_x86_64.S +++ b/libs/libc/machine/sim/arch_setjmp_x86_64.S @@ -130,9 +130,12 @@ SYMBOL(longjmp): /* Setup return value */ movl %esi,%eax + testl %eax,%eax + jnz 1f + incl %eax /* Restore registers */ - +1: movq JB_RBX(REGS),%rbx /* Load 1: rbx */ movq JB_RSP(REGS),%rsp /* Load 2: rsp */ movq JB_RBP(REGS),%rbp /* Load 3: rdi */