From e29a3fad04764d3afe2b1a71fbced0b165b0e98b Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 14 Aug 2022 12:42:43 +0200 Subject: [PATCH] Add null checks in public API parameters, add tests --- src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs | 12 +++ .../Shaders/Interop/D2D1PixelShader.cs | 6 ++ .../Shaders/Interop/D2D1PixelShaderEffect.cs | 61 ++++++++++++- .../D2D1PixelShaderEffectTests.cs | 88 +++++++++++++++++++ .../D2D1PixelShaderTests.cs | 7 ++ 5 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs diff --git a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs index 36fd8f77..288452c9 100644 --- a/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs +++ b/src/ComputeSharp.D2D1/Helpers/ThrowHelper.cs @@ -34,6 +34,18 @@ internal static class ThrowHelper throw new ArgumentException(message, name); } + /// + /// Throws a new . + /// + /// The argument name. + /// The message to include in the exception. + /// Thrown with the specified parameters. + [DoesNotReturn] + public static void ThrowArgumentNullException(string? name, string? message) + { + throw new ArgumentNullException(name, message); + } + /// /// Throws a new . /// diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs index 83149d16..99015f4e 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShader.cs @@ -337,10 +337,16 @@ public static class D2D1PixelShader /// The type of D2D1 pixel shader to set the constant buffer for. /// The input D2D1 pixel shader to set the contant buffer for. /// A pointer to the ID2D1DrawInfo instance to use. + /// Thrown if is . /// For more info, see . public static unsafe void SetConstantBufferForD2D1DrawInfo(in T shader, void* d2D1DrawInfo) where T : unmanaged, ID2D1PixelShader { + if (d2D1DrawInfo is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(d2D1DrawInfo), "The input ID2D1DrawInfo object cannot be null."); + } + D2D1DrawInfoDispatchDataLoader dataLoader = new((ID2D1DrawInfo*)d2D1DrawInfo); Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); diff --git a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs index 46fa6db9..02405e86 100644 --- a/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs +++ b/src/ComputeSharp.D2D1/Shaders/Interop/D2D1PixelShaderEffect.cs @@ -27,11 +27,17 @@ public static unsafe class D2D1PixelShaderEffect /// The type of D2D1 pixel shader to register. /// A pointer to the ID2D1Factory1 instance to use. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. + /// Thrown if is . /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader { + if (d2D1Factory1 is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null."); + } + RegisterForD2D1Factory1(d2D1Factory1, null, out effectId); } @@ -42,12 +48,18 @@ public static unsafe class D2D1PixelShaderEffect /// The type of implementation to register. /// A pointer to the ID2D1Factory1 instance to use. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. + /// Thrown if is . /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static void RegisterForD2D1Factory1(void* d2D1Factory1, out Guid effectId) where T : unmanaged, ID2D1PixelShader where TMapper : class, ID2D1TransformMapper, new() { + if (d2D1Factory1 is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null."); + } + RegisterForD2D1Factory1(d2D1Factory1, static () => new TMapper(), out effectId); } @@ -58,11 +70,17 @@ public static unsafe class D2D1PixelShaderEffect /// A pointer to the ID2D1Factory1 instance to use. /// An optional factory of instances to use for the transform. /// The of the registered effect, which can be used to call ID2D1DeviceContext::CreateEffect. + /// Thrown if is . /// Thrown if an effect is registered multiple times with different properties. /// For more info, see . public static void RegisterForD2D1Factory1(void* d2D1Factory1, Func>? mapperFactory, out Guid effectId) where T : unmanaged, ID2D1PixelShader { + if (d2D1Factory1 is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(d2D1Factory1), "The input ID2D1Factory1 object cannot be null."); + } + effectId = default; PixelShaderEffect.For.Initialize(mapperFactory); @@ -484,6 +502,7 @@ public static unsafe class D2D1PixelShaderEffect /// The type of D2D1 pixel shader to create an effect for. /// A pointer to the ID2D1DeviceContext instance to use. /// A pointer to the resulting ID2D1Effect* pointer to produce. + /// Thrown if or are . /// /// Thrown if this method is called before initializing a shader effect for the shader type . /// To do so, make sure to call either (or an overload), or @@ -493,6 +512,16 @@ public static unsafe class D2D1PixelShaderEffect public static void CreateFromD2D1DeviceContext(void* d2D1DeviceContext, void** d2D1Effect) where T : unmanaged, ID2D1PixelShader { + if (d2D1DeviceContext is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(d2D1DeviceContext), "The input ID2D1DeviceContext object cannot be null."); + } + + if (d2D1Effect is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The pointer to the target ID2D1Effect result cannot be null."); + } + if (!PixelShaderEffect.For.TryGetId(out Guid id)) { ThrowHelper.ThrowInvalidOperationException("The effect for the input shader type has not been initialized yet."); @@ -509,10 +538,16 @@ public static unsafe class D2D1PixelShaderEffect /// The type of D2D1 pixel shader to set the constant buffer for. /// The input D2D1 pixel shader to set the contant buffer for. /// A pointer to the ID2D1Effect instance to use. + /// Thrown if is . /// For more info, see . public static void SetConstantBufferForD2D1Effect(in T shader, void* d2D1Effect) where T : unmanaged, ID2D1PixelShader { + if (d2D1Effect is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1DeviceContext object cannot be null."); + } + D2D1EffectDispatchDataLoader dataLoader = new((ID2D1Effect*)d2D1Effect); Unsafe.AsRef(in shader).LoadDispatchData(ref dataLoader); @@ -524,14 +559,25 @@ public static unsafe class D2D1PixelShaderEffect /// A pointer to the ID2D1Effect instance to use. /// The input ID2D1ResourceTextureManager object (see ). /// The index of the resource texture to assign the resource texture manager to. + /// Thrown if or are . /// For more info, see . public static void SetResourceTextureManagerForD2D1Effect(void* d2D1Effect, void* resourceTextureManager, int index) { + if (d2D1Effect is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1Effect object cannot be null."); + } + + if (resourceTextureManager is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(resourceTextureManager), "The input ID2D1ResourceTextureManager object cannot be null."); + } + ((ID2D1Effect*)d2D1Effect)->SetValue( index: D2D1PixelShaderEffectProperty.ResourceTextureManager0 + (uint)index, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN, data: (byte*)&resourceTextureManager, - dataSize: (uint)sizeof(void*)); + dataSize: (uint)sizeof(void*)).Assert(); } /// @@ -540,9 +586,20 @@ public static unsafe class D2D1PixelShaderEffect /// A pointer to the ID2D1Effect instance to use. /// The input instance.. /// The index of the resource texture to assign the resource texture manager to. + /// Thrown if or are . /// For more info, see . public static void SetResourceTextureManagerForD2D1Effect(void* d2D1Effect, D2D1ResourceTextureManager resourceTextureManager, int index) { + if (d2D1Effect is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(d2D1Effect), "The input ID2D1Effect object cannot be null."); + } + + if (resourceTextureManager is null) + { + ThrowHelper.ThrowArgumentNullException(nameof(resourceTextureManager), "The input D2D1ResourceTextureManager object cannot be null."); + } + using ComPtr resourceTextureManager2 = default; Guid uuidOfResourceTextureManager = ID2D1ResourceTextureManager.Guid; @@ -553,6 +610,6 @@ public static unsafe class D2D1PixelShaderEffect index: D2D1PixelShaderEffectProperty.ResourceTextureManager0 + (uint)index, type: D2D1_PROPERTY_TYPE.D2D1_PROPERTY_TYPE_IUNKNOWN, data: (byte*)resourceTextureManager2.GetAddressOf(), - dataSize: (uint)sizeof(void*)); + dataSize: (uint)sizeof(void*)).Assert(); } } diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs new file mode 100644 index 00000000..83a3dc65 --- /dev/null +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderEffectTests.cs @@ -0,0 +1,88 @@ +using System; +using ComputeSharp.D2D1.Interop; +using ComputeSharp.D2D1.Tests.Effects; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace ComputeSharp.D2D1.Tests; + +[TestClass] +[TestCategory("D2D1PixelShaderEffect")] +public class D2D1PixelShaderEffectTests +{ + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void RegisterForD2D1Factory1_NullD2D1Factory1() + { + D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, out _); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void RegisterForD2D1Factory1_WithTransformMapper_NullD2D1Factory1() + { + D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, out _); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void RegisterForD2D1Factory1_WithTransformMapperFactory_NullD2D1Factory1() + { + D2D1PixelShaderEffect.RegisterForD2D1Factory1(null, () => new PixelateEffect(), out _); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void CreateFromD2D1DeviceContext_NullD2D1DeviceContext() + { + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext(null, (void**)1); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void CreateFromD2D1DeviceContext_NullD2D1Effect() + { + D2D1PixelShaderEffect.CreateFromD2D1DeviceContext((void*)1, null); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void SetConstantBufferForD2D1Effect_NullD2D1Effect() + { + D2D1PixelShaderEffect.SetConstantBufferForD2D1Effect(default, null); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void SetResourceTextureManagerForD2D1Effect_NullD2D1Effect() + { + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(null, (void*)1, 0); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void SetResourceTextureManagerForD2D1Effect_NullD2D1ResourceTextureManager() + { + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect((void*)1, (void*)null, 0); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void SetResourceTextureManagerForD2D1Effect_RCW_NullD2D1Effect() + { + D2D1ResourceTextureManager resourceTextureManager = new( + extents: stackalloc uint[] { 64 }, + bufferPrecision: D2D1BufferPrecision.UInt8Normalized, + channelDepth: D2D1ChannelDepth.One, + filter: D2D1Filter.MinLinearMagMipPoint, + extendModes: stackalloc D2D1ExtendMode[] { D2D1ExtendMode.Clamp }); + + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect(null, resourceTextureManager, 0); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void SetResourceTextureManagerForD2D1Effect_RCW_NullD2D1ResourceTextureManager() + { + D2D1PixelShaderEffect.SetResourceTextureManagerForD2D1Effect((void*)1, (D2D1ResourceTextureManager)null!, 0); + } +} \ No newline at end of file diff --git a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs index 3028574b..a0375100 100644 --- a/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs +++ b/tests/ComputeSharp.D2D1.Tests/D2D1PixelShaderTests.cs @@ -559,4 +559,11 @@ public partial class D2D1PixelShaderTests return 0; } } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException), AllowDerivedTypes = false)] + public unsafe void SetConstantBufferForD2D1DrawInfo_NullD2D1DrawInfo() + { + D2D1PixelShader.SetConstantBufferForD2D1DrawInfo(default, null); + } } \ No newline at end of file