Skip to content

Commit 5c791cb

Browse files
committed
Added locks.
1 parent adeaa09 commit 5c791cb

File tree

4 files changed

+66
-31
lines changed

4 files changed

+66
-31
lines changed

AddressParser/Interpreter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ public IntPtr Execute(Operation operation, RemoteProcess process)
2121
}
2222
else if (operation is ModuleOffsetOperation)
2323
{
24-
var module = process.Modules
25-
.Where(m => m.Name.Equals(((ModuleOffsetOperation)operation).Name, StringComparison.InvariantCultureIgnoreCase))
26-
.FirstOrDefault();
24+
var module = process.GetModuleByName(((ModuleOffsetOperation)operation).Name);
2725
if (module != null)
2826
{
2927
return module.Start;

Memory/RemoteProcess.cs

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ public class Section
5959
}
6060

6161
private readonly List<Module> modules = new List<Module>();
62-
public IEnumerable<Module> Modules => modules;
6362

6463
private readonly List<Section> sections = new List<Section>();
65-
public IEnumerable<Section> Sections => sections;
6664

6765
private readonly Symbols symbols = new Symbols();
6866
public Symbols Symbols => symbols;
@@ -337,7 +335,7 @@ private string ReadRemoteRuntimeTypeInformation64(IntPtr address)
337335
return null;
338336
}
339337

340-
#endregion
338+
#endregion
341339

342340
#region WriteMemory
343341

@@ -377,17 +375,33 @@ public bool WriteRemoteMemory<T>(IntPtr address, T value) where T : struct
377375

378376
public Section GetSectionToPointer(IntPtr address)
379377
{
380-
return sections
381-
.Where(s => s.Category != SectionCategory.Unknown)
382-
.Where(s => address.InRange(s.Start, s.End))
383-
.FirstOrDefault();
378+
lock (sections)
379+
{
380+
return sections
381+
.Where(s => s.Category != SectionCategory.Unknown)
382+
.Where(s => address.InRange(s.Start, s.End))
383+
.FirstOrDefault();
384+
}
384385
}
385386

386387
public Module GetModuleToPointer(IntPtr address)
387388
{
388-
return modules
389-
.Where(m => address.InRange(m.Start, m.End))
390-
.FirstOrDefault();
389+
lock (modules)
390+
{
391+
return modules
392+
.Where(m => address.InRange(m.Start, m.End))
393+
.FirstOrDefault();
394+
}
395+
}
396+
397+
public Module GetModuleByName(string name)
398+
{
399+
lock (modules)
400+
{
401+
return modules
402+
.Where(m => m.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
403+
.FirstOrDefault();
404+
}
391405
}
392406

393407
/// <summary>Tries to map the given address to a section or a module of the process.</summary>
@@ -420,16 +434,22 @@ public Task UpdateProcessInformationsAsync()
420434
{
421435
if (!IsValid)
422436
{
423-
modules.Clear();
424-
sections.Clear();
437+
lock(modules)
438+
{
439+
modules.Clear();
440+
}
441+
lock(sections)
442+
{
443+
sections.Clear();
444+
}
425445

426446
return Task.CompletedTask;
427447
}
428448

429449
return Task.Run(() =>
430450
{
431-
var modules = new List<Module>();
432-
var sections = new List<Section>();
451+
var newModules = new List<Module>();
452+
var newSections = new List<Section>();
433453

434454
nativeHelper.EnumerateRemoteSectionsAndModules(
435455
process.Handle,
@@ -459,11 +479,11 @@ public Task UpdateProcessInformationsAsync()
459479
section.Category = SectionCategory.Data;
460480
break;
461481
}
462-
sections.Add(section);
482+
newSections.Add(section);
463483
},
464484
delegate (IntPtr baseAddress, IntPtr regionSize, string modulePath)
465485
{
466-
modules.Add(new Module
486+
newModules.Add(new Module
467487
{
468488
Start = baseAddress,
469489
End = baseAddress.Add(regionSize),
@@ -473,10 +493,16 @@ public Task UpdateProcessInformationsAsync()
473493
}
474494
);
475495

476-
this.modules.Clear();
477-
this.modules.AddRange(modules);
478-
this.sections.Clear();
479-
this.sections.AddRange(sections);
496+
lock (modules)
497+
{
498+
modules.Clear();
499+
modules.AddRange(newModules);
500+
}
501+
lock (sections)
502+
{
503+
sections.Clear();
504+
sections.AddRange(newSections);
505+
}
480506
});
481507
}
482508

Nodes/BaseHexCommentNode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected int AddComment(ViewInfo view, int x, int y, float fvalue, IntPtr ivalu
5050

5151
if (Program.Settings.ShowCommentSymbol)
5252
{
53-
var module = view.Memory.Process.Modules.Where(m => ivalue.InRange(m.Start, m.End)).FirstOrDefault();
53+
var module = view.Memory.Process.GetModuleToPointer(ivalue);
5454
if (module != null)
5555
{
5656
var symbols = view.Memory.Process.Symbols.GetSymbolsForModule(module);

SymbolReader/Symbols.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,23 @@ public void LoadSymbolsForModule(RemoteProcess.Module module)
9595
Contract.Requires(module != null);
9696

9797
var reader = SymbolReader.FromModule(module, SymbolSearchPath);
98-
symbolReaders[module.Name.ToLower()] = reader;
98+
99+
lock (symbolReaders)
100+
{
101+
symbolReaders[module.Name.ToLower()] = reader;
102+
}
99103
}
100104

101105
public void LoadSymbolsFromPDB(string path)
102106
{
103107
Contract.Requires(path != null);
104108

105109
var reader = SymbolReader.FromDatabase(path);
106-
symbolReaders[Path.GetFileName(path).ToLower()] = reader;
110+
111+
lock (symbolReaders)
112+
{
113+
symbolReaders[Path.GetFileName(path).ToLower()] = reader;
114+
}
107115
}
108116

109117
public SymbolReader GetSymbolsForModule(RemoteProcess.Module module)
@@ -112,14 +120,17 @@ public SymbolReader GetSymbolsForModule(RemoteProcess.Module module)
112120

113121
var name = module.Name.ToLower();
114122

115-
SymbolReader reader;
116-
if (!symbolReaders.TryGetValue(name, out reader))
123+
lock (symbolReaders)
117124
{
118-
name = Path.ChangeExtension(name, ".pdb");
125+
SymbolReader reader;
126+
if (!symbolReaders.TryGetValue(name, out reader))
127+
{
128+
name = Path.ChangeExtension(name, ".pdb");
119129

120-
symbolReaders.TryGetValue(name, out reader);
130+
symbolReaders.TryGetValue(name, out reader);
131+
}
132+
return reader;
121133
}
122-
return reader;
123134
}
124135
}
125136
}

0 commit comments

Comments
 (0)