Debian Codegraph @

testsuite/systemtap.syscall/clone.c

raw
1 /* COVERAGE: clone wait4 */
2 #define _GNU_SOURCE
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sched.h>
6 #include <sys/wait.h>
7 #include <unistd.h>
8 #include <sys/syscall.h>
9
10 static int child_fn(void *arg)
11 {
12 exit(0);
13 }
14
15 #define STACK_SIZE (1024 * 1024) /* Stack size for cloned child */
16
17 /* For limit testing, we need the raw syscalls. */
18 #if defined(__ia64__) && defined(SYS_clone2)
19 static inline int
20 __sys_clone2(int flags, void *ustack_base, size_t ustack_size,
21 void *parent_tidptr, void *child_tidptr, void *tls)
22 {
23 return syscall(SYS_clone2, flags, ustack_base, ustack_size, parent_tidptr,
24 child_tidptr, tls);
25 }
26 #endif
27
28 #ifdef SYS_clone
29 // The main kernel config file says that clone comes from the "ABI
30 // hall of shame". Here's why. clone differs in the order of arguments
31 // between architectures. Here's the kernel code:
32 //
33 // #ifdef CONFIG_CLONE_BACKWARDS
34 // SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
35 // int __user *, parent_tidptr,
36 // int, tls_val,
37 // int __user *, child_tidptr)
38 // #elif defined(CONFIG_CLONE_BACKWARDS2)
39 // SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,
40 // int __user *, parent_tidptr,
41 // int __user *, child_tidptr,
42 // int, tls_val)
43 // #elif defined(CONFIG_CLONE_BACKWARDS3)
44 // SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,
45 // int, stack_size,
46 // int __user *, parent_tidptr,
47 // int __user *, child_tidptr,
48 // int, tls_val)
49 // #else
50 // SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
51 // int __user *, parent_tidptr,
52 // int __user *, child_tidptr,
53 // int, tls_val)
54 // #endif
55 //
56 // Note that systemtap actually probes do_fork(), which doesn't get
57 // the tls_val.
58
59 #if defined(__s390__) || defined(__s390x__)
60 #define CLONE_BACKWARDS2
61 #endif
62 #if defined(__powerpc__) || defined(__powerpc64__) \
63 || defined(__arm__) || defined(__aarch64__) \
64 || defined(__i386__)
65 #define CLONE_BACKWARDS
66 #endif
67
68 static inline long
69 __clone(unsigned long flags, void *child_stack, void *ptid, void *ctid,
70 int tls_val)
71 {
72 #ifdef CLONE_BACKWARDS
73 return syscall(SYS_clone, flags, child_stack, ptid, tls_val, ctid);
74 #elif defined(CLONE_BACKWARDS2)
75 return syscall(SYS_clone, child_stack, flags, ptid, ctid, tls_val);
76 #elif defined(CLONE_BACKWARDS3)
77 return syscall(SYS_clone, flags, child_stack, 0, ptid, ctid, tls_val);
78 #else
79 return syscall(SYS_clone, flags, child_stack, ptid, ctid, tls_val);
80 #endif
81 }
82 #endif
83
84 int
85 main()
86 {
87 char *stack;
88 char *stackTop;
89 pid_t pid;
90
91 stack = malloc(STACK_SIZE);
92 stackTop = stack + STACK_SIZE; /* Assume stack grows downward */
93
94 #if defined(__ia64__)
95 pid = __clone2(child_fn, stack, STACK_SIZE, SIGCHLD);
96 //staptest// clone2 (0x0|SIGCHLD, XXXX, 0x100000, XXXX, XXXX) = NNNN
97 #else
98 pid = clone(child_fn, stackTop, SIGCHLD, NULL);
99 //staptest// clone (0x0|SIGCHLD, XXXX, XXXX, XXXX) = NNNN
100 #endif
101 wait4(pid, NULL, 0, NULL); /* Wait for child */
102 //staptest// wait4 (NNNN, 0x0, 0x0, 0x0) = NNNN
103
104 #if defined(__ia64__)
105 pid = __clone2(child_fn, stack, STACK_SIZE, SIGCHLD);
106 //staptest// clone2 (0x0|SIGCHLD, XXXX, 0x100000, XXXX, XXXX) = NNNN
107 #else
108 pid = clone(child_fn, stackTop, SIGCHLD, NULL);
109 //staptest// clone (0x0|SIGCHLD, XXXX, XXXX, XXXX) = NNNN
110 #endif
111 wait4(pid, NULL, 0, NULL); /* Wait for child */
112 //staptest// wait4 (NNNN, 0x0, 0x0, 0x0) = NNNN
113
114 /* Limit testing. BTW, we want all these calls to fail, since we
115 * aren't cleaning up the child processes. */
116
117 #if defined(__ia64__) && defined(SYS_clone2)
118 // Keep sending an invalid flag value so all calls will fail.
119 pid = __sys_clone2(-1, NULL, 0, NULL, NULL, NULL);
120 //staptest// clone2 (CLONE_[^ ]+|XXXX, 0x0, 0x0, 0x0, 0x0) = -NNNN
121
122 pid = __sys_clone2(-1, (void *)-1, 0, NULL, NULL, NULL);
123 //staptest// clone2 (CLONE_[^ ]+|XXXX, 0x[f]+, 0x0, 0x0, 0x0) = -NNNN
124
125 pid = __sys_clone2(-1, NULL, -1, NULL, NULL, NULL);
126 //staptest// clone2 (CLONE_[^ ]+|XXXX, 0x0, 0x[f]+, 0x0, 0x0) = -NNNN
127
128 pid = __sys_clone2(-1, NULL, 0, (void *)-1, NULL, NULL);
129 //staptest// clone2 (CLONE_[^ ]+|XXXX, 0x0, 0x0, 0x[f]+, 0x0) = -NNNN
130
131 pid = __sys_clone2(-1, NULL, 0, NULL, (void *)-1, NULL);
132 //staptest// clone2 (CLONE_[^ ]+|XXXX, 0x0, 0x0, 0x0, 0x[f]+) = -NNNN
133 #endif
134
135 #ifdef SYS_clone
136 // We've got a bit of a problem here. On RHEL6, if you don't
137 // pass a stack pointer, the kernel uses a register value as a
138 // stack pointer. So, we'll pass an invalid value (1) for a stack
139 // pointer. Sigh, except that rawhide
140 // (4.0.0-0.rc3.git0.1.fc23.x86_64) needs a real stack pointer or
141 // this test program will crash.
142
143 pid = __clone((unsigned long)-1, stackTop, NULL, NULL, 0);
144 // On a 32-bit kernel, the full clone flags will overflow
145 // MAXSTRINGLEN.
146 //staptest// clone (CLONE_[^ ]+[[[[ ?!!!!|XXXX, XXXX, 0x0, 0x0]]]]?) = -NNNN
147
148 // Keep sending an invalid flag value so all calls will fail. But,
149 // sending a -1 will cause all the flags to be set, overflowing
150 // MAXSTRINGLEN. So, we'll use a different invalid flag value. You
151 // can't specify CLONE_NEWNS (new mount namespace group) and CLONE_FS
152 // (share filesystem info between processes) together.
153
154 // Unfortunately, on 4.0.0-0.rc3.git0.1.fc23.x86_64 passing -1 for
155 // a stack pointer causes the test executable to get a
156 // segmentation fault. So, we'll skip this one.
157 //
158 // pid = __clone(CLONE_FS|CLONE_NEWNS, (void *)-1, NULL, NULL, 0);
159
160 pid = __clone(CLONE_FS|CLONE_NEWNS, stackTop, (void *)-1, NULL, 0);
161 #ifdef __s390__
162 //staptest// clone (CLONE_FS|CLONE_NEWNS, XXXX, 0x[7]?[f]+, 0x0) = -NNNN
163 #else
164 //staptest// clone (CLONE_FS|CLONE_NEWNS, XXXX, 0x[f]+, 0x0) = -NNNN
165 #endif
166
167 pid = __clone(CLONE_FS|CLONE_NEWNS, stackTop, NULL, (void *)-1, 0);
168 #ifdef __s390__
169 //staptest// clone (CLONE_FS|CLONE_NEWNS, XXXX, 0x0, 0x[7]?[f]+) = -NNNN
170 #else
171 //staptest// clone (CLONE_FS|CLONE_NEWNS, XXXX, 0x0, 0x[f]+) = -NNNN
172 #endif
173 #endif
174
175 return 0;
176 }