Repository navigation
Open
Description
Per the description in https://github.com/dotnet/designs/blob/main/accepted/2025/memory-safety/caller-unsafe.md, we need to annotate functions that should be considered unsafe.
Links to API Proposals:
- [API Proposal]: Mark Unsafe as caller-unsafe #126956
- [API Proposal]: Mark MemoryMarshal APIs as caller-unsafe #127098
- [API Proposal]: Mark System.Runtime.InteropServices APIs as caller-unsafe #129750
- [API Proposal]: Mark Vector-related APIs as caller-unsafe #128075
- [API Proposal]: Batch #1 of caller-unsafe APIs #129751
Criteria
The doc defines a safe API when it meets two criteria:
- Code never accesses memory that is not managed by the runtime
- No access to uninitialized memory
Here are a few more fine-grained criteria based on those two:
- May create a potentially incorrect managed pointer. Examples include all
Unsafe.Add*andUnsafe.Subtract*. Such pointers may crash an app even when not directly dereferenced anywhere. - May create a managed pointer that should never be directly dereferenced. Example:
MemoryMarshal.GetArrayDataReferenceover an empty array. - Dereferences a caller-supplied unmanaged address - reads or writes through a raw
IntPtr/nint/T*parameter (or any kind of wrapper with those e.g.SafeHandle) directly or just lets it flow into a system where it might be dereferenced later. - May strip readonly-ness from a pointer. Example:
Unsafe.AsRefremoving readonly and making a pointer to a readonly memory (e.g. RVA) writeable. This also may violate security invariants a type may hold. - May read uninitialized data. Examples include reading padding/alignment in structs via various
MemoryMarshalAPIs,Unsafe.BitCast,Unsafe.As. Also, variousUnsafe.SkipInit,GC.AllocateUninitialized*, etc. - May set an unsafe field without an
unsafecontext. A good example isMemoryMarshal.Read<T>(ROS<byte>)where T might theoretically have an unsafe field that this API lets you set without anunsafecontext. - Produces a misaligned pointer from a properly aligned input. Examples:
MemoryMarshal.Cast,[StructLayout(LayoutKind.Explicit)].
Edge-cases
A section for edge-cases if any.
- Sometimes it's just not realistic to mark some widely used interface as unsafe e.g.
IDisposable.Disposeto make some particular dangerous Dispose unsafe to call. In that case we need annotate all APIs that create that kind object as unsafe. Example:GCHandle<T>.Disposemay lead to a global state corruption if called twice (e.g. on two copies of the same handle). - All
extern/[LibraryImport]/InternalCallAPIs are considered unsafe by default unless explicitly annotated withsafekeyword.
Out of scope
- Reflection. Eventually we will likely make it mostly unsafe, but today it's completely out of scope.
- Shapes considered safe today because C# doesn't have lifetimes/ownership, allows static mutable state, etc.
- Thread-safety (although there are some intersections, it's generally considered orthogonal to memory safety)
- APIs in OOB packages (for now)
Activity
hamarb123 commentedon Mar 3, 2026
This definition seems problematic to me. What about this definition says I should have to mark the following as
[RequiresUnsafe]:Shouldn't it also discuss that it's not memory-safe to:
(object)x is MyTypewould givefalse(maybe this also interacts with IDIC, not certain)None of these seem to be memory-unsafe by the unsafe definition, if I do not "access them" (which would then imply that if I can create them safely, that any potential accessing must have to have
[RequiresUnsafe]if it would break the definition, which seems problematic also) - but they are actually unsafe (and can lead to AV's & etc.).If it's intended to be implied by the definition, imo it would be great to add these as examples so there's no ambiguity it's meant to be covered.
agocke commentedon Mar 4, 2026
That is not the operative definition for your use case because you are not manipulating the memory directly. You are using Unsafe.As. For your function to be safe you must follow the stated rules on Unsafe.As. The remarks on Unsafe.As state:
So your function is correct only if you use
Unsafe.Asfor casts that would otherwise be valid in the language.EgorBo commentedon Mar 4, 2026
vyrp commentedon Jun 25, 2026
[-]Annotate libraries with RequiresUnsafe attribute[/-][+]Annotate libraries with caller-unsafe keyword[/+]jeffhandley commentedon Sep 9, 2026
@EgorBo I moved this to .NET 12. Please pull back in to .NET 11 if that's still the target.