1 /*
2 * Check decoding of set_mempolicy syscall.
3 *
4 * Copyright (c) 2016 Dmitry V. Levin <ldv@altlinux.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "tests.h"
31 #include <asm/unistd.h>
32
33 #ifdef __NR_set_mempolicy
34
35 # include <errno.h>
36 # include <stdio.h>
37 # include <string.h>
38 # include <unistd.h>
39
40 # include "xlat.h"
41 # include "xlat/policies.h"
42
43 # define MAX_STRLEN 3
44 # define NLONGS(n) ((n + 8 * sizeof(long) - 2) \
45 / (8 * sizeof(long)))
46
47 static void
print_nodes(const unsigned long maxnode,unsigned int offset)48 print_nodes(const unsigned long maxnode, unsigned int offset)
49 {
50 unsigned int nlongs = NLONGS(maxnode);
51 if (nlongs <= offset)
52 nlongs = 0;
53 else
54 nlongs -= offset;
55 const unsigned int size = nlongs * sizeof(long);
56 unsigned long *const nodemask =
57 tail_alloc(size ? size : (offset ? 1 : 0));
58 memset(nodemask, 0, size);
59
60 long rc = syscall(__NR_set_mempolicy, 0, nodemask, maxnode);
61 const char *errstr = sprintrc(rc);
62
63 fputs("set_mempolicy(MPOL_DEFAULT, ", stdout);
64
65 if (nlongs) {
66 putc('[', stdout);
67 unsigned int i;
68 for (i = 0; i < nlongs + offset; ++i) {
69 if (i)
70 fputs(", ", stdout);
71 if (i < nlongs) {
72 if (i >= MAX_STRLEN) {
73 fputs("...", stdout);
74 break;
75 }
76 printf("%#0*lx", (int) sizeof(long) * 2 + 2,
77 nodemask[i]);
78 } else {
79 printf("%p", nodemask + i);
80 break;
81 }
82 }
83 putc(']', stdout);
84 } else {
85 if (maxnode)
86 printf("%p", nodemask);
87 else
88 printf("[]");
89 }
90
91 printf(", %lu) = %s\n", maxnode, errstr);
92 }
93
94 static void
test_offset(const unsigned int offset)95 test_offset(const unsigned int offset)
96 {
97 unsigned long maxnode = get_page_size() * 8;
98
99 print_nodes(maxnode, offset);
100 print_nodes(maxnode + 1, offset);
101 print_nodes(maxnode + 2, offset);
102
103 maxnode = sizeof(long) * 8;
104 print_nodes(0, offset);
105 print_nodes(1, offset);
106 print_nodes(2, offset);
107 print_nodes(maxnode - 1, offset);
108 print_nodes(maxnode , offset);
109 print_nodes(maxnode + 1, offset);
110 print_nodes(maxnode + 2, offset);
111 print_nodes(maxnode * 2 - 1, offset);
112 print_nodes(maxnode * 2 , offset);
113 print_nodes(maxnode * 2 + 1, offset);
114 print_nodes(maxnode * 2 + 2, offset);
115 print_nodes(maxnode * 3 - 1, offset);
116 print_nodes(maxnode * 3 , offset);
117 print_nodes(maxnode * 3 + 1, offset);
118 print_nodes(maxnode * 3 + 2, offset);
119 print_nodes(maxnode * 4 + 2, offset);
120 }
121
122 int
main(void)123 main(void)
124 {
125 if (syscall(__NR_set_mempolicy, 0, 0, 0))
126 perror_msg_and_skip("set_mempolicy");
127 puts("set_mempolicy(MPOL_DEFAULT, NULL, 0) = 0");
128
129 const unsigned long *nodemask = (void *) 0xfacefeedfffffffeULL;
130 const unsigned long maxnode = (unsigned long) 0xcafef00dbadc0dedULL;
131 long rc = syscall(__NR_set_mempolicy, 1, nodemask, maxnode);
132 printf("set_mempolicy(MPOL_PREFERRED, %p, %lu) = %s\n",
133 nodemask, maxnode, sprintrc(rc));
134
135 test_offset(0);
136 test_offset(1);
137
138 puts("+++ exited with 0 +++");
139 return 0;
140 }
141
142 #else
143
144 SKIP_MAIN_UNDEFINED("__NR_set_mempolicy")
145
146 #endif
147