Skip to content

Commit

Permalink
Moved AssemblyName helpers to managed (part 2) (#63915)
Browse files Browse the repository at this point in the history
* implement GetAssemblyName via dynamic call to MetadataReader

* A few more file-locking tests.

* fix #28153

* no need for version when getting MetadataReader

* rename the argument to match AssemblyName

* perf tweaks

* use memory-mapped file to read metadata

* adjust tests for the new implementation

* use "bufferSize: 1" when stream is going to be mapped.

* null-conditional operator.

* do Dispose before re-throwing

* get rid of the platform-specific/native stuff

* remove assemblyname.hpp

* remove `VerifyIsAssembly()`

* PR feedback

* put back gStdMngIEnumerableFuncs and the others
  • Loading branch information
VSadov authored Jan 26, 2022
1 parent cbc4cd8 commit 2cf4826
Show file tree
Hide file tree
Showing 29 changed files with 100 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ internal AssemblyName(string? name,
_flags = flags;
}

// This call opens and closes the file, but does not add the
// assembly to the domain.
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern AssemblyName nGetFileInformation(string s);

internal static AssemblyName GetFileInformationCore(string assemblyFile)
{
string fullPath = Path.GetFullPath(assemblyFile);
return nGetFileInformation(fullPath);
}

internal void SetProcArchIndex(PortableExecutableKinds pek, ImageFileMachine ifm)
{
#pragma warning disable SYSLIB0037 // AssemblyName.ProcessorArchitecture is obsolete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
<Compile Include="System\Reflection\AssemblyRuntimeNameHelpers.cs" />
<Compile Include="System\Reflection\Attribute.CoreRT.cs" />
<Compile Include="System\Reflection\Assembly.CoreRT.cs" />
<Compile Include="System\Reflection\AssemblyName.CoreRT.cs" />
<Compile Include="System\Reflection\BinderBundle.cs" />
<Compile Include="System\Reflection\Emit\AssemblyBuilder.cs" />
<Compile Include="System\Reflection\Emit\ConstructorBuilder.cs" />
Expand Down

This file was deleted.

11 changes: 4 additions & 7 deletions src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public static unsafe PEReader OpenPEFile(string filePath, out MemoryMappedViewAc
try
{
// Create stream because CreateFromFile(string, ...) uses FileShare.None which is too strict
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, false);
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1);
mappedFile = MemoryMappedFile.CreateFromFile(
fileStream, null, fileStream.Length, MemoryMappedFileAccess.Read, HandleInheritability.None, true);
accessor = mappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);
Expand All @@ -174,12 +174,9 @@ public static unsafe PEReader OpenPEFile(string filePath, out MemoryMappedViewAc
}
finally
{
if (accessor != null)
accessor.Dispose();
if (mappedFile != null)
mappedFile.Dispose();
if (fileStream != null)
fileStream.Dispose();
accessor?.Dispose();
mappedFile?.Dispose();
fileStream?.Dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static unsafe MetadataReader TryOpenMetadataFile(string filePath, Metada
try
{
// Create stream because CreateFromFile(string, ...) uses FileShare.None which is too strict
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, false);
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1);
mappedFile = MemoryMappedFile.CreateFromFile(
fileStream, null, fileStream.Length, MemoryMappedFileAccess.Read, HandleInheritability.None, true);

Expand Down Expand Up @@ -56,12 +56,9 @@ private static unsafe MetadataReader TryOpenMetadataFile(string filePath, Metada
}
finally
{
if (accessor != null)
accessor.Dispose();
if (mappedFile != null)
mappedFile.Dispose();
if (fileStream != null)
fileStream.Dispose();
accessor?.Dispose();
mappedFile?.Dispose();
fileStream?.Dispose();
}
}

Expand Down
11 changes: 4 additions & 7 deletions src/coreclr/tools/dotnet-pgo/TraceTypeSystemContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public static unsafe PEReader OpenPEFile(string filePath, byte[] moduleBytes, ou
try
{
// Create stream because CreateFromFile(string, ...) uses FileShare.None which is too strict
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, false);
fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1);
mappedFile = MemoryMappedFile.CreateFromFile(
fileStream, null, fileStream.Length, MemoryMappedFileAccess.Read, HandleInheritability.None, true);
accessor = mappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);
Expand All @@ -278,12 +278,9 @@ public static unsafe PEReader OpenPEFile(string filePath, byte[] moduleBytes, ou
}
finally
{
if (accessor != null)
accessor.Dispose();
if (mappedFile != null)
mappedFile.Dispose();
if (fileStream != null)
fileStream.Dispose();
accessor?.Dispose();
mappedFile?.Dispose();
fileStream?.Dispose();
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,6 @@ set(GC_HEADERS_DAC
set(VM_SOURCES_WKS
${VM_SOURCES_DAC_AND_WKS_COMMON}
appdomainnative.cpp
assemblyname.cpp
assemblynative.cpp
assemblyspec.cpp
baseassemblyspec.cpp
Expand Down Expand Up @@ -392,7 +391,6 @@ set(VM_HEADERS_WKS
${VM_HEADERS_DAC_AND_WKS_COMMON}
../inc/jithelpers.h
appdomainnative.hpp
assemblyname.hpp
assemblynative.hpp
assemblyspec.hpp
assemblyspecbase.h
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/appdomain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "eeconfig.h"
#include "gcheaputilities.h"
#include "eventtrace.h"
#include "assemblyname.hpp"
#include "eeprofinterfaces.h"
#include "dbginterface.h"
#ifndef DACCESS_COMPILE
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/vm/assembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

#include "assembly.hpp"
#include "appdomain.hpp"
#include "assemblyname.hpp"



#include "eeprofinterfaces.h"
#include "reflectclasswriter.h"
Expand Down
69 changes: 0 additions & 69 deletions src/coreclr/vm/assemblyname.cpp

This file was deleted.

25 changes: 0 additions & 25 deletions src/coreclr/vm/assemblyname.hpp

This file was deleted.

1 change: 0 additions & 1 deletion src/coreclr/vm/assemblynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "assemblynative.hpp"
#include "dllimport.h"
#include "field.h"
#include "assemblyname.hpp"
#include "eeconfig.h"
#include "interoputil.h"
#include "frames.h"
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/corelib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include "comdatetime.h"
#include "compatibilityswitch.h"
#include "debugdebugger.h"
#include "assemblyname.hpp"
#include "assemblynative.hpp"
#include "comthreadpool.h"
#include "comwaithandle.h"
Expand Down
5 changes: 0 additions & 5 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,6 @@ FCFuncStart(gAssemblyLoadContextFuncs)
FCFuncElement("IsTracingEnabled", AssemblyNative::IsTracingEnabled)
FCFuncEnd()

FCFuncStart(gAssemblyNameFuncs)
FCFuncElement("nGetFileInformation", AssemblyNameNative::GetFileInformation)
FCFuncEnd()

FCFuncStart(gAssemblyBuilderFuncs)
FCFuncElement("GetInMemoryAssemblyModule", AssemblyNative::GetInMemoryAssemblyModule)
FCFuncEnd()
Expand Down Expand Up @@ -768,7 +764,6 @@ FCClassElement("ArgIterator", "System", gVarArgFuncs)
FCClassElement("Array", "System", gArrayFuncs)
FCClassElement("AssemblyBuilder", "System.Reflection.Emit", gAssemblyBuilderFuncs)
FCClassElement("AssemblyLoadContext", "System.Runtime.Loader", gAssemblyLoadContextFuncs)
FCClassElement("AssemblyName", "System.Reflection", gAssemblyNameFuncs)
FCClassElement("Buffer", "System", gBufferFuncs)
FCClassElement("CastHelpers", "System.Runtime.CompilerServices", gCastHelpers)
FCClassElement("CompatibilitySwitch", "System.Runtime.Versioning", gCompatibilitySwitchFuncs)
Expand Down
28 changes: 0 additions & 28 deletions src/coreclr/vm/peimage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,34 +456,6 @@ void PEImage::GetMVID(GUID *pMvid)
#endif // _DEBUG
}

void PEImage::VerifyIsAssembly()
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;

// buch of legacy stuff here wrt the error codes...

if (!HasNTHeaders())
ThrowFormat(COR_E_BADIMAGEFORMAT);

if(!HasCorHeader())
ThrowFormat(COR_E_ASSEMBLYEXPECTED);

CHECK checkGoodFormat;
checkGoodFormat = CheckILFormat();
if (!checkGoodFormat)
ThrowFormat(COR_E_BADIMAGEFORMAT);

mdAssembly a;
if (FAILED(GetMDImport()->GetAssemblyFromScope(&a)))
ThrowFormat(COR_E_ASSEMBLYEXPECTED);
}

void DECLSPEC_NORETURN PEImage::ThrowFormat(HRESULT hrError)
{
CONTRACTL
Expand Down
2 changes: 0 additions & 2 deletions src/coreclr/vm/peimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ class PEImage final
CHECK CheckILFormat();
CHECK CheckUniqueInstance();

void VerifyIsAssembly();

void SetModuleFileNameHintForDAC();
#ifdef DACCESS_COMPILE
void EnumMemoryRegions(CLRDataEnumMemoryFlags flags);
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "comdatetime.h"
#include "compatibilityswitch.h"
#include "debugdebugger.h"
#include "assemblyname.hpp"
#include "assemblynative.hpp"
#include "comthreadpool.h"
#include "comwaithandle.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void UpdateResources()
try
{
// Open the source host file.
appHostSourceStream = new FileStream(appHostSourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
appHostSourceStream = new FileStream(appHostSourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1);
memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostSourceStream, null, 0, MemoryMappedFileAccess.Read, HandleInheritability.None, true);
memoryMappedViewAccessor = memoryMappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.CopyOnWrite);
Expand Down
Loading

0 comments on commit 2cf4826

Please sign in to comment.