Debian Codegraph @

testsuite/systemtap.unprivileged/unprivileged_embedded_C.exp

raw
1 set test "unprivileged embedded C"
2 set arch [exec uname -i]
3 # normalize arch; PR4186
4 set arch [normalize_arch $arch]
5
6 # Some functions are only "native" for certain kernel versions.
7 set kv [exec uname -r | grep -o -G {^[0-9]\+.[0-9]\+.[0-9]\+}]
8 set kvbits [split $kv .]
9 set k1 [lindex $kvbits 0]
10 set k2 [lindex $kvbits 1]
11 set k3 [lindex $kvbits 2]
12 verbose -log "kernel version $k1.$k2.$k3"
13
14 # Add arguments to a probe type template.
15 proc add_args { prototype } {
16 set call "$prototype"
17
18 # Replace long with 0
19 regsub -all "(\[(,]\[\[:space:]]*)long" $call "\\10" call
20
21 # Replace string with "0"
22 regsub -all "(\[(,]\[\[:space:]]*)string" $call "\\1\"0\"" call
23
24 return "$call"
25 }
26
27 # Test valid probe types
28 proc test_embedded_C { privilege test_fraction { transitive_test 0 } } {
29 global prototype_info test arch
30 global k1 k2 k3
31
32 set test "unprivileged embedded C: $privilege"
33
34 set emsg2 "semantic error: embedded expression may not be used when --privilege=stapusr is specified"
35
36 # Test using --unprivileged and --privilege=stapusr. They should
37 # be equivalent.
38 set stapusr_options [list --unprivileged --privilege=stapusr ]
39 # Test using no option and --privilege=stapdev. They should be equivalent
40 set stapdev_options [list "" --privilege=stapdev ]
41
42 set test_num 0
43 set selector 0
44 foreach info $prototype_info {
45 set test_num [expr $test_num + 1]
46 verbose -log "testing $info $privilege $test_num $selector"
47
48 # Select the proper --privilege option
49 switch $privilege {
50 stapusr {
51 set option [lindex $stapusr_options $selector]
52 }
53 stapdev {
54 set option [lindex $stapdev_options $selector]
55 }
56 default {
57 set option "--privilege=$privilege"
58 }
59 }
60
61 # Separate the prototype and its status.
62 set tokens [split $info ";"]
63 set prototype [lindex $tokens 0]
64 set status [lindex $tokens 1]
65
66 # Only test the specified fraction of the functions
67 if {[expr $test_num % $test_fraction] != 0} {
68 # untested "$test: $status: $option: $prototype"
69 continue
70 }
71
72 # Some function in earlier versions of systemtap were written
73 # in embedded-C, but later versions of systemtap had pure
74 # script versions. There is no point in testing these.
75 switch "$prototype" {
76 "probefunc ()"
77 {
78 untested "$test: $status: $option: $prototype (rewritten pure)"
79 continue
80 }
81 }
82
83 # Deprecated functions will be untested. The awk script that
84 # finds functions doesn't know about deprecated functions.
85 switch "$prototype" {
86 "cpuid ()" -
87 "get_mmap_args (long)"
88 {
89 untested "$test: $status: $option: $prototype (deprecated)"
90 continue
91 }
92 }
93
94 # Before 2.6.21 __get_skb_iphdr was pure script code. After
95 # 2.6.21, it is an embedded C function.
96 if { "$prototype" == "__get_skb_iphdr(long)" } {
97 if {($k1*256*256 + $k2*256 + $k3) < (2*256*256 + 6*256 + 21)} {
98 untested "$test: $status: $prototype"
99 continue
100 }
101 }
102
103 # If the function we're about to test doesn't contain any
104 # embedded C, we can't know if it calls any privileged
105 # functions. So, we won't bother testing functions without
106 # embedded C.
107 if { $status == "no embedded C" } {
108 # untested "$test: $status: $prototype"
109 continue
110 }
111
112
113 # Test calling the function. If successful, the stap rc will be 0.
114 if { $transitive_test } {
115 set call [add_args $prototype]
116 set cmd [concat [list stap -u -p2 $option -e] "'function foo() { $call } probe begin { foo() }'"]
117 } else {
118 set call [add_args $prototype]
119 set cmd [concat [list stap -u -p2 $option -e] "'probe begin { $call }'"]
120 }
121 # XXX drop the pass2 standard output, ~800k lines in systemtap.log
122 # whereas standard error is kept for inspecting error messages
123 if {! [info exists env(SYSTEMTAP_SHOW_EMBEDDED_C)]} {
124 verbose -log "eval exec sh -c $cmd >/dev/null"
125 set rc [catch {exec sh -c "$cmd" >/dev/null} res_stap]
126 } else {
127 verbose -log "eval exec sh -c $cmd"
128 set rc [catch {exec sh -c "$cmd"} res_stap]
129 }
130 verbose -log $res_stap
131
132 # Look for a message saying that the function is not allowed
133 # for unprivileged users.
134 regexp "(\[^\[:space:](]*).*" $prototype match funcname
135 verbose -log "funcname is $funcname"
136
137 set emsg1 "semantic error: function may not be used when --privilege=stapusr is specified: identifier '$funcname'"
138 set found [expr [regexp ".*$emsg1.*" $res_stap] || [regexp ".*$emsg2.*" $res_stap]]
139
140 # Evaluate the result based upon the function's status and the
141 # privilege level.
142 switch $status {
143 unprivileged -
144 myproc-unprivileged -
145 "no embedded C"
146 {
147 # There should be no message excluding this function
148 set passed [expr ! $found]
149 }
150 privileged
151 {
152 if {$privilege == "stapusr"} {
153 # There should be a message excluding this function
154 set passed $found
155 } else {
156 # There should be no message excluding this function
157 set passed [expr ! $found]
158 }
159 }
160 default
161 {
162 set passed 0
163 }
164 }
165
166 if {$passed} {
167 pass "$test: $status: $option: $prototype"
168 } else {
169 fail "$test: $status: $option: $prototype"
170 }
171
172 set selector [expr ($selector + 1) % 2]
173 }
174 }
175
176 # Obtain the prototypes of all tapset functions containing embedded C code.
177 set tapsetdir "$srcdir/../tapset"
178 verbose -log "Looking for tapset scripts in $tapsetdir"
179 set files [glob -nocomplain $tapsetdir/*.stp]
180 verbose -log "Looking for tapset scripts in $tapsetdir/[exec uname -i]"
181 set files [concat $files [glob -nocomplain $tapsetdir/[exec uname -i]/*.stp]]
182 verbose -log "Looking for tapset scripts in $tapsetdir/linux"
183 set files [glob -nocomplain $tapsetdir/linux/*.stp]
184 verbose -log "Looking for tapset scripts in $tapsetdir/linux/[exec uname -i]"
185 set files [concat $files [glob -nocomplain $tapsetdir/linux/[exec uname -i]/*.stp]]
186
187 verbose -log "eval exec $srcdir/$subdir/embeddedc.awk [join $files]"
188 catch {eval exec $srcdir/$subdir/embeddedc.awk [join $files]} res_stap
189 set prototype_info [split $res_stap "\n"]
190
191 verbose -log "prototype_info contains [llength prototype_info] items"
192 if {[llength prototype_info] > 0} {
193 pass "$test: Obtain list tapset functions containing embedded C"
194 } else {
195 fail "$test: Obtain list tapset functions containing embedded C"
196 untested "$test"
197 return
198 }
199
200 # Now run the tests. We'll test all the embedded C functions.
201 test_embedded_C stapusr 1
202 test_embedded_C stapsys 1
203 #test_embedded_C stapdev 1
204
205 # Now let's make sure that privileged function are "transitive" -
206 # i.e. if an embedded-C function is privileged, then any pure script
207 # function that calls it is also privileged. Since we don't know the
208 # call graph information for functions, we'll define our own new pure
209 # script function that then calls the real embedded-C function. (See
210 # function "foo" above in test_embedded_C().)
211 #
212 # Here we'll just test a subset of the embedded C functions.
213 if {! [info exists env(SYSTEMTAP_TEST_ALL_EMBEDDED_C)]} {
214 # Test a subset (1/5) of the tapset functions containing embedded C code.
215 set test_fraction 5
216 } else {
217 # Test all of the tapset functions containing embedded C code.
218 set test_fraction 1
219 }
220 set test "$test (transitive)"
221 test_embedded_C stapusr $test_fraction 1
222 test_embedded_C stapsys $test_fraction 1
223 #test_embedded_C stapdev $test_fraction 1