/// <summary> /// Method to open the module in the MetadataReader. This method loads the module and returns the object corresponding to the opened module. /// Also returns the ModuleIDentifier corresponding to the module as the out parameter. Modules are opened as if they are not contained in any assembly. /// </summary> /// <param name="binaryDocument">The binary document that needes to be opened as an module.</param> /// <param name="moduleIdentity">Contains the module identity of the binary document in case it is an module.</param> /// <returns>Module that is loaded or Dummy.Module in case module could not be loaded.</returns> public IModule OpenModule( IBinaryDocument binaryDocument, out ModuleIdentity /*?*/ moduleIdentity ) { moduleIdentity = null; lock (GlobalLock.LockingObject) { IBinaryDocumentMemoryBlock /*?*/ binaryDocumentMemoryBlock = this.metadataReaderHost.OpenBinaryDocument(binaryDocument); if (binaryDocumentMemoryBlock == null) { // Error... return(Dummy.Module); } PEFileReader peFileReader = new PEFileReader(this, binaryDocumentMemoryBlock); if (peFileReader.ReaderState < ReaderState.Metadata) { // Error... return(Dummy.Module); } //^ assert peFileReader.ReaderState >= ReaderState.Metadata; if (peFileReader.IsAssembly) { AssemblyIdentity assemblyIdentity = this.GetAssemblyIdentifier(peFileReader); moduleIdentity = assemblyIdentity; Assembly /*?*/ lookupAssembly = this.LookupAssembly(null, assemblyIdentity); if (lookupAssembly != null) { return(lookupAssembly); } } else { moduleIdentity = this.GetModuleIdentifier(peFileReader); Module /*?*/ lookupModule = this.LookupModule(null, moduleIdentity); if (lookupModule != null) { return(lookupModule); } } try { PEFileToObjectModel peFileToObjectModel = new PEFileToObjectModel(this, peFileReader, moduleIdentity, null, this.metadataReaderHost.PointerSize); this.LoadedModule(peFileToObjectModel.Module); Assembly /*?*/ assembly = peFileToObjectModel.Module as Assembly; if (assembly != null) { this.OpenMemberModules(binaryDocument, assembly); } return(peFileToObjectModel.Module); } catch (MetadataReaderException) { // Error... } } return(Dummy.Module); }
internal MemoryBlockEnumerator( IBinaryDocumentMemoryBlock binaryDocumentMemoryBlock ) { this.BinaryDocumentMemoryBlock = binaryDocumentMemoryBlock; this.pointer = binaryDocumentMemoryBlock.Pointer; this.length = (int)binaryDocumentMemoryBlock.Length; this.currentOffset = -1; }
/// <summary> /// Opens a binary document. /// </summary> public override IBinaryDocumentMemoryBlock OpenBinaryDocument(IBinaryDocument sourceDocument) { try { IBinaryDocumentMemoryBlock binDocMemoryBlock = UnmanagedBinaryMemoryBlock.CreateUnmanagedBinaryMemoryBlock(sourceDocument.Location, sourceDocument); return(binDocMemoryBlock); } catch (IOException) { return(null); } }
internal MemoryBlockEnumerator( IBinaryDocumentMemoryBlock binaryDocumentMemoryBlock ) { _binaryDocumentMemoryBlock = binaryDocumentMemoryBlock; _pointer = binaryDocumentMemoryBlock.Pointer; _length = (int)binaryDocumentMemoryBlock.Length; _currentOffset = -1; }
/// <summary> /// override this here to not use memory mapped files since we want to use asmmeta in msbuild and it is sticky /// </summary> public override IBinaryDocumentMemoryBlock /*?*/ OpenBinaryDocument(IBinaryDocument sourceDocument) { try { IBinaryDocumentMemoryBlock binDocMemoryBlock = UnmanagedBinaryMemoryBlock.CreateUnmanagedBinaryMemoryBlock(sourceDocument.Location, sourceDocument); this.disposableObjectAllocatedByThisHost.Add((IDisposable)binDocMemoryBlock); return(binDocMemoryBlock); } catch (IOException) { return(null); } }
public override IBinaryDocumentMemoryBlock OpenBinaryDocument(IBinaryDocument sourceDocument) { VSServiceProvider.Current.Logger.WriteToLog("Opening document: " + sourceDocument.Name + " from: " + sourceDocument.Location); try { IBinaryDocumentMemoryBlock binDocMemoryBlock = UnmanagedBinaryMemoryBlock.CreateUnmanagedBinaryMemoryBlock(sourceDocument.Location, sourceDocument); return(binDocMemoryBlock); } catch (IOException) { VSServiceProvider.Current.Logger.WriteToLog("Failed to open document."); return(null); } }
/// <summary> /// This method loads a module from a file containing only the metadata section of a PE file. (No headers and no IL.) /// </summary> /// <param name="binaryDocument">The binary document that needes to be opened as an module.</param> /// <returns>Module that is loaded or Dummy.Module in case module could not be loaded.</returns> public IModule OpenSnapshot(IBinaryDocument binaryDocument) { ModuleIdentity moduleIdentity; lock (GlobalLock.LockingObject) { IBinaryDocumentMemoryBlock /*?*/ binaryDocumentMemoryBlock = this.metadataReaderHost.OpenBinaryDocument(binaryDocument); if (binaryDocumentMemoryBlock == null) { // Error... return(Dummy.Module); } PEFileReader peFileReader = new PEFileReader(this, binaryDocumentMemoryBlock, snapshot: true); if (peFileReader.ReaderState < ReaderState.Metadata) { // Error... return(Dummy.Module); } if (peFileReader.IsAssembly) { AssemblyIdentity assemblyIdentity = this.GetAssemblyIdentifier(peFileReader); moduleIdentity = assemblyIdentity; Assembly /*?*/ lookupAssembly = this.LookupAssembly(null, assemblyIdentity); if (lookupAssembly != null) { return(lookupAssembly); } } else { moduleIdentity = this.GetModuleIdentifier(peFileReader); Module /*?*/ lookupModule = this.LookupModule(null, moduleIdentity); if (lookupModule != null) { return(lookupModule); } } if (peFileReader.ReaderState < ReaderState.Metadata) { // Error... return(Dummy.Module); } try { PEFileToObjectModel peFileToObjectModel = new PEFileToObjectModel(this, peFileReader, moduleIdentity, null, this.metadataReaderHost.PointerSize); this.LoadedModule(peFileToObjectModel.Module); CciEventSource.Log.ModuleOpened(peFileToObjectModel.Module, moduleIdentity, binaryDocument.Length); return(peFileToObjectModel.Module); } catch (MetadataReaderException) { // Error... } } return(Dummy.Module); }
/// <summary> /// This method is called when an assembly is loaded. This makes sure that all the member modules of the assembly are loaded. /// </summary> /// <param name="binaryDocument"></param> /// <param name="assembly"></param> private void OpenMemberModules(IBinaryDocument binaryDocument, Assembly assembly) { List <Module> memberModuleList = new List <Module>(); AssemblyIdentity assemblyIdentity = assembly.AssemblyIdentity; foreach (IFileReference fileRef in assembly.PEFileToObjectModel.GetFiles()) { if (!fileRef.HasMetadata) { continue; } IBinaryDocumentMemoryBlock /*?*/ binaryDocumentMemoryBlock = this.metadataReaderHost.OpenBinaryDocument(binaryDocument, fileRef.FileName.Value); if (binaryDocumentMemoryBlock == null) { // Error... continue; } try { PEFileReader peFileReader = new PEFileReader(this, binaryDocumentMemoryBlock); if (peFileReader.ReaderState < ReaderState.Metadata) { // Error... continue; } if (peFileReader.IsAssembly) { // Error... continue; } ModuleIdentity moduleIdentity = this.GetModuleIdentifier(peFileReader, assemblyIdentity); PEFileToObjectModel peFileToObjectModel = new PEFileToObjectModel(this, peFileReader, moduleIdentity, assembly, this.metadataReaderHost.PointerSize); memberModuleList.Add(peFileToObjectModel.Module); } catch (MetadataReaderException) { continue; } } if (memberModuleList.Count == 0) { return; } assembly.SetMemberModules(new EnumerableArrayWrapper <Module, IModule>(memberModuleList.ToArray(), Dummy.Module)); }
/// <summary> /// Method to open the assembly in MetadataReader. This method loads the assembly and returns the object corresponding to the /// opened assembly. Also returns the AssemblyIdentifier corresponding to the assembly as the out parameter. /// Only assemblies that unify to themselves can be opened i.e. if the unification policy of the compilation host says that mscorlib 1.0 unifies to mscorlib 2.0 /// then only mscorlib 2.0 can be loaded. /// </summary> /// <param name="binaryDocument">The binary document that needes to be opened as an assembly.</param> /// <param name="assemblyIdentity">Contains the assembly identifier of the binary document in case it is an assembly.</param> /// <returns>Assembly that is loaded or Dummy.Assembly in case assembly could not be loaded.</returns> public IAssembly OpenAssembly(IBinaryDocument binaryDocument, out AssemblyIdentity /*?*/ assemblyIdentity) { Contract.Requires(binaryDocument != null); Contract.Ensures(Contract.Result <IAssembly>() != null); assemblyIdentity = null; lock (GlobalLock.LockingObject) { IBinaryDocumentMemoryBlock /*?*/ binaryDocumentMemoryBlock = this.metadataReaderHost.OpenBinaryDocument(binaryDocument); if (binaryDocumentMemoryBlock == null) { // Error... return(Dummy.Assembly); } PEFileReader peFileReader = new PEFileReader(this, binaryDocumentMemoryBlock); if (peFileReader.ReaderState < ReaderState.Metadata) { // Error... return(Dummy.Assembly); } //^ assert peFileReader.ReaderState >= ReaderState.Metadata; if (!peFileReader.IsAssembly) { // Error... return(Dummy.Assembly); } assemblyIdentity = this.GetAssemblyIdentifier(peFileReader); Assembly /*?*/ lookupAssembly = this.LookupAssembly(null, assemblyIdentity); if (lookupAssembly != null) { return(lookupAssembly); } try { PEFileToObjectModel peFileToObjectModel = new PEFileToObjectModel(this, peFileReader, assemblyIdentity, null, this.metadataReaderHost.PointerSize); Assembly /*?*/ assembly = peFileToObjectModel.Module as Assembly; //^ assert assembly != null; this.LoadedModule(assembly); CciEventSource.Log.ModuleOpened(assembly, assemblyIdentity, binaryDocument.Length); this.OpenMemberModules(binaryDocument, assembly); return(assembly); } catch (MetadataReaderException) { return(Dummy.Assembly); } } }
/// <summary> /// If the given binary document contains a CLR assembly, return the identity of the assembly. Otherwise, return null. /// </summary> public AssemblyIdentity /*?*/ GetAssemblyIdentifier(IBinaryDocument binaryDocument) { IBinaryDocumentMemoryBlock /*?*/ binaryDocumentMemoryBlock = this.metadataReaderHost.OpenBinaryDocument(binaryDocument); if (binaryDocumentMemoryBlock == null) { return(null); } PEFileReader peFileReader = new PEFileReader(this, binaryDocumentMemoryBlock); if (peFileReader.ReaderState < ReaderState.Metadata) { return(null); } if (!peFileReader.IsAssembly) { return(null); } return(this.GetAssemblyIdentifier(peFileReader)); }
internal EnumerableBinaryDocumentMemoryBlockWrapper( IBinaryDocumentMemoryBlock binaryDocumentMemoryBlock ) { this.BinaryDocumentMemoryBlock = binaryDocumentMemoryBlock; }
/*^ #pragma warning disable 2674, 2666 ^*/ internal PEFileReader( PeReader moduleReadWriteFactory, IBinaryDocumentMemoryBlock binaryDocumentMemoryBlock ) { this.ErrorContainer = new MetadataErrorContainer(moduleReadWriteFactory, binaryDocumentMemoryBlock.BinaryDocument); this.ReaderState = ReaderState.Initialized; this.BinaryDocumentMemoryBlock = binaryDocumentMemoryBlock; if (!this.ReadPEFileLevelData()) return; if (!this.ReadCORModuleLevelData()) return; if (!this.ReadMetadataLevelData()) return; // TODO: Add phase for Metadata validation of offsets type row ids etc... }
internal PEFileReader(PeReader moduleReadWriteFactory, IBinaryDocumentMemoryBlock binaryDocumentMemoryBlock, bool snapshot = false) { this.ErrorContainer = new MetadataErrorContainer(moduleReadWriteFactory, binaryDocumentMemoryBlock.BinaryDocument); this.ReaderState = ReaderState.Initialized; this.BinaryDocumentMemoryBlock = binaryDocumentMemoryBlock; if (!snapshot) { if (!this.ReadPEFileLevelData()) return; if (!this.ReadCORModuleLevelData()) return; } else { this.SectionHeaders = new SectionHeader[0]; if (!this.ReadMetadataRoot()) return; } if (!this.ReadMetadataLevelData()) return; // TODO: Add phase for Metadata validation of offsets type row ids etc... }