Add bounds checks for sources and resource texture managers
このコミットが含まれているのは:
1 親
ab998c6228
コミット
49ecac2169
6ファイル変更
+221
-10
検索条件に一致するファイルはありません
@@ -40,6 +40,13 @@
|
||||
See: https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1060.
|
||||
-->
|
||||
<NoWarn>$(NoWarn);CA1060</NoWarn>
|
||||
|
||||
<!--
|
||||
Suppress ref safety warnings in unsafe contexts (see https://github.com/dotnet/csharplang/issues/6476).
|
||||
This is used eg. to replace Unsafe.SizeOf<T>() calls with just sizeof(T). The warning is not necessary
|
||||
since in order to use these APIs the caller already has to be in an unsafe context.
|
||||
-->
|
||||
<NoWarn>$(NoWarn);CS8500</NoWarn>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Centralized location for all generated artifacts -->
|
||||
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.__Internals;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.Shaders.Loaders;
|
||||
|
||||
/// <summary>
|
||||
/// A resource texture index array loader for D2D1 pixel shaders.
|
||||
/// </summary>
|
||||
internal struct D2D1ImmutableArrayResourceTextureDescriptionsLoader : ID2D1ResourceTextureDescriptionsLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// The array of indices for resource textures for the current effect.
|
||||
/// </summary>
|
||||
private ImmutableArray<int> indices;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the resulting array of indices for resource textures for the current effect.
|
||||
/// </summary>
|
||||
/// <returns>An array of indices for resource textures for the current effect.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public ImmutableArray<int> GetResultingIndices()
|
||||
{
|
||||
return this.indices;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
unsafe void ID2D1ResourceTextureDescriptionsLoader.LoadResourceTextureDescriptions(ReadOnlySpan<byte> data)
|
||||
{
|
||||
if (data.IsEmpty)
|
||||
{
|
||||
this.indices = ImmutableArray<int>.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadOnlySpan<D2D1ResourceTextureDescription> descriptions = MemoryMarshal.Cast<byte, D2D1ResourceTextureDescription>(data);
|
||||
|
||||
int[] indices = new int[descriptions.Length];
|
||||
|
||||
for (int i = 0; i < descriptions.Length; i++)
|
||||
{
|
||||
indices[i] = descriptions[i].Index;
|
||||
}
|
||||
|
||||
this.indices = *(ImmutableArray<int>*)&indices;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ComputeSharp.D2D1.__Internals;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.Shaders.Loaders;
|
||||
|
||||
/// <summary>
|
||||
/// A resource texture index bitmask loader for D2D1 pixel shaders.
|
||||
/// </summary>
|
||||
internal struct D2D1IndexBitmaskResourceTextureDescriptionsLoader : ID2D1ResourceTextureDescriptionsLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// The bitmask of indices for resource textures in the target shader.
|
||||
/// </summary>
|
||||
private int indexBitmask;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the resulting bitmask of indices for resource textures in the target shader.
|
||||
/// </summary>
|
||||
/// <returns>A bitmask of indices for resource textures in the target shader.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public int GetResultingIndexBitmask()
|
||||
{
|
||||
return this.indexBitmask;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
void ID2D1ResourceTextureDescriptionsLoader.LoadResourceTextureDescriptions(ReadOnlySpan<byte> data)
|
||||
{
|
||||
int indexBitmask = 0;
|
||||
|
||||
foreach (ref readonly D2D1ResourceTextureDescription description in MemoryMarshal.Cast<byte, D2D1ResourceTextureDescription>(data))
|
||||
{
|
||||
indexBitmask |= 1 << description.Index;
|
||||
}
|
||||
|
||||
this.indexBitmask = indexBitmask;
|
||||
}
|
||||
}
|
||||
@@ -195,7 +195,7 @@ unsafe partial class PixelShaderEffect<T>
|
||||
this.d2D1BufferPrecision = this.d2D1Effect.Get()->GetPrecisionProperty();
|
||||
|
||||
// Loop over all effect inputs and update the cache back as well
|
||||
for (int i = 0; i < 16; i++)
|
||||
for (int i = 0; i < SourceCollection.Count; i++)
|
||||
{
|
||||
// If this index has been explicitly requested to be skipped, just dispose the source reference.
|
||||
// This is the case when the effect is being unrealized, and callers will set this value on their own.
|
||||
@@ -277,12 +277,13 @@ unsafe partial class PixelShaderEffect<T>
|
||||
this.d2D1Effect.Get()->SetPrecisionProperty(this.d2D1BufferPrecision);
|
||||
}
|
||||
|
||||
// Set all available resource texture managers (only set those that are not null, as they're all null by default anyway)
|
||||
for (int i = 0; i < 16; i++)
|
||||
// Set all available resource texture managers (only set those that are not null, as they're all null by default anyway).
|
||||
// The loop only goes over the indices of valid slots for resource textures for the current effect, and ignores the others.
|
||||
foreach (int index in ResourceTextureManagerCollection.Indices)
|
||||
{
|
||||
if (ResourceTextureManagers.Storage[i] is D2D1ResourceTextureManager resourceTextureManager)
|
||||
if (ResourceTextureManagers.Storage[index] is D2D1ResourceTextureManager resourceTextureManager)
|
||||
{
|
||||
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), resourceTextureManager, i);
|
||||
D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(this.d2D1Effect.Get(), resourceTextureManager, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -295,7 +296,7 @@ unsafe partial class PixelShaderEffect<T>
|
||||
/// <param name="deviceContext">The <see cref="ID2D1DeviceContext"/> instance in use.</param>
|
||||
private void RefreshInputs(WIN2D_GET_D2D_IMAGE_FLAGS flags, float targetDpi, ID2D1DeviceContext* deviceContext)
|
||||
{
|
||||
for (int i = 0; i < 16; i++)
|
||||
for (int i = 0; i < SourceCollection.Count; i++)
|
||||
{
|
||||
// Retrieve the managed wrapper for the current source
|
||||
IGraphicsEffectSource? source = GetD2DInput(i);
|
||||
|
||||
+77
-2
@@ -1,8 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Shaders.Loaders;
|
||||
using ComputeSharp.D2D1.Uwp.Buffers;
|
||||
|
||||
#pragma warning disable CS0618
|
||||
|
||||
namespace ComputeSharp.D2D1.Uwp;
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -13,6 +19,11 @@ partial class PixelShaderEffect<T>
|
||||
/// </summary>
|
||||
public sealed class ResourceTextureManagerCollection
|
||||
{
|
||||
/// <summary>
|
||||
/// The bitmask of valid indices for resource texture managers in the current shader type.
|
||||
/// </summary>
|
||||
private static readonly int IndexBitmask = GetIndexBitmask();
|
||||
|
||||
/// <summary>
|
||||
/// The fixed buffer of <see cref="D2D1ResourceTextureManager"/> instances.
|
||||
/// </summary>
|
||||
@@ -27,6 +38,11 @@ partial class PixelShaderEffect<T>
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of valid resource texture indices for the current effect.
|
||||
/// </summary>
|
||||
internal static ImmutableArray<int> Indices { get; } = GetIndices();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="D2D1ResourceTextureManager"/> object at a specified index.
|
||||
/// </summary>
|
||||
@@ -37,8 +53,23 @@ partial class PixelShaderEffect<T>
|
||||
[DisallowNull]
|
||||
public D2D1ResourceTextureManager? this[int index]
|
||||
{
|
||||
get => Owner.GetResourceTextureManager(index);
|
||||
set => Owner.SetResourceTextureManager(value, index);
|
||||
get
|
||||
{
|
||||
ValidateIndex(index);
|
||||
|
||||
return Owner.GetResourceTextureManager(index);
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentNullException(nameof(value), "Input resource texture managers cannot be null.");
|
||||
}
|
||||
|
||||
ValidateIndex(index);
|
||||
|
||||
Owner.SetResourceTextureManager(value, index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -50,5 +81,49 @@ partial class PixelShaderEffect<T>
|
||||
/// Gets a reference to the <see cref="ResourceTextureManagerBuffer"/> value containing the available <see cref="D2D1ResourceTextureManager"/>-s.
|
||||
/// </summary>
|
||||
internal ref ResourceTextureManagerBuffer Storage => ref this.fixedBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the input index for <see cref="this[int]"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">The index to validate.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is out of range.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void ValidateIndex(int index)
|
||||
{
|
||||
if ((uint)index >= 16 || (IndexBitmask & (1 << index)) == 0)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is not a valid resource texture manager index for the current effect.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bitmask of indices for resource textures in the target shader.
|
||||
/// </summary>
|
||||
/// <returns>The bitmask of indices for resource textures in the target shader.</returns>
|
||||
private static int GetIndexBitmask()
|
||||
{
|
||||
D2D1IndexBitmaskResourceTextureDescriptionsLoader bitmaskLoader = default;
|
||||
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
shader.LoadResourceTextureDescriptions(ref bitmaskLoader);
|
||||
|
||||
return bitmaskLoader.GetResultingIndexBitmask();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the collection of valid indices for the current effect.
|
||||
/// </summary>
|
||||
/// <returns>The collection of valid indices for the current effect.</returns>
|
||||
private static ImmutableArray<int> GetIndices()
|
||||
{
|
||||
D2D1ImmutableArrayResourceTextureDescriptionsLoader indicesLoader = default;
|
||||
|
||||
Unsafe.SkipInit(out T shader);
|
||||
|
||||
shader.LoadResourceTextureDescriptions(ref indicesLoader);
|
||||
|
||||
return indicesLoader.GetResultingIndices();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using ComputeSharp.D2D1.Helpers;
|
||||
using ComputeSharp.D2D1.Interop;
|
||||
using ComputeSharp.D2D1.Uwp.Buffers;
|
||||
using Windows.Graphics.Effects;
|
||||
|
||||
@@ -26,6 +29,11 @@ partial class PixelShaderEffect<T>
|
||||
Owner = owner;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the actual input count for the current effect.
|
||||
/// </summary>
|
||||
internal static int Count { get; } = D2D1PixelShader.GetInputCount<T>();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="IGraphicsEffectSource"/> source at a specified index.
|
||||
/// </summary>
|
||||
@@ -34,8 +42,18 @@ partial class PixelShaderEffect<T>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is not a valid index for the current effect.</exception>
|
||||
public IGraphicsEffectSource? this[int index]
|
||||
{
|
||||
get => Owner.GetSource(index);
|
||||
set => Owner.SetSource(value, index);
|
||||
get
|
||||
{
|
||||
ValidateIndex(index);
|
||||
|
||||
return Owner.GetSource(index);
|
||||
}
|
||||
set
|
||||
{
|
||||
ValidateIndex(index);
|
||||
|
||||
Owner.SetSource(value, index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -47,5 +65,19 @@ partial class PixelShaderEffect<T>
|
||||
/// Gets a reference to the <see cref="GraphicsEffectSourceBuffer"/> value containing the available <see cref="IGraphicsEffectSource"/>-s.
|
||||
/// </summary>
|
||||
internal ref GraphicsEffectSourceBuffer Storage => ref this.fixedBuffer;
|
||||
|
||||
/// <summary>
|
||||
/// Validates the input index for <see cref="this[int]"/>.
|
||||
/// </summary>
|
||||
/// <param name="index">The index to validate.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown if <paramref name="index"/> is out of range.</exception>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void ValidateIndex(int index)
|
||||
{
|
||||
if ((uint)index >= Count)
|
||||
{
|
||||
ThrowHelper.ThrowArgumentOutOfRangeException(nameof(index), "The input index is not a valid source index for the current effect.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする