Skip to content

Commit 6d4fc1a

Browse files
authored
[NativeAOT] Add null checks into memcpy/memset helpers (#98547)
1 parent 9a391bc commit 6d4fc1a

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime;
6+
7+
namespace Internal.Runtime.CompilerHelpers
8+
{
9+
/// <summary>
10+
/// These methods are used to implement memcpy and memset intrinsics with null checks.
11+
/// </summary>
12+
internal static class MemoryHelpers
13+
{
14+
private static unsafe void MemSet(ref byte dest, byte value, nuint size)
15+
{
16+
if (size > 0)
17+
{
18+
_ = dest;
19+
SpanHelpers.Fill(ref dest, size, value);
20+
}
21+
}
22+
23+
private static unsafe void MemCopy(ref byte dest, ref byte src, nuint size)
24+
{
25+
if (size > 0)
26+
{
27+
_ = dest;
28+
_ = src;
29+
Buffer.Memmove(ref dest, ref src, size);
30+
}
31+
}
32+
}
33+
}

src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="Internal\Runtime\CompilerHelpers\ArrayHelpers.cs" />
101101
<Compile Include="Internal\Runtime\CompilerHelpers\InteropHelpers.cs" />
102102
<Compile Include="Internal\Runtime\CompilerHelpers\LdTokenHelpers.cs" />
103+
<Compile Include="Internal\Runtime\CompilerHelpers\MemoryHelpers.cs" />
103104
<Compile Include="Internal\Runtime\CompilerHelpers\RuntimeInteropData.cs" />
104105
<Compile Include="Internal\Runtime\CompilerHelpers\SynchronizedMethodHelpers.cs" />
105106
<Compile Include="Internal\Runtime\CompilerHelpers\TypedReferenceHelpers.cs" />

src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/JitHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ public static void GetEntryPoint(TypeSystemContext context, ReadyToRunHelper id,
134134
break;
135135

136136
case ReadyToRunHelper.MemCpy:
137-
mangledName = "memcpy"; // TODO: Null reference handling
137+
methodDesc = context.GetHelperEntryPoint("MemoryHelpers", "MemCopy");
138138
break;
139139
case ReadyToRunHelper.MemSet:
140-
mangledName = "memset"; // TODO: Null reference handling
140+
methodDesc = context.GetHelperEntryPoint("MemoryHelpers", "MemSet");
141141
break;
142142

143143
case ReadyToRunHelper.GetRuntimeTypeHandle:

0 commit comments

Comments
 (0)