Skip to content

Commit 00fd37c

Browse files
authored
Merge pull request OpenSWE1R#167 from JayFoxRox/alloc-mem
Allocate memory in MapMemory
2 parents 7815f6b + 4542519 commit 00fd37c

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

emulation.c

+7-10
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,17 @@ void DumpProfilingHeat(const char* path) {
273273
}
274274
}
275275

276-
void MapMemory(void* memory, uint32_t address, uint32_t size, bool read, bool write, bool execute) {
276+
void* MapMemory(uint32_t address, uint32_t size, bool read, bool write, bool execute) {
277277
//FIXME: Permissions!
278278
uc_err err;
279279
assert(size % ucAlignment == 0);
280+
void* memory = aligned_alloc(ucAlignment, size);
280281
err = uc_mem_map_ptr(uc, address, size, UC_PROT_ALL, memory);
281282
if (err) {
282283
printf("Failed on uc_mem_map_ptr() with error returned %u: %s\n", err, uc_strerror(err));
283284
}
284285
//FIXME: Add to mapped memory list
286+
return memory;
285287
}
286288

287289
Address Allocate(Size size) {
@@ -428,7 +430,7 @@ void InitializeEmulation() {
428430

429431
#ifndef UC_KVM
430432
// Setup segments
431-
SegmentDescriptor* gdtEntries = (SegmentDescriptor*)aligned_malloc(ucAlignment, AlignUp(gdtSize, ucAlignment));
433+
SegmentDescriptor* gdtEntries = (SegmentDescriptor*) = MapMemory(gdtAddress, AlignUp(gdtSize, ucAlignment), true, true, false);
432434
memset(gdtEntries, 0x00, gdtSize);
433435

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

442-
err = uc_mem_map_ptr(uc, gdtAddress, AlignUp(gdtSize, ucAlignment), UC_PROT_WRITE | UC_PROT_READ, gdtEntries);
443-
444444
uc_x86_mmr gdtr;
445445
gdtr.base = gdtAddress;
446446
gdtr.limit = gdtSize - 1;
@@ -478,14 +478,12 @@ void InitializeEmulation() {
478478
#endif
479479

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

485484
// Allocate a heap
486-
heap = aligned_malloc(ucAlignment, heapSize);
485+
heap = MapMemory(heapAddress, heapSize, true, true, true);
487486
memset(heap, 0xAA, heapSize);
488-
MapMemory(heap, heapAddress, heapSize, true, true, true);
489487
}
490488

491489
void SetTracing(bool enabled) {
@@ -548,8 +546,7 @@ unsigned int CreateEmulatedThread(uint32_t eip) {
548546
// Map and set stack
549547
//FIXME: Use requested size
550548
if (stack == NULL) {
551-
stack = aligned_malloc(ucAlignment, stackSize);
552-
MapMemory(stack, stackAddress, stackSize, true, true, false);
549+
stack = MapMemory(stackAddress, stackSize, true, true, false);
553550
}
554551
static int threadId = 0;
555552
uint32_t esp = stackAddress + stackSize / 2 + 256 * 1024 * threadId++; // 256 kiB per late thread

emulation.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void RunEmulation();
2525

2626
// Memory API
2727

28-
void MapMemory(void* data, uint32_t address, uint32_t size, bool read, bool write, bool execute);
28+
void* MapMemory(uint32_t address, uint32_t size, bool read, bool write, bool execute);
2929
Address Allocate(Size size);
3030
void Free(Address address);
3131
void* Memory(uint32_t address);

main.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -3962,11 +3962,14 @@ void RunX86(Exe* exe) {
39623962
// Map the important exe parts into emu memory
39633963
for(unsigned int sectionIndex = 0; sectionIndex < exe->coffHeader.numberOfSections; sectionIndex++) {
39643964
PeSection* section = &exe->sections[sectionIndex];
3965-
void* mappedSection = (void*)exe->mappedSections[sectionIndex];
3966-
if (mappedSection != NULL) {
3965+
void** mappedSection = (void**)&exe->mappedSections[sectionIndex];
3966+
if (*mappedSection != NULL) {
39673967
uint32_t base = exe->peHeader.imageBase + section->virtualAddress;
39683968
printf("Mapping 0x%" PRIX32 " - 0x%" PRIX32 "\n", base, base + section->virtualSize - 1);
3969-
MapMemory(mappedSection, base, AlignUp(section->virtualSize, exe->peHeader.sectionAlignment), true, true, true);
3969+
void* relocatedMappedSection = MapMemory(base, AlignUp(section->virtualSize, exe->peHeader.sectionAlignment), true, true, true);
3970+
memcpy(relocatedMappedSection, *mappedSection, section->virtualSize);
3971+
aligned_free(*mappedSection);
3972+
*mappedSection = relocatedMappedSection;
39703973
}
39713974
}
39723975

0 commit comments

Comments
 (0)