Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tools/nolibc: keep brk(), sbrk(), mmap() away from __sysret()

The __sysret() function causes some undesirable casts so we'll revert
it. In order to keep it simple it will now only support integer return
values like in the past, so we must basically revert the changes that
were made to these 3 syscalls which return a pointer so that they
simply rely on their own test and the SET_ERRNO() macro.

Fixes: 4201cfce15fe ("tools/nolibc: clean up sbrk() routine")
Fixes: 924e9539aeaa ("tools/nolibc: clean up mmap() routine")
Fixes: d27447bc2e0a ("tools/nolibc: sys.h: apply __sysret() helper")
Link: https://lore.kernel.org/lkml/20230806095846.GB10627@1wt.eu/
Link: https://lore.kernel.org/lkml/ZNKOJY+g66nkIyvv@1wt.eu/
Cc: Zhangjin Wu <falcon@tinylab.org>
Cc: David Laight <David.Laight@ACULAB.COM>
Cc: Thomas Weißschuh <thomas@t-8ch.de>
Signed-off-by: Willy Tarreau <w@1wt.eu>

+16 -3
+16 -3
tools/include/nolibc/sys.h
··· 82 82 static __attribute__((unused)) 83 83 int brk(void *addr) 84 84 { 85 - return __sysret(sys_brk(addr) ? 0 : -ENOMEM); 85 + void *ret = sys_brk(addr); 86 + 87 + if (!ret) { 88 + SET_ERRNO(ENOMEM); 89 + return -1; 90 + } 91 + return 0; 86 92 } 87 93 88 94 static __attribute__((unused)) ··· 100 94 if (ret && sys_brk(ret + inc) == ret + inc) 101 95 return ret + inc; 102 96 103 - return (void *)__sysret(-ENOMEM); 97 + SET_ERRNO(ENOMEM); 98 + return (void *)-1; 104 99 } 105 100 106 101 ··· 689 682 static __attribute__((unused)) 690 683 void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) 691 684 { 692 - return (void *)__sysret((unsigned long)sys_mmap(addr, length, prot, flags, fd, offset)); 685 + void *ret = sys_mmap(addr, length, prot, flags, fd, offset); 686 + 687 + if ((unsigned long)ret >= -4095UL) { 688 + SET_ERRNO(-(long)ret); 689 + ret = MAP_FAILED; 690 + } 691 + return ret; 693 692 } 694 693 695 694 static __attribute__((unused))