Skip to content

Commit

Permalink
Change stack size on all Apple and desktop platforms to at least 1.5MB (
Browse files Browse the repository at this point in the history
#98007)

* Change stack size to 1.5MB on all desktop platforms & re-enable some tests

* Re-enable some tests, and respect `IlcDefaultStackSize` on Windows

- Re-enable disabled tests tracked by #1417 and #2084
- Make stack size configurable on NAOT via `IlcDefaultStackSize` on Windows
  • Loading branch information
hamarb123 authored Feb 12, 2024
1 parent 671a443 commit e5df956
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<LinkerFlavor Condition="'$(LinkerFlavor)' == '' and '$(_targetOS)' == 'freebsd'">lld</LinkerFlavor>
<LinkerFlavor Condition="'$(LinkerFlavor)' == '' and '$(_linuxLibcFlavor)' == 'bionic'">lld</LinkerFlavor>
<LinkerFlavor Condition="'$(LinkerFlavor)' == '' and '$(_targetOS)' == 'linux'">bfd</LinkerFlavor>
<IlcDefaultStackSize Condition="'$(_linuxLibcFlavor)' == 'musl'">1572864</IlcDefaultStackSize>
<IlcDefaultStackSize Condition="'$(IlcDefaultStackSize)' == '' and '$(_linuxLibcFlavor)' == 'musl'">1572864</IlcDefaultStackSize>
</PropertyGroup>

<Target Name="SetupOSSpecificProps" DependsOnTargets="$(IlcDynamicBuildPropertyDependencies)">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<LinkerSubsystem Condition="'$(OutputType)' == 'Exe' and '$(LinkerSubsystem)' == ''">CONSOLE</LinkerSubsystem>
<EventPipeName>eventpipe-disabled</EventPipeName>
<EventPipeName Condition="'$(EventSourceSupport)' == 'true'">eventpipe-enabled</EventPipeName>
<IlcDefaultStackSize Condition="'$(IlcDefaultStackSize)' == ''">1572864</IlcDefaultStackSize>
</PropertyGroup>

<!-- Ensure that runtime-specific paths have already been set -->
Expand Down Expand Up @@ -94,6 +95,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<LinkerArg Condition="'$(OutputType)' == 'WinExe' or '$(OutputType)' == 'Exe'" Include="/ENTRY:$(EntryPointSymbol) /NOEXP /NOIMPLIB" />
<LinkerArg Include="/NATVIS:&quot;$(MSBuildThisFileDirectory)NativeAOT.natvis&quot;" />
<LinkerArg Condition="'$(ControlFlowGuard)' == 'Guard'" Include="/guard:cf" />
<LinkerArg Condition="'$(OutputType)' == 'WinExe' or '$(OutputType)' == 'Exe'" Include="/STACK:$(IlcDefaultStackSize)" />
<!-- Do not warn if someone declares UnmanagedCallersOnly with an entrypoint of 'DllGetClassObject' and similar -->
<LinkerArg Include="/IGNORE:4104" />
</ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/coreclr/pal/src/init/pal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@ InitializeDefaultStackSize()
}
}

#ifdef HOST_APPLE
// Match Windows stack size
if (g_defaultStackSize == 0)
{
g_defaultStackSize = 1536 * 1024;
}
#endif

#ifdef ENSURE_PRIMARY_STACK_SIZE
if (g_defaultStackSize == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4475,7 +4475,6 @@ public static void DCS_TypeWithPrimitiveKnownTypes()
Assert.NotNull(actual);
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/1417", TestPlatforms.OSX)]
[SkipOnPlatform(TestPlatforms.Browser, "Causes a stack overflow")]
[Fact]
public static void DCS_DeeplyLinkedData()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ public static void RunLazyCancellationTests_Negative()
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/2084", TestRuntimes.Mono)]
[SkipOnPlatform(TestPlatforms.Browser, "Causes a stack overflow")]
public static void LongContinuationChain_ContinueWith_DoesNotStackOverflow()
{
const int DiveDepth = 12_000;
Expand Down
8 changes: 8 additions & 0 deletions src/native/libs/System.Native/pal_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ int32_t SystemNative_CreateThread(uintptr_t stackSize, void *(*startAddress)(voi
error = pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
assert(error == 0);

#ifdef HOST_APPLE
// Match Windows stack size
if (stackSize == 0)
{
stackSize = 1536 * 1024;
}
#endif

if (stackSize > 0)
{
if (stackSize < (uintptr_t)PTHREAD_STACK_MIN)
Expand Down
4 changes: 0 additions & 4 deletions src/tests/JIT/jit64/opt/cse/HugeArray1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
<!-- Needed for GCStressIncompatible -->
<RequiresProcessIsolation>true</RequiresProcessIsolation>
<GCStressIncompatible>true</GCStressIncompatible>

<!-- The test is too complex to compile on macOS where secondary threads have small stack size by default
and that is not enough for Roslyn, see https://github.com/dotnet/runtime/issues/87879 -->
<DisableProjectBuild Condition="'$(HostOS)' == 'osx'">true</DisableProjectBuild>
</PropertyGroup>
<PropertyGroup>
<DebugType>Full</DebugType>
Expand Down
4 changes: 0 additions & 4 deletions src/tests/JIT/jit64/opt/cse/hugeSimpleExpr1.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
<RequiresProcessIsolation>true</RequiresProcessIsolation>
<!-- Timeout on Arm64 -->
<GCStressIncompatible>true</GCStressIncompatible>

<!-- The test is too complex to compile on macOS where secondary threads have small stack size by default
and that is not enough for Roslyn, see https://github.com/dotnet/runtime/issues/87879 -->
<DisableProjectBuild Condition="'$(HostOS)' == 'osx'">true</DisableProjectBuild>
</PropertyGroup>
<PropertyGroup>
<DebugType>Full</DebugType>
Expand Down
4 changes: 0 additions & 4 deletions src/tests/JIT/jit64/opt/rngchk/RngchkStress2_o.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
<PropertyGroup>
<DebugType>PdbOnly</DebugType>
<Optimize>True</Optimize>

<!-- The test is too complex to compile on macOS where secondary threads have small stack size by default
and that is not enough for Roslyn, see https://github.com/dotnet/runtime/issues/87879 -->
<DisableProjectBuild Condition="'$(HostOS)' == 'osx'">true</DisableProjectBuild>
</PropertyGroup>
<ItemGroup>
<Compile Include="RngchkStress2.cs" />
Expand Down
39 changes: 39 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_87879/test87879.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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.Threading;
using System.Runtime.CompilerServices;
using Xunit;

namespace test87879;

public class test87879
{
[Fact, SkipLocalsInit]
public static void TestEntryPoint()
{
//determine the expected available stack size 1.5MB, minus a little bit (384kB) for overhead.
var expectedSize = 0x180000 - 0x60000;

//allocate on the stack as specified above
Span<byte> bytes = stackalloc byte[expectedSize];
Consume(bytes);
Console.WriteLine("Main thread succeeded.");

//repeat on a secondary thread
Thread t = new Thread([SkipLocalsInit] () =>
{
Span<byte> bytes = stackalloc byte[expectedSize];
Consume(bytes);
});
t.Start();
t.Join();
Console.WriteLine("Secondary thread succeeded.");
}

[MethodImpl(MethodImplOptions.NoInlining)]
static void Consume(Span<byte> bytes)
{
}
}
11 changes: 11 additions & 0 deletions src/tests/Regressions/coreclr/GitHub_87879/test87879.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RequiresProcessIsolation>true</RequiresProcessIsolation>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<CLRTestTargetUnsupported>true</CLRTestTargetUnsupported>
<CLRTestTargetUnsupported Condition="'$(TargetsWindows)' == 'true' OR '$(TargetsAppleMobile)' == 'true' OR ('$(TargetsUnix)' == 'true' AND '$(TargetsMobile)' != 'true')">false</CLRTestTargetUnsupported>
</PropertyGroup>
<ItemGroup>
<Compile Include="test87879.cs" />
</ItemGroup>
</Project>

0 comments on commit e5df956

Please sign in to comment.