libpcap 中的 struct block

在pcap_compile 函数中

root = NULL; // root就是 struct block的指针

...中间没有对root 操作,

if (root == NULL)
        root = gen_retblk(snaplen_arg);

有必要判断吗? 有必要的

root 是应全局变量 事实上

  在结束解析函数 finish_parse 有对全局变量的赋值, 这个赋值也是对整个解析结果的保存。 而root block也就是 cfg(control flow graph的根 root即取其义)

中间的

***** (void)pcap_parse(); *******

if (root == NULL) /* 全局的block 结构 */
        root = gen_retblk(snaplen);

program->bf_insns = icode_to_fcode(root, &len); // root 作为参数的调用

---------------------------------------------------------

下面来认识一下这个结构

struct block {
    int id;        /* cfg 图中的一个标识 */
    struct slist *stmts;    /* side effect stmts */
    struct stmt s;        /* branch stmt 分支语句 */
    int mark;
    int longjt;        /* jt branch requires long jump */
    int longjf;        /* jf branch requires long jump */
    int level;
    int offset;
    int sense;
    struct edge et; // edge true
    struct edge ef; // edge false
    struct block *head; // 指向head部 , 如果没有其他block 则指向自身
    struct block *link;    /* link field used by optimizer */
    uset dom;  // uset的定义 typedef bpf_u_int32 *uset;
    uset closure;
    struct edge *in_edges;
    atomset def, kill;  // typedef bpf_u_int32 *uset; 定义
    atomset in_use;
    atomset out_use;
    int oval;
    int val[N_ATOMS]; // #define N_ATOMS (BPF_MEMWORDS+2) = 18
};

-------------------------------------------------------------------------------------

optimize.c:icode_to_fcode(root, lenp)   pcap虚拟机的重要级函数

原文地址:https://www.cnblogs.com/kwingmei/p/3627413.html