Skip to content

Commit

Permalink
Merge pull request #167 from JayFoxRox/alloc-mem
Browse files Browse the repository at this point in the history
Allocate memory in MapMemory
  • Loading branch information
JayFoxRox committed Jun 6, 2019
2 parents 7815f6b + 3f39517 commit dc3cd15
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
17 changes: 7 additions & 10 deletions emulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,17 @@ void DumpProfilingHeat(const char* path) {
}
}

void MapMemory(void* memory, uint32_t address, uint32_t size, bool read, bool write, bool execute) {
void* MapMemory(uint32_t address, uint32_t size, bool read, bool write, bool execute) {
//FIXME: Permissions!
uc_err err;
assert(size % ucAlignment == 0);
void* memory = aligned_alloc(ucAlignment, size);
err = uc_mem_map_ptr(uc, address, size, UC_PROT_ALL, memory);
if (err) {
printf("Failed on uc_mem_map_ptr() with error returned %u: %s\n", err, uc_strerror(err));
}
//FIXME: Add to mapped memory list
return memory;
}

Address Allocate(Size size) {
Expand Down Expand Up @@ -428,7 +430,7 @@ void InitializeEmulation() {

#ifndef UC_KVM
// Setup segments
SegmentDescriptor* gdtEntries = (SegmentDescriptor*)aligned_malloc(ucAlignment, AlignUp(gdtSize, ucAlignment));
SegmentDescriptor* gdtEntries = (SegmentDescriptor*)MapMemory(gdtAddress, AlignUp(gdtSize, ucAlignment), true, true, false);
memset(gdtEntries, 0x00, gdtSize);

gdtEntries[14] = CreateDescriptor(0x00000000, 0xFFFFF000, true); // CS
Expand All @@ -439,8 +441,6 @@ void InitializeEmulation() {
gdtEntries[17] = CreateDescriptor(0x00000000, 0xFFFFF000, false); // Ring 0
gdtEntries[17].dpl = 0; //set descriptor privilege level

err = uc_mem_map_ptr(uc, gdtAddress, AlignUp(gdtSize, ucAlignment), UC_PROT_WRITE | UC_PROT_READ, gdtEntries);

uc_x86_mmr gdtr;
gdtr.base = gdtAddress;
gdtr.limit = gdtSize - 1;
Expand Down Expand Up @@ -478,14 +478,12 @@ void InitializeEmulation() {
#endif

// Map and set TLS (not exposed via flat memory)
uint8_t* tls = aligned_malloc(ucAlignment, tlsSize);
uint8_t* tls = MapMemory(tlsAddress, tlsSize, true, true, false);
memset(tls, 0xBB, tlsSize);
err = uc_mem_map_ptr(uc, tlsAddress, tlsSize, UC_PROT_WRITE | UC_PROT_READ, tls);

// Allocate a heap
heap = aligned_malloc(ucAlignment, heapSize);
heap = MapMemory(heapAddress, heapSize, true, true, true);
memset(heap, 0xAA, heapSize);
MapMemory(heap, heapAddress, heapSize, true, true, true);
}

void SetTracing(bool enabled) {
Expand Down Expand Up @@ -548,8 +546,7 @@ unsigned int CreateEmulatedThread(uint32_t eip) {
// Map and set stack
//FIXME: Use requested size
if (stack == NULL) {
stack = aligned_malloc(ucAlignment, stackSize);
MapMemory(stack, stackAddress, stackSize, true, true, false);
stack = MapMemory(stackAddress, stackSize, true, true, false);
}
static int threadId = 0;
uint32_t esp = stackAddress + stackSize / 2 + 256 * 1024 * threadId++; // 256 kiB per late thread
Expand Down
2 changes: 1 addition & 1 deletion emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void RunEmulation();

// Memory API

void MapMemory(void* data, uint32_t address, uint32_t size, bool read, bool write, bool execute);
void* MapMemory(uint32_t address, uint32_t size, bool read, bool write, bool execute);
Address Allocate(Size size);
void Free(Address address);
void* Memory(uint32_t address);
Expand Down
9 changes: 6 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3962,11 +3962,14 @@ void RunX86(Exe* exe) {
// Map the important exe parts into emu memory
for(unsigned int sectionIndex = 0; sectionIndex < exe->coffHeader.numberOfSections; sectionIndex++) {
PeSection* section = &exe->sections[sectionIndex];
void* mappedSection = (void*)exe->mappedSections[sectionIndex];
if (mappedSection != NULL) {
void** mappedSection = (void**)&exe->mappedSections[sectionIndex];
if (*mappedSection != NULL) {
uint32_t base = exe->peHeader.imageBase + section->virtualAddress;
printf("Mapping 0x%" PRIX32 " - 0x%" PRIX32 "\n", base, base + section->virtualSize - 1);
MapMemory(mappedSection, base, AlignUp(section->virtualSize, exe->peHeader.sectionAlignment), true, true, true);
void* relocatedMappedSection = MapMemory(base, AlignUp(section->virtualSize, exe->peHeader.sectionAlignment), true, true, true);
memcpy(relocatedMappedSection, *mappedSection, section->virtualSize);
aligned_free(*mappedSection);
*mappedSection = relocatedMappedSection;
}
}

Expand Down

0 comments on commit dc3cd15

Please sign in to comment.