1 /* 2 * Check decoding of SPARC-specific kern_features syscall. 3 * 4 * Copyright (c) 2018 The strace developers. 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 32 #include <asm/unistd.h> 33 34 #include "raw_syscall.h" 35 #include "scno.h" 36 37 #if defined __NR_kern_features && defined raw_syscall_0 38 39 # include <errno.h> 40 # include <stdio.h> 41 # include <stdlib.h> 42 # include <unistd.h> 43 44 static void 45 test_kern_features(unsigned int num) 46 { 47 /* The expected return codes, as enforced by fault injection */ 48 static struct { 49 kernel_ulong_t ret; 50 const char *str; 51 } checks[] = { 52 { 0, NULL }, 53 { 1, "KERN_FEATURE_MIXED_MODE_STACK" }, 54 { 2, "0x2" }, 55 { 0x7ffffffe, "0x7ffffffe" }, 56 { 0x7fffffff, "KERN_FEATURE_MIXED_MODE_STACK|0x7ffffffe" }, 57 { (kernel_ulong_t) 0xbadc0deddeadfaceULL, 58 sizeof(kernel_ulong_t) == 8 ? 59 "0xbadc0deddeadface" : "0xdeadface" }, 60 { (kernel_ulong_t) -1ULL, 61 sizeof(kernel_ulong_t) == 8 ? 62 "KERN_FEATURE_MIXED_MODE_STACK|0xfffffffffffffffe" : 63 "KERN_FEATURE_MIXED_MODE_STACK|0xfffffffe" }, 64 }; 65 66 kernel_ulong_t err = 0; 67 kernel_ulong_t rc = raw_syscall_0(__NR_kern_features, &err); 68 69 if (err) { 70 errno = rc; 71 printf("kern_features() = %s\n", sprintrc(-1)); 72 return; 73 } 74 75 printf("kern_features() = %#llx", (unsigned long long) rc); 76 77 if (num < ARRAY_SIZE(checks)) { 78 if (rc != checks[num].ret) 79 error_msg_and_fail("Expected return value (%llx) " 80 "doesn't match expected one (%#llx)", 81 (unsigned long long) rc, 82 (unsigned long long) checks[num].ret); 83 84 if (checks[num].str) 85 printf(" (%s)", checks[num].str); 86 87 printf(" (INJECTED)"); 88 } else if (rc) { 89 printf(" ("); 90 91 if (rc & 1) 92 printf("KERN_FEATURE_MIXED_MODE_STACK"); 93 94 if (rc & ~1ULL) { 95 if (rc & 1) 96 printf("|"); 97 98 printf("%#llx", rc & ~1ULL); 99 } 100 101 printf(")"); 102 } 103 104 puts(""); 105 } 106 107 int 108 main(int ac, char **av) 109 { 110 test_kern_features(ac > 1 ? atoi(av[1]) : -1); 111 112 puts("+++ exited with 0 +++"); 113 return 0; 114 } 115 116 #else 117 118 SKIP_MAIN_UNDEFINED("__NR_kern_features && raw_syscall_0"); 119 120 #endif 121