~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/lib/test_kprobes.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /lib/test_kprobes.c (Version linux-6.12-rc7) and /lib/test_kprobes.c (Version linux-6.2.16)


  1 // SPDX-License-Identifier: GPL-2.0-or-later        1 // SPDX-License-Identifier: GPL-2.0-or-later
  2 /*                                                  2 /*
  3  * test_kprobes.c - simple sanity test for k*p !!   3  * test_kprobes.c - simple sanity test for *probes
  4  *                                                  4  *
  5  * Copyright IBM Corp. 2008                         5  * Copyright IBM Corp. 2008
  6  */                                                 6  */
  7                                                     7 
  8 #include <linux/kernel.h>                           8 #include <linux/kernel.h>
  9 #include <linux/kprobes.h>                          9 #include <linux/kprobes.h>
 10 #include <linux/random.h>                          10 #include <linux/random.h>
 11 #include <kunit/test.h>                            11 #include <kunit/test.h>
 12                                                    12 
 13 #define div_factor 3                               13 #define div_factor 3
 14                                                    14 
 15 static u32 rand1, preh_val, posth_val;             15 static u32 rand1, preh_val, posth_val;
 16 static u32 (*target)(u32 value);                   16 static u32 (*target)(u32 value);
 17 static u32 (*recursed_target)(u32 value);      << 
 18 static u32 (*target2)(u32 value);                  17 static u32 (*target2)(u32 value);
 19 static struct kunit *current_test;                 18 static struct kunit *current_test;
 20                                                    19 
 21 static unsigned long (*internal_target)(void);     20 static unsigned long (*internal_target)(void);
 22 static unsigned long (*stacktrace_target)(void     21 static unsigned long (*stacktrace_target)(void);
 23 static unsigned long (*stacktrace_driver)(void     22 static unsigned long (*stacktrace_driver)(void);
 24 static unsigned long target_return_address[2];     23 static unsigned long target_return_address[2];
 25                                                    24 
 26 static noinline u32 kprobe_target(u32 value)       25 static noinline u32 kprobe_target(u32 value)
 27 {                                                  26 {
 28         return (value / div_factor);               27         return (value / div_factor);
 29 }                                                  28 }
 30                                                    29 
 31 static noinline u32 kprobe_recursed_target(u32 << 
 32 {                                              << 
 33         return (value / div_factor);           << 
 34 }                                              << 
 35                                                << 
 36 static int kp_pre_handler(struct kprobe *p, st     30 static int kp_pre_handler(struct kprobe *p, struct pt_regs *regs)
 37 {                                                  31 {
 38         KUNIT_EXPECT_FALSE(current_test, preem     32         KUNIT_EXPECT_FALSE(current_test, preemptible());
 39                                                !!  33         preh_val = (rand1 / div_factor);
 40         preh_val = recursed_target(rand1);     << 
 41         return 0;                                  34         return 0;
 42 }                                                  35 }
 43                                                    36 
 44 static void kp_post_handler(struct kprobe *p,      37 static void kp_post_handler(struct kprobe *p, struct pt_regs *regs,
 45                 unsigned long flags)               38                 unsigned long flags)
 46 {                                                  39 {
 47         u32 expval = recursed_target(rand1);   << 
 48                                                << 
 49         KUNIT_EXPECT_FALSE(current_test, preem     40         KUNIT_EXPECT_FALSE(current_test, preemptible());
 50         KUNIT_EXPECT_EQ(current_test, preh_val !!  41         KUNIT_EXPECT_EQ(current_test, preh_val, (rand1 / div_factor));
 51                                                << 
 52         posth_val = preh_val + div_factor;         42         posth_val = preh_val + div_factor;
 53 }                                                  43 }
 54                                                    44 
 55 static struct kprobe kp = {                        45 static struct kprobe kp = {
 56         .symbol_name = "kprobe_target",            46         .symbol_name = "kprobe_target",
 57         .pre_handler = kp_pre_handler,             47         .pre_handler = kp_pre_handler,
 58         .post_handler = kp_post_handler            48         .post_handler = kp_post_handler
 59 };                                                 49 };
 60                                                    50 
 61 static void test_kprobe(struct kunit *test)        51 static void test_kprobe(struct kunit *test)
 62 {                                                  52 {
 63         current_test = test;                       53         current_test = test;
 64         KUNIT_EXPECT_EQ(test, 0, register_kpro     54         KUNIT_EXPECT_EQ(test, 0, register_kprobe(&kp));
 65         target(rand1);                             55         target(rand1);
 66         unregister_kprobe(&kp);                    56         unregister_kprobe(&kp);
 67         KUNIT_EXPECT_NE(test, 0, preh_val);        57         KUNIT_EXPECT_NE(test, 0, preh_val);
 68         KUNIT_EXPECT_NE(test, 0, posth_val);       58         KUNIT_EXPECT_NE(test, 0, posth_val);
 69 }                                                  59 }
 70                                                    60 
 71 static noinline u32 kprobe_target2(u32 value)      61 static noinline u32 kprobe_target2(u32 value)
 72 {                                                  62 {
 73         return (value / div_factor) + 1;           63         return (value / div_factor) + 1;
 74 }                                                  64 }
 75                                                    65 
 76 static noinline unsigned long kprobe_stacktrac     66 static noinline unsigned long kprobe_stacktrace_internal_target(void)
 77 {                                                  67 {
 78         if (!target_return_address[0])             68         if (!target_return_address[0])
 79                 target_return_address[0] = (un     69                 target_return_address[0] = (unsigned long)__builtin_return_address(0);
 80         return target_return_address[0];           70         return target_return_address[0];
 81 }                                                  71 }
 82                                                    72 
 83 static noinline unsigned long kprobe_stacktrac     73 static noinline unsigned long kprobe_stacktrace_target(void)
 84 {                                                  74 {
 85         if (!target_return_address[1])             75         if (!target_return_address[1])
 86                 target_return_address[1] = (un     76                 target_return_address[1] = (unsigned long)__builtin_return_address(0);
 87                                                    77 
 88         if (internal_target)                       78         if (internal_target)
 89                 internal_target();                 79                 internal_target();
 90                                                    80 
 91         return target_return_address[1];           81         return target_return_address[1];
 92 }                                                  82 }
 93                                                    83 
 94 static noinline unsigned long kprobe_stacktrac     84 static noinline unsigned long kprobe_stacktrace_driver(void)
 95 {                                                  85 {
 96         if (stacktrace_target)                     86         if (stacktrace_target)
 97                 stacktrace_target();               87                 stacktrace_target();
 98                                                    88 
 99         /* This is for preventing inlining the     89         /* This is for preventing inlining the function */
100         return (unsigned long)__builtin_return     90         return (unsigned long)__builtin_return_address(0);
101 }                                                  91 }
102                                                    92 
103 static int kp_pre_handler2(struct kprobe *p, s     93 static int kp_pre_handler2(struct kprobe *p, struct pt_regs *regs)
104 {                                                  94 {
105         preh_val = (rand1 / div_factor) + 1;       95         preh_val = (rand1 / div_factor) + 1;
106         return 0;                                  96         return 0;
107 }                                                  97 }
108                                                    98 
109 static void kp_post_handler2(struct kprobe *p,     99 static void kp_post_handler2(struct kprobe *p, struct pt_regs *regs,
110                 unsigned long flags)              100                 unsigned long flags)
111 {                                                 101 {
112         KUNIT_EXPECT_EQ(current_test, preh_val    102         KUNIT_EXPECT_EQ(current_test, preh_val, (rand1 / div_factor) + 1);
113         posth_val = preh_val + div_factor;        103         posth_val = preh_val + div_factor;
114 }                                                 104 }
115                                                   105 
116 static struct kprobe kp2 = {                      106 static struct kprobe kp2 = {
117         .symbol_name = "kprobe_target2",          107         .symbol_name = "kprobe_target2",
118         .pre_handler = kp_pre_handler2,           108         .pre_handler = kp_pre_handler2,
119         .post_handler = kp_post_handler2          109         .post_handler = kp_post_handler2
120 };                                                110 };
121                                                   111 
122 static void test_kprobes(struct kunit *test)      112 static void test_kprobes(struct kunit *test)
123 {                                                 113 {
124         struct kprobe *kps[2] = {&kp, &kp2};      114         struct kprobe *kps[2] = {&kp, &kp2};
125                                                   115 
126         current_test = test;                      116         current_test = test;
127                                                   117 
128         /* addr and flags should be cleard for    118         /* addr and flags should be cleard for reusing kprobe. */
129         kp.addr = NULL;                           119         kp.addr = NULL;
130         kp.flags = 0;                             120         kp.flags = 0;
131                                                   121 
132         KUNIT_EXPECT_EQ(test, 0, register_kpro    122         KUNIT_EXPECT_EQ(test, 0, register_kprobes(kps, 2));
133         preh_val = 0;                             123         preh_val = 0;
134         posth_val = 0;                            124         posth_val = 0;
135         target(rand1);                            125         target(rand1);
136                                                   126 
137         KUNIT_EXPECT_NE(test, 0, preh_val);       127         KUNIT_EXPECT_NE(test, 0, preh_val);
138         KUNIT_EXPECT_NE(test, 0, posth_val);      128         KUNIT_EXPECT_NE(test, 0, posth_val);
139                                                   129 
140         preh_val = 0;                             130         preh_val = 0;
141         posth_val = 0;                            131         posth_val = 0;
142         target2(rand1);                           132         target2(rand1);
143                                                   133 
144         KUNIT_EXPECT_NE(test, 0, preh_val);       134         KUNIT_EXPECT_NE(test, 0, preh_val);
145         KUNIT_EXPECT_NE(test, 0, posth_val);      135         KUNIT_EXPECT_NE(test, 0, posth_val);
146         unregister_kprobes(kps, 2);               136         unregister_kprobes(kps, 2);
147 }                                                 137 }
148                                                   138 
149 static struct kprobe kp_missed = {             << 
150         .symbol_name = "kprobe_recursed_target << 
151         .pre_handler = kp_pre_handler,         << 
152         .post_handler = kp_post_handler,       << 
153 };                                             << 
154                                                << 
155 static void test_kprobe_missed(struct kunit *t << 
156 {                                              << 
157         current_test = test;                   << 
158         preh_val = 0;                          << 
159         posth_val = 0;                         << 
160                                                << 
161         KUNIT_EXPECT_EQ(test, 0, register_kpro << 
162                                                << 
163         recursed_target(rand1);                << 
164                                                << 
165         KUNIT_EXPECT_EQ(test, 2, kp_missed.nmi << 
166         KUNIT_EXPECT_NE(test, 0, preh_val);    << 
167         KUNIT_EXPECT_NE(test, 0, posth_val);   << 
168                                                << 
169         unregister_kprobe(&kp_missed);         << 
170 }                                              << 
171                                                << 
172 #ifdef CONFIG_KRETPROBES                          139 #ifdef CONFIG_KRETPROBES
173 static u32 krph_val;                              140 static u32 krph_val;
174                                                   141 
175 static int entry_handler(struct kretprobe_inst    142 static int entry_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
176 {                                                 143 {
177         KUNIT_EXPECT_FALSE(current_test, preem    144         KUNIT_EXPECT_FALSE(current_test, preemptible());
178         krph_val = (rand1 / div_factor);          145         krph_val = (rand1 / div_factor);
179         return 0;                                 146         return 0;
180 }                                                 147 }
181                                                   148 
182 static int return_handler(struct kretprobe_ins    149 static int return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
183 {                                                 150 {
184         unsigned long ret = regs_return_value(    151         unsigned long ret = regs_return_value(regs);
185                                                   152 
186         KUNIT_EXPECT_FALSE(current_test, preem    153         KUNIT_EXPECT_FALSE(current_test, preemptible());
187         KUNIT_EXPECT_EQ(current_test, ret, ran    154         KUNIT_EXPECT_EQ(current_test, ret, rand1 / div_factor);
188         KUNIT_EXPECT_NE(current_test, krph_val    155         KUNIT_EXPECT_NE(current_test, krph_val, 0);
189         krph_val = rand1;                         156         krph_val = rand1;
190         return 0;                                 157         return 0;
191 }                                                 158 }
192                                                   159 
193 static struct kretprobe rp = {                    160 static struct kretprobe rp = {
194         .handler        = return_handler,         161         .handler        = return_handler,
195         .entry_handler  = entry_handler,          162         .entry_handler  = entry_handler,
196         .kp.symbol_name = "kprobe_target"         163         .kp.symbol_name = "kprobe_target"
197 };                                                164 };
198                                                   165 
199 static void test_kretprobe(struct kunit *test)    166 static void test_kretprobe(struct kunit *test)
200 {                                                 167 {
201         current_test = test;                      168         current_test = test;
202         KUNIT_EXPECT_EQ(test, 0, register_kret    169         KUNIT_EXPECT_EQ(test, 0, register_kretprobe(&rp));
203         target(rand1);                            170         target(rand1);
204         unregister_kretprobe(&rp);                171         unregister_kretprobe(&rp);
205         KUNIT_EXPECT_EQ(test, krph_val, rand1)    172         KUNIT_EXPECT_EQ(test, krph_val, rand1);
206 }                                                 173 }
207                                                   174 
208 static int return_handler2(struct kretprobe_in    175 static int return_handler2(struct kretprobe_instance *ri, struct pt_regs *regs)
209 {                                                 176 {
210         unsigned long ret = regs_return_value(    177         unsigned long ret = regs_return_value(regs);
211                                                   178 
212         KUNIT_EXPECT_EQ(current_test, ret, (ra    179         KUNIT_EXPECT_EQ(current_test, ret, (rand1 / div_factor) + 1);
213         KUNIT_EXPECT_NE(current_test, krph_val    180         KUNIT_EXPECT_NE(current_test, krph_val, 0);
214         krph_val = rand1;                         181         krph_val = rand1;
215         return 0;                                 182         return 0;
216 }                                                 183 }
217                                                   184 
218 static struct kretprobe rp2 = {                   185 static struct kretprobe rp2 = {
219         .handler        = return_handler2,        186         .handler        = return_handler2,
220         .entry_handler  = entry_handler,          187         .entry_handler  = entry_handler,
221         .kp.symbol_name = "kprobe_target2"        188         .kp.symbol_name = "kprobe_target2"
222 };                                                189 };
223                                                   190 
224 static void test_kretprobes(struct kunit *test    191 static void test_kretprobes(struct kunit *test)
225 {                                                 192 {
226         struct kretprobe *rps[2] = {&rp, &rp2}    193         struct kretprobe *rps[2] = {&rp, &rp2};
227                                                   194 
228         current_test = test;                      195         current_test = test;
229         /* addr and flags should be cleard for    196         /* addr and flags should be cleard for reusing kprobe. */
230         rp.kp.addr = NULL;                        197         rp.kp.addr = NULL;
231         rp.kp.flags = 0;                          198         rp.kp.flags = 0;
232         KUNIT_EXPECT_EQ(test, 0, register_kret    199         KUNIT_EXPECT_EQ(test, 0, register_kretprobes(rps, 2));
233                                                   200 
234         krph_val = 0;                             201         krph_val = 0;
235         target(rand1);                            202         target(rand1);
236         KUNIT_EXPECT_EQ(test, krph_val, rand1)    203         KUNIT_EXPECT_EQ(test, krph_val, rand1);
237                                                   204 
238         krph_val = 0;                             205         krph_val = 0;
239         target2(rand1);                           206         target2(rand1);
240         KUNIT_EXPECT_EQ(test, krph_val, rand1)    207         KUNIT_EXPECT_EQ(test, krph_val, rand1);
241         unregister_kretprobes(rps, 2);            208         unregister_kretprobes(rps, 2);
242 }                                                 209 }
243                                                   210 
244 #ifdef CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETP    211 #ifdef CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE
245 #define STACK_BUF_SIZE 16                         212 #define STACK_BUF_SIZE 16
246 static unsigned long stack_buf[STACK_BUF_SIZE]    213 static unsigned long stack_buf[STACK_BUF_SIZE];
247                                                   214 
248 static int stacktrace_return_handler(struct kr    215 static int stacktrace_return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
249 {                                                 216 {
250         unsigned long retval = regs_return_val    217         unsigned long retval = regs_return_value(regs);
251         int i, ret;                               218         int i, ret;
252                                                   219 
253         KUNIT_EXPECT_FALSE(current_test, preem    220         KUNIT_EXPECT_FALSE(current_test, preemptible());
254         KUNIT_EXPECT_EQ(current_test, retval,     221         KUNIT_EXPECT_EQ(current_test, retval, target_return_address[1]);
255                                                   222 
256         /*                                        223         /*
257          * Test stacktrace inside the kretprob    224          * Test stacktrace inside the kretprobe handler, this will involves
258          * kretprobe trampoline, but must incl    225          * kretprobe trampoline, but must include correct return address
259          * of the target function.                226          * of the target function.
260          */                                       227          */
261         ret = stack_trace_save(stack_buf, STAC    228         ret = stack_trace_save(stack_buf, STACK_BUF_SIZE, 0);
262         KUNIT_EXPECT_NE(current_test, ret, 0);    229         KUNIT_EXPECT_NE(current_test, ret, 0);
263                                                   230 
264         for (i = 0; i < ret; i++) {               231         for (i = 0; i < ret; i++) {
265                 if (stack_buf[i] == target_ret    232                 if (stack_buf[i] == target_return_address[1])
266                         break;                    233                         break;
267         }                                         234         }
268         KUNIT_EXPECT_NE(current_test, i, ret);    235         KUNIT_EXPECT_NE(current_test, i, ret);
269                                                   236 
270 #if !IS_MODULE(CONFIG_KPROBES_SANITY_TEST)        237 #if !IS_MODULE(CONFIG_KPROBES_SANITY_TEST)
271         /*                                        238         /*
272          * Test stacktrace from pt_regs at the    239          * Test stacktrace from pt_regs at the return address. Thus the stack
273          * trace must start from the target re    240          * trace must start from the target return address.
274          */                                       241          */
275         ret = stack_trace_save_regs(regs, stac    242         ret = stack_trace_save_regs(regs, stack_buf, STACK_BUF_SIZE, 0);
276         KUNIT_EXPECT_NE(current_test, ret, 0);    243         KUNIT_EXPECT_NE(current_test, ret, 0);
277         KUNIT_EXPECT_EQ(current_test, stack_bu    244         KUNIT_EXPECT_EQ(current_test, stack_buf[0], target_return_address[1]);
278 #endif                                            245 #endif
279                                                   246 
280         return 0;                                 247         return 0;
281 }                                                 248 }
282                                                   249 
283 static struct kretprobe rp3 = {                   250 static struct kretprobe rp3 = {
284         .handler        = stacktrace_return_ha    251         .handler        = stacktrace_return_handler,
285         .kp.symbol_name = "kprobe_stacktrace_t    252         .kp.symbol_name = "kprobe_stacktrace_target"
286 };                                                253 };
287                                                   254 
288 static void test_stacktrace_on_kretprobe(struc    255 static void test_stacktrace_on_kretprobe(struct kunit *test)
289 {                                                 256 {
290         unsigned long myretaddr = (unsigned lo    257         unsigned long myretaddr = (unsigned long)__builtin_return_address(0);
291                                                   258 
292         current_test = test;                      259         current_test = test;
293         rp3.kp.addr = NULL;                       260         rp3.kp.addr = NULL;
294         rp3.kp.flags = 0;                         261         rp3.kp.flags = 0;
295                                                   262 
296         /*                                        263         /*
297          * Run the stacktrace_driver() to reco    264          * Run the stacktrace_driver() to record correct return address in
298          * stacktrace_target() and ensure stac    265          * stacktrace_target() and ensure stacktrace_driver() call is not
299          * inlined by checking the return addr    266          * inlined by checking the return address of stacktrace_driver()
300          * and the return address of this func    267          * and the return address of this function is different.
301          */                                       268          */
302         KUNIT_ASSERT_NE(test, myretaddr, stack    269         KUNIT_ASSERT_NE(test, myretaddr, stacktrace_driver());
303                                                   270 
304         KUNIT_ASSERT_EQ(test, 0, register_kret    271         KUNIT_ASSERT_EQ(test, 0, register_kretprobe(&rp3));
305         KUNIT_ASSERT_NE(test, myretaddr, stack    272         KUNIT_ASSERT_NE(test, myretaddr, stacktrace_driver());
306         unregister_kretprobe(&rp3);               273         unregister_kretprobe(&rp3);
307 }                                                 274 }
308                                                   275 
309 static int stacktrace_internal_return_handler(    276 static int stacktrace_internal_return_handler(struct kretprobe_instance *ri, struct pt_regs *regs)
310 {                                                 277 {
311         unsigned long retval = regs_return_val    278         unsigned long retval = regs_return_value(regs);
312         int i, ret;                               279         int i, ret;
313                                                   280 
314         KUNIT_EXPECT_FALSE(current_test, preem    281         KUNIT_EXPECT_FALSE(current_test, preemptible());
315         KUNIT_EXPECT_EQ(current_test, retval,     282         KUNIT_EXPECT_EQ(current_test, retval, target_return_address[0]);
316                                                   283 
317         /*                                        284         /*
318          * Test stacktrace inside the kretprob    285          * Test stacktrace inside the kretprobe handler for nested case.
319          * The unwinder will find the kretprob    286          * The unwinder will find the kretprobe_trampoline address on the
320          * return address, and kretprobe must     287          * return address, and kretprobe must solve that.
321          */                                       288          */
322         ret = stack_trace_save(stack_buf, STAC    289         ret = stack_trace_save(stack_buf, STACK_BUF_SIZE, 0);
323         KUNIT_EXPECT_NE(current_test, ret, 0);    290         KUNIT_EXPECT_NE(current_test, ret, 0);
324                                                   291 
325         for (i = 0; i < ret - 1; i++) {           292         for (i = 0; i < ret - 1; i++) {
326                 if (stack_buf[i] == target_ret    293                 if (stack_buf[i] == target_return_address[0]) {
327                         KUNIT_EXPECT_EQ(curren    294                         KUNIT_EXPECT_EQ(current_test, stack_buf[i + 1], target_return_address[1]);
328                         break;                    295                         break;
329                 }                                 296                 }
330         }                                         297         }
331         KUNIT_EXPECT_NE(current_test, i, ret);    298         KUNIT_EXPECT_NE(current_test, i, ret);
332                                                   299 
333 #if !IS_MODULE(CONFIG_KPROBES_SANITY_TEST)        300 #if !IS_MODULE(CONFIG_KPROBES_SANITY_TEST)
334         /* Ditto for the regs version. */         301         /* Ditto for the regs version. */
335         ret = stack_trace_save_regs(regs, stac    302         ret = stack_trace_save_regs(regs, stack_buf, STACK_BUF_SIZE, 0);
336         KUNIT_EXPECT_NE(current_test, ret, 0);    303         KUNIT_EXPECT_NE(current_test, ret, 0);
337         KUNIT_EXPECT_EQ(current_test, stack_bu    304         KUNIT_EXPECT_EQ(current_test, stack_buf[0], target_return_address[0]);
338         KUNIT_EXPECT_EQ(current_test, stack_bu    305         KUNIT_EXPECT_EQ(current_test, stack_buf[1], target_return_address[1]);
339 #endif                                            306 #endif
340                                                   307 
341         return 0;                                 308         return 0;
342 }                                                 309 }
343                                                   310 
344 static struct kretprobe rp4 = {                   311 static struct kretprobe rp4 = {
345         .handler        = stacktrace_internal_    312         .handler        = stacktrace_internal_return_handler,
346         .kp.symbol_name = "kprobe_stacktrace_i    313         .kp.symbol_name = "kprobe_stacktrace_internal_target"
347 };                                                314 };
348                                                   315 
349 static void test_stacktrace_on_nested_kretprob    316 static void test_stacktrace_on_nested_kretprobe(struct kunit *test)
350 {                                                 317 {
351         unsigned long myretaddr = (unsigned lo    318         unsigned long myretaddr = (unsigned long)__builtin_return_address(0);
352         struct kretprobe *rps[2] = {&rp3, &rp4    319         struct kretprobe *rps[2] = {&rp3, &rp4};
353                                                   320 
354         current_test = test;                      321         current_test = test;
355         rp3.kp.addr = NULL;                       322         rp3.kp.addr = NULL;
356         rp3.kp.flags = 0;                         323         rp3.kp.flags = 0;
357                                                   324 
358         //KUNIT_ASSERT_NE(test, myretaddr, sta    325         //KUNIT_ASSERT_NE(test, myretaddr, stacktrace_driver());
359                                                   326 
360         KUNIT_ASSERT_EQ(test, 0, register_kret    327         KUNIT_ASSERT_EQ(test, 0, register_kretprobes(rps, 2));
361         KUNIT_ASSERT_NE(test, myretaddr, stack    328         KUNIT_ASSERT_NE(test, myretaddr, stacktrace_driver());
362         unregister_kretprobes(rps, 2);            329         unregister_kretprobes(rps, 2);
363 }                                                 330 }
364 #endif /* CONFIG_ARCH_CORRECT_STACKTRACE_ON_KR    331 #endif /* CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE */
365                                                   332 
366 #endif /* CONFIG_KRETPROBES */                    333 #endif /* CONFIG_KRETPROBES */
367                                                   334 
368 static int kprobes_test_init(struct kunit *tes    335 static int kprobes_test_init(struct kunit *test)
369 {                                                 336 {
370         target = kprobe_target;                   337         target = kprobe_target;
371         target2 = kprobe_target2;                 338         target2 = kprobe_target2;
372         recursed_target = kprobe_recursed_targ << 
373         stacktrace_target = kprobe_stacktrace_    339         stacktrace_target = kprobe_stacktrace_target;
374         internal_target = kprobe_stacktrace_in    340         internal_target = kprobe_stacktrace_internal_target;
375         stacktrace_driver = kprobe_stacktrace_    341         stacktrace_driver = kprobe_stacktrace_driver;
376         rand1 = get_random_u32_above(div_facto    342         rand1 = get_random_u32_above(div_factor);
377         return 0;                                 343         return 0;
378 }                                                 344 }
379                                                   345 
380 static struct kunit_case kprobes_testcases[] =    346 static struct kunit_case kprobes_testcases[] = {
381         KUNIT_CASE(test_kprobe),                  347         KUNIT_CASE(test_kprobe),
382         KUNIT_CASE(test_kprobes),                 348         KUNIT_CASE(test_kprobes),
383         KUNIT_CASE(test_kprobe_missed),        << 
384 #ifdef CONFIG_KRETPROBES                          349 #ifdef CONFIG_KRETPROBES
385         KUNIT_CASE(test_kretprobe),               350         KUNIT_CASE(test_kretprobe),
386         KUNIT_CASE(test_kretprobes),              351         KUNIT_CASE(test_kretprobes),
387 #ifdef CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETP    352 #ifdef CONFIG_ARCH_CORRECT_STACKTRACE_ON_KRETPROBE
388         KUNIT_CASE(test_stacktrace_on_kretprob    353         KUNIT_CASE(test_stacktrace_on_kretprobe),
389         KUNIT_CASE(test_stacktrace_on_nested_k    354         KUNIT_CASE(test_stacktrace_on_nested_kretprobe),
390 #endif                                            355 #endif
391 #endif                                            356 #endif
392         {}                                        357         {}
393 };                                                358 };
394                                                   359 
395 static struct kunit_suite kprobes_test_suite =    360 static struct kunit_suite kprobes_test_suite = {
396         .name = "kprobes_test",                   361         .name = "kprobes_test",
397         .init = kprobes_test_init,                362         .init = kprobes_test_init,
398         .test_cases = kprobes_testcases,          363         .test_cases = kprobes_testcases,
399 };                                                364 };
400                                                   365 
401 kunit_test_suites(&kprobes_test_suite);           366 kunit_test_suites(&kprobes_test_suite);
402                                                   367 
403 MODULE_DESCRIPTION("simple sanity test for k*p << 
404 MODULE_LICENSE("GPL");                            368 MODULE_LICENSE("GPL");
405                                                   369 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php