Skip to content
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

[NativeAOT-LLVM] Don't use mmap in the frozen object manager and enable writeable data #2492

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ internal static bool SupportsWritableData
{
get
{
// For now just key this off of SupportsRelativePointer to avoid this on both CppCodegen and WASM.
return SupportsRelativePointers;
return true;
}
}

Expand Down Expand Up @@ -1184,7 +1183,7 @@ internal void* WritableData

uint offset = GetFieldOffset(EETypeField.ETF_WritableData);

if (!IsDynamicType)
if (!IsDynamicType && SupportsRelativePointers)
return (void*)GetField<RelativePointer>(offset).Value;
else
return (void*)GetField<Pointer>(offset).Value;
Expand Down
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.

#pragma warning disable IDE0060 // Remove unused parameter

using System.Runtime.InteropServices;

namespace Internal.Runtime
{
internal unsafe partial class FrozenObjectHeapManager
{
private static void* ClrVirtualReserve(nuint size)
{
void* alloc = Interop.Sys.AlignedAlloc(8, size);
if (alloc != null)
{
NativeMemory.Clear(alloc, size);
}
return alloc;
}

private static void* ClrVirtualCommit(void* pBase, nuint size)
{
// Already 'commited'.
return pBase;
}

private static void ClrVirtualFree(void* pBase, nuint size)
{
// This will only be called before an OOM. We have no way to do a partial unmap, so do not try.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ internal unsafe partial class FrozenObjectHeapManager
private readonly LowLevelLock m_Crst = new LowLevelLock();
private FrozenObjectSegment m_CurrentSegment;

#if TARGET_WASM
// WASM doesn't support reserving memory so we use a smaller size.
private const nuint FOH_SEGMENT_DEFAULT_SIZE = 64 * 1024;
#else
// Default size to reserve for a frozen segment
private const nuint FOH_SEGMENT_DEFAULT_SIZE = 4 * 1024 * 1024;
#endif
// Size to commit on demand in that reserved space
private const nuint FOH_COMMIT_SIZE = 64 * 1024;

Expand Down Expand Up @@ -119,7 +124,7 @@ public FrozenObjectSegment(nuint sizeHint)
{
m_Size = sizeHint;

Debug.Assert(m_Size > FOH_COMMIT_SIZE);
Debug.Assert(m_Size >= FOH_COMMIT_SIZE);
Debug.Assert(m_Size % FOH_COMMIT_SIZE == 0);

void* alloc = ClrVirtualReserve(m_Size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@
<Compile Include="System\Threading\ThreadPoolCallbackWrapper.NativeAot.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetsUnix)'=='true' or '$(TargetsBrowser)'=='true' or '$(TargetsWasi)'=='true'">
<Compile Include="Internal\Runtime\FrozenObjectHeapManager.Unix.cs" />
<Compile Include="Internal\Runtime\FrozenObjectHeapManager.Unix.cs" Condition="'$(TargetsWasm)' != 'true'" />
<Compile Include="Internal\Runtime\FrozenObjectHeapManager.Wasm.cs" Condition="'$(TargetsWasm)' == 'true'" />
<Compile Include="System\Environment.NativeAot.Unix.cs" />
<Compile Include="System\Runtime\InteropServices\NativeLibrary.NativeAot.Unix.cs" />
<Compile Include="System\Runtime\InteropServices\PInvokeMarshal.Unix.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public EETypeNode(NodeFactory factory, TypeDesc type)
factory.TypeSystemContext.EnsureLoadableType(type);
}

public static bool SupportsWritableData(TargetDetails target)
=> target.SupportsRelativePointers;
public static bool SupportsWritableData(TargetDetails target) => true;

public static bool SupportsFrozenRuntimeTypeInstances(TargetDetails target)
=> SupportsWritableData(target);
Expand Down Expand Up @@ -1125,11 +1124,17 @@ protected void OutputWritableData(NodeFactory factory, ref ObjectDataBuilder obj
{
if (_writableDataNode != null)
{
objData.EmitReloc(_writableDataNode, RelocType.IMAGE_REL_BASED_RELPTR32);
if (factory.Target.SupportsRelativePointers)
objData.EmitReloc(_writableDataNode, RelocType.IMAGE_REL_BASED_RELPTR32);
else
objData.EmitPointerReloc(_writableDataNode);
}
else if (SupportsWritableData(factory.Target))
{
objData.EmitInt(0);
if (factory.Target.SupportsRelativePointers)
objData.EmitInt(0);
else
objData.EmitZeroPointer();
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/coreclr/tools/aot/ILCompiler.LLVM/CodeGen/LLVMObjectWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ private void EmitObjectNode(ObjectNode node, ObjectData nodeContents)
if (nextRelocValid)
{
Relocation reloc = nodeContents.Relocs[relocIndex];
Debug.Assert(IsSupportedRelocType(node, reloc.RelocType), $"{reloc.RelocType} in {node} not supported");

long delta;
fixed (void* location = &data[reloc.Offset])
{
Expand Down Expand Up @@ -313,6 +315,16 @@ private void EmitObjectNode(ObjectNode node, ObjectData nodeContents)
}
}

private static bool IsSupportedRelocType(ObjectNode node, RelocType type)
{
if (node is StackTraceMethodMappingNode)
{
// Stack trace metadata uses relative pointers, but is currently unused.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have an issue about deleting it or making it work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - this is #2404.

return true;
}
return type is RelocType.IMAGE_REL_BASED_HIGHLOW;
}

private void EmitSymbolDef(LLVMValueRef baseSymbol, ReadOnlySpan<byte> symbolIdentifier, int offsetFromBaseSymbol)
{
LLVMValueRef symbolAddress = baseSymbol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ private static int Main()
TestReadOnlySpan.Run();
TestStaticInterfaceMethod.Run();
TestConstrainedCall.Run();
#if !CODEGEN_WASM
TestTypeHandles.Run();
#endif
TestIsValueType.Run();
TestIndirectLoads.Run();
TestInitBlock.Run();
Expand Down