• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2018 Google, Inc.
4  *
5  * Architectures may provide up to three syscalls that have been used to
6  * implement getrlimit(2) in different libc implementations.  These syscalls
7  * differ in the size and signedness of rlim_t:
8  *
9  * - __NR_getrlimit uses long or unsigned long, depending on the
10  *   architecture
11  *
12  * - __NR_ugetrlimit uses unsigned long, and only exists on
13  *   architectures where __NR_getrlimit is signed
14  *
15  * - __NR_prlimit64 uses uint64_t
16  *
17  * This test compares the results returned by all three syscalls, confirming
18  * that they either match or were appropriately capped at the respective
19  * RLIM_INFINITY constant.
20  */
21 
22 #include <inttypes.h>
23 #include <stdint.h>
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 
27 #include "tst_test.h"
28 #include "lapi/syscalls.h"
29 
30 /**
31  * Linux provides an "old" getrlimit syscall handler that uses signed long,
32  * and a "new" getrlimit syscall handler that uses unsigned long.
33  *
34  * The underlying syscall names vary across architectures, depending on whether
35  * the architecture predates the "new" handler.  For clarity, this test
36  * will call them getrlimit_long and getlimit_ulong internally.
37  */
38 #define SIGNED_GETRLIMIT (__NR_ugetrlimit != __LTP__NR_INVALID_SYSCALL)
39 #if SIGNED_GETRLIMIT
40 #define __NR_getrlimit_ulong		__NR_ugetrlimit
41 #define __NR_getrlimit_ulong_str	"__NR_ugetrlimit"
42 #else
43 #define __NR_getrlimit_ulong		__NR_getrlimit
44 #define __NR_getrlimit_ulong_str	"__NR_getrlimit"
45 #endif
46 
47 #ifndef HAVE_STRUCT_RLIMIT64
48 struct rlimit64 {
49 	uint64_t rlim_cur;
50 	uint64_t rlim_max;
51 };
52 #endif
53 const uint64_t RLIM_INFINITY_U64 = UINT64_MAX;
54 
getrlimit_u64(int resource,struct rlimit64 * rlim)55 static int getrlimit_u64(int resource, struct rlimit64 *rlim)
56 {
57 	return tst_syscall(__NR_prlimit64, 0, resource, NULL, rlim);
58 }
59 
60 struct rlimit_ulong {
61 	unsigned long rlim_cur;
62 	unsigned long rlim_max;
63 };
64 const unsigned long RLIM_INFINITY_UL = ULONG_MAX;
65 
getrlimit_ulong(int resource,struct rlimit_ulong * rlim)66 static int getrlimit_ulong(int resource, struct rlimit_ulong *rlim)
67 {
68 	return syscall(__NR_getrlimit_ulong, resource, rlim);
69 }
70 
71 #if SIGNED_GETRLIMIT
72 struct rlimit_long {
73 	long rlim_cur;
74 	long rlim_max;
75 };
76 const long RLIM_INFINITY_L = LONG_MAX;
77 
getrlimit_long(int resource,struct rlimit_long * rlim)78 static int getrlimit_long(int resource, struct rlimit_long *rlim)
79 {
80 	return syscall(__NR_getrlimit, resource, rlim);
81 }
82 #endif
83 
compare_retval(int resource,int ret_u64,int errno_u64,int ret_other,int errno_other,const char * other_syscall)84 static int compare_retval(int resource, int ret_u64, int errno_u64,
85 			  int ret_other, int errno_other,
86 			  const char *other_syscall)
87 {
88 	if (ret_u64 != ret_other || errno_u64 != errno_other) {
89 		tst_res(TFAIL, "__NR_prlimit64(%d) returned %d (%s) but %s(%d) returned %d (%s)",
90 			resource, ret_u64, tst_strerrno(errno_u64),
91 			other_syscall, resource, ret_other,
92 			tst_strerrno(errno_other));
93 		return -1;
94 	}
95 
96 	return 0;
97 }
98 
compare_u64_ulong(int resource,uint64_t val_u64,unsigned long val_ul,const char * kind)99 static int compare_u64_ulong(int resource, uint64_t val_u64,
100 			     unsigned long val_ul, const char *kind)
101 {
102 	if ((val_u64 > RLIM_INFINITY_UL && val_ul != RLIM_INFINITY_UL) ||
103 	    (val_u64 <= RLIM_INFINITY_UL && val_ul != val_u64)) {
104 		tst_res(TFAIL, "__NR_prlimit64(%d) had %s = %" PRIx64 " but " __NR_getrlimit_ulong_str "(%d) had %s = %lx",
105 			resource, kind, val_u64,
106 			resource, kind, val_ul);
107 		return -1;
108 	}
109 
110 	return 0;
111 }
112 
113 #if SIGNED_GETRLIMIT
compare_u64_long(int resource,uint64_t val_u64,long val_l,const char * kind)114 static int compare_u64_long(int resource, uint64_t val_u64, long val_l,
115 			    const char *kind)
116 {
117 	if ((val_u64 > (uint64_t)RLIM_INFINITY_L && val_l != RLIM_INFINITY_L) ||
118 	    (val_u64 <= (uint64_t)RLIM_INFINITY_L && val_l != (long)val_u64)) {
119 		tst_res(TFAIL, "__NR_prlimit64(%d) had %s = %" PRIx64 " but __NR_getrlimit(%d) had %s = %lx",
120 			resource, kind, val_u64,
121 			resource, kind, val_l);
122 		return -1;
123 	}
124 
125 	return 0;
126 }
127 #endif
128 
run(unsigned int resource)129 static void run(unsigned int resource)
130 {
131 	struct rlimit64 rlim_u64;
132 	int ret_u64;
133 	int errno_u64;
134 
135 	struct rlimit_ulong rlim_ul;
136 	int ret_ul;
137 	int errno_ul;
138 
139 #if SIGNED_GETRLIMIT
140 	struct rlimit_long rlim_l;
141 	int ret_l;
142 	int errno_l;
143 #endif
144 
145 	errno = 0;
146 	ret_u64 = getrlimit_u64(resource, &rlim_u64);
147 	errno_u64 = errno;
148 
149 	errno = 0;
150 	ret_ul = getrlimit_ulong(resource, &rlim_ul);
151 	errno_ul = errno;
152 
153 	if (compare_retval(resource, ret_u64, errno_u64, ret_ul, errno_ul,
154 			   __NR_getrlimit_ulong_str) ||
155 	    compare_u64_ulong(resource, rlim_u64.rlim_cur, rlim_ul.rlim_cur,
156 			      "rlim_cur") ||
157 	    compare_u64_ulong(resource, rlim_u64.rlim_max, rlim_ul.rlim_max,
158 			      "rlim_max"))
159 		return;
160 
161 	tst_res(TPASS, "__NR_prlimit64(%d) and %s(%d) gave consistent results",
162 		resource, __NR_getrlimit_ulong_str, resource);
163 
164 #if SIGNED_GETRLIMIT
165 	errno = 0;
166 	ret_l = getrlimit_long(resource, &rlim_l);
167 	errno_l = errno;
168 	if (errno_l == ENOSYS) {
169 		tst_res(TCONF | TERRNO,
170 			"__NR_getrlimit(%d) not implemented", __NR_getrlimit);
171 		return;
172 	}
173 
174 	if (compare_retval(resource, ret_u64, errno_u64, ret_l, errno_l,
175 			   "__NR_getrlimit") ||
176 	    compare_u64_long(resource, rlim_u64.rlim_cur, rlim_l.rlim_cur,
177 			     "rlim_cur") ||
178 	    compare_u64_long(resource, rlim_u64.rlim_max, rlim_l.rlim_max,
179 			     "rlim_max"))
180 		return;
181 
182 	tst_res(TPASS, "__NR_prlimit64(%d) and __NR_getrlimit(%d) gave "
183 		"consistent results", resource, resource);
184 #endif
185 }
186 
187 static struct tst_test test = {
188 	.tcnt = RLIM_NLIMITS,
189 	.test = run,
190 };
191