Skip to content

Commit

Permalink
Merge branch 'main' into feature/system.io.mmf/unix-memfdcreate
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 authored Jul 20, 2024
2 parents 996cc30 + f26dbf0 commit ca5b970
Show file tree
Hide file tree
Showing 42 changed files with 1,496 additions and 729 deletions.
50 changes: 29 additions & 21 deletions src/coreclr/jit/inductionvariableopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1845,16 +1845,32 @@ bool StrengthReductionContext::CheckAdvancedCursors(ArrayStack<CursorInfo>* curs
//
bool StrengthReductionContext::StaysWithinManagedObject(ArrayStack<CursorInfo>* cursors, ScevAddRec* addRec)
{
int64_t offset;
Scev* baseScev = addRec->Start->PeelAdditions(&offset);
offset = static_cast<target_ssize_t>(offset);
ValueNumPair addRecStartVNP = m_scevContext.MaterializeVN(addRec->Start);
if (!addRecStartVNP.BothDefined())
{
return false;
}

ValueNumPair addRecStartBase = addRecStartVNP;
target_ssize_t offsetLiberal = 0;
target_ssize_t offsetConservative = 0;
m_comp->vnStore->PeelOffsets(addRecStartBase.GetLiberalAddr(), &offsetLiberal);
m_comp->vnStore->PeelOffsets(addRecStartBase.GetConservativeAddr(), &offsetConservative);

if (offsetLiberal != offsetConservative)
{
return false;
}

target_ssize_t offset = offsetLiberal;

// We only support arrays and strings here. To strength reduce Span<T>
// accesses we need additional properies on the range designated by a
// Span<T> that we currently do not specify, or we need to prove that the
// byref we may form in the IV update would have been formed anyway by the
// loop.
if (!baseScev->OperIs(ScevOper::Local) || !baseScev->TypeIs(TYP_REF))
// We only support objects here (targeting array/strings). To strength
// reduce Span<T> accesses we need additional properties on the range
// designated by a Span<T> that we currently do not specify, or we need to
// prove that the byref we may form in the IV update would have been formed
// anyway by the loop.
if ((m_comp->vnStore->TypeOfVN(addRecStartBase.GetConservative()) != TYP_REF) ||
(m_comp->vnStore->TypeOfVN(addRecStartBase.GetLiberal()) != TYP_REF))
{
return false;
}
Expand Down Expand Up @@ -1888,22 +1904,14 @@ bool StrengthReductionContext::StaysWithinManagedObject(ArrayStack<CursorInfo>*
return false;
}

ScevLocal* local = (ScevLocal*)baseScev;

ValueNumPair vnp = m_scevContext.MaterializeVN(baseScev);
if (!vnp.BothDefined())
{
return false;
}

BasicBlock* preheader = m_loop->EntryEdge(0)->getSourceBlock();
if (!m_comp->optAssertionVNIsNonNull(vnp.GetConservative(), preheader->bbAssertionOut))
if (!m_comp->optAssertionVNIsNonNull(addRecStartBase.GetConservative(), preheader->bbAssertionOut))
{
return false;
}

// We have a non-null array/string. Check that the 'start' offset looks
// fine. TODO: We could also use assertions on the length of the
// We have a non-null object as the base. Check that the 'start' offset
// looks fine. TODO: We could also use assertions on the length of the
// array/string. E.g. if we know the length of the array is > 3, then we
// can allow the add rec to have a later start. Maybe range check can be
// used?
Expand All @@ -1914,7 +1922,7 @@ bool StrengthReductionContext::StaysWithinManagedObject(ArrayStack<CursorInfo>*

// Now see if we have a bound that guarantees that we iterate fewer times
// than the array/string's length.
ValueNum arrLengthVN = m_comp->vnStore->VNForFunc(TYP_INT, VNF_ARR_LENGTH, vnp.GetLiberal());
ValueNum arrLengthVN = m_comp->vnStore->VNForFunc(TYP_INT, VNF_ARR_LENGTH, addRecStartBase.GetLiberal());

for (int i = 0; i < m_backEdgeBounds.Height(); i++)
{
Expand Down
36 changes: 36 additions & 0 deletions src/coreclr/jit/valuenum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14998,3 +14998,39 @@ CORINFO_CLASS_HANDLE ValueNumStore::GetObjectType(ValueNum vn, bool* pIsExact, b

return NO_CLASS_HANDLE;
}

//--------------------------------------------------------------------------------
// PeelOffsets: Peel all additions with a constant offset away from the
// specified VN.
//
// Arguments:
// vn - [in, out] The VN. Will be modified to the base VN that the offsets are added to.
// offset - [out] The offsets peeled out of the VNF_ADD funcs.
//
void ValueNumStore::PeelOffsets(ValueNum* vn, target_ssize_t* offset)
{
#ifdef DEBUG
var_types vnType = TypeOfVN(*vn);
assert((vnType == TYP_I_IMPL) || (vnType == TYP_REF) || (vnType == TYP_BYREF));
#endif

*offset = 0;
VNFuncApp app;
while (GetVNFunc(*vn, &app) && (app.m_func == VNF_ADD))
{
if (IsVNConstantNonHandle(app.m_args[0]))
{
*offset += ConstantValue<target_ssize_t>(app.m_args[0]);
*vn = app.m_args[1];
}
else if (IsVNConstantNonHandle(app.m_args[1]))
{
*offset += ConstantValue<target_ssize_t>(app.m_args[1]);
*vn = app.m_args[0];
}
else
{
break;
}
}
}
2 changes: 2 additions & 0 deletions src/coreclr/jit/valuenum.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ class ValueNumStore

CORINFO_CLASS_HANDLE GetObjectType(ValueNum vn, bool* pIsExact, bool* pIsNonNull);

void PeelOffsets(ValueNum* vn, target_ssize_t* offset);

// And the single constant for an object reference type.
static ValueNum VNForNull()
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Crypto
{
[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_EcDsaSignHash(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
ref byte hash,
int hashLength,
ref byte destination,
int destinationLength);

internal static int EcDsaSignHash(
SafeEvpPKeyHandle pkey,
ReadOnlySpan<byte> hash,
Span<byte> destination)
{
int written = CryptoNative_EcDsaSignHash(
pkey,
pkey.ExtraHandle,
ref MemoryMarshal.GetReference(hash),
hash.Length,
ref MemoryMarshal.GetReference(destination),
destination.Length);

if (written < 0)
{
Debug.Assert(written == -1);
throw CreateOpenSslCryptographicException();
}

return written;
}

[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_EcDsaVerifyHash(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
ref byte hash,
int hashLength,
ref byte signature,
int signatureLength);

internal static bool EcDsaVerifyHash(
SafeEvpPKeyHandle pkey,
ReadOnlySpan<byte> hash,
ReadOnlySpan<byte> signature)
{
int ret = CryptoNative_EcDsaVerifyHash(
pkey,
pkey.ExtraHandle,
ref MemoryMarshal.GetReference(hash),
hash.Length,
ref MemoryMarshal.GetReference(signature),
signature.Length);

if (ret == 1)
{
return true;
}

if (ret == 0)
{
return false;
}

Debug.Assert(ret == -1);
throw CreateOpenSslCryptographicException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ internal static partial class Crypto
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeyGetEcKey")]
internal static partial SafeEcKeyHandle EvpPkeyGetEcKey(SafeEvpPKeyHandle pkey);

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeySetEcKey")]
[LibraryImport(Libraries.CryptoNative)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool EvpPkeySetEcKey(SafeEvpPKeyHandle pkey, SafeEcKeyHandle key);
private static partial bool CryptoNative_EvpPkeySetEcKey(SafeEvpPKeyHandle pkey, SafeEcKeyHandle key);

// Calls EVP_PKEY_set1_EC_KEY therefore the key will be duplicated
internal static SafeEvpPKeyHandle CreateEvpPkeyFromEcKey(SafeEcKeyHandle key)
{
SafeEvpPKeyHandle pkey = Interop.Crypto.EvpPkeyCreate();
if (!CryptoNative_EvpPkeySetEcKey(pkey, key))
{
pkey.Dispose();
throw Interop.Crypto.CreateOpenSslCryptographicException();
}

return pkey;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,35 @@ internal static partial class Interop
{
internal static partial class Crypto
{
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxCreate")]
internal static partial SafeEvpPKeyCtxHandle EvpPKeyCtxCreate(SafeEvpPKeyHandle pkey, SafeEvpPKeyHandle peerkey, out uint secretLength);

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyDeriveSecretAgreement")]
private static partial int EvpPKeyDeriveSecretAgreement(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
SafeEvpPKeyHandle peerKey,
ref byte secret,
uint secretLength,
SafeEvpPKeyCtxHandle ctx);

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxDestroy")]
internal static partial void EvpPKeyCtxDestroy(IntPtr ctx);
uint secretLength);

internal static void EvpPKeyDeriveSecretAgreement(SafeEvpPKeyCtxHandle ctx, Span<byte> destination)
internal static int EvpPKeyDeriveSecretAgreement(SafeEvpPKeyHandle pkey, SafeEvpPKeyHandle peerKey, Span<byte> destination)
{
Debug.Assert(ctx != null);
Debug.Assert(!ctx.IsInvalid);
Debug.Assert(pkey != null);
Debug.Assert(!pkey.IsInvalid);
Debug.Assert(peerKey != null);
Debug.Assert(!peerKey.IsInvalid);

int ret = EvpPKeyDeriveSecretAgreement(
int written = EvpPKeyDeriveSecretAgreement(
pkey,
pkey.ExtraHandle,
peerKey,
ref MemoryMarshal.GetReference(destination),
(uint)destination.Length,
ctx);
(uint)destination.Length);

if (ret != 1)
if (written <= 0)
{
Debug.Assert(written == 0);
throw CreateOpenSslCryptographicException();
}

return written;
}
}
}
Loading

0 comments on commit ca5b970

Please sign in to comment.