|
| 1 | +using CapstoneSharp.Arm; |
| 2 | +using Cpp2IL.Core.Il2CppApiFunctions; |
| 3 | +using Cpp2IL.Core.Logging; |
| 4 | +using LibCpp2IL; |
| 5 | +using LibCpp2IL.Reflection; |
| 6 | + |
| 7 | +namespace Cpp2IL.InstructionSets.ArmV7; |
| 8 | + |
| 9 | +public class ArmV7KeyFunctionAddresses : BaseKeyFunctionAddresses |
| 10 | +{ |
| 11 | + private List<CapstoneArmInstruction>? _cachedDisassembledBytes; |
| 12 | + |
| 13 | + private List<CapstoneArmInstruction> DisassembleTextSection() |
| 14 | + { |
| 15 | + if (_cachedDisassembledBytes == null) |
| 16 | + { |
| 17 | + var toDisasm = LibCpp2IlMain.Binary!.GetEntirePrimaryExecutableSection(); |
| 18 | + _cachedDisassembledBytes = ArmV7Utils.Disassembler.Iterate(toDisasm, LibCpp2IlMain.Binary.GetVirtualAddressOfPrimaryExecutableSection()).ToList(); |
| 19 | + } |
| 20 | + |
| 21 | + return _cachedDisassembledBytes; |
| 22 | + } |
| 23 | + |
| 24 | + protected override IEnumerable<ulong> FindAllThunkFunctions(ulong addr, uint maxBytesBack = 0, params ulong[] addressesToIgnore) |
| 25 | + { |
| 26 | + //Disassemble .text |
| 27 | + var disassembly = DisassembleTextSection(); |
| 28 | + |
| 29 | + //Find all jumps to the target address |
| 30 | + var matchingJmps = disassembly.Where(i => i.IsBranchingTo((int)addr)).ToList(); |
| 31 | + |
| 32 | + foreach (var matchingJmp in matchingJmps) |
| 33 | + { |
| 34 | + if (addressesToIgnore.Contains(matchingJmp.Address)) continue; |
| 35 | + |
| 36 | + var backtrack = 0; |
| 37 | + var idx = disassembly.IndexOf(matchingJmp); |
| 38 | + |
| 39 | + do |
| 40 | + { |
| 41 | + //About the only way we have of checking for a thunk is if there is an all-zero instruction or another unconditional branch just before this |
| 42 | + //Or a ret, but that's less reliable. |
| 43 | + //if so, this is probably a thunk. |
| 44 | + if (idx - backtrack > 0) |
| 45 | + { |
| 46 | + var prevInstruction = disassembly[idx - backtrack - 1]; |
| 47 | + |
| 48 | + if (addressesToIgnore.Contains(prevInstruction.Address)) |
| 49 | + { |
| 50 | + backtrack++; |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + if (prevInstruction.IsSkippedData && prevInstruction.Bytes.IsAllZero()) |
| 55 | + { |
| 56 | + //All-zero instructions are a match |
| 57 | + yield return matchingJmp.Address - (ulong)(backtrack * 4); |
| 58 | + break; |
| 59 | + } |
| 60 | + |
| 61 | + if (prevInstruction.Id is CapstoneArmInstructionId.STR) |
| 62 | + { |
| 63 | + //ADRP instructions are a deal breaker - this means we're loading something from memory, so it's not a simple thunk |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + if (prevInstruction.Id is CapstoneArmInstructionId.B or CapstoneArmInstructionId.BL) |
| 68 | + { |
| 69 | + //Previous branches are a match |
| 70 | + yield return matchingJmp.Address - (ulong)(backtrack * 4); |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + //We're working in the .text section here so we have few symbols, so there's no point looking for the previous one. |
| 76 | + |
| 77 | + backtrack++; |
| 78 | + } while (backtrack * 4 < maxBytesBack); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + protected override ulong GetObjectIsInstFromSystemType() |
| 83 | + { |
| 84 | + Logger.Verbose("\tTrying to use System.Type::IsInstanceOfType to find il2cpp::vm::Object::IsInst..."); |
| 85 | + var typeIsInstanceOfType = LibCpp2IlReflection.GetType("Type", "System")?.Methods?.FirstOrDefault(m => m.Name == "IsInstanceOfType"); |
| 86 | + if (typeIsInstanceOfType == null) |
| 87 | + { |
| 88 | + Logger.VerboseNewline("Type or method not found, aborting."); |
| 89 | + return 0; |
| 90 | + } |
| 91 | + |
| 92 | + //IsInstanceOfType is a very simple ICall, that looks like this: |
| 93 | + // Il2CppClass* klass = vm::Class::FromIl2CppType(type->type.type); |
| 94 | + // return il2cpp::vm::Object::IsInst(obj, klass) != NULL; |
| 95 | + //The last call is to Object::IsInst |
| 96 | + |
| 97 | + Logger.Verbose($"IsInstanceOfType found at 0x{typeIsInstanceOfType.MethodPointer:X}..."); |
| 98 | + var instructions = ArmV7Utils.DisassembleManagedMethod(typeIsInstanceOfType.MethodPointer); |
| 99 | + |
| 100 | + var lastCall = instructions.LastOrDefault(i => i.Id == CapstoneArmInstructionId.BL); |
| 101 | + |
| 102 | + if (lastCall == null) |
| 103 | + { |
| 104 | + Logger.VerboseNewline("Method does not match expected signature. Aborting."); |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + var target = lastCall.GetBranchTarget(); |
| 109 | + Logger.VerboseNewline($"Success. IsInst found at 0x{target:X}"); |
| 110 | + return (ulong)target; |
| 111 | + } |
| 112 | + |
| 113 | + protected override ulong FindFunctionThisIsAThunkOf(ulong thunkPtr, bool prioritiseCall = false) |
| 114 | + { |
| 115 | + var instructions = ArmV7Utils.DisassembleFunction(thunkPtr); |
| 116 | + |
| 117 | + try |
| 118 | + { |
| 119 | + var target = prioritiseCall ? CapstoneArmInstructionId.BL : CapstoneArmInstructionId.B; |
| 120 | + var matchingCall = instructions.FirstOrDefault(i => i.Id == target); |
| 121 | + |
| 122 | + if (matchingCall == null) |
| 123 | + { |
| 124 | + target = target == CapstoneArmInstructionId.BL ? CapstoneArmInstructionId.B : CapstoneArmInstructionId.BL; |
| 125 | + matchingCall = instructions.First(i => i.Id == target); |
| 126 | + } |
| 127 | + |
| 128 | + return (ulong)matchingCall.GetBranchTarget(); |
| 129 | + } |
| 130 | + catch (Exception) |
| 131 | + { |
| 132 | + return 0; |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + protected override int GetCallerCount(ulong toWhere) |
| 137 | + { |
| 138 | + //Disassemble .text |
| 139 | + var disassembly = DisassembleTextSection(); |
| 140 | + |
| 141 | + //Find all jumps to the target address |
| 142 | + return disassembly.Count(i => i.IsBranchingTo((int)toWhere)); |
| 143 | + } |
| 144 | +} |
0 commit comments