static void clean_call_mem_stats(reg_t memvalue){ dr_mem_info_t info; uint base; uint limit; uint reserve; void * drcontext = dr_get_current_drcontext(); dr_switch_to_app_state(drcontext); dr_query_memory_ex(memvalue, &info); dr_printf("mem - %d, base_pc - %d , size - %d, prot - %d, type - %d\n", memvalue, info.base_pc, info.size, info.prot, info.type); __asm{ mov EAX, FS : [0x04] mov[base], EAX mov EAX, FS : [0x08] mov[limit], EAX mov EAX, FS : [0xE0C] mov[reserve], EAX } dr_printf("stack information - %d %d %d\n", base, limit, reserve); dr_switch_to_dr_state(drcontext); }
app_pc get_heap_start(void) { static app_pc heap_start; /* cached value */ if (heap_start == NULL) { app_pc cur_brk = get_brk(true/*pre-us*/); dr_mem_info_t info; module_data_t *data; /* Locate the heap */ if (!dr_query_memory_ex(cur_brk - 1, &info)) { ASSERT(false, "cannot find heap region"); return NULL; } if (info.type == DR_MEMTYPE_FREE || info.type == DR_MEMTYPE_IMAGE || !TEST(DR_MEMPROT_WRITE, info.prot)) { /* Heap is empty */ heap_start = cur_brk; } else { ASSERT(!dr_memory_is_dr_internal(info.base_pc), "heap location error"); /* we no longer assert that these are equal b/c -replace_malloc * has extended the brk already */ ASSERT(info.base_pc + info.size >= cur_brk, "heap location error"); heap_start = info.base_pc; /* workaround for PR 618178 where /proc/maps is wrong on suse * and lists last 2 pages of executable as heap! */ /* On some old Linux kernel, the heap might be right after the bss * segment. DR's map iterator used by dr_query_memory_ex cannot * split bss out of heap. * We use dr_lookup_module to find the right bounds of bss so that * we can check whether the base is bss, existing heap, or merge of * the two. */ /* XXX: we still cannot handle the case that the application creates * memory right before the heap. */ data = dr_lookup_module(info.base_pc); if (data != NULL) { if (data->start < heap_start && data->end > heap_start) { heap_start = (byte *) ALIGN_FORWARD(data->end, PAGE_SIZE); LOG(1, "WARNING: workaround for invalid heap_start "PFX" => "PFX"\n", info.base_pc, heap_start); } dr_free_module_data(data); } } } return heap_start; }
static void memory_iteration_test(void) { dr_mem_info_t info; byte *pc = NULL; while (true) { bool res = dr_query_memory_ex(pc, &info); if (!res) { ASSERT(info.type == DR_MEMTYPE_ERROR IF_WINDOWS(|| info.type == DR_MEMTYPE_ERROR_WINKERNEL)); if (info.type == DR_MEMTYPE_ERROR) dr_fprintf(STDERR, "error: memory iteration failed\n"); break; } ASSERT(info.type != DR_MEMTYPE_ERROR IF_WINDOWS(&& info.type != DR_MEMTYPE_ERROR_WINKERNEL)); if (POINTER_OVERFLOW_ON_ADD(pc, info.size)) break; pc += info.size; }
static void raw_alloc_test(void) { uint prot; char *array = PREFERRED_ADDR; dr_mem_info_t info; bool res; dr_fprintf(STDERR, " testing raw memory alloc..."); res = dr_raw_mem_alloc(PAGE_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE, array) != NULL; if (!res) { dr_fprintf(STDERR, "[error: fail to alloc at "PFX"]\n", array); return; } write_array(array); dr_query_memory((const byte *)array, NULL, NULL, &prot); if (prot != get_os_mem_prot(DR_MEMPROT_READ|DR_MEMPROT_WRITE)) dr_fprintf(STDERR, "[error: prot %d doesn't match rw]\n", prot); dr_raw_mem_free(array, PAGE_SIZE); dr_query_memory_ex((const byte *)array, &info); if (info.prot != DR_MEMPROT_NONE) dr_fprintf(STDERR, "[error: prot %d doesn't match none]\n", info.prot); dr_fprintf(STDERR, "success\n"); }
static void raw_alloc_test(void) { uint prot; char *array, *preferred; dr_mem_info_t info; bool res; dr_fprintf(STDERR, " testing raw memory alloc..."); /* Find a free region of memory without inadvertently "preloading" it. * First probe by allocating 2x the platform allocation alignment unit. */ array = dr_raw_mem_alloc(HINT_ALLOC_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE, NULL); /* Then select the second half as the preferred address for the allocation test. */ preferred = (void *)((ptr_uint_t)array + HINT_OFFSET); /* Free the probe allocation. */ dr_raw_mem_free(array, HINT_ALLOC_SIZE); array = preferred; /* Now `array` is guaranteed to be available. */ res = dr_raw_mem_alloc(PAGE_SIZE, DR_MEMPROT_READ | DR_MEMPROT_WRITE, array) != NULL; if (!res) { dr_fprintf(STDERR, "[error: fail to alloc at "PFX"]\n", array); return; } write_array(array); dr_query_memory((const byte *)array, NULL, NULL, &prot); if (prot != get_os_mem_prot(DR_MEMPROT_READ|DR_MEMPROT_WRITE)) dr_fprintf(STDERR, "[error: prot %d doesn't match rw]\n", prot); dr_raw_mem_free(array, PAGE_SIZE); dr_query_memory_ex((const byte *)array, &info); if (info.prot != DR_MEMPROT_NONE) dr_fprintf(STDERR, "[error: prot %d doesn't match none]\n", info.prot); dr_fprintf(STDERR, "success\n"); }