dotnet / dotnet/linker

Linker appears to incorrectly trim static field initialization logic

Open
#1,876 6 comments 0 reactions 0 assignees View on GitHub
question
Dominant language
C#
Stars
392
Forks
128
Avg merge
2d 10h
Merged PRs (30d)
2

Description

The following simple program fails with a segmentation fault:
```xml


Exe
net6.0
false

```

```csharp
using System;

namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(DateTime.Now);
}
}
}
```

```sh
$ dotnet publish -c Release -r linux-x64 -p:PublishTrimmed=true -p:TrimMode=link
$ ./bin/Release/net6.0/linux-x64/publish/ConsoleApp7
Segmentation fault (core dumped)
```

This appears to be because `System.Globalization.GlobalizationMode` is trimmed down to just the following:
```csharp
namespace System.Globalization
{
internal static class GlobalizationMode
{
internal static bool Invariant => false;

internal static bool UseNls => false;
}
}
```

The original file is here: https://source.dot.net/#System.Private.CoreLib/GlobalizationMode.Unix.cs,f6b65aaea1440971

As you can see, the `Invariant` property has a field initializer which is getting trimmed where it should not be as it has necessary side effects. My guess is that the backing field that gets generated for such logic is getting trimmed because it is private and not returned anywhere: (https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA0ATEBqAPgAQCYBGAWACh8BmAAn2KTsJoGEaBvCm7mgSwDsALjCj8AhgBs6DGsAgQpASX4A3MVF5ihHGgHMYggNw0AvjQC8NAOIGrEiMEm8AXmMG8I/ZWo1bBAWQgMGAAKAEpDLh5oqO4ABw01YWlGOQVrW3tHCRc3Dy9VdU0hQODw2I4K6PwAdhoAM0kAZxhI8miTChMgA=)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.