我正在将字符串从用户空间传递到我的 BPF 代码,并且想知道是否有可能超出我的 char 结构数组可能的大小限制。是否可以将我的行一一放入 Map 并绕过堆栈大小限制?我通过 Python 传递字符串的方式在这里:
import ctypes
from bcc import BPF
b = BPF(src_file="TP-4091lite.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="TP4091")
b.trace_print()
基本上逐行打印我的 CSV 中的任何内容并将其放入我的 BPF Map 中。我的 C 代码在这里:
#include <uapi/linux/bpf.h>
#define ARRAYSIZE 64
struct data_t {
char buf[ARRAYSIZE];
};
struct char_t {
char c[ARRAYSIZE];
};
BPF_ARRAY(lookupTable, struct data_t, ARRAYSIZE);
BPF_ARRAY(characterTable, struct char_t, ARRAYSIZE);
//find substring in a string
static bool isSubstring(struct data_t stringVal)
{
char substring[] = "New York";
int M = sizeof(substring) - 1;
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 TP4091(void *ctx)
{
for (int i = 0; i < ARRAYSIZE; i++) {
char name[20];
int k = i;
struct data_t *line = lookupTable.lookup(&k);
if (line) {
// bpf_trace_printk("%s\n", key->buf);
if (isSubstring(*line) == true) {
bpf_trace_printk("%s\n", line->buf);
}
}
}
return 0;
}
输入任意值时仍然存在大小限制,我想看看我能推多远。