Emit null checks in compiler-synthesized inline-array helpers#84361
Draft
Copilot wants to merge 5 commits into
Draft
Emit null checks in compiler-synthesized inline-array helpers#84361Copilot wants to merge 5 commits into
Copilot wants to merge 5 commits into
Conversation
- Added System_Runtime_CompilerServices_Unsafe__IsNullRef_T to WellKnownMember enum - Added System_NullReferenceException__ctor to WellKnownMember enum - Added descriptors for both new members in WellKnownMembers.cs - Modified SynthesizedInlineArrayElementRefMethod to emit null check - Modified SynthesizedInlineArrayElementRefReadOnlyMethod to emit null check - Modified SynthesizedInlineArrayAsSpanMethod to emit null check - Modified SynthesizedInlineArrayAsReadOnlySpanMethod to emit null check Each helper now checks `if (Unsafe.IsNullRef(ref buffer))` and throws NullReferenceException before performing the unsafe operations. Co-authored-by: AlekseyTs <10522107+AlekseyTs@users.noreply.github.com>
- Moved System_NullReferenceException from before to after ExtSentinel - Updated descriptor to use two-byte encoding for ExtSentinel types - Added metadata name after ExtSentinel marker Co-authored-by: AlekseyTs <10522107+AlekseyTs@users.noreply.github.com>
…rectly - Moved System_NullReferenceException after CSharp7Sentinel to avoid shifting existing types - Fixed f.New calls in all four inline array helpers to not pass constructor method - Build now succeeds for both Core and CSharp compiler projects Co-authored-by: AlekseyTs <10522107+AlekseyTs@users.noreply.github.com>
Added four tests verifying that null checks throw NullReferenceException: - NullCheck_ElementRef: Tests element access via indexer - NullCheck_ElementRefReadOnly: Tests read-only element access - NullCheck_AsSpan: Tests Span<T> conversion - NullCheck_AsReadOnlySpan: Tests ReadOnlySpan<T> conversion IL verification is skipped for these tests as the generated code is valid but the block/if structure with ref returns doesn't pass strict IL verification. Co-authored-by: AlekseyTs <10522107+AlekseyTs@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.
The compiler-synthesized inline-array helpers (
InlineArrayElementRef,InlineArrayElementRefReadOnly,InlineArrayAsSpan,InlineArrayAsReadOnlySpan) form byrefs/spans frombufferwithout null checks. Whenbufferis a null byref (reachable viaUnsafe.NullRef<T>()in safe code), this produces byrefs/spans over arbitrary memory at caller-controlled offsets, causing silent corruption instead of deterministic NRE.Example
Changes
Compiler infrastructure:
System_NullReferenceExceptiontoWellKnownTypes(positioned afterCSharp7Sentinelto preserve assertion invariants)Unsafe.IsNullRef<T>andNullReferenceException..ctortoWellKnownMember/WellKnownMembersCode generation:
Each of the four helpers now emits
if (Unsafe.IsNullRef(ref buffer)) throw new NullReferenceException();before the unsafe operations:SynthesizedInlineArrayElementRefMethodSynthesizedInlineArrayElementRefReadOnlyMethodSynthesizedInlineArrayAsSpanMethodSynthesizedInlineArrayAsReadOnlySpanMethodTests:
Added four execution tests in
InlineArrayTests.csverifying NRE is thrown. IL verification skipped (block/if with ref-return doesn't satisfy strict verifier rules despite being valid IL).