Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle non-ASCII strings in GetNonRandomizedHashCodeOrdinalIgnoreCase #44688

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,22 @@ public void CantAcceptDuplicateKeysFromSourceDictionary()
AssertExtensions.Throws<ArgumentException>(null, () => new Dictionary<string, int>(source, StringComparer.OrdinalIgnoreCase));
}

[Fact]
// https://github.com/dotnet/runtime/issues/44681
public void DictionaryOrdinalIgnoreCaseCyrillicKeys()
{
const string Lower = "абвгдеёжзийклмнопрстуфхцчшщьыъэюя";
const string Higher = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЫЪЭЮЯ";

var dictionary = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);

for (int i = 0; i < Lower.Length; i++)
{
dictionary[Lower[i].ToString()] = i;
Assert.Equal(i, dictionary[Higher[i].ToString()]);
}
}

public static IEnumerable<object[]> CopyConstructorStringComparerData
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text.Unicode;
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
using System.Buffers;

using Internal.Runtime.CompilerServices;

Expand Down Expand Up @@ -834,43 +836,73 @@ internal unsafe int GetNonRandomizedHashCode()
}
}

// Use this if and only if 'Denial of Service' attacks are not a concern (i.e. never used for free-form user input),
// or are otherwise mitigated
internal unsafe int GetNonRandomizedHashCodeOrdinalIgnoreCase()
internal int GetNonRandomizedHashCodeOrdinalIgnoreCase()
{
fixed (char* src = &_firstChar)
{
Debug.Assert(src[this.Length] == '\0', "src[this.Length] == '\\0'");
Debug.Assert(((int)src) % 4 == 0, "Managed string should start at 4 bytes boundary");
return GetNonRandomizedHashCodeOrdinalIgnoreCaseStatic(ref _firstChar, Length, true);
}

uint hash1 = (5381 << 16) + 5381;
uint hash2 = hash1;
private static int GetNonRandomizedHashCodeOrdinalIgnoreCaseStatic(ref char firstChar, int length, bool normalizeNonAscii)
{
uint hash1 = (5381 << 16) + 5381;
uint hash2 = hash1;

uint* ptr = (uint*)src;
int length = this.Length;
// We "normalize to lowercase" every char by ORing with 0x0020. This casts
// a very wide net because it will change, e.g., '^' to '~'. But that should
// be ok because we expect this to be very rare in practice.

// We "normalize to lowercase" every char by ORing with 0x0020. This casts
// a very wide net because it will change, e.g., '^' to '~'. But that should
// be ok because we expect this to be very rare in practice.
const uint NormalizeToLowercase = 0x0020_0020u; // valid both for big-endian and for little-endian

const uint NormalizeToLowercase = 0x0020_0020u; // valid both for big-endian and for little-endian
int i = 0;
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
int count = 0;

while (length > 2)
while (count > 2)
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
uint p0 = Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref Unsafe.As<char, byte>(ref firstChar), i));
uint p1 = Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref Unsafe.As<char, byte>(ref firstChar), i + 4));
if (normalizeNonAscii && !Utf16Utility.AllCharsInUInt32AreAscii(p0 | p1))
EgorBo marked this conversation as resolved.
Show resolved Hide resolved
{
length -= 4;
// Where length is 4n-1 (e.g. 3,7,11,15,19) this additionally consumes the null terminator
hash1 = (BitOperations.RotateLeft(hash1, 5) + hash1) ^ (ptr[0] | NormalizeToLowercase);
hash2 = (BitOperations.RotateLeft(hash2, 5) + hash2) ^ (ptr[1] | NormalizeToLowercase);
ptr += 2;
goto NotAscii;
}
count -= 4;
// Where count is 4n-1 (e.g. 3,7,11,15,19) this additionally consumes the null terminator
hash1 = (BitOperations.RotateLeft(hash1, 5) + hash1) ^ (p0 | NormalizeToLowercase);
hash2 = (BitOperations.RotateLeft(hash2, 5) + hash2) ^ (p1 | NormalizeToLowercase);
i += 8;
}

if (length > 0)
if (count > 0)
{
uint p0 = Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref Unsafe.As<char, byte>(ref firstChar), i));
if (normalizeNonAscii && !Utf16Utility.AllCharsInUInt32AreAscii(p0))
{
// Where length is 4n-3 (e.g. 1,5,9,13,17) this additionally consumes the null terminator
hash2 = (BitOperations.RotateLeft(hash2, 5) + hash2) ^ (ptr[0] | NormalizeToLowercase);
goto NotAscii;
}

return (int)(hash1 + (hash2 * 1566083941));
// Where count is 4n-3 (e.g. 1,5,9,13,17) this additionally consumes the null terminator
hash2 = (BitOperations.RotateLeft(hash2, 5) + hash2) ^ (p0 | NormalizeToLowercase);
}

return (int)(hash1 + (hash2 * 1566083941));

NotAscii:
return GetNonRandomizedHashCodeOrdinalIgnoreCaseSlow(ref firstChar, length);

static int GetNonRandomizedHashCodeOrdinalIgnoreCaseSlow(ref char firstChar, int length)
{
char[]? borrowedArr = null;
Span<char> scratch = (uint)length <= 64 ? stackalloc char[64] : (borrowedArr = ArrayPool<char>.Shared.Rent(length));

int charsWritten = System.Globalization.Ordinal.ToUpperOrdinal(
MemoryMarshal.CreateReadOnlySpan(ref firstChar, length), scratch);

ref char upperCase = ref MemoryMarshal.GetReference(scratch);
int hashCode = GetNonRandomizedHashCodeOrdinalIgnoreCaseStatic(ref upperCase, length, false);

if (borrowedArr != null)
{
ArrayPool<char>.Shared.Return(borrowedArr);
}
return hashCode;
}
}

Expand Down