xref: /openbsd-src/regress/sys/uvm/mmap_hint/mmap_hint.c (revision c4613df5e092f3e6f7b9527332fada6091df9c67)
1 /*	$OpenBSD: mmap_hint.c,v 1.3 2017/07/07 10:49:12 bluhm Exp $	*/
2 /*
3  * Copyright (c) 2011 Ariane van der Steldt <ariane@stack.nl>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/types.h>
20 #include <sys/mman.h>
21 #include <sys/tree.h>
22 #include <machine/pmap.h>
23 #include <machine/vmparam.h>
24 #include <err.h>
25 #include <sysexits.h>
26 #include <stdio.h>
27 
28 #define MAX_HINT_DIST	(2UL * 1024 * 1024 * 1024)
29 
30 int	errors = 0;
31 
32 void *
33 mmap_hint(void *hint)
34 {
35 	void	*p;
36 	size_t	 pd;
37 
38 	p = mmap(hint, 1, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
39 	if (p == MAP_FAILED) {
40 		warn("mmap(addr=%p, len=1) failed", hint);
41 		errors++;
42 		return MAP_FAILED;
43 	} else if (p == NULL) {
44 		warnx("mmap(addr=%p, len=1) mapped at address 0", hint);
45 		errors++;
46 		return MAP_FAILED;
47 	} else
48 		fprintf(stderr, "    -> %p\n", p);
49 
50 	if (hint > p)
51 		pd = hint - p;
52 	else
53 		pd = p - hint;
54 
55 	if (hint != NULL && pd > MAX_HINT_DIST) {
56 		warnx("hinted allocation more than 0x%lx "
57 		    "bytes away from hint: "
58 		    "hint %p, result %p", pd, hint, p);
59 		errors++;
60 		return MAP_FAILED;
61 	}
62 	return p;
63 }
64 
65 int
66 main()
67 {
68 	void	*p, *p_prev;
69 
70 	/* Check that unhinted allocation works properly. */
71 	fprintf(stderr, "1: Checking hint NULL mmap\n");
72 	p = mmap_hint(NULL);
73 
74 	/* Check hinted allocation at top of map. */
75 	fprintf(stderr, "2: Checking hint VM_MAXUSER_ADDRESS 0x%lx mmap\n",
76 	    (unsigned long)VM_MAXUSER_ADDRESS);
77 	p = mmap_hint((void*)VM_MAXUSER_ADDRESS);
78 
79 	/* Check hinted allocation at bottom of map. */
80 	fprintf(stderr, "3: Checking hint VM_MIN_ADDRESS 0x%lx mmap\n",
81 	    (unsigned long)VM_MIN_ADDRESS);
82 	p = mmap_hint((void*)VM_MIN_ADDRESS);
83 
84 	/*
85 	 * Check that hinted allocation doesn't overwrite existing allocation.
86 	 */
87 	if (p == MAP_FAILED) {
88 		fprintf(stderr, "4: Skipping test: required previous test "
89 		    "to succeed");
90 		goto skip4;
91 	}
92 	p_prev = p;
93 	fprintf(stderr, "4: Checking hint 0x%lx mmap, which is in use\n",
94 	    p_prev);
95 	p = mmap_hint(p_prev);
96 	if (p == p_prev) {
97 		warnx("hinted allocation %p "
98 		    "overwrites previous allocation", p_prev);
99 		errors++;
100 	}
101 skip4:
102 
103 	return errors;
104 }
105