]>
git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/mprotect.c
4 * The contents of this file are subject to the terms
5 * of the Common Development and Distribution License
6 * (the "License"). You may not use this file except
7 * in compliance with the License.
9 * You can obtain a copy of the license at
10 * src/OPENSOLARIS.LICENSE
11 * or http://www.opensolaris.org/os/licensing.
12 * See the License for the specific language governing
13 * permissions and limitations under the License.
15 * When distributing Covered Code, include this CDDL
16 * HEADER in each file and include the License file at
17 * usr/src/OPENSOLARIS.LICENSE. If applicable,
18 * add the following below this CDDL HEADER, with the
19 * fields enclosed by brackets "[]" replaced with your
20 * own identifying information: Portions Copyright [yyyy]
21 * [name of copyright owner]
27 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
40 typedef volatile char vchar_t
;
47 #define DEFF "/dev/zero"
50 static char *optf
= DEFF
;
51 static long long optl
= DEFL
;
65 lm_tsdsize
= sizeof (tsd_t
);
67 (void) sprintf(lm_optstr
, "f:l:rstw");
69 (void) sprintf(lm_usage
,
70 " [-f file-to-map (default %s)]\n"
71 " [-l mapping-length (default %d)]\n"
72 " [-r] (read a byte from each page)\n"
73 " [-w] (write a byte on each page)\n"
74 " [-s] (use MAP_SHARED)\n"
75 " [-t] (touch each page after restoring permissions)\n"
76 "notes: measures mprotect()\n",
79 (void) sprintf(lm_header
, "%8s %5s", "size", "flags");
85 benchmark_optswitch(int opt
, char *optarg
)
90 anon
= strcmp(optf
, "MAP_ANON") == 0;
93 optl
= sizetoll(optarg
);
120 fd
= open(optf
, O_RDWR
);
122 flags
= opts
? MAP_SHARED
: MAP_PRIVATE
;
123 flags
|= anon
? MAP_ANON
: 0;
125 seg
= (vchar_t
*)mmap(NULL
, lm_optB
* optl
, PROT_READ
| PROT_WRITE
,
126 flags
, anon
? -1 : fd
, 0L);
128 if (seg
== MAP_FAILED
) {
133 for (i
= 0; i
< lm_optB
* optl
; i
+= 4096) {
139 for (i
= 0; i
< lm_optB
* optl
; i
+= 4096) {
144 pagesize
= getpagesize();
150 benchmark(void *tsd
, result_t
*res
)
152 tsd_t
*ts
= (tsd_t
*)tsd
;
155 int prot
= PROT_NONE
;
158 #if !defined(__APPLE__)
159 us
= (getpindex() * lm_optT
) + gettindex();
161 us
= gettsdindex(tsd
);
162 #endif /* __APPLE__ */
164 for (i
= 0; i
< lm_optB
; i
++) {
165 switch ((us
+ ts
->ts_batch
+ i
) % 2) {
169 for (j
= k
= 0; j
< optl
; j
+= pagesize
)
170 k
+= seg
[i
* optl
+ j
];
175 prot
= PROT_READ
| PROT_WRITE
;
179 if (mprotect((void *)&seg
[i
* optl
], optl
, prot
) == -1) {
183 res
->re_count
+= lm_optB
;
192 static char result
[256];
195 flags
[0] = anon
? 'a' : '-';
196 flags
[1] = optr
? 'r' : '-';
197 flags
[2] = optw
? 'w' : '-';
198 flags
[3] = opts
? 's' : '-';
199 flags
[4] = optt
? 't' : '-';
202 (void) sprintf(result
, "%8lld %5s", optl
, flags
);