Fix same-module TypeRefs in sandboxing

This commit is contained in:
Pieter-Jan Briers
2022-08-23 22:52:18 +02:00
parent d82e1a2c9f
commit ff4f70593b
2 changed files with 36 additions and 3 deletions

View File

@@ -381,10 +381,16 @@ namespace Robust.Shared.ContentPack
_ => false
};
}
public override bool IsCoreTypeDefined()
{
return ResolutionScope.IsCoreTypeDefined();
}
}
internal abstract record MResScope
{
public virtual bool IsCoreTypeDefined() => false;
}
internal sealed record MResScopeType(MType Type) : MResScope
@@ -403,6 +409,14 @@ namespace Robust.Shared.ContentPack
}
}
internal sealed record MResScopeModuleDefinition : MResScope
{
public static readonly MResScopeModuleDefinition Instance = new();
public override bool IsCoreTypeDefined() => true;
public override string ToString() => "[SAME MODULE]";
}
internal sealed record MTypeGenericTypePlaceHolder(int Index) : MType
{
public override string ToString()

View File

@@ -132,7 +132,7 @@ namespace Robust.Shared.ContentPack
var errors = new ConcurrentBag<SandboxError>();
var types = GetReferencedTypes(reader, errors);
var types = GetExternalReferencedTypes(reader, errors);
var members = GetReferencedMembers(reader, errors);
var inherited = GetExternalInheritedTypes(reader, errors);
_sawmill.Debug($"References loaded... {fullStopwatch.ElapsedMilliseconds}ms");
@@ -538,13 +538,17 @@ namespace Robust.Shared.ContentPack
return nsDict.TryGetValue(type.Name, out cfg);
}
private List<MTypeReferenced> GetReferencedTypes(MetadataReader reader, ConcurrentBag<SandboxError> errors)
private List<MTypeReferenced> GetExternalReferencedTypes(MetadataReader reader, ConcurrentBag<SandboxError> errors)
{
return reader.TypeReferences.Select(typeRefHandle =>
{
try
{
return ParseTypeReference(reader, typeRefHandle);
var type = ParseTypeReference(reader, typeRefHandle);
if (type.IsCoreTypeDefined())
return null;
return type;
}
catch (UnsupportedMetadataException e)
{
@@ -573,6 +577,10 @@ namespace Robust.Shared.ContentPack
try
{
parent = ParseTypeReference(reader, (TypeReferenceHandle) memRef.Parent);
// In case of TypeReference to same ModuleDefinition
if (parent.IsCoreTypeDefined())
return null;
}
catch (UnsupportedMetadataException u)
{
@@ -723,6 +731,9 @@ namespace Robust.Shared.ContentPack
try
{
type = ParseTypeReference(reader, (TypeReferenceHandle) handle);
if (type.IsCoreTypeDefined())
return false;
return true;
}
catch (UnsupportedMetadataException u)
@@ -813,6 +824,14 @@ namespace Robust.Shared.ContentPack
throw new UnsupportedMetadataException(
$"Cross-module reference to type {nameSpace}.{name}. ");
}
case HandleKind.ModuleDefinition:
{
// Same module, effectively a proxy to a TypeDefinition.
// Generated by funky IL emit in some cases.
// I blame Spain.
resScope = MResScopeModuleDefinition.Instance;
break;
}
default:
// Edge cases not handled:
// https://github.com/dotnet/runtime/blob/b2e5a89085fcd87e2fa9300b4bb00cd499c5845b/src/libraries/System.Reflection.Metadata/tests/Metadata/Decoding/DisassemblingTypeProvider.cs#L130-L132