Skip to content

Commit

Permalink
Addressing issue #781 (#782)
Browse files Browse the repository at this point in the history
* Pose the problem

* Quick and v dirty fix

* A better and more targeted fix (to fix the previous fix)
  • Loading branch information
SteveGilham authored Aug 12, 2021
1 parent 2f1077d commit a0a6ce4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Mono.Cecil.Cil/MethodBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ void UpdateLocalScopes (Instruction removedInstruction, Instruction existingInst
if (debug_info == null)
return;

// Local scopes store start/end pair of "instruction offsets". Instruction offset can be either resolved, in which case it
// Local scopes store start/end pair of "instruction offsets". Instruction offset can be either resolved, in which case it
// has a reference to Instruction, or unresolved in which case it stores numerical offset (instruction offset in the body).
// Typically local scopes loaded from PE/PDB files will be resolved, but it's not a requirement.
// Each instruction has its own offset, which is populated on load, but never updated (this would be pretty expensive to do).
Expand Down Expand Up @@ -407,7 +407,7 @@ InstructionOffset ResolveInstructionOffset(InstructionOffset inputOffset, ref In
// Allow for trailing null values in the case of
// instructions.Size < instructions.Capacity
if (item == null)
break;
return new InstructionOffset (items [i - 1]);

cache.Instruction = item;

Expand All @@ -424,4 +424,4 @@ InstructionOffset ResolveInstructionOffset(InstructionOffset inputOffset, ref In
}
}
}
}
}
39 changes: 39 additions & 0 deletions Test/Mono.Cecil.Tests/ILProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,45 @@ public void InsertBeforeIssue697 ()
}
}

[Test]
public void InsertBeforeIssue697bis ()
{
var parameters = new ReaderParameters { SymbolReaderProvider = new MdbReaderProvider () };
using (var module = GetResourceModule ("Issue697.dll", parameters)) {
var pathGetterDef = module.GetTypes ()
.SelectMany (t => t.Methods)
.First (m => m.Name.Equals ("get_Defer"));

var body = pathGetterDef.Body;
var worker = body.GetILProcessor ();
var initialBody = body.Instructions.ToList ();
Console.WriteLine (initialBody.Sum (i => i.GetSize ()));

var head = initialBody.First ();
var opcode = worker.Create (OpCodes.Ldc_I4_1);
worker.InsertBefore (head, opcode);

Assert.That (pathGetterDef.DebugInformation.Scope.Start.IsEndOfMethod, Is.False);
foreach (var subscope in pathGetterDef.DebugInformation.Scope.Scopes)
Assert.That (subscope.Start.IsEndOfMethod, Is.False);

// big test -- we can write w/o crashing
var unique = Guid.NewGuid ().ToString ();
var output = Path.GetTempFileName ();
var outputdll = output + ".dll";

var writer = new WriterParameters () {
SymbolWriterProvider = new MdbWriterProvider (),
WriteSymbols = true
};
using (var sink = File.Open (outputdll, FileMode.Create, FileAccess.ReadWrite)) {
module.Write (sink, writer);
}

Assert.Pass ();
}
}

[Test]
public void InsertAfter ()
{
Expand Down

0 comments on commit a0a6ce4

Please sign in to comment.