Dmitry V. Levin | aa1ed76 | 2016-04-27 21:58:43 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Check decoding of get_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 <sys/syscall.h> |
| 32 | |
| 33 | #ifdef __NR_get_mempolicy |
| 34 | |
| 35 | # include <stdio.h> |
| 36 | # include <unistd.h> |
| 37 | |
| 38 | # include "xlat.h" |
| 39 | # include "xlat/policies.h" |
| 40 | |
| 41 | # define MAX_STRLEN 3 |
| 42 | # define NLONGS(n) ((n + 8 * sizeof(long) - 2) \ |
| 43 | / (8 * sizeof(long))) |
| 44 | |
| 45 | static void |
| 46 | print_nodes(unsigned long maxnode) |
| 47 | { |
| 48 | unsigned long *const nodemask = |
| 49 | tail_alloc(sizeof(*nodemask) * NLONGS(maxnode)); |
| 50 | (void) tail_alloc(1); |
| 51 | |
| 52 | if (syscall(__NR_get_mempolicy, 0, nodemask, maxnode, 0, 0)) { |
| 53 | printf("get_mempolicy(NULL, %p, %lu, NULL, 0) = -1 %s (%m)\n", |
| 54 | nodemask, maxnode, errno2name()); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | printf("get_mempolicy(NULL, ["); |
| 59 | |
| 60 | unsigned int nlongs = NLONGS(maxnode); |
| 61 | unsigned int i; |
| 62 | for (i = 0; i < nlongs; ++i) { |
| 63 | if (i) |
| 64 | fputs(", ", stdout); |
| 65 | if (i >= MAX_STRLEN) { |
| 66 | fputs("...", stdout); |
| 67 | break; |
| 68 | } |
| 69 | printf("%#0*lx", (int) sizeof(*nodemask) * 2 + 2, nodemask[i]); |
| 70 | } |
| 71 | |
| 72 | printf("], %lu, NULL, 0) = 0\n", maxnode); |
| 73 | } |
| 74 | |
| 75 | int |
| 76 | main(void) |
| 77 | { |
| 78 | long rc; |
| 79 | |
| 80 | if (syscall(__NR_get_mempolicy, 0, 0, 0, 0, 0)) |
| 81 | perror_msg_and_skip("get_mempolicy"); |
| 82 | puts("get_mempolicy(NULL, NULL, 0, NULL, 0) = 0"); |
| 83 | |
| 84 | int *mode = (void *) 0xdefaced1baddeed2; |
| 85 | unsigned long maxnode = (unsigned long) 0xcafef00dbadc0ded; |
| 86 | const unsigned long *nodemask = (void *) 0xfacedad3bebefed4; |
| 87 | const unsigned long addr = (unsigned long) 0xfacefeeddeadbeef; |
| 88 | const unsigned long flags = -1U; |
| 89 | rc = syscall(__NR_get_mempolicy, mode, nodemask, maxnode, addr, flags); |
| 90 | printf("get_mempolicy(%p, %p, %lu, %#lx, %s|%#lx) = %ld %s (%m)\n", |
| 91 | mode, nodemask, maxnode, addr, |
| 92 | "MPOL_F_NODE|MPOL_F_ADDR", |
| 93 | flags & ~3, rc, errno2name()); |
| 94 | |
| 95 | (void) tail_alloc(1); |
| 96 | mode = tail_alloc(sizeof(*mode)); |
| 97 | (void) tail_alloc(1); |
| 98 | |
| 99 | rc = syscall(__NR_get_mempolicy, mode, 0, 0, 0, 0); |
| 100 | printf("get_mempolicy(["); |
| 101 | printxval(policies, (unsigned) *mode, "MPOL_???"); |
| 102 | printf("], NULL, 0, NULL, 0) = %ld\n", rc); |
| 103 | |
| 104 | *mode = -1; |
| 105 | rc = syscall(__NR_get_mempolicy, mode, 0, 0, mode - 1, 2); |
| 106 | printf("get_mempolicy(["); |
| 107 | printxval(policies, (unsigned) *mode, "MPOL_???"); |
| 108 | printf("], NULL, 0, %p, MPOL_F_ADDR) = %ld\n", mode - 1, rc); |
| 109 | |
| 110 | maxnode = get_page_size() * 8; |
| 111 | |
| 112 | print_nodes(maxnode); |
| 113 | print_nodes(maxnode + 1); |
| 114 | print_nodes(maxnode + 2); |
| 115 | |
| 116 | maxnode = sizeof(*nodemask) * 8; |
| 117 | print_nodes(maxnode - 1); |
| 118 | print_nodes(maxnode ); |
| 119 | print_nodes(maxnode + 1); |
| 120 | print_nodes(maxnode + 2); |
| 121 | print_nodes(maxnode * 2 - 1); |
| 122 | print_nodes(maxnode * 2 ); |
| 123 | print_nodes(maxnode * 2 + 1); |
| 124 | print_nodes(maxnode * 2 + 2); |
| 125 | print_nodes(maxnode * 3 - 1); |
| 126 | print_nodes(maxnode * 3 ); |
| 127 | print_nodes(maxnode * 3 + 1); |
| 128 | print_nodes(maxnode * 3 + 2); |
| 129 | |
| 130 | puts("+++ exited with 0 +++"); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | #else |
| 135 | |
| 136 | SKIP_MAIN_UNDEFINED("__NR_get_mempolicy") |
| 137 | |
| 138 | #endif |