Skip to content

Commit 9f5a3ae

Browse files
committed
updated to memflow-ffi 0.2.1
1 parent 8293976 commit 9f5a3ae

11 files changed

Lines changed: 851 additions & 422 deletions

File tree

CodeGenFix/CodeGenFix.csproj

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<StartupObject>CodeGenFix.Program</StartupObject>
99
<Platforms>x64</Platforms>
10+
<Copyright>Copyright © uberhalit 2024</Copyright>
11+
<Version>0.2.1</Version>
12+
</PropertyGroup>
13+
14+
<PropertyGroup>
15+
<!-- Workaround for issue https://github.com/microsoft/ClangSharp/issues/129 -->
16+
<RuntimeIdentifier Condition="'$(RuntimeIdentifier)' == '' AND '$(PackAsTool)' != 'true'">$(NETCoreSdkRuntimeIdentifier)</RuntimeIdentifier>
1017
</PropertyGroup>
1118

1219
<ItemGroup>
13-
<PackageReference Include="CppAst" Version="0.7.3" />
14-
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.0.1" />
15-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" />
20+
<PackageReference Include="CppAst" Version="0.20.1" />
21+
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.9.2" />
22+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
1623
</ItemGroup>
1724

1825
<ItemGroup>
@@ -25,13 +32,17 @@
2532
<ItemGroup>
2633
<None Remove="SourceDefinitions\NativeInheritanceAttribute.cx" />
2734
<None Remove="SourceDefinitions\NativeTypeNameAttribute.cx" />
35+
<None Remove="SourceDefinitions\Page_INVALID.cx" />
36+
<None Remove="SourceDefinitions\PhysicalAddress_INVALID.cx" />
2837
<None Remove="SourceDefinitions\ProcessInfo.cx" />
2938
</ItemGroup>
3039

3140
<ItemGroup>
3241
<Content Include="SourceDefinitions\NativeInheritanceAttribute.cx" />
3342
<Content Include="SourceDefinitions\NativeTypeNameAttribute.cx" />
34-
<Content Include="SourceDefinitions\ProcessInfo.cx">
43+
<Content Include="SourceDefinitions\ProcessInfo.cx" />
44+
<Content Include="SourceDefinitions\PhysicalAddress_INVALID.cx" />
45+
<Content Include="SourceDefinitions\Page_INVALID.cx">
3546
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3647
<Visible>True</Visible>
3748
</Content>

CodeGenFix/Program.cs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static void Main(string[] args)
2020
Console.Title = "CodeGenFix";
2121

2222
// get interop file
23-
string filePath = Path.Combine(Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "..", "..", "..", "..", ".."), "memflow.NET", "memflow.NET"), "memflowInterop.cs");
23+
string filePath = Path.Combine(Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "..", "..", "..", "..", "..", ".."), "memflow.NET", "memflow.NET"), "memflowInterop.cs");
2424
if (args.Length == 1)
2525
filePath = args[0];
2626
Console.WriteLine($"Trying to parse '{filePath}'");
@@ -67,10 +67,10 @@ public static void Main(string[] args)
6767
// NativeTypeName and NativeTypeNameAttribute get inserted by ClangSharp but have to be manually defined
6868
case "CS0246":
6969
// find all usings directives
70-
var Usings = parsedNodes.Where(x => x.Kind() == SyntaxKind.UsingDirective);
70+
var Usings = parsedNodes.Where(x => x.IsKind(SyntaxKind.UsingDirective));
7171

7272
// find namespace line
73-
var NameSpace = parsedNodes.First(x => x.Kind() == SyntaxKind.NamespaceDeclaration);
73+
var NameSpace = parsedNodes.First(x => x.IsKind(SyntaxKind.NamespaceDeclaration));
7474
int line = NameSpace.GetLocation().GetLineSpan().StartLinePosition.Line + 2;
7575

7676
if (errMsg.Contains("NativeTypeNameAttribute") || errMsg.Contains("NativeTypeName"))
@@ -106,6 +106,7 @@ public static void Main(string[] args)
106106

107107
// Invalid expression term 'character'
108108
// Clangsharp breaks some regular '=' operators and generates '.operator=' directives
109+
// Clangsharp gets thrown off on some defined fields
109110
case "CS1001":
110111
case "CS1002":
111112
case "CS1513":
@@ -116,6 +117,25 @@ public static void Main(string[] args)
116117
case "CS1525":
117118
if (errLine.Contains(".operator="))
118119
newSrcTextLines[errLineNo] = errLine.Replace(".operator=", "=");
120+
else if (errLine.Contains(";") && (errLine.Contains("PhysicalAddress_INVALID") || errLine.Contains("Page_INVALID")))
121+
{
122+
if (errLine.Contains("PhysicalAddress_INVALID"))
123+
{
124+
// insert PhysicalAddress_INVALID definition
125+
List<string> def = File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "SourceDefinitions", "PhysicalAddress_INVALID.cx")).ToList();
126+
newSrcTextLines[errLineNo] = def[0];
127+
newSrcTextLines.InsertRange(errLineNo + 1, def[1..]);
128+
goto Recompile;
129+
}
130+
else
131+
{
132+
// insert Page_INVALID definition
133+
List<string> def = File.ReadAllLines(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "SourceDefinitions", "Page_INVALID.cx")).ToList();
134+
newSrcTextLines[errLineNo] = def[0];
135+
newSrcTextLines.InsertRange(errLineNo + 1, def[1..]);
136+
goto Recompile;
137+
}
138+
}
119139
else
120140
goto ThrowError;
121141
break;
@@ -194,7 +214,7 @@ public static void Main(string[] args)
194214
continue;
195215
FixStructs:
196216
// replace lazy typed structs with explicit ones
197-
var Structs = parsedNodes.Where(x => x.Kind() == SyntaxKind.StructDeclaration);
217+
var Structs = parsedNodes.Where(x => x.IsKind(SyntaxKind.StructDeclaration));
198218

199219
// ProcessInfo
200220
if (Structs.Any(x => x.ToString().Contains("struct ProcessInfo")))
@@ -224,36 +244,36 @@ public static void Main(string[] args)
224244
CppCompilation parsedHeader = CppParser.ParseFile(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "SourceDefinitions", "memflow.h"));
225245

226246
// enums
227-
var Enums = parsedNodes.Where(x => x.Kind() == SyntaxKind.EnumDeclaration);
247+
var Enums = parsedNodes.Where(x => x.IsKind(SyntaxKind.EnumDeclaration));
228248
foreach (CppEnum cDef in parsedHeader.Enums)
229249
{
230250
if (cDef.TypeKind == CppTypeKind.Enum && cDef.Comment != null)
231251
{
232252
// find C# definition
233-
SyntaxNode? csDef = Enums.FirstOrDefault(x => x.ChildTokens().FirstOrDefault(y => y.Kind() == SyntaxKind.IdentifierToken).Text.Equals(cDef.Name, StringComparison.OrdinalIgnoreCase));
253+
SyntaxNode? csDef = Enums.FirstOrDefault(x => x.ChildTokens().FirstOrDefault(y => y.IsKind(SyntaxKind.IdentifierToken)).Text.Equals(cDef.Name, StringComparison.OrdinalIgnoreCase));
234254
InsertCppComment(csDef, cDef.Comment, ref lineIndex, ref newSrcTextLines);
235255
}
236256
}
237257

238258
// structs
239-
Structs = parsedNodes.Where(x => x.Kind() == SyntaxKind.StructDeclaration);
259+
Structs = parsedNodes.Where(x => x.IsKind(SyntaxKind.StructDeclaration));
240260
foreach (CppClass cDef in parsedHeader.Classes)
241261
{
242262
if (cDef.TypeKind == CppTypeKind.StructOrClass && cDef.Comment != null)
243263
{
244264
// find C# definition
245-
SyntaxNode? csDef = Structs.FirstOrDefault(x => x.ChildTokens().FirstOrDefault(y => y.Kind() == SyntaxKind.IdentifierToken).Text.Equals(cDef.Name, StringComparison.OrdinalIgnoreCase));
265+
SyntaxNode? csDef = Structs.FirstOrDefault(x => x.ChildTokens().FirstOrDefault(y => y.IsKind(SyntaxKind.IdentifierToken)).Text.Equals(cDef.Name, StringComparison.OrdinalIgnoreCase));
246266
InsertCppComment(csDef, cDef.Comment, ref lineIndex, ref newSrcTextLines);
247267
}
248268
}
249269

250270
// functions
251-
var Classes = parsedNodes.Where(x => x.Kind() == SyntaxKind.ClassDeclaration);
252-
var Functions = Classes.First(x => x.ChildTokens().FirstOrDefault(y => y.Kind() == SyntaxKind.IdentifierToken).Text.Equals("Methods", StringComparison.OrdinalIgnoreCase)).ChildNodes();
271+
var Classes = parsedNodes.Where(x => x.IsKind(SyntaxKind.ClassDeclaration));
272+
var Functions = Classes.First(x => x.ChildTokens().FirstOrDefault(y => y.IsKind(SyntaxKind.IdentifierToken)).Text.Equals("Methods", StringComparison.OrdinalIgnoreCase)).ChildNodes();
253273
foreach (CppFunction cDef in parsedHeader.Functions)
254274
{
255275
// find C# field definition in Methods struct
256-
SyntaxNode? csDef = Functions.FirstOrDefault(x => x.ChildTokens().FirstOrDefault(y => y.Kind() == SyntaxKind.IdentifierToken).Text.Equals(cDef.Name, StringComparison.OrdinalIgnoreCase));
276+
SyntaxNode? csDef = Functions.FirstOrDefault(x => x.ChildTokens().FirstOrDefault(y => y.IsKind(SyntaxKind.IdentifierToken)).Text.Equals(cDef.Name, StringComparison.OrdinalIgnoreCase));
257277
InsertCppComment(csDef, cDef.Comment, ref lineIndex, ref newSrcTextLines, 2);
258278
}
259279

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public static readonly Page Page_INVALID = new Page()
2+
{
3+
page_type = PageType_UNKNOWN,
4+
page_base = ulong.MaxValue,
5+
page_size = 0
6+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public static readonly PhysicalAddress PhysicalAddress_INVALID = new PhysicalAddress()
2+
{
3+
address = ulong.MaxValue,
4+
page_type = PageType_UNKNOWN,
5+
page_size_log2 = 0
6+
};

CodeGenFix/SourceDefinitions/ProcessInfo.cx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[StructLayout(LayoutKind.Explicit, Size = 56)]
1+
[StructLayout(LayoutKind.Explicit, Size = 72)]
22
public unsafe partial struct ProcessInfo
33
{
44
[FieldOffset(0)]
@@ -32,4 +32,12 @@
3232
[FieldOffset(52)]
3333
[NativeTypeName("struct ArchitectureIdent")]
3434
public ArchitectureIdent proc_arch;
35+
36+
[FieldOffset(56)]
37+
[NativeTypeName("Address")]
38+
public ulong dtb1;
39+
40+
[FieldOffset(64)]
41+
[NativeTypeName("Address")]
42+
public ulong dtb2;
3543
}

0 commit comments

Comments
 (0)