Sequence points can land in the middle of an IL instruction

Aperta
#4,122 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
74/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
csharp

Direzione di ricerca

Inizia da ICSharpCode.Decompiler/CSharp/SequencePointBuilder.cs, in SequencePointBuilder.GetSequencePoints, quindi confronta la gestione degli offset con SequencePointCandidates in ILReader.cs intorno alla riga 560. Riproduci il problema con il codice sorgente C# fornito usando dotnet build -c Release e ilspycmd -genpdb; il lavoro è completato quando i punti di sequenza generati ricadono in modo coerente su offset di inizio istruzione, inclusi i casi visibili e nascosti segnalati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Bug PDBGen
Summary

Sequence points in a generated PDB can carry an IL offset that is not the start of an instruction. A consumer that resolves such an offset to an instruction gets nothing back: Mono.Cecil's GetInstruction returns null, and a debugger asked to bind a breakpoint there has no instruction to bind to.

Only visible in assemblies compiled in Release. In a Debug build the first instruction of every method is a one-byte nop, which happens to make the offending offset land on a boundary.

Reproduction
using System;

namespace ProbeNs
{
	public struct Holder
	{
		public int Value;
	}

	public class Minimal
	{
		public static int UseDefault(int input)
		{
			Holder holder = default(Holder);
			holder.Value = input;
			return holder.Value;
		}

		public static Func<int, int> MakeDelegate(int offset)
		{
			return (int n) => n + offset;
		}
	}
}
dotnet build -c Release
ilspycmd -genpdb Probe.dll
Minimal.UseDefault:   sequence point at IL_0001 (hidden) is inside IL_0000: ldloca.s (2 bytes)
Minimal.MakeDelegate: sequence point at IL_0001 (hidden) is inside IL_0000: newobj (5 bytes)

The same source built in Debug produces no such point.

The full point list for one of them shows the shape - a one-byte first point, and the hidden filler that follows it inherits its end offset:

off=0x0   visible  line=30
off=0x1   hidden            <- inside the 5-byte instruction at IL_0000
off=0x14  visible  line=31
off=0x23  hidden
off=0x29  visible  line=32

Visible sequence points are affected too, not only hidden ones. In Microsoft.Extensions.Http 10.0.11, <<SendCoreAsync>g__Core|4_0>d.MoveNext gets a visible point for line 62 at IL_000b, inside the 5-byte ldfld at IL_0008.

Scale over shipping assemblies: 156 such points in Newtonsoft.Json 13.0.4, 21 in AutoMapper 16.2.0, 4 in Microsoft.Extensions.Http 10.0.11.

Context

In SequencePointBuilder.GetSequencePoints (ICSharpCode.Decompiler/CSharp/SequencePointBuilder.cs), the loop that snaps offsets to function.SequencePointCandidates adjusts EndOffset of the current point and Offset of the next one only when the neighbouring-point conditions hold; the gap-filling insert at the end of the loop body then takes currSequencePoint.EndOffset verbatim as the new point's Offset. SequencePointCandidates itself only ever contains inst.StartILOffset values (ILReader.cs line 560), so the candidate list is not the source of the bad offsets.

Posted by an AI agent (Claude) on Siegfried's behalf.

Lingua principale
C#
Stelle
27.1k
Fork
3.8k
Merge medio
1g 23h
PR unite (30g)
44

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di icsharpcode/ILSpy

Tutte le issue di icsharpcode/ILSpy

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.