├── README.md
├── RazorSharp.sln
├── RazorSharp
├── Analysis
│ ├── Inspect.cs
│ ├── InspectOptions.cs
│ ├── ObjectGuide.cs
│ └── ObjectInfo.cs
├── Core
│ ├── Clr.cs
│ ├── Global.cs
│ └── Initializer.cs
├── CoreClr
│ ├── Constants.cs
│ ├── GCHeap.cs
│ ├── Meta
│ │ ├── Base
│ │ │ ├── AnonymousClrStructure.cs
│ │ │ ├── ClrStructure.cs
│ │ │ ├── ClrStructureSettings.cs
│ │ │ └── EmbeddedClrStructure.cs
│ │ ├── MetaField.cs
│ │ ├── MetaIL.cs
│ │ ├── MetaLayout.cs
│ │ ├── MetaMethod.cs
│ │ └── MetaType.cs
│ ├── Metadata
│ │ ├── Enums
│ │ │ ├── CorEnums.cs
│ │ │ ├── EEClassEnums.cs
│ │ │ ├── FieldDescEnums.cs
│ │ │ ├── MethodDescEnums.cs
│ │ │ └── MethodTableEnums.cs
│ │ ├── ExecutionEngine
│ │ │ ├── EEClass.cs
│ │ │ ├── EEClassLayoutInfo.cs
│ │ │ └── LayoutEEClass.cs
│ │ ├── FieldDesc.cs
│ │ ├── JitIL
│ │ │ ├── FatILMethod.cs
│ │ │ ├── ILMethod.cs
│ │ │ ├── InspectIL.cs
│ │ │ ├── Instruction.cs
│ │ │ └── TinyILMethod.cs
│ │ ├── MethodDesc.cs
│ │ ├── MethodTable.cs
│ │ ├── ObjHeader.cs
│ │ ├── PackedDWORDFields.cs
│ │ └── TypeHandle.cs
│ ├── Offsets.cs
│ ├── Runtime.Info.cs
│ ├── Runtime.cs
│ └── Tokens.cs
├── Import
│ ├── Attributes
│ │ ├── ImportAttribute.cs
│ │ ├── ImportCallAttribute.cs
│ │ ├── ImportFieldAttribute.cs
│ │ ├── ImportForwardCallAttribute.cs
│ │ ├── ImportMapDesignationAttribute.cs
│ │ └── ImportNamespaceAttribute.cs
│ ├── Enums
│ │ ├── IdentifierOptions.cs
│ │ ├── ImportCallOptions.cs
│ │ └── ImportFieldOptions.cs
│ ├── IImportProvider.cs
│ ├── ImportManager.cs
│ ├── ImportMap.cs
│ ├── ModuleImport.cs
│ └── SymbolManager.cs
├── Interop
│ ├── Enums
│ │ ├── DataSectionFlags.cs
│ │ ├── MemState.cs
│ │ ├── MemType.cs
│ │ ├── MemoryProtection.cs
│ │ ├── ProcessAccess.cs
│ │ ├── SymbolFlag.cs
│ │ ├── SymbolOptions.cs
│ │ └── SymbolTag.cs
│ ├── FunctionFactory.Delegates.cs
│ ├── FunctionFactory.Managed.cs
│ ├── FunctionFactory.cs
│ ├── Functions.Fluid.cs
│ ├── Functions.Module.cs
│ ├── Functions.Native.cs
│ ├── Functions.Reflection.cs
│ ├── Functions.cs
│ ├── Native.DebugHelp.cs
│ ├── Native.Kernel32.cs
│ ├── Native.cs
│ ├── Structures
│ │ ├── ImageDOSHeader.cs
│ │ ├── ImageDataDirectory.cs
│ │ ├── ImageFileHeader.cs
│ │ ├── ImageOptionalHeader32.cs
│ │ ├── ImageOptionalHeader64.cs
│ │ ├── ImageSectionHeader.cs
│ │ ├── ImageSectionInfo.cs
│ │ ├── MemoryBasicInformation.cs
│ │ ├── Symbol.cs
│ │ └── SymbolInfo.cs
│ └── Utilities
│ │ ├── NativeAttribute.cs
│ │ └── PEHeaderReader.cs
├── Memory
│ ├── Allocation
│ │ ├── AllocationManager.cs
│ │ ├── Allocators.cs
│ │ └── IAllocator.cs
│ ├── Components
│ │ ├── ComponentField.cs
│ │ ├── IStructure.cs
│ │ ├── MemoryField.cs
│ │ └── PaddingField.cs
│ ├── Converter.cs
│ ├── Enums
│ │ ├── OffsetOptions.cs
│ │ └── SizeOfOptions.cs
│ ├── Mem.Kernel.cs
│ ├── Mem.cs
│ ├── Pointers
│ │ ├── Handle.cs
│ │ └── Pointer.cs
│ ├── Segment.cs
│ ├── SigScanner.cs
│ └── Unsafe.cs
├── Model
│ ├── Closable.cs
│ └── Releasable.cs
├── Properties
│ └── AssemblyInfo.cs
├── RazorSharp.csproj
├── Utilities
│ ├── Bits.cs
│ ├── EnumFlags.cs
│ ├── Fixed
│ │ ├── ObjectPinner.cs
│ │ └── PinHelper.cs
│ ├── Members.cs
│ ├── Modules.cs
│ └── Security
│ │ ├── Exceptions
│ │ ├── AmbiguousStateException.cs
│ │ ├── ClrException.cs
│ │ ├── CorILException.cs
│ │ ├── CoreException.cs
│ │ ├── ImageException.cs
│ │ ├── ImportException.cs
│ │ └── SentryException.cs
│ │ ├── FailMessageTemplateAttribute.cs
│ │ └── Guard.cs
└── packages.config
├── Test
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
└── Test.csproj
├── icon512.png
└── icon64.png
/README.md:
--------------------------------------------------------------------------------
1 | # RazorSharp
2 |
3 | [](https://www.nuget.org/packages/RazorSharp/)
4 | [](https://www.nuget.org/packages/RazorSharp/)
5 |
6 | 
7 |
8 | Low-level utilities and tools for working with the CLR, CLR internal structures, and memory.
9 |
10 | # Notice
11 |
12 | **For a new and improved .NET 5 implementation, see [Novus](https://github.com/Decimation/Novus).**
13 |
14 | # Goals
15 |
16 | `RazorSharp` aims to provide functionality similar to that of `ClrMD`, `WinDbg SOS`, and `Reflection` but in a more detailed fashion while also exposing more underlying metadata and CLR functionality.
17 |
18 | `RazorSharp` also allows for manipulation of the CLR and low-level operations with managed objects. Additionally, `RazorSharp` doesn't require attachment of a debugger to the process to acquire metadata. All metadata is acquired through memory or low-level functions.
19 |
20 | # Usage
21 |
22 | Some structures of `RazorSharp` must be set up to use certain features. Their respective type initializers and constructors should do this automatically, but this is still being tested as static initialization can be tricky.
23 |
24 | # Features
25 |
26 | * Calculating heap size of managed objects
27 | * Taking the address of managed objects
28 | * Pointer to managed types
29 | * Pinning unblittable objects
30 | * And much more
31 |
32 | # Compatibility
33 | * 64-bit (only partial 32-bit support)
34 | * Windows
35 | * .NET CLR
36 | * .NET Framework 4.7.2
37 | * Workstation GC
38 |
39 | # License
40 |
41 | Icons made by Freepik
42 |
--------------------------------------------------------------------------------
/RazorSharp.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28010.2016
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RazorSharp", "RazorSharp\RazorSharp.csproj", "{D350BF0A-5CA4-43C3-8F01-32F561FE0727}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj", "{B2CA536C-C670-4643-A5C7-C57070E95223}"
9 | EndProject
10 | Global
11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
12 | Debug|Any CPU = Debug|Any CPU
13 | Release|Any CPU = Release|Any CPU
14 | EndGlobalSection
15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
16 | {D350BF0A-5CA4-43C3-8F01-32F561FE0727}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17 | {D350BF0A-5CA4-43C3-8F01-32F561FE0727}.Debug|Any CPU.Build.0 = Debug|Any CPU
18 | {D350BF0A-5CA4-43C3-8F01-32F561FE0727}.Release|Any CPU.ActiveCfg = Release|Any CPU
19 | {D350BF0A-5CA4-43C3-8F01-32F561FE0727}.Release|Any CPU.Build.0 = Release|Any CPU
20 | {B2CA536C-C670-4643-A5C7-C57070E95223}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21 | {B2CA536C-C670-4643-A5C7-C57070E95223}.Debug|Any CPU.Build.0 = Debug|Any CPU
22 | {B2CA536C-C670-4643-A5C7-C57070E95223}.Release|Any CPU.ActiveCfg = Release|Any CPU
23 | {B2CA536C-C670-4643-A5C7-C57070E95223}.Release|Any CPU.Build.0 = Release|Any CPU
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {22D521BA-2C52-4408-A385-DA2E6A9DE6E2}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/RazorSharp/Analysis/Inspect.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using RazorSharp.Utilities;
3 |
4 | namespace RazorSharp.Analysis
5 | {
6 | public static class Inspect
7 | {
8 | public static ObjectInfo Scan(ref T value, InspectOptions options)
9 | {
10 | var info = Scan(value.GetType(), options);
11 |
12 | info.Update(ref value);
13 |
14 | return info;
15 | }
16 |
17 | public static ObjectInfo Scan(Type t, InspectOptions options)
18 | {
19 | var info = new ObjectInfo(t, options);
20 |
21 | info.Update();
22 |
23 | return info;
24 | }
25 | }
26 | }
--------------------------------------------------------------------------------
/RazorSharp/Analysis/InspectOptions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace RazorSharp.Analysis
4 | {
5 | [Flags]
6 | public enum InspectOptions
7 | {
8 | None = 0,
9 |
10 | Fields = 1 << 0,
11 |
12 | Values = 1 << 1,
13 |
14 | MemoryFields = (1 << 2) | Fields,
15 |
16 | Addresses = 1 << 3,
17 |
18 | Padding = 1 << 4,
19 | }
20 | }
--------------------------------------------------------------------------------
/RazorSharp/Analysis/ObjectGuide.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using RazorSharp.Memory;
4 | using RazorSharp.Memory.Components;
5 | using RazorSharp.Memory.Pointers;
6 | using RazorSharp.Utilities;
7 | using RazorSharp.Utilities.Security;
8 | using SimpleSharp.Diagnostics;
9 |
10 | namespace RazorSharp.Analysis
11 | {
12 | internal class ObjectGuide
13 | {
14 | private IStructure Struct { get; }
15 |
16 | private InspectOptions Options { get; }
17 |
18 | private Pointer Address { get; set; }
19 |
20 | private object Value { get; set; }
21 |
22 | internal ObjectGuide(IStructure structure, InspectOptions options)
23 | {
24 | Struct = structure;
25 | Address = Mem.Nullptr;
26 | Value = null;
27 | Options = options;
28 | }
29 |
30 | internal void Update(ref T t)
31 | {
32 | Value = Struct.GetValue(t);
33 | Address = Struct.GetAddress(ref t);
34 |
35 | Conditions.AssertDebug(Value == Address.ReadAny(Value.GetType()));
36 | }
37 |
38 | public override string ToString()
39 | {
40 | var sb = new StringBuilder();
41 | sb.AppendFormat("Name: {0} | ", Struct.Name)
42 | .AppendFormat("Offset: {0} | ", Struct.Offset)
43 | .AppendFormat("Size: {0}", Struct.Size);
44 |
45 | if (Options.HasFlagFast(InspectOptions.Values)) {
46 | if (Value == null) {
47 | throw Guard.InvalidOperationFail("Value is required");
48 | }
49 |
50 | sb.AppendFormat(" | Value: {0}", Value);
51 | }
52 |
53 | if (Options.HasFlagFast(InspectOptions.Addresses)) {
54 | if (Address.IsNull) {
55 | throw Guard.InvalidOperationFail("Address is null");
56 | }
57 | sb.AppendFormat(" | Address: {0}", Address);
58 | }
59 |
60 | return sb.ToString();
61 | }
62 | }
63 | }
--------------------------------------------------------------------------------
/RazorSharp/Analysis/ObjectInfo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Text;
6 | using RazorSharp.CoreClr;
7 | using RazorSharp.CoreClr.Meta;
8 | using RazorSharp.CoreClr.Metadata.Enums;
9 | using RazorSharp.Memory;
10 | using RazorSharp.Memory.Components;
11 | using RazorSharp.Memory.Pointers;
12 | using RazorSharp.Utilities;
13 | using RazorSharp.Utilities.Security;
14 | using SimpleSharp.Diagnostics;
15 |
16 | // ReSharper disable ReturnTypeCanBeEnumerable.Local
17 |
18 | namespace RazorSharp.Analysis
19 | {
20 | public class ObjectInfo
21 | {
22 | private object m_value;
23 |
24 | public IStructure[] Structures { get; private set; }
25 |
26 | public MetaType Type { get; }
27 |
28 | private object Value {
29 | get => m_value;
30 | set {
31 | if (value.GetType() != Type || value == null) {
32 | throw Guard.InvalidOperationFail(nameof(value));
33 | }
34 |
35 | m_value = value;
36 | }
37 | }
38 |
39 | private Pointer Address { get; set; }
40 |
41 | private InspectOptions Options { get; }
42 |
43 | private InspectOptions Completed { get; set; }
44 |
45 | private bool HasValue => Value != null;
46 |
47 | private ObjectGuide[] m_guides;
48 |
49 |
50 | internal ObjectInfo(MetaType t, InspectOptions options)
51 | {
52 | if (options.HasFlagFast(InspectOptions.None)) {
53 | throw new ArgumentException();
54 | }
55 |
56 | Type = t;
57 | Options = options;
58 | }
59 |
60 | private void HandleOption(InspectOptions options, Action fn)
61 | {
62 | if (Options.HasFlagFast(options) && !Completed.HasFlagFast(options)) {
63 | fn();
64 | Completed |= options;
65 | }
66 | }
67 |
68 | internal ObjectInfo Update(ref T t)
69 | {
70 | Update();
71 |
72 | Value = t;
73 | Address = Unsafe.AddressOf(ref t).Cast();
74 |
75 | foreach (var objectGuide in m_guides) {
76 | objectGuide.Update(ref t);
77 | }
78 |
79 | return this;
80 | }
81 |
82 | internal ObjectInfo Update()
83 | {
84 | if (HasValue) {
85 | HandleOption(InspectOptions.MemoryFields, AddMemoryFields);
86 | }
87 |
88 | HandleOption(InspectOptions.Fields, AddFields);
89 |
90 | HandleOption(InspectOptions.Padding, AddPaddingFields);
91 |
92 | m_guides = BuildGuides();
93 |
94 | return this;
95 | }
96 |
97 | private void AddFields()
98 | {
99 | var fields = Type.Fields.Where(f => !f.IsStatic).ToArray();
100 |
101 | var structures = new List(fields.Length);
102 | structures.AddRange(fields);
103 | Structures = structures.ToArray();
104 | }
105 |
106 | private void AddMemoryFields()
107 | {
108 | var structures = new List();
109 |
110 | if (Type.IsStringOrArray) {
111 | Array array = null;
112 |
113 | if (Type.IsString) {
114 | string s = m_value as string;
115 |
116 | // ReSharper disable once AssignNullToNotNullAttribute
117 | array = s.ToArray();
118 | }
119 |
120 | if (Type.IsArray) {
121 | array = m_value as Array;
122 |
123 | structures.Add(new MemoryField("(Array length)", 0, sizeof(int)));
124 |
125 | if (Mem.Is64Bit) {
126 | structures.Add(new PaddingField(sizeof(int)));
127 | }
128 | }
129 |
130 | // ReSharper disable once PossibleNullReferenceException
131 | for (int i = 0; i < array.Length; i++) {
132 | structures.Add(new ComponentField(Type, i));
133 | }
134 | }
135 |
136 |
137 | // We assume that fields are already added
138 | var buf = Structures.ToList();
139 | buf.AddRange(structures);
140 | Structures = buf.ToArray();
141 | }
142 |
143 | private void AddPaddingFields()
144 | {
145 | var padding = new List();
146 | var nextOffsetOrSize = Type.InstanceFieldsSize;
147 | var memFields = Type.Fields.Where(f => !f.IsStatic).ToArray();
148 |
149 | for (int i = 0; i < memFields.Length; i++) {
150 | // start padding
151 |
152 | if (i != memFields.Length - 1) {
153 | nextOffsetOrSize = Type.Fields[i + 1].Offset;
154 | }
155 |
156 | int nextSectOfsCandidate = Type.Fields[i].Offset + Type.Fields[i].Size;
157 |
158 | if (nextSectOfsCandidate < nextOffsetOrSize) {
159 | int padSize = nextOffsetOrSize - nextSectOfsCandidate;
160 |
161 | padding.Add(new PaddingField(nextSectOfsCandidate, padSize));
162 | }
163 |
164 | // end padding
165 | }
166 |
167 |
168 | // We assume that fields are already added
169 | var buf = Structures.ToList();
170 | buf.AddRange(padding);
171 | Structures = buf.OrderBy(f => f.Offset).ToArray();
172 | }
173 |
174 |
175 | private ObjectGuide[] BuildGuides()
176 | {
177 | var dumps = new ObjectGuide[Structures.Length];
178 | for (int i = 0; i < dumps.Length; i++) {
179 | dumps[i] = new ObjectGuide(Structures[i], Options);
180 | }
181 |
182 | return dumps;
183 | }
184 |
185 | public void Dump()
186 | {
187 | foreach (var structure in m_guides) {
188 | Console.WriteLine(structure);
189 | }
190 | }
191 |
192 | public override string ToString()
193 | {
194 | return base.ToString();
195 | }
196 | }
197 | }
--------------------------------------------------------------------------------
/RazorSharp/Core/Clr.cs:
--------------------------------------------------------------------------------
1 | #region
2 |
3 | using System;
4 | using System.Configuration;
5 | using System.Diagnostics;
6 | using System.IO;
7 | using System.Runtime;
8 | using System.Runtime.InteropServices;
9 | using RazorSharp.Import;
10 | using RazorSharp.Model;
11 | using RazorSharp.Utilities;
12 | using SimpleSharp.Diagnostics;
13 |
14 | #endregion
15 |
16 | namespace RazorSharp.Core
17 | {
18 | ///
19 | /// Contains resources for working with the CLR.
20 | ///
21 | internal sealed class Clr : Releasable
22 | {
23 | protected override string Id => nameof(Clr);
24 |
25 | ///
26 | /// CLR DLL file
27 | ///
28 | internal FileInfo LibraryFile { get; }
29 |
30 | ///
31 | /// CLR symbol file
32 | /// A PDB file will be searched for in
33 | ///
34 | internal FileInfo SymbolsFile { get; }
35 |
36 | ///
37 | /// The of the CLR
38 | ///
39 | internal ProcessModule Module { get; }
40 |
41 | ///
42 | /// The of the CLR
43 | ///
44 | internal Version Version { get; }
45 |
46 | ///
47 | /// CLR symbol access.
48 | ///
49 | internal IImportProvider Imports { get; }
50 |
51 |
52 | private static FileInfo GetRuntimeFile(string fileName)
53 | {
54 | string path = RuntimeEnvironment.GetRuntimeDirectory() + fileName;
55 | var file = new FileInfo(path);
56 |
57 | Conditions.Require(file.Exists);
58 |
59 | return file;
60 | }
61 |
62 | public override void Close()
63 | {
64 | // Delete instance
65 | Value = null;
66 |
67 | base.Close();
68 | }
69 |
70 |
71 | #region Constants
72 |
73 | ///
74 | /// CLR module file: clr.dll
75 | ///
76 | private const string CLR_DLL_SHORT = "clr.dll";
77 |
78 | ///
79 | /// CLR symbol file name: clr.pdb
80 | ///
81 | private const string CLR_PDB_SHORT = "clr.pdb";
82 |
83 | #endregion
84 |
85 | #region Singleton
86 |
87 | ///
88 | /// Gets an instance of
89 | ///
90 | internal static Clr Value { get; private set; } = new Clr();
91 |
92 | private Clr()
93 | {
94 | // Version: 4.0.30319.42000
95 | // symchk "C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll" /s SRV*C:\Users\Deci\Desktop\clr.pdb*http://msdl.microsoft.com/download/symbols
96 | // symchk "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll" /s SRV*C:\Users\Deci\Desktop\clr.pdb*http://msdl.microsoft.com/download/symbols
97 |
98 | LibraryFile = GetRuntimeFile(CLR_DLL_SHORT);
99 | SymbolsFile = GetRuntimeFile(CLR_PDB_SHORT);
100 | Module = Modules.FindModule(CLR_DLL_SHORT);
101 | Version = new Version(4, 0, 30319, 42000);
102 | Imports = new ModuleImport(SymbolsFile, Module);
103 |
104 | Setup();
105 | }
106 |
107 | #endregion
108 | }
109 | }
--------------------------------------------------------------------------------
/RazorSharp/Core/Initializer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using RazorSharp.CoreClr;
3 | using RazorSharp.CoreClr.Metadata;
4 | using RazorSharp.Import;
5 | using RazorSharp.Interop;
6 | using RazorSharp.Interop.Utilities;
7 | using RazorSharp.Memory;
8 | using RazorSharp.Model;
9 | using SimpleSharp.Diagnostics;
10 |
11 | namespace RazorSharp.Core
12 | {
13 | internal static class Initializer
14 | {
15 | private const string CONTEXT = "Init";
16 |
17 | private static bool IsSetup { get; set; }
18 |
19 | ///
20 | /// Core CLR types
21 | ///
22 | private static readonly Type[] CoreClrTypes =
23 | {
24 | typeof(FunctionFactory),
25 | typeof(MethodDesc),
26 | typeof(FieldDesc),
27 | typeof(MethodTable),
28 | typeof(GCHeap),
29 | typeof(TypeHandle)
30 | };
31 |
32 | ///
33 | /// Core objects
34 | ///
35 | private static readonly Closable[] CoreObjects =
36 | {
37 | Clr.Value, // Calls Setup in ctor
38 | SymbolManager.Value, // Calls Setup in ctor
39 | ImportManager.Value, // Calls Setup in ctor
40 | Global.Value, // Calls Setup in ctor
41 | Mem.Allocator
42 | };
43 |
44 | internal static void Setup()
45 | {
46 | // Original order: Clr, SymbolManager, Global
47 |
48 | /*foreach (var core in CoreObjects) {
49 | if (core is Releasable releasable && !releasable.IsSetup) {
50 | releasable.Setup();
51 | }
52 | }
53 |
54 | ImportManager.Value.LoadAll(CoreClrTypes, Clr.Value.Imports);*/
55 |
56 | // Register for domain unload
57 |
58 | var appDomain = AppDomain.CurrentDomain;
59 | appDomain.ProcessExit += (sender, eventArgs) => { Close(); };
60 |
61 | IsSetup = true;
62 | }
63 |
64 |
65 | internal static void Close()
66 | {
67 | Conditions.Require(IsSetup);
68 |
69 | // SHUT IT DOWN
70 |
71 | // Original order: Clr, Global, SymbolManager, Mem.Allocator
72 |
73 | foreach (var core in CoreObjects) {
74 | core?.Close();
75 | }
76 |
77 | IsSetup = false;
78 | }
79 | }
80 | }
--------------------------------------------------------------------------------
/RazorSharp/CoreClr/Constants.cs:
--------------------------------------------------------------------------------
1 | namespace RazorSharp.CoreClr
2 | {
3 | internal static unsafe class Constants
4 | {
5 | ///
6 | /// Common value representing an invalid value or a failure
7 | ///
8 | internal const int INVALID_VALUE = -1;
9 |
10 | ///
11 | /// Bits per or
12 | ///
13 | internal const int BITS_PER_DWORD = sizeof(int) * 8;
14 | }
15 | }
--------------------------------------------------------------------------------
/RazorSharp/CoreClr/GCHeap.cs:
--------------------------------------------------------------------------------
1 | using RazorSharp.Core;
2 | using RazorSharp.CoreClr.Meta;
3 | using RazorSharp.CoreClr.Metadata;
4 | using RazorSharp.Import;
5 | using RazorSharp.Import.Attributes;
6 | using RazorSharp.Import.Enums;
7 | using RazorSharp.Interop;
8 | using RazorSharp.Memory;
9 | using RazorSharp.Memory.Pointers;
10 |
11 | // ReSharper disable InconsistentNaming
12 |
13 | namespace RazorSharp.CoreClr
14 | {
15 | [ImportNamespace(WKS_NAMESPACE)]
16 | public static unsafe class GCHeap
17 | {
18 | private const string WKS_NAMESPACE = "WKS";
19 |
20 | static GCHeap()
21 | {
22 | ImportManager.Value.Load(typeof(GCHeap), Clr.Value.Imports);
23 | }
24 |
25 | [ImportField(IdentifierOptions.FullyQualified, ImportFieldOptions.Fast)]
26 | private static readonly Pointer g_pGCHeap;
27 |
28 | [ImportMapDesignation]
29 | private static readonly ImportMap Imports = new ImportMap();
30 |
31 | public static int GCCount {
32 | [ImportCall("GetGcCount", ImportCallOptions.Map)]
33 | get => Functions.Native.Call((void*) Imports[nameof(GCCount)], g_pGCHeap.ToPointer());
34 | }
35 |
36 | public static object AllocateObject(MetaType mt, bool fHandleCom = false)
37 | {
38 | void* ptr = AllocateObject(mt.Value.ToPointer(), fHandleCom);
39 | return Unsafe.Read