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

Fix AV when indexing a sliced Memory2D<T> backed by custom MemoryManager<T> #675

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -75,15 +75,15 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
ThrowArgumentOutOfRangeExceptionForInvalidIndex();
}

int bytePrefix = this.offset * sizeof(TFrom);
int byteSuffix = elementIndex * sizeof(TTo);
int byteOffset = bytePrefix + byteSuffix;
nint bytePrefix = this.offset * sizeof(TFrom);
nint byteSuffix = elementIndex * sizeof(TTo);
nint byteOffset = bytePrefix + byteSuffix;

GCHandle handle = GCHandle.Alloc(this.array, GCHandleType.Pinned);

ref TFrom r0 = ref this.array.DangerousGetReference();
ref byte r1 = ref Unsafe.As<TFrom, byte>(ref r0);
ref byte r2 = ref Unsafe.Add(ref r1, byteOffset);
ref byte r2 = ref Unsafe.AddByteOffset(ref r1, byteOffset);
void* pi = Unsafe.AsPointer(ref r2);

return new(pi, handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public override unsafe MemoryHandle Pin(int elementIndex = 0)
ThrowArgumentOutOfRangeExceptionForInvalidIndex();
}

int bytePrefix = this.offset * sizeof(char);
int byteSuffix = elementIndex * sizeof(TTo);
int byteOffset = bytePrefix + byteSuffix;
nint bytePrefix = this.offset * sizeof(char);
nint byteSuffix = elementIndex * sizeof(TTo);
nint byteOffset = bytePrefix + byteSuffix;

GCHandle handle = GCHandle.Alloc(this.text, GCHandleType.Pinned);

ref char r0 = ref this.text.DangerousGetReference();
ref byte r1 = ref Unsafe.As<char, byte>(ref r0);
ref byte r2 = ref Unsafe.Add(ref r1, byteOffset);
ref byte r2 = ref Unsafe.AddByteOffset(ref r1, byteOffset);
void* pi = Unsafe.AsPointer(ref r2);

return new(pi, handle);
Expand Down
6 changes: 3 additions & 3 deletions src/CommunityToolkit.HighPerformance/Memory/Memory2D{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ namespace CommunityToolkit.HighPerformance;
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down Expand Up @@ -603,7 +603,7 @@ public Span2D<T> Span
if (this.instance is MemoryManager<T> memoryManager)
{
ref T r0 = ref memoryManager.GetSpan().DangerousGetReference();
ref T r1 = ref Unsafe.Add(ref r0, this.offset);
ref T r1 = ref Unsafe.AddByteOffset(ref r0, this.offset);

return new(ref r1, this.height, this.width, this.pitch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ namespace CommunityToolkit.HighPerformance;
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down Expand Up @@ -615,7 +615,7 @@ public ReadOnlySpan2D<T> Span
if (this.instance is MemoryManager<T> memoryManager)
{
ref T r0 = ref memoryManager.GetSpan().DangerousGetReference();
ref T r1 = ref Unsafe.Add(ref r0, this.offset);
ref T r1 = ref Unsafe.AddByteOffset(ref r0, this.offset);

return new(in r1, this.height, this.width, this.pitch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public ref struct Enumerator
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public readonly ref partial struct ReadOnlySpan2D<T>
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public ref struct Enumerator
private readonly object? instance;

/// <summary>
/// The initial offset within <see cref="instance"/>.
/// The initial byte offset within <see cref="instance"/>.
/// </summary>
private readonly IntPtr offset;
private readonly nint offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down
4 changes: 2 additions & 2 deletions src/CommunityToolkit.HighPerformance/Memory/Span2D{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public readonly ref partial struct Span2D<T>
internal readonly object? Instance;

/// <summary>
/// The initial offset within <see cref="Instance"/>.
/// The initial byte offset within <see cref="Instance"/>.
/// </summary>
internal readonly IntPtr Offset;
internal readonly nint Offset;

/// <summary>
/// The height of the specified 2D region.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,4 +516,29 @@ public void Test_Memory2DT_ToString()

Assert.AreEqual(text, expected);
}

#if NET6_0_OR_GREATER
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/3536
[TestMethod]
[DataRow(720, 1280)]
public void Test_Memory2DT_CastAndSlice_WorksCorrectly(int height, int width)
{
Memory2D<int> data =
new byte[width * height * sizeof(int)]
.AsMemory()
.Cast<byte, int>()
.AsMemory2D(height: height, width: width);

Memory2D<int> slice = data.Slice(
row: height / 2,
column: 0,
height: height / 2,
width: width);

Assert.IsTrue(Unsafe.AreSame(ref data.Span[height / 2, 0], ref slice.Span[0, 0]));
Assert.IsTrue(Unsafe.AreSame(ref data.Span[height / 2, width - 1], ref slice.Span[0, width - 1]));
Assert.IsTrue(Unsafe.AreSame(ref data.Span[height - 1, 0], ref slice.Span[(height / 2) - 1, 0]));
Assert.IsTrue(Unsafe.AreSame(ref data.Span[height - 1, width - 1], ref slice.Span[(height / 2) - 1, width - 1]));
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -464,4 +464,29 @@ public void Test_ReadOnlyMemory2DT_ToString()

Assert.AreEqual(text, expected);
}

#if NET6_0_OR_GREATER
// See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/3536
[TestMethod]
[DataRow(720, 1280)]
public void Test_ReadOnlyMemory2DT_CastAndSlice_WorksCorrectly(int height, int width)
{
ReadOnlyMemory2D<int> data =
new byte[width * height * sizeof(int)]
.AsMemory()
.Cast<byte, int>()
.AsMemory2D(height: height, width: width);

ReadOnlyMemory2D<int> slice = data.Slice(
row: height / 2,
column: 0,
height: height / 2,
width: width);

Assert.IsTrue(Unsafe.AreSame(ref Unsafe.AsRef(in data.Span[height / 2, 0]), ref Unsafe.AsRef(in slice.Span[0, 0])));
Assert.IsTrue(Unsafe.AreSame(ref Unsafe.AsRef(in data.Span[height / 2, width - 1]), ref Unsafe.AsRef(in slice.Span[0, width - 1])));
Assert.IsTrue(Unsafe.AreSame(ref Unsafe.AsRef(in data.Span[height - 1, 0]), ref Unsafe.AsRef(in slice.Span[(height / 2) - 1, 0])));
Assert.IsTrue(Unsafe.AreSame(ref Unsafe.AsRef(in data.Span[height - 1, width - 1]), ref Unsafe.AsRef(in slice.Span[(height / 2) - 1, width - 1])));
}
#endif
}