From ca0e3711147cabe72e57e64f0a095eef1bd3f9a1 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Thu, 8 Oct 2026 17:01:52 +0200 Subject: [PATCH] [build] Remove unsafe managed-memory access from hashing Use bounded managed reads and flat table indexing in both legacy CRC64 helpers, retaining all table constants, the CRC-64-Jones algorithm and HashAlgorithm wrappers. Validate input ranges before changing state. Use bounded little-endian operations for Files length XOR and span-based UTF8 encoding in the trimmable typemap scanner. Remove unsafe permission only from the five tooling projects whose evaluated inputs are now safe. Preserve package references, framework targets and task-hosting behavior. This addresses the tooling portion of issue #11467 step 2. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- ....Interop.Tools.JavaCallableWrappers.csproj | 1 - .../Crc64.Table.cs | 32 +++--------- .../Crc64.cs | 2 +- .../Crc64Helper.cs | 49 ++++++++++--------- ...Java.Interop.Tools.TypeNameMappings.csproj | 1 - .../Crc64.Table.cs | 32 +++--------- .../Crc64.cs | 2 +- .../Crc64Helper.cs | 49 ++++++++++--------- .../Files.cs | 6 +-- .../Microsoft.Android.Build.BaseTasks.csproj | 1 - .../Microsoft.Android.Sdk.ILLink.csproj | 1 - ...rosoft.Android.Sdk.TrimmableTypeMap.csproj | 1 - .../Scanner/ScannerHashingHelper.cs | 15 ++---- 13 files changed, 77 insertions(+), 115 deletions(-) diff --git a/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj b/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj index 1d177f3dd19..54bb308d4d3 100644 --- a/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj +++ b/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers.csproj @@ -3,7 +3,6 @@ netstandard2.0 enable - true true true $(ProductSnkPath) diff --git a/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.Table.cs b/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.Table.cs index c82097ed5b0..ec2a32ebec1 100644 --- a/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.Table.cs +++ b/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.Table.cs @@ -2,8 +2,7 @@ namespace Java.Interop.Tools.JavaCallableWrappers { partial class Crc64Helper { - static readonly ulong[,] table = { - { + static readonly ulong [] table = { 0x0000000000000000, 0x7ad870c830358979, 0xf5b0e190606b12f2, 0x8f689158505e9b8b, 0xc038e5739841b68f, 0xbae095bba8743ff6, 0x358804e3f82aa47d, 0x4f50742bc81f2d04, 0xab28ecb46814fe75, 0xd1f09c7c5821770c, 0x5e980d24087fec87, 0x24407dec384a65fe, @@ -67,9 +66,7 @@ partial class Crc64Helper 0xcdcf48d84fe75459, 0xb71738107fd2dd20, 0x387fa9482f8c46ab, 0x42a7d9801fb9cfd2, 0x0df7adabd7a6e2d6, 0x772fdd63e7936baf, 0xf8474c3bb7cdf024, 0x829f3cf387f8795d, 0x66e7a46c27f3aa2c, 0x1c3fd4a417c62355, 0x935745fc4798b8de, 0xe98f353477ad31a7, - 0xa6df411fbfb21ca3, 0xdc0731d78f8795da, 0x536fa08fdfd90e51, 0x29b7d047efec8728 - }, - { + 0xa6df411fbfb21ca3, 0xdc0731d78f8795da, 0x536fa08fdfd90e51, 0x29b7d047efec8728, 0x0000000000000000, 0x89e99ffd73bddf69, 0x388a19a9bfec2db9, 0xb1638654cc51f2d0, 0x711433537fd85b72, 0xf8fdacae0c65841b, 0x499e2afac03476cb, 0xc077b507b389a9a2, 0xe22866a6ffb0b6e4, 0x6bc1f95b8c0d698d, 0xdaa27f0f405c9b5d, 0x534be0f233e14434, @@ -133,9 +130,7 @@ partial class Crc64Helper 0x734f76b272bed2b6, 0xfaa6e94f01030ddf, 0x4bc56f1bcd52ff0f, 0xc22cf0e6beef2066, 0x025b45e10d6689c4, 0x8bb2da1c7edb56ad, 0x3ad15c48b28aa47d, 0xb338c3b5c1377b14, 0x916710148d0e6452, 0x188e8fe9feb3bb3b, 0xa9ed09bd32e249eb, 0x20049640415f9682, - 0xe0732347f2d63f20, 0x699abcba816be049, 0xd8f93aee4d3a1299, 0x5110a5133e87cdf0 - }, - { + 0xe0732347f2d63f20, 0x699abcba816be049, 0xd8f93aee4d3a1299, 0x5110a5133e87cdf0, 0x0000000000000000, 0xf4125129ce4038be, 0xc37d8400c417e217, 0x376fd5290a57daa9, 0xada22e52d0b85745, 0x59b07f7b1ef86ffb, 0x6edfaa5214afb552, 0x9acdfb7bdaef8dec, 0x701d7af6f9e73de1, 0x840f2bdf37a7055f, 0xb360fef63df0dff6, 0x4772afdff3b0e748, @@ -199,9 +194,7 @@ partial class Crc64Helper 0x274ed9f07fd16399, 0xd35c88d9b1915b27, 0xe4335df0bbc6818e, 0x10210cd97586b930, 0x8aecf7a2af6934dc, 0x7efea68b61290c62, 0x499173a26b7ed6cb, 0xbd83228ba53eee75, 0x5753a30686365e78, 0xa341f22f487666c6, 0x942e27064221bc6f, 0x603c762f8c6184d1, - 0xfaf18d54568e093d, 0x0ee3dc7d98ce3183, 0x398c09549299eb2a, 0xcd9e587d5cd9d394 - }, - { + 0xfaf18d54568e093d, 0x0ee3dc7d98ce3183, 0x398c09549299eb2a, 0xcd9e587d5cd9d394, 0x0000000000000000, 0x8ce168638c796306, 0x329bf69440655567, 0xbe7a9ef7cc1c3661, 0x6537ed2880caaace, 0xe9d6854b0cb3c9c8, 0x57ac1bbcc0afffa9, 0xdb4d73df4cd69caf, 0xca6fda510195559c, 0x468eb2328dec369a, 0xf8f42cc541f000fb, 0x741544a6cd8963fd, @@ -265,9 +258,7 @@ partial class Crc64Helper 0x3973beb78cab005b, 0xb592d6d400d2635d, 0x0be84823ccce553c, 0x8709204040b7363a, 0x5c44539f0c61aa95, 0xd0a53bfc8018c993, 0x6edfa50b4c04fff2, 0xe23ecd68c07d9cf4, 0xf31c64e68d3e55c7, 0x7ffd0c85014736c1, 0xc1879272cd5b00a0, 0x4d66fa11412263a6, - 0x962b89ce0df4ff09, 0x1acae1ad818d9c0f, 0xa4b07f5a4d91aa6e, 0x28511739c1e8c968 - }, - { + 0x962b89ce0df4ff09, 0x1acae1ad818d9c0f, 0xa4b07f5a4d91aa6e, 0x28511739c1e8c968, 0x0000000000000000, 0x3504e58b9ba6dd1e, 0x6a09cb17374dba3c, 0x5f0d2e9caceb6722, 0xd413962e6e9b7478, 0xe11773a5f53da966, 0xbe1a5d3959d6ce44, 0x8b1eb8b2c270135a, 0x837e0a0f85a17b9b, 0xb67aef841e07a685, 0xe977c118b2ecc1a7, 0xdc732493294a1cb9, @@ -331,9 +322,7 @@ partial class Crc64Helper 0x860b2bd47d168c10, 0xb30fce5fe6b0510e, 0xec02e0c34a5b362c, 0xd9060548d1fdeb32, 0x5218bdfa138df868, 0x671c5871882b2576, 0x381176ed24c04254, 0x0d159366bf669f4a, 0x057521dbf8b7f78b, 0x3071c45063112a95, 0x6f7ceacccffa4db7, 0x5a780f47545c90a9, - 0xd166b7f5962c83f3, 0xe462527e0d8a5eed, 0xbb6f7ce2a16139cf, 0x8e6b99693ac7e4d1 - }, - { + 0xd166b7f5962c83f3, 0xe462527e0d8a5eed, 0xbb6f7ce2a16139cf, 0x8e6b99693ac7e4d1, 0x0000000000000000, 0xe39d1389931b9354, 0xec6301407ea0b5c3, 0x0ffe12c9edbb2697, 0xf39f24d3a5d6f8ed, 0x1002375a36cd6bb9, 0x1ffc2593db764d2e, 0xfc61361a486dde7a, 0xcc676ff4133a62b1, 0x2ffa7c7d8021f1e5, 0x20046eb46d9ad772, 0xc3997d3dfe814426, @@ -397,9 +386,7 @@ partial class Crc64Helper 0x7d8ef4105cc3790d, 0x9e13e799cfd8ea59, 0x91edf5502263ccce, 0x7270e6d9b1785f9a, 0x8e11d0c3f91581e0, 0x6d8cc34a6a0e12b4, 0x6272d18387b53423, 0x81efc20a14aea777, 0xb1e99be44ff91bbc, 0x5274886ddce288e8, 0x5d8a9aa43159ae7f, 0xbe17892da2423d2b, - 0x4276bf37ea2fe351, 0xa1ebacbe79347005, 0xae15be77948f5692, 0x4d88adfe0794c5c6 - }, - { + 0x4276bf37ea2fe351, 0xa1ebacbe79347005, 0xae15be77948f5692, 0x4d88adfe0794c5c6, 0x0000000000000000, 0x62a95de6e302eff2, 0xc552bbcdc605dfe4, 0xa7fbe62b25073016, 0xa1fc51c8d49c2ca3, 0xc3550c2e379ec351, 0x64aeea051299f347, 0x0607b7e3f19b1cb5, 0x68a185c2f1afca2d, 0x0a08d82412ad25df, 0xadf33e0f37aa15c9, 0xcf5a63e9d4a8fa3b, @@ -463,9 +450,7 @@ partial class Crc64Helper 0x11b5f7fbd03c02fa, 0x731caa1d333eed08, 0xd4e74c361639dd1e, 0xb64e11d0f53b32ec, 0xb049a63304a02e59, 0xd2e0fbd5e7a2c1ab, 0x751b1dfec2a5f1bd, 0x17b2401821a71e4f, 0x791472392193c8d7, 0x1bbd2fdfc2912725, 0xbc46c9f4e7961733, 0xdeef94120494f8c1, - 0xd8e823f1f50fe474, 0xba417e17160d0b86, 0x1dba983c330a3b90, 0x7f13c5dad008d462 - }, - { + 0xd8e823f1f50fe474, 0xba417e17160d0b86, 0x1dba983c330a3b90, 0x7f13c5dad008d462, 0x0000000000000000, 0x381d0015c96f4444, 0x703a002b92de8888, 0x4827003e5bb1cccc, 0xe074005725bd1110, 0xd8690042ecd25554, 0x904e007cb7639998, 0xa85300697e0cdddc, 0xebb126fd13edb14b, 0xd3ac26e8da82f50f, 0x9b8b26d6813339c3, 0xa39626c3485c7d87, @@ -530,7 +515,6 @@ partial class Crc64Helper 0x7332f05c99f595cc, 0x4b2ff049509ad188, 0x0308f0770b2b1d44, 0x3b15f062c2445900, 0x78f7d6f6afa53597, 0x40ead6e366ca71d3, 0x08cdd6dd3d7bbd1f, 0x30d0d6c8f414f95b, 0x9883d6a18a182487, 0xa09ed6b4437760c3, 0xe8b9d68a18c6ac0f, 0xd0a4d69fd1a9e84b - }, }; } } diff --git a/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.cs b/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.cs index f3dabdc2f71..823a434acc4 100644 --- a/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.cs +++ b/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64.cs @@ -53,7 +53,7 @@ public override void Initialize () length = 0; } - protected override unsafe void HashCore (byte [] array, int ibStart, int cbSize) + protected override void HashCore (byte [] array, int ibStart, int cbSize) { Crc64Helper.HashCore (array, ibStart, cbSize, ref crc, ref length); } diff --git a/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64Helper.cs b/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64Helper.cs index 82ab2a90bc8..a0abd5dfcdf 100644 --- a/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64Helper.cs +++ b/external/Java.Interop/src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/Crc64Helper.cs @@ -55,34 +55,37 @@ internal static byte [] Compute (byte [] array) return BitConverter.GetBytes (crc ^ length); } - internal static unsafe void HashCore (byte [] array, int ibStart, int cbSize, ref ulong crc, ref ulong length) + internal static void HashCore (byte [] array, int ibStart, int cbSize, ref ulong crc, ref ulong length) { + if (array == null) + throw new ArgumentNullException (nameof (array)); + if (ibStart < 0 || ibStart > array.Length) + throw new ArgumentOutOfRangeException (nameof (ibStart)); + if (cbSize < 0 || cbSize > array.Length - ibStart) + throw new ArgumentOutOfRangeException (nameof (cbSize)); + int len = cbSize; int idx = ibStart; - fixed (ulong* tptr = table) { - fixed (byte* aptr = array) { - while (len >= 8) { - crc ^= *((ulong*) (aptr + idx)); - crc = - tptr [7 * 256 + (crc & 0xff)] ^ - tptr [6 * 256 + ((crc >> 8) & 0xff)] ^ - tptr [5 * 256 + ((crc >> 16) & 0xff)] ^ - tptr [4 * 256 + ((crc >> 24) & 0xff)] ^ - tptr [3 * 256 + ((crc >> 32) & 0xff)] ^ - tptr [2 * 256 + ((crc >> 40) & 0xff)] ^ - tptr [1 * 256 + ((crc >> 48) & 0xff)] ^ - tptr [0 * 256 + (crc >> 56)]; - idx += 8; - len -= 8; - } + while (len >= 8) { + crc ^= BitConverter.ToUInt64 (array, idx); + crc = + table [7 * 256 + (crc & 0xff)] ^ + table [6 * 256 + ((crc >> 8) & 0xff)] ^ + table [5 * 256 + ((crc >> 16) & 0xff)] ^ + table [4 * 256 + ((crc >> 24) & 0xff)] ^ + table [3 * 256 + ((crc >> 32) & 0xff)] ^ + table [2 * 256 + ((crc >> 40) & 0xff)] ^ + table [1 * 256 + ((crc >> 48) & 0xff)] ^ + table [0 * 256 + (crc >> 56)]; + idx += 8; + len -= 8; + } - while (len > 0) { - crc = tptr [0 * 256 + ((crc ^ aptr [idx]) & 0xff)] ^ (crc >> 8); - idx++; - len--; - } - } + while (len > 0) { + crc = table [0 * 256 + ((crc ^ array [idx]) & 0xff)] ^ (crc >> 8); + idx++; + len--; } length += (ulong) cbSize; diff --git a/external/Java.Interop/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings.csproj b/external/Java.Interop/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings.csproj index b5a852d6508..ddd1bff1db6 100644 --- a/external/Java.Interop/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings.csproj +++ b/external/Java.Interop/src/Java.Interop.Tools.TypeNameMappings/Java.Interop.Tools.TypeNameMappings.csproj @@ -2,7 +2,6 @@ $(DotNetTargetFramework) enable - true $(DefineConstants);JCW_ONLY_TYPE_NAMES;HAVE_CECIL diff --git a/src/Microsoft.Android.Build.BaseTasks/Crc64.Table.cs b/src/Microsoft.Android.Build.BaseTasks/Crc64.Table.cs index 20f94499bbf..46c1cbdf6b0 100644 --- a/src/Microsoft.Android.Build.BaseTasks/Crc64.Table.cs +++ b/src/Microsoft.Android.Build.BaseTasks/Crc64.Table.cs @@ -4,8 +4,7 @@ namespace Microsoft.Android.Build.Tasks { partial class Crc64Helper { - static readonly ulong[,] table = { - { + static readonly ulong [] table = { 0x0000000000000000, 0x7ad870c830358979, 0xf5b0e190606b12f2, 0x8f689158505e9b8b, 0xc038e5739841b68f, 0xbae095bba8743ff6, 0x358804e3f82aa47d, 0x4f50742bc81f2d04, 0xab28ecb46814fe75, 0xd1f09c7c5821770c, 0x5e980d24087fec87, 0x24407dec384a65fe, @@ -69,9 +68,7 @@ partial class Crc64Helper 0xcdcf48d84fe75459, 0xb71738107fd2dd20, 0x387fa9482f8c46ab, 0x42a7d9801fb9cfd2, 0x0df7adabd7a6e2d6, 0x772fdd63e7936baf, 0xf8474c3bb7cdf024, 0x829f3cf387f8795d, 0x66e7a46c27f3aa2c, 0x1c3fd4a417c62355, 0x935745fc4798b8de, 0xe98f353477ad31a7, - 0xa6df411fbfb21ca3, 0xdc0731d78f8795da, 0x536fa08fdfd90e51, 0x29b7d047efec8728 - }, - { + 0xa6df411fbfb21ca3, 0xdc0731d78f8795da, 0x536fa08fdfd90e51, 0x29b7d047efec8728, 0x0000000000000000, 0x89e99ffd73bddf69, 0x388a19a9bfec2db9, 0xb1638654cc51f2d0, 0x711433537fd85b72, 0xf8fdacae0c65841b, 0x499e2afac03476cb, 0xc077b507b389a9a2, 0xe22866a6ffb0b6e4, 0x6bc1f95b8c0d698d, 0xdaa27f0f405c9b5d, 0x534be0f233e14434, @@ -135,9 +132,7 @@ partial class Crc64Helper 0x734f76b272bed2b6, 0xfaa6e94f01030ddf, 0x4bc56f1bcd52ff0f, 0xc22cf0e6beef2066, 0x025b45e10d6689c4, 0x8bb2da1c7edb56ad, 0x3ad15c48b28aa47d, 0xb338c3b5c1377b14, 0x916710148d0e6452, 0x188e8fe9feb3bb3b, 0xa9ed09bd32e249eb, 0x20049640415f9682, - 0xe0732347f2d63f20, 0x699abcba816be049, 0xd8f93aee4d3a1299, 0x5110a5133e87cdf0 - }, - { + 0xe0732347f2d63f20, 0x699abcba816be049, 0xd8f93aee4d3a1299, 0x5110a5133e87cdf0, 0x0000000000000000, 0xf4125129ce4038be, 0xc37d8400c417e217, 0x376fd5290a57daa9, 0xada22e52d0b85745, 0x59b07f7b1ef86ffb, 0x6edfaa5214afb552, 0x9acdfb7bdaef8dec, 0x701d7af6f9e73de1, 0x840f2bdf37a7055f, 0xb360fef63df0dff6, 0x4772afdff3b0e748, @@ -201,9 +196,7 @@ partial class Crc64Helper 0x274ed9f07fd16399, 0xd35c88d9b1915b27, 0xe4335df0bbc6818e, 0x10210cd97586b930, 0x8aecf7a2af6934dc, 0x7efea68b61290c62, 0x499173a26b7ed6cb, 0xbd83228ba53eee75, 0x5753a30686365e78, 0xa341f22f487666c6, 0x942e27064221bc6f, 0x603c762f8c6184d1, - 0xfaf18d54568e093d, 0x0ee3dc7d98ce3183, 0x398c09549299eb2a, 0xcd9e587d5cd9d394 - }, - { + 0xfaf18d54568e093d, 0x0ee3dc7d98ce3183, 0x398c09549299eb2a, 0xcd9e587d5cd9d394, 0x0000000000000000, 0x8ce168638c796306, 0x329bf69440655567, 0xbe7a9ef7cc1c3661, 0x6537ed2880caaace, 0xe9d6854b0cb3c9c8, 0x57ac1bbcc0afffa9, 0xdb4d73df4cd69caf, 0xca6fda510195559c, 0x468eb2328dec369a, 0xf8f42cc541f000fb, 0x741544a6cd8963fd, @@ -267,9 +260,7 @@ partial class Crc64Helper 0x3973beb78cab005b, 0xb592d6d400d2635d, 0x0be84823ccce553c, 0x8709204040b7363a, 0x5c44539f0c61aa95, 0xd0a53bfc8018c993, 0x6edfa50b4c04fff2, 0xe23ecd68c07d9cf4, 0xf31c64e68d3e55c7, 0x7ffd0c85014736c1, 0xc1879272cd5b00a0, 0x4d66fa11412263a6, - 0x962b89ce0df4ff09, 0x1acae1ad818d9c0f, 0xa4b07f5a4d91aa6e, 0x28511739c1e8c968 - }, - { + 0x962b89ce0df4ff09, 0x1acae1ad818d9c0f, 0xa4b07f5a4d91aa6e, 0x28511739c1e8c968, 0x0000000000000000, 0x3504e58b9ba6dd1e, 0x6a09cb17374dba3c, 0x5f0d2e9caceb6722, 0xd413962e6e9b7478, 0xe11773a5f53da966, 0xbe1a5d3959d6ce44, 0x8b1eb8b2c270135a, 0x837e0a0f85a17b9b, 0xb67aef841e07a685, 0xe977c118b2ecc1a7, 0xdc732493294a1cb9, @@ -333,9 +324,7 @@ partial class Crc64Helper 0x860b2bd47d168c10, 0xb30fce5fe6b0510e, 0xec02e0c34a5b362c, 0xd9060548d1fdeb32, 0x5218bdfa138df868, 0x671c5871882b2576, 0x381176ed24c04254, 0x0d159366bf669f4a, 0x057521dbf8b7f78b, 0x3071c45063112a95, 0x6f7ceacccffa4db7, 0x5a780f47545c90a9, - 0xd166b7f5962c83f3, 0xe462527e0d8a5eed, 0xbb6f7ce2a16139cf, 0x8e6b99693ac7e4d1 - }, - { + 0xd166b7f5962c83f3, 0xe462527e0d8a5eed, 0xbb6f7ce2a16139cf, 0x8e6b99693ac7e4d1, 0x0000000000000000, 0xe39d1389931b9354, 0xec6301407ea0b5c3, 0x0ffe12c9edbb2697, 0xf39f24d3a5d6f8ed, 0x1002375a36cd6bb9, 0x1ffc2593db764d2e, 0xfc61361a486dde7a, 0xcc676ff4133a62b1, 0x2ffa7c7d8021f1e5, 0x20046eb46d9ad772, 0xc3997d3dfe814426, @@ -399,9 +388,7 @@ partial class Crc64Helper 0x7d8ef4105cc3790d, 0x9e13e799cfd8ea59, 0x91edf5502263ccce, 0x7270e6d9b1785f9a, 0x8e11d0c3f91581e0, 0x6d8cc34a6a0e12b4, 0x6272d18387b53423, 0x81efc20a14aea777, 0xb1e99be44ff91bbc, 0x5274886ddce288e8, 0x5d8a9aa43159ae7f, 0xbe17892da2423d2b, - 0x4276bf37ea2fe351, 0xa1ebacbe79347005, 0xae15be77948f5692, 0x4d88adfe0794c5c6 - }, - { + 0x4276bf37ea2fe351, 0xa1ebacbe79347005, 0xae15be77948f5692, 0x4d88adfe0794c5c6, 0x0000000000000000, 0x62a95de6e302eff2, 0xc552bbcdc605dfe4, 0xa7fbe62b25073016, 0xa1fc51c8d49c2ca3, 0xc3550c2e379ec351, 0x64aeea051299f347, 0x0607b7e3f19b1cb5, 0x68a185c2f1afca2d, 0x0a08d82412ad25df, 0xadf33e0f37aa15c9, 0xcf5a63e9d4a8fa3b, @@ -465,9 +452,7 @@ partial class Crc64Helper 0x11b5f7fbd03c02fa, 0x731caa1d333eed08, 0xd4e74c361639dd1e, 0xb64e11d0f53b32ec, 0xb049a63304a02e59, 0xd2e0fbd5e7a2c1ab, 0x751b1dfec2a5f1bd, 0x17b2401821a71e4f, 0x791472392193c8d7, 0x1bbd2fdfc2912725, 0xbc46c9f4e7961733, 0xdeef94120494f8c1, - 0xd8e823f1f50fe474, 0xba417e17160d0b86, 0x1dba983c330a3b90, 0x7f13c5dad008d462 - }, - { + 0xd8e823f1f50fe474, 0xba417e17160d0b86, 0x1dba983c330a3b90, 0x7f13c5dad008d462, 0x0000000000000000, 0x381d0015c96f4444, 0x703a002b92de8888, 0x4827003e5bb1cccc, 0xe074005725bd1110, 0xd8690042ecd25554, 0x904e007cb7639998, 0xa85300697e0cdddc, 0xebb126fd13edb14b, 0xd3ac26e8da82f50f, 0x9b8b26d6813339c3, 0xa39626c3485c7d87, @@ -532,7 +517,6 @@ partial class Crc64Helper 0x7332f05c99f595cc, 0x4b2ff049509ad188, 0x0308f0770b2b1d44, 0x3b15f062c2445900, 0x78f7d6f6afa53597, 0x40ead6e366ca71d3, 0x08cdd6dd3d7bbd1f, 0x30d0d6c8f414f95b, 0x9883d6a18a182487, 0xa09ed6b4437760c3, 0xe8b9d68a18c6ac0f, 0xd0a4d69fd1a9e84b - }, }; } } diff --git a/src/Microsoft.Android.Build.BaseTasks/Crc64.cs b/src/Microsoft.Android.Build.BaseTasks/Crc64.cs index 84dd209dbfb..0973ba79420 100644 --- a/src/Microsoft.Android.Build.BaseTasks/Crc64.cs +++ b/src/Microsoft.Android.Build.BaseTasks/Crc64.cs @@ -55,7 +55,7 @@ public override void Initialize () length = 0; } - protected override unsafe void HashCore (byte [] array, int ibStart, int cbSize) + protected override void HashCore (byte [] array, int ibStart, int cbSize) { Crc64Helper.HashCore (array, ibStart, cbSize, ref crc, ref length); } diff --git a/src/Microsoft.Android.Build.BaseTasks/Crc64Helper.cs b/src/Microsoft.Android.Build.BaseTasks/Crc64Helper.cs index decab0d019e..728125308f5 100644 --- a/src/Microsoft.Android.Build.BaseTasks/Crc64Helper.cs +++ b/src/Microsoft.Android.Build.BaseTasks/Crc64Helper.cs @@ -57,34 +57,37 @@ internal static byte [] Compute (byte [] array) return BitConverter.GetBytes (crc ^ length); } - internal static unsafe void HashCore (byte [] array, int ibStart, int cbSize, ref ulong crc, ref ulong length) + internal static void HashCore (byte [] array, int ibStart, int cbSize, ref ulong crc, ref ulong length) { + if (array == null) + throw new ArgumentNullException (nameof (array)); + if (ibStart < 0 || ibStart > array.Length) + throw new ArgumentOutOfRangeException (nameof (ibStart)); + if (cbSize < 0 || cbSize > array.Length - ibStart) + throw new ArgumentOutOfRangeException (nameof (cbSize)); + int len = cbSize; int idx = ibStart; - fixed (ulong* tptr = table) { - fixed (byte* aptr = array) { - while (len >= 8) { - crc ^= *((ulong*) (aptr + idx)); - crc = - tptr [7 * 256 + (crc & 0xff)] ^ - tptr [6 * 256 + ((crc >> 8) & 0xff)] ^ - tptr [5 * 256 + ((crc >> 16) & 0xff)] ^ - tptr [4 * 256 + ((crc >> 24) & 0xff)] ^ - tptr [3 * 256 + ((crc >> 32) & 0xff)] ^ - tptr [2 * 256 + ((crc >> 40) & 0xff)] ^ - tptr [1 * 256 + ((crc >> 48) & 0xff)] ^ - tptr [0 * 256 + (crc >> 56)]; - idx += 8; - len -= 8; - } + while (len >= 8) { + crc ^= BitConverter.ToUInt64 (array, idx); + crc = + table [7 * 256 + (crc & 0xff)] ^ + table [6 * 256 + ((crc >> 8) & 0xff)] ^ + table [5 * 256 + ((crc >> 16) & 0xff)] ^ + table [4 * 256 + ((crc >> 24) & 0xff)] ^ + table [3 * 256 + ((crc >> 32) & 0xff)] ^ + table [2 * 256 + ((crc >> 40) & 0xff)] ^ + table [1 * 256 + ((crc >> 48) & 0xff)] ^ + table [0 * 256 + (crc >> 56)]; + idx += 8; + len -= 8; + } - while (len > 0) { - crc = tptr [0 * 256 + ((crc ^ aptr [idx]) & 0xff)] ^ (crc >> 8); - idx++; - len--; - } - } + while (len > 0) { + crc = table [0 * 256 + ((crc ^ array [idx]) & 0xff)] ^ (crc >> 8); + idx++; + len--; } length += (ulong) cbSize; diff --git a/src/Microsoft.Android.Build.BaseTasks/Files.cs b/src/Microsoft.Android.Build.BaseTasks/Files.cs index 710ae254e62..f6bfe2bbf83 100644 --- a/src/Microsoft.Android.Build.BaseTasks/Files.cs +++ b/src/Microsoft.Android.Build.BaseTasks/Files.cs @@ -2,12 +2,12 @@ // https://github.com/dotnet/android/blob/34acbbae6795854cc4e9f8eb7167ab011e0266b4/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs#L409 using System; +using System.Buffers.Binary; using System.Collections.Generic; using System.Globalization; using System.IO; using System.IO.Compression; using System.Linq; -using System.Runtime.CompilerServices; using System.Security.Cryptography; using System.Text; using Microsoft.Build.Utilities; @@ -694,8 +694,8 @@ public static string HashStream (Stream stream) /// XOR the data length into the hash to avoid collisions on zero-filled inputs. static void XorLength (Span hash, ulong length) { - ref var crc = ref Unsafe.As (ref hash [0]); - crc ^= length; + ulong crc = BinaryPrimitives.ReadUInt64LittleEndian (hash); + BinaryPrimitives.WriteUInt64LittleEndian (hash, crc ^ length); } public static string ToHexString (byte[] hash) diff --git a/src/Microsoft.Android.Build.BaseTasks/Microsoft.Android.Build.BaseTasks.csproj b/src/Microsoft.Android.Build.BaseTasks/Microsoft.Android.Build.BaseTasks.csproj index d99669434b4..8fcf18642b0 100644 --- a/src/Microsoft.Android.Build.BaseTasks/Microsoft.Android.Build.BaseTasks.csproj +++ b/src/Microsoft.Android.Build.BaseTasks/Microsoft.Android.Build.BaseTasks.csproj @@ -8,7 +8,6 @@ netstandard2.0 Microsoft.Android.Build.Tasks - true true ..\..\product.snk $(VendorPrefix)Microsoft.Android.Build.BaseTasks$(VendorSuffix) diff --git a/src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj b/src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj index 10deba3e62b..f58a4f7e38b 100644 --- a/src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj +++ b/src/Microsoft.Android.Sdk.ILLink/Microsoft.Android.Sdk.ILLink.csproj @@ -6,7 +6,6 @@ false $(MicrosoftAndroidSdkOutDir) annotations - true