Ejemplo n.º 1
0
        public unsafe bool Patch(ModuleDef module, out RawModuleBytes newModuleData)
        {
            var moduleData = this.moduleData;

            // NOTE: We can't remove the type from the corlib (eg. mscorlib) because the compiler
            // (Roslyn) won't recognize it as the corlib if it has any AssemblyRefs.
            // A possible fix is to add a new netmodule to the corlib assembly.
            bool fixTypeDefRefs = !(nonNestedEditedType is null) &&
                                  MDPatcherUtils.ExistsInMetadata(nonNestedEditedType) &&
                                  MDPatcherUtils.ReferencesModule(module, nonNestedEditedType?.Module) &&
                                  !module.Assembly.IsCorLib();
            bool addIVT = module == editedModule || MDPatcherUtils.HasModuleInternalAccess(module, editedModule);

            if (fixTypeDefRefs || addIVT)
            {
                DnSpyEventSource.Log.EditCodePatchModuleStart(module.Location);
                using (var md = MDPatcherUtils.TryCreateMetadata(moduleData, isFileLayout)) {
                    if (md is null)
                    {
                        throw new InvalidOperationException("Couldn't create metadata");
                    }
                    var mdEditor = new MetadataEditor(moduleData, md);
                    var options  = MDEditorPatcherOptions.None;
                    if (fixTypeDefRefs)
                    {
                        options |= MDEditorPatcherOptions.UpdateTypeReferences;
                    }
                    if (addIVT)
                    {
                        options |= MDEditorPatcherOptions.AllowInternalAccess;
                    }
                    var patcher = new MDEditorPatcher(moduleData, mdEditor, tempAssembly, nonNestedEditedType, options);
                    patcher.Patch(module);
                    if (mdEditor.MustRewriteMetadata())
                    {
                        var stream = new MDWriterMemoryStream();
                        new MDWriter(moduleData, mdEditor, stream).Write();
                        NativeMemoryRawModuleBytes?newRawData = null;
                        try {
                            newRawData = new NativeMemoryRawModuleBytes((int)stream.Length, isFileLayout: true);
                            stream.CopyTo((IntPtr)newRawData.Pointer, newRawData.Size);
                            moduleData = newRawData;
                        }
                        catch {
                            newRawData?.Dispose();
                            throw;
                        }
                    }
                }
                DnSpyEventSource.Log.EditCodePatchModuleStop(module.Location);
            }

            newModuleData = moduleData;
            return(true);
        }
Ejemplo n.º 2
0
        public unsafe bool Patch(ModuleDef module, out RawModuleBytes newModuleData)
        {
            var moduleData = this.moduleData;

            // NOTE: We can't remove the type from the corlib (eg. mscorlib) because the compiler
            // (Roslyn) won't recognize it as the corlib if it has any AssemblyRefs.
            // A possible fix is to add a new netmodule to the corlib assembly.
            bool fixTypeDefRefs = nonNestedEditedTypeOrNull != null &&
                                  MDPatcherUtils.ExistsInMetadata(nonNestedEditedTypeOrNull) &&
                                  MDPatcherUtils.ReferencesModule(module, nonNestedEditedTypeOrNull?.Module) &&
                                  !module.Assembly.IsCorLib();

            if (makeEverythingPublic || fixTypeDefRefs)
            {
                using (var md = MDPatcherUtils.TryCreateMetadata(moduleData, isFileLayout)) {
                    if (makeEverythingPublic)
                    {
                        new MetadataFixer(moduleData, md).MakePublic();
                    }
                    if (fixTypeDefRefs)
                    {
                        var mdEditor = new MetadataEditor(moduleData, md);
                        var patcher  = new MDEditorPatcher(moduleData, mdEditor, tempAssembly, nonNestedEditedTypeOrNull);
                        patcher.Patch(module);
                        if (mdEditor.MustRewriteMetadata())
                        {
                            var stream = new MDWriterMemoryStream();
                            new MDWriter(moduleData, mdEditor, stream).Write();
                            NativeMemoryRawModuleBytes newRawData = null;
                            try {
                                newRawData = new NativeMemoryRawModuleBytes((int)stream.Length);
                                stream.CopyTo((IntPtr)newRawData.Pointer, newRawData.Size);
                                moduleData = newRawData;
                            }
                            catch {
                                newRawData?.Dispose();
                                throw;
                            }
                        }
                    }
                }
            }

            newModuleData = moduleData;
            return(true);
        }