Use more suitable exceptions and clean up JIT class.

This commit is contained in:
Andy Nguyen 2021-11-13 13:34:22 +01:00
parent ab11d2d2be
commit 25523978fd
2 changed files with 23 additions and 25 deletions

View File

@ -10,6 +10,7 @@ package com.bdjb;
import com.bdjb.api.API; import com.bdjb.api.API;
import com.bdjb.api.Buffer; import com.bdjb.api.Buffer;
import com.bdjb.api.Int8; import com.bdjb.api.Int8;
import com.bdjb.api.Text;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
/** /**
@ -94,7 +95,7 @@ public final class JIT {
write = api.dlsym(API.LIBKERNEL_MODULE_HANDLE, WRITE_SYMBOL); write = api.dlsym(API.LIBKERNEL_MODULE_HANDLE, WRITE_SYMBOL);
if (sceKernelGetModuleInfo == 0 || read == 0 || write == 0) { if (sceKernelGetModuleInfo == 0 || read == 0 || write == 0) {
throw new IllegalStateException("Could not find symbols."); throw new InternalError("symbols not found");
} }
} }
@ -103,7 +104,7 @@ public final class JIT {
modinfo.fill((byte) 0); modinfo.fill((byte) 0);
modinfo.putLong(0x00, SCE_KERNEL_MODULE_INFO_SIZE); modinfo.putLong(0x00, SCE_KERNEL_MODULE_INFO_SIZE);
if (api.call(sceKernelGetModuleInfo, BDJ_MODULE_HANDLE, modinfo.address()) != 0) { if (api.call(sceKernelGetModuleInfo, BDJ_MODULE_HANDLE, modinfo.address()) != 0) {
throw new IllegalStateException("sceKernelGetModuleInfo failed."); throw new InternalError("sceKernelGetModuleInfo failed");
} }
long bdjBase = modinfo.getLong(0x108); long bdjBase = modinfo.getLong(0x108);
@ -115,7 +116,7 @@ public final class JIT {
i++; i++;
} }
if (i == bdjSize) { if (i == bdjSize) {
throw new IllegalStateException("Could not find BufferBlob::create."); throw new InternalError("BufferBlob::create not found");
} }
BufferBlob__create = bdjBase + i - 0x21; BufferBlob__create = bdjBase + i - 0x21;
@ -129,7 +130,7 @@ public final class JIT {
i++; i++;
} }
if (i == bdjSize) { if (i == bdjSize) {
throw new IllegalStateException("Could not find compiler agent socket."); throw new InternalError("compiler agent socket not found");
} }
long compilerAgentSocketOpcode = bdjBase + i - 0x10; long compilerAgentSocketOpcode = bdjBase + i - 0x10;
compilerAgentSocket = compilerAgentSocket =
@ -141,11 +142,13 @@ public final class JIT {
} }
public long jitMap(long size, long alignment) { public long jitMap(long size, long alignment) {
Buffer name = new Buffer(4); if (size >= MAX_CODE_SIZE) {
api.strcpy(name.address(), "jit"); throw new IllegalArgumentException("size too big");
long blob = api.call(BufferBlob__create, name.address(), size); }
Text name = new Text("jit");
long blob = api.call(BufferBlob__create, name.address(), size + 0x88 + alignment - 1);
if (blob == 0) { if (blob == 0) {
throw new IllegalStateException("Could not map JIT memory."); throw new OutOfMemoryError("BufferBlob__create failed");
} }
long code = blob + api.read32(blob + 0x20); long code = blob + api.read32(blob + 0x20);
return align(code, alignment); return align(code, alignment);
@ -169,7 +172,7 @@ public final class JIT {
api.call(read, compilerAgentSocket, resp, Int8.SIZE); api.call(read, compilerAgentSocket, resp, Int8.SIZE);
if (api.read8(resp) != ACK_MAGIC_NUMBER) { if (api.read8(resp) != ACK_MAGIC_NUMBER) {
throw new IllegalStateException("Wrong compiler resp."); throw new AssertionError("wrong compiler response");
} }
} }
@ -181,20 +184,15 @@ public final class JIT {
RandomAccessFile file = new RandomAccessFile(path, "r"); RandomAccessFile file = new RandomAccessFile(path, "r");
if ((dataSectionOffset & (PAGE_SIZE - 1)) != 0) { if ((dataSectionOffset & (PAGE_SIZE - 1)) != 0) {
throw new IllegalArgumentException("Unaligned data section offset."); throw new IllegalArgumentException("unaligned data section offset");
} }
if (dataSectionOffset < 0 || dataSectionOffset > file.length()) { if (dataSectionOffset < 0 || dataSectionOffset > file.length()) {
throw new IllegalArgumentException("Invalid data section offset."); throw new IllegalArgumentException("invalid data section offset");
}
long size = file.length() + 0x88 + ALIGNMENT - 1;
if (size >= MAX_CODE_SIZE) {
throw new IllegalArgumentException("Payload is too big.");
} }
// Allocate JIT memory. // Allocate JIT memory.
long address = jitMap(size, ALIGNMENT); long address = jitMap(file.length(), ALIGNMENT);
byte[] chunk = new byte[CHUNK_SIZE]; byte[] chunk = new byte[CHUNK_SIZE];
@ -218,7 +216,7 @@ public final class JIT {
-1, -1,
0) 0)
== MAP_FAILED) { == MAP_FAILED) {
throw new IllegalStateException("Could not map data section."); throw new InternalError("mmap failed");
} }
// Copy .data section. // Copy .data section.

View File

@ -127,7 +127,7 @@ public final class API {
private void initSymbols() { private void initSymbols() {
JVM_NativePath = dlsym(RTLD_DEFAULT, JVM_NATIVE_PATH_SYMBOL); JVM_NativePath = dlsym(RTLD_DEFAULT, JVM_NATIVE_PATH_SYMBOL);
if (JVM_NativePath == 0) { if (JVM_NativePath == 0) {
throw new IllegalStateException("Could not find JVM_NativePath."); throw new InternalError("JVM_NativePath not found");
} }
__Ux86_64_setcontext = dlsym(LIBKERNEL_MODULE_HANDLE, UX86_64_SETCONTEXT_SYMBOL); __Ux86_64_setcontext = dlsym(LIBKERNEL_MODULE_HANDLE, UX86_64_SETCONTEXT_SYMBOL);
@ -143,7 +143,7 @@ public final class API {
__Ux86_64_setcontext = dlsym(LIBKERNEL_MODULE_HANDLE, UX86_64_SETCONTEXT_SYMBOL); __Ux86_64_setcontext = dlsym(LIBKERNEL_MODULE_HANDLE, UX86_64_SETCONTEXT_SYMBOL);
} }
if (__Ux86_64_setcontext == 0) { if (__Ux86_64_setcontext == 0) {
throw new IllegalStateException("Could not find __Ux86_64_setcontext."); throw new InternalError("__Ux86_64_setcontext not found");
} }
if (jdk11) { if (jdk11) {
@ -154,17 +154,17 @@ public final class API {
dlsym(RTLD_DEFAULT, JAVA_JAVA_LANG_REFLECT_ARRAY_MULTI_NEW_ARRAY_SYMBOL); dlsym(RTLD_DEFAULT, JAVA_JAVA_LANG_REFLECT_ARRAY_MULTI_NEW_ARRAY_SYMBOL);
} }
if (Java_java_lang_reflect_Array_multiNewArray == 0) { if (Java_java_lang_reflect_Array_multiNewArray == 0) {
throw new IllegalStateException("Could not find Java_java_lang_reflect_Array_multiNewArray."); throw new InternalError("Java_java_lang_reflect_Array_multiNewArray not found");
} }
setjmp = dlsym(LIBC_MODULE_HANDLE, SETJMP_SYMBOL); setjmp = dlsym(LIBC_MODULE_HANDLE, SETJMP_SYMBOL);
if (setjmp == 0) { if (setjmp == 0) {
throw new IllegalStateException("Could not find setjmp."); throw new InternalError("setjmp not found");
} }
__error = dlsym(LIBKERNEL_MODULE_HANDLE, ERROR_SYMBOL); __error = dlsym(LIBKERNEL_MODULE_HANDLE, ERROR_SYMBOL);
if (__error == 0) { if (__error == 0) {
throw new IllegalStateException("Could not find __error."); throw new InternalError("__error not found");
} }
} }
@ -224,7 +224,7 @@ public final class API {
} }
if (!installed) { if (!installed) {
throw new IllegalStateException("Could not install native method."); throw new InternalError("installing native method failed");
} }
// Invoke call method many times to kick in optimization. // Invoke call method many times to kick in optimization.
@ -281,7 +281,7 @@ public final class API {
long fakeKlassVtable = malloc(0x400); long fakeKlassVtable = malloc(0x400);
if (fakeClassOop == 0 || fakeClass == 0 || fakeKlass == 0 || fakeKlassVtable == 0) { if (fakeClassOop == 0 || fakeClass == 0 || fakeKlass == 0 || fakeKlassVtable == 0) {
throw new IllegalStateException("Could not allocate memory."); throw new OutOfMemoryError("malloc failed");
} }
try { try {