Repeated failed LoadFromAssemblyPath on a native DLL ends in 'Stack overflow.' and a hung process instead of BadImageFormatException

オープン
#137,153 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
31/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp

調査の方向性

Start with the AssemblyLoadContext.Default.LoadFromAssemblyPath reproduction in Program.cs and NativeLoadRepro.csproj, then investigate the Windows native loading path and its handling of repeated BadImageFormatException failures. Reproduce on the reported .NET 10.0.13 Windows x64 setup and trace the native stack around the overflow and lock. Done means repeated failed loads return BadImageFormatException without overflowing or hanging.

索引モデルが issue の本文から書いたものです。

説明

area-Host untriaged

Written by Claude Code, an AI agent.

Description

Calling AssemblyLoadContext.Default.LoadFromAssemblyPath on the same native (non-managed) DLL again and again, on one thread, throws BadImageFormatException each time at first. After about 1,000 calls for covrun64.dll (the code coverage's native profiler DLL, from Microsoft.Testing.Extensions.CodeCoverage), the process prints Stack overflow. and then hangs: it uses no CPU, and does not exit.

A library that scans its output folder for extensions does exactly this: it tries to load every *.dll, and a test process that starts many hosts repeats the scan. Here the test process hung after about 300 tests. The library's part is reported separately: https://github.com/JasperFx/jasperfx/issues/1143.

Reproduction Steps

NativeLoadRepro.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <!-- Only for its native DLLs, which it copies to runtimes/win-*/native. -->
    <PackageReference Include="Microsoft.Testing.Extensions.CodeCoverage" Version="18.12.0" />
  </ItemGroup>
</Project>

Program.cs:

using System.Runtime.Loader;

var file = Path.Combine(AppContext.BaseDirectory, "runtimes", "win-x64", "native", "covrun64.dll");

for (var i = 1; i <= 100_000; i++)
{
    try
    {
        AssemblyLoadContext.Default.LoadFromAssemblyPath(file);
    }
    catch (BadImageFormatException)
    {
    }

    if (i % 100 == 0)
    {
        Console.WriteLine($"{i} failed loads");
    }
}

Console.WriteLine("done");

dotnet run -c Release on Windows x64.

Expected behavior

Every call throws BadImageFormatException, and the loop prints done.

Actual behavior
100 failed loads
...
900 failed loads
1000 failed loads
Stack overflow.

Then the process hangs until it is killed. Run today: a 120-second limit killed it.

What else was measured on the same machine:

  • covrun64.dll alone: the overflow came after 1,081 failed loads in one run, and between 1,000 and 1,100 in the run above.
  • All 24 native DLLs of the package's runtimes folder in turn: after about 8,650 failed loads.
  • The count adds up per process, not per thread: spreading the loads over several threads gives the same total.
  • msdia140.dll from the same folder survived 71,928 failed loads, and loads by name only (LoadFromAssemblyName) of the same files never overflowed.
  • A dump of the hung test process (dotnet-dump, managed frames only) showed the loading thread inside the native part of LoadFromAssemblyPath on runtimes\win-arm64\native\covrun64.dll, holding a lock that other threads waited on in Monitor.Enter. No native debugger was available, so the native stack was not read.
Regression?

Not known. Only .NET 10.0.13 was tried.

Known Workarounds

Don't load native DLLs by path: skip the runtimes and native folders when scanning.

Configuration
  • .NET runtime 10.0.13, SDK 10.0.402
  • Windows 11 Pro 10.0.26300, x64
  • Microsoft.Testing.Extensions.CodeCoverage 18.12.0
  • Windows Defender real-time protection is on. No other endpoint security product is installed. microsoft/codecoverage#212 reported a similar crash on the same file and was closed because the reporter traced it to an endpoint security product; on this machine the only such product is Defender, and this report cannot rule out its part.
Other information

The stack overflow suggests something grows per failed load of this file (perhaps a list of failed images, or a chain of handlers) and is walked recursively. That is a guess, not measured.

主要言語
C#
スター
18.4k
フォーク
5.8k
平均マージ
2日 14時間
マージ済み PR(30日)
585

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/runtime のほかの issue

dotnet/runtime の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。