|
| 1 | +package jadx.core.dex.instructions.invokedynamic; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import jadx.api.plugins.input.data.IMethodHandle; |
| 7 | +import jadx.api.plugins.input.data.IMethodProto; |
| 8 | +import jadx.api.plugins.input.data.annotations.EncodedValue; |
| 9 | +import jadx.api.plugins.input.insns.InsnData; |
| 10 | +import jadx.core.dex.info.ClassInfo; |
| 11 | +import jadx.core.dex.info.MethodInfo; |
| 12 | +import jadx.core.dex.instructions.ConstStringNode; |
| 13 | +import jadx.core.dex.instructions.InvokeCustomRawNode; |
| 14 | +import jadx.core.dex.instructions.InvokeNode; |
| 15 | +import jadx.core.dex.instructions.InvokeType; |
| 16 | +import jadx.core.dex.instructions.args.ArgType; |
| 17 | +import jadx.core.dex.instructions.args.InsnArg; |
| 18 | +import jadx.core.dex.nodes.InsnNode; |
| 19 | +import jadx.core.dex.nodes.MethodNode; |
| 20 | +import jadx.core.dex.nodes.RootNode; |
| 21 | +import jadx.core.utils.exceptions.JadxRuntimeException; |
| 22 | + |
| 23 | +import static jadx.core.utils.EncodedValueUtils.buildLookupArg; |
| 24 | +import static jadx.core.utils.EncodedValueUtils.convertToInsnArg; |
| 25 | + |
| 26 | +/** |
| 27 | + * Show `invoke-custom` similar to polymorphic call |
| 28 | + */ |
| 29 | +public class CustomRawCall { |
| 30 | + |
| 31 | + public static InsnNode build(MethodNode mth, InsnData insn, boolean isRange, List<EncodedValue> values) { |
| 32 | + IMethodHandle resolveHandle = (IMethodHandle) values.get(0).getValue(); |
| 33 | + String invokeName = (String) values.get(1).getValue(); |
| 34 | + IMethodProto invokeProto = (IMethodProto) values.get(2).getValue(); |
| 35 | + List<InsnArg> resolveArgs = buildArgs(mth, values); |
| 36 | + |
| 37 | + if (resolveHandle.getType().isField()) { |
| 38 | + throw new JadxRuntimeException("Field handle not yet supported"); |
| 39 | + } |
| 40 | + |
| 41 | + RootNode root = mth.root(); |
| 42 | + MethodInfo resolveMth = MethodInfo.fromRef(root, resolveHandle.getMethodRef()); |
| 43 | + InvokeType resolveInvokeType = InvokeCustomUtils.convertInvokeType(resolveHandle.getType()); |
| 44 | + InvokeNode resolve = new InvokeNode(resolveMth, resolveInvokeType, resolveArgs.size()); |
| 45 | + resolveArgs.forEach(resolve::addArg); |
| 46 | + |
| 47 | + ClassInfo invokeCls = ClassInfo.fromType(root, ArgType.OBJECT); // type will be known at runtime |
| 48 | + MethodInfo invokeMth = MethodInfo.fromMethodProto(root, invokeCls, invokeName, invokeProto); |
| 49 | + InvokeCustomRawNode customRawNode = new InvokeCustomRawNode(resolve, invokeMth, insn, isRange); |
| 50 | + customRawNode.setCallSiteValues(values); |
| 51 | + return customRawNode; |
| 52 | + } |
| 53 | + |
| 54 | + private static List<InsnArg> buildArgs(MethodNode mth, List<EncodedValue> values) { |
| 55 | + int valuesCount = values.size(); |
| 56 | + List<InsnArg> list = new ArrayList<>(valuesCount); |
| 57 | + RootNode root = mth.root(); |
| 58 | + list.add(buildLookupArg(root)); // use `java.lang.invoke.MethodHandles.lookup()` as first arg |
| 59 | + for (int i = 1; i < valuesCount; i++) { |
| 60 | + EncodedValue value = values.get(i); |
| 61 | + try { |
| 62 | + list.add(convertToInsnArg(root, value)); |
| 63 | + } catch (Exception e) { |
| 64 | + mth.addWarnComment("Failed to build arg in invoke-custom insn: " + value, e); |
| 65 | + list.add(InsnArg.wrapArg(new ConstStringNode(value.toString()))); |
| 66 | + } |
| 67 | + } |
| 68 | + return list; |
| 69 | + } |
| 70 | +} |
0 commit comments