Add CSWINRT2018 analyzer for reads from write-only Span<T> parameters#2438
Open
Sergio0694 wants to merge 3 commits into
Open
Add CSWINRT2018 analyzer for reads from write-only Span<T> parameters#2438Sergio0694 wants to merge 3 commits into
Sergio0694 wants to merge 3 commits into
Conversation
…' parameters Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Detects reads from 'Span<T>' parameters on Windows Runtime methods, which are projected as write-only fill arrays in the ABI. Covers indexer reads, conversions to 'ReadOnlySpan<T>', and 'foreach' iteration, while skipping write-only usages (assignment targets, 'out'/'ref' arguments, writable 'ref' aliases and loop variables) to avoid false positives. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new Roslyn analyzer (
CSWINRT2018, severityWarning) that detects when the implementation of an authored Windows Runtime method reads from aSpan<T>parameter. Such parameters are projected as write-only fill arrays in the Windows Runtime ABI, so reading from them is not supported.Motivation
In the Windows Runtime ABI, array parameters use one of three conventions:
ReadOnlySpan<T>-> pass array ([in], read-only)out T[]-> receive array ([out]with byref)Span<T>-> fill array ([out]without byref)A fill array is write-only: the implementation is handed a caller-allocated buffer that it is expected to fill, and reading the existing contents is not supported (see this write-up on WinRT array parameters). Until now nothing flagged accidental reads from such a parameter, which can lead to subtle, hard-to-diagnose bugs in authored components. This analyzer surfaces the most common mistakes at compile time, while being careful to avoid false positives on legitimate write-only usage.
What it detects
For a by-value
System.Span<T>parameter of a Windows Runtime-exposed method, the analyzer reports a read when it sees:var x = span[i],Foo(span[i]),ref readonly var x = ref span[i],Foo(in span[i]),span[i]++,span[i] += 1ReadOnlySpan<T>(implicit or explicit), including when passed as an argumentforeachiteration over the spanAvoiding false positives
The analyzer only runs when authoring a component (
CsWinRTComponentistrue), and only for by-valueSpan<T>parameters of methods that are actually part of the ABI surface: public methods and constructors, or explicit implementations of public interfaces, declared on public, top-level classes. It intentionally does not warn on:span[i] = value,out/refarguments, writablerefaliases (ref var x = ref span[i]), and writablerefforeachloop variables (which can be used to fill all elements)span.Lengthandspan.IsEmptyReadOnlySpan<T>parametersReads that flow through aliasing or a call to another method taking a
Span<T>are an accepted blind spot, since the analyzer cannot reason about the callee's behavior.Changes
src/Authoring/WinRT.SourceGenerator2/Diagnostics/DiagnosticDescriptors.cs: add theCSWINRT2018(WriteOnlySpanParameterRead) descriptor.src/Authoring/WinRT.SourceGenerator2/AnalyzerReleases.Shipped.md: registerCSWINRT2018under the3.0.0release.src/Authoring/WinRT.SourceGenerator2/Diagnostics/Analyzers/WriteOnlySpanParameterAnalyzer.cs: new analyzer (operation-based detection for indexer reads,ReadOnlySpan<T>conversions, andforeach).src/Tests/SourceGenerator2Test/Test_WriteOnlySpanParameterAnalyzer.cs: tests covering the warning cases and the write-only / non-exposed cases that must stay silent.