-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add language reference material for unsafe updates for memory safety
#54367
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
Draft
BillWagner
wants to merge
3
commits into
dotnet:main
Choose a base branch
from
BillWagner:unsafe-2-reference
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
docs/csharp/language-reference/snippets/memory-safety/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Entry point so the snippet project produces an executable and is verified by a build. | ||
| MemorySafety.Relaxations.CreatePointer(); | ||
| MemorySafety.Relaxations.PinArray([1, 2, 3]); | ||
| MemorySafety.Relaxations.AllocateOnStack(); | ||
| System.Console.WriteLine(MemorySafety.Relaxations.SizeOfStruct()); | ||
| System.Console.WriteLine(MemorySafety.Relaxations.ReadValue([10, 20, 30])); |
61 changes: 61 additions & 0 deletions
61
docs/csharp/language-reference/snippets/memory-safety/Relaxations.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // This file demonstrates the C# 15 memory safety relaxations verified against | ||
| // .NET 11 Preview 5. Creating pointers, the fixed statement, stackalloc-to-pointer, | ||
| // and sizeof no longer require an unsafe context. Only operations that access the | ||
| // pointed-to memory still require one. | ||
|
|
||
| namespace MemorySafety; | ||
|
|
||
| public class Relaxations | ||
| { | ||
| // <CreatePointer> | ||
| public static void CreatePointer() | ||
| { | ||
| int value = 42; | ||
| // Creating a pointer doesn't require an unsafe context. | ||
| int* pointer = &value; | ||
| int** pointerToPointer = &pointer; | ||
| } | ||
| // </CreatePointer> | ||
|
|
||
| // <FixedStatement> | ||
| public static void PinArray(int[] numbers) | ||
| { | ||
| // The fixed statement no longer requires an unsafe context. | ||
| fixed (int* first = numbers) | ||
| { | ||
| int* current = first; | ||
| } | ||
| } | ||
| // </FixedStatement> | ||
|
|
||
| // <StackallocToPointer> | ||
| public static void AllocateOnStack() | ||
| { | ||
| // Converting a stackalloc to a pointer no longer requires an unsafe context. | ||
| int* buffer = stackalloc int[10]; | ||
| } | ||
| // </StackallocToPointer> | ||
|
|
||
| // <SizeOf> | ||
| public static int SizeOfStruct() | ||
| { | ||
| // sizeof of any unmanaged type no longer requires an unsafe context. | ||
| return sizeof(System.Guid); | ||
| } | ||
| // </SizeOf> | ||
|
|
||
| // <Dereference> | ||
| public static int ReadValue(int[] numbers) | ||
| { | ||
| fixed (int* first = numbers) | ||
| { | ||
| // Dereferencing a pointer accesses unmanaged memory, so it still | ||
| // requires an unsafe context. | ||
| unsafe | ||
| { | ||
| return *first; | ||
| } | ||
| } | ||
| } | ||
| // </Dereference> | ||
| } |
12 changes: 12 additions & 0 deletions
12
docs/csharp/language-reference/snippets/memory-safety/memory-safety.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net11.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <LangVersion>preview</LangVersion> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| </PropertyGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.