runtime icon indicating copy to clipboard operation
runtime copied to clipboard

JIT: NullReferenceException in iText layout code on .NET 11.0.0 with optimized code; works on .NET 10

Open kambbado opened this issue 2 weeks ago • 7 comments

Description

On .NET 11.0.0, iText (the itext NuGet package) throws a NullReferenceException from iText.Layout.Renderer.LineRenderer.Layout when a PDF form text field is created or filled. The same program and the same iText binaries work on .NET 10.0.15.

The failure depends on JIT optimization:

  • With DOTNET_TieredCompilation=0 (fully optimized code) it fails on the first field, every time.
  • With DOTNET_TieredCompilation=0 and DOTNET_JITMinOpts=1 it never fails.
  • With default settings it starts failing once the layout methods have been tiered up, so short runs pass and longer or busier ones crash. In a real application it showed up as an intermittent failure under load.

The process exits with code -1073741819 (0xC0000005).

Reproduction Steps

repro.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net10.0;net11.0</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <UseAppHost>false</UseAppHost>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="itext" Version="9.8.0" />
    <PackageReference Include="itext.bouncy-castle-adapter" Version="9.8.0" />
  </ItemGroup>
</Project>

Program.cs

using iText.Forms;
using iText.Forms.Fields;
using iText.IO.Font.Constants;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;

Console.WriteLine($"Runtime {Environment.Version}");

// 1. Build a PDF with 200 empty text fields.
byte[] template;
using (MemoryStream created = new())
{
    using (PdfDocument doc = new(new PdfWriter(created)))
    {
        doc.AddNewPage();
        PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, true);
        for (int i = 0; i < 200; i++)
        {
            Rectangle rect = new(36 + (i % 4) * 130, 780 - (i / 4) * 15, 120, 12);
            form.AddField(new TextFormFieldBuilder(doc, "field" + i).SetWidgetRectangle(rect).CreateText());
        }
    }
    template = created.ToArray();
}

// 2. Fill the fields.
int set = 0;
for (int pass = 0; pass < 10; pass++)
{
    using PdfDocument doc = new(new PdfReader(new MemoryStream(template)), new PdfWriter(new MemoryStream()));
    PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, false);
    PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
    foreach (KeyValuePair<string, PdfFormField> kvp in form.GetAllFormFields())
    {
        kvp.Value.SetFont(font);
        kvp.Value.SetFontSize(8f);
        kvp.Value.SetValue("sample text");
        set++;
    }
}
Console.WriteLine($"OK: {set} fields set");

Run (PowerShell):

dotnet build
$env:DOTNET_TieredCompilation = '0'
dotnet bin\Debug\net10.0\repro.dll
dotnet bin\Debug\net11.0\repro.dll

Expected behavior

Both runs print OK: 2000 fields set, as .NET 10.0.15 does.

Actual behavior

Runtime DOTNET_TieredCompilation=0 Result
10.0.15 not set OK
10.0.15 0 OK
11.0.0 0 NullReferenceException, exit code -1073741819
11.0.0 0, plus DOTNET_JITMinOpts=1 OK

Output on .NET 11.0.0 with DOTNET_TieredCompilation=0:

Runtime 11.0.0
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at iText.Layout.Renderer.LineRenderer.Layout(LayoutContext layoutContext)
   at iText.Layout.Renderer.ParagraphRenderer.DirectLayout(LayoutContext layoutContext)
   at iText.Layout.Renderer.ParagraphRenderer.Layout(LayoutContext layoutContext)
   at iText.Layout.Renderer.BlockRenderer.Layout(LayoutContext layoutContext)
   at iText.Forms.Form.Renderer.AbstractFormFieldRenderer.Layout(LayoutContext layoutContext)
   at iText.Layout.Renderer.RootRenderer.LayoutChild(IRenderer renderer, MarginsCollapseInfo childMarginsInfo)
   at iText.Layout.Renderer.RootRenderer.AddChild(IRenderer renderer)
   at iText.Layout.Renderer.CanvasRenderer.AddChild(IRenderer renderer)
   at iText.Layout.RootElement`1.CreateAndAddRendererSubTree(IElement element)
   at iText.Forms.Fields.PdfFormAnnotation.DrawTextFormFieldAndSaveAppearance()
   at iText.Forms.Fields.PdfFormAnnotation.RegenerateWidget()
   at iText.Forms.Fields.PdfFormField.RegenerateField()
   at iText.Forms.Fields.AbstractPdfFormField.EnableFieldRegeneration()
   at iText.Forms.Fields.TextFormFieldBuilder.CreateText(Boolean multiline)
   at Program.<Main>$(String[] args)

Regression?

Yes. Works on .NET 10.0.15, fails on .NET 11.0.0. Both runtimes are installed side by side on the same machine and run the same iText assemblies.

Known Workarounds

None that is usable in production. DOTNET_JITMinOpts=1 avoids it but disables optimization for the whole process. iText 9.7.0, 9.3.0 and 8.0.5 fail the same way.

Configuration

  • .NET SDK 11.0.100 (commit 297acf930f), runtime Microsoft.NETCore.App 11.0.0
  • Windows 11 x64 (10.0.26300), RID win-x64
  • CPU: Intel Core Ultra 9 285H
  • iText 9.8.0 (also 9.7.0, 9.3.0, 8.0.5)

Other information

Possibly related to #135811 (redundant branch opts removing a null check, spurious NullReferenceException, regression in 11.0.0-rc.1). I have not confirmed that it is the same bug.

kambbado avatar Nov 11 '26 21:11 kambbado

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.

dotnet-policy-service avatar Nov 11 '26 21:11 dotnet-policy-service

Thanks for the report and the repro. This is the same bug as #133817, which is fixed in main via https://github.com/dotnet/runtime/pull/133826 but not backported.

There doesn't seem to be a good workaround. I have started a backport request.

AndyAyersMS avatar Nov 11 '26 23:11 AndyAyersMS

Same issue as #137270.

AndyAyersMS avatar Nov 11 '26 23:11 AndyAyersMS

I ran into the exact same issue. For now I downgraded back to .NET 10

@AndyAyersMS is this something which will be fixed in .NET 11.0.1 next month?

MarvinKlein1508 avatar Nov 12 '26 08:11 MarvinKlein1508

@AndyAyersMS is this something which will be fixed in .NET 11.0.1 next month?

I believe this will be in 11.0.1, but I'll have to double check.

AndyAyersMS avatar Nov 13 '26 22:11 AndyAyersMS

@AndyAyersMS is this something which will be fixed in .NET 11.0.1 next month?

I believe this will be in 11.0.1, but I'll have to double check.

Yes, it should be in 11.0.1.

AndyAyersMS avatar Nov 16 '26 21:11 AndyAyersMS

@AndyAyersMS thanks for the update. I will move my apps to .NET 11 then once 11.0.1 comes out. Otherwise we aren't able to create invoices anymore ;)

MarvinKlein1508 avatar Nov 17 '26 10:11 MarvinKlein1508