forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundedMemory.Creation.cs
99 lines (88 loc) · 4.35 KB
/
BoundedMemory.Creation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Runtime.InteropServices;
namespace System.Buffers
{
/// <summary>
/// Contains factory methods to create <see cref="BoundedMemory{T}"/> instances.
/// </summary>
public static partial class BoundedMemory
{
public static bool UnixBoundsEnabled { get; set; }
private static readonly int SystemPageSize = Environment.SystemPageSize;
/// <summary>
/// Allocates a new <see cref="BoundedMemory{T}"/> region which is immediately preceded by
/// or immediately followed by a poison (MEM_NOACCESS) page. If <paramref name="placement"/>
/// is <see cref="PoisonPagePlacement.Before"/>, then attempting to read the memory
/// immediately before the returned <see cref="BoundedMemory{T}"/> will result in an AV.
/// If <paramref name="placement"/> is <see cref="PoisonPagePlacement.After"/>, then
/// attempting to read the memory immediately after the returned <see cref="BoundedMemory{T}"/>
/// will result in AV.
/// </summary>
/// <remarks>
/// The newly-allocated memory will be populated with random data.
/// </remarks>
public static BoundedMemory<T> Allocate<T>(int elementCount, PoisonPagePlacement placement = PoisonPagePlacement.After) where T : unmanaged
{
if (elementCount < 0)
{
throw new ArgumentOutOfRangeException(nameof(elementCount));
}
if (placement != PoisonPagePlacement.Before && placement != PoisonPagePlacement.After)
{
throw new ArgumentOutOfRangeException(nameof(placement));
}
BoundedMemory<T> retVal = AllocateWithoutDataPopulation<T>(elementCount, placement);
FillRandom(MemoryMarshal.AsBytes(retVal.Span));
return retVal;
}
/// <summary>
/// Similar to <see cref="Allocate(int, PoisonPagePlacement)"/>, but populates the allocated
/// native memory block from existing data rather than using random data.
/// </summary>
public static BoundedMemory<T> AllocateFromExistingData<T>(ReadOnlySpan<T> data, PoisonPagePlacement placement = PoisonPagePlacement.After) where T : unmanaged
{
if (placement != PoisonPagePlacement.Before && placement != PoisonPagePlacement.After)
{
throw new ArgumentOutOfRangeException(nameof(placement));
}
BoundedMemory<T> retVal = AllocateWithoutDataPopulation<T>(data.Length, placement);
data.CopyTo(retVal.Span);
return retVal;
}
/// <summary>
/// Similar to <see cref="Allocate(int, PoisonPagePlacement)"/>, but populates the allocated
/// native memory block from existing data rather than using random data.
/// </summary>
public static BoundedMemory<T> AllocateFromExistingData<T>(T[] data, PoisonPagePlacement placement = PoisonPagePlacement.After) where T : unmanaged
{
return AllocateFromExistingData(new ReadOnlySpan<T>(data), placement);
}
private static void FillRandom(Span<byte> buffer)
{
// Loop over a Random instance manually since Random.NextBytes(Span<byte>) doesn't
// exist on all platforms we target.
Random random = new Random(); // doesn't need to be cryptographically strong
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte)random.Next();
}
}
private static BoundedMemory<T> AllocateWithoutDataPopulation<T>(int elementCount, PoisonPagePlacement placement) where T : unmanaged
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return AllocateWithoutDataPopulationWindows<T>(elementCount, placement);
}
#if NETFRAMEWORK
return AllocateWithoutDataPopulationDefault<T>(elementCount, placement);
#else
else if (OperatingSystem.IsBrowser() || OperatingSystem.IsWasi())
{
return AllocateWithoutDataPopulationDefault<T>(elementCount, placement);
}
return AllocateWithoutDataPopulationUnix<T>(elementCount, placement);
#endif
}
}
}