Skip to contentSkip to conversation

linux-user: Add close_range() syscall

PATCH Superseded Helge Deller 2 participants 5 messages Oct 24, 2022 – Oct 25, 2022

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 linux-user: Add close_range() syscall

---
Changes:
v4: Fix check for arg2
v3: fd_trans_unregister() only called if close_range() doesn't fail
v2: consider CLOSE_RANGE_CLOEXEC flag

linux-user/strace.list +3 -0
index 3df2184580..cd995e5d56 100644
@@ -103,6 +103,9 @@
 #ifdef TARGET_NR_close
 { TARGET_NR_close, "close" , "%s(%d)", NULL, NULL },
 #endif
+#ifdef TARGET_NR_close_range
+{ TARGET_NR_close_range, "close_range" , "%s(%u,%u,%u)", NULL, NULL },
+#endif
 #ifdef TARGET_NR_connect
 { TARGET_NR_connect, "connect" , "%s(%d,%#x,%d)", NULL, NULL },
 #endif
linux-user/syscall.c +25 -0
index 12195d4e99..984039f928 100644
@@ -339,6 +339,13 @@ _syscall3(int,sys_syslog,int,type,char*,bufp,int,len)
 #ifdef __NR_exit_group
 _syscall1(int,exit_group,int,error_code)
 #endif
+#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
+#define __NR_sys_close_range __NR_close_range
+_syscall3(int,sys_close_range,int,first,int,last,int,flags)
+#ifndef CLOSE_RANGE_CLOEXEC
+#define CLOSE_RANGE_CLOEXEC     (1U << 2)
+#endif
+#endif
 #if defined(__NR_futex)
 _syscall6(int,sys_futex,int *,uaddr,int,op,int,val,
           const struct timespec *,timeout,int *,uaddr2,int,val3)
@@ -8735,6 +8742,24 @@ static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1,
     case TARGET_NR_close:
         fd_trans_unregister(arg1);
         return get_errno(close(arg1));
+#if defined(__NR_close_range) && defined(TARGET_NR_close_range)
+    case TARGET_NR_close_range:
+        ret = get_errno(sys_close_range(arg1, arg2, arg3));
+        if (ret == 0 && !(arg3 & CLOSE_RANGE_CLOEXEC)) {
+            abi_long fd;
+            abi_long maxfd = arg2;
+
+            if ((sizeof(abi_long) == 4 && arg2 == (abi_long)0x7FFFFFFFUL) ||
+                (sizeof(abi_long) == 8 && arg2 == (abi_long)0x7FFFFFFFFFFFFFFFULL)) {
+                maxfd = target_fd_max;
+            }
+
+            for (fd = arg1; fd < maxfd; fd++) {
+                fd_trans_unregister(fd);
+            }
+        }
+        return ret;
+#endif
 
     case TARGET_NR_brk:
         return do_brk(arg1);
#1 lore raw

linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 8757

Quoted 8 lines · Helge Deller
linux-user/syscall.c +8 -0
+            abi_long maxfd = arg2;
+
+            if ((sizeof(abi_long) == 4 && arg2 == (abi_long)0x7FFFFFFFUL) ||
+                (sizeof(abi_long) == 8 && arg2 == (abi_long)0x7FFFFFFFFFFFFFFFULL)) {
+                maxfd = target_fd_max;
+            }
+
+            for (fd = arg1; fd < maxfd; fd++) {

Why do we need explicit checks for INT32/64_MAX? If the guest passes 0x7FFFFFFFFFFFFFFEULL, do we really need to iterate over all of those impossible values?

I should think some expression involving MIN() is in order.

r~

#2 lore raw
Quoted 12 lines · Richard Henderson

linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 8757

Quoted 8 lines · Helge Deller

+            abi_long maxfd = arg2;

linux-user/syscall.c +7 -0
+
+            if ((sizeof(abi_long) == 4 && arg2 == (abi_long)0x7FFFFFFFUL) ||
+                (sizeof(abi_long) == 8 && arg2 == (abi_long)0x7FFFFFFFFFFFFFFFULL)) {
+                maxfd = target_fd_max;
+            }
+
+            for (fd = arg1; fd < maxfd; fd++) {

Why do we need explicit checks for INT32/64_MAX?
If the guest passes 0x7FFFFFFFFFFFFFFEULL,

A 32-bit guest (on a 64bit host) will pass 0x7FFFFFFFUL...

Quoted 1 line

do we really need to iterate over all of those impossible values?

The compiler will optimize one of those checks away, so it's effectively just one expression.

Quoted 1 line

I should think some expression involving MIN() is in order.

Helge

#3 lore raw
Quoted 14 lines · Helge Deller
Quoted 12 lines · Richard Henderson

linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 8757

Quoted 8 lines · Helge Deller

+            abi_long maxfd = arg2;

linux-user/syscall.c +7 -0
+
+            if ((sizeof(abi_long) == 4 && arg2 == (abi_long)0x7FFFFFFFUL) ||
+                (sizeof(abi_long) == 8 && arg2 == (abi_long)0x7FFFFFFFFFFFFFFFULL)) {
+                maxfd = target_fd_max;
+            }
+
+            for (fd = arg1; fd < maxfd; fd++) {

Why do we need explicit checks for INT32/64_MAX?
If the guest passes 0x7FFFFFFFFFFFFFFEULL,

Quoted 6 lines

A 32-bit guest (on a 64bit host) will pass 0x7FFFFFFFUL...

Quoted 1 line

do we really need to iterate over all of those impossible values?

The compiler will optimize one of those checks away, so it's effectively just one expression.

My above comments are correct, but....

Quoted 1 line
Quoted 1 line

I should think some expression involving MIN() is in order.

that's even better.

Will resend v5 patch.

Helge

#4 lore raw
In reply to Helge Deller
Quoted 14 lines · Helge Deller
Quoted 12 lines · Richard Henderson

linux-user/syscall.c static abi_long do_syscall1(CPUArchState *cpu_env, int num, abi_long arg1, · line 8757

Quoted 8 lines · Helge Deller

+            abi_long maxfd = arg2;

linux-user/syscall.c +7 -0
+
+            if ((sizeof(abi_long) == 4 && arg2 == (abi_long)0x7FFFFFFFUL) ||
+                (sizeof(abi_long) == 8 && arg2 == (abi_long)0x7FFFFFFFFFFFFFFFULL)) {
+                maxfd = target_fd_max;
+            }
+
+            for (fd = arg1; fd < maxfd; fd++) {

Why do we need explicit checks for INT32/64_MAX?
If the guest passes 0x7FFFFFFFFFFFFFFEULL,

Quoted 6 lines

A 32-bit guest (on a 64bit host) will pass 0x7FFFFFFFUL...

Quoted 1 line

do we really need to iterate over all of those impossible values?

The compiler will optimize one of those checks away, so it's effectively just one expression.

By impossible values, I mean all descriptors above target_fd_max. The compiler will most certainly not optimize the number of loop iterations.

r~