Skip to content

Commit

Permalink
Gracefully handle lack of IMetaDataImport interface (refs #249)
Browse files Browse the repository at this point in the history
  • Loading branch information
ElektroKill committed Oct 11, 2023
1 parent 1915d88 commit 02fb851
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ void UpdateDynamicModuleIds(DnModule dnModule) {
}
if (updatedModules is not null) {
foreach (var info in updatedModules) {
var mdi = info.dnModule.CorModule.GetMetaDataInterface<IMetaDataImport2>();
var mdi = info.dnModule.CorModule.GetMetaDataInterface<IMetaDataImport>();
var scopeName = MDAPI.GetModuleName(mdi) ?? string.Empty;
((DbgCorDebugInternalModuleImpl)info.dbgModule.InternalModule).ReflectionModule!.ScopeName = scopeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public DnlibAssemblyResolverImpl(DbgEngineImpl engine, DmdAppDomain appDomain) {
return null;
if (!engine.TryGetDnModule(dbgModule, out var dnModule))
return null;
return dnModule.GetOrCreateCorModuleDef().Assembly;
return dnModule.GetOrCreateCorModuleDef()?.Assembly;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ DbgDotNetRawModuleBytes GetRawModuleBytesCore(DbgModule module) {
return state.ToDbgDotNetRawModuleBytes();

var md = dnModule.GetOrCreateCorModuleDef();
if (md is null)
return DbgDotNetRawModuleBytes.None;

try {
md.DisableMDAPICalls = false;
md.LoadEverything(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void CreateCorLibTypes() {
corLibTypes = new CorLibTypes(this, UpdateRowId(corLibAsmRef));
}

IAssembly GetCorAssemblyRef() {
IAssembly? GetCorAssemblyRef() {
if (corModuleDefHelper.IsCorLib == true)
return Assembly;
if (corModuleDefHelper.IsDynamic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface ICorModuleDefHelper {
/// <summary>
/// Gets the currently loaded core library (eg. mscorlib)
/// </summary>
IAssembly CorLib { get; }
IAssembly? CorLib { get; }

/// <summary>
/// true if it's a dynamic module (types can be added at runtime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CorModuleDefHelper(DnModule module) {
Debug.Assert(!module.IsDynamic || module.Address == 0);
}

public IAssembly CorLib {
public IAssembly? CorLib {
get {
var corAsm = module.AppDomain.Assemblies.FirstOrDefault();
Debug2.Assert(corAsm is not null);
Expand All @@ -47,7 +47,7 @@ public IAssembly CorLib {
Debug2.Assert(corMod is not null);
if (corMod is null)
return AssemblyRefUser.CreateMscorlibReferenceCLR20();
return corMod.GetOrCreateCorModuleDef().Assembly;
return corMod.GetOrCreateCorModuleDef()?.Assembly;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,26 @@ internal void InitializeAssemblyAndModules() {

var created = new List<DnModule>();
var modules = this.modules.GetAll();
bool canCreateAssemblyDef = true;
for (int i = 0; i < modules.Length; i++) {
var module = modules[i];
if (module.CorModuleDef is not null) {
Debug2.Assert(corAssemblyDef is not null);
continue;
}
module.CorModuleDef = new CorModuleDef(module.CorModule.GetMetaDataInterface<IMetaDataImport>(), new CorModuleDefHelper(module));
if (corAssemblyDef is null)
corAssemblyDef = new CorAssemblyDef(module.CorModuleDef, 1);
corAssemblyDef.Modules.Add(module.CorModuleDef);

var mdi = module.CorModule.GetMetaDataInterface<IMetaDataImport>();
if (mdi is null) {
canCreateAssemblyDef = false;
continue;
}

module.CorModuleDef = new CorModuleDef(mdi, new CorModuleDefHelper(module));

if (canCreateAssemblyDef)
corAssemblyDef ??= new CorAssemblyDef(module.CorModuleDef, 1);
corAssemblyDef?.Modules.Add(module.CorModuleDef);

module.CorModuleDef.Initialize();
created.Add(module);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ sealed class DnModule {
/// Returns the created module or creates one if none has been created
/// </summary>
/// <returns></returns>
public CorModuleDef GetOrCreateCorModuleDef() {
public CorModuleDef? GetOrCreateCorModuleDef() {
Debugger.DebugVerifyThread();
if (CorModuleDef is not null)
return CorModuleDef;
Expand Down

0 comments on commit 02fb851

Please sign in to comment.