static int structTest(void) { struct SmallTestStruct_t { uint16_t member1; uint16_t member2; uint32_t member3; }; struct LargeTestStruct_t { uint32_t member1; uint32_t member2; uint32_t member3; uint32_t member4; }; // Make sure the small test struct warrants a small buffer (8 bytes) // and the large struct warrants a large buffer (32 bytes). expectEquals(sizeof(struct SmallTestStruct_t), 8); expectEquals(sizeof(struct LargeTestStruct_t), 16); heapInit(); // Allocate memory for a small struct and make sure it functions properly. struct SmallTestStruct_t *smallStructPointer = (struct SmallTestStruct_t *) heapAlloc(sizeof(struct SmallTestStruct_t)); struct SmallTestStruct_t smallStruct; expect(smallStructPointer != NULL); smallStructPointer->member1 = 1; smallStructPointer->member2 = 2; smallStructPointer->member3 = 3; expectEquals(smallStructPointer->member1, 1); expectEquals(smallStructPointer->member2, 2); expectEquals(smallStructPointer->member3, 3); anmatMemcpy(&smallStruct, smallStructPointer, sizeof(struct SmallTestStruct_t)); expectEquals(smallStruct.member1, 1); expectEquals(smallStruct.member2, 2); expectEquals(smallStruct.member3, 3); // Same thing with a large struct. struct LargeTestStruct_t *largeStructPointer = (struct LargeTestStruct_t *) heapAlloc(sizeof(struct LargeTestStruct_t)); struct LargeTestStruct_t largeStruct; expect(largeStructPointer != NULL); largeStructPointer->member1 = 1; largeStructPointer->member2 = 2; largeStructPointer->member3 = 3; largeStructPointer->member4 = 4; expectEquals(largeStructPointer->member1, 1); expectEquals(largeStructPointer->member2, 2); expectEquals(largeStructPointer->member3, 3); expectEquals(largeStructPointer->member4, 4); anmatMemcpy(&largeStruct, largeStructPointer, sizeof(struct LargeTestStruct_t)); expectEquals(largeStruct.member1, 1); expectEquals(largeStruct.member2, 2); expectEquals(largeStruct.member3, 3); expectEquals(largeStruct.member4, 4); return 0; }
strArray_t* strArrayInit(heap_t* h, int initSize) { strArray_t* p = heapAlloc(h, sizeof(strArray_t)); p->array = NULL; p->num = 0; p->allocSize = initSize; if (initSize > 0) { p->array = heapAlloc(h, initSize * (sizeof(char*))); } return p; }
static int doubleTest(void) { double *pointers[2] = {NULL,NULL,}; // At the beginning of allocation, we should have all of the bytes. expectHeapEmpty(); // Can't allocate 0 bytes! pointers[0] = heapAlloc(0); expect(pointers[0] == NULL); // The number of free bytes should still be the total heap. expectHeapEmpty(); // Allocating 1 double (8 bytes) should be no problem. // The heap should automatically initialize itself the first time we alloc. pointers[0] = (double *)heapAlloc(1 * sizeof(double)); expect(pointers[0] != NULL); pointers[0][0] = 12.345; expect(*pointers[0] == 12.345); // We should have 9 bytes missing from the heap, since allocations need one // extra byte for length purposes. expectHeapSize(HEAP_SIZE - 9); // If we free the memory, we should get back all of our bytes. heapFree(pointers[0]); expectHeapEmpty(); // How about if we free one 16 byte chunk and one 24 byte chunk. pointers[0] = NULL; pointers[0] = (double *)heapAlloc(2 * sizeof(double)); expect(pointers[0] != NULL); pointers[1] = (double *)heapAlloc(3 * sizeof(double)); expect(pointers[1] != NULL); // We should have 16 + 1 + 24 + 1 bytes missing. expectHeapSize(HEAP_SIZE - 16 - 1 - 24 - 1); // If we free the 24 byte chunk, we should have only 16 + 1 bytes missing. heapFree(pointers[1]); expectHeapSize(HEAP_SIZE - 16 - 1); // After we free the 16 byte chunk, we should be back to 0 bytes missing. heapFree(pointers[0]); expectHeapEmpty(); return 0; }
int spiMasterRequest(uint8_t busId, struct SpiDevice **dev_out) { int ret = 0; struct SpiDeviceState *state = heapAlloc(sizeof(*state)); if (!state) return -ENOMEM; struct SpiDevice *dev = &state->dev; ret = spiRequest(dev, busId); if (ret < 0) goto err_request; if (!dev->ops->masterRxTx) { ret = -EOPNOTSUPP; goto err_opsupp; } *dev_out = dev; return 0; err_opsupp: if (dev->ops->release) dev->ops->release(dev); err_request: heapFree(state); return ret; }
static void *allocMem( int size,int flags ){ size=(size + sizeof(GCBlock) + 15) & ~15; int i=size/16; GCBlock *t; if( i<256 && (t=freeBlocks[i]) ){ freeBlocks[i]=t->succ; }else{ static int alloced; if( gc_mode==BBGC_AUTOMATIC && (gc_alloced-alloced)>heap_size/3 ){ collectMem(0); alloced=gc_alloced; } if( i>255 ){ t=heapAlloc( size ); setMemBit( t ); }else if( t=freeBlocks[i] ){ freeBlocks[i]=t->succ; }else{ if( size>freeBufSize ){ if( freeBufSize ){ int i=freeBufSize/16; GCBlock *t=freeBuf; t->flags=BBGC_MARKED; t->succ=freeBlocks[i]; freeBlocks[i]=t; setMemBit( t ); } freeBufSize=65536; freeBuf=heapAlloc( freeBufSize ); } t=freeBuf; freeBuf+=size; freeBufSize-=size; setMemBit( t ); } } t->succ=usedBlocks; t->flags=size|flags; usedBlocks=t; gc_alloced+=size; return t->data; }
// Usage: ./client <fileIn> <fileOut> <fileOut2> <heapSize> <rowCount> <rowLength> int main (int argc, char* argv[]) { char* nameFileIn = argv[1]; char* nameFileOut = argv[2]; char* nameFileOut2 = argv[3]; char** page; FILE* fpI,* fpO; int nr; heap_t* heap; strArray_t* save; int D = atoi(argv[4]), N = atoi(argv[5]), M = atoi(argv[6]); printf("input file: %s\noutput file: %s\noutput file 2: %s\n", nameFileIn, nameFileOut, nameFileOut2); printf("D: %d - N: %d - M: %d\n", D, N, M); heap = heapCreate(D); save = strArrayInit(heap, 0); /* inizializza a vuoto */ page = (char**)heapAlloc(heap, N * sizeof(char*)); fpI = fopen(nameFileIn, "r"); fpO = fopen(nameFileOut, "w"); if (page == NULL || fpI == NULL || fpO == NULL) { printf("Errore in allocazione memoria o apertura file\n"); exit(1); } while ((nr = leggiPagina(heap, page, fpI, N)) > 0) { ordinaPagina(page, nr); scriviPagina(page, fpO, nr); filtraeLiberaPagina(heap, page, nr, save, M); } fclose(fpI); fclose(fpO); heapFree(heap, page); fpO = fopen(nameFileOut2, "w"); if (fpO == NULL) { printf("Errore in apertura file\n"); exit(1); } strArraySort(save); strArrayPrint(save, fpO); fclose(fpO); strArrayFree(heap, save); heapDestroy(heap); return 0; }
char *get_fn_by_range(handle_t *h, uint64_t vaddr) { char *buf = heapAlloc(512); for (int i = 0; i < h->lsc; i++) { if (vaddr >= h->lsyms[i].value && vaddr < h->lsyms[i].value + h->lsyms[i].size) { strncpy(buf, h->lsyms[i].name, sizeof(buf) - 3); strcat(buf, "()"); return buf; } } return NULL; }
static int stressTest(void) { unsigned char *pointers[3] = {NULL, NULL, NULL,}; // Initializing the heap should mean all the bytes are available. heapInit(); expectHeapEmpty(); // Let's allocate the whole heap with two chunks. One will be // (HEAP_SIZE / 4 - 1) bytes and the other will be // ((3 * HEAP_SIZE / 4) - 1) bytes. pointers[0] = (unsigned char *)heapAlloc((HEAP_SIZE / 4) - 1); expect(pointers[0] != NULL); pointers[1] = (unsigned char *)heapAlloc(((3 * HEAP_SIZE) / 4) - 1); expect(pointers[1] != NULL); // There should be no bytes left. expectHeapFull(); // Therefore, we should not be able to allocate anymore. pointers[2] = (unsigned char *)heapAlloc(1); expect(pointers[2] == NULL); // If we free the first guy, we should only be missing the second big chunk. heapFree(pointers[0]); expectHeapSize(HEAP_SIZE - (((3 * HEAP_SIZE) / 4) - 1) - 1); // If we try to allocate some memory that is equal to the bytes left, // then we should fail, because we need any extra byte to mark the end of // the reference. pointers[2] = heapAlloc(heapFreeBytesCount); expect(pointers[2] == NULL); // Make sure when we free the second chunk, we have a full heap available. heapFree(pointers[1]); expectHeapEmpty(); return 0; }
static int arrayTest(void) { int **matrix, i; heapInit(); expectHeapEmpty(); // Allocate room for 3 pointers. matrix = (int **)heapAlloc(3 * sizeof(int *)); expectHeapSize(HEAP_SIZE - (3 * (sizeof(int *))) - 1 // 3 pointers and 1 alloc byte - 0); // Allocate 5 integers in each array. for (i = 0; i < 3; i ++) { matrix[i] = (int *)heapAlloc(5 * sizeof(int)); } expectHeapSize(HEAP_SIZE // 3 pointers and 1 alloc byte - (3 * (sizeof(int *))) - 1 // 3 arrays of 5 int's plus an alloc byte each - 3 * ((5 * sizeof(int)) + 1) - 0); // Free the arrays. for (i = 0; i < 3; i ++) { heapFree(matrix[i]); } expectHeapSize(HEAP_SIZE - (3 * (sizeof(int *))) - 1 // 3 pointers and 1 alloc byte - 0); // Free the pointers. heapFree(matrix); expectHeapEmpty(); return 0; }
int leggiPagina(heap_t* h, char** page, FILE* fp, int maxR) { int nr = 0, len; char buf[MAXLINELEN+1]; for (nr = 0; nr < maxR; nr++) { if (fgets(buf, MAXLINELEN, fp) == NULL) break; /* rimuovi eventuale a-capo */ len = strlen(buf); if (buf[len-1]=='\n') { buf[len-1] = '\0'; len--; } page[nr] = heapAlloc(h, (len + 1) * sizeof(char)); strcpy(page[nr],buf); } return nr; }
void strArrayPush(heap_t* h, strArray_t* sarray, char* s) { if (sarray->num + 1 >= sarray->allocSize) { /* rialloca */ int i; char** old = sarray->array; sarray->allocSize *= 2; if (sarray->allocSize == 0) sarray->allocSize = 2; sarray->array = heapAlloc(h, sarray->allocSize * (sizeof(char*))); if (old != NULL) { for (i = 0; i < sarray->num; i++) { sarray->array[i] = old[i]; } heapFree(h, old); } } sarray->array[sarray->num++] = s; }
int main() { int i,n,x; heap *p=NULL; p=heapAlloc(); scanf("%d",&n); heapInit(p,sizeof(int),n+1,gt); for(i=0;i<n/2;i++) { x=i+n/2; heapPush(p,&x); } for(i=0;i<n/2;i++) heapPush(p,&i); while(!heapEmpty(p)) { heapPop(p,&x); printf("%d%c",x,heapEmpty(p)?'\n':' '); } return 0; }
int spiSlaveRequest(uint8_t busId, const struct SpiMode *mode, struct SpiDevice **dev_out) { int ret = 0; struct SpiDeviceState *state = heapAlloc(sizeof(*state)); if (!state) return -ENOMEM; struct SpiDevice *dev = &state->dev; ret = spiRequest(dev, busId); if (ret < 0) goto err_request; if (!dev->ops->slaveIdle || !dev->ops->slaveRxTx) { ret = -EOPNOTSUPP; goto err_opsupp; } state->mode = *mode; state->err = 0; ret = spiSlaveStart(state, mode); if (ret < 0) goto err_opsupp; *dev_out = dev; return 0; err_opsupp: if (dev->ops->release) dev->ops->release(dev); err_request: heapFree(state); return ret; }
int main(int argc, char **argv, char **envp) { handle_t *handle; char *token = NULL, **args; int ac, i, status, ret; pid_t pid; long ptraceOpts; char *argv0; if (argc < 3) usage(); if (argv[1][0] != '-') usage(); /* * XXX handle is huge because of branch_site (Which is a massive array) * so we must use heap. In the future move branch_site from array to * list or tree for fast lookup. */ handle = (handle_t *)heapAlloc(sizeof(handle_t)); memset((void *)handle, 0, sizeof(handle_t)); memset((void *)&opts, 0, sizeof(opts)); if (argv[1][0] != '-') usage(); if (argv[1][1] != 'b' && argv[1][1] != 'p') { for (token = (argv[1] + 1); *token != '\0'; token++) { switch(*token) { case 'v': opts.verbose++; break; case 's': opts.strings++; break; case 'c': opts.cflow++; break; case 'e': opts.elfdata++; break; case 'f': opts.ehframe++; break; case 't': opts.threads++; break; case 'd': opts.debug++; break; default: printf("Unknown option: '%c'\n", *token); usage(); } } if (argc < 4) usage(); if (argv[2][0] != '-') usage(); if (argv[2][1] == 'b') handle->path = strdup(argv[3]); else if (argv[2][1] == 'p') handle->pid = atoi(argv[3]); else usage(); argv0 = handle->path ? xstrdup(argv[3]) : get_path(handle->pid); ac = parse_sub_args(&argv[4], argc - 4, &handle->args, argv0); } else { if (argv[1][0] != '-') usage(); if (argv[1][1] == 'b') handle->path = strdup(argv[2]); else if (argv[1][1] == 'p') handle->pid = atoi(argv[2]); else usage(); argv0 = handle->path ? xstrdup(argv[2]) : get_path(handle->pid); ac = parse_sub_args(&argv[3], argc - 3, &handle->args, argv0); } switch(__ELF_NATIVE_CLASS) { case 32: opts.arch = 32; break; case 64: opts.arch = 64; break; default: fprintf(stderr, "[!] Unsupported architecture: %d\n", __ELF_NATIVE_CLASS); exit(0); } handle->arch = opts.arch; pid = handle->pid; if (pid) { if (opts.verbose) printf("[+] Attaching to pid: %d\n", pid); opts.attach++; handle->path = get_path(handle->pid); } if (!validate_em_type(handle->path)) { printf("[!] ELF Architecture is set to %d, the target %s is not the same architecture\n", opts.arch, handle->path); exit(-1); } /* * process_binary() will create the mapping of branch instructions * and layout of the executable, so that we can instrument it before * execution. */ if ((ret = process_binary(handle) < 0)) { fprintf(stderr, "process_binary() failed on [%s]\n", handle->path); exit(-1); } if (!opts.attach) { if ((pid = fork()) < 0) { perror("fork"); exit(-1); } if (pid == 0) { if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) == -1) { perror("PTRACE_TRACEME"); exit(-1); } ptraceOpts = PTRACE_O_TRACECLONE|PTRACE_O_TRACEFORK|PTRACE_O_TRACEEXEC|PTRACE_O_TRACEEXIT|PTRACE_O_EXITKILL; ptrace(PTRACE_SETOPTIONS, 0, 0, ptraceOpts); execve(handle->path, handle->args, envp); exit(0); } wait(&status); handle->pid = pid; if (opts.debug) printf("[+] Calling instrument_process()\n"); instrument_process(handle); if (opts.debug) printf("[+] Calling examine_process()\n"); examine_process(handle); goto done; } printf("Attaching to %d\n", handle->pid); if (ptrace(PTRACE_ATTACH, handle->pid, NULL, NULL) == -1) { perror("PTRACE_ATTACH"); exit(-1); } wait(&status); // waitpid(handle->pid, &status, WUNTRACED); instrument_process(handle); examine_process(handle); for (i = 0; i < ac; i++) printf("arg[%d]: %s\n", i, handle->args[i]); done: free(handle); exit(0); }
/* Garbage collector */ static void gc(long c) { any p; heap *h; int i; h = Heaps; do { p = h->cells + CELLS-1; do *(long*)&cdr(p) |= 1; while (--p >= h->cells); } while (h = h->next); /* Mark */ mark(Nil+1); mark(Intern[0]), mark(Intern[1]); mark(Transient[0]), mark(Transient[1]); mark(ApplyArgs), mark(ApplyBody); mark(Reloc); for (p = Env.stack; p; p = cdr(p)) mark(car(p)); for (p = (any)Env.bind; p; p = (any)((bindFrame*)p)->link) for (i = ((bindFrame*)p)->cnt; --i >= 0;) { mark(((bindFrame*)p)->bnd[i].sym); mark(((bindFrame*)p)->bnd[i].val); } for (p = (any)CatchPtr; p; p = (any)((catchFrame*)p)->link) { if (((catchFrame*)p)->tag) mark(((catchFrame*)p)->tag); mark(((catchFrame*)p)->fin); } /* Sweep */ Avail = NULL; h = Heaps; if (c) { do { p = h->cells + CELLS-1; do if (num(p->cdr) & 1) Free(p), --c; while (--p >= h->cells); } while (h = h->next); while (c >= 0) heapAlloc(), c -= CELLS; } else { heap **hp = &Heaps; cell *av; do { c = CELLS; av = Avail; p = h->cells + CELLS-1; do if (num(p->cdr) & 1) Free(p), --c; while (--p >= h->cells); if (c) hp = &h->next, h = h->next; else Avail = av, h = h->next, free(*hp), *hp = h; } while (h); } }
//lzma allocator functions static void *SzAlloc(void *, size_t size) { return heapAlloc(size); }