Skip to content

Commit 1ee8287

Browse files
authored
Fix wrong calculation of native methods checksum (#81)
***UPDATE_DEPENDENTS***
1 parent fcb169e commit 1ee8287

File tree

4 files changed

+33
-33
lines changed

4 files changed

+33
-33
lines changed

source/MetadataProcessor.Core/Utility/NativeMethodsCrc.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public sealed class NativeMethodsCrc
2525

2626
private readonly List<string> _classNamesToExclude;
2727

28+
private int _methodsWithNativeImplementation = 0;
29+
private uint _currentCrc = 0;
30+
2831
public NativeMethodsCrc(
2932
AssemblyDefinition assembly,
3033
List<string> classNamesToExclude)
@@ -33,7 +36,24 @@ public NativeMethodsCrc(
3336
_classNamesToExclude = classNamesToExclude;
3437
}
3538

36-
public uint Current { get; private set; }
39+
/// <summary>
40+
/// Current CRC32 of the native methods.
41+
/// Will return 0 if there are no methods with native implementation.
42+
/// </summary>
43+
public uint CurrentCrc
44+
{
45+
get
46+
{
47+
if(_methodsWithNativeImplementation > 0)
48+
{
49+
return _currentCrc;
50+
}
51+
else
52+
{
53+
return 0;
54+
}
55+
}
56+
}
3757

3858
public void UpdateCrc(MethodDefinition method)
3959
{
@@ -42,13 +62,15 @@ public void UpdateCrc(MethodDefinition method)
4262
if (type.IncludeInStub() &&
4363
(method.RVA == 0 && !method.IsAbstract) )
4464
{
45-
Current = Crc32.Compute(_name, Current);
46-
Current = Crc32.Compute(Encoding.ASCII.GetBytes(GetClassName(type)), Current);
47-
Current = Crc32.Compute(Encoding.ASCII.GetBytes(GetMethodName(method)), Current);
65+
_currentCrc = Crc32.Compute(_name, CurrentCrc);
66+
_currentCrc = Crc32.Compute(Encoding.ASCII.GetBytes(GetClassName(type)), CurrentCrc);
67+
_currentCrc = Crc32.Compute(Encoding.ASCII.GetBytes(GetMethodName(method)), CurrentCrc);
68+
69+
_methodsWithNativeImplementation++;
4870
}
4971
else
5072
{
51-
Current = Crc32.Compute(_null, Current);
73+
_currentCrc = Crc32.Compute(_null, CurrentCrc);
5274
}
5375
}
5476

source/MetadataProcessor.Core/nanoAssemblyBuilder.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,7 @@ public string GetNativeChecksum()
267267
{
268268
if(_tablesContext.MinimizeComplete)
269269
{
270-
// check if there are any native methods
271-
if (_tablesContext.MethodDefinitionTable.Items.Any(method => method.RVA == 0 && !method.IsAbstract))
272-
{
273-
return $"0x{_tablesContext.NativeMethodsCrc.Current.ToString("X")}";
274-
}
275-
else
276-
{
277-
// this assembly doesn't have native implementation
278-
return "0x00000000";
279-
}
270+
return $"0x{_tablesContext.NativeMethodsCrc.CurrentCrc.ToString("X8")}";
280271
}
281272
else
282273
{

source/MetadataProcessor.Core/nanoAssemblyDefinition.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,8 @@ public void Write(
8989
// current builds are for little endian targets only
9090
// keeping this here for now, just for compatibility
9191
writer.WriteUInt32(0);
92-
93-
var nativeMethdodsCount = _context.MethodDefinitionTable.Items.Count(method => method.RVA == 0 && !method.IsAbstract);
94-
95-
// native methods CRC32
96-
// this is used by other tools to check if the assembly has native implementation
97-
// (like the deployment provider in the VS extension)
98-
if (nativeMethdodsCount > 0)
99-
{
100-
writer.WriteUInt32(_context.NativeMethodsCrc.Current);
101-
}
102-
else
103-
{
104-
// this assembly doesn't have any native methods implemented
105-
writer.WriteUInt32(0);
106-
}
92+
93+
writer.WriteUInt32(_context.NativeMethodsCrc.CurrentCrc);
10794

10895
// Native methods offset
10996
writer.WriteUInt32(0xFFFFFFFF);

source/MetadataProcessor.Core/nanoSkeletonGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public nanoSkeletonGenerator(
5050
public void GenerateSkeleton()
5151
{
5252
// check if there are any native methods
53-
if(_tablesContext.MethodDefinitionTable.Items.Any(method => method.RVA == 0 && !method.IsAbstract))
53+
if(_tablesContext.NativeMethodsCrc.CurrentCrc > 0)
5454
{
5555
// create <assembly>.h with the structs declarations
5656
GenerateAssemblyHeader();
@@ -63,7 +63,7 @@ public void GenerateSkeleton()
6363

6464
// output native checksum so it shows in build log
6565
Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++");
66-
Console.WriteLine($"+ Native declaration checksum: 0x{_tablesContext.NativeMethodsCrc.Current.ToString("X")} +");
66+
Console.WriteLine($"+ Native declaration checksum: 0x{_tablesContext.NativeMethodsCrc.CurrentCrc.ToString("X8")} +");
6767
Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++");
6868
}
6969
else
@@ -327,7 +327,7 @@ private void GenerateAssemblyLookup()
327327
AssemblyName = _tablesContext.AssemblyDefinition.Name.Name,
328328
HeaderFileName = _safeProjectName,
329329
NativeVersion = nativeVersion,
330-
NativeCRC32 = "0x" + _tablesContext.NativeMethodsCrc.Current.ToString("X")
330+
NativeCRC32 = "0x" + _tablesContext.NativeMethodsCrc.CurrentCrc.ToString("X8")
331331
};
332332

333333

0 commit comments

Comments
 (0)