Skip to content

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Sep 19, 2025

Summary

This PR addresses community feedback on #48557 article + some cleanup

  1. Added more xrefs
  2. Inlined a couple of sub-bullets because they were not rendered nicely
  3. Added an example about RVA + AsPointer + ALC
  4. Non-zeroed fixed size buffers (see Fixed-size buffer is not zeroed runtime#119903 for more context)
  5. Reduced stackalloc size from int[512] to int[256] because previously we recommended to use 1024 bytes threshold
  6. added a new section 26. Compiler warnings

Internal previews

📄 File 🔗 Preview link
docs/standard/unsafe-code/best-practices.md docs/standard/unsafe-code/best-practices

@EgorBo
Copy link
Member Author

EgorBo commented Sep 19, 2025

@jkotas when you have a moment, could you please take a look at these changes?

@@ -947,7 +973,7 @@ While most of the suggestions in this document apply to interop scenarios as wel

## 23. Thread safety

See [Managed threading best practices](../../standard/threading/managed-threading-best-practices.md) and [.NET Memory Model](https://github.com/dotnet/runtime/blob/main/docs/design/specs/Memory-model.md).
Although memory safety and thread safety are orthogonal concepts, many of the highlighed issues in this document can lead to thread safety issues as well. For example, non-atomic coalesced writes. See [Managed threading best practices](../../standard/threading/managed-threading-best-practices.md) and [.NET Memory Model](https://github.com/dotnet/runtime/blob/main/docs/design/specs/Memory-model.md).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

many of the highlighed issues in this document

What are the bullet points that this is referring to?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was referring to

  1. Memory access coalescing
  2. Unaligned memory access

I don't have a strong opinion on this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would either omit it; or make it more concrete. As written, it raises more questions than answers.

@@ -961,6 +987,24 @@ In the context of the unsafe code it's important to keep in mind:

Fuzz testing (or "fuzzing") is an automated software testing technique that involves providing invalid, unexpected, or random data as inputs to a computer program. It provides a way to detect memory safety issues in code that may have gaps in test coverage. You can use tools like [SharpFuzz](https://github.com/Metalnem/sharpfuzz) to set up fuzz testing for .NET code.

## 26. Compiler warnings

Generally, Roslyn intentionally doesn't provide extensive support such as warnings and analyzers around incorrect unsafe code usage. However, there are some existing warnings that can help detect potential issues and should not be ignored or suppressed without careful consideration. Some examples include:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Generally, Roslyn intentionally doesn't provide extensive support such as warnings and analyzers around incorrect unsafe code usage. However, there are some existing warnings that can help detect potential issues and should not be ignored or suppressed without careful consideration. Some examples include:
Generally, C# compiler doesn't provide extensive support such as warnings and analyzers around incorrect unsafe code usage. However, there are some existing warnings that can help detect potential issues and should not be ignored or suppressed without careful consideration. Some examples include:

@@ -947,7 +973,7 @@ While most of the suggestions in this document apply to interop scenarios as wel

## 23. Thread safety

See [Managed threading best practices](../../standard/threading/managed-threading-best-practices.md) and [.NET Memory Model](https://github.com/dotnet/runtime/blob/main/docs/design/specs/Memory-model.md).
Although memory safety and thread safety are orthogonal concepts, many of the highlighed issues in this document can lead to thread safety issues as well. For example, non-atomic coalesced writes. See [Managed threading best practices](../../standard/threading/managed-threading-best-practices.md) and [.NET Memory Model](https://github.com/dotnet/runtime/blob/main/docs/design/specs/Memory-model.md).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would either omit it; or make it more concrete. As written, it raises more questions than answers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants