-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Remove a number of calls to Unsafe.AsPointer
#99144
Comments
I'm generally supportive of removing unsafe code in favor of safer patterns, particularly where newer features like Notably a lot of these places where its being used are fairly core APIs, so there is risk in making changes to them even if to remove unsafe code. It's something that we need to be extra cautious around and ensure gets the right scrutiny. |
List of areas with calls to
|
Why do you think that this use is unnecessary? What would you replace it with (while preserving the perf characteristics). I do not see a problem with this one.
No much different from every other
Sounds good to me. I see ~5 places where this can be used.
I am not convinced that it is that easy. Many of these uses have to do with low-level interop. |
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices Issue DetailsThere are a number of unnecessary uses of
Most of the uses can be broken in to 2 categories:
The idea was mentioned by @tannergooding on the discord, and one of the suggested solutions was to create an internal API I am happy to make PRs for these. @tannergooding suggested that I make one for each area separately. Just making this issue to track it and get support for the general idea from people. /cc @stephentoub @jkotas @tannergooding
|
|
Ok this works but it requires pragma disable. |
Which file/type should I define this on? |
It can be internal method on |
Do we want to disable it globally like it was mentioned in #80484 (review)? |
This issue is itself about trying to reduce unsafe patterns where unnecessary. Globally suppressing the warning that you're taking a pointer to a managed object is kind-of counter intuitive to that ;) I think we want to be explicit when we're taking a pointer to a managed object to help reduce risk and make it very apparent we're taking the necessary approaches to keep the underlying object alive, etc. |
In my opinion, |
It's also not clear to me how the changes proposed here would have prevented the GC hole mentioned in the original post above. |
CS8500 is a bit of a silly warning anyway imo. The issue with e.g., consider I have this: SomeType v = ...;
...
CallMethod(&v); if I moved (or otherwise have a)
If we avoid using |
There are a number of unnecessary uses of
Unsafe.AsPointer
in this repository (e.g.runtime/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
Line 868 in eaa6f01
runtime/src/libraries/System.Private.CoreLib/src/System/Number.BigInteger.cs
Line 1237 in 9863bf1
Most of the uses can be broken in to 2 categories:
runtime/src/libraries/System.Private.CoreLib/src/System/Text/Ascii.cs
Line 144 in eaa6f01
runtime/src/mono/System.Private.CoreLib/src/System/Reflection/TypeNameParser.Mono.cs
Line 48 in eaa6f01
The idea was mentioned by @tannergooding on the discord, and one of the suggested solutions was to create an internal API
bool IsOpportunisticallyAligned(ref T address, nuint aligment)
or similar for the alignment uses.I am happy to make PRs for these. @tannergooding suggested that I make one for each area separately. Just making this issue to track it and get support for the general idea from people.
/cc @stephentoub @jkotas @tannergooding
The text was updated successfully, but these errors were encountered: