In jitTransitionToWritable() and jitTransitionToExecutable(), only run the JitType_CodeMemory handling code when it's actually needed. Actually free j->rx_addr in jitClose() for JitType_JitMemory.

This commit is contained in:
yellows8 2018-05-21 16:57:11 -04:00
parent a12eb11eab
commit b11288ea44
2 changed files with 15 additions and 7 deletions

View File

@ -20,6 +20,7 @@ typedef struct {
void* src_addr; void* src_addr;
void* rx_addr; void* rx_addr;
void* rw_addr; void* rw_addr;
bool is_executable;
Handle handle; Handle handle;
} Jit; } Jit;

View File

@ -40,6 +40,7 @@ Result jitCreate(Jit* j, size_t size)
j->src_addr = src_addr; j->src_addr = src_addr;
j->rx_addr = virtmemReserve(j->size); j->rx_addr = virtmemReserve(j->size);
j->handle = INVALID_HANDLE; j->handle = INVALID_HANDLE;
j->is_executable = 0;
Result rc = 0; Result rc = 0;
@ -94,7 +95,7 @@ Result jitTransitionToWritable(Jit* j)
switch (j->type) { switch (j->type) {
case JitType_CodeMemory: case JitType_CodeMemory:
rc = svcUnmapProcessCodeMemory(envGetOwnProcessHandle(), (u64) j->rx_addr, (u64) j->src_addr, j->size); if (j->is_executable) rc = svcUnmapProcessCodeMemory(envGetOwnProcessHandle(), (u64) j->rx_addr, (u64) j->src_addr, j->size);
break; break;
case JitType_JitMemory: case JitType_JitMemory:
@ -102,6 +103,8 @@ Result jitTransitionToWritable(Jit* j)
break; break;
} }
if (R_SUCCEEDED(rc)) j->is_executable = 0;
return rc; return rc;
} }
@ -111,13 +114,15 @@ Result jitTransitionToExecutable(Jit* j)
switch (j->type) { switch (j->type) {
case JitType_CodeMemory: case JitType_CodeMemory:
rc = svcMapProcessCodeMemory(envGetOwnProcessHandle(), (u64) j->rx_addr, (u64) j->src_addr, j->size); if (!j->is_executable) {
rc = svcMapProcessCodeMemory(envGetOwnProcessHandle(), (u64) j->rx_addr, (u64) j->src_addr, j->size);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
rc = svcSetProcessMemoryPermission(envGetOwnProcessHandle(), (u64) j->rx_addr, j->size, Perm_Rx); rc = svcSetProcessMemoryPermission(envGetOwnProcessHandle(), (u64) j->rx_addr, j->size, Perm_Rx);
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
jitTransitionToWritable(j); jitTransitionToWritable(j);
}
} }
} }
break; break;
@ -128,6 +133,8 @@ Result jitTransitionToExecutable(Jit* j)
break; break;
} }
if (R_SUCCEEDED(rc)) j->is_executable = 1;
return rc; return rc;
} }
@ -154,7 +161,7 @@ Result jitClose(Jit* j)
rc = svcControlCodeMemory(j->handle, CodeMapOperation_UnmapSlave, j->rx_addr, j->size, 0); rc = svcControlCodeMemory(j->handle, CodeMapOperation_UnmapSlave, j->rx_addr, j->size, 0);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
virtmemFree(j->rw_addr, j->size); virtmemFree(j->rx_addr, j->size);
svcCloseHandle(j->handle); svcCloseHandle(j->handle);
} }
} }