Loading...
1#include <sys/mman.h>
2#include <stdio.h>
3#include <unistd.h>
4#include <string.h>
5#include <sys/time.h>
6#include <sys/resource.h>
7
8#ifndef MCL_ONFAULT
9#define MCL_ONFAULT (MCL_FUTURE << 1)
10#endif
11
12static int test_limit(void)
13{
14 int ret = 1;
15 struct rlimit lims;
16 void *map;
17
18 if (getrlimit(RLIMIT_MEMLOCK, &lims)) {
19 perror("getrlimit");
20 return ret;
21 }
22
23 if (mlockall(MCL_ONFAULT | MCL_FUTURE)) {
24 perror("mlockall");
25 return ret;
26 }
27
28 map = mmap(NULL, 2 * lims.rlim_max, PROT_READ | PROT_WRITE,
29 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, 0, 0);
30 if (map != MAP_FAILED)
31 printf("mmap should have failed, but didn't\n");
32 else {
33 ret = 0;
34 munmap(map, 2 * lims.rlim_max);
35 }
36
37 munlockall();
38 return ret;
39}
40
41int main(int argc, char **argv)
42{
43 int ret = 0;
44
45 ret += test_limit();
46 return ret;
47}
1// SPDX-License-Identifier: GPL-2.0
2#include <sys/mman.h>
3#include <stdio.h>
4#include <unistd.h>
5#include <string.h>
6#include <sys/time.h>
7#include <sys/resource.h>
8
9#ifndef MCL_ONFAULT
10#define MCL_ONFAULT (MCL_FUTURE << 1)
11#endif
12
13static int test_limit(void)
14{
15 int ret = 1;
16 struct rlimit lims;
17 void *map;
18
19 if (getrlimit(RLIMIT_MEMLOCK, &lims)) {
20 perror("getrlimit");
21 return ret;
22 }
23
24 if (mlockall(MCL_ONFAULT | MCL_FUTURE)) {
25 perror("mlockall");
26 return ret;
27 }
28
29 map = mmap(NULL, 2 * lims.rlim_max, PROT_READ | PROT_WRITE,
30 MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
31 if (map != MAP_FAILED)
32 printf("mmap should have failed, but didn't\n");
33 else {
34 ret = 0;
35 munmap(map, 2 * lims.rlim_max);
36 }
37
38 munlockall();
39 return ret;
40}
41
42int main(int argc, char **argv)
43{
44 int ret = 0;
45
46 ret += test_limit();
47 return ret;
48}