Hi all,

I tried to run a win32 application under valgrind+wine on a gentoo x86 installation.
Sadly this failed on an internal error like in
https://bugs.kde.org/show_bug.cgi?id=275673

The error looks like this part from the bug:
strace.txt:61324:[pid  2563] pipe([20, 21])              = 0
strace.txt:61325:[pid 2563] fcntl(20, F_DUPFD, 1014) = -1 EINVAL (Invalid
argument)
strace.txt:61327:[pid  2563] gettid()                    = 2563
strace.txt:61408:[pid  2563] fcntl(-1, F_SETFD, FD_CLOEXEC) = -1 EBADF (Bad
file descriptor)
strace.txt:61413:[pid  2563] gettid()                    = 2563
strace.txt:61509:[pid  2563] poll([{fd=1019, events=POLLIN}], 1, -1

The duplicating of the fd fails.
The reason is, that before prlimit64 is called to change RLIMIT_NOFILE.
But valgrind does not emulate it like setrlimit and getrlimit.
(The second call to set FD_CLOEXEC could be avoided on error of F_DUPFD before.)

I changed valgrind to make prlimit64 behave like the setrlimit and getrlimit syscalls. The patch also contains a testcase for setrlimit64 and getrlimit64 for fileno. I needed to move all code to the pre-handler, as it is possible to call prlimit64 with both set and get parameter.
This also deserves a testcase.
What also is missing is: only using the emulation if pid is 0 or equal the current pid.

The attached patch applies against valgrind svn revision 12373.
Tested on a Gentoo amd64 installation built for 64bit and also built for 32bit (with --enable-only32bit).

Regards,
Matthias

    Add better implementation of prlimit64
    
    improve prlimit64
    improve setrlimit to return EINVAL if softlimit>hardlimit
    add tests for all that
    
    v4
    
    The emulation should only happen if ARG0(pid) == 0 or equal getpid()

diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c
index 18f2a5f..275c5d1 100644
--- a/coregrind/m_syswrap/syswrap-generic.c
+++ b/coregrind/m_syswrap/syswrap-generic.c
@@ -3908,6 +3908,14 @@ PRE(sys_setrlimit)
    arg1 &= ~_RLIMIT_POSIX_FLAG;
 #endif
 
+   if (ARG2) {
+      if (((struct vki_rlimit *)ARG2)->rlim_cur > ((struct vki_rlimit *)ARG2)->rlim_max)
+      {
+         SET_STATUS_Failure( VKI_EINVAL );
+         return;
+      }
+   }
+
    if (arg1 == VKI_RLIMIT_NOFILE) {
       if (((struct vki_rlimit *)ARG2)->rlim_cur > VG_(fd_hard_limit) ||
           ((struct vki_rlimit *)ARG2)->rlim_max != VG_(fd_hard_limit)) {
diff --git a/coregrind/m_syswrap/syswrap-linux.c b/coregrind/m_syswrap/syswrap-linux.c
index 5a62f59..5a63d67 100644
--- a/coregrind/m_syswrap/syswrap-linux.c
+++ b/coregrind/m_syswrap/syswrap-linux.c
@@ -1285,32 +1285,80 @@ PRE(sys_prlimit64)
       PRE_MEM_READ( "rlimit64(new_rlim)", ARG3, sizeof(struct vki_rlimit64) );
    if (ARG4)
       PRE_MEM_WRITE( "rlimit64(old_rlim)", ARG4, sizeof(struct vki_rlimit64) );
-}
 
-POST(sys_prlimit64)
-{
-   if (ARG4) {
-      POST_MEM_WRITE( ARG4, sizeof(struct vki_rlimit64) );
+   if (ARG3) {
+      if (((struct vki_rlimit64 *)ARG3)->rlim_cur > ((struct vki_rlimit64 *)ARG3)->rlim_max)
+      {
+         SET_STATUS_Failure( VKI_EINVAL );
+         return;
+      }
+   }
 
+   if (ARG1 == 0) {
       switch (ARG2) {
       case VKI_RLIMIT_NOFILE:
-         ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(fd_soft_limit);
-         ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(fd_hard_limit);
+         SET_STATUS_Success( 0 );
+         if (ARG4) {
+            ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(fd_soft_limit);
+            ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(fd_hard_limit);
+         }
+         if (ARG3) {
+            if (((struct vki_rlimit64 *)ARG3)->rlim_cur > VG_(fd_hard_limit) ||
+                ((struct vki_rlimit64 *)ARG3)->rlim_max != VG_(fd_hard_limit)) {
+               SET_STATUS_Failure( VKI_EPERM );
+            }
+            else {
+               VG_(fd_soft_limit) = ((struct vki_rlimit64 *)ARG3)->rlim_cur;
+            }
+         }
          break;
 
       case VKI_RLIMIT_DATA:
-         ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_data).rlim_cur;
-         ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_data).rlim_max;
+         SET_STATUS_Success( 0 );
+         if (ARG4) {
+            ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_data).rlim_cur;
+            ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_data).rlim_max;
+         }
+         if (ARG3) {
+            if (((struct vki_rlimit64 *)ARG3)->rlim_cur > VG_(client_rlimit_data).rlim_max ||
+                ((struct vki_rlimit64 *)ARG3)->rlim_max > VG_(client_rlimit_data).rlim_max) {
+               SET_STATUS_Failure( VKI_EPERM );
+            }
+            else {
+               VG_(client_rlimit_data).rlim_cur = ((struct vki_rlimit64 *)ARG3)->rlim_cur;
+               VG_(client_rlimit_data).rlim_max = ((struct vki_rlimit64 *)ARG3)->rlim_max;
+            }
+         }
          break;
 
       case VKI_RLIMIT_STACK:
-         ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_stack).rlim_cur;
-         ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_stack).rlim_max;
+         SET_STATUS_Success( 0 );
+         if (ARG4) {
+            ((struct vki_rlimit64 *)ARG4)->rlim_cur = VG_(client_rlimit_stack).rlim_cur;
+            ((struct vki_rlimit64 *)ARG4)->rlim_max = VG_(client_rlimit_stack).rlim_max;
+         }
+         if (ARG3) {
+            if (((struct vki_rlimit64 *)ARG3)->rlim_cur > VG_(client_rlimit_stack).rlim_max ||
+                ((struct vki_rlimit64 *)ARG3)->rlim_max > VG_(client_rlimit_stack).rlim_max) {
+               SET_STATUS_Failure( VKI_EPERM );
+            }
+            else {
+               VG_(threads)[tid].client_stack_szB = ((struct vki_rlimit64 *)ARG3)->rlim_cur;
+               VG_(client_rlimit_stack).rlim_cur = ((struct vki_rlimit64 *)ARG3)->rlim_cur;
+               VG_(client_rlimit_stack).rlim_max = ((struct vki_rlimit64 *)ARG3)->rlim_max;
+            }
+         }
          break;
       }
    }
 }
 
+POST(sys_prlimit64)
+{
+   if (ARG4)
+      POST_MEM_WRITE( ARG4, sizeof(struct vki_rlimit64) );
+}
+
 /* ---------------------------------------------------------------------
    tid-related wrappers
    ------------------------------------------------------------------ */
diff --git a/none/tests/Makefile.am b/none/tests/Makefile.am
index 49350c7..338f371 100644
--- a/none/tests/Makefile.am
+++ b/none/tests/Makefile.am
@@ -130,6 +130,7 @@ EXTRA_DIST = \
 	res_search.stderr.exp res_search.stdout.exp res_search.vgtest \
 	resolv.stderr.exp resolv.stdout.exp resolv.vgtest \
 	rlimit_nofile.stderr.exp rlimit_nofile.stdout.exp rlimit_nofile.vgtest \
+	rlimit64_nofile.stderr.exp rlimit64_nofile.stdout.exp rlimit64_nofile.vgtest \
 	selfrun.stderr.exp selfrun.stdout.exp selfrun.vgtest \
 	sem.stderr.exp sem.stdout.exp sem.vgtest \
 	semlimit.stderr.exp semlimit.stdout.exp semlimit.vgtest \
@@ -184,7 +185,7 @@ check_PROGRAMS = \
 	rcrl readline1 \
 	require-text-symbol \
 	res_search resolv \
-	rlimit_nofile selfrun sem semlimit sha1_test \
+	rlimit_nofile rlimit64_nofile selfrun sem semlimit sha1_test \
 	shortpush shorts stackgrowth sigstackgrowth \
 	syscall-restart1 syscall-restart2 \
 	syslog \
diff --git a/none/tests/rlimit64_nofile.c b/none/tests/rlimit64_nofile.c
new file mode 100644
index 0000000..7580fb6
--- /dev/null
+++ b/none/tests/rlimit64_nofile.c
@@ -0,0 +1,105 @@
+#define _LARGEFILE_SOURCE
+#define _LARGEFILE64_SOURCE
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/resource.h>
+#include <unistd.h>
+#include "fdleak.h"
+
+int main(int argc, char **argv)
+{
+   struct rlimit64 oldrlim;
+   struct rlimit64 newrlim;
+   int fd;
+
+   CLOSE_INHERITED_FDS;
+
+   if (getrlimit64(RLIMIT_NOFILE, &oldrlim) < 0)
+   {
+      perror("getrlimit");
+      exit(1);
+   }
+
+   newrlim.rlim_cur = oldrlim.rlim_max+1;
+   newrlim.rlim_max = oldrlim.rlim_max;     
+   if (setrlimit64(RLIMIT_NOFILE, &newrlim) == -1)
+   {
+      if (errno != EINVAL) {
+         fprintf(stderr, "setrlimit64 exceeding hardlimit must set errno=EINVAL\n");
+         exit(1);
+      }
+   }
+   else
+   {
+        fprintf(stderr, "setrlimit64 exceeding hardlimit must return -1\n");
+        exit(1);
+   }
+
+   newrlim.rlim_cur = oldrlim.rlim_max;
+   newrlim.rlim_max = oldrlim.rlim_max+1;
+   if (setrlimit64(RLIMIT_NOFILE, &newrlim) == -1)
+   {
+      if (errno != EPERM) {
+         fprintf(stderr, "setrlimit64 changing hardlimit must set errno=EPERM\n");
+         exit(1);
+      }
+   }
+   else
+   {
+        fprintf(stderr, "setrlimit64 changing hardlimit must return -1\n");
+        exit(1);
+   }
+
+   newrlim.rlim_cur = oldrlim.rlim_cur / 2;
+   newrlim.rlim_max = oldrlim.rlim_max;
+     
+   if (setrlimit64(RLIMIT_NOFILE, &newrlim) < 0)
+   {
+      perror("setrlimit64");
+      exit(1);
+   }
+     
+   if (getrlimit64(RLIMIT_NOFILE, &newrlim) < 0)
+   {
+      perror("getrlimit");
+      exit(1);
+   }
+
+   if (newrlim.rlim_cur != oldrlim.rlim_cur / 2)
+   {
+      fprintf(stderr, "rlim_cur is %llu (should be %llu)\n",
+              (unsigned long long)newrlim.rlim_cur,
+              (unsigned long long)oldrlim.rlim_cur / 2);
+   }
+
+   if (newrlim.rlim_max != oldrlim.rlim_max)
+   {
+      fprintf(stderr, "rlim_max is %llu (should be %llu)\n",
+              (unsigned long long)newrlim.rlim_max,
+              (unsigned long long)oldrlim.rlim_max);
+   }
+
+   newrlim.rlim_cur -= 3; /* allow for stdin, stdout and stderr */
+
+   while (newrlim.rlim_cur-- > 0)
+   {
+      if (open("/dev/null", O_RDONLY) < 0)
+      {
+         perror("open");
+      }
+   }
+
+   if ((fd = open("/dev/null", O_RDONLY)) >= 0)
+   {
+      fprintf(stderr, "open succeeded with fd %d - it should have failed!\n", fd);
+   }
+   else if (errno != EMFILE)
+   {
+      perror("open");
+   }
+   
+   exit(0);
+}
diff --git a/none/tests/rlimit64_nofile.stderr.exp b/none/tests/rlimit64_nofile.stderr.exp
new file mode 100644
index 0000000..139597f
--- /dev/null
+++ b/none/tests/rlimit64_nofile.stderr.exp
@@ -0,0 +1,2 @@
+
+
diff --git a/none/tests/rlimit64_nofile.stdout.exp b/none/tests/rlimit64_nofile.stdout.exp
new file mode 100644
index 0000000..e69de29
diff --git a/none/tests/rlimit64_nofile.vgtest b/none/tests/rlimit64_nofile.vgtest
new file mode 100644
index 0000000..de86a21
--- /dev/null
+++ b/none/tests/rlimit64_nofile.vgtest
@@ -0,0 +1 @@
+prog: rlimit64_nofile
diff --git a/none/tests/rlimit_nofile.c b/none/tests/rlimit_nofile.c
index c5a0a41..135f1a1 100644
--- a/none/tests/rlimit_nofile.c
+++ b/none/tests/rlimit_nofile.c
@@ -20,6 +20,36 @@ int main(int argc, char **argv)
       exit(1);
    }
 
+   newrlim.rlim_cur = oldrlim.rlim_max+1;
+   newrlim.rlim_max = oldrlim.rlim_max;
+   if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1)
+   {
+      if (errno != EINVAL) {
+         fprintf(stderr, "setrlimit exceeding hardlimit must set errno=EINVAL\n");
+         exit(1);
+      }
+   }
+   else
+   {
+        fprintf(stderr, "setrlimit exceeding hardlimit must return -1\n");
+        exit(1);
+   }
+
+   newrlim.rlim_cur = oldrlim.rlim_max;
+   newrlim.rlim_max = oldrlim.rlim_max+1;
+   if (setrlimit(RLIMIT_NOFILE, &newrlim) == -1)
+   {
+      if (errno != EPERM) {
+         fprintf(stderr, "setrlimit changing hardlimit must set errno=EPERM\n");
+         exit(1);
+      }
+   }
+   else
+   {
+        fprintf(stderr, "setrlimit changing hardlimit must return -1\n");
+        exit(1);
+   }
+
    newrlim.rlim_cur = oldrlim.rlim_cur / 2;
    newrlim.rlim_max = oldrlim.rlim_max;
      

------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to