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] Workaround to align thread statics to 8 byte boundary #2609

Merged
merged 2 commits into from
Jun 12, 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 @@ -137,7 +137,13 @@ private static unsafe object AllocateThreadStaticStorageForType(TypeManagerHandl
gcDesc = Internal.Runtime.Augments.RuntimeAugments.TypeLoaderCallbacks.GetThreadStaticGCDescForDynamicType(typeManager, typeTlsIndex);
}

#if TARGET_WASM
// TODO-LLVM: Remove when there is an upstream fix. See https://github.com/dotnet/runtimelab/issues/2606 and https://github.com/dotnet/runtime/issues/103234
// This is a fix for aligning to 8, thread static fields that should be aligned 8, we just conservatively align everything to 8.
return InternalCalls.RhpNewFastAlign8((MethodTable*)gcDesc);
#else
return RuntimeImports.RhNewObject((MethodTable*)gcDesc);
#endif
}
}
}
45 changes: 45 additions & 0 deletions src/tests/nativeaot/SmokeTests/HelloWasm/HelloWasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ private static unsafe int Main(string[] args)

LSSATests.Run();

TestThreadStaticAlignment();

if (OperatingSystem.IsBrowser())
{
EventLoopTestClass.TestEventLoopIntegration();
Expand Down Expand Up @@ -4251,6 +4253,49 @@ static void TestGenStructContains()
EndTest(contains);
}

[InlineArray(3)]
internal struct ReturnArea
{
private ulong buffer;

internal unsafe nint AddressOfReturnArea()
{
return (nint)Unsafe.AsPointer(ref buffer);
}
}

internal class ThreadStaticAlignCheck1
{
[ThreadStatic]
[FixedAddressValueType]
internal static ReturnArea returnArea = default;
}

internal class Padder
{
private object o1;
}

internal class ThreadStaticAlignCheck2
{
[ThreadStatic]
[FixedAddressValueType]
internal static ReturnArea returnArea = default;
}

static void TestThreadStaticAlignment()
{
StartTest("Test ThreadStatic Alignment");

// Assume that these are allocated sequentially, use a padding object of size 12 ( mod 8 is not 0 ) to move the alignment of the second AddressOfReturnArea in case the first is
// coincidentally aligned 8.
var ts1Addr = ThreadStaticAlignCheck1.returnArea.AddressOfReturnArea();
var p = new Padder();
var ts2Addr = ThreadStaticAlignCheck2.returnArea.AddressOfReturnArea();

EndTest((((nint)ts1Addr) % 8 == 0) && (((nint)ts2Addr) % 8 == 0));
}

static ushort ReadUInt16()
{
// something with MSB set
Expand Down