Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/core/Microsoft.Scripting.Metadata/ClrStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
namespace Microsoft.Scripting.Metadata {
internal static class ClrStubs {
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters")]
internal static unsafe int GetCharCount(this Encoding encoding, byte* bytes, int byteCount, object nls) {
internal static unsafe int GetCharCount(this Encoding encoding, byte* bytes, int byteCount, object? nls) {
return encoding.GetCharCount(bytes, byteCount);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters")]
internal static unsafe void GetChars(this Encoding encoding, byte* bytes, int byteCount, char* chars, int charCount, object nls) {
internal static unsafe void GetChars(this Encoding encoding, byte* bytes, int byteCount, char* chars, int charCount, object? nls) {
encoding.GetChars(bytes, byteCount, chars, charCount);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/core/Microsoft.Scripting.Metadata/MemoryMapping.V4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed unsafe class MemoryMapping : CriticalFinalizerObject {
[SecurityCritical]
internal byte* _pointer;

private SafeMemoryMappedViewHandle _handle;
private SafeMemoryMappedViewHandle? _handle;
internal long _capacity;

[CLSCompliant(false)]
Expand Down Expand Up @@ -51,11 +51,11 @@ public MemoryBlock GetRange(int start, int length) {

[SecuritySafeCritical]
public static MemoryMapping Create(string path) {
MemoryMappedFile file = null;
MemoryMappedViewAccessor accessor = null;
SafeMemoryMappedViewHandle handle = null;
MemoryMapping mapping = null;
FileStream stream = null;
MemoryMappedFile? file = null;
MemoryMappedViewAccessor? accessor = null;
SafeMemoryMappedViewHandle? handle = null;
MemoryMapping? mapping = null;
FileStream? stream = null;
byte* ptr = null;

try {
Expand Down Expand Up @@ -95,7 +95,7 @@ public static MemoryMapping Create(string path) {
// It is not safe to close the view at this point if there are any MemoryBlocks alive.
// It's up to the user to ensure not to use them. Since you need unmanaged code priviledge to use them
// this is not a security issue (it would be if this API was security safe critical).
_handle.ReleasePointer();
_handle!.ReleasePointer(); // _handle is guaranteed to be non-null if _pointer is non-null, since we assign them together in Create.
_pointer = null;
}
}
Expand Down
125 changes: 70 additions & 55 deletions src/core/Microsoft.Scripting.Metadata/MetadataImport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.IO;

Expand Down Expand Up @@ -59,6 +60,7 @@ private void ReadOptionalHeaderDirectoryEntries(MemoryReader memReader) {
memReader.SeekRelative(1 * 2 * sizeof(uint));
}

[MemberNotNull(nameof(_sectionHeaders))]
private void ReadSectionHeaders(MemoryReader memReader) {
if (memReader.RemainingBytes < _numberOfSections * PEFileConstants.SizeofSectionHeader) {
throw new BadImageFormatException();
Expand All @@ -83,6 +85,7 @@ private void ReadSectionHeaders(MemoryReader memReader) {
}
}

[MemberNotNull(nameof(_sectionHeaders))]
private void ReadPEFileLevelData() {
if (_image.Length < PEFileConstants.BasicPEHeaderSize) {
throw new BadImageFormatException();
Expand Down Expand Up @@ -150,7 +153,7 @@ internal MemoryBlock RvaToMemoryBlock(uint rva, uint size) {
throw new BadImageFormatException();
}

private MemoryBlock DirectoryToMemoryBlock(DirectoryEntry directory) {
private MemoryBlock? DirectoryToMemoryBlock(DirectoryEntry directory) {
if (directory.RelativeVirtualAddress == 0 || directory.Size == 0) {
return null;
}
Expand All @@ -166,17 +169,17 @@ private MemoryBlock DirectoryToMemoryBlock(DirectoryEntry directory) {
private StreamHeader[] _streamHeaders;

private MemoryBlock _stringStream;
private MemoryBlock _blobStream;
private MemoryBlock? _blobStream;
private MemoryBlock _guidStream;
private MemoryBlock _userStringStream;
private MemoryBlock? _userStringStream;

private MetadataStreamKind _metadataStreamKind;
private MemoryBlock _metadataTableStream;
// private MemoryBlock _resourceMemoryBlock;
// private MemoryBlock _strongNameSignatureBlock;

private void ReadCOR20Header() {
MemoryBlock memBlock = DirectoryToMemoryBlock(_optionalHeaderDirectoryEntries.COR20HeaderTableDirectory);
MemoryBlock? memBlock = DirectoryToMemoryBlock(_optionalHeaderDirectoryEntries.COR20HeaderTableDirectory);
if (memBlock is null || memBlock.Length < _optionalHeaderDirectoryEntries.COR20HeaderTableDirectory.Size) {
throw new BadImageFormatException();
}
Expand Down Expand Up @@ -230,6 +233,7 @@ private void ReadStorageHeader(MemoryReader memReader) {
_storageHeader.NumberOfStreams = memReader.ReadUInt16();
}

[MemberNotNull(nameof(_streamHeaders))]
private void ReadStreamHeaders(MemoryReader memReader) {
int numberOfStreams = _storageHeader.NumberOfStreams;
_streamHeaders = new StreamHeader[numberOfStreams];
Expand All @@ -246,6 +250,9 @@ private void ReadStreamHeaders(MemoryReader memReader) {
}
}

[MemberNotNull(nameof(_stringStream))]
[MemberNotNull(nameof(_guidStream))]
[MemberNotNull(nameof(_metadataTableStream))]
private void ProcessAndCacheStreams(MemoryBlock metadataRoot) {
_metadataStreamKind = MetadataStreamKind.Illegal;

Expand Down Expand Up @@ -310,15 +317,19 @@ private void ProcessAndCacheStreams(MemoryBlock metadataRoot) {
}

// mandatory streams:
if (_stringStream is null || _guidStream is null || _metadataStreamKind == MetadataStreamKind.Illegal) {
if (_stringStream is null || _guidStream is null || _metadataTableStream is null || _metadataStreamKind == MetadataStreamKind.Illegal) {
throw new BadImageFormatException();
}
}

[MemberNotNull(nameof(_streamHeaders))]
[MemberNotNull(nameof(_stringStream))]
[MemberNotNull(nameof(_guidStream))]
[MemberNotNull(nameof(_metadataTableStream))]
private void ReadCORModuleLevelData() {
ReadCOR20Header();

MemoryBlock metadataRoot = DirectoryToMemoryBlock(_cor20Header.MetaDataDirectory);
MemoryBlock? metadataRoot = DirectoryToMemoryBlock(_cor20Header.MetaDataDirectory);
if (metadataRoot is null || metadataRoot.Length < _cor20Header.MetaDataDirectory.Size) {
throw new BadImageFormatException();
}
Expand All @@ -339,53 +350,56 @@ private void ReadCORModuleLevelData() {
#region Metadata

private MetadataTableHeader _metadataTableHeader;
private int[] _tableRowCounts;

internal ModuleTable ModuleTable;
internal TypeRefTable TypeRefTable;
internal TypeDefTable TypeDefTable;
internal FieldPtrTable FieldPtrTable;
internal FieldTable FieldTable;
internal MethodPtrTable MethodPtrTable;
internal MethodTable MethodTable;
internal ParamPtrTable ParamPtrTable;
internal ParamTable ParamTable;
internal InterfaceImplTable InterfaceImplTable;
internal MemberRefTable MemberRefTable;
internal ConstantTable ConstantTable;
internal CustomAttributeTable CustomAttributeTable;
internal FieldMarshalTable FieldMarshalTable;
internal DeclSecurityTable DeclSecurityTable;
internal ClassLayoutTable ClassLayoutTable;
internal FieldLayoutTable FieldLayoutTable;
internal StandAloneSigTable StandAloneSigTable;
internal EventMapTable EventMapTable;
internal EventPtrTable EventPtrTable;
internal EventTable EventTable;
internal PropertyMapTable PropertyMapTable;
internal PropertyPtrTable PropertyPtrTable;
internal PropertyTable PropertyTable;
internal MethodSemanticsTable MethodSemanticsTable;
internal MethodImplTable MethodImplTable;
internal ModuleRefTable ModuleRefTable;
internal TypeSpecTable TypeSpecTable;
internal ImplMapTable ImplMapTable;
internal FieldRVATable FieldRVATable;
internal EnCLogTable EnCLogTable;
internal EnCMapTable EnCMapTable;
internal AssemblyTable AssemblyTable;
internal AssemblyProcessorTable AssemblyProcessorTable;
internal AssemblyOSTable AssemblyOSTable;
internal AssemblyRefTable AssemblyRefTable;
internal AssemblyRefProcessorTable AssemblyRefProcessorTable;
internal AssemblyRefOSTable AssemblyRefOSTable;
internal FileTable FileTable;
internal ExportedTypeTable ExportedTypeTable;
internal ManifestResourceTable ManifestResourceTable;
internal NestedClassTable NestedClassTable;
internal GenericParamTable GenericParamTable;
internal MethodSpecTable MethodSpecTable;
internal GenericParamConstraintTable GenericParamConstraintTable;

// all non-nullable fields assigned null! here below are unconditionally initialized to non-null by
// MetadataImport ctor -> ReadMetadataLevelData -> ProcessAndCacheMetadataTableBlocks
private int[] _tableRowCounts = null!;

internal ModuleTable ModuleTable = null!;
internal TypeRefTable TypeRefTable = null!;
internal TypeDefTable TypeDefTable = null!;
internal FieldPtrTable FieldPtrTable = null!;
internal FieldTable FieldTable = null!;
internal MethodPtrTable MethodPtrTable = null!;
internal MethodTable MethodTable = null!;
internal ParamPtrTable ParamPtrTable = null!;
internal ParamTable ParamTable = null!;
internal InterfaceImplTable InterfaceImplTable = null!;
internal MemberRefTable MemberRefTable = null!;
internal ConstantTable ConstantTable = null!;
internal CustomAttributeTable CustomAttributeTable = null!;
internal FieldMarshalTable FieldMarshalTable = null!;
internal DeclSecurityTable DeclSecurityTable = null!;
internal ClassLayoutTable ClassLayoutTable = null!;
internal FieldLayoutTable FieldLayoutTable = null!;
internal StandAloneSigTable StandAloneSigTable = null!;
internal EventMapTable EventMapTable = null!;
internal EventPtrTable EventPtrTable = null!;
internal EventTable EventTable = null!;
internal PropertyMapTable PropertyMapTable = null!;
internal PropertyPtrTable PropertyPtrTable = null!;
internal PropertyTable PropertyTable = null!;
internal MethodSemanticsTable MethodSemanticsTable = null!;
internal MethodImplTable MethodImplTable = null!;
internal ModuleRefTable ModuleRefTable = null!;
internal TypeSpecTable TypeSpecTable = null!;
internal ImplMapTable ImplMapTable = null!;
internal FieldRVATable FieldRVATable = null!;
internal EnCLogTable EnCLogTable = null!;
internal EnCMapTable EnCMapTable = null!;
internal AssemblyTable AssemblyTable = null!;
internal AssemblyProcessorTable AssemblyProcessorTable = null!;
internal AssemblyOSTable AssemblyOSTable = null!;
internal AssemblyRefTable AssemblyRefTable = null!;
internal AssemblyRefProcessorTable AssemblyRefProcessorTable = null!;
internal AssemblyRefOSTable AssemblyRefOSTable = null!;
internal FileTable FileTable = null!;
internal ExportedTypeTable ExportedTypeTable = null!;
internal ManifestResourceTable ManifestResourceTable = null!;
internal NestedClassTable NestedClassTable = null!;
internal GenericParamTable GenericParamTable = null!;
internal MethodSpecTable MethodSpecTable = null!;
internal GenericParamConstraintTable GenericParamConstraintTable = null!;

internal bool IsManifestModule {
get { return AssemblyTable.NumberOfRows == 1; }
Expand Down Expand Up @@ -748,6 +762,7 @@ internal MemoryBlock GetBlobBlock(uint blob) {
return _blobStream.GetRange(dataOffset, size);
}

[MemberNotNull(nameof(_blobStream))]
internal int GetBlobDataOffset(uint blob, out int size) {
if (_blobStream is null || blob >= _blobStream.Length) {
throw new BadImageFormatException();
Expand All @@ -762,7 +777,7 @@ internal int GetBlobDataOffset(uint blob, out int size) {
return offset + bytesRead;
}

internal object GetBlobValue(uint blob, ElementType type) {
internal object? GetBlobValue(uint blob, ElementType type) {
int offset = GetBlobDataOffset(blob, out int size);
if (size < GetMinTypeSize(type)) {
throw new BadImageFormatException();
Expand Down Expand Up @@ -1065,7 +1080,7 @@ internal MetadataName GetMetadataName(uint blob) {
return _stringStream.ReadName(blob);
}

internal object GetDefaultValue(MetadataToken token) {
internal object? GetDefaultValue(MetadataToken token) {
int constantRid = ConstantTable.GetConstantRowId(token);
if (constantRid == 0) {
return Missing.Value;
Expand Down
4 changes: 2 additions & 2 deletions src/core/Microsoft.Scripting.Metadata/MetadataName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public bool IsEmpty {

// SECURITY: The method is actually not safe. We must make sure that this object is not leaked to partially-trusted code.
[SecuritySafeCritical]
public override bool Equals(object obj) {
public override bool Equals(object? obj) {
return obj is MetadataName name && Equals(name)
|| obj is MetadataNamePart part && Equals(part);
}
Expand Down Expand Up @@ -402,7 +402,7 @@ public override int GetHashCode() {

// SECURITY: The method is actually not safe. We must make sure that this object is not leaked to partially-trusted code.
[SecuritySafeCritical]
public override bool Equals(object obj) {
public override bool Equals(object? obj) {
return obj is MetadataNamePart part && Equals(part)
|| obj is MetadataName name && Equals(name);
}
Expand Down
7 changes: 5 additions & 2 deletions src/core/Microsoft.Scripting.Metadata/MetadataServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Microsoft.Scripting.Metadata {
public static class MetadataServices {
// Stores metadata tables for each loaded non-dynamic assemblies.
// The first module in the array is always a manifest module.
private static Dictionary<Assembly, MetadataTables[]> _metadataCache;
private static Dictionary<Assembly, MetadataTables[]>? _metadataCache;

private static MetadataTables[] GetAsseblyMetadata(Assembly assembly) {
_metadataCache ??= new Dictionary<Assembly, MetadataTables[]>();
Expand Down Expand Up @@ -96,7 +96,10 @@ public static List<KeyValuePair<Module, int>> GetVisibleExtensionMethods(Assembl
(tattrs & TypeAttributes.VisibilityMask) == TypeAttributes.NestedPublic) &&
(tattrs & TypeAttributes.Abstract) != 0 &&
(tattrs & TypeAttributes.Sealed) != 0) {
result.Add(new KeyValuePair<Module, int>(manifest.Module, mdef.Record.Token.Value));

// GetAsseblyMetadata (call above) always produces MetadataTables with elements (here: manifest)
// that have a defined (non-null) Module property.
result.Add(new KeyValuePair<Module, int>(manifest.Module!, mdef.Record.Token.Value));
}
}
}
Expand Down
Loading
Loading