1 /*
2  * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31 
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <sys/mman.h>
35 #include <sys/syscall.h>
36 
37 #if defined __NR_get_robust_list && defined __NR_set_robust_list
38 
39 int
main(void)40 main(void)
41 {
42 	const size_t page_len = sysconf(_SC_PAGESIZE);
43 	const pid_t pid = getpid();
44 	const long long_pid = (unsigned long) (0xdeadbeef00000000LL | pid);
45 
46 	void *p = mmap(NULL, page_len * 4, PROT_READ | PROT_WRITE,
47 		       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
48 	if (p == MAP_FAILED ||
49 	    mprotect(p + page_len, page_len, PROT_NONE) ||
50 	    mprotect(p + page_len * 3, page_len, PROT_NONE))
51 		return 77;
52 
53 	void **p_head = p + page_len - sizeof(void *);
54 	size_t *p_len = p + page_len * 3 - sizeof(size_t);
55 
56 	if (syscall(__NR_get_robust_list, long_pid, p_head, p_len))
57 		return 77;
58 	printf("get_robust_list(%d, [%#lx], [%lu]) = 0\n",
59 	       (int) pid, (unsigned long) *p_head, (unsigned long) *p_len);
60 
61 	if (syscall(__NR_set_robust_list, p, *p_len))
62 		return 77;
63 	printf("set_robust_list(%#lx, %lu) = 0\n",
64 	       (unsigned long) p, (unsigned long) *p_len);
65 
66 	if (syscall(__NR_get_robust_list, long_pid, p_head, p_len))
67 		return 77;
68 	printf("get_robust_list(%d, [%#lx], [%lu]) = 0\n",
69 	       (int) pid, (unsigned long) *p_head, (unsigned long) *p_len);
70 
71 	puts("+++ exited with 0 +++");
72 	return 0;
73 }
74 
75 #else
76 
77 int
main(void)78 main(void)
79 {
80 	return 77;
81 }
82 
83 #endif
84