Skip to content

Commit de0244a

Browse files
wtarreaupaulmckrcu
authored andcommitted
tools/nolibc: fix incorrect truncation of exit code
Ammar Faizi reported that our exit code handling is wrong. We truncate it to the lowest 8 bits but the syscall itself is expected to take a regular 32-bit signed integer, not an unsigned char. It's the kernel that later truncates it to the lowest 8 bits. The difference is visible in strace, where the program below used to show exit(255) instead of exit(-1): int main(void) { return -1; } This patch applies the fix to all archs. x86_64, i386, arm64, armv7 and mips were all tested and confirmed to work fine now. Risc-v was not tested but the change is trivial and exactly the same as for other archs. Reported-by: Ammar Faizi <ammar.faizi@students.amikom.ac.id> Cc: stable@vger.kernel.org Signed-off-by: Willy Tarreau <w@1wt.eu> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
1 parent ebbe0d8 commit de0244a

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

tools/include/nolibc/nolibc.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ asm(".section .text\n"
414414
"xor %ebp, %ebp\n" // zero the stack frame
415415
"and $-16, %rsp\n" // x86 ABI : esp must be 16-byte aligned before call
416416
"call main\n" // main() returns the status code, we'll exit with it.
417-
"movzb %al, %rdi\n" // retrieve exit code from 8 lower bits
417+
"mov %eax, %edi\n" // retrieve exit code (32 bit)
418418
"mov $60, %rax\n" // NR_exit == 60
419419
"syscall\n" // really exit
420420
"hlt\n" // ensure it does not return
@@ -602,9 +602,9 @@ asm(".section .text\n"
602602
"push %ebx\n" // support both regparm and plain stack modes
603603
"push %eax\n"
604604
"call main\n" // main() returns the status code in %eax
605-
"movzbl %al, %ebx\n" // retrieve exit code from lower 8 bits
606-
"movl $1, %eax\n" // NR_exit == 1
607-
"int $0x80\n" // exit now
605+
"mov %eax, %ebx\n" // retrieve exit code (32-bit int)
606+
"movl $1, %eax\n" // NR_exit == 1
607+
"int $0x80\n" // exit now
608608
"hlt\n" // ensure it does not
609609
"");
610610

@@ -788,7 +788,6 @@ asm(".section .text\n"
788788
"and %r3, %r1, $-8\n" // AAPCS : sp must be 8-byte aligned in the
789789
"mov %sp, %r3\n" // callee, an bl doesn't push (lr=pc)
790790
"bl main\n" // main() returns the status code, we'll exit with it.
791-
"and %r0, %r0, $0xff\n" // limit exit code to 8 bits
792791
"movs r7, $1\n" // NR_exit == 1
793792
"svc $0x00\n"
794793
"");
@@ -985,7 +984,6 @@ asm(".section .text\n"
985984
"add x2, x2, x1\n" // + argv
986985
"and sp, x1, -16\n" // sp must be 16-byte aligned in the callee
987986
"bl main\n" // main() returns the status code, we'll exit with it.
988-
"and x0, x0, 0xff\n" // limit exit code to 8 bits
989987
"mov x8, 93\n" // NR_exit == 93
990988
"svc #0\n"
991989
"");
@@ -1190,7 +1188,7 @@ asm(".section .text\n"
11901188
"addiu $sp,$sp,-16\n" // the callee expects to save a0..a3 there!
11911189
"jal main\n" // main() returns the status code, we'll exit with it.
11921190
"nop\n" // delayed slot
1193-
"and $a0, $v0, 0xff\n" // limit exit code to 8 bits
1191+
"move $a0, $v0\n" // retrieve 32-bit exit code from v0
11941192
"li $v0, 4001\n" // NR_exit == 4001
11951193
"syscall\n"
11961194
".end __start\n"
@@ -1388,7 +1386,6 @@ asm(".section .text\n"
13881386
"add a2,a2,a1\n" // + argv
13891387
"andi sp,a1,-16\n" // sp must be 16-byte aligned
13901388
"call main\n" // main() returns the status code, we'll exit with it.
1391-
"andi a0, a0, 0xff\n" // limit exit code to 8 bits
13921389
"li a7, 93\n" // NR_exit == 93
13931390
"ecall\n"
13941391
"");

0 commit comments

Comments
 (0)