1

我在这里运行了一个示例程序,以查看子字符串是否与字符串匹配,然后将它们打印出来。到目前为止,由于地址错误,我无法运行该程序。我想知道是否有办法解决这个问题?我附上了整个代码,但我的问题主要与 isSubstring 有关。

#include <uapi/linux/bpf.h>
#define ARRAYSIZE 64

struct data_t {
    char buf[ARRAYSIZE];
};

BPF_ARRAY(lookupTable, struct data_t, ARRAYSIZE);
//char name[20]; 

//find substring in a string
static bool isSubstring(struct data_t stringVal)
{
    char substring[] = "New York";
    int M = sizeof(substring);
    int N = sizeof(stringVal.buf) - 1;
 
    /* A loop to slide pat[] one by one */
    for (int i = 0; i <= N - M; i++) {
        int j;
 
        /* For current index i, check for
 pattern match */
        for (j = 0; j < M; j++)
            if (stringVal.buf[i + j] != substring[j])
                break;
 
        if (j == M)
            return true;
    }
 
    return false;
}

int Test(void *ctx)
{
    #pragma clang loop unroll(full)
    for (int i = 0; i < ARRAYSIZE; i++) {
        int k = i;
        struct data_t *line = lookupTable.lookup(&k);
        if (line) {
            // bpf_trace_printk("%s\n", key->buf);
            if (isSubstring(*line)) {
                bpf_trace_printk("%s\n", line->buf);
            }

        }
    }
    return 0;
}

我的python代码在这里:

import ctypes
from bcc import BPF


b = BPF(src_file="hello.c")

lookupTable = b["lookupTable"]
#add hello.csv to the lookupTable array
f = open("hello.csv","r")
contents = f.readlines()
for i in range(0,len(contents)):
    string = contents[i].encode('utf-8')
    print(len(string))
    lookupTable[ctypes.c_int(i)] = ctypes.create_string_buffer(string, len(string))

f.close()
b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="Test")
b.trace_print()

编辑:忘记添加错误:它真的很长,可以在这里找到:https ://pastebin.com/a7E9L230

我认为错误中最有趣的部分在它提到的底部附近:

8193跳的顺序太复杂了。

再往下一点提到:错误的地址。

4

1 回答 1

1

验证器检查程序中的所有分支。每次看到跳转指令时,它都会将新分支推送到其“要检查的分支堆栈”。正如验证者告诉你的那样,这个堆栈有一个BPF_COMPLEXITY_LIMIT_JMP_SEQ你正在达到的限制(当前为 8192)。“错误地址”只是在这种情况下errno设置的内核值的转换-EFAULT

不知道如何解决它,您可以尝试:

  1. 使用较小的字符串,或
  2. 在 5.3+ 内核(支持有界循环)上:无需使用 clang 展开循环(我不知道它是否有帮助)。
于 2021-08-09T11:07:59.820 回答