[PATCH v6 1/2] tools/nolibc: let sys_brk, sys_mmap and sys_mmap2 return long

Zhangjin Wu
2023-08-11T21:51:07Z

Firstly, since the sys_* functions are internally used by our library
routines, it is ok to let them preserve the 'long' return type of
my_syscall<N> macros, that means not necessary to return pointer like
their library routines do.

Secondly, in order to avoid the size inflating issues introduced by the
sign extension, it is better to let __sysret() only accept integer input
types, to do so, we must let all of the sys_* functions not return
pointers.

There are only three sys_* functions which return pointer, let's make
them return 'long' instead of pointer.

Link: https://lore.kernel.org/lkml/20230809221743.83107-1-falcon@tinylab.org/
Signed-off-by: Zhangjin Wu <falcon@tinylab.org>
---
tools/include/nolibc/arch-s390.h | 4 ++--
tools/include/nolibc/sys.h | 16 ++++++++--------
2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/tools/include/nolibc/arch-s390.h b/tools/include/nolibc/arch-s390.h
index 5d60fd43f883..6396c2a6bc3a 100644
--- a/tools/include/nolibc/arch-s390.h
+++ b/tools/include/nolibc/arch-s390.h
@@ -160,7 +160,7 @@ struct s390_mmap_arg_struct {
};

static __attribute__((unused))
-void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
+long sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
off_t offset)
{
struct s390_mmap_arg_struct args = {
@@ -172,7 +172,7 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
.offset = (unsigned long)offset
};

- return (void *)my_syscall1(__NR_mmap, &args);
+ return my_syscall1(__NR_mmap, &args);
}
#define sys_mmap sys_mmap

diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 833d6c5e86dc..a28e7fbff448 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -74,9 +74,9 @@ long __sysret(unsigned long ret)
*/

static __attribute__((unused))
-void *sys_brk(void *addr)
+long sys_brk(void *addr)
{
- return (void *)my_syscall1(__NR_brk, addr);
+ return my_syscall1(__NR_brk, addr);
}

static __attribute__((unused))
@@ -89,12 +89,12 @@ static __attribute__((unused))
void *sbrk(intptr_t inc)
{
/* first call to find current end */
- void *ret = sys_brk(0);
+ void *ret = (void *)sys_brk(0);

- if (ret && sys_brk(ret + inc) == ret + inc)
+ if (ret && (void *)sys_brk(ret + inc) == ret + inc)
return ret + inc;

- return (void *)__sysret(-ENOMEM);
+ return (void *)__sysret((long)-ENOMEM);
}

@@ -658,7 +658,7 @@ int mknod(const char *path, mode_t mode, dev_t dev)

#ifndef sys_mmap
static __attribute__((unused))
-void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
+long sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
off_t offset)
{
int n;
@@ -670,7 +670,7 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
n = __NR_mmap;
#endif

- return (void *)my_syscall6(n, addr, length, prot, flags, fd, offset);
+ return my_syscall6(n, addr, length, prot, flags, fd, offset);
}
#endif

@@ -682,7 +682,7 @@ void *sys_mmap(void *addr, size_t length, int prot, int flags, int fd,
static __attribute__((unused))
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
- return (void *)__sysret((unsigned long)sys_mmap(addr, length, prot, flags, fd, offset));
+ return (void *)__sysret(sys_mmap(addr, length, prot, flags, fd, offset));
}

static __attribute__((unused))
--
2.25.1