Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (c) 2019 Facebook
0003 
0004 #include <linux/ptrace.h>
0005 #include <linux/bpf.h>
0006 #include <bpf/bpf_helpers.h>
0007 
0008 const struct {
0009     unsigned a[4];
0010     /*
0011      * if the struct's size is multiple of 16, compiler will put it into
0012      * .rodata.cst16 section, which is not recognized by libbpf; work
0013      * around this by ensuring we don't have 16-aligned struct
0014      */
0015     char _y;
0016 } rdonly_values = { .a = {2, 3, 4, 5} };
0017 
0018 struct {
0019     unsigned did_run;
0020     unsigned iters;
0021     unsigned sum;
0022 } res = {};
0023 
0024 SEC("raw_tracepoint/sys_enter:skip_loop")
0025 int skip_loop(struct pt_regs *ctx)
0026 {
0027     /* prevent compiler to optimize everything out */
0028     unsigned * volatile p = (void *)&rdonly_values.a;
0029     unsigned iters = 0, sum = 0;
0030 
0031     /* we should never enter this loop */
0032     while (*p & 1) {
0033         iters++;
0034         sum += *p;
0035         p++;
0036     }
0037     res.did_run = 1;
0038     res.iters = iters;
0039     res.sum = sum;
0040     return 0;
0041 }
0042 
0043 SEC("raw_tracepoint/sys_enter:part_loop")
0044 int part_loop(struct pt_regs *ctx)
0045 {
0046     /* prevent compiler to optimize everything out */
0047     unsigned * volatile p = (void *)&rdonly_values.a;
0048     unsigned iters = 0, sum = 0;
0049 
0050     /* validate verifier can derive loop termination */
0051     while (*p < 5) {
0052         iters++;
0053         sum += *p;
0054         p++;
0055     }
0056     res.did_run = 1;
0057     res.iters = iters;
0058     res.sum = sum;
0059     return 0;
0060 }
0061 
0062 SEC("raw_tracepoint/sys_enter:full_loop")
0063 int full_loop(struct pt_regs *ctx)
0064 {
0065     /* prevent compiler to optimize everything out */
0066     unsigned * volatile p = (void *)&rdonly_values.a;
0067     int i = sizeof(rdonly_values.a) / sizeof(rdonly_values.a[0]);
0068     unsigned iters = 0, sum = 0;
0069 
0070     /* validate verifier can allow full loop as well */
0071     while (i > 0 ) {
0072         iters++;
0073         sum += *p;
0074         p++;
0075         i--;
0076     }
0077     res.did_run = 1;
0078     res.iters = iters;
0079     res.sum = sum;
0080     return 0;
0081 }
0082 
0083 char _license[] SEC("license") = "GPL";