Skip to content

Conversation

@rcj1
Copy link
Contributor

@rcj1 rcj1 commented Jan 18, 2026

Sometimes code is placed at a higher native offset than the code at IL offsets adjacent to it. Our debugger algorithm deals with this by, during traversal of mappings, setting the IL offset back if one is encountered that is lower than the current IL offset in the traversal. Native AOT does not have this mechanism, which leads to such code being misattributed to higher-than-actual source lines.

This change reproduces this mechanism in NativeAOT by inserting a native sequence point whenever the IL offset drops below the previous offset.

Repro issue (line attribution of DoWork):

using System.Runtime.CompilerServices;

static class Program
{
#line 53
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	public async static Task Main()
	{
		await DoWork(0);
	}
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	public async static Task BackgroundWorker1()
	{
		// await Task.Delay(10000);
		// await Task.Yield();
		throw new InvalidOperationException("BackgroundWorker1 exception.");
	}
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	public async static Task BackgroundWorker2()
	{
		await Task.Delay(10);
		Console.WriteLine("BackgroundWorker2 completed.");
	}
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	public async static Task BackgroundWorker3()
	{
		await Task.Delay(10);
		Console.WriteLine("BackgroundWorker3 completed.");
	}
	
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	static async Task DoWork(int i)
	{
		Task worker1 = BackgroundWorker1();
		Task worker2 = BackgroundWorker2();
		Task worker3 = BackgroundWorker3();
		await Task.WhenAll(worker1, worker2, worker3);
		Console.WriteLine("All background workers completed.");
	}
}

Config: Windows x64 Debug, .NET 11.0.100-alpha.1.25618.104, runtime-async on

NativeOffset 0 106 107 107 116 124 124 133 141 141 150 158 169 183 192 205 222 231 244 261 270 283 307 312 357 379 380 387 392 393 420 431 867 878 1067 1096
ILOffset 0 0 1 1 6 7 7 12 13 13 18 19 27 30 35 37 40 45 47 50 55 57 60 65 70 75 76 81 86 87 87 70 87 70 0 87
ILOffset 0 1 7 13 19 76
LineNumber 80 81 82 83 84 85

Before:

NativeOffset 0 106 107 124 141 158 380 1067
LineNumber 80 80 81 82 83 84 85 80
Notice in particular that code after native offset 431, the suspension code for DoWork, is mistakenly attributed to line 85 (Console.WriteLine).

After:

NativeOffset 0 106 107 124 141 158 380 431 878 1067
LineNumber 80 80 81 82 83 84 85 84 84 80
Here we see that 380-431 is attributed to Console.WriteLine, while the suspension code that follows is correctly attributed to the previous line of await.

aot.txt

@rcj1 rcj1 requested a review from jkotas January 18, 2026 18:22
Copilot AI review requested due to automatic review settings January 18, 2026 18:22
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 18, 2026
@rcj1 rcj1 added area-NativeAOT-coreclr and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jan 18, 2026
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes source-to-native line mapping in NativeAOT for scenarios where code generation results in out-of-order placement—specifically, when code at lower IL offsets appears at higher native offsets. The fix implements a mechanism similar to CoreCLR's debugger algorithm by tracking when IL offsets decrease during native mapping traversal and inserting sequence points accordingly.

Changes:

  • Modified GetNativeSequencePoints() to forward-fill sequence point information across all IL offsets between real sequence points
  • Added tracking to detect when IL offsets decrease during native mapping traversal, emitting additional sequence points to maintain correct source line attribution
  • Introduced IsBackedSequencePoint flag to distinguish between real and propagated sequence points

Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Cc @jakobbotsch

@rcj1 rcj1 merged commit 05ed884 into dotnet:main Jan 19, 2026
100 checks passed
@rcj1 rcj1 deleted the aot-mapping-fix branch January 19, 2026 07:00
rosebyte pushed a commit that referenced this pull request Jan 19, 2026
Sometimes code is placed at a higher native offset than the code at IL
offsets adjacent to it. Our debugger algorithm deals with this by,
during traversal of mappings, [setting the IL offset
back](https://github.com/dotnet/runtime/blob/6afecd4adc64210e5b46767d02a6495af1a08c46/src/coreclr/vm/debugdebugger.cpp#L1321)
if one is encountered that is lower than the current IL offset in the
traversal. Native AOT does not have this mechanism, which leads to such
code being misattributed to higher-than-actual source lines.

This change reproduces this mechanism in NativeAOT by inserting a native
sequence point whenever the IL offset drops below the previous offset.

Repro issue (line attribution of DoWork):

```csharp
using System.Runtime.CompilerServices;

static class Program
{
#line 53
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	public async static Task Main()
	{
		await DoWork(0);
	}
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	public async static Task BackgroundWorker1()
	{
		// await Task.Delay(10000);
		// await Task.Yield();
		throw new InvalidOperationException("BackgroundWorker1 exception.");
	}
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	public async static Task BackgroundWorker2()
	{
		await Task.Delay(10);
		Console.WriteLine("BackgroundWorker2 completed.");
	}
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	public async static Task BackgroundWorker3()
	{
		await Task.Delay(10);
		Console.WriteLine("BackgroundWorker3 completed.");
	}
	
	[MethodImpl(MethodImplOptions.NoOptimization | MethodImplOptions.NoInlining)]
	static async Task DoWork(int i)
	{
		Task worker1 = BackgroundWorker1();
		Task worker2 = BackgroundWorker2();
		Task worker3 = BackgroundWorker3();
		await Task.WhenAll(worker1, worker2, worker3);
		Console.WriteLine("All background workers completed.");
	}
}
```
Config: Windows x64 Debug, .NET 11.0.100-alpha.1.25618.104,
runtime-async on
<table>
  <tr>
    <th>NativeOffset</th>
    <td>0</td>
    <td>106</td>
    <td>107</td>
    <td>107</td>
    <td>116</td>
    <td>124</td>
    <td>124</td>
    <td>133</td>
    <td>141</td>
    <td>141</td>
    <td>150</td>
    <td>158</td>
    <td>169</td>
    <td>183</td>
    <td>192</td>
    <td>205</td>
    <td>222</td>
    <td>231</td>
    <td>244</td>
    <td>261</td>
    <td>270</td>
    <td>283</td>
    <td>307</td>
    <td>312</td>
    <td>357</td>
    <td>379</td>
    <td>380</td>
    <td>387</td>
    <td>392</td>
    <td>393</td>
    <td>420</td>
    <td>431</td>
    <td>867</td>
    <td>878</td>
    <td>1067</td>
    <td>1096</td>
  </tr>
  <tr>
    <th>ILOffset</th>
    <td>0</td>
    <td>0</td>
    <td>1</td>
    <td>1</td>
    <td>6</td>
    <td>7</td>
    <td>7</td>
    <td>12</td>
    <td>13</td>
    <td>13</td>
    <td>18</td>
    <td>19</td>
    <td>27</td>
    <td>30</td>
    <td>35</td>
    <td>37</td>
    <td>40</td>
    <td>45</td>
    <td>47</td>
    <td>50</td>
    <td>55</td>
    <td>57</td>
    <td>60</td>
    <td>65</td>
    <td>70</td>
    <td>75</td>
    <td>76</td>
    <td>81</td>
    <td>86</td>
    <td>87</td>
    <td>87</td>
    <td>70</td>
    <td>87</td>
    <td>70</td>
    <td>0</td>
    <td>87</td>
  </tr>
</table>

<table>
  <tr>
    <th>ILOffset</th>
    <td>0</td>
    <td>1</td>
    <td>7</td>
    <td>13</td>
    <td>19</td>
    <td>76</td>
  </tr>
  <tr>
    <th>LineNumber</th>
    <td>80</td>
    <td>81</td>
    <td>82</td>
    <td>83</td>
    <td>84</td>
    <td>85</td>
  </tr>
</table>

Before:
<table>
  <tr>
    <th>NativeOffset</th>
    <td>0</td>
    <td>106</td>
    <td>107</td>
    <td>124</td>
    <td>141</td>
    <td>158</td>
    <td>380</td>
    <td>1067</td>
  </tr>
  <tr>
    <th>LineNumber</th>
    <td>80</td>
    <td>80</td>
    <td>81</td>
    <td>82</td>
    <td>83</td>
    <td>84</td>
    <td>85</td>
    <td>80</td>
  </tr>
</table>
Notice in particular that code after native offset 431, the suspension
code for DoWork, is mistakenly attributed to line 85
(Console.WriteLine).

After:
<table>
  <tr>
    <th>NativeOffset</th>
    <td>0</td>
    <td>106</td>
    <td>107</td>
    <td>124</td>
    <td>141</td>
    <td>158</td>
    <td>380</td>
    <td>431</td>
    <td>878</td>
    <td>1067</td>
  </tr>
  <tr>
    <th>LineNumber</th>
    <td>80</td>
    <td>80</td>
    <td>81</td>
    <td>82</td>
    <td>83</td>
    <td>84</td>
    <td>85</td>
    <td>84</td>
    <td>84</td>
    <td>80</td>
  </tr>
</table>
Here we see that 380-431 is attributed to Console.WriteLine, while the
suspension code that follows is correctly attributed to the previous
line of await.

[aot.txt](https://github.com/user-attachments/files/24697543/aot.txt)
rcj1 added a commit that referenced this pull request Feb 3, 2026
Testing on #122722 revealed some issues with #123333 so I am fixing
these here.

After #123333 there remain two error cases:
1. Our IL offset is greater than the previously found offset, but lower
than the max found offset. In this case, we would like to advance the
line number in accordance with the new IL offset, which was omitted
after #123333.
2. Our IL offset is greater than the highest IL offset for which there
exists a sequence point. In this case, we want to ensure we can create a
native <-> line mapping - #123333 had left these IL offsets with a
Document of null, meaning no mappings were possible.
This fixes both of these issues.

Before:
<img width="1298" height="268" alt="Screenshot 2026-01-30 192451"
src="https://github.com/user-attachments/assets/ab46947f-ea52-4530-949b-3e36bf3b3df6"
/>
<img width="1312" height="359" alt="Screenshot 2026-01-30 192408"
src="https://github.com/user-attachments/assets/b08a8f45-0af2-484a-8392-8d12c3694361"
/>

After:
<img width="1020" height="311" alt="Screenshot 2026-01-30 190332"
src="https://github.com/user-attachments/assets/ac8bca54-6466-4436-9fac-1f6f685e7bd0"
/>
<img width="1030" height="400" alt="Screenshot 2026-01-30 190613"
src="https://github.com/user-attachments/assets/321c8160-cf06-4f43-8c36-352d2bd6908a"
/>
[foo.txt](https://github.com/user-attachments/files/24984632/foo.txt)
[foo2.txt](https://github.com/user-attachments/files/24984633/foo2.txt)
[New Compressed (zipped)
Folder.zip](https://github.com/user-attachments/files/24976959/New.Compressed.zipped.Folder.zip)

---------

Co-authored-by: Michal Strehovský <[email protected]>
Co-authored-by: Jan Kotas <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants