xref: /dragonfly/test/stress/t_mlock.c (revision f9993810)
1 #include <sys/mman.h>
2 #include <sys/types.h>
3 #include <sys/wait.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <err.h>
10 
11 #define TESTSIZE	(128L*1024*1024)
12 
13 char *TestBuf;
14 char *FlagBuf;
15 
16 int
17 main(int argc, char **argv)
18 {
19 	size_t beg;
20 	size_t len;
21 	size_t fbeg;
22 	size_t flen;
23 	size_t i;
24 	long counter = 0;
25 
26 	TestBuf = calloc(1, TESTSIZE);
27 	FlagBuf = calloc(1, TESTSIZE / 4096);
28 
29 	for (;;) {
30 		beg = random() & (TESTSIZE - 1) & ~4095L;
31 		len = random() & (TESTSIZE - 1) & ~4095L;
32 		if (beg + len > TESTSIZE)
33 			len = TESTSIZE - beg;
34 		fbeg = beg / 4096;
35 		flen = len / 4096;
36 		if (len == 0)
37 			continue;
38 
39 		if (random() & 1) {
40 			if (mlock(TestBuf + beg, len) < 0) {
41 				printf("%ld   mlock: %016lx-%016lx: ERROR %s\n",
42 					counter, beg, beg + len,
43 					strerror(errno));
44 			} else {
45 				memset(FlagBuf + fbeg, 1, flen);
46 				printf("%ld   mlock: %016lx-%016lx: OK\n",
47 					counter, beg, beg + len);
48 			}
49 		} else {
50 			for (i = 0; i < flen; ++i) {
51 				if (FlagBuf[fbeg+i] == 0)
52 					break;
53 			}
54 			if (i == flen) {
55 				memset(FlagBuf + fbeg, 0, flen);
56 				if (munlock(TestBuf + beg, len) < 0) {
57 					printf("%ld munlock: %016lx-%016lx: "
58 						"ERROR %s\n",
59 						counter, beg, beg + len,
60 						strerror(errno));
61 				} else {
62 					printf("%ld munlock: %016lx-%016lx: OK\n",
63 						counter, beg, beg + len);
64 				}
65 			} else {
66 				if (munlock(TestBuf + beg, len) == 0) {
67 					printf("%ld munlock: %016lx-%016lx: ERROR "
68 						"should have failed "
69 						"at %016lx\n",
70 						counter, beg, beg + len,
71 						(fbeg + i) * 4096L);
72 				} else {
73 					printf("%ld munlock: %016lx-%016lx: OK "
74 						"(intentional failure)\n",
75 						counter, beg, beg + len);
76 				}
77 			}
78 		}
79 #if 0
80 		printf("%ld\r", counter);
81 #endif
82 		fflush(stdout);
83 		++counter;
84 	}
85         return (0);
86 }
87