diff options
28 files changed, 305 insertions, 343 deletions
diff --git a/Mono.Cecil.Cil/CodeReader.cs b/Mono.Cecil.Cil/CodeReader.cs index 38bba66..487b7b8 100644 --- a/Mono.Cecil.Cil/CodeReader.cs +++ b/Mono.Cecil.Cil/CodeReader.cs @@ -17,58 +17,38 @@ using RVA = System.UInt32; namespace Mono.Cecil.Cil { - sealed class CodeReader : ByteBuffer { + sealed class CodeReader : BinaryStreamReader { readonly internal MetadataReader reader; int start; - Section code_section; - MethodDefinition method; MethodBody body; int Offset { - get { return base.position - start; } + get { return Position - start; } } - public CodeReader (Section section, MetadataReader reader) - : base (section.Data) + public CodeReader (MethodDefinition method, MetadataReader reader) + : base (reader.image.Stream) { - this.code_section = section; this.reader = reader; + this.reader.context = method; + this.Position = (int) reader.image.ResolveVirtualAddress ((uint) method.RVA); } - public MethodBody ReadMethodBody (MethodDefinition method) + public static MethodBody ReadMethodBody (MethodDefinition method, MetadataReader metadata) { - this.method = method; - this.body = new MethodBody (method); + var reader = new CodeReader (method, metadata); + reader.body = new MethodBody (method); - reader.context = method; + reader.ReadMethodBody (); - ReadMethodBody (); - - return this.body; - } - - public void MoveTo (int rva) - { - if (!IsInSection (rva)) { - code_section = reader.image.GetSectionAtVirtualAddress ((uint) rva); - Reset (code_section.Data); - } - - base.position = rva - (int) code_section.VirtualAddress; - } - - bool IsInSection (int rva) - { - return code_section.VirtualAddress <= rva && rva < code_section.VirtualAddress + code_section.SizeOfRawData; + return reader.body; } void ReadMethodBody () { - MoveTo (method.RVA); - var flags = ReadByte (); switch (flags & 0x3) { case 0x2: // tiny @@ -77,7 +57,7 @@ namespace Mono.Cecil.Cil { ReadCode (); break; case 0x3: // fat - base.position--; + Advance (-1); ReadFatMethod (); break; default: @@ -120,17 +100,17 @@ namespace Mono.Cecil.Cil { void ReadCode () { - start = position; + start = Position; var code_size = body.code_size; - if (code_size < 0 || buffer.Length <= (uint) (code_size + position)) + if (code_size < 0 || Length <= (uint) (code_size + Position)) code_size = 0; var end = start + code_size; var instructions = body.instructions = new InstructionCollection ((code_size + 1) / 2); - while (position < end) { - var offset = base.position - start; + while (Position < end) { + var offset = Position - start; var opcode = ReadOpCode (); var current = new Instruction (offset, opcode); @@ -305,7 +285,7 @@ namespace Mono.Cecil.Cil { void ReadFatSection () { - position--; + Advance (-1); var count = (ReadInt32 () >> 8) / 24; ReadExceptionHandlers ( @@ -351,6 +331,7 @@ namespace Mono.Cecil.Cil { void Align (int align) { align--; + var position = Position; Advance (((position + align) & ~align) - position); } @@ -366,11 +347,6 @@ namespace Mono.Cecil.Cil { var buffer = new ByteBuffer (); symbols = new MethodSymbols (method.Name); - this.method = method; - reader.context = method; - - MoveTo (method.RVA); - var flags = ReadByte (); MetadataToken local_var_token; @@ -383,8 +359,7 @@ namespace Mono.Cecil.Cil { PatchRawCode (buffer, symbols.code_size, writer); break; case 0x3: // fat - base.position--; - + Advance (-1); PatchRawFatMethod (buffer, symbols, writer, out local_var_token); break; default: @@ -501,9 +476,9 @@ namespace Mono.Cecil.Cil { void PatchRawSection (ByteBuffer buffer, MetadataBuilder metadata) { - var position = base.position; + var position = Position; Align (4); - buffer.WriteBytes (base.position - position); + buffer.WriteBytes (Position - position); const byte fat_format = 0x40; const byte more_sects = 0x80; @@ -534,7 +509,7 @@ namespace Mono.Cecil.Cil { void PatchRawFatSection (ByteBuffer buffer, MetadataBuilder metadata) { - position--; + Advance (-1); var length = ReadInt32 (); buffer.WriteInt32 (length); diff --git a/Mono.Cecil.Cil/CodeWriter.cs b/Mono.Cecil.Cil/CodeWriter.cs index 7f7d095..c103824 100644 --- a/Mono.Cecil.Cil/CodeWriter.cs +++ b/Mono.Cecil.Cil/CodeWriter.cs @@ -75,7 +75,7 @@ namespace Mono.Cecil.Cil { void WriteUnresolvedMethodBody (MethodDefinition method) { - var code_reader = metadata.module.Read (method, (_, reader) => reader.code); + var code_reader = metadata.module.Read (method, (m, reader) => new CodeReader (m, reader)); MethodSymbols symbols; var buffer = code_reader.PatchRawMethodBody (method, this, out symbols); diff --git a/Mono.Cecil.Metadata/BlobHeap.cs b/Mono.Cecil.Metadata/BlobHeap.cs index 527d042..407702c 100644 --- a/Mono.Cecil.Metadata/BlobHeap.cs +++ b/Mono.Cecil.Metadata/BlobHeap.cs @@ -10,25 +10,21 @@ using System; -using Mono.Cecil.PE; - namespace Mono.Cecil.Metadata { sealed class BlobHeap : Heap { - public BlobHeap (Section section, uint start, uint size) - : base (section, start, size) + public BlobHeap (byte [] data) + : base (data) { } public byte [] Read (uint index) { - if (index == 0 || index > Size - 1) + if (index == 0 || index > this.data.Length - 1) return Empty<byte>.Array; - var data = Section.Data; - - int position = (int) (index + Offset); + int position = (int) index; int length = (int) data.ReadCompressedUInt32 (ref position); var buffer = new byte [length]; diff --git a/Mono.Cecil.Metadata/GuidHeap.cs b/Mono.Cecil.Metadata/GuidHeap.cs index 74af80e..c62db61 100644 --- a/Mono.Cecil.Metadata/GuidHeap.cs +++ b/Mono.Cecil.Metadata/GuidHeap.cs @@ -10,14 +10,12 @@ using System; -using Mono.Cecil.PE; - namespace Mono.Cecil.Metadata { sealed class GuidHeap : Heap { - public GuidHeap (Section section, uint start, uint size) - : base (section, start, size) + public GuidHeap (byte [] data) + : base (data) { } @@ -32,10 +30,9 @@ namespace Mono.Cecil.Metadata { index--; - Buffer.BlockCopy (Section.Data, (int) (Offset + index), buffer, 0, guid_size); + Buffer.BlockCopy (this.data, (int) index, buffer, 0, guid_size); return new Guid (buffer); - } } } diff --git a/Mono.Cecil.Metadata/Heap.cs b/Mono.Cecil.Metadata/Heap.cs index c600f46..1badfbb 100644 --- a/Mono.Cecil.Metadata/Heap.cs +++ b/Mono.Cecil.Metadata/Heap.cs @@ -8,23 +8,17 @@ // Licensed under the MIT/X11 license. // -using Mono.Cecil.PE; - namespace Mono.Cecil.Metadata { abstract class Heap { public int IndexSize; - public readonly Section Section; - public readonly uint Offset; - public readonly uint Size; + internal byte [] data; - protected Heap (Section section, uint offset, uint size) + protected Heap (byte [] data) { - this.Section = section; - this.Offset = offset; - this.Size = size; + this.data = data; } } } diff --git a/Mono.Cecil.Metadata/StringHeap.cs b/Mono.Cecil.Metadata/StringHeap.cs index ee6009a..4bde70e 100644 --- a/Mono.Cecil.Metadata/StringHeap.cs +++ b/Mono.Cecil.Metadata/StringHeap.cs @@ -12,16 +12,14 @@ using System; using System.Collections.Generic; using System.Text; -using Mono.Cecil.PE; - namespace Mono.Cecil.Metadata { class StringHeap : Heap { readonly Dictionary<uint, string> strings = new Dictionary<uint, string> (); - public StringHeap (Section section, uint start, uint size) - : base (section, start, size) + public StringHeap (byte [] data) + : base (data) { } @@ -34,7 +32,7 @@ namespace Mono.Cecil.Metadata { if (strings.TryGetValue (index, out @string)) return @string; - if (index > Size - 1) + if (index > data.Length - 1) return string.Empty; @string = ReadStringAt (index); @@ -47,8 +45,7 @@ namespace Mono.Cecil.Metadata { protected virtual string ReadStringAt (uint index) { int length = 0; - byte [] data = Section.Data; - int start = (int) (index + Offset); + int start = (int) index; for (int i = start; ; i++) { if (data [i] == 0) diff --git a/Mono.Cecil.Metadata/TableHeap.cs b/Mono.Cecil.Metadata/TableHeap.cs index 9a83eb3..612a0ff 100644 --- a/Mono.Cecil.Metadata/TableHeap.cs +++ b/Mono.Cecil.Metadata/TableHeap.cs @@ -80,8 +80,8 @@ namespace Mono.Cecil.Metadata { get { return Tables [(int) table]; } } - public TableHeap (Section section, uint start, uint size) - : base (section, start, size) + public TableHeap (byte [] data) + : base (data) { } diff --git a/Mono.Cecil.Metadata/UserStringHeap.cs b/Mono.Cecil.Metadata/UserStringHeap.cs index eb4b0d3..7b1b42f 100644 --- a/Mono.Cecil.Metadata/UserStringHeap.cs +++ b/Mono.Cecil.Metadata/UserStringHeap.cs @@ -8,23 +8,18 @@ // Licensed under the MIT/X11 license. // -using System; - -using Mono.Cecil.PE; - namespace Mono.Cecil.Metadata { sealed class UserStringHeap : StringHeap { - public UserStringHeap (Section section, uint start, uint size) - : base (section, start, size) + public UserStringHeap (byte [] data) + : base (data) { } protected override string ReadStringAt (uint index) { - byte [] data = Section.Data; - int start = (int) (index + Offset); + int start = (int) index; uint length = (uint) (data.ReadCompressedUInt32 (ref start) & ~1); if (length < 1) diff --git a/Mono.Cecil.PE/BinaryStreamReader.cs b/Mono.Cecil.PE/BinaryStreamReader.cs index dbb2c30..eb4b77e 100644 --- a/Mono.Cecil.PE/BinaryStreamReader.cs +++ b/Mono.Cecil.PE/BinaryStreamReader.cs @@ -15,17 +15,31 @@ namespace Mono.Cecil.PE { class BinaryStreamReader : BinaryReader { + public int Position { + get { return (int) BaseStream.Position; } + set { BaseStream.Position = value; } + } + + public int Length { + get { return (int) BaseStream.Length; } + } + public BinaryStreamReader (Stream stream) : base (stream) { } - protected void Advance (int bytes) + public void Advance (int bytes) { BaseStream.Seek (bytes, SeekOrigin.Current); } - protected DataDirectory ReadDataDirectory () + public void MoveTo (uint position) + { + BaseStream.Seek (position, SeekOrigin.Begin); + } + + public DataDirectory ReadDataDirectory () { return new DataDirectory (ReadUInt32 (), ReadUInt32 ()); } diff --git a/Mono.Cecil.PE/ByteBuffer.cs b/Mono.Cecil.PE/ByteBuffer.cs index a8dfd16..075e6f6 100644 --- a/Mono.Cecil.PE/ByteBuffer.cs +++ b/Mono.Cecil.PE/ByteBuffer.cs @@ -34,12 +34,6 @@ namespace Mono.Cecil.PE { this.length = this.buffer.Length; } - public void Reset (byte [] buffer) - { - this.buffer = buffer ?? Empty<byte>.Array; - this.length = this.buffer.Length; - } - public void Advance (int length) { position += length; diff --git a/Mono.Cecil.PE/Image.cs b/Mono.Cecil.PE/Image.cs index 31f220f..4e5e606 100644 --- a/Mono.Cecil.PE/Image.cs +++ b/Mono.Cecil.PE/Image.cs @@ -9,8 +9,8 @@ // using System; +using System.IO; -using Mono; using Mono.Cecil.Cil; using Mono.Cecil.Metadata; @@ -18,13 +18,14 @@ using RVA = System.UInt32; namespace Mono.Cecil.PE { - sealed class Image { + sealed class Image : IDisposable { + + public Stream Stream; public ModuleKind Kind; public string RuntimeVersion; public TargetArchitecture Architecture; public ModuleCharacteristics Characteristics; - public string FileName; public Section [] Sections; @@ -115,34 +116,42 @@ namespace Mono.Cecil.PE { return null; } + public BinaryStreamReader GetReaderAt (RVA rva) + { + var reader = new BinaryStreamReader (Stream); + reader.MoveTo (ResolveVirtualAddress (rva)); + return reader; + } + public ImageDebugDirectory GetDebugHeader (out byte [] header) { - var section = GetSectionAtVirtualAddress (Debug.VirtualAddress); - var buffer = new ByteBuffer (section.Data); - buffer.position = (int) (Debug.VirtualAddress - section.VirtualAddress); + var reader = GetReaderAt (Debug.VirtualAddress); var directory = new ImageDebugDirectory { - Characteristics = buffer.ReadInt32 (), - TimeDateStamp = buffer.ReadInt32 (), - MajorVersion = buffer.ReadInt16 (), - MinorVersion = buffer.ReadInt16 (), - Type = buffer.ReadInt32 (), - SizeOfData = buffer.ReadInt32 (), - AddressOfRawData = buffer.ReadInt32 (), - PointerToRawData = buffer.ReadInt32 (), + Characteristics = reader.ReadInt32 (), + TimeDateStamp = reader.ReadInt32 (), + MajorVersion = reader.ReadInt16 (), + MinorVersion = reader.ReadInt16 (), + Type = reader.ReadInt32 (), + SizeOfData = reader.ReadInt32 (), + AddressOfRawData = reader.ReadInt32 (), + PointerToRawData = reader.ReadInt32 (), }; - if (directory.SizeOfData == 0 || directory.PointerToRawData == 0) { - header = Empty<byte>.Array; - return directory; - } - - buffer.position = (int) (directory.PointerToRawData - section.PointerToRawData); + reader = GetReaderAt ((uint) directory.AddressOfRawData); + header = reader.ReadBytes (directory.SizeOfData); + return directory; + } - header = new byte [directory.SizeOfData]; - Buffer.BlockCopy (buffer.buffer, buffer.position, header, 0, header.Length); + ~Image () + { + if (Stream != null) + Stream.Dispose (); + } - return directory; + public void Dispose () + { + Stream.Dispose (); } } } diff --git a/Mono.Cecil.PE/ImageReader.cs b/Mono.Cecil.PE/ImageReader.cs index 78458fc..ab60d33 100644 --- a/Mono.Cecil.PE/ImageReader.cs +++ b/Mono.Cecil.PE/ImageReader.cs @@ -24,12 +24,13 @@ namespace Mono.Cecil.PE { DataDirectory cli; DataDirectory metadata; + uint table_heap_offset; + public ImageReader (Stream stream) : base (stream) { image = new Image (); - - image.FileName = stream.GetFullyQualifiedName (); + image.Stream = stream; } void MoveTo (DataDirectory directory) @@ -37,11 +38,6 @@ namespace Mono.Cecil.PE { BaseStream.Position = image.ResolveVirtualAddress (directory.VirtualAddress); } - void MoveTo (uint position) - { - BaseStream.Position = position; - } - void ReadImage () { if (BaseStream.Length < 128) @@ -261,31 +257,11 @@ namespace Mono.Cecil.PE { Advance (16); sections [i] = section; - - ReadSectionData (section); } image.Sections = sections; } - void ReadSectionData (Section section) - { - var position = BaseStream.Position; - - MoveTo (section.PointerToRawData); - - var length = (int) section.SizeOfRawData; - var data = new byte [length]; - int offset = 0, read; - - while ((read = Read (data, offset, length - offset)) > 0) - offset += read; - - section.Data = data; - - BaseStream.Position = position; - } - void ReadCLIHeader () { MoveTo (cli); @@ -348,39 +324,50 @@ namespace Mono.Cecil.PE { void ReadMetadataStream (Section section) { // Offset 4 - uint start = metadata.VirtualAddress - section.VirtualAddress + ReadUInt32 (); // relative to the section start + uint offset = metadata.VirtualAddress - section.VirtualAddress + ReadUInt32 (); // relative to the section start // Size 4 uint size = ReadUInt32 (); + var data = ReadHeapData (offset, size); + var name = ReadAlignedString (16); switch (name) { case "#~": case "#-": - image.TableHeap = new TableHeap (section, start, size); + image.TableHeap = new TableHeap (data); + table_heap_offset = offset; break; case "#Strings": - image.StringHeap = new StringHeap (section, start, size); + image.StringHeap = new StringHeap (data); break; case "#Blob": - image.BlobHeap = new BlobHeap (section, start, size); + image.BlobHeap = new BlobHeap (data); break; case "#GUID": - image.GuidHeap = new GuidHeap (section, start, size); + image.GuidHeap = new GuidHeap (data); break; case "#US": - image.UserStringHeap = new UserStringHeap (section, start, size); + image.UserStringHeap = new UserStringHeap (data); break; } } + byte [] ReadHeapData (uint offset, uint size) + { + var position = BaseStream.Position; + MoveTo (offset + image.MetadataSection.PointerToRawData); + var data = ReadBytes ((int) size); + BaseStream.Position = position; + + return data; + } + void ReadTableHeap () { var heap = image.TableHeap; - uint start = heap.Section.PointerToRawData; - - MoveTo (heap.Offset + start); + MoveTo (table_heap_offset + image.MetadataSection.PointerToRawData); // Reserved 4 // MajorVersion 1 @@ -433,7 +420,7 @@ namespace Mono.Cecil.PE { void ComputeTableInformations () { - uint offset = (uint) BaseStream.Position - image.MetadataSection.PointerToRawData; // header + uint offset = (uint) BaseStream.Position - table_heap_offset - image.MetadataSection.PointerToRawData; // header int stridx_size = image.StringHeap.IndexSize; int blobidx_size = image.BlobHeap != null ? image.BlobHeap.IndexSize : 2; diff --git a/Mono.Cecil.PE/ImageWriter.cs b/Mono.Cecil.PE/ImageWriter.cs index 9ecbae8..d5e6b72 100644 --- a/Mono.Cecil.PE/ImageWriter.cs +++ b/Mono.Cecil.PE/ImageWriter.cs @@ -79,9 +79,8 @@ namespace Mono.Cecil.PE { if (rsrc == null) return; - var raw_resources = new byte [rsrc.Data.Length]; - Buffer.BlockCopy (rsrc.Data, 0, raw_resources, 0, rsrc.Data.Length); - win32_resources = new ByteBuffer (raw_resources); + var reader = module.Image.GetReaderAt (rsrc.VirtualAddress); + win32_resources = new ByteBuffer (reader.ReadBytes ((int) rsrc.SizeOfRawData)); } Section GetImageResourceSection () diff --git a/Mono.Cecil.PE/Section.cs b/Mono.Cecil.PE/Section.cs index d32365a..49d8e90 100644 --- a/Mono.Cecil.PE/Section.cs +++ b/Mono.Cecil.PE/Section.cs @@ -20,6 +20,5 @@ namespace Mono.Cecil.PE { public uint VirtualSize; public uint SizeOfRawData; public uint PointerToRawData; - public byte [] Data; } } diff --git a/Mono.Cecil/AssemblyDefinition.cs b/Mono.Cecil/AssemblyDefinition.cs index de6ed17..e49f7da 100644 --- a/Mono.Cecil/AssemblyDefinition.cs +++ b/Mono.Cecil/AssemblyDefinition.cs @@ -15,7 +15,7 @@ using Mono.Collections.Generic; namespace Mono.Cecil { - public sealed class AssemblyDefinition : ICustomAttributeProvider, ISecurityDeclarationProvider { + public sealed class AssemblyDefinition : ICustomAttributeProvider, ISecurityDeclarationProvider, IDisposable { AssemblyNameDefinition name; @@ -89,6 +89,13 @@ namespace Mono.Cecil { { } + public void Dispose () + { + var modules = this.Modules; + for (int i = 0; i < modules.Count; i++) + modules [i].Dispose (); + } + #if !READ_ONLY public static AssemblyDefinition CreateAssembly (AssemblyNameDefinition assemblyName, string moduleName, ModuleKind kind) { diff --git a/Mono.Cecil/AssemblyReader.cs b/Mono.Cecil/AssemblyReader.cs index 6ea6a1e..ec78a52 100644 --- a/Mono.Cecil/AssemblyReader.cs +++ b/Mono.Cecil/AssemblyReader.cs @@ -366,7 +366,6 @@ namespace Mono.Cecil { readonly internal MetadataSystem metadata; internal IGenericContext context; - internal CodeReader code; uint Position { get { return (uint) base.position; } @@ -374,12 +373,11 @@ namespace Mono.Cecil { } public MetadataReader (ModuleDefinition module) - : base (module.Image.MetadataSection.Data) + : base (module.Image.TableHeap.data) { this.image = module.Image; this.module = module; this.metadata = module.MetadataSystem; - this.code = new CodeReader (image.MetadataSection, this); } int GetCodedIndexSize (CodedIndex index) @@ -663,17 +661,9 @@ namespace Mono.Cecil { public MemoryStream GetManagedResourceStream (uint offset) { - var rva = image.Resources.VirtualAddress; - var section = image.GetSectionAtVirtualAddress (rva); - var position = (rva - section.VirtualAddress) + offset; - var buffer = section.Data; - - var length = buffer [position] - | (buffer [position + 1] << 8) - | (buffer [position + 2] << 16) - | (buffer [position + 3] << 24); - - return new MemoryStream (buffer, (int) position + 4, length); + var reader = image.GetReaderAt (image.Resources.VirtualAddress); + reader.Advance ((int) offset); + return new MemoryStream (reader.ReadBytes (reader.ReadInt32 ())); } void PopulateVersionAndFlags (AssemblyNameReference name) @@ -1234,13 +1224,8 @@ namespace Mono.Cecil { byte [] GetFieldInitializeValue (int size, RVA rva) { - var section = image.GetSectionAtVirtualAddress (rva); - if (section == null) - return Empty<byte>.Array; - - var value = new byte [size]; - Buffer.BlockCopy (section.Data, (int) (rva - section.VirtualAddress), value, 0, size); - return value; + var reader = image.GetReaderAt (rva); + return reader.ReadBytes (size); } static int GetFieldTypeSize (TypeReference type) @@ -2021,7 +2006,7 @@ namespace Mono.Cecil { public MethodBody ReadMethodBody (MethodDefinition method) { - return code.ReadMethodBody (method); + return CodeReader.ReadMethodBody (method, this); } public CallSite ReadCallSite (MetadataToken token) @@ -2694,19 +2679,12 @@ namespace Mono.Cecil { } public SignatureReader (uint blob, MetadataReader reader) - : base (reader.buffer) + : base (reader.image.BlobHeap.data) { this.reader = reader; - - MoveToBlob (blob); - - this.sig_length = ReadCompressedUInt32 (); - this.start = (uint) position; - } - - void MoveToBlob (uint blob) - { - position = (int) (reader.image.BlobHeap.Offset + blob); + this.start = blob; + this.position = (int) this.start; + this.sig_length = ReadCompressedUInt32(); } MetadataToken ReadTypeTokenSignature () @@ -3221,7 +3199,7 @@ namespace Mono.Cecil { public bool CanReadMore () { - return position - start < sig_length; + return position - start <= sig_length; } } } diff --git a/Mono.Cecil/AssemblyWriter.cs b/Mono.Cecil/AssemblyWriter.cs index f5bfe73..4b0d919 100644 --- a/Mono.Cecil/AssemblyWriter.cs +++ b/Mono.Cecil/AssemblyWriter.cs @@ -94,6 +94,9 @@ namespace Mono.Cecil { var writer = ImageWriter.CreateWriter (module, metadata, stream); + if (module.HasImage) + module.Image.Dispose (); + writer.WriteImage (); #if !SILVERLIGHT && !CF diff --git a/Mono.Cecil/DefaultAssemblyResolver.cs b/Mono.Cecil/DefaultAssemblyResolver.cs index a21405b..eb87277 100644 --- a/Mono.Cecil/DefaultAssemblyResolver.cs +++ b/Mono.Cecil/DefaultAssemblyResolver.cs @@ -13,7 +13,9 @@ using System.Collections.Generic; namespace Mono.Cecil { - public class DefaultAssemblyResolver : BaseAssemblyResolver { + class AssemblyResolver : BaseAssemblyResolver {} + + public class DefaultAssemblyResolver : BaseAssemblyResolver, IDisposable { readonly IDictionary<string, AssemblyDefinition> cache; @@ -48,5 +50,13 @@ namespace Mono.Cecil { cache [name] = assembly; } + + public void Dispose () + { + foreach (var assembly in cache.Values) + assembly.Dispose (); + + cache.Clear (); + } } } diff --git a/Mono.Cecil/ModuleDefinition.cs b/Mono.Cecil/ModuleDefinition.cs index cadba1c..c665d1e 100644 --- a/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Cecil/ModuleDefinition.cs @@ -212,7 +212,7 @@ namespace Mono.Cecil { #endif - public sealed class ModuleDefinition : ModuleReference, ICustomAttributeProvider { + public sealed class ModuleDefinition : ModuleReference, ICustomAttributeProvider, IDisposable { internal Image Image; internal MetadataSystem MetadataSystem; @@ -342,7 +342,7 @@ namespace Mono.Cecil { public IAssemblyResolver AssemblyResolver { get { if (assembly_resolver == null) - Interlocked.CompareExchange (ref assembly_resolver, new DefaultAssemblyResolver (), null); + Interlocked.CompareExchange (ref assembly_resolver, new AssemblyResolver (), null); return assembly_resolver; } @@ -515,12 +515,21 @@ namespace Mono.Cecil { this.architecture = image.Architecture; this.attributes = image.Attributes; this.characteristics = image.Characteristics; - this.fq_name = image.FileName; + this.fq_name = image.Stream.GetFullyQualifiedName (); this.reader = new MetadataReader (this); } - public bool HasTypeReference (string fullName) + public void Dispose () + { + if (Image != null) + Image.Dispose (); + + if (SymbolReader != null) + SymbolReader.Dispose (); + } + + public bool HasTypeReference (string fullName) { return HasTypeReference (string.Empty, fullName); } @@ -1004,9 +1013,7 @@ namespace Mono.Cecil { public static ModuleDefinition ReadModule (string fileName, ReaderParameters parameters) { - using (var stream = GetFileStream (fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) { - return ReadModule (stream, parameters); - } + return ReadModule (GetFileStream (fileName, FileMode.Open, FileAccess.Read, FileShare.Read), parameters); } static void CheckStream (object stream) diff --git a/Test/Mono.Cecil.Tests/BaseTestFixture.cs b/Test/Mono.Cecil.Tests/BaseTestFixture.cs index 2cba703..2d0dcb2 100644 --- a/Test/Mono.Cecil.Tests/BaseTestFixture.cs +++ b/Test/Mono.Cecil.Tests/BaseTestFixture.cs @@ -96,20 +96,20 @@ namespace Mono.Cecil.Tests { private static void Run (TestCase testCase) { - var runner = new TestRunner (testCase, TestCaseType.ReadDeferred); - runner.RunTest (); + using (var runner = new TestRunner (testCase, TestCaseType.ReadDeferred)) + runner.RunTest (); - runner = new TestRunner (testCase, TestCaseType.ReadImmediate); - runner.RunTest (); + using (var runner = new TestRunner (testCase, TestCaseType.ReadImmediate)) + runner.RunTest (); if (testCase.ReadOnly) return; - runner = new TestRunner (testCase, TestCaseType.WriteFromDeferred); - runner.RunTest(); + using (var runner = new TestRunner (testCase, TestCaseType.WriteFromDeferred)) + runner.RunTest(); - runner = new TestRunner (testCase, TestCaseType.WriteFromImmediate); - runner.RunTest(); + using (var runner = new TestRunner (testCase, TestCaseType.WriteFromImmediate)) + runner.RunTest(); } } @@ -189,11 +189,14 @@ namespace Mono.Cecil.Tests { } } - class TestRunner { + class TestRunner : IDisposable { readonly TestCase test_case; readonly TestCaseType type; + ModuleDefinition test_module; + DefaultAssemblyResolver test_resolver; + public TestRunner (TestCase testCase, TestCaseType type) { this.test_case = testCase; @@ -205,12 +208,12 @@ namespace Mono.Cecil.Tests { var location = test_case.ModuleLocation; var directory = Path.GetDirectoryName (location); - var resolver = new DefaultAssemblyResolver (); - resolver.AddSearchDirectory (directory); + test_resolver = new DefaultAssemblyResolver (); + test_resolver.AddSearchDirectory (directory); var parameters = new ReaderParameters { SymbolReaderProvider = GetSymbolReaderProvider (), - AssemblyResolver = resolver, + AssemblyResolver = test_resolver, }; switch (type) { @@ -275,7 +278,14 @@ namespace Mono.Cecil.Tests { if (module == null) return; - test_case.Test(module); + test_module = module; + test_case.Test (module); + } + + public void Dispose () + { + test_module.Dispose (); + test_resolver.Dispose (); } } diff --git a/Test/Mono.Cecil.Tests/CustomAttributesTests.cs b/Test/Mono.Cecil.Tests/CustomAttributesTests.cs index 5a1d4b6..8379c8f 100644 --- a/Test/Mono.Cecil.Tests/CustomAttributesTests.cs +++ b/Test/Mono.Cecil.Tests/CustomAttributesTests.cs @@ -445,6 +445,8 @@ namespace Mono.Cecil.Tests { Assert.IsNotNull (attribute); Assert.AreEqual ("CaBlob", (string) attribute.ConstructorArguments [0].Value); + + module.Dispose (); } static void AssertCustomAttribute (string expected, CustomAttribute attribute) diff --git a/Test/Mono.Cecil.Tests/Extensions.cs b/Test/Mono.Cecil.Tests/Extensions.cs index 0788d21..ab27e9b 100644 --- a/Test/Mono.Cecil.Tests/Extensions.cs +++ b/Test/Mono.Cecil.Tests/Extensions.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using SR = System.Reflection; @@ -20,7 +21,7 @@ namespace Mono.Cecil.Tests { public static TypeDefinition ToDefinition (this Type self) { - var module = ModuleDefinition.ReadModule (self.Module.FullyQualifiedName); + var module = ModuleDefinition.ReadModule (new MemoryStream (File.ReadAllBytes (self.Module.FullyQualifiedName))); return (TypeDefinition) module.LookupToken (self.MetadataToken); } diff --git a/Test/Mono.Cecil.Tests/ImageReadTests.cs b/Test/Mono.Cecil.Tests/ImageReadTests.cs index 1211658..4f737c4 100644 --- a/Test/Mono.Cecil.Tests/ImageReadTests.cs +++ b/Test/Mono.Cecil.Tests/ImageReadTests.cs @@ -15,77 +15,78 @@ namespace Mono.Cecil.Tests { [Test] public void ImageSections () { - var image = GetResourceImage ("hello.exe"); - - Assert.AreEqual (3, image.Sections.Length); - Assert.AreEqual (".text", image.Sections [0].Name); - Assert.AreEqual (".rsrc", image.Sections [1].Name); - Assert.AreEqual (".reloc", image.Sections [2].Name); + using (var image = GetResourceImage ("hello.exe")) { + Assert.AreEqual (3, image.Sections.Length); + Assert.AreEqual (".text", image.Sections [0].Name); + Assert.AreEqual (".rsrc", image.Sections [1].Name); + Assert.AreEqual (".reloc", image.Sections [2].Name); + } } [Test] public void ImageMetadataVersion () { - var image = GetResourceImage ("hello.exe"); - Assert.AreEqual (TargetRuntime.Net_2_0, image.RuntimeVersion.ParseRuntime ()); + using (var image = GetResourceImage ("hello.exe")) + Assert.AreEqual (TargetRuntime.Net_2_0, image.RuntimeVersion.ParseRuntime ()); - image = GetResourceImage ("hello1.exe"); - Assert.AreEqual (TargetRuntime.Net_1_1, image.RuntimeVersion.ParseRuntime ()); + using (var image = GetResourceImage ("hello1.exe")) + Assert.AreEqual (TargetRuntime.Net_1_1, image.RuntimeVersion.ParseRuntime ()); } [Test] public void ImageModuleKind () { - var image = GetResourceImage ("hello.exe"); - Assert.AreEqual (ModuleKind.Console, image.Kind); + using (var image = GetResourceImage ("hello.exe")) + Assert.AreEqual (ModuleKind.Console, image.Kind); - image = GetResourceImage ("libhello.dll"); - Assert.AreEqual (ModuleKind.Dll, image.Kind); + using (var image = GetResourceImage ("libhello.dll")) + Assert.AreEqual (ModuleKind.Dll, image.Kind); - image = GetResourceImage ("hellow.exe"); - Assert.AreEqual (ModuleKind.Windows, image.Kind); + using (var image = GetResourceImage ("hellow.exe")) + Assert.AreEqual (ModuleKind.Windows, image.Kind); } [Test] public void MetadataHeaps () { - var image = GetResourceImage ("hello.exe"); - - Assert.IsNotNull (image.TableHeap); + using (var image = GetResourceImage ("hello.exe")) { + Assert.IsNotNull (image.TableHeap); - Assert.IsNotNull (image.StringHeap); - Assert.AreEqual (string.Empty, image.StringHeap.Read (0)); - Assert.AreEqual ("<Module>", image.StringHeap.Read (1)); + Assert.IsNotNull (image.StringHeap); + Assert.AreEqual (string.Empty, image.StringHeap.Read (0)); + Assert.AreEqual ("<Module>", image.StringHeap.Read (1)); - Assert.IsNotNull (image.UserStringHeap); - Assert.AreEqual (string.Empty, image.UserStringHeap.Read (0)); - Assert.AreEqual ("Hello Cecil World !", image.UserStringHeap.Read (1)); + Assert.IsNotNull (image.UserStringHeap); + Assert.AreEqual (string.Empty, image.UserStringHeap.Read (0)); + Assert.AreEqual ("Hello Cecil World !", image.UserStringHeap.Read (1)); - Assert.IsNotNull (image.GuidHeap); - Assert.AreEqual (new Guid (), image.GuidHeap.Read (0)); - Assert.AreEqual (new Guid ("C3BC2BD3-2576-4D00-A80E-465B5632415F"), image.GuidHeap.Read (1)); + Assert.IsNotNull (image.GuidHeap); + Assert.AreEqual (new Guid (), image.GuidHeap.Read (0)); + Assert.AreEqual (new Guid ("C3BC2BD3-2576-4D00-A80E-465B5632415F"), image.GuidHeap.Read (1)); - Assert.IsNotNull (image.BlobHeap); - Assert.AreEqual (new byte [0], image.BlobHeap.Read (0)); + Assert.IsNotNull (image.BlobHeap); + Assert.AreEqual (new byte [0], image.BlobHeap.Read (0)); + } } [Test] public void TablesHeap () { - var image = GetResourceImage ("hello.exe"); - var heap = image.TableHeap; - - Assert.IsNotNull (heap); - - Assert.AreEqual (1, heap [Table.Module].Length); - Assert.AreEqual (4, heap [Table.TypeRef].Length); - Assert.AreEqual (2, heap [Table.TypeDef].Length); - Assert.AreEqual (0, heap [Table.Field].Length); - Assert.AreEqual (2, heap [Table.Method].Length); - Assert.AreEqual (4, heap [Table.MemberRef].Length); - Assert.AreEqual (2, heap [Table.CustomAttribute].Length); - Assert.AreEqual (1, heap [Table.Assembly].Length); - Assert.AreEqual (1, heap [Table.AssemblyRef].Length); + using (var image = GetResourceImage ("hello.exe")) { + var heap = image.TableHeap; + + Assert.IsNotNull (heap); + + Assert.AreEqual (1, heap [Table.Module].Length); + Assert.AreEqual (4, heap [Table.TypeRef].Length); + Assert.AreEqual (2, heap [Table.TypeDef].Length); + Assert.AreEqual (0, heap [Table.Field].Length); + Assert.AreEqual (2, heap [Table.Method].Length); + Assert.AreEqual (4, heap [Table.MemberRef].Length); + Assert.AreEqual (2, heap [Table.CustomAttribute].Length); + Assert.AreEqual (1, heap [Table.Assembly].Length); + Assert.AreEqual (1, heap [Table.AssemblyRef].Length); + } } [Test] diff --git a/Test/Mono.Cecil.Tests/ImportCecilTests.cs b/Test/Mono.Cecil.Tests/ImportCecilTests.cs index 92f0cd7..299948f 100644 --- a/Test/Mono.Cecil.Tests/ImportCecilTests.cs +++ b/Test/Mono.Cecil.Tests/ImportCecilTests.cs @@ -214,11 +214,11 @@ namespace Mono.Cecil.Tests { public void ImportMethodOnOpenGeneric () { var generic = typeof (Generic<>).ToDefinition (); - var module = ModuleDefinition.CreateModule ("foo", ModuleKind.Dll); - var method = module.ImportReference (generic.GetMethod ("Method")); - - Assert.AreEqual ("T Mono.Cecil.Tests.ImportCecilTests/Generic`1::Method(T)", method.FullName); + using (var module = ModuleDefinition.CreateModule ("foo", ModuleKind.Dll)) { + var method = module.ImportReference (generic.GetMethod ("Method")); + Assert.AreEqual ("T Mono.Cecil.Tests.ImportCecilTests/Generic`1::Method(T)", method.FullName); + } } public class ContextGeneric1Method2<G1> diff --git a/Test/Mono.Cecil.Tests/ImportReflectionTests.cs b/Test/Mono.Cecil.Tests/ImportReflectionTests.cs index cef6d20..71412af 100644 --- a/Test/Mono.Cecil.Tests/ImportReflectionTests.cs +++ b/Test/Mono.Cecil.Tests/ImportReflectionTests.cs @@ -209,13 +209,13 @@ namespace Mono.Cecil.Tests { [Test] public void ImportGenericTypeDefOrOpen () { - var module = typeof (Foo<>).ToDefinition ().Module; + using (var module = typeof (Foo<>).ToDefinition ().Module) { + var foo_def = module.ImportReference (typeof (Foo<>)); + var foo_open = module.ImportReference (typeof (Foo<>), foo_def); - var foo_def = module.ImportReference (typeof (Foo<>)); - var foo_open = module.ImportReference (typeof (Foo<>), foo_def); - - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1", foo_def.FullName); - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>", foo_open.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1", foo_def.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>", foo_open.FullName); + } } [Test] @@ -225,12 +225,12 @@ namespace Mono.Cecil.Tests { var generic_list_foo_open = typeof (Generic<>).MakeGenericType (list_foo); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var generic_foo = module.ImportReference (generic_list_foo_open, foo_def); - var generic_foo = module.ImportReference (generic_list_foo_open, foo_def); - - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>", - generic_foo.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>", + generic_foo.FullName); + } } [Test] @@ -240,12 +240,12 @@ namespace Mono.Cecil.Tests { var generic_foo_open = typeof (Generic<>).MakeGenericType (foo_open); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var generic_foo = module.ImportReference (generic_foo_open, foo_def); - var generic_foo = module.ImportReference (generic_foo_open, foo_def); - - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>>", - generic_foo.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Generic`1<Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>>", + generic_foo.FullName); + } } [Test] @@ -255,12 +255,12 @@ namespace Mono.Cecil.Tests { var foo_open_array = foo_open.MakeArrayType (); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var array_foo = module.ImportReference (foo_open_array, foo_def); - var array_foo = module.ImportReference (foo_open_array, foo_def); - - Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>[]", - array_foo.FullName); + Assert.AreEqual ("Mono.Cecil.Tests.ImportReflectionTests/Foo`1<TFoo>[]", + array_foo.FullName); + } } [Test] @@ -271,12 +271,12 @@ namespace Mono.Cecil.Tests { var generic_list_foo_open_field = generic_list_foo_open.GetField ("Field"); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var generic_field = module.ImportReference (generic_list_foo_open_field, foo_def); - var generic_field = module.ImportReference (generic_list_foo_open_field, foo_def); - - Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Field", - generic_field.FullName); + Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Field", + generic_field.FullName); + } } [Test] @@ -287,36 +287,36 @@ namespace Mono.Cecil.Tests { var generic_list_foo_open_method = generic_list_foo_open.GetMethod ("Method"); var foo_def = typeof (Foo<>).ToDefinition (); - var module = foo_def.Module; + using (var module = foo_def.Module) { + var generic_method = module.ImportReference (generic_list_foo_open_method, foo_def); - var generic_method = module.ImportReference (generic_list_foo_open_method, foo_def); - - Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Method(T)", - generic_method.FullName); + Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<System.Collections.Generic.List`1<TFoo>>::Method(T)", + generic_method.FullName); + } } [Test] public void ImportMethodOnOpenGenericType () { - var module = typeof (Generic<>).ToDefinition ().Module; + using (var module = typeof (Generic<>).ToDefinition ().Module) { + var method = module.ImportReference (typeof (Generic<>).GetMethod ("Method")); - var method = module.ImportReference (typeof (Generic<>).GetMethod ("Method")); - - Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::Method(T)", method.FullName); + Assert.AreEqual ("T Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::Method(T)", method.FullName); + } } [Test] public void ImportGenericMethodOnOpenGenericType () { - var module = typeof (Generic<>).ToDefinition ().Module; + using (var module = typeof (Generic<>).ToDefinition ().Module) { + var generic_method = module.ImportReference (typeof (Generic<>).GetMethod ("GenericMethod")); - var generic_method = module.ImportReference (typeof (Generic<>).GetMethod ("GenericMethod")); + Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod(T,TS)", generic_method.FullName); - Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod(T,TS)", generic_method.FullName); + generic_method = module.ImportReference (typeof (Generic<>).GetMethod ("GenericMethod"), generic_method); - generic_method = module.ImportReference (typeof (Generic<>).GetMethod ("GenericMethod"), generic_method); - - Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod<TS>(T,TS)", generic_method.FullName); + Assert.AreEqual ("TS Mono.Cecil.Tests.ImportReflectionTests/Generic`1<T>::GenericMethod<TS>(T,TS)", generic_method.FullName); + } } delegate void Emitter (ModuleDefinition module, MethodBody body); diff --git a/Test/Mono.Cecil.Tests/ModuleTests.cs b/Test/Mono.Cecil.Tests/ModuleTests.cs index 84efb4a..e8b9484 100644 --- a/Test/Mono.Cecil.Tests/ModuleTests.cs +++ b/Test/Mono.Cecil.Tests/ModuleTests.cs @@ -225,10 +225,10 @@ namespace Mono.Cecil.Tests { [Test] public void MixedModeModule () { - var module = GetResourceModule ("cppcli.dll"); - - Assert.AreEqual (1, module.ModuleReferences.Count); - Assert.AreEqual (string.Empty, module.ModuleReferences [0].Name); + using (var module = GetResourceModule ("cppcli.dll")) { + Assert.AreEqual (1, module.ModuleReferences.Count); + Assert.AreEqual (string.Empty, module.ModuleReferences [0].Name); + } } [Test] @@ -239,40 +239,28 @@ namespace Mono.Cecil.Tests { } [Test] - public void WriteModuleTwice () - { - var module = GetResourceModule ("iterator.exe"); - - var path = Path.Combine (Path.GetTempPath (), "cecil"); - var file = Path.Combine (path, "iteratorrt.exe"); - - module.Write (file); - module.Write (file); - } - - [Test] public void GetTypeNamespacePlusName () { - var module = GetResourceModule ("moda.netmodule"); - - var type = module.GetType ("Module.A", "Foo"); - Assert.IsNotNull (type); + using (var module = GetResourceModule ("moda.netmodule")) { + var type = module.GetType ("Module.A", "Foo"); + Assert.IsNotNull (type); + } } [Test] public void OpenModuleImmediate () { - var module = GetResourceModule ("hello.exe", ReadingMode.Immediate); - - Assert.AreEqual (ReadingMode.Immediate, module.ReadingMode); + using (var module = GetResourceModule ("hello.exe", ReadingMode.Immediate)) { + Assert.AreEqual (ReadingMode.Immediate, module.ReadingMode); + } } [Test] public void OpenModuleDeferred () { - var module = GetResourceModule ("hello.exe", ReadingMode.Deferred); - - Assert.AreEqual (ReadingMode.Deferred, module.ReadingMode); + using (var module = GetResourceModule ("hello.exe", ReadingMode.Deferred)) { + Assert.AreEqual (ReadingMode.Deferred, module.ReadingMode); + } } } } diff --git a/Test/Mono.Cecil.Tests/ResolveTests.cs b/Test/Mono.Cecil.Tests/ResolveTests.cs index 00bfdd0..02eeb8c 100644 --- a/Test/Mono.Cecil.Tests/ResolveTests.cs +++ b/Test/Mono.Cecil.Tests/ResolveTests.cs @@ -113,12 +113,13 @@ namespace Mono.Cecil.Tests { resolver.Register (mma.Assembly); - var current_module = GetCurrentModule (parameters); - var reference = new TypeReference ("Module.A", "Foo", current_module, AssemblyNameReference.Parse (mma.Assembly.FullName), false); + using (var current_module = GetCurrentModule (parameters)) { + var reference = new TypeReference ("Module.A", "Foo", current_module, AssemblyNameReference.Parse (mma.Assembly.FullName), false); - var definition = reference.Resolve (); - Assert.IsNotNull (definition); - Assert.AreEqual ("Module.A.Foo", definition.FullName); + var definition = reference.Resolve (); + Assert.IsNotNull (definition); + Assert.AreEqual ("Module.A.Foo", definition.FullName); + } } [Test] diff --git a/rocks/Mono.Cecil.Rocks/ILParser.cs b/rocks/Mono.Cecil.Rocks/ILParser.cs index e88fe5a..a6b9383 100644 --- a/rocks/Mono.Cecil.Rocks/ILParser.cs +++ b/rocks/Mono.Cecil.Rocks/ILParser.cs @@ -61,8 +61,6 @@ namespace Mono.Cecil.Rocks { var context = CreateContext (method, visitor); var code = context.Code; - code.MoveTo (method.RVA); - var flags = code.ReadByte (); switch (flags & 0x3) { @@ -71,7 +69,7 @@ namespace Mono.Cecil.Rocks { ParseCode (code_size, context); break; case 0x3: // fat - code.position--; + code.Advance (-1); ParseFatMethod (context); break; default: @@ -81,7 +79,7 @@ namespace Mono.Cecil.Rocks { static ParseContext CreateContext (MethodDefinition method, IILVisitor visitor) { - var code = method.Module.Read (method, (_, reader) => new CodeReader (reader.image.MetadataSection, reader)); + var code = method.Module.Read (method, (m, reader) => new CodeReader (method, reader)); return new ParseContext { Code = code, @@ -110,10 +108,10 @@ namespace Mono.Cecil.Rocks { var metadata = context.Metadata; var visitor = context.Visitor; - var start = code.position; + var start = code.Position; var end = start + code_size; - while (code.position < end) { + while (code.Position < end) { var il_opcode = code.ReadByte (); var opcode = il_opcode != 0xfe ? OpCodes.OneByteOpCode [il_opcode] |