1 /*
2  * Check decoding of lookup_dcookie syscall.
3  *
4  * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
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 #ifdef __NR_lookup_dcookie
35 
36 # include <inttypes.h>
37 # include <limits.h>
38 # include <stdio.h>
39 # include <unistd.h>
40 
41 static void
42 do_lookup_cookie(uint64_t cookie, char *buf, kernel_ulong_t len)
43 {
44 	long rc;
45 	const char *errstr;
46 
47 # if (LONG_MAX > INT_MAX) \
48   || (defined __x86_64__ && defined __ILP32__) \
49   || defined LINUX_MIPSN32
50 	rc = syscall(__NR_lookup_dcookie, cookie, buf, len);
51 # else
52 	rc = syscall(__NR_lookup_dcookie, LL_VAL_TO_PAIR(cookie), buf, len);
53 # endif
54 
55 	errstr = sprintrc(rc);
56 	printf("lookup_dcookie(%" PRIu64 ", ", cookie);
57 
58 	/* Here, we trust successful return code */
59 	if ((rc >= 0) && (rc < (long) INT_MAX)) {
60 		printf("%.*s, ", (int) rc, buf);
61 	} else {
62 		if (buf != NULL)
63 			printf("%p, ", buf);
64 		else
65 			printf("NULL, ");
66 	}
67 
68 	printf("%" PRIu64 ") = %s\n", (uint64_t) len, errstr);
69 }
70 
71 int
72 main(void)
73 {
74 	enum { BUF_SIZE = 4096 };
75 
76 	static const uint64_t bogus_cookie =
77 		(uint64_t) 0xf157feeddeadfaceULL;
78 	static const kernel_ulong_t bogus_len =
79 		(kernel_ulong_t) 0xbadc0dedda7a1057ULL;
80 
81 	char *buf = tail_alloc(BUF_SIZE);
82 
83 	do_lookup_cookie(0, NULL, 0);
84 	do_lookup_cookie(bogus_cookie, buf + BUF_SIZE, bogus_len);
85 	do_lookup_cookie(bogus_cookie, buf, BUF_SIZE);
86 
87 	puts("+++ exited with 0 +++");
88 
89 	return 0;
90 }
91 
92 #else
93 
94 SKIP_MAIN_UNDEFINED("__NR_lookup_dcookie");
95 
96 #endif
97