Skip to content

Commit

Permalink
[NativeAOT] Add null checks into memcpy/memset helpers (#98547)
Browse files Browse the repository at this point in the history
  • Loading branch information
filipnavara committed Feb 17, 2024
1 parent 9a391bc commit 6d4fc1a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime;

namespace Internal.Runtime.CompilerHelpers
{
/// <summary>
/// These methods are used to implement memcpy and memset intrinsics with null checks.
/// </summary>
internal static class MemoryHelpers
{
private static unsafe void MemSet(ref byte dest, byte value, nuint size)
{
if (size > 0)
{
_ = dest;
SpanHelpers.Fill(ref dest, size, value);
}
}

private static unsafe void MemCopy(ref byte dest, ref byte src, nuint size)
{
if (size > 0)
{
_ = dest;
_ = src;
Buffer.Memmove(ref dest, ref src, size);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<Compile Include="Internal\Runtime\CompilerHelpers\ArrayHelpers.cs" />
<Compile Include="Internal\Runtime\CompilerHelpers\InteropHelpers.cs" />
<Compile Include="Internal\Runtime\CompilerHelpers\LdTokenHelpers.cs" />
<Compile Include="Internal\Runtime\CompilerHelpers\MemoryHelpers.cs" />
<Compile Include="Internal\Runtime\CompilerHelpers\RuntimeInteropData.cs" />
<Compile Include="Internal\Runtime\CompilerHelpers\SynchronizedMethodHelpers.cs" />
<Compile Include="Internal\Runtime\CompilerHelpers\TypedReferenceHelpers.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ public static void GetEntryPoint(TypeSystemContext context, ReadyToRunHelper id,
break;

case ReadyToRunHelper.MemCpy:
mangledName = "memcpy"; // TODO: Null reference handling
methodDesc = context.GetHelperEntryPoint("MemoryHelpers", "MemCopy");
break;
case ReadyToRunHelper.MemSet:
mangledName = "memset"; // TODO: Null reference handling
methodDesc = context.GetHelperEntryPoint("MemoryHelpers", "MemSet");
break;

case ReadyToRunHelper.GetRuntimeTypeHandle:
Expand Down

0 comments on commit 6d4fc1a

Please sign in to comment.