Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion accepted/2025/memory-safety/caller-unsafe.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ public sealed class RequiresUnsafeAttribute : System.Attribute

### Global invariants

Two properties which should always hold in .NET programs are:
Properties which should always hold in .NET programs are:

* Memory safety
* No access to uninitialized memory
* Managed references are either null, or always valid to dereference, both as references and if converted to a pointer of the same type

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is valid for a managed reference to point to the end of the array. It is valid unsafe use of managed references.

https://github.com/dotnet/runtime/blob/main/docs/design/specs/Ecma-335-Augments.md goes into detail about this. (look for "Managed pointers which point at the address just past the end of an object, or the address where an element just past the end of an array would be stored, are permitted but not dereferenceable.")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, managed references are particularly weird in that they have separate rules for safe code and unsafe code:

  1. Some things are valid everywhere (normal managed references created by safe APIs).
  2. Some thigns are only valid in unsafe code (unaligned managed references).
  3. Some things are never valid (managed references pointing to invalid memory).


The "safe" subset of C# must guarantee these properties by construction. The unsafe subset cannot be guaranteed entirely by the system -- it needs external validation by the user or other tooling.

Expand Down
Loading