use of net.runelite.asm.attributes.code.instructions.InvokeSpecial in project runelite by runelite.
the class MaxMemoryTransformer method insert.
private void insert(Instructions ins, int idx) {
Class randomClass = new net.runelite.asm.pool.Class("java/util/Random");
ins.getInstructions().remove(idx);
// pop runtime
ins.getInstructions().add(idx++, new Pop(ins));
ins.getInstructions().add(idx++, new New(ins, randomClass));
ins.getInstructions().add(idx++, new Dup(ins));
// new Random
ins.getInstructions().add(idx++, new InvokeSpecial(ins, new net.runelite.asm.pool.Method(randomClass, "<init>", new Signature("()V"))));
ins.getInstructions().add(idx++, new LDC(ins, 31457280));
// nextInt(31457280)
ins.getInstructions().add(idx++, new InvokeVirtual(ins, new net.runelite.asm.pool.Method(randomClass, "nextInt", new Signature("(I)I"))));
ins.getInstructions().add(idx++, new LDC(ins, 230686720));
// 230686720 + nextInt(31457280)
ins.getInstructions().add(idx++, new IAdd(ins));
ins.getInstructions().add(idx++, new I2L(ins));
}
use of net.runelite.asm.attributes.code.instructions.InvokeSpecial in project runelite by runelite.
the class MixinInjector method injectMethods.
private void injectMethods(ClassFile mixinCf, ClassFile cf, Map<net.runelite.asm.pool.Field, Field> shadowFields) throws InjectionException {
// Keeps mappings between methods annotated with @Copy -> the copied method within the vanilla pack
Map<net.runelite.asm.pool.Method, CopiedMethod> copiedMethods = new HashMap<>();
// Handle the copy mixins first, so all other mixins know of the copies
for (Method method : mixinCf.getMethods()) {
Annotation copyAnnotation = method.getAnnotations().find(COPY);
if (copyAnnotation == null) {
continue;
}
String deobMethodName = (String) copyAnnotation.getElement().getValue();
ClassFile deobCf = inject.toDeobClass(cf);
Method deobMethod = findDeobMethod(deobCf, deobMethodName, method.getDescriptor());
if (deobMethod == null) {
throw new InjectionException("Failed to find the deob method " + deobMethodName + " for mixin " + mixinCf);
}
if (method.isStatic() != deobMethod.isStatic()) {
throw new InjectionException("Mixin method " + method + " should be " + (deobMethod.isStatic() ? "static" : "non-static"));
}
// Find the vanilla class where the method to copy is in
String obClassName = DeobAnnotations.getObfuscatedName(deobMethod.getClassFile().getAnnotations());
ClassFile obCf = inject.getVanilla().findClass(obClassName);
assert obCf != null : "unable to find vanilla class from obfuscated name " + obClassName;
String obMethodName = DeobAnnotations.getObfuscatedName(deobMethod.getAnnotations());
Signature obMethodSignature = DeobAnnotations.getObfuscatedSignature(deobMethod);
if (obMethodName == null) {
obMethodName = deobMethod.getName();
}
if (obMethodSignature == null) {
obMethodSignature = deobMethod.getDescriptor();
}
Method obMethod = obCf.findMethod(obMethodName, obMethodSignature);
if (obMethod == null) {
throw new InjectionException("Failed to find the ob method " + obMethodName + " for mixin " + mixinCf);
}
if (method.getDescriptor().size() > obMethod.getDescriptor().size()) {
throw new InjectionException("Mixin methods cannot have more parameters than their corresponding ob method");
}
Method copy = new Method(cf, "copy$" + deobMethodName, obMethodSignature);
moveCode(copy, obMethod.getCode());
copy.setAccessFlags(obMethod.getAccessFlags());
copy.setPublic();
copy.getExceptions().getExceptions().addAll(obMethod.getExceptions().getExceptions());
copy.getAnnotations().getAnnotations().addAll(obMethod.getAnnotations().getAnnotations());
cf.addMethod(copy);
/*
If the desc for the mixin method and the desc for the ob method
are the same in length, assume that the mixin method is taking
care of the garbage parameter itself.
*/
boolean hasGarbageValue = method.getDescriptor().size() != obMethod.getDescriptor().size() && deobMethod.getDescriptor().size() < obMethodSignature.size();
copiedMethods.put(method.getPoolMethod(), new CopiedMethod(copy, hasGarbageValue));
logger.debug("Injected copy of {} to {}", obMethod, copy);
}
// Handle the rest of the mixin types
for (Method method : mixinCf.getMethods()) {
boolean isClinit = "<clinit>".equals(method.getName());
boolean isInit = "<init>".equals(method.getName());
boolean hasInject = method.getAnnotations().find(INJECT) != null;
// You can't annotate clinit, so its always injected
if ((hasInject && isInit) || isClinit) {
if (!"()V".equals(method.getDescriptor().toString())) {
throw new InjectionException("Injected constructors cannot have arguments");
}
Method[] originalMethods = cf.getMethods().stream().filter(n -> n.getName().equals(method.getName())).toArray(Method[]::new);
// If there isn't a <clinit> already just inject ours, otherwise rename it
// This is always true for <init>
String name = method.getName();
if (originalMethods.length > 0) {
name = "rl$$" + (isInit ? "init" : "clinit");
}
String numberlessName = name;
for (int i = 1; cf.findMethod(name, method.getDescriptor()) != null; i++) {
name = numberlessName + i;
}
Method copy = new Method(cf, name, method.getDescriptor());
moveCode(copy, method.getCode());
copy.setAccessFlags(method.getAccessFlags());
copy.setPrivate();
assert method.getExceptions().getExceptions().isEmpty();
// Remove the call to the superclass's ctor
if (isInit) {
Instructions instructions = copy.getCode().getInstructions();
ListIterator<Instruction> listIter = instructions.getInstructions().listIterator();
for (; listIter.hasNext(); ) {
Instruction instr = listIter.next();
if (instr instanceof InvokeSpecial) {
InvokeSpecial invoke = (InvokeSpecial) instr;
assert invoke.getMethod().getName().equals("<init>");
listIter.remove();
int pops = invoke.getMethod().getType().getArguments().size() + 1;
for (int i = 0; i < pops; i++) {
listIter.add(new Pop(instructions));
}
break;
}
}
}
setOwnersToTargetClass(mixinCf, cf, copy, shadowFields, copiedMethods);
cf.addMethod(copy);
// Call our method at the return point of the matching method(s)
for (Method om : originalMethods) {
Instructions instructions = om.getCode().getInstructions();
ListIterator<Instruction> listIter = instructions.getInstructions().listIterator();
for (; listIter.hasNext(); ) {
Instruction instr = listIter.next();
if (instr instanceof ReturnInstruction) {
listIter.previous();
if (isInit) {
listIter.add(new ALoad(instructions, 0));
listIter.add(new InvokeSpecial(instructions, copy.getPoolMethod()));
} else if (isClinit) {
listIter.add(new InvokeStatic(instructions, copy.getPoolMethod()));
}
listIter.next();
}
}
}
logger.debug("Injected mixin method {} to {}", copy, cf);
} else if (hasInject) {
// Make sure the method doesn't invoke copied methods
for (Instruction i : method.getCode().getInstructions().getInstructions()) {
if (i instanceof InvokeInstruction) {
InvokeInstruction ii = (InvokeInstruction) i;
if (copiedMethods.containsKey(ii.getMethod())) {
throw new InjectionException("Injected methods cannot invoke copied methods");
}
}
}
Method copy = new Method(cf, method.getName(), method.getDescriptor());
moveCode(copy, method.getCode());
copy.setAccessFlags(method.getAccessFlags());
copy.setPublic();
assert method.getExceptions().getExceptions().isEmpty();
setOwnersToTargetClass(mixinCf, cf, copy, shadowFields, copiedMethods);
cf.addMethod(copy);
logger.debug("Injected mixin method {} to {}", copy, cf);
} else if (method.getAnnotations().find(REPLACE) != null) {
Annotation replaceAnnotation = method.getAnnotations().find(REPLACE);
String deobMethodName = (String) replaceAnnotation.getElement().getValue();
ClassFile deobCf = inject.toDeobClass(cf);
Method deobMethod = findDeobMethod(deobCf, deobMethodName, method.getDescriptor());
if (deobMethod == null) {
throw new InjectionException("Failed to find the deob method " + deobMethodName + " for mixin " + mixinCf);
}
if (method.isStatic() != deobMethod.isStatic()) {
throw new InjectionException("Mixin method " + method + " should be " + (deobMethod.isStatic() ? "static" : "non-static"));
}
String obMethodName = DeobAnnotations.getObfuscatedName(deobMethod.getAnnotations());
Signature obMethodSignature = DeobAnnotations.getObfuscatedSignature(deobMethod);
// Deob signature is the same as ob signature
if (obMethodName == null) {
obMethodName = deobMethod.getName();
}
if (obMethodSignature == null) {
obMethodSignature = deobMethod.getDescriptor();
}
// Find the vanilla class where the method to copy is in
String obClassName = DeobAnnotations.getObfuscatedName(deobMethod.getClassFile().getAnnotations());
ClassFile obCf = inject.getVanilla().findClass(obClassName);
Method obMethod = obCf.findMethod(obMethodName, obMethodSignature);
assert obMethod != null : "obfuscated method " + obMethodName + obMethodSignature + " does not exist";
if (method.getDescriptor().size() > obMethod.getDescriptor().size()) {
throw new InjectionException("Mixin methods cannot have more parameters than their corresponding ob method");
}
Type returnType = method.getDescriptor().getReturnValue();
Type deobReturnType = inject.apiTypeToDeobfuscatedType(returnType);
if (!returnType.equals(deobReturnType)) {
ClassFile deobReturnTypeClassFile = inject.getDeobfuscated().findClass(deobReturnType.getInternalName());
if (deobReturnTypeClassFile != null) {
ClassFile obReturnTypeClass = inject.toObClass(deobReturnTypeClassFile);
Instructions instructions = method.getCode().getInstructions();
ListIterator<Instruction> listIter = instructions.getInstructions().listIterator();
for (; listIter.hasNext(); ) {
Instruction instr = listIter.next();
if (instr instanceof ReturnInstruction) {
listIter.previous();
CheckCast checkCast = new CheckCast(instructions);
checkCast.setType(new Type(obReturnTypeClass.getName()));
listIter.add(checkCast);
listIter.next();
}
}
}
}
moveCode(obMethod, method.getCode());
boolean hasGarbageValue = method.getDescriptor().size() != obMethod.getDescriptor().size() && deobMethod.getDescriptor().size() < obMethodSignature.size();
if (hasGarbageValue) {
int garbageIndex = obMethod.isStatic() ? obMethod.getDescriptor().size() - 1 : obMethod.getDescriptor().size();
/*
If the mixin method doesn't have the garbage parameter,
the compiler will have produced code that uses the garbage
parameter's local variable index for other things,
so we'll have to add 1 to all loads/stores to indices
that are >= garbageIndex.
*/
shiftLocalIndices(obMethod.getCode().getInstructions(), garbageIndex);
}
setOwnersToTargetClass(mixinCf, cf, obMethod, shadowFields, copiedMethods);
logger.debug("Replaced method {} with mixin method {}", obMethod, method);
}
}
}
use of net.runelite.asm.attributes.code.instructions.InvokeSpecial in project runelite by runelite.
the class InjectConstruct method injectConstruct.
public void injectConstruct(ClassFile targetClass, java.lang.reflect.Method apiMethod) throws InjectionException {
logger.info("Injecting construct for {}", apiMethod);
assert targetClass.findMethod(apiMethod.getName()) == null;
Class<?> typeToConstruct = apiMethod.getReturnType();
ClassFile vanillaClass = inject.findVanillaForInterface(typeToConstruct);
if (vanillaClass == null) {
throw new InjectionException("Unable to find vanilla class which implements interface " + typeToConstruct);
}
Signature sig = inject.javaMethodToSignature(apiMethod);
Signature constructorSig = new Signature.Builder().addArguments(Stream.of(apiMethod.getParameterTypes()).map(arg -> {
ClassFile vanilla = inject.findVanillaForInterface(arg);
if (vanilla != null) {
return new Type("L" + vanilla.getName() + ";");
}
return Inject.classToType(arg);
}).collect(Collectors.toList())).setReturnType(Type.VOID).build();
Method vanillaConstructor = vanillaClass.findMethod("<init>", constructorSig);
if (vanillaConstructor == null) {
throw new InjectionException("Unable to find constructor for " + vanillaClass.getName() + ".<init>" + constructorSig);
}
Method setterMethod = new Method(targetClass, apiMethod.getName(), sig);
setterMethod.setAccessFlags(ACC_PUBLIC);
targetClass.addMethod(setterMethod);
Code code = new Code(setterMethod);
setterMethod.setCode(code);
Instructions instructions = code.getInstructions();
List<Instruction> ins = instructions.getInstructions();
ins.add(new New(instructions, vanillaClass.getPoolClass()));
ins.add(new Dup(instructions));
int idx = 1;
int parameter = 0;
for (Type type : vanillaConstructor.getDescriptor().getArguments()) {
Instruction load = inject.createLoadForTypeIndex(instructions, type, idx);
idx += type.getSize();
ins.add(load);
Type paramType = sig.getTypeOfArg(parameter);
if (!type.equals(paramType)) {
CheckCast checkCast = new CheckCast(instructions);
checkCast.setType(type);
ins.add(checkCast);
}
++parameter;
}
ins.add(new InvokeSpecial(instructions, vanillaConstructor.getPoolMethod()));
ins.add(new Return(instructions));
}
use of net.runelite.asm.attributes.code.instructions.InvokeSpecial in project runelite by runelite.
the class EnumDeobfuscator method makeEnum.
private void makeEnum(ClassFile cf) {
// make class an enum
cf.setEnum();
// enums super class is java/lang/Enum
assert cf.getParentClass().getName().equals("java/lang/Object");
cf.setSuperName("java/lang/Enum");
// all static fields of the type of the class become enum members
for (Field field : cf.getFields()) {
if (field.isStatic() && field.getType().equals(new Type("L" + cf.getName() + ";"))) {
field.setEnum();
}
}
for (Method method : cf.getMethods()) {
if (!method.getName().equals("<init>")) {
continue;
}
// Add string as first argument, which is the field name,
// and ordinal as second argument
Signature signature = new Signature.Builder().setReturnType(method.getDescriptor().getReturnValue()).addArgument(Type.STRING).addArgument(Type.INT).addArguments(method.getDescriptor().getArguments()).build();
method.setDescriptor(signature);
// Remove instructions up to invokespecial
Instructions ins = method.getCode().getInstructions();
Instruction i;
do {
i = ins.getInstructions().get(0);
ins.remove(i);
} while (i.getType() != InstructionType.INVOKESPECIAL);
// load this
ins.addInstruction(0, new ALoad(ins, 0));
// load constant name
ins.addInstruction(1, new ALoad(ins, 1));
// ordinal
ins.addInstruction(2, new ILoad(ins, 2));
// invoke enum constructor
ins.addInstruction(3, new InvokeSpecial(ins, ENUM_INIT));
// Shift all indexes after this up +2 because of the new String and int argument
for (int j = 4; j < ins.getInstructions().size(); ++j) {
i = ins.getInstructions().get(j);
if (i instanceof LVTInstruction) {
LVTInstruction lvt = ((LVTInstruction) i);
int idx = lvt.getVariableIndex();
if (idx != 0) {
lvt.setVariableIndex(idx + 2);
}
}
}
}
// Order of fields being set in clinit, which is the order
// the enum fields are actually in
List<Field> order = new ArrayList<>();
for (Method method : cf.getMethods()) {
if (!method.getName().equals("<clinit>")) {
continue;
}
Instructions ins = method.getCode().getInstructions();
int count = 0;
// sometimes there is new new invokespecial invokespecial putfield
// for eg enum member field30(1, 2, String.class, new class5());
boolean seenDup = false;
for (int j = 0; j < ins.getInstructions().size(); ++j) {
Instruction i = ins.getInstructions().get(j);
if (i.getType() == InstructionType.DUP && !seenDup) {
// XXX this should actually be the field name, but it seems to have no effect on fernflower
ins.addInstruction(j + 1, new LDC(ins, "runelite"));
ins.addInstruction(j + 2, new LDC(ins, count++));
seenDup = true;
} else if (i.getType() == InstructionType.INVOKESPECIAL) {
Instruction next = ins.getInstructions().get(j + 1);
// check if this is the invokespecial on the enum, putstatic comes next
if (next.getType() == InstructionType.PUTSTATIC) {
InvokeSpecial is = (InvokeSpecial) i;
PutStatic ps = (PutStatic) next;
net.runelite.asm.pool.Method pmethod = new net.runelite.asm.pool.Method(is.getMethod().getClazz(), is.getMethod().getName(), new Signature.Builder().setReturnType(is.getMethod().getType().getReturnValue()).addArgument(Type.STRING).addArgument(Type.INT).addArguments(is.getMethod().getType().getArguments()).build());
is.setMethod(pmethod);
Field field = ps.getMyField();
assert field != null;
order.add(field);
seenDup = false;
}
}
}
}
// Enum fields must be first. Also they are in order in clinit.
// Sort fields
Collections.sort(cf.getFields(), (f1, f2) -> {
int idx1 = order.indexOf(f1);
int idx2 = order.indexOf(f2);
if (idx1 == -1) {
idx1 = Integer.MAX_VALUE;
}
if (idx2 == -1) {
idx2 = Integer.MAX_VALUE;
}
return Integer.compare(idx1, idx2);
});
}
Aggregations