/* Copy a list */ list_t *elist_copy(list_t *h) { list_t *newlist; listent_t *newent; listent_t *prevent; listent_t *curent; void *newelem; int size; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); XALLOC(__FILE__, __FUNCTION__, __LINE__, newlist, sizeof(list_t), NULL); *newlist = *h; prevent = NULL; size = aspect_typesize_get(h->type); /* Copy the list */ for (curent = h->head; curent; curent = curent->next) { XALLOC(__FILE__, __FUNCTION__, __LINE__, newent, sizeof(listent_t), NULL); *newent = *curent; XALLOC(__FILE__, __FUNCTION__, __LINE__, newelem, size, NULL); memcpy(newelem, curent->data, size); newent->data = newelem; newent->key = strdup(curent->key); newent->next = NULL; if (prevent) prevent->next = newent; else newlist->head = newent; prevent = newent; } PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, newlist); }
/** * Inject a section from ET_REL object into ET_EXEC * @param file * @param sect * @param mod * @return */ static int elfsh_inject_etrel_section(elfshobj_t *file, elfshsect_t *sect, u_int mod) { elfsh_Shdr hdr; elfshsect_t *enew; char *newname; char writable; int mode; char *data; u_int modulo; elfshsect_t *plt; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (elfsh_dynamic_file(file) && NULL == (plt = elfsh_get_plt(file, NULL))) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unable to get PLT", -1); /* else create a new section */ hdr = elfsh_create_shdr(0, sect->shdr->sh_type, sect->shdr->sh_flags, 0, 0, sect->shdr->sh_size, 0, 0, 0, 0); XALLOC(__FILE__, __FUNCTION__, __LINE__,newname, strlen(sect->parent->name) + strlen(sect->name) + 2, -1); sprintf(newname, "%s%s", sect->parent->name, sect->name); enew = elfsh_create_section(newname); /* Copy the data */ XALLOC(__FILE__, __FUNCTION__, __LINE__,data, sect->shdr->sh_size, -1); memcpy(data, sect->data, sect->shdr->sh_size); /* Inject new section by top or after bss depending on its type */ writable = elfsh_get_section_writableflag(sect->shdr); /* FreeBSD is incompatible with pre-interp injection */ ELFSH_SELECT_INJECTION(file,writable,mode); if (mode == ELFSH_DATA_INJECTION) modulo = sizeof(eresi_Addr); else { /* modulo = mod; (to be uncommented one day) */ //modulo = elfsh_get_pagesize(file); modulo = sizeof(eresi_Addr); } #if __DEBUG_RELADD__ printf("[DEBUG_RELADD] Mapping new section %s with data = %p \n", enew->name, data); #endif if (elfsh_insert_mapped_section(file, enew, hdr, data, mode, modulo) < 0) goto bad; enew = elfsh_get_section_by_name(file, newname, NULL, NULL, NULL); if (enew == NULL) goto bad; PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); bad: XFREE(__FILE__, __FUNCTION__, __LINE__,newname); PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unable to inject ET_REL section", -1); }
static void def_enum(definition *defp) { token tok; enumval_list *elist; enumval_list **tailp; defp->def_kind = DEF_ENUM; scan(TOK_IDENT, &tok); defp->def_name = tok.str; scan(TOK_LBRACE, &tok); tailp = &defp->def.en.vals; do { scan(TOK_IDENT, &tok); elist = XALLOC(enumval_list); elist->name = tok.str; elist->assignment = NULL; scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok); if (tok.kind == TOK_EQUAL) { scan_num(&tok); elist->assignment = tok.str; scan2(TOK_COMMA, TOK_RBRACE, &tok); } *tailp = elist; tailp = &elist->next; } while (tok.kind != TOK_RBRACE); *tailp = NULL; }
static void def_struct(definition *defp) { token tok; declaration dec; decl_list *decls; decl_list **tailp; defp->def_kind = DEF_STRUCT; scan(TOK_IDENT, &tok); defp->def_name = tok.str; scan(TOK_LBRACE, &tok); tailp = &defp->def.st.decls; do { get_declaration(&dec, DEF_STRUCT); decls = XALLOC(decl_list); decls->decl = dec; *tailp = decls; tailp = &decls->next; scan(TOK_SEMICOLON, &tok); peek(&tok); } while (tok.kind != TOK_RBRACE); get_token(&tok); *tailp = NULL; }
/** * @brief Initialize containers for context * @param ctx mjollnir context */ int mjr_init_containers(mjrcontext_t *ctx) { PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); XALLOC(__FILE__, __FUNCTION__, __LINE__, ctx->reg_containers, sizeof(container_t*) * ctx->cntnrs_size, -1); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * Init an add context * @param input file to init */ int edfmt_add_init(elfshobj_t *file) { PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); cu_obj = file; if (!file) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid file object", NULL); /* Init the object if needed */ if (file->debug_format.uni == NULL) { XALLOC(__FILE__, __FUNCTION__, __LINE__, file->debug_format.uni, sizeof(edfmtinfo_t), NULL); uniinfo = file->debug_format.uni; hash_init(&(uniinfo->htype), API_HTYPE_NAME, 30, ASPECT_TYPE_UNKNOW); hash_init(&(uniinfo->hvar), API_HVAR_NAME, 30, ASPECT_TYPE_UNKNOW); hash_init(&(uniinfo->hfunc), API_HFUNC_NAME, 30, ASPECT_TYPE_UNKNOW); } else { uniinfo = file->debug_format.uni; } PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * @brief Add an element at the head of the list */ int elist_append(list_t *h, char *key, void *data) { listent_t *cur; listent_t *next; int ret; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!h || !key || !data) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid NULL parameters", -1); if (!h->head) { ret = elist_add(h, key, data); if (ret < 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unable to append list element", -1); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); } XALLOC(__FILE__, __FUNCTION__, __LINE__, cur, sizeof(listent_t), -1); cur->key = key; cur->data = data; cur->next = NULL; next = h->head; while (next->next) next = next->next; next->next = cur; h->elmnbr++; PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * @brief Create a new container * @param type Type of element inside container * @param data Data pointer for contained element * @param inlist Input links list if any (else it will be created empty) * @param outlist Output links list if any (else it will be created empty) * @param uniqid Unique ID to be put into name * @return Container newly created */ container_t *container_create(u_int type, void *data, list_t *inlist, list_t *outlist, u_int uniqid) { container_t *newcntnr; aspectype_t *rtype; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); rtype = aspect_type_get_by_id(type); if (!rtype) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unknown container element type", NULL); /* Make sure meta-data is initialized and contiguous with pointed data */ #if defined(DEBUG_LIBASPECT) fprintf(stderr, "Allocating sizeof(container) + (%s type->size = %u) \n", rtype->name, rtype->size); #endif XALLOC(__FILE__, __FUNCTION__, __LINE__, newcntnr, sizeof(container_t) + rtype->size, NULL); newcntnr->data = (char *) newcntnr + sizeof(container_t); newcntnr->type = type; memcpy((char *) newcntnr->data, (char *) data, rtype->size); /* Create lists if not specified */ if (inlist) newcntnr->inlinks = elist_copy(inlist); else container_linklists_create(newcntnr, CONTAINER_LINK_IN, uniqid); if (outlist) newcntnr->outlinks = elist_copy(outlist); else container_linklists_create(newcntnr, CONTAINER_LINK_OUT, uniqid); /* Make sure the container and contained data are contiguous */ PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, newcntnr); }
/** * @brief Register a new vector. A vector is an multidimentional array of hooks * @param name Vector name to be registered * @param defaultfunc Default handler for initializing all vector elements * @param dimensions Dimension arrays for the vector * @param strdims Dimension arrays for dimension description (to be printed) * @param dimsz Number of dimensions * @param vectype Type of elements inside this new vector * @return 0 on success and -1 on error */ int aspect_register_vector(char *name, void *defaultfunc, unsigned int *dimensions, char **strdims, unsigned int dimsz, unsigned int vectype) { vector_t *vector; unsigned long *ptr; void *mem; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!defaultfunc || !dimsz || !dimensions) { write(1, "Invalid NULL parameters\n", 24); return (-1); } if (vectype >= aspect_type_nbr) { write(1, "Invalid vector element type\n", 28); return (-1); } XALLOC(__FILE__, __FUNCTION__, __LINE__, mem, sizeof(vector_t), -1); vector = (vector_t *) mem; XALLOC(__FILE__, __FUNCTION__, __LINE__, mem, dimensions[0] * sizeof(unsigned long), -1); ptr = (unsigned long *) mem; vector->hook = ptr; if (dimsz > 1) aspect_vectors_recalloc((unsigned long *) vector->hook, dimensions, 1, dimsz); vector->arraysz = dimsz; vector->arraydims = dimensions; vector->strdims = strdims; vector->default_func = defaultfunc; hash_add(vector_hash, name, vector); /* Initialize vectored elements */ aspect_vectors_recinit((unsigned long *) vector->hook, dimensions, 1, dimsz, defaultfunc); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, (0)); }
/** * Create an allocation pool used to store different data and optimize performance * This pool didn't realloc the buffer each time it needs more memory, but create a new * buffer and store reference of the previous (like a linked list). * * Reallocation isn't used because I already have some pointer inside in the allocation and * if I made a XREALLOC the whole buffer would be recreate on another memory part, and some * pointers would be lost. * @param pool pool pointer * @param apos pool position * @param asize pool size * @param astep pool step * @param nsize needed size * @return pointer on the allocated memory */ void *edfmt_alloc_pool(char **pool, int *apos, int *asize, int astep, int nsize) { char *prevpool; char *ret; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!pool || !apos || astep <= 0 || !asize || nsize <= 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid parameters", NULL); /* First allocation */ if (*pool == NULL || *asize == 0) { /* We add a void* blanked that indicate that its the first block*/ XALLOC(__FILE__, __FUNCTION__, __LINE__, *pool, astep+sizeof(void*), NULL); *asize = astep; *apos += sizeof(void*); } else if (*apos + nsize >= *asize) { *asize = 0; *apos = 0; do { *asize += astep; } while (*apos + nsize >= *asize); prevpool = *pool; XALLOC(__FILE__, __FUNCTION__, __LINE__, *pool, *asize+sizeof(void*), NULL); *apos += 4; /* Store previous block address then we can retrieve it later */ *(void **) *pool = prevpool; } /* Save current position and update position pointer */ ret = (char *) *pool + *apos; *apos += nsize; PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, (void *) ret); }
/** * Remove a symbol * This function is not e2dbg safe * @param symtab * @param name * @return */ int elfsh_remove_symbol(elfshsect_t *symtab, char *name) { elfsh_Sym *ret; elfsh_Sym *tab; elfsh_Sym *enew; u_int off; u_int movedsz; hash_t *uptable = NULL; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); /* Sanity checks */ if (symtab == NULL || name == NULL) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid parameters", -1); ret = elfsh_get_symbol_by_name(symtab->parent, name); if (ret == NULL) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unknown symbol", -1); /* Do it */ tab = symtab->data; off = (u_long) ret - (u_long) tab; movedsz = symtab->shdr->sh_size - off - sizeof(elfsh_Sym); if (movedsz) memcpy((char *) symtab->data + off, (char *) symtab->data + off + sizeof(elfsh_Sym), movedsz); symtab->shdr->sh_size -= sizeof(elfsh_Sym); symtab->curend -= sizeof(elfsh_Sym); XALLOC(__FILE__, __FUNCTION__, __LINE__,enew, symtab->shdr->sh_size, -1); memcpy(enew, tab, symtab->shdr->sh_size); XFREE(__FILE__, __FUNCTION__, __LINE__,tab); symtab->data = enew; /* We just cant remove the string because of ELF string table format */ elfsh_sync_sorted_symtab(symtab); /* Update hashtable */ switch(symtab->shdr->sh_type) { case SHT_SYMTAB: uptable = &symtab->parent->symhash; break; case SHT_DYNSYM: uptable = &symtab->parent->dynsymhash; break; } if (uptable && uptable->ent) hash_del(uptable, name); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/* * store a value in a list */ void storeval(list **lstp, definition *val) { list **l; list *lst; for (l = lstp; *l != NULL; l = (list **) & (*l)->next); lst = XALLOC(list); lst->val = val; lst->next = NULL; *l = lst; }
/** * Init color structure * @return an allocated color structure */ color_t *revm_colorblank() { color_t *c; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); XALLOC(__FILE__, __FUNCTION__, __LINE__,c, sizeof(color_t), NULL); c->fground = COLOR_NONE; c->bground = COLOR_NONE; c->bold = COLOR_NONE; c->underline = COLOR_NONE; PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, (c)); }
/** * @brief Empty a list */ list_t *elist_empty(char *name) { list_t *list; char *newname; char type; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); list = elist_find(name); if (!list) PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, NULL); type = list->type; hash_del(hash_lists, name); elist_destroy(list); XALLOC(__FILE__, __FUNCTION__, __LINE__, newname, strlen(name) + 1, NULL); strncpy(newname, name, strlen(name)); XALLOC(__FILE__, __FUNCTION__, __LINE__, list, sizeof(list_t), NULL); elist_init(list, newname, type); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, list); }
/* Handle the gdt */ int cmd_gdt() { int ret; int index; int i; listent_t *actual; list_t *h; libkernshsgdt_t *sgdt; char buff[BUFSIZ]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); XALLOC(__FILE__, __FUNCTION__, __LINE__, h, sizeof(list_t), -1); elist_init(h, "cmd_gdt_list", ASPECT_TYPE_UNKNOW); ret = kernsh_gdt(h); memset(buff, '\0', sizeof(buff)); snprintf(buff, sizeof(buff), "%s\n\n", revm_colorfieldstr("[+] GDT")); revm_output(buff); for (i = 0, index = 0, actual = h->head; index < h->elmnbr; i+=8, index++, actual = actual->next) { sgdt = (libkernshsgdt_t *) actual->data; snprintf(buff, sizeof(buff), "%s%s %s %s\n", revm_coloraddress("%.8lX", (eresi_Addr) sgdt->deb), revm_coloraddress("%.8lX", (eresi_Addr) sgdt->fin), revm_colorstr("@"), revm_coloraddress(XFMT, (eresi_Addr) sgdt->addr)); revm_output(buff); revm_endline(); } revm_output("\n"); elist_destroy(h); if (ret) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Cannot get gdt", -1); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * Initialize Input/Output hooks */ int revm_initio() { static int done = 0; revmjob_t *initial; u_int i; NOPROFILER_IN(); if (done) NOPROFILER_ROUT(0); done = 1; XALLOC(__FILE__, __FUNCTION__, __LINE__,initial, sizeof(revmjob_t), -1); memset(initial, 0, sizeof(revmjob_t)); hash_init(&initial->recur[0].exprs , "job0_rec0_exprs" , 23, ASPECT_TYPE_EXPR); hash_init(&initial->recur[0].labels, "job0_rec0_labels", 23, ASPECT_TYPE_STR); revm_std_io(initial); initial->ws.active = 1; initial->ws.createtime = time(&initial->ws.createtime); world.initial = world.curjob = initial; hash_init(&world.jobs, "jobs", 11, ASPECT_TYPE_UNKNOW); hash_add(&world.jobs, "local", initial); initial->ws.name = strdup("local"); hash_init(&initial->loaded, "initial_loaded_files", 51, ASPECT_TYPE_UNKNOW); hash_init(&initial->dbgloaded, "initial_dbgloaded_files", 11, ASPECT_TYPE_UNKNOW); for (i = 0; i < REVM_MAXSRCNEST; i++) { initial->recur[i].script = NULL; initial->recur[i].lstcmd = NULL; initial->iter[i].elmidx = REVM_IDX_UNINIT; } initial->recur[0].funcname = "top-level"; profiler_setcolor(revm_endline, revm_colorinstr, revm_colorstr, revm_colorfieldstr, revm_colortypestr, revm_colorend, revm_colorwarn, revm_colorfunction, revm_colorfilename); profiler_setmorecolor(revm_coloradv, revm_colorinstr_fmt, revm_coloraddress, revm_colornumber, revm_colorstr_fmt, revm_colorfieldstr_fmt, revm_colortypestr_fmt, revm_colorwarn_fmt); NOPROFILER_ROUT(0); }
int cmd_kmem_read() { int ret, len; char *new_buff; eresi_Addr addr; revmlist_t *actual; char buff[BUFSIZ]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); ret = len = 0; addr = 0; actual = world.curjob->curcmd->disasm + 0; if (actual) { kernsh_addrlen(actual, &addr, &len); XALLOC(__FILE__, __FUNCTION__, __LINE__, new_buff, len, -1); memset(new_buff, '\0', len); memset(buff, '\0', sizeof(buff)); snprintf(buff, sizeof(buff), "Reading kernel memory %s %s strlen(%s)\n\n", revm_colorstr("@"), revm_coloraddress(XFMT, (eresi_Addr) addr), revm_colornumber("%u", len)); revm_output(buff); ret = elfsh_readmema(libkernshworld.root, addr, new_buff, len); kernsh_hexdump((unsigned char *)new_buff, len, addr); XFREE(__FILE__, __FUNCTION__, __LINE__, new_buff); } if (ret != len) { PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Impossible to read mem", -1); } PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/* #SDTREF(TEXT,"C:\\Program Files (x86)\\IBM\\Rational\\TAU\\4.3\\addins\\sdlkernels\\ADT\\BasicCTypes.pr",197,15) */ extern SDL_Charstring z_U2ctypes_H1O0_ToString(z_U2ctypes_H1_ptr_char C) { SDL_Charstring Result; int i = 0; if (C != 0) { i = strlen(C); } Result = (SDL_Charstring)XALLOC(i+2, (tSDLTypeInfo *)&ySDL_SDL_Charstring); if (i>0) { memcpy(Result+1, C, i); } Result[0] = 'T'; Result[i+1] = '\0'; return Result; }
/* Handle the sys_call_table */ int cmd_sct() { int ret; int index; listent_t *actual; list_t *h; libkernshsyscall_t *sct; char buff[BUFSIZ]; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); XALLOC(__FILE__, __FUNCTION__, __LINE__, h, sizeof(list_t), -1); elist_init(h, "cmd_sct_list", ASPECT_TYPE_UNKNOW); ret = kernsh_sct(h); memset(buff, '\0', sizeof(buff)); snprintf(buff, sizeof(buff), "%s\n\n", revm_colorfieldstr("[+] SYS_CALL_TABLE")); revm_output(buff); for (index = 0, actual = h->head; index < h->elmnbr; index++, actual = actual->next) { sct = (libkernshsyscall_t *) actual->data; memset(buff, '\0', sizeof(buff)); snprintf(buff, sizeof(buff), "%s %-40s %s %s\n", revm_colornumber("id:%-10u", (unsigned int)index), revm_colortypestr_fmt("%s", sct->name), revm_colorstr("@"), revm_coloraddress(XFMT, (eresi_Addr) sct->addr)); revm_output(buff); revm_endline(); } revm_output("\n"); elist_destroy(h); if (ret) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Cannot get syscalltable", -1); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
extern z_U2ctypes_H1_ptr_char z_U2ctypes_H1O1_To_ptr_char(SDL_Charstring C) { z_U2ctypes_H1_ptr_char Result; int i = 0; if (C != (SDL_Charstring)0) { i = strlen(C)-1; } Result = (z_U2ctypes_H1_ptr_char)XALLOC(i+1, (tSDLTypeInfo *)&ySDL_z_U2ctypes_H1_ptr_char); if (i>0) { memcpy(Result, C+1, i); } Result[i] = '\0'; if ((C != 0) && (C[0] == 'T')) { xFree_SDL_Charstring((void **)&C); } return Result; }
/** * @brief Initialize the hash table * @param h Pointer to the hash to initialize * @param name Name of the hash. * @param size Size to document * @param type Type to document * @param Returns 0 on success, -1 on error or 1 if hash already exists. */ int hash_init(hash_t *h, char *name, int size, u_int type) { NOPROFILER_IN(); /* First checks */ /* Initialize the global hash table of lists and the global hash table of hash tables */ if (!hash_hash) { hash_hash = (hash_t *) calloc(sizeof(hash_t), 1); hash_init(hash_hash, "hashes", 51, ASPECT_TYPE_UNKNOW); } if (type >= aspect_type_nbr) { fprintf(stderr, "Unable to initialize hash table %s \n", name); PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Unable to initialize hash table", -1); } if (h != hash_hash && hash_find(name) && h->ent) { #if __DEBUG__ fprint(stderr, "Hash table already exists and initialized\n"); #endif NOPROFILER_ROUT(1); } //printf("INIT HASH %s \n", name); /* Add a new element */ XALLOC(__FILE__, __FUNCTION__, __LINE__, h->ent, size * sizeof(listent_t), -1); h->size = size; h->type = type; h->elmnbr = 0; h->linearity = 0; h->name = name; hash_add(hash_hash, name, h); if (!hash_lists) { hash_lists = (hash_t *) calloc(sizeof(hash_t), 1); hash_init(hash_lists, "lists", 51, ASPECT_TYPE_UNKNOW); } NOPROFILER_ROUT(0); }
/** * @brief Add an element at the head of the list */ int elist_add(list_t *h, char *key, void *data) { listent_t *cur; listent_t *next; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!h || !key || !data) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid NULL parameters", -1); XALLOC(__FILE__, __FUNCTION__, __LINE__, cur, sizeof(listent_t), -1); next = h->head; cur->key = key; cur->data = data; cur->next = next; h->head = cur; h->elmnbr++; PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * Init the global type table * * @param name * @return */ int revm_type_hashcreate(char *name) { char hashname[BUFSIZ]; hash_t *newhash; aspectype_t *type; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); /* Create the hash table for objects of that type */ type = aspect_type_get_by_name(name); if (!type) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Type subject does not exist", -1); snprintf(hashname, sizeof(hashname), "type_%s", name); XALLOC(__FILE__, __FUNCTION__, __LINE__, newhash, sizeof(hash_t), -1); hash_init(newhash, strdup(hashname), 11, type->type); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * @brief Add an entry to the hash table. * @param h Hash table. * @param key Key of the new entry. * @param data Value associated with the key. * @return Returns an index (to document) */ int hash_add(hash_t *h, char *key, void *data) { listent_t *actual; listent_t *newent; char *backup; u_int index; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (!h || !key) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid NULL parameters", -1); /* If the element already exist, make sure we erase the existing one */ actual = hash_get(h, key); if (actual) PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, hash_set(h, key, data)); newent = NULL; for (index = 0, backup = key; *backup; backup++) index += *backup; index %= h->size; if (h->ent[index].key == NULL) { h->ent[index].key = key; h->ent[index].data = data; } else { XALLOC(__FILE__, __FUNCTION__, __LINE__, newent, sizeof(listent_t), -1); newent->key = key; newent->data = data; actual = h->ent + index; while (actual->next) actual = actual->next; actual->next = newent; } h->elmnbr++; PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, index); }
/* * @brief Add a breakpoint */ int elfsh_bp_add(hash_t *bps, elfshobj_t *file, char *resolv, eresi_Addr addr) { static int lastbpid = 1; elfshbp_t *bp; char tmp[32]; int ret; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); if (file == NULL || addr == 0 || bps == 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Invalid NULL parameter", -1); /* Breakpoints handlers must be initialized */ elfsh_setup_hooks(); XALLOC(__FILE__, __FUNCTION__, __LINE__,bp , sizeof(elfshbp_t), (-1)); bp->obj = file; bp->type = INSTR; bp->addr = addr; bp->symname = strdup(resolv); snprintf(tmp, 32, XFMT, addr); if (hash_get(bps, tmp)) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Breakpoint already exist", -1); /* Call the architecture dependent hook for breakpoints */ ret = e2dbg_setbreak(file, bp); if (ret < 0) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Breakpoint insertion failed", (-1)); /* Add new breakpoint to hash table */ bp->id = lastbpid++; hash_add(bps, strdup(tmp), bp); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
void AStarPathFind::InitMapData(int nMapID) { m_poMapConf = ConfMgr::Instance()->GetMapMgr()->GetConf(nMapID); if (m_poMapConf == NULL) { return; } int nGridNum = m_poMapConf->nUnitNumX * m_poMapConf->nUnitNumY; m_pMapData = (ASTAR_NODE*)XALLOC(m_pMapData, nGridNum * sizeof(ASTAR_NODE)); for (int i = 0; i < m_poMapConf->nUnitNumY; i++) { for (int j = 0; j < m_poMapConf->nUnitNumX; j++) { int32_t nIdx = i * m_poMapConf->nUnitNumX + j; m_pMapData[nIdx].nX = j; m_pMapData[nIdx].nY = i; m_pMapData[nIdx].Reset(); } } m_uSearchVersion = 0; }
/** * Do a copy meta-representation of a datatype * @param from Source type name to copy from * @param to Destination type name to copy into * @return 0 on success, or < 0 on error. */ int revm_type_copy(char *from, char *to) { aspectype_t *tocopy, *newtype; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); tocopy = (aspectype_t *) hash_get(&types_hash, from); if (!tocopy) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Type not found", -1); newtype = (aspectype_t *) hash_get(&types_hash, to); if (newtype) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Type destination already exist", -1); XALLOC(__FILE__, __FUNCTION__, __LINE__, newtype, sizeof(aspectype_t), -1); memcpy(newtype, tocopy, sizeof(aspectype_t)); newtype->name = strdup(to); aspect_type_register_real(newtype->name, newtype); revm_type_hashcreate(newtype->name); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * Beginning of the transform command, open a transformation switch */ int cmd_match() { list_t *list; revmexpr_t *ind; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); /* The first time we enter this command, we have to fetch the params */ list = (list_t *) world.curjob->iter[world.curjob->curloop].container; ind = world.curjob->iter[world.curjob->curloop].curind; if (!list || !ind || strcmp(ind->label, world.curjob->curcmd->param[0]) || list->type != ASPECT_TYPE_EXPR) { PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Match/Rewrite only acts on iterated lists of expressions", -1); } /* We assume that the rewritten expression is the induction variable */ world.curjob->recur[world.curjob->curscope].rwrt.matchexpr = ind; /* Create or flush the transformed expressions output list */ list = elist_find("transformed"); if (list) { elist_empty(list->name); world.curjob->recur[world.curjob->curscope].rwrt.transformed = list; } else { XALLOC(__FILE__, __FUNCTION__, __LINE__, world.curjob->recur[world.curjob->curscope].rwrt.transformed, sizeof(list_t), -1); elist_init(world.curjob->recur[world.curjob->curscope].rwrt.transformed, "transformed", ASPECT_TYPE_EXPR); } world.curjob->recur[world.curjob->curscope].rwrt.idloop = world.curjob->curloop; PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/** * @brief Set a list by its name (overwrite if existing ) */ int elist_register(list_t *list, char *name) { list_t *h; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); h = hash_get(hash_lists, name); if (h) { if (h->type == ASPECT_TYPE_UNKNOW) h->type = list->type; if (h->type != list->type) PROFILER_ERR(__FILE__, __FUNCTION__, __LINE__, "Incompatible lists", -1); if (h->elmnbr) h = elist_empty(name); elist_merge(h, list); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); } XALLOC(__FILE__, __FUNCTION__, __LINE__, h, sizeof(list_t), -1); elist_init(h, name, h->type); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, 0); }
/* Reverse a list */ list_t *elist_reverse(list_t *l) { list_t *newlist; listent_t *nextent; listent_t *curent; PROFILER_IN(__FILE__, __FUNCTION__, __LINE__); hash_del(hash_lists, l->name); XALLOC(__FILE__, __FUNCTION__, __LINE__, newlist, sizeof(list_t), NULL); elist_init(newlist, l->name, l->type); /* Copy the list inserting at the beginning all the time */ for (curent = l->head; curent; curent = nextent) { elist_add(newlist, curent->key, curent->data); nextent = curent->next; XFREE(__FILE__, __FUNCTION__, __LINE__, curent); } XFREE(__FILE__, __FUNCTION__, __LINE__, l); PROFILER_ROUT(__FILE__, __FUNCTION__, __LINE__, newlist); }