-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathCilVirtualMachine.cs
More file actions
234 lines (207 loc) · 8.12 KB
/
CilVirtualMachine.cs
File metadata and controls
234 lines (207 loc) · 8.12 KB
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using AsmResolver.DotNet;
using Echo.Memory;
using Echo.Platforms.AsmResolver.Emulation.Dispatch;
using Echo.Platforms.AsmResolver.Emulation.Heap;
using Echo.Platforms.AsmResolver.Emulation.Invocation;
using Echo.Platforms.AsmResolver.Emulation.Runtime;
using Echo.Platforms.AsmResolver.Emulation.Stack;
namespace Echo.Platforms.AsmResolver.Emulation
{
/// <summary>
/// Represents a machine that executes CIL instructions in a virtual environment.
/// </summary>
public class CilVirtualMachine
{
/// <summary>
/// Fires when a new thread was created.
/// </summary>
public event EventHandler<CilThread>? ThreadCreated;
/// <summary>
/// Fires when a thread was destroyed.
/// </summary>
public event EventHandler<CilThread>? ThreadDestroyed;
private readonly List<CilThread> _threads = new();
private readonly CallStackMemory _callStackMemory;
/// <summary>
/// Creates a new CIL virtual machine.
/// </summary>
/// <param name="contextModule">The main module to base the context on.</param>
/// <param name="is32Bit">Indicates whether the virtual machine runs in 32-bit mode or 64-bit mode.</param>
public CilVirtualMachine(RuntimeContext runtimeContext, bool is32Bit)
{
Memory = new VirtualMemory(is32Bit ? uint.MaxValue : long.MaxValue);
Loader = new PELoader(Memory);
ValueFactory = new ValueFactory(runtimeContext, is32Bit);
HostObjects = new ObjectMapMemory<object, HostObject>(0x1000_0000, o => new HostObject(o, this));
ObjectMarshaller = new ObjectMarshaller(this);
if (is32Bit)
{
Memory.Map(0x1000_0000, Heap = new ManagedObjectHeap(0x0100_0000, ValueFactory));
Memory.Map(0x6000_0000, HostObjects);
Memory.Map(0x7000_0000, StaticFields = new StaticFieldStorage(ValueFactory, 0x0100_0000));
Memory.Map(0x7100_0000, ValueFactory.ClrMockMemory);
Memory.Map(0x7f00_0000, _callStackMemory = new CallStackMemory(0x100_0000, ValueFactory));
}
else
{
Memory.Map(0x0000_0100_0000_0000, Heap = new ManagedObjectHeap(0x01000_0000, ValueFactory));
Memory.Map(0x0000_7ffd_0000_0000, HostObjects);
Memory.Map(0x0000_7ffe_0000_0000, StaticFields = new StaticFieldStorage(ValueFactory, 0x1000_0000));
Memory.Map(0x0000_7ffe_1000_0000, ValueFactory.ClrMockMemory);
Memory.Map(0x0000_7ffe_8000_0000, _callStackMemory = new CallStackMemory(0x1000_0000, ValueFactory));
}
Dispatcher = new CilDispatcher();
TypeManager = new RuntimeTypeManager(this);
Threads = new ReadOnlyCollection<CilThread>(_threads);
}
/// <summary>
/// Gets a value indicating whether the environment is a 32-bit or 64-bit system.
/// </summary>
public bool Is32Bit => ValueFactory.Is32Bit;
/// <summary>
/// Gets the main memory interface of the virtual machine.
/// </summary>
public VirtualMemory Memory
{
get;
}
/// <summary>
/// Gets the heap used for storing managed objects.
/// </summary>
/// <remarks>
/// The heap is also addressable from <see cref="Memory"/>.
/// </remarks>
public ManagedObjectHeap Heap
{
get;
}
/// <summary>
/// Gets the memory chunk responsible for storing static fields.
/// </summary>
public StaticFieldStorage StaticFields
{
get;
}
/// <summary>
/// Gets the memory manager that embeds managed objects into virtual memory.
/// </summary>
public ObjectMapMemory<object, HostObject> HostObjects
{
get;
}
/// <summary>
/// Gets the service that is responsible for managing types in the virtual machine.
/// </summary>
public ValueFactory ValueFactory
{
get;
}
public RuntimeContext RuntimeContext => ValueFactory.RuntimeContext;
/// <summary>
/// Gets the service that is responsible for mapping executable files in memory.
/// </summary>
public PELoader Loader
{
get;
}
/// <summary>
/// Gets the service that is responsible for dispatching individual instructions to their respective handlers.
/// </summary>
public CilDispatcher Dispatcher
{
get;
}
/// <summary>
/// Gets the service that is responsible for allocating new objects.
/// </summary>
public IObjectAllocator Allocator
{
get;
set;
} = DefaultAllocators.String.WithFallback(DefaultAllocators.VirtualHeap);
/// <summary>
/// Gets the service that is responsible for invoking external functions or methods.
/// </summary>
public IMethodInvoker Invoker
{
get;
set;
} = DefaultInvokers.ReturnUnknown;
/// <summary>
/// Gets the service that is responsible for the initialization and management of runtime types residing in
/// the virtual machine.
/// </summary>
public RuntimeTypeManager TypeManager
{
get;
}
/// <summary>
/// Gets or sets the service that is responsible for resolving unknown values on the stack in critical moments.
/// </summary>
public IUnknownResolver UnknownResolver
{
get;
set;
} = ThrowUnknownResolver.Instance;
/// <summary>
/// Gets or sets the service for marshalling managed objects into bitvectors and back.
/// </summary>
public IObjectMarshaller ObjectMarshaller
{
get;
set;
}
/// <summary>
/// Gets a collection of threads that are currently active in the machine.
/// </summary>
public IReadOnlyList<CilThread> Threads
{
get;
}
/// <summary>
/// Gets or sets flags that control the behavior of the virtual machine.
/// </summary>
public CilEmulationFlags EmulationFlags
{
get;
set;
}
/// <summary>
/// Creates a new thread in the machine.
/// </summary>
/// <param name="stackSize">The amount of memory to allocate for the thread's stack.</param>
/// <returns>The created thread.</returns>
public CilThread CreateThread(uint stackSize = 0x0010_0000)
{
var stack = _callStackMemory.Allocate(stackSize);
var thread = new CilThread(this, stack);
_threads.Add(thread);
ThreadCreated?.Invoke(this, thread);
return thread;
}
/// <summary>
/// Removes a thread and its stack from the machine.
/// </summary>
/// <param name="thread">The thread to remove.</param>
/// <remarks>
/// This does not gracefully terminate a thread. Any code that is still running will remain executing, and may
/// have unwanted side effects. Therefore, be sure to only call this method only when it is certain that no code
/// is running.
/// </remarks>
public void DestroyThread(CilThread thread)
{
if (thread.Machine != this)
throw new ArgumentException("Cannot remove a thread from a different machine.");
if (!thread.IsAlive)
throw new ArgumentException("Cannot destroy a thread that is already destroyed.");
thread.IsAlive = false;
_threads.Remove(thread);
_callStackMemory.Free(thread.CallStack);
ThreadDestroyed?.Invoke(this, thread);
}
}
}