Skip to content

Commit

Permalink
iOS continue...
Browse files Browse the repository at this point in the history
  • Loading branch information
WebDucerBlog committed Jun 20, 2020
1 parent b21a111 commit 22f0584
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2834,18 +2834,15 @@ private long mmap(Emulator<?> emulator) {

boolean warning = length >= 0x10000000;
long base = emulator.getMemory().mmap2(addr == null ? 0 : addr.peer, length, prot, flags, fd, (int) offset);
String msg = "mmap addr=" + addr + ", length=" + length + ", prot=0x" + Integer.toHexString(prot) + ", flags=0x" + Integer.toHexString(flags) + ", fd=" + fd + ", offset=" + offset + ", tag=" + tag;
String msg = "mmap addr=" + addr + ", base=0x" + Long.toHexString(base) + ", length=" + length + ", prot=0x" + Integer.toHexString(prot) + ", flags=0x" + Integer.toHexString(flags) + ", fd=" + fd + ", offset=" + offset + ", tag=" + tag + ", LR=" + context.getLRPointer();
if (log.isDebugEnabled() || warning) {
if (warning) {
log.warn(msg);
} else {
log.debug(msg);
}
} else {
Log log = LogFactory.getLog("com.github.unidbg.ios.malloc");
if (log.isDebugEnabled()) {
log.debug(msg + ", base=0x" + Long.toHexString(base));
}
} else if(LogFactory.getLog("com.github.unidbg.ios.malloc").isDebugEnabled()) {
log.debug(msg);
}
return base;
}
Expand Down
27 changes: 26 additions & 1 deletion unidbg-ios/src/main/java/com/github/unidbg/ios/MachOLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -1739,7 +1739,32 @@ public Module getExecutableModule() {
public long mmap2(long start, int length, int prot, int flags, int fd, int offset) {
int aligned = (int) ARM.alignSize(length, emulator.getPageAlign());

if (((flags & com.github.unidbg.ios.MachO.MAP_ANONYMOUS) != 0) || (start == 0 && fd <= 0 && offset == 0)) {
boolean isAnonymous = ((flags & com.github.unidbg.ios.MachO.MAP_ANONYMOUS) != 0) || (start == 0 && fd <= 0 && offset == 0);
if ((flags & MAP_FIXED) != 0 && isAnonymous) {
if (log.isDebugEnabled()) {
log.debug("mmap2 MAP_FIXED start=0x" + Long.toHexString(start) + ", length=" + length + ", prot=" + prot);
}

MemoryMap mapped = null;
for (MemoryMap map : memoryMap.values()) {
if (start >= map.base && start + aligned <= map.base + map.size) {
mapped = map;
}
}

if (mapped != null) {
munmap(start, aligned);
unicorn.mem_map(start, aligned, prot);
if (memoryMap.put(start, new MemoryMap(start, aligned, prot)) != null) {
log.warn("mmap2 replace exists memory map: start=" + Long.toHexString(start));
}
return start;
} else {
throw new IllegalStateException("mmap2 MAP_FIXED not found mapped memory: start=0x" + Long.toHexString(start));
}
}

if (isAnonymous) {
long addr = allocateMapAddress(0, aligned);
if (log.isDebugEnabled()) {
log.debug("mmap2 addr=0x" + Long.toHexString(addr) + ", mmapBaseAddress=0x" + Long.toHexString(mmapBaseAddress) + ", start=" + start + ", fd=" + fd + ", offset=" + offset + ", aligned=" + aligned);
Expand Down
8 changes: 8 additions & 0 deletions unidbg-ios/src/main/native/ios/bootstrap.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import <AVFoundation/AVFoundation.h>
#import <CommonCrypto/CommonDigest.h>
#import <CommonCrypto/CommonCryptor.h>
#include <sys/mman.h>
#include "test.h"

@interface BootstrapTest : NSObject {}
Expand Down Expand Up @@ -228,6 +229,12 @@ static void test_CommonDigest() {
fprintf(stderr, "\n");
}

static void test_mmap() {
void *addr = mmap(NULL, 0x4000 * 2, 0, 0x1002, -1, 0);
void *fix = mmap(addr, 0x4000, 3, 0x1012, -1, 0);
NSLog(@"test_mmap addr=%p, fix=%p", addr, fix);
}

int main(int argc, char *argv[]) {
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
Expand Down Expand Up @@ -264,6 +271,7 @@ int main(int argc, char *argv[]) {
test_CoreGraphics(argv[1]);
}
test_CommonDigest();
test_mmap();

return 0;
}
Binary file modified unidbg-ios/src/main/resources/ios/bootstrap_objc
Binary file not shown.

0 comments on commit 22f0584

Please sign in to comment.