Skip to content

[NativeAOT] Source to native mapping fix for out-of-order code - #123333

Merged
rcj1 merged 2 commits into
dotnet:mainfrom
rcj1:aot-mapping-fix
Jan 19, 2026
Merged

[NativeAOT] Source to native mapping fix for out-of-order code#123333
rcj1 merged 2 commits into
dotnet:mainfrom
rcj1:aot-mapping-fix

Conversation

@rcj1

@rcj1rcj1 commented Jan 18, 2026

Copy link
Copy Markdown
Contributor

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):

usingSystem.Runtime.CompilerServices;staticclassProgram{
#line 53[MethodImpl(MethodImplOptions.NoOptimization|MethodImplOptions.NoInlining)]publicasyncstaticTaskMain(){awaitDoWork(0);}[MethodImpl(MethodImplOptions.NoOptimization|MethodImplOptions.NoInlining)]publicasyncstaticTaskBackgroundWorker1(){// await Task.Delay(10000);// await Task.Yield();thrownewInvalidOperationException("BackgroundWorker1 exception.");}[MethodImpl(MethodImplOptions.NoOptimization|MethodImplOptions.NoInlining)]publicasyncstaticTaskBackgroundWorker2(){awaitTask.Delay(10);Console.WriteLine("BackgroundWorker2 completed.");}[MethodImpl(MethodImplOptions.NoOptimization|MethodImplOptions.NoInlining)]publicasyncstaticTaskBackgroundWorker3(){awaitTask.Delay(10);Console.WriteLine("BackgroundWorker3 completed.");}[MethodImpl(MethodImplOptions.NoOptimization|MethodImplOptions.NoInlining)]staticasyncTaskDoWork(inti){Taskworker1=BackgroundWorker1();Taskworker2=BackgroundWorker2();Taskworker3=BackgroundWorker3();awaitTask.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

NativeOffset010610710711612412413314114115015816918319220522223124426127028330731235737938038739239342043186787810671096
ILOffset0011677121313181927303537404547505557606570757681868787708770087
ILOffset017131976
LineNumber808182838485

Before:

NativeOffset01061071241411583801067
LineNumber8080818283848580
Notice in particular that code after native offset 431, the suspension code for DoWork, is mistakenly attributed to line 85 (Console.WriteLine).

After:

NativeOffset01061071241411583804318781067
LineNumber80808182838485848480
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 jkotasJanuary 18, 2026 18:22
CopilotAI review requested due to automatic review settings January 18, 2026 18:22
@github-actionsgithub-actionsBot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jan 18, 2026
@rcj1rcj1 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
Copy Markdown
Contributor

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

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

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

@MichalStrehovskyMichalStrehovsky left a comment

Copy link
Copy Markdown
Member

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:mainJan 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ý <MichalStrehovsky@users.noreply.github.com>
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
lewing pushed a commit to lewing/runtime that referenced this pull request Feb 9, 2026
…3831)
Testing on dotnet#122722 revealed some issues with dotnet#123333 so I am fixing
these here.
After dotnet#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 dotnet#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 - dotnet#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ý <MichalStrehovsky@users.noreply.github.com>
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Feb 18, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@rcj1@MichalStrehovsky