Debian Codegraph @

testsuite/systemtap.unprivileged/unprivileged_myproc.exp

raw
1 set test "unprivileged myproc"
2 if {![installtest_p]} { untested $test; return }
3 if {![uprobes_p]} { untested $test; return }
4
5 set exepath "loop"
6 set sopath "libloop.so"
7
8 # These probe types should fire unprivileged users, but only within their own processes.
9 set myproc_probe_types [list \
10 "process.begin" \
11 "process.end" \
12 "process.function(string)" \
13 "process.function(number)" \
14 "process.function(number).call" \
15 "process.function(number).inline" \
16 "process.function(number).return" \
17 "process.function(number).exported" \
18 "process.function(string).call" \
19 "process.function(string).inline" \
20 "process.function(string).label(string)" \
21 "process.function(string).return" \
22 "process.function(string).exported" \
23 "process.function(string).callee(string)" \
24 "process.function(string).callee(string).call" \
25 "process.function(string).callee(string).return" \
26 "process.function(string).callees" \
27 "process.function(string).callees(number)" \
28 "process.plt" \
29 "process.plt.return" \
30 "process.plt(string)" \
31 "process.plt(string).return" \
32 "process.library(string).function(number)" \
33 "process.library(string).function(number).call" \
34 "process.library(string).function(number).inline" \
35 "process.library(string).function(number).return" \
36 "process.library(string).function(number).exported" \
37 "process.library(string).function(string)" \
38 "process.library(string).function(string).call" \
39 "process.library(string).function(string).inline" \
40 "process.library(string).function(string).return" \
41 "process.library(string).function(string).exported" \
42 "process.library(string).function(string).label(string)" \
43 "process.library(string).function(string).callee(string)" \
44 "process.library(string).function(string).callee(string).call" \
45 "process.library(string).function(string).callee(string).return" \
46 "process.library(string).function(string).callees" \
47 "process.library(string).function(string).callees(number)" \
48 "process.library(string).plt" \
49 "process.library(string).plt.return" \
50 "process.library(string).plt(string)" \
51 "process.library(string).plt(string).return" \
52 "process.library(string).mark(string)" \
53 "process.library(string).provider(string).mark(string)" \
54 "process.library(string).statement(number)" \
55 "process.library(string).statement(number).nearest" \
56 "process.library(string).statement(string)" \
57 "process.library(string).statement(string).nearest" \
58 "process.provider(string).mark(string)" \
59 "process.mark(string)" \
60 "process.statement(number)" \
61 "process.statement(number).nearest" \
62 "process.statement(string)" \
63 "process.statement(string).nearest" \
64 "process.syscall" \
65 "process.syscall.return" \
66 "process.thread.begin" \
67 "process.thread.end" \
68 "process(number).begin" \
69 "process(number).end" \
70 "process(number).statement(number).absolute" \
71 "process(number).statement(number).absolute.return" \
72 "process(number).syscall" \
73 "process(number).syscall.return" \
74 "process(number).thread.begin" \
75 "process(number).thread.end" \
76 "process(number).function(number)" \
77 "process(number).function(number).call" \
78 "process(number).function(number).exported" \
79 "process(number).function(number).return" \
80 "process(number).function(string)" \
81 "process(number).function(string).call" \
82 "process(number).function(string).callee(string)" \
83 "process(number).function(string).callee(string).call" \
84 "process(number).function(string).callee(string).return" \
85 "process(number).function(string).callees" \
86 "process(number).function(string).callees(number)" \
87 "process(number).function(string).exported" \
88 "process(number).function(string).inline" \
89 "process(number).function(string).label(string)" \
90 "process(number).function(string).return" \
91 "process(number).mark(string)" \
92 "process(number).plt" \
93 "process(number).plt.return" \
94 "process(number).plt(string)" \
95 "process(number).plt(string).return" \
96 "process(number).provider(string).mark(string)" \
97 "process(number).statement(number)" \
98 "process(number).statement(number).nearest" \
99 "process(number).statement(string)" \
100 "process(number).statement(string).nearest" \
101 "process(string).begin" \
102 "process(string).end" \
103 "process(string).function(number)" \
104 "process(string).function(number).call" \
105 "process(string).function(number).inline" \
106 "process(string).function(number).return" \
107 "process(string).function(number).exported" \
108 "process(string).function(string)" \
109 "process(string).function(string).call" \
110 "process(string).function(string).inline" \
111 "process(string).function(string).return" \
112 "process(string).function(string).exported" \
113 "process(string).function(string).callee(string)" \
114 "process(string).function(string).callee(string).call" \
115 "process(string).function(string).callee(string).return" \
116 "process(string).function(string).callees" \
117 "process(string).function(string).callees(number)" \
118 "process(string).library(string).function(number)" \
119 "process(string).library(string).function(number).call" \
120 "process(string).library(string).function(number).inline" \
121 "process(string).library(string).function(number).return" \
122 "process(string).library(string).function(number).exported" \
123 "process(string).library(string).function(string)" \
124 "process(string).library(string).function(string).call" \
125 "process(string).library(string).function(string).inline" \
126 "process(string).library(string).function(string).return" \
127 "process(string).library(string).function(string).exported" \
128 "process(string).library(string).function(string).label(string)" \
129 "process(string).library(string).function(string).callee(string)" \
130 "process(string).library(string).function(string).callee(string).call" \
131 "process(string).library(string).function(string).callee(string).return" \
132 "process(string).library(string).function(string).callees" \
133 "process(string).library(string).function(string).callees(number)" \
134 "process(string).library(string).mark(string)" \
135 "process(string).library(string).provider(string).mark(string)" \
136 "process(string).library(string).statement(number)" \
137 "process(string).library(string).statement(number).nearest" \
138 "process(string).library(string).statement(string)" \
139 "process(string).library(string).statement(string).nearest" \
140 "process(string).library(string).plt" \
141 "process(string).library(string).plt.return" \
142 "process(string).library(string).plt(string)" \
143 "process(string).library(string).plt(string).return" \
144 "process(string).mark(string)" \
145 "process(string).provider(string).mark(string)" \
146 "process(string).statement(number)" \
147 "process(string).statement(number).nearest" \
148 "process(string).statement(string)" \
149 "process(string).statement(string).nearest" \
150 "process(string).plt" \
151 "process(string).plt.return" \
152 "process(string).plt(string)" \
153 "process(string).plt(string).return" \
154 "process(string).syscall" \
155 "process(string).syscall.return" \
156 "process(string).thread.begin" \
157 "process(string).thread.end" \
158 "process(string).function(string).label(string)" \
159 ]
160
161 # Add arguments to a probe type template.
162 proc add_args { probe_type pid } {
163 global exepath sopath
164 global addr_of_bar addr_of_libloopfunc
165
166 set probe "$probe_type"
167
168 # Replace "hz(number)" with "hz(1000)"
169 regsub -all "hz\\(number\\)" $probe "hz(1000)" probe
170 # Replace "ns(number)" with "ns(100000)"
171 regsub -all "ns\\(number\\)" $probe "ns(100000)" probe
172 # Replace "nsec(number)" with "nsec(100000)"
173 regsub -all "nsec\\(number\\)" $probe "nsec(100000)" probe
174 # Replace "us(number)" with "us(100)"
175 regsub -all "us\\(number\\)" $probe "us(100)" probe
176 # Replace "usec(number)" with "usec(100)"
177 regsub -all "usec\\(number\\)" $probe "usec(100)" probe
178 # Replace "process(number)" with "process($pid)"
179 regsub -all "process\\(number\\)" $probe "process($pid)" probe
180
181 # Replace "library(string).function(string).inline" with "library(string).function("ilibloopfunc").inline"
182 regsub -all "library\\(string\\).function\\(string\\).inline" $probe "library(string).function(\"ilibloopfunc\").inline" probe
183
184 # Replace "function(string).inline" with "function("ibar").inline"
185 regsub -all "function\\(string\\).inline" $probe "function(\"ibar\").inline" probe
186
187 # Replace 'library(string).function(string).callee(string)' with
188 # 'library(string).function("libloopfun").callee("ilibloopfunc")'
189 regsub -all "library\\(string\\).function\\(string\\).callee\\(string\\)" \
190 $probe \
191 "library(string).function(\"libloopfunc\").callee(\"ilibloopfunc\")" \
192 probe
193 # Replace 'library(string).function(string).callees' with
194 # 'library(string).function("libloopfunc").callees'
195 regsub -all "library\\(string\\).function\\(string\\).callees" $probe \
196 "library(string).function(\"libloopfunc\").callees" probe
197
198 # Replace 'function(string).callee(string)' with
199 # 'function("main").callee("tbar")'
200 regsub -all "function\\(string\\).callee\\(string\\)" $probe \
201 "function(\"main\").callee(\"tbar\")" probe
202 # Replace 'function(string).callees' with 'function("main").callees'
203 regsub -all "function\\(string\\).callees" $probe \
204 "function(\"main\").callees" probe
205
206 # Replace 'callees(number)' with 'callees(1)'
207 regsub -all "callees\\(number\\)" $probe "callees(1)" probe
208
209 # Replace "library(string).function(string)" with "library(string.function("libloopfunc")"
210 regsub -all "library\\(string\\).function\\(string\\)" $probe "library(string).function(\"libloopfunc\")" probe
211 # Replace "library(string).function(number)" with "library(string).function($addr_of_libloopfunc)"
212 regsub -all "library\\(string\\).function\\(number\\)" $probe "library(string).function($addr_of_libloopfunc)" probe
213
214 # Replace "function(string)" with "function("bar")"
215 regsub -all "function\\(string\\)" $probe "function(\"bar\")" probe
216 # Replace "function(number)" with "function($addr_of_bar)"
217 regsub -all "function\\(number\\)" $probe "function($addr_of_bar)" probe
218
219 # Replace "label(string)" with "label("a")"
220 regsub -all "label\\(string\\)" $probe "label(\"a\")" probe
221
222 # Replace "library(string).mark(string)" with "library(string).mark("libloopfunc_enter")"
223 regsub -all "library\\(string\\).mark\\(string\\)" $probe "library(string).mark(\"libloopfunc_enter\")" probe
224 # Replace "library(string).provider(string).mark(string)" with "library(string).provider(string).mark("libloopfunc_enter")"
225 regsub -all "library\\(string\\).provider\\(string\\).mark\\(string\\)" $probe "library(string).provider(string).mark(\"libloopfunc_enter\")" probe
226 # Replace "mark(string)" with "mark("main_enter")"
227 regsub -all "mark\\(string\\)" $probe "mark(\"main_enter\")" probe
228
229 # Replace "library(string).statement(number)" with "library(string).statement($addr_of_libloopfunc)"
230 regsub -all "library\\(string\\).statement\\(number\\)" $probe "library(string).statement($addr_of_libloopfunc)" probe
231 # Replace "library(string).statement(string)" with "library(string).statement("[email protected]:*")"
232 regsub -all "library\\(string\\).statement\\(string\\)" $probe "library(string).statement(\"[email protected]:*\")" probe
233
234 # Replace "statement(number)" with "statement($addr_of_bar)"
235 regsub -all "statement\\(number\\)" $probe "statement($addr_of_bar)" probe
236 # Replace "statement(string)" with "statement("[email protected]:*")"
237 regsub -all "statement\\(string\\)" $probe "statement(\"[email protected]:*\")" probe
238
239 # Replace "library(string)" with "library("$sopath")"
240 regsub -all "library\\(string\\)" $probe "library(\"$sopath\")" probe
241
242 # Replace "provider(string)" with "provider("_test_")"
243 regsub -all "provider\\(string\\)" $probe "provider(\"_test_\")" probe
244
245 # Replace plt with plt("*") (all shlib calls)
246 regsub -all "plt\\(string\\)" $probe "plt(\"*\")" probe
247
248 # Replace "(number)" with "(10)"
249 regsub -all "\\(number\\)" $probe "(10)" probe
250 # Replace "(string)" with "(\"$exepath\")"
251 regsub -all "\\(string\\)" $probe "(\"./$exepath\")" probe
252
253 return "$probe"
254 }
255
256 # Add required extra options according to the probe type.
257 proc extra_options { probe_type } {
258 global exepath
259
260 # Add '-c $exepath 1' for process.* probes.
261 if {[regexp "^process\\." $probe_type]} {
262 return "-c \"./$exepath 1\""
263 }
264
265 return ""
266 }
267
268 # Test valid probe types
269 proc test_probes { privilege } {
270 global myproc_probe_types allowed test srcdir subdir tested
271
272 set effective_uid [exec /usr/bin/id -u]
273 set stap [exec /usr/bin/which stap]
274
275 # Start the test executable in the background
276 set our_pid [exec ./loop &]
277 verbose -log "our ./loop pid is $our_pid"
278
279 # If we are not root start another one as root. It will be a failure if any probes fail against
280 # this instance.
281 if {$effective_uid != 0} {
282 # ask for password here, if required ...
283 as_root { true }
284 # trigger PR16406 scenario if possible
285 as_root { /sbin/modprobe loop }
286 # ... and not here.
287 exec sudo ./loop &
288 # Give it a chance to start
289 sleep 1
290 set root_pid [exec ps -fe | awk {/^root.*[^o] \.\/loop/ { print $2 }}]
291 verbose -log "root's ./loop pid is '$root_pid'"
292 }
293
294 # Test using --unprivileged and --privilege=stapusr. They should be equivalent
295 set stapusr_options [list --unprivileged --privilege=stapusr ]
296 # Test using no option and --privilege=stapdev. They should be equivalent
297 set stapdev_options [list "" --privilege=stapdev ]
298
299 set selector 0
300 foreach probe_type $myproc_probe_types {
301 # Select the proper --privilege option
302 switch $privilege {
303 stapusr {
304 set option [lindex $stapusr_options $selector]
305 }
306 stapdev {
307 set option [lindex $stapdev_options $selector]
308 }
309 default {
310 set option "--privilege=$privilege"
311 }
312 }
313
314 # There are currently some untestable probe types
315 if {[regexp "\.plt" $probe_type] && ! [plt_probes_p]} {
316 # .plt is only supported on certain arches
317 untested "$test: $option $probe_type"
318 set tested($probe_type) 1
319 continue
320 }
321
322 switch $probe_type {
323 process(number).thread.begin -
324 process(number).thread.end
325 {
326 # Impossible to predict the task_id of a thread
327 untested "$test: $option $probe_type"
328 set tested($probe_type) 1
329 continue
330 }
331 process.library(string).function(string).callee(string).return -
332 process(string).library(string).function(string).callee(string).return
333 {
334 untested "$test: $option $probe_type"
335 set tested($probe_type) 1
336 continue
337 }
338 }
339
340 # There are currently some known and expected failures
341 switch $probe_type {
342 process.library(string).function(number).inline -
343 process(string).library(string).function(number).inline -
344 process.function(number).inline -
345 process(string).function(number).inline
346 {
347 setup_kfail GCC *-*-*
348 }
349 process.function(string).callee(string) -
350 process.function(string).callee(string).call -
351 process.function(string).callee(string).return -
352 process(number).function(string).callee(string) -
353 process(number).function(string).callee(string).call -
354 process(number).function(string).callee(string).return -
355 process(string).function(string).callee(string) -
356 process(string).function(string).callee(string).call -
357 process(string).function(string).callee(string).return
358 {
359 if {![callee_probes_p]} {
360 setup_kfail CALLEE *-*-*
361 }
362 }
363 process.function(number).return -
364 process.function(string).return -
365 process.library(string).function(number).return -
366 process.library(string).function(string).return -
367 process(string).function(number).return -
368 process(string).function(string).return -
369 process(string).library(string).function(number).return -
370 process(string).library(string).function(string).return
371 {
372 if {![uretprobes_p]} { setup_kfail URETPROBES *-*-* }
373 }
374 process(number).statement(number).absolute
375 {
376 # Inode uprobes don't support 'absolute' probes.
377 if {[inode_uprobes_p]} { setup_kfail INODE_UPROBES *-*-* }
378 }
379 process(number).statement(number).absolute.return
380 {
381 # 'absolute.return' probes could fail because we're
382 # using inode uprobes or we don't have uretprobes.
383 if {[inode_uprobes_p]} {
384 setup_kfail INODE_UPROBES *-*-*
385 } elseif {![uretprobes_p]} {
386 setup_kfail URETPROBES *-*-*
387 }
388 }
389 }
390
391 # Test the probe type. If successful, the stap rc will be 0.
392 set probe [add_args $probe_type $our_pid]
393 set cmd [concat [list $stap $option -e "probe $probe { if (is_myproc ()) println (\"is myproc\") else println (\"not myproc\"); exit(); } probe timer.s(15) { println(\"timeout\") exit() }"] [extra_options $probe_type]]
394
395 # If the probe types was 'process(number).end' or 'process(string).end' then set up a
396 # delayed kill of our test program in order to make the probe fire.
397 if {[regexp "^process\\(number\\)\\.end" $probe_type] || \
398 [regexp "^process\\(string\\)\\.end" $probe_type]} {
399 exec $srcdir/$subdir/delayedkill $our_pid 10 &
400 }
401
402 # Run stap.
403 verbose -log "eval exec $cmd"
404 set rc [catch {eval exec $cmd} res_stap]
405 verbose -log "rc == $rc"
406 verbose -log "$res_stap"
407
408 if {$privilege == "stapusr"} {
409 # The test passes if it returns 0 and prints "is myproc".
410 if {$rc == 0 && [regexp ".*is myproc.*" "$res_stap"]} {
411 pass "$test: $option $probe_type"
412 } else {
413 fail "$test: $option $probe_type"
414 }
415 } else {
416 # The test passes if it returns 0
417 if {$rc == 0} {
418 pass "$test: $option $probe_type"
419 } else {
420 fail "$test: $option $probe_type"
421 }
422 }
423
424 # Indicate that this probe type has been tested
425 set tested($probe_type) 1
426
427 # If the probe types was 'process(number).end' or 'process(string).end', then our test
428 # program was killed in order to make the probe fire. Restart it.
429 if {[regexp "^process\\(number\\)\\.end" $probe_type] || \
430 [regexp "^process\\(string\\)\\.end" $probe_type]} {
431 set our_pid [exec ./loop &]
432 verbose -log "our ./loop pid is $our_pid"
433 }
434
435 set selector [expr ($selector + 1) % 2]
436
437 # This test suite runs for a long time. There will be an as_root command at the end. Keep
438 # sudo status alive in case it requires a password in order to avoid being prompted again
439 # for it.
440 as_root { true }
441 }
442
443 # Kill the the test executable(s)
444 kill -INT $our_pid 5
445 if {$effective_uid != 0} {
446 # Notice the 1 at the end of the 'kill' proc kills as root.
447 kill -INT $root_pid 5 1
448 }
449 }
450
451 # Need to build a user shared library.
452 set libflags [sdt_includes]
453 set libflags "$libflags additional_flags=-g"
454 set libflags "$libflags additional_flags=-O"
455 set libflags "$libflags additional_flags=-Wall"
456 set libflags "$libflags additional_flags=-Werror"
457 set libflags "$libflags additional_flags=-I."
458 set libflags "$libflags additional_flags=-shared"
459 set libflags "$libflags additional_flags=-fPIC"
460 set res [target_compile $srcdir/$subdir/libloop.c $sopath executable $libflags ]
461 if { $res == "" } {
462 pass "$test library compile"
463 } else {
464 fail "$test library compile: $res"
465 untested "$test Tests"
466 return
467 }
468
469 # Need to build a user application
470 set exeflags "additional_flags=-g"
471 set exeflags "$exeflags additional_flags=-O"
472 set exeflags "$exeflags additional_flags=-lpthread"
473 set exeflags "$exeflags [sdt_includes]"
474 set exeflags "$exeflags additional_flags=-Wl,-rpath,[pwd]"
475 set exeflags "$exeflags additional_flags=-L[pwd] additional_flags=-lloop"
476 set exeflags "$exeflags compiler=gcc"
477 # ppc64 needs a more restrictive constraint for the probe args
478 if {[regexp "^(x86_64|i.86)$" $::tcl_platform(machine)] == 0} {
479 set exeflags "$exeflags additional_flags=-DSTAP_SDT_ARG_CONSTRAINT=nr"
480 }
481 set res [target_compile $srcdir/$subdir/loop.c $exepath executable "$exeflags"]
482 if { $res == "" } {
483 pass "$test exe compile"
484 } else {
485 fail "$test exe compile: $res"
486 untested "$test Tests"
487 return
488 }
489
490 # Extract needed addresses. Note, some architectures add a '.' to the
491 # function name, so match that optionally.
492 set addr_of_bar [exec objdump -d $exepath | awk {/<\.?bar>:/ { printf "0x%s\n",$1 }}]
493 verbose -log "addr_of_bar: $addr_of_bar"
494 set addr_of_libloopfunc [exec objdump -d $sopath | awk {/<\.?libloopfunc>:/ { printf "0x%s\n",$1 }}]
495 verbose -log "addr_of_libloopfunc: $addr_of_libloopfunc"
496
497 # Obtain a list of all process* probe types from stap
498 verbose -log "eval exec stap --dump-probe-types | grep ^process"
499 catch {eval exec stap --dump-probe-types | grep ^process} res_stap
500 set all_probe_types [split $res_stap "\n"]
501
502 # Initialize each probe type to untested
503 foreach probe_type $all_probe_types {
504 verbose -log $probe_type
505 set tested($probe_type) 0
506 }
507 if {[llength all_probe_types] > 0} {
508 pass "$test: Obtain list of supported probe types"
509 } else {
510 fail "$test: Obtain list of supported probe types"
511 untested "$test Tests"
512 return
513 }
514
515 # Now run the tests
516 test_probes stapusr
517 test_probes stapsys
518 #test_probes stapdev
519
520 # Generate a failure for each untested probe type
521 foreach probe_type $all_probe_types {
522 set status $tested($probe_type)
523 if {$status != 0} {
524 pass "$test: tested: $probe_type"
525 } else {
526 fail "$test: not tested: $probe_type"
527 }
528 }
529
530 # Cleanup
531 catch {exec rm -f $exepath}
532 catch {exec rm -f $sopath}