//�������������������������������������������������������������������������Ŀ // ��� Protected ��� � // EschElement - link_tree � // � // Creates a linked-list from the tree (destroying the tree). � //��������������������������������������������������������������������������� EschElement *EschElement::link_tree(EschElement *elm) { assertMyth("EschElement::draw_tree() needs valid input", elm != 0); EschElement *right = (elm->right) ? link_tree(elm->right) : 0; EschElement *left = (elm->left) ? link_tree(elm->left) : 0; if (right) { for(EschElement *e = right; e->right != 0; e = e->right); e->right = elm; elm->left = e; elm->right = left; if (left) left->left = elm; return right; } else if (left) { elm->left = 0; elm->right = left; if (left) left->left = elm; return elm; } else { elm->left = elm->right = 0; return elm; } }
static int link_tree(struct rtnl_ematch *index[], int nmatches, int pos, struct nl_list_head *root) { struct rtnl_ematch *ematch; int i; for (i = pos; i < nmatches; i++) { ematch = index[i]; nl_list_add_tail(&ematch->e_list, root); if (ematch->e_kind == TCF_EM_CONTAINER) link_tree(index, nmatches, container_ref(ematch), &ematch->e_childs); if (!(ematch->e_flags & TCF_EM_REL_MASK)) return 0; } /* Last entry in chain can't possibly have no relation */ return -NLE_INVAL; }
//�������������������������������������������������������������������������Ŀ // EschElement - flush_truesort � // � // Draws any pending element entries using a more complex sort and clears � // the arena. � //��������������������������������������������������������������������������� void EschElement::flush_truesort() { assertMyth("EschElement::flush_truesort() needs sort area", EschSysInstance != 0 && EschSysInstance->sspace != 0); //��� Perform draw of any pending elements if (EschSysInstance->sroot) { //��� Create linked-list of elements sorted by Z-max //��� with right as next, left as prev. EschElement *head = link_tree(EschSysInstance->sroot); assert(head != 0); //��� Start from end of list for(EschElement *elm=head; elm->right != 0; elm = elm->right); //��� Process each polygon from back to front while(elm != 0) { // for now just draw... elm->draw(); } } //��� Clear sort area ivory_arena_clear(EschSysInstance->sspace); EschSysInstance->sroot=0; //��� Update stats if (EschElementDepth > EschSysInstance->sspace_mdepth) EschSysInstance->sspace_mdepth = EschElementDepth; if (EschElementSize > EschSysInstance->sspace_mbytes) EschSysInstance->sspace_mbytes = EschElementSize; EschElementDepth=0; EschElementSize=0; }
/** * Parse ematch netlink attributes * * @return 0 on success or a negative error code. */ int rtnl_ematch_parse_attr(struct nlattr *attr, struct rtnl_ematch_tree **result) { struct nlattr *a, *tb[TCA_EMATCH_TREE_MAX+1]; struct tcf_ematch_tree_hdr *thdr; struct rtnl_ematch_tree *tree; struct rtnl_ematch **index; int nmatches = 0, err, remaining; NL_DBG(2, "Parsing attribute %p as ematch tree\n", attr); err = nla_parse_nested(tb, TCA_EMATCH_TREE_MAX, attr, tree_policy); if (err < 0) return err; if (!tb[TCA_EMATCH_TREE_HDR]) return -NLE_MISSING_ATTR; thdr = nla_data(tb[TCA_EMATCH_TREE_HDR]); /* Ignore empty trees */ if (thdr->nmatches == 0) { NL_DBG(2, "Ignoring empty ematch configuration\n"); return 0; } if (!tb[TCA_EMATCH_TREE_LIST]) return -NLE_MISSING_ATTR; NL_DBG(2, "ematch tree found with nmatches=%u, progid=%u\n", thdr->nmatches, thdr->progid); /* * Do some basic sanity checking since we will allocate * index[thdr->nmatches]. Calculate how many ematch headers fit into * the provided data and make sure nmatches does not exceed it. */ if (thdr->nmatches > (nla_len(tb[TCA_EMATCH_TREE_LIST]) / nla_total_size(sizeof(struct tcf_ematch_hdr)))) return -NLE_INVAL; if (!(index = calloc(thdr->nmatches, sizeof(struct rtnl_ematch *)))) return -NLE_NOMEM; if (!(tree = rtnl_ematch_tree_alloc(thdr->progid))) { err = -NLE_NOMEM; goto errout; } nla_for_each_nested(a, tb[TCA_EMATCH_TREE_LIST], remaining) { struct rtnl_ematch_ops *ops; struct tcf_ematch_hdr *hdr; struct rtnl_ematch *ematch; void *data; size_t len; NL_DBG(3, "parsing ematch attribute %d, len=%u\n", nmatches+1, nla_len(a)); if (nla_len(a) < sizeof(*hdr)) { err = -NLE_INVAL; goto errout; } /* Quit as soon as we've parsed more matches than expected */ if (nmatches >= thdr->nmatches) { err = -NLE_RANGE; goto errout; } hdr = nla_data(a); data = nla_data(a) + NLA_ALIGN(sizeof(*hdr)); len = nla_len(a) - NLA_ALIGN(sizeof(*hdr)); NL_DBG(3, "ematch attribute matchid=%u, kind=%u, flags=%u\n", hdr->matchid, hdr->kind, hdr->flags); /* * Container matches contain a reference to another sequence * of matches. Ensure that the reference is within boundries. */ if (hdr->kind == TCF_EM_CONTAINER && *((uint32_t *) data) >= thdr->nmatches) { err = -NLE_INVAL; goto errout; } if (!(ematch = rtnl_ematch_alloc())) { err = -NLE_NOMEM; goto errout; } ematch->e_id = hdr->matchid; ematch->e_kind = hdr->kind; ematch->e_flags = hdr->flags; if ((ops = rtnl_ematch_lookup_ops(hdr->kind))) { if (ops->eo_minlen && len < ops->eo_minlen) { rtnl_ematch_free(ematch); err = -NLE_INVAL; goto errout; } rtnl_ematch_set_ops(ematch, ops); if (ops->eo_parse && (err = ops->eo_parse(ematch, data, len)) < 0) { rtnl_ematch_free(ematch); goto errout; } } NL_DBG(3, "index[%d] = %p\n", nmatches, ematch); index[nmatches++] = ematch; } if (nmatches != thdr->nmatches) { err = -NLE_INVAL; goto errout; } err = link_tree(index, nmatches, 0, &tree->et_list); if (err < 0) goto errout; free(index); *result = tree; return 0; errout: rtnl_ematch_tree_free(tree); free(index); return err; }
pAssemblerTree Analysis::disasm(pCodeBufferInfo pinfo) { std::vector<CodePiece> piece_test; disasm(pinfo,piece_test) ; for (std::vector<CodePiece>::iterator iter = piece_test.begin(); iter != piece_test.end();iter++) { printf("\033[31m标签:%08x,\033[34m跳转到:%08x\033[0m,条件跳转:%d\n",iter->get_label(),iter->get_jmplabel(),iter->get_is_jcc()); for (std::list<ud_t>::iterator xiter = iter->get_piece().begin(); xiter != iter->get_piece().end();xiter++) { printf("%s\n",xiter->insn_buffer); } } //__asm("int3"); ud_t ud_obj; ud_init(&ud_obj); #ifndef PROTECT_X64 ud_set_mode(&ud_obj,32); #else ud_set_mode(&ud_obj,64); #endif ud_set_pc(&ud_obj,pinfo->addr); ud_set_input_buffer(&ud_obj, (uint8_t*)pinfo->buf, pinfo->size); ud_set_syntax(&ud_obj,UD_SYN_INTEL); base = pinfo->addr; size = pinfo->size; std::vector <long> address_array; //pAssemblerTree root = new AssemblerTree; // pAssemblerTree nowtree = root; std::vector <ud_t> ud_obj_array; address_array.push_back(pinfo->addr); address_array.push_back(pinfo->addr + pinfo->size); while (ud_disassemble(&ud_obj) != 0) { ud_obj_array.push_back(ud_obj); // nowtree.asmpiect.push_back(ud_obj); if (ud_obj.operand[0].type == UD_OP_JIMM || ud_obj.mnemonic == UD_Icall )//&& ud_obj.mnemonic != UD_Icall) { long addr = ud_obj.operand[0].size == 8 ? ((signed char)ud_obj.operand[0].lval.sbyte + ud_obj.pc) : (ud_obj.operand[0].lval.sdword + ud_obj.pc); if (addr >= pinfo->addr && addr <= pinfo->addr + pinfo->size) { address_array.push_back(addr); address_array.push_back(ud_obj.pc); } } } long count = 0; long *a_array; size_t address_size = address_array.size(); a_array = new long[address_size]; for (std::vector <long>::iterator iter = address_array.begin() ; iter != address_array.end() ; ++iter) { bool fk = true; for (int i = 0; i < count; ++i) { if (*iter == a_array[i]) { fk = false; } } if (fk) a_array[count++] = *iter; } for (int i = 0; i < count; ++i) { for (int j = i; j < count ; ++j) { if (a_array[i] > a_array[j]) { long t = a_array[i]; a_array[i] = a_array[j]; a_array[j] = t; } } } for (int i = 0; i < count; ++i) { printf("%08x\r\n",a_array[i]); } int point = 0; pAssemblerTree nowtree = NULL; pAssemblerTree parent = NULL; const bool begin = true; const bool end = false; bool status = end; for (std::vector <ud_t>::iterator iter = ud_obj_array.begin(); iter != ud_obj_array.end(); ++iter) { //typedef true begin; //typedef false end; // ud_t *p = iter; ud_t ud = *iter; if (ud.insn_offset == a_array[point] && status == end) { point++; status = begin; nowtree = new AssemblerTree; root ? NULL : root = nowtree; nowtree->LeftChild = NULL; nowtree->RightChild = NULL; nowtree->id = point - 1; // nowtree.asmpiect.push_back(ud); } if (nowtree) { nowtree->asmpiece.push_back(ud); } if (ud.pc == a_array[point] && status == begin) { nowtree->base = a_array[point-1]; //point++; nowtree->size = ud.pc - a_array[point-1]; //代码块大小 status = end; parent = add_tree(parent,nowtree,L); //nowtree = new AssemblerTree; //nowtree->LeftChild = NULL; //nowtree->RightChild = NULL; } nowtree->reloc_address = -1; nowtree->next_instruction = ud.pc; } block_count = point; link_tree(); #ifdef DEBUG show_tree(root,"false:"); #endif delete [] a_array; // show_tree(root); /*for (std::vector <long>::iterator iter = address_array.begin() ; iter != address_array.end() ; ++iter) { int addr = *iter; printf("%08x\r\n",addr); }*/ return NULL; }