/// <summary> /// TODO 更好的传参方式,兼容普通线程创建 /// </summary> /// <param name="entry"></param> /// <param name="args"></param> public void Execute(VMMethod entry, string[] args) { CurrentMethod = entry; // TODO Main的参数,暂时push了一个null Stack.PushAddress(); Execute(); }
public void ReadExceptionHandlers(VMMethod method, int exceptionHandlers) { for (var i = 0; i < exceptionHandlers; i++) { var EH = new VMExceptionHandler().Read(Ctx.Module, Ctx.Parser); method.MethodBody.ExceptionHandlers.Add(EH); } }
public void ReadLocals(VMMethod method, int locals) { for (var i = 0; i < locals; i++) { var token = Ctx.Parser.ReadEncryptedByte(); method.MethodBody.Locals.Add(Ctx.Module.CorLibTypeFactory.Object); //method.MethodBody.Locals.Add((ITypeDescriptor)Ctx.Module.LookupMember(token)); } }
public void ReadAllInstructions(VMMethod method, int instructions) { for (var i = 0; i < instructions; i++) { var instr = new VMInstruction(VMOpCode.Nop, null, i); var b = Ctx.Parser.Reader.ReadByte(); var opCode = Ctx.PatternMatcher.GetOpCodeValue(b); if (opCode == VMOpCode.Nop) { method.MethodBody.Instructions.Add(instr); continue; } instr.OpCode = opCode; if (b > 173) { throw new DevirtualizationException("Disassembling Exception!"); } var operand = Ctx.Parser.Operands[b]; instr.Operand = operand switch { 1 => Ctx.Parser.ReadEncryptedByte(), 2 => Ctx.Parser.Reader.ReadInt64(), 3 => Ctx.Parser.Reader.ReadSingle(), 4 => Ctx.Parser.Reader.ReadDouble(), 5 => ((Func <object>)(() => { var num9 = Ctx.Parser.ReadEncryptedByte(); var array = new int[num9]; for (var m = 0; m < num9; m++) { array[m] = Ctx.Parser.ReadEncryptedByte(); } return(array); }))(), _ => null }; method.MethodBody.Instructions.Add(instr); } } }
public VMMethod DisassembleMethod(int methodKey) { var method = new VMMethod(methodKey); Ctx.Parser.Reader.BaseStream.Position = methodKey; var mdToken = Ctx.Parser.ReadEncryptedByte(); var parentMethod = ((IMethodDescriptor)Ctx.Module.LookupMember(mdToken)).Resolve(); method.Parent = parentMethod; var locals = Ctx.Parser.ReadEncryptedByte(); var exceptionHandlers = Ctx.Parser.ReadEncryptedByte(); var instructions = Ctx.Parser.ReadEncryptedByte(); ReadLocals(method, locals); ReadExceptionHandlers(method, exceptionHandlers); ReadAllInstructions(method, instructions); return(method); }
/// <summary> /// 加载模块 /// </summary> /// <param name="binaryModule"></param> /// <param name="isDependency">是执行模块还是依赖模块</param> /// <returns></returns> public static VMModule AddModule(BinaryModule binaryModule, bool isDependency) { if (!isDependency) { LoadWatch.Start(); } VMModule module = new VMModule() { StringPoolLink = new List <uint>(), Classes = new Dictionary <uint, VMClass>(), ClassPool = binaryModule.ClassPool, ClassPoolLink = new List <VMClass>(), FieldPool = binaryModule.FieldPool, FieldPoolLink = new List <VMField>(), MethodPool = binaryModule.MethodPool, MethodPoolLink = new List <VMMethod>() }; HashSet <int> externalModuleNameIndexes = new HashSet <int>(); // 字符串常量 foreach (string stringConstant in binaryModule.StringPool) { // 建立映射 module.StringPoolLink.Add(MethodArea.Singleton.AddConstantString(stringConstant)); } Modules.Add(module.StringPoolLink[binaryModule.ModuleNameIndex - 1], module); // 类 foreach (ClassConstantInfo classInfo in module.ClassPool) { int moduleNameIndex = classInfo.Module; if (moduleNameIndex != binaryModule.ModuleNameIndex) { // 外部域 externalModuleNameIndexes.Add(moduleNameIndex); // 占位 module.ClassPoolLink.Add(null); } else { VMClass vmClass = new VMClass() { Parent = module, Methods = new Dictionary <uint, List <VMMethod> >(), StaticFields = new List <VMField>(), StaticFieldSize = HeapData.MiscDataSize, // 头部信息 Fields = new List <VMField>(), FieldSize = HeapData.MiscDataSize // 头部信息 }; module.Classes.Add(module.StringPoolLink[classInfo.Name - 1], vmClass); module.ClassPoolLink.Add(vmClass); } } // Field AccessFlag accessFlag = new AccessFlag(); foreach (FieldConstantInfo fieldInfo in module.FieldPool) { int moduleNameIndex = module.ClassPool[fieldInfo.Class - 1].Module; if (moduleNameIndex != binaryModule.ModuleNameIndex) { // 外部域 externalModuleNameIndexes.Add(moduleNameIndex); // 占位 module.FieldPoolLink.Add(null); } else { module.Classes.TryGetValue(module.StringPoolLink[module.ClassPool[fieldInfo.Class - 1].Name - 1], out VMClass vmClass); // 分配方法区空间并且链接地址 accessFlag.Flag = fieldInfo.Flag; VariableType fieldType = VariableType.GetType(binaryModule.StringPool[fieldInfo.Descriptor - 1]); VMField vmField; if (accessFlag.IsStatic) { vmField = new VMField(fieldInfo.Flag, fieldType, fieldInfo.Class, vmClass.StaticFieldSize); module.FieldPoolLink.Add(vmField); vmClass.StaticFields.Add(vmField); vmClass.StaticFieldSize += fieldType.Size; } else { vmField = new VMField(fieldInfo.Flag, fieldType, fieldInfo.Class, vmClass.FieldSize); module.FieldPoolLink.Add(vmField); vmClass.Fields.Add(vmField); vmClass.FieldSize += fieldType.Size; } } } // 完成静态空间分配 foreach (var vmClass in module.ClassPoolLink) { if (vmClass != null) { vmClass.StaticFieldAddress = StaticArea.Singleton.MallocClassStaticArea(vmClass); Classes.Add(vmClass.StaticFieldAddress, vmClass); } } // Method foreach ((MethodConstantInfo methodInfo, BinaryMethod binaryMethod) in module.MethodPool.Zip(binaryModule.Methods)) { int moduleNameIndex = module.ClassPool[methodInfo.Class - 1].Module; if (moduleNameIndex != binaryModule.ModuleNameIndex) { // 外部方法 externalModuleNameIndexes.Add(moduleNameIndex); // 占位 module.MethodPoolLink.Add(null); } else { module.Classes.TryGetValue(module.StringPoolLink[module.ClassPool[methodInfo.Class - 1].Name - 1], out VMClass vmClass); // 构造VMMethod VMMethod vmMethod = new VMMethod() { Parent = vmClass, Flag = new AccessFlag() { Flag = methodInfo.Flag }, DescriptorAddress = module.StringPoolLink[methodInfo.Descriptor - 1], LocalDescriptorAddress = binaryMethod.LocalDescriptorIndex.Select(i => module.StringPoolLink[i - 1]).ToList(), CodeBlock = MethodArea.Singleton.Malloc(binaryMethod.Instructions) }; Methods.Add(vmMethod.CodeAddress, vmMethod); // 将VMMethod添加到Class中 uint methodNameAddr = module.StringPoolLink[methodInfo.Name - 1]; if (vmClass.Methods.TryGetValue(methodNameAddr, out List <VMMethod> methodGroup)) { methodGroup.Add(vmMethod); } else { vmClass.Methods.Add(methodNameAddr, new List <VMMethod>() { vmMethod }); } // 建立Link module.MethodPoolLink.Add(vmMethod); } } if (!isDependency) { DependencyLoadWatch.Start(); } // 导入外部模块 foreach (int externalModuleNameIndex in externalModuleNameIndexes) { if (!Modules.ContainsKey(module.StringPoolLink[externalModuleNameIndex - 1])) { // 导入未导入的模块,图的广度优先遍历 AddModule(Program.LoadModule(binaryModule.StringPool[externalModuleNameIndex - 1]), true); } } if (!isDependency) { DependencyLoadWatch.Stop(); } // 链接外部符号 ExternalSymbolResolution(module); if (!isDependency) { LoadWatch.Stop(); } return(module); }
public void Execute(VMMethod entry) { CurrentMethod = entry; Execute(); }