forked from dnSpy/dnSpy
/
DsDocument.cs
340 lines (299 loc) · 10.3 KB
/
DsDocument.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
Copyright (C) 2014-2019 de4dot@gmail.com
This file is part of dnSpy
dnSpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dnSpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dnSpy. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using dnlib.DotNet;
using dnlib.PE;
using dnSpy.Contracts.Utilities;
namespace dnSpy.Contracts.Documents {
/// <summary>
/// Document base class
/// </summary>
public abstract class DsDocument : IDsDocument2 {
/// <inheritdoc/>
public abstract DsDocumentInfo? SerializedDocument { get; }
/// <inheritdoc/>
public abstract IDsDocumentNameKey Key { get; }
/// <inheritdoc/>
public AssemblyDef? AssemblyDef => ModuleDef?.Assembly;
/// <inheritdoc/>
public virtual ModuleDef? ModuleDef => null;
/// <inheritdoc/>
public virtual IPEImage? PEImage => (ModuleDef as ModuleDefMD)?.Metadata?.PEImage;
/// <inheritdoc/>
public string Filename {
get => filename;
set {
if (filename != value) {
filename = value;
OnPropertyChanged(nameof(Filename));
}
}
}
string filename = string.Empty;
/// <summary>
/// Gets called when a property has changed
/// </summary>
/// <param name="propName">Name of property</param>
protected virtual void OnPropertyChanged(string propName) {
}
/// <inheritdoc/>
public bool IsAutoLoaded { get; set; }
/// <inheritdoc/>
public TList<IDsDocument> Children {
get {
if (children is null) {
lock (lockObj) {
if (children is null) {
children = CreateChildren();
Debug2.Assert(children is not null);
if (children is null)
children = new TList<IDsDocument>();
}
}
}
return children;
}
}
readonly object lockObj;
TList<IDsDocument>? children;
/// <inheritdoc/>
public bool ChildrenLoaded => children is not null;
/// <summary>
/// Creates the children
/// </summary>
/// <returns></returns>
protected virtual TList<IDsDocument> CreateChildren() => new TList<IDsDocument>();
/// <summary>
/// Constructor
/// </summary>
protected DsDocument() => lockObj = new object();
/// <inheritdoc/>
public T? AddAnnotation<T>(T? annotation) where T : class => annotations.AddAnnotation(annotation);
/// <inheritdoc/>
public T? Annotation<T>() where T : class => annotations.Annotation<T>();
/// <inheritdoc/>
public IEnumerable<T> Annotations<T>() where T : class => annotations.Annotations<T>();
/// <inheritdoc/>
public void RemoveAnnotations<T>() where T : class => annotations.RemoveAnnotations<T>();
readonly AnnotationsImpl annotations = new AnnotationsImpl();
/// <inheritdoc/>
public virtual void OnAdded() { }
}
/// <summary>
/// Unknown type of file
/// </summary>
public sealed class DsUnknownDocument : DsDocument {
/// <inheritdoc/>
public override DsDocumentInfo? SerializedDocument => DsDocumentInfo.CreateDocument(Filename);
/// <inheritdoc/>
public override IDsDocumentNameKey Key => new FilenameKey(Filename);
/// <summary>
/// Constructor
/// </summary>
/// <param name="filename">Filename</param>
public DsUnknownDocument(string filename) => Filename = filename ?? string.Empty;
}
/// <summary>
/// PE file
/// </summary>
public sealed class DsPEDocument : DsDocument, IDsPEDocument, IDisposable {
/// <inheritdoc/>
public override DsDocumentInfo? SerializedDocument => DsDocumentInfo.CreateDocument(Filename);
/// <inheritdoc/>
public override IDsDocumentNameKey Key => FilenameKey.CreateFullPath(Filename);
/// <inheritdoc/>
public override IPEImage? PEImage { get; }
/// <summary>
/// Constructor
/// </summary>
/// <param name="peImage">PE image</param>
public DsPEDocument(IPEImage peImage) {
PEImage = peImage;
Filename = peImage.Filename ?? string.Empty;
}
/// <inheritdoc/>
public void Dispose() => PEImage!.Dispose();
}
/// <summary>
/// .NET file base class
/// </summary>
public abstract class DsDotNetDocumentBase : DsDocument, IDsDotNetDocument, IInMemoryDocument {
/// <inheritdoc/>
public override ModuleDef? ModuleDef { get; }
/// <inheritdoc/>
public virtual bool IsActive => true;
/// <summary>true if the symbols have been loaded</summary>
protected bool loadedSymbols;
/// <summary>
/// Constructor
/// </summary>
/// <param name="module">Module</param>
/// <param name="loadSyms">true if symbols should be loaded</param>
protected DsDotNetDocumentBase(ModuleDef module, bool loadSyms) {
ModuleDef = module;
loadedSymbols = loadSyms;
Filename = module.Location ?? string.Empty;
module.EnableTypeDefFindCache = true;
}
/// <inheritdoc/>
public override void OnAdded() {
if (loadedSymbols)
LoadSymbols();
base.OnAdded();
}
/// <summary>
/// Creates a module context
/// </summary>
/// <param name="asmResolver">Assembly resolver</param>
/// <returns></returns>
public static ModuleContext CreateModuleContext(IAssemblyResolver asmResolver) {
var moduleCtx = new ModuleContext();
moduleCtx.AssemblyResolver = asmResolver;
// Disable WinMD projection since the user probably expects that clicking on a type
// will take you to that type, and not to the projected CLR type.
// The decompiler shouldn't have a problem with this since it uses SigComparer() which
// defaults to projecting WinMD types.
moduleCtx.Resolver = new Resolver(moduleCtx.AssemblyResolver) { ProjectWinMDRefs = false };
return moduleCtx;
}
void LoadSymbols() {
Debug2.Assert(ModuleDef is not null);
// Happens if a module has been removed but then the exact same instance
// was re-added.
if (ModuleDef.PdbState is not null)
return;
var m = ModuleDef as ModuleDefMD;
if (m is null)
return;
try {
m.LoadPdb();
}
catch {
}
}
}
/// <summary>
/// .NET file
/// </summary>
public class DsDotNetDocument : DsDotNetDocumentBase, IDisposable {
readonly bool isAsmNode;
/// <inheritdoc/>
public override IDsDocumentNameKey Key => FilenameKey.CreateFullPath(Filename);
/// <inheritdoc/>
public override DsDocumentInfo? SerializedDocument => documentInfo;
DsDocumentInfo documentInfo;
/// <summary>
/// Constructor
/// </summary>
/// <param name="documentInfo">Document info</param>
/// <param name="module">Module</param>
/// <param name="loadSyms">true to load symbols</param>
/// <param name="isAsmNode">true if it's an assembly node, false if it's a module node</param>
protected DsDotNetDocument(DsDocumentInfo documentInfo, ModuleDef module, bool loadSyms, bool isAsmNode)
: base(module, loadSyms) {
this.documentInfo = documentInfo;
this.isAsmNode = isAsmNode;
}
/// <inheritdoc/>
protected override void OnPropertyChanged(string propName) {
base.OnPropertyChanged(propName);
if (propName == nameof(Filename))
documentInfo = DsDocumentInfo.CreateDocument(Filename);
}
/// <summary>
/// Creates an assembly
/// </summary>
/// <param name="documentInfo">Document info</param>
/// <param name="module">Module</param>
/// <param name="loadSyms">true to load symbols</param>
/// <returns></returns>
public static DsDotNetDocument CreateAssembly(DsDocumentInfo documentInfo, ModuleDef module, bool loadSyms) => new DsDotNetDocument(documentInfo, module, loadSyms, true);
/// <summary>
/// Creates a module
/// </summary>
/// <param name="documentInfo">Document info</param>
/// <param name="module">Module</param>
/// <param name="loadSyms">true to load symbols</param>
/// <returns></returns>
public static DsDotNetDocument CreateModule(DsDocumentInfo documentInfo, ModuleDef module, bool loadSyms) => new DsDotNetDocument(documentInfo, module, loadSyms, false);
/// <summary>
/// Creates an assembly
/// </summary>
/// <param name="module">Module</param>
/// <returns></returns>
public static DsDotNetDocument CreateAssembly(IDsDotNetDocument module) => new DsDotNetDocumentAsmWithMod(module);
/// <inheritdoc/>
protected override TList<IDsDocument> CreateChildren() {
var asm = AssemblyDef;
var list = new TList<IDsDocument>(asm is null ? 1 : asm.Modules.Count);
if (isAsmNode && asm is not null) {
bool foundThis = false;
foreach (var module in asm.Modules) {
if (ModuleDef == module) {
Debug.Assert(!foundThis);
foundThis = true;
}
list.Add(new DsDotNetDocument(DsDocumentInfo.CreateDocument(module.Location), module, loadedSymbols, false));
}
Debug.Assert(foundThis);
}
return list;
}
/// <inheritdoc/>
public void Dispose() => ModuleDef!.Dispose();
}
sealed class DsDotNetDocumentAsmWithMod : DsDotNetDocument {
IDsDotNetDocument? module;
public DsDotNetDocumentAsmWithMod(IDsDotNetDocument modmodule)
: base(modmodule.SerializedDocument ?? new DsDocumentInfo(), modmodule.ModuleDef!, false, true) => module = modmodule;
protected override TList<IDsDocument> CreateChildren() {
Debug2.Assert(module is not null);
var list = new TList<IDsDocument>();
if (module is not null)
list.Add(module);
module = null;
return list;
}
}
/// <summary>
/// mmap'd I/O helper methods
/// </summary>
static class MemoryMappedIOHelper {
/// <summary>
/// Disable memory mapped I/O
/// </summary>
/// <param name="document">Document</param>
public static void DisableMemoryMappedIO(IDsDocument document) {
if (document is null)
return;
DisableMemoryMappedIO(document.PEImage);
}
/// <summary>
/// Disable memory mapped I/O
/// </summary>
/// <param name="peImage">PE image</param>
public static void DisableMemoryMappedIO(IPEImage? peImage) {
if (peImage is null)
return;
// Files in the GAC are read-only so there's no need to disable memory mapped I/O to
// allow other programs to write to the file.
if (GacInfo.IsGacPath(peImage.Filename))
return;
(peImage as IInternalPEImage)?.UnsafeDisableMemoryMappedIO();
}
}
}