|
From: <sv...@va...> - 2012-03-01 13:46:56
|
Author: tom
Date: 2012-03-01 13:42:18 +0000 (Thu, 01 Mar 2012)
New Revision: 12411
Log:
Handle prlimit64 the same way we do getrlimit and setrlimit, with
some requests trapped and handled by valgrind.
Patch from Matthias Schwarzott via BZ#294047.
Added:
trunk/none/tests/rlimit64_nofile.c
trunk/none/tests/rlimit64_nofile.stderr.exp
trunk/none/tests/rlimit64_nofile.stdout.exp
trunk/none/tests/rlimit64_nofile.vgtest
Modified:
trunk/coregrind/m_syswrap/syswrap-generic.c
trunk/coregrind/m_syswrap/syswrap-linux.c
trunk/none/tests/Makefile.am
trunk/none/tests/rlimit_nofile.c
Modified: trunk/coregrind/m_syswrap/syswrap-generic.c
===================================================================
--- trunk/coregrind/m_syswrap/syswrap-generic.c 2012-02-28 22:37:44 UTC (rev 12410)
+++ trunk/coregrind/m_syswrap/syswrap-generic.c 2012-03-01 13:42:18 UTC (rev 12411)
@@ -3917,7 +3917,11 @@
arg1 &= ~_RLIMIT_POSIX_FLAG;
#endif
- if (arg1 == VKI_RLIMIT_NOFILE) {
+ if (ARG2 &&
+ ((struct vki_rlimit *)ARG2)->rlim_cur > ((struct vki_rlimit *)ARG2)->rlim_max) {
+ SET_STATUS_Failure( VKI_EINVAL );
+ }
+ else 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)) {
SET_STATUS_Failure( VKI_EPERM );
Modified: trunk/coregrind/m_syswrap/syswrap-linux.c
===================================================================
--- trunk/coregrind/m_syswrap/syswrap-linux.c 2012-02-28 22:37:44 UTC (rev 12410)
+++ trunk/coregrind/m_syswrap/syswrap-linux.c 2012-03-01 13:42:18 UTC (rev 12411)
@@ -1285,32 +1285,76 @@
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 &&
+ ((struct vki_rlimit64 *)ARG3)->rlim_cur > ((struct vki_rlimit64 *)ARG3)->rlim_max) {
+ SET_STATUS_Failure( VKI_EINVAL );
+ }
+ else if (ARG1 == 0 || ARG1 == VG_(getpid)()) {
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
------------------------------------------------------------------ */
Modified: trunk/none/tests/Makefile.am
===================================================================
--- trunk/none/tests/Makefile.am 2012-02-28 22:37:44 UTC (rev 12410)
+++ trunk/none/tests/Makefile.am 2012-03-01 13:42:18 UTC (rev 12411)
@@ -130,6 +130,7 @@
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 \
@@ -185,7 +186,7 @@
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 \
Added: trunk/none/tests/rlimit64_nofile.c
===================================================================
--- trunk/none/tests/rlimit64_nofile.c (rev 0)
+++ trunk/none/tests/rlimit64_nofile.c 2012-03-01 13:42:18 UTC (rev 12411)
@@ -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);
+}
Property changes on: trunk/none/tests/rlimit64_nofile.c
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Added: trunk/none/tests/rlimit64_nofile.stderr.exp
===================================================================
--- trunk/none/tests/rlimit64_nofile.stderr.exp (rev 0)
+++ trunk/none/tests/rlimit64_nofile.stderr.exp 2012-03-01 13:42:18 UTC (rev 12411)
@@ -0,0 +1,2 @@
+
+
Added: trunk/none/tests/rlimit64_nofile.stdout.exp
===================================================================
Added: trunk/none/tests/rlimit64_nofile.vgtest
===================================================================
--- trunk/none/tests/rlimit64_nofile.vgtest (rev 0)
+++ trunk/none/tests/rlimit64_nofile.vgtest 2012-03-01 13:42:18 UTC (rev 12411)
@@ -0,0 +1 @@
+prog: rlimit64_nofile
Modified: trunk/none/tests/rlimit_nofile.c
===================================================================
--- trunk/none/tests/rlimit_nofile.c 2012-02-28 22:37:44 UTC (rev 12410)
+++ trunk/none/tests/rlimit_nofile.c 2012-03-01 13:42:18 UTC (rev 12411)
@@ -20,6 +20,36 @@
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;
|