-
Notifications
You must be signed in to change notification settings - Fork 215
Merge requiresunsafe-corelib2 into feature/requires-unsafe #3209
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
base: feature/requires-unsafe
Are you sure you want to change the base?
Merge requiresunsafe-corelib2 into feature/requires-unsafe #3209
Conversation
28bb667 to
8aec308
Compare
agocke
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Please fix
src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs
Outdated
Show resolved
Hide resolved
…OWNERS and format unsafe blocks Co-authored-by: agocke <[email protected]>
agocke
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please fix
| // or a U[] which is blittable to a T[] (e.g., int[] and uint[]). | ||
|
|
||
| // The array may be prepinned, so remove the high bit from the start index in the line below. | ||
| // The ArraySegment<T> ctor will perform bounds checking on index & length. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this comment down to keep it on ArraySegment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 9a8c31b. Moved the comment about ArraySegment.<T> constructor bounds checking down to the line before the constructor call.
| // ! or you may introduce a security hole! | ||
| T[] @this = Unsafe.As<T[]>(this); | ||
| T[] @this; | ||
| // FIXME: review unsafe to confirm correct annotation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For every place in this file with the // ! Warning: "this" is an array comment, please remove the FIXME, as it is stated what the invariant is in the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit 9a8c31b. Removed FIXME comments from all 7 methods in Array.CoreCLR.cs that have the "Warning: this is an array" comment explaining the invariant.
...s/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConfiguredValueTaskAwaitable.cs
Show resolved
Hide resolved
…, refactor Delegate.CoreCLR.cs, add clarifying comments Co-authored-by: agocke <[email protected]>
Fixed in commit 9a8c31b. All PR feedback has been addressed:
|
…ocks Co-authored-by: agocke <[email protected]>
agocke
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Please address comments
| Debug.Assert(obj is MulticastDelegate, "Shouldn't have failed here since we already checked the types are the same!"); | ||
| MulticastDelegate d = Unsafe.As<MulticastDelegate>(obj); | ||
| MulticastDelegate d; | ||
| // FIXME: review unsafe to confirm correct annotation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, I think we can remove the comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit c0df804. Removed the FIXME comment.
| return false; | ||
| } | ||
|
|
||
| private unsafe MulticastDelegate NewMulticastDelegate(object[] invocationList, int invocationCount, bool thisIsMultiCastAlready) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove unsafe from this method since we have an unsafe block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit c0df804. Removed unsafe modifier from the method signature.
src/libraries/System.Private.CoreLib/src/System/Buffers/SharedArrayPool.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Buffers/SharedArrayPool.cs
Show resolved
Hide resolved
| /// <summary>Schedules the continuation action for this ValueTask.</summary> | ||
| public void OnCompleted(Action continuation) | ||
| { | ||
| object? obj = _value._obj; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make the same changes here as suggested in the ConfiguredValueTaskAwaitable class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit c0df804. Made the same changes to ValueTaskAwaiter._value._obj field.
| IntPtr[]? result; | ||
| unsafe | ||
| { | ||
| result = Unsafe.As<IntPtr[]>(managed); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just return the value directly instead of saving to a local.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit c0df804. Refactored to return the value directly.
| IntPtr[]? result; | ||
| unsafe | ||
| { | ||
| result = Unsafe.As<IntPtr[]>(_managedArray); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add a comment above the unsafe block in both of the instances in this file saying
// IntPtr[] and T*[] have the same representation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit c0df804. Refactored to return directly and added comment "// IntPtr[] and T*[] have the same representation" to both instances.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit c0df804. Added comment "// IntPtr[] and T*[] have the same representation" to both instances.
| T result; | ||
| unsafe | ||
| { | ||
| result = Unsafe.As<T>(GCHandle.InternalGet(handle)!); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just return the value and remove the result variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit c0df804. Refactored to return the value directly and removed the result variable.
| ManagedObjectWrapperHolder? holder; | ||
| unsafe | ||
| { | ||
| holder = Unsafe.As<ManagedObjectWrapperHolder>(GCHandle.FromIntPtr(handle).Target); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just return, and remove the holder variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit c0df804. Refactored to return directly and removed the holder variable.
…urn statements, add clarifying comments Co-authored-by: agocke <[email protected]>
agocke
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Please address comments. Also, there are new warnings/errors from [RequiresUnsafe] additions. Please fix those by adding a new unsafe block with the comment
// FIXME: review unsafe to confirm correct annotation
``` above
| buffer = Unsafe.As<T[]>(b.TryPop()); | ||
| unsafe | ||
| { | ||
| buffer = Unsafe.As<T[]>(b.TryPop()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please mark the TryPush method RequiresUnsafe instead.
| /// This allows tools to understand which methods are unsafe to call when removing unreferenced | ||
| /// code from an application. | ||
| /// </remarks> | ||
| [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add "field" to the valid targets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit b73ffce. Added Field to AttributeTargets.
| #pragma warning restore CA1823 | ||
| private IntPtr m_cache; | ||
| // Must be a handle to a RuntimeTypeCache type | ||
| private unsafe IntPtr m_cache; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add [RequiresUnsafe] as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit b73ffce. Added [RequiresUnsafe] to m_cache field.
| /// <summary>The stored array.</summary> | ||
| public Array? Array; | ||
| // Must be an array of T[] at runtime | ||
| public unsafe Array? Array; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also add [RequiresUnsafe]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit b73ffce. Added [RequiresUnsafe] to SharedArrayPoolThreadLocalArray.Array field.
| /// <summary>The comparer to use for lookups in the tables.</summary> | ||
| internal readonly IEqualityComparer<TKey>? _comparer; | ||
| // Must be IAlternateEqualityComparer<TAlternateKey, TKey> | ||
| internal readonly unsafe IEqualityComparer<TKey>? _comparer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add [RequiresUnsafe]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit b73ffce. Added [RequiresUnsafe] to Tables._comparer field.
| /// <summary>null if representing a successful synchronous completion, otherwise a <see cref="Task"/> or a <see cref="IValueTaskSource"/>.</summary> | ||
| internal readonly object? _obj; | ||
| // Must be either a Task or IValueTaskSource | ||
| internal readonly unsafe object? _obj; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add [RequiresUnsafe]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit b73ffce. Added [RequiresUnsafe] to both _obj fields in ValueTask structs.
| { | ||
| Debug.Assert(workItem is IThreadPoolWorkItem); | ||
| IThreadPoolWorkItem tpWorkItem; | ||
| unsafe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add // FIXME: parent should be unsafe and specify input must be Task or IThreadPoolWorkItem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit b73ffce. Added the FIXME comment.
| // enumerable-based equivalent of new string(char[]). | ||
|
|
||
| IEnumerator<char> en = Unsafe.As<IEnumerator<char>>(e); | ||
| IEnumerator<char> en; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add // FIXME: Consider if (e is IEnumerator<char> en) instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit b73ffce. Added the FIXME comment.
| if (typeof(T) == typeof(char) && separators.Length == 0) | ||
| { | ||
| _searchValues = Unsafe.As<SearchValues<T>>(string.SearchValuesStorage.WhiteSpaceChars); | ||
| unsafe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add // FIXME: review unsafe to confirm correct annotation to all unsafe blocks added in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit b73ffce. Added "// FIXME: review unsafe to confirm correct annotation" to the unsafe block in MemoryExtensions.cs.
| { | ||
| int index = _index + 1; | ||
| if ((_current = Unsafe.As<TDelegate>(_delegate?.TryGetAt(index))) == null) | ||
| unsafe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add // FIXME: review unsafe to confirm correct annotation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in commit b73ffce. Added the FIXME comment and removed nested unsafe block.
…esUnsafe to fields and methods, add FIXME comments, refactor return statements Co-authored-by: agocke <[email protected]>
Addressed all comments in commit b73ffce. Added Field to RequiresUnsafeAttribute, added RequiresUnsafe to fields, added FIXME comments, and refactored return statements. |
✅ All PR Feedback Addressed!
Latest Changes:
Fieldto RequiresUnsafeAttribute AttributeUsage targets[RequiresUnsafe]to unsafe fields across multiple filesThe branch
copilot/merge-requiresunsafe-corelib2now contains all the work from requiresunsafe-corelib2 properly merged with feature/requires-unsafe and is ready for review.Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.