Fix old_getrlimit handling

On some historic 32-bit platforms (and 64-bit SuperH), __NR_getrlimit
corresponds to a non-SuS-compatible version that uses a hardcoded value
of 0x7fffffff as infinity. Moreover, in order to provide a backwards
compatibility with that behaviour, some architectures (mips, sparc, and
alpha, with latter being especially peculiar in its ways) just defined
RLIM_INFINITY to that value (with alpha defining RLIM_INFINITY to 2^63-1)
instead of employing sys_old_getrlimit syscall and providing
__NR_ugetrlimit with proper implementation, as other arches did.

Overall, that led to conclusion that printing "RLIM_INFINITY" is never
enough and the actial value should be printed as well.

* linux/i386/syscallent.h ([76]): Changing decoder to old_getrlimit.
* linux/m68k/syscallent.h ([76]): Likewise.
* linux/microblaze/syscallent.h ([76]): Likewise.
* linux/powerpc/syscallent.h ([76]): Likewise.
* linux/s390/syscallent.h ([76]): Likewise.
* linux/sh/syscallent.h ([76]): Likewise.
* linux/sh64/syscallent.h ([76]): Likewise.
* resource.c (sprint_rlim64, sprint_rlim32, print_rlimit64,
print_rlimit32): Remove.
(STRACE_RLIM_INFINITY, STRACE_M32_RLIM_INFINITY,
STRACE_RLIM64_INFINITY, OLD_GETLIMIT_INFINITY): New macro constants.
(enum rlimit_decode_mode): New enumeration.
(struct rlimit_64): Move type definition out of print_rlimit64.
(print_rlim_t): New function, printer of an rlimit value, in accordance
with rlimit decode mode and xlat verbosity mode.
(print_rlimit): Printer of a fetched rlimit_64 structure.
(decode_rlimit): Fetches struct rlimit/rlimit64 in accordance with
current mode and personality, uses print_rlimit for printing.
(do_getrlimit): New function, decodes old_getrlimit/getrlimit syscalls.
(SYS_FUNC(getrlimit)): Call do_getrlimit with RDM_NORMAL mode.
(SYS_FUNC(old_getrlimit)): New function, calls do_getrlimit with
RDM_OLD_GETRLIMIT mode.
(SYS_FUNC(setrlimit)): Call decode_rlimit with RDM_NORMAL mode.
(SYS_FUNC(prlimit64)): Call decode_rlimit with RDM_PRLIMIT64 mode
instead of print_rlimit64.
* tests/xgetrlimit.c (sprint_rlim): Rename to...
(print_rlim): ...this, print to terminal instead of string buffer in
accordance with the output format expected.
(main): Accomodate sprint_rlim -> print_rlim change, use sprintrc.
* tests/getrlimit.c (INFINITY, INFINITY_STR): New macros.
* tests/setrlimit.c: Likewise.
* tests/ugetrlimit.c: Likewise.
This commit is contained in:
Eugene Syromyatnikov 2018-09-17 20:12:02 +02:00
parent 719d1f8986
commit 156777f967
12 changed files with 181 additions and 101 deletions

View File

@ -103,7 +103,7 @@
[ 73] = { 1, TS, SEN(sigpending), "sigpending" },
[ 74] = { 2, 0, SEN(sethostname), "sethostname" },
[ 75] = { 2, 0, SEN(setrlimit), "setrlimit" },
[ 76] = { 2, 0, SEN(getrlimit), "getrlimit" },
[ 76] = { 2, 0, SEN(old_getrlimit), "getrlimit" },
[ 77] = { 2, 0, SEN(getrusage), "getrusage" },
[ 78] = { 2, 0, SEN(gettimeofday), "gettimeofday" },
[ 79] = { 2, 0, SEN(settimeofday), "settimeofday" },

View File

@ -103,7 +103,7 @@
[ 73] = { 1, TS, SEN(sigpending), "sigpending" },
[ 74] = { 2, 0, SEN(sethostname), "sethostname" },
[ 75] = { 2, 0, SEN(setrlimit), "setrlimit" },
[ 76] = { 2, 0, SEN(getrlimit), "getrlimit" },
[ 76] = { 2, 0, SEN(old_getrlimit), "getrlimit" },
[ 77] = { 2, 0, SEN(getrusage), "getrusage" },
[ 78] = { 2, 0, SEN(gettimeofday), "gettimeofday" },
[ 79] = { 2, 0, SEN(settimeofday), "settimeofday" },

View File

@ -103,7 +103,7 @@
[ 73] = { 1, TS, SEN(sigpending), "sigpending" },
[ 74] = { 2, 0, SEN(sethostname), "sethostname" },
[ 75] = { 2, 0, SEN(setrlimit), "setrlimit" },
[ 76] = { 2, 0, SEN(getrlimit), "getrlimit" },
[ 76] = { 2, 0, SEN(old_getrlimit), "getrlimit" },
[ 77] = { 2, 0, SEN(getrusage), "getrusage" },
[ 78] = { 2, 0, SEN(gettimeofday), "gettimeofday" },
[ 79] = { 2, 0, SEN(settimeofday), "settimeofday" },

View File

@ -103,7 +103,7 @@
[ 73] = { 1, TS, SEN(sigpending), "sigpending" },
[ 74] = { 2, 0, SEN(sethostname), "sethostname" },
[ 75] = { 2, 0, SEN(setrlimit), "setrlimit" },
[ 76] = { 2, 0, SEN(getrlimit), "getrlimit" },
[ 76] = { 2, 0, SEN(old_getrlimit), "getrlimit" },
[ 77] = { 2, 0, SEN(getrusage), "getrusage" },
[ 78] = { 2, 0, SEN(gettimeofday), "gettimeofday" },
[ 79] = { 2, 0, SEN(settimeofday), "settimeofday" },

View File

@ -105,7 +105,7 @@
[ 73] = { 1, TS, SEN(sigpending), "sigpending" },
[ 74] = { 2, 0, SEN(sethostname), "sethostname" },
[ 75] = { 2, 0, SEN(setrlimit), "setrlimit" },
[ 76] = { 2, 0, SEN(getrlimit), "getrlimit" },
[ 76] = { 2, 0, SEN(old_getrlimit), "getrlimit" },
[ 77] = { 2, 0, SEN(getrusage), "getrusage" },
[ 78] = { 2, 0, SEN(gettimeofday), "gettimeofday" },
[ 79] = { 2, 0, SEN(settimeofday), "settimeofday" },

View File

@ -105,7 +105,7 @@
[ 73] = { 1, TS, SEN(sigpending), "sigpending" },
[ 74] = { 2, 0, SEN(sethostname), "sethostname" },
[ 75] = { 2, 0, SEN(setrlimit), "setrlimit" },
[ 76] = { 2, 0, SEN(getrlimit), "getrlimit" },
[ 76] = { 2, 0, SEN(old_getrlimit), "getrlimit" },
[ 77] = { 2, 0, SEN(getrusage), "getrusage" },
[ 78] = { 2, 0, SEN(gettimeofday), "gettimeofday" },
[ 79] = { 2, 0, SEN(settimeofday), "settimeofday" },

View File

@ -103,7 +103,7 @@
[ 73] = { 1, TS, SEN(sigpending), "sigpending" },
[ 74] = { 2, 0, SEN(sethostname), "sethostname" },
[ 75] = { 2, 0, SEN(setrlimit), "setrlimit" },
[ 76] = { 2, 0, SEN(getrlimit), "getrlimit" },
[ 76] = { 2, 0, SEN(old_getrlimit), "getrlimit" },
[ 77] = { 2, 0, SEN(getrusage), "getrusage" },
[ 78] = { 2, 0, SEN(gettimeofday), "gettimeofday" },
[ 79] = { 2, 0, SEN(settimeofday), "settimeofday" },

View File

@ -36,103 +36,175 @@
#include "xlat/resources.h"
static const char *
sprint_rlim64(uint64_t lim)
{
static char buf[sizeof(uint64_t)*3 + sizeof("*1024")];
/* Normally, RLIM_INFINITY is defined to ~0LU, but we have some very special
* architectures here.
*/
if (lim == UINT64_MAX)
return "RLIM64_INFINITY";
#if defined ALPHA
/* arch/alpha/include/uapi/asm/resource.h */
# define STRACE_RLIM_INFINITY 0x7fffffffffffffffUL
#endif
if (lim > 1024 && lim % 1024 == 0)
xsprintf(buf, "%" PRIu64 "*1024", lim / 1024);
else
xsprintf(buf, "%" PRIu64, lim);
return buf;
}
#if defined MIPS || defined SPARC || defined SPARC64
# if SIZEOF_LONG > 4
/* arch/{mips,sparc}/include/asm/compat.h */
# define STRACE_M32_RLIM_INFINITY 0x7fffffff
# else
/* arch/{mips,sparc}/include/uapi/asm/resource.h */
# define STRACE_RLIM_INFINITY 0x7fffffff
# endif
#endif
/* Generic RLIM_INFINITY definition */
#ifndef STRACE_RLIM_INFINITY
# define STRACE_RLIM_INFINITY ((kernel_ulong_t) ~0ULL)
# ifdef RLIM_INFINITY
static_assert(STRACE_RLIM_INFINITY == RLIM_INFINITY,
"Discrepancy in RLIM_INFINITY definition");
# endif
#endif /* !STRACE_RLIM_INFINITY */
#ifndef STRACE_M32_RLIM_INFINITY
# define STRACE_M32_RLIM_INFINITY (~0U)
#endif
#define STRACE_RLIM64_INFINITY (~0ULL)
#ifdef RLIM64_INFINITY
static_assert(STRACE_RLIM64_INFINITY == RLIM64_INFINITY,
"Discrepancy in RLIM64_INFINITY definition");
#endif
#define OLD_GETLIMIT_INFINITY 0x7fffffff
enum rlimit_decode_mode {
RDM_NORMAL, /* RLIM_INFINITY or its compat value */
RDM_OLD_GETRLIMIT, /* Use INT_MAX instead of RLIM_INFINITY */
RDM_PRLIMIT64, /* prlimit64 always uses RLIM64_INFINITY */
};
struct rlimit_64 {
uint64_t rlim_cur;
uint64_t rlim_max;
};
static void
print_rlimit64(struct tcb *const tcp, const kernel_ulong_t addr)
print_rlim_t(uint64_t lim, enum rlimit_decode_mode mode)
{
struct rlimit_64 {
uint64_t rlim_cur;
uint64_t rlim_max;
} rlim;
uint64_t inf;
const char *inf_str;
enum xlat_style xst;
if (!umove_or_printaddr(tcp, addr, &rlim)) {
tprintf("{rlim_cur=%s,", sprint_rlim64(rlim.rlim_cur));
tprintf(" rlim_max=%s}", sprint_rlim64(rlim.rlim_max));
if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_ABBREV)
tprintf("%" PRIu64, lim);
if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_RAW)
return;
switch (mode) {
case RDM_NORMAL:
inf = (current_personality == 1) ? STRACE_M32_RLIM_INFINITY
: STRACE_RLIM_INFINITY;
inf_str = "RLIM_INFINITY";
xst = XLAT_STYLE_VERBOSE;
break;
case RDM_OLD_GETRLIMIT:
inf = OLD_GETLIMIT_INFINITY;
inf_str = "old getrlimit() infinity";
xst = XLAT_STYLE_VERBOSE;
break;
case RDM_PRLIMIT64:
inf = STRACE_RLIM64_INFINITY;
inf_str = "RLIM64_INFINITY";
xst = xlat_verbosity;
break;
}
}
#if !defined(current_wordsize) || current_wordsize == 4
static const char *
sprint_rlim32(uint32_t lim)
{
static char buf[sizeof(uint32_t)*3 + sizeof("*1024")];
if (lim == UINT32_MAX)
return "RLIM_INFINITY";
if (lim > 1024 && lim % 1024 == 0)
xsprintf(buf, "%" PRIu32 "*1024", lim / 1024);
else
xsprintf(buf, "%" PRIu32, lim);
return buf;
}
static void
print_rlimit32(struct tcb *const tcp, const kernel_ulong_t addr)
{
struct rlimit_32 {
uint32_t rlim_cur;
uint32_t rlim_max;
} rlim;
if (!umove_or_printaddr(tcp, addr, &rlim)) {
tprintf("{rlim_cur=%s,", sprint_rlim32(rlim.rlim_cur));
tprintf(" rlim_max=%s}", sprint_rlim32(rlim.rlim_max));
if (lim == inf) {
if (xlat_verbose(xst) == XLAT_STYLE_VERBOSE) {
if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV)
tprintf("%" PRIu64, lim);
tprints_comment(inf_str);
} else {
tprints(inf_str);
}
} else if (lim > 1024 && lim % 1024 == 0) {
(xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV
? tprintf : tprintf_comment)("%" PRIu64 "*1024",
lim / 1024);
} else {
if (xlat_verbose(xlat_verbosity) == XLAT_STYLE_ABBREV)
tprintf("%" PRIu64, lim);
}
}
static void
decode_rlimit(struct tcb *const tcp, const kernel_ulong_t addr)
print_rlimit(struct tcb *const tcp, const struct rlimit_64 *rlim,
enum rlimit_decode_mode mode)
{
tprints("{rlim_cur=");
print_rlim_t(rlim->rlim_cur, mode);
tprints(", rlim_max=");
print_rlim_t(rlim->rlim_max, mode);
tprints("}");
}
static void
decode_rlimit(struct tcb *const tcp, const kernel_ulong_t addr,
enum rlimit_decode_mode mode)
{
struct rlimit_64 rlim;
/*
* i386 is the only personality on X86_64 and X32
* with 32-bit rlim_t.
* When current_personality is X32, current_wordsize
* equals to 4 but rlim_t is 64-bit.
*/
if (current_klongsize == 4)
print_rlimit32(tcp, addr);
else
print_rlimit64(tcp, addr);
if (current_klongsize == 4 && mode != RDM_PRLIMIT64) {
struct rlimit_32 {
uint32_t rlim_cur;
uint32_t rlim_max;
} m32_rlim;
if (umove_or_printaddr(tcp, addr, &m32_rlim))
return;
rlim.rlim_cur = m32_rlim.rlim_cur;
rlim.rlim_max = m32_rlim.rlim_max;
} else {
if (umove_or_printaddr(tcp, addr, &rlim))
return;
}
print_rlimit(tcp, &rlim, mode);
}
#else /* defined(current_wordsize) && current_wordsize != 4 */
# define decode_rlimit print_rlimit64
#endif
SYS_FUNC(getrlimit)
static int
do_getrlimit(struct tcb * const tcp, enum rlimit_decode_mode mode)
{
if (entering(tcp)) {
printxval(resources, tcp->u_arg[0], "RLIMIT_???");
tprints(", ");
} else {
decode_rlimit(tcp, tcp->u_arg[1]);
decode_rlimit(tcp, tcp->u_arg[1], mode);
}
return 0;
}
SYS_FUNC(getrlimit)
{
return do_getrlimit(tcp, RDM_NORMAL);
}
SYS_FUNC(old_getrlimit)
{
return do_getrlimit(tcp, RDM_OLD_GETRLIMIT);
}
SYS_FUNC(setrlimit)
{
printxval(resources, tcp->u_arg[0], "RLIMIT_???");
tprints(", ");
decode_rlimit(tcp, tcp->u_arg[1]);
decode_rlimit(tcp, tcp->u_arg[1], RDM_NORMAL);
return RVAL_DECODED;
}
@ -143,10 +215,10 @@ SYS_FUNC(prlimit64)
tprintf("%d, ", (int) tcp->u_arg[0]);
printxval(resources, tcp->u_arg[1], "RLIMIT_???");
tprints(", ");
print_rlimit64(tcp, tcp->u_arg[2]);
decode_rlimit(tcp, tcp->u_arg[2], RDM_PRLIMIT64);
tprints(", ");
} else {
print_rlimit64(tcp, tcp->u_arg[3]);
decode_rlimit(tcp, tcp->u_arg[3], RDM_PRLIMIT64);
}
return 0;
}

View File

@ -5,6 +5,15 @@
# define NR_GETRLIMIT __NR_getrlimit
# define STR_GETRLIMIT "getrlimit"
# ifdef __NR_ugetrlimit
# define INFINITY 0x7fffffff
# define INFINITY_STR "old getrlimit() infinity"
# else
# define INFINITY RLIM_INFINITY
# define INFINITY_STR "RLIM_INFINITY"
# endif
# include "xgetrlimit.c"
#else

View File

@ -32,6 +32,9 @@
#ifdef __NR_setrlimit
#define INFINITY RLIM_INFINITY
#define INFINITY_STR "RLIM_INFINITY"
# include "xgetrlimit.c"
int
@ -57,10 +60,12 @@ main(void)
rc = syscall(__NR_setrlimit, res, rlimit);
const char *errstr = sprintrc(rc);
printf("setrlimit(%s, {rlim_cur=%s, rlim_max=%s}) = %s\n",
xlat->str,
sprint_rlim(rlimit[0]), sprint_rlim(rlimit[1]),
errstr);
printf("setrlimit(%s, {rlim_cur=", xlat->str);
print_rlim(rlimit[0]);
printf(", rlim_max=");
print_rlim(rlimit[1]);
printf("}) = %s\n", errstr);
}
puts("+++ exited with 0 +++");

View File

@ -5,6 +5,8 @@
# define NR_GETRLIMIT __NR_ugetrlimit
# define STR_GETRLIMIT "ugetrlimit"
# define INFINITY RLIM_INFINITY
# define INFINITY_STR "RLIM_INFINITY"
# include "xgetrlimit.c"
#else

View File

@ -36,26 +36,15 @@
#include "xlat.h"
#include "xlat/resources.h"
const char *
sprint_rlim(kernel_ulong_t lim)
void
print_rlim(kernel_ulong_t lim)
{
if (sizeof(lim) == sizeof(uint64_t)) {
if (lim == (kernel_ulong_t) -1ULL)
return "RLIM64_INFINITY";
} else {
if (lim == (kernel_ulong_t) -1U)
return "RLIM_INFINITY";
}
static char buf[2][sizeof(lim)*3 + sizeof("*1024")];
static int i;
i &= 1;
if (lim > 1024 && lim % 1024 == 0)
sprintf(buf[i], "%llu*1024", (unsigned long long) lim / 1024);
if (lim == INFINITY)
printf("%llu /* %s */", (unsigned long long) lim, INFINITY_STR);
else if (lim > 1024 && lim % 1024 == 0)
printf("%llu*1024", (unsigned long long) lim / 1024);
else
sprintf(buf[i], "%llu", (unsigned long long) lim);
return buf[i++];
printf("%llu", (unsigned long long) lim);
}
#ifdef NR_GETRLIMIT
@ -79,13 +68,16 @@ main(void)
STR_GETRLIMIT, xlat->str, rc, errno2name());
rc = syscall(NR_GETRLIMIT, res, rlimit);
if (rc)
printf("%s(%s, NULL) = %ld %s (%m)\n",
STR_GETRLIMIT, xlat->str, rc, errno2name());
else
printf("%s(%s, {rlim_cur=%s, rlim_max=%s})"
" = 0\n", STR_GETRLIMIT, xlat->str,
sprint_rlim(rlimit[0]), sprint_rlim(rlimit[1]));
if (rc) {
printf("%s(%s, %p) = %s\n",
STR_GETRLIMIT, xlat->str, rlimit, sprintrc(rc));
} else {
printf("%s(%s, {rlim_cur=", STR_GETRLIMIT, xlat->str);
print_rlim(rlimit[0]);
printf(", rlim_max=");
print_rlim(rlimit[1]);
printf("}) = 0\n");
}
}
puts("+++ exited with 0 +++");