Skip to content

NRE in System.Text.RegularExpresssions on ARM64 #78340

Closed

Description

@anmalkov

Steps to reproduce

  1. Create a new app in VS from template "ASP.NET Core with React.js"
  2. Name a project and solution as "testspa"
  3. Select .NET 7
  4. Create Dockerfile in the solution file directory:
FROM mcr.microsoft.com/dotnet/aspnet:7.0-jammy-arm64v8 AS base
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y libpng-dev libjpeg-dev curl libxi6 build-essential libgl1-mesa-glx
RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash -
RUN apt-get install -y nodejs
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0-jammy-arm64v8 AS build
RUN apt-get update
RUN apt-get install -y curl
RUN apt-get install -y libpng-dev libjpeg-dev curl libxi6 build-essential libgl1-mesa-glx
RUN curl -sL https://deb.nodesource.com/setup_lts.x | bash -
RUN apt-get install -y nodejs
WORKDIR /src
COPY ["testspa/testspa.csproj", "testspa/"]
RUN dotnet restore "testspa/testspa.csproj"
COPY . .
WORKDIR "/src/testspa"
RUN dotnet build "testspa.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "testspa.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "testspa.dll"]
  1. Open terminal and go to solution directory
  2. Build an image:
docker build -t testspa .

Expected result:

A solution should be built and container image should be created

Actual result:

dotnet restore command fails with:

------
 > [build  9/12] RUN dotnet restore "testspa/testspa.csproj":
dotnet/msbuild#21 15.85 MSBUILD : error : This is an unhandled exception in MSBuild -- PLEASE UPVOTE AN EXISTING ISSUE OR FILE A NEW ONE AT https://aka.ms/msbuild/unhandled. [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :     System.NullReferenceException: Object reference not set to an instance of an object. [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Regex1_Scan(RegexRunner, ReadOnlySpan`1) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at System.Text.RegularExpressions.Regex.ScanInternal(RegexRunnerMode mode, Boolean reuseMatchObject, String input, Int32 beginning, RegexRunner runner, ReadOnlySpan`1 span, Boolean returnNullIfReuseMatchObject) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at System.Text.RegularExpressions.Regex.RunAllMatchesWithCallback[TState](String inputString, ReadOnlySpan`1 inputSpan, Int32 startat, TState& state, MatchCallback`1 callback, RegexRunnerMode mode, Boolean reuseMatchObject) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at System.Text.RegularExpressions.Regex.RunAllMatchesWithCallback[TState](String input, Int32 startat, TState& state, MatchCallback`1 callback, RegexRunnerMode mode, Boolean reuseMatchObject) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at System.Text.RegularExpressions.Regex.Replace(MatchEvaluator evaluator, Regex regex, String input, Int32 count, Int32 startat) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.Evaluation.Expander`2.MetadataExpander.ExpandMetadataLeaveEscaped(String expression, IMetadataTable metadata, ExpanderOptions options, IElementLocation elementLocation) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.Evaluation.Expander`2.ExpandIntoStringLeaveEscaped(String expression, ExpanderOptions options, IElementLocation elementLocation, LoggingContext loggingContext) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.EvaluateItemDefinitionElement(ProjectItemDefinitionElement itemDefinitionElement) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.EvaluateItemDefinitionGroupElement(ProjectItemDefinitionGroupElement itemDefinitionGroupElement) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.Evaluate() [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.Evaluate(IEvaluatorData`4 data, Project project, ProjectRootElement root, ProjectLoadSettings loadSettings, Int32 maxNodeCount, PropertyDictionary`1 environmentProperties, ILoggingService loggingService, IItemFactory`2 itemFactory, IToolsetProvider toolsetProvider, ProjectRootElementCacheBase projectRootElementCache, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, EvaluationContext evaluationContext, Boolean interactive) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.Execution.ProjectInstance.Initialize(ProjectRootElement xml, IDictionary`2 globalProperties, String explicitToolsVersion, String explicitSubToolsetVersion, Int32 visualStudioVersionFromSolution, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, Nullable`1 projectLoadSettings, EvaluationContext evaluationContext) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.Execution.ProjectInstance..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, Nullable`1 projectLoadSettings) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.BackEnd.BuildRequestConfiguration.<>c__DisplayClass60_0.<LoadProjectIntoConfiguration>b__0() [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.BackEnd.BuildRequestConfiguration.InitializeProject(BuildParameters buildParameters, Func`1 loadProjectFromFile) [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.BackEnd.RequestBuilder.BuildProject() [/src/testspa/testspa.csproj]
dotnet/msbuild#21 15.85 MSBUILD : error :    at Microsoft.Build.BackEnd.RequestBuilder.BuildAndReport() [/src/testspa/testspa.csproj]
------

Debian

If you try to do the same but with the base image with tag 7.0-bullseye-slim-arm64v8 which is based on Debian, the dotnet restore command also fails with:

------
 > [build  9/12] RUN dotnet restore "testspa/testspa.csproj":
dotnet/msbuild#22 17.28 /usr/share/dotnet/sdk/7.0.100/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(55,5): error MSB4184: The expression "[MSBuild]::GetTargetFrameworkVersion(net7.0, 2)" cannot be evaluated. Exception has been thrown by the target of an invocation. [/src/testspa/testspa.csproj]
------

.NET 7 on Linux amd64

Everything works well if I use the base images with the tag "7.0" which is targeted amd64. So, it is an ARM issue.

.NET 6 on ARM

If you run the same on .NET6 everything works perfectly with Ubuntu and Debian base images for ARM64v8.

Activity

  1. rainersigwald commented on Nov 14, 2022

    @rainersigwald
    Member

    @anmalkov is the host machine you're running on ARM64, or are you using Docker's built in qemu ARM64 emulation?

    The expression "[MSBuild]::GetTargetFrameworkVersion(net7.0, 2)" cannot be evaluated. Exception has been thrown by the target of an invocation.

    This one is tracked by NuGet/Home#12227.

  2. rainersigwald commented on Nov 14, 2022

    @rainersigwald
    Member

    I don't see anything in the MSBuild-layer calls to RegularExpression.Replace that look fishy (or have been touched in a couple years)

    https://github.com/dotnet/msbuild/blob/d14b74d6f08ab7f2e17706359a38f8c22b569cb5/src/Build/Evaluation/Expander.cs#L901-L957

    So I'm going to rename + reroute this to the regex folks in dotnet/runtime.

  3. changed the title [-]Unhandled error while building .NET 7 ASP.NET Core with React.js app on ARM64v8[/-] [+]NRE in System.Text.RegularExpresssions on ARM64[/+] on Nov 14, 2022
  4. ghost added
    untriaged
    on Nov 14, 2022
  5. added this to the 8.0.0 milestone on Nov 16, 2022
  6. ghost removed
    untriaged
    on Nov 16, 2022
  7. anmalkov commented on Nov 17, 2022

    @anmalkov
    Author

    @anmalkov is the host machine you're running on ARM64, or are you using Docker's built in qemu ARM64 emulation?

    I'm building the image on Windows 11, so, on emulation. However, I want to run the container on ARM64

  8. omanikhi commented on Apr 12, 2023

    @omanikhi

    So 7.x will not be usable on arm64 devices and we need to wait for 8.0?!

  9. stephentoub commented on Apr 12, 2023

    @stephentoub
    Member

    We run a bazillion regex tests on ARM64, and to my knowledge we've never seen such an NRE. It's far from "not usable".

    Do you have a standalone repro with just regex that shows the problem you're hitting?

  10. omanikhi commented on Apr 12, 2023

    @omanikhi

    We run a bazillion regex tests on ARM64, and to my knowledge we've never seen such an NRE. It's far from "not usable".

    Do you have a standalone repro with just regex that shows the problem you're hitting?

    You're absolutely right about it being far from "not usable", perhaps I was a bit overdramatic in my choice of words.

    It actually doesn't happen directly on ARM64 but rather under QEMU emulation of ARM64 (on Ubuntu over x64 in our case) which should be a common production build practice building the project on a non-ARM64 CI server.

    I don't have a standalone example that reproduces it but I can certainly try to get one together if it helps.

  11. stephentoub commented on Apr 12, 2023

    @stephentoub
    Member

    Thanks.

    @richlander, @AndyAyersMS, do we support / test QEMU emulation of ARM64 on .NET 7?

    I'm struggling at the moment to see how this could be an issue specific to regex.

  12. AndyAyersMS commented on Apr 12, 2023

    @AndyAyersMS
    Member

    I don't believe running under QEMU is officially supported, but it would be nice if it worked. My guess is that this indicates a problem in QEMU.

    IIRC there is some kind of verbose mode where QEMU can give you more details when it hits something it does not expect. @RobertHenry6bev ran into something like this (though he was emulating x64, not arm64) -- maybe he can suggest how to diagnose these cases.

  13. moo-the-cow commented on Jun 5, 2023

    @moo-the-cow

    same issue:
    on WSL2 (ubuntu jammy x86_x64)
    when I run docker buildx bake -f docker-compose.yaml
    edit: for testing reason I also tried docker buildx build -t test --platform linux/arm64 . same result
    for ARM64 build
    used image: mcr.microsoft.com/dotnet/sdk:7.0-jammy-arm64v8

    [build 5/6] RUN dotnet restore:
    #0 7.385 MSBUILD : error : This is an unhandled exception in MSBuild -- PLEASE UPVOTE AN EXISTING ISSUE OR FILE A NEW ONE AT https://aka.ms/msbuild/unhandled. [/app/aspnetapp/proj.csproj]
    #0 7.385 MSBUILD : error :     System.NullReferenceException: Object reference not set to an instance of an object. [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Regex1_Scan(RegexRunner, ReadOnlySpan`1) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at System.Text.RegularExpressions.Regex.ScanInternal(RegexRunnerMode mode, Boolean reuseMatchObject, String input, Int32 beginning, RegexRunner runner, ReadOnlySpan`1 span, Boolean returnNullIfReuseMatchObject) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at System.Text.RegularExpressions.Regex.RunAllMatchesWithCallback[TState](String inputString, ReadOnlySpan`1 inputSpan, Int32 startat, TState& state, MatchCallback`1 callback, RegexRunnerMode mode, Boolean reuseMatchObject) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at System.Text.RegularExpressions.Regex.RunAllMatchesWithCallback[TState](String input, Int32 startat, TState& state, MatchCallback`1 callback, RegexRunnerMode mode, Boolean reuseMatchObject) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at System.Text.RegularExpressions.Regex.Replace(MatchEvaluator evaluator, Regex regex, String input, Int32 count, Int32 startat) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.Evaluation.Expander`2.MetadataExpander.ExpandMetadataLeaveEscaped(String expression, IMetadataTable metadata, ExpanderOptions options, IElementLocation elementLocation) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.Evaluation.Expander`2.ExpandIntoStringLeaveEscaped(String expression, ExpanderOptions options, IElementLocation elementLocation, LoggingContext loggingContext) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.EvaluateItemDefinitionElement(ProjectItemDefinitionElement itemDefinitionElement) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.EvaluateItemDefinitionGroupElement(ProjectItemDefinitionGroupElement itemDefinitionGroupElement) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.Evaluate() [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.Evaluation.Evaluator`4.Evaluate(IEvaluatorData`4 data, Project project, ProjectRootElement root, ProjectLoadSettings loadSettings, Int32 maxNodeCount, PropertyDictionary`1 environmentProperties, ILoggingService loggingService, IItemFactory`2 itemFactory, IToolsetProvider toolsetProvider, ProjectRootElementCacheBase projectRootElementCache, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, EvaluationContext evaluationContext, Boolean interactive) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.Execution.ProjectInstance.Initialize(ProjectRootElement xml, IDictionary`2 globalProperties, String explicitToolsVersion, String explicitSubToolsetVersion, Int32 visualStudioVersionFromSolution, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, Nullable`1 projectLoadSettings, EvaluationContext evaluationContext) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.Execution.ProjectInstance..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion, BuildParameters buildParameters, ILoggingService loggingService, BuildEventContext buildEventContext, ISdkResolverService sdkResolverService, Int32 submissionId, Nullable`1 projectLoadSettings) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.BackEnd.BuildRequestConfiguration.<>c__DisplayClass61_0.<LoadProjectIntoConfiguration>b__0() [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.BackEnd.BuildRequestConfiguration.InitializeProject(BuildParameters buildParameters, Func`1 loadProjectFromFile) [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.BackEnd.RequestBuilder.BuildProject() [/app/aspnetapp/proj.csproj]
    #0 7.386 MSBUILD : error :    at Microsoft.Build.BackEnd.RequestBuilder.BuildAndReport() [/app/aspnetapp/proj.csproj]
    
  14. mhoeper commented on Jun 7, 2023

    @mhoeper

    We are facing the same issue, too on Amazon Linux x86_x64

  15. danmoseley commented on Jun 7, 2023

    @danmoseley
    Contributor

    So neither of you are using QEMU?

  16. mhoeper commented on Jun 7, 2023

    @mhoeper

    Correct. The NullReferenceException happens as well locally, when I am building with Docker Desktop on Windows on my x86_x64 machine, using --platform linux/arm64 with FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build

  17. stephentoub commented on Jun 7, 2023

    @stephentoub
    Member

    So neither of you are using QEMU?

    on my x86_x64 machine, using --platform linux/arm64

    Targeting arm64 while running on x86/x64 hardware requires emulation. Is this ever happening without emulation?

  18. mhoeper commented on Jun 7, 2023

    @mhoeper

    Looks like this issue is reproducible as reported from multiple persons and a Nullreference Exception in Regex1_Scan. This is a programming mistake in Msbuild as null check missing and should be easy to fix, right?

  19. stephentoub commented on Jun 7, 2023

    @stephentoub
    Member

    Looks like this issue is reproducible as reported from multiple persons

    All confirmed cases are using emulated arm64

    This is a programming mistake in Msbuild as null check missing and should be easy to fix, right?

    No.

  20. danmoseley commented on Jun 7, 2023

    @danmoseley
    Contributor

    @RobertHenry6bev do you have any suggestions folks can try to gather more info here, per suggestion above?

  21. danmoseley commented on Jun 7, 2023

    @danmoseley
    Contributor

    @jkotas what is the support status of .NET in QEMU -- and if not supported, how well is it known to work?

  22. jkotas commented on Jun 7, 2023

    @jkotas
    Member
  23. danmoseley commented on Jun 7, 2023

    @danmoseley
    Contributor

    OK, I'm going to close this as it's a QEMU issue. If anyone is encountering this but not in fact emulating arm64 please open a new issue.

  24. ghost locked as resolved and limited conversation to collaborators on Jul 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions