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
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,33 @@ public static bool CanBeResolved(this TypeReference parent)
/// <para> Note that calling ArraySegment`T.get_Count directly gives an invalid IL error </para>
/// </summary>
/// <param name="self"></param>
/// <param name="module">
/// Module that will use the reference. Must not be <c>self.Module</c> when self is from another assembly,
/// otherwise type args from <paramref name="module"/> are scoped to an assembly reference and it ends up referencing itself
/// </param>
/// <param name="instanceType"></param>
/// <returns></returns>
public static MethodReference MakeHostInstanceGeneric(this MethodReference self, GenericInstanceType instanceType)
public static MethodReference MakeHostInstanceGeneric(this MethodReference self, ModuleDefinition module, GenericInstanceType instanceType)
{
var reference = new MethodReference(self.Name, self.ReturnType, instanceType)
// import self first so its return and parameter types belong to module.
// Importing the new reference instead is skipped by cecil when instanceType is already in module,
// which leaves those types in self's module and fails when the assembly is written
var imported = module.ImportReference(self);

var reference = new MethodReference(imported.Name, imported.ReturnType, module.ImportReference(instanceType))
{
CallingConvention = self.CallingConvention,
HasThis = self.HasThis,
ExplicitThis = self.ExplicitThis
CallingConvention = imported.CallingConvention,
HasThis = imported.HasThis,
ExplicitThis = imported.ExplicitThis
};

foreach (var parameter in self.Parameters)
foreach (var parameter in imported.Parameters)
reference.Parameters.Add(new ParameterDefinition(parameter.ParameterType));

foreach (var generic_parameter in self.GenericParameters)
foreach (var generic_parameter in imported.GenericParameters)
reference.GenericParameters.Add(new GenericParameter(generic_parameter.Name, reference));

return self.Module.ImportReference(reference);
return reference;
}

public static MethodReference MakeHostInstanceSelfGeneric(this MethodReference self)
Expand All @@ -226,7 +235,8 @@ public static MethodReference MakeHostInstanceSelfGeneric(this MethodReference s
else
{
var genericType = (GenericInstanceType)type.MakeSelfGeneric();
return self.MakeHostInstanceGeneric(genericType);
// self generic uses only the type's own generic params, so self.Module is the module that uses it
return self.MakeHostInstanceGeneric(self.Module, genericType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public static MethodDefinition[] GetMethods(this TypeDefinition td, string metho
/// </para>
/// </summary>
/// <param name="typeReference">Unresolved type reference, dont resolve if generic</param>
/// <param name="module">Module that will call the method, generic instances are imported into it</param>
/// <param name="methodName"></param>
/// <returns></returns>
public static MethodReference GetMethodInBaseType(this TypeReference typeReference, string methodName)
public static MethodReference GetMethodInBaseType(this TypeReference typeReference, ModuleDefinition module, string methodName)
{
return GetMethodInBaseType(typeReference, (md) => md.Name == methodName);
return GetMethodInBaseType(typeReference, module, (md) => md.Name == methodName);
}

/// <summary>
Expand All @@ -47,9 +48,10 @@ public static MethodReference GetMethodInBaseType(this TypeReference typeReferen
/// </para>
/// </summary>
/// <param name="typeReference">Unresolved type reference, dont resolve if generic</param>
/// <param name="module">Module that will call the method, generic instances are imported into it</param>
/// <param name="methodName"></param>
/// <returns></returns>
public static MethodReference GetMethodInBaseType(this TypeReference typeReference, Predicate<MethodDefinition> match)
public static MethodReference GetMethodInBaseType(this TypeReference typeReference, ModuleDefinition module, Predicate<MethodDefinition> match)
{
var typedef = typeReference.Resolve();
var typeRef = typeReference;
Expand All @@ -63,7 +65,8 @@ public static MethodReference GetMethodInBaseType(this TypeReference typeReferen
if (typeRef.IsGenericInstance)
{
var generic = (GenericInstanceType)typeRef;
method = method.MakeHostInstanceGeneric(generic);
// md can be from a base type in another assembly, so import into the calling module, not md.Module
method = method.MakeHostInstanceGeneric(module, generic);
}

return method;
Expand All @@ -79,7 +82,7 @@ public static MethodReference GetMethodInBaseType(this TypeReference typeReferen

if (parent.IsGenericInstance)
{
parent = MatchGenericParameters((GenericInstanceType)parent, typeRef);
parent = MatchGenericParameters((GenericInstanceType)parent, module, typeRef);
}
typeRef = parent;
typedef = parent?.Resolve();
Expand All @@ -94,7 +97,8 @@ public static MethodReference GetMethodInBaseType(this TypeReference typeReferen
return null;
}

public static TypeReference GetBaseType(this TypeReference typeReference, Predicate<TypeReference> match)
/// <param name="module">Module that will use the type, generic args are imported into it</param>
public static TypeReference GetBaseType(this TypeReference typeReference, ModuleDefinition module, Predicate<TypeReference> match)
{
var typedef = typeReference.Resolve();
var typeRef = typeReference;
Expand All @@ -114,7 +118,7 @@ public static TypeReference GetBaseType(this TypeReference typeReference, Predic

if (parent.IsGenericInstance)
{
parent = MatchGenericParameters((GenericInstanceType)parent, typeRef);
parent = MatchGenericParameters((GenericInstanceType)parent, module, typeRef);
}
typeRef = parent;
typedef = parent?.Resolve();
Expand All @@ -135,9 +139,13 @@ public static TypeReference GetBaseType(this TypeReference typeReference, Predic
/// eg makes `Base{T}` in <c>Child{int} : Base{int}</c> have `int` instead of `T`
/// </summary>
/// <param name="parentReference"></param>
/// <param name="module">
/// Module that will use the type. Not parentReference.Module, that can be another assembly when walking up base types,
/// and importing child args into it scopes them to an assembly reference so module ends up referencing itself
/// </param>
/// <param name="childReference"></param>
/// <returns></returns>
public static GenericInstanceType MatchGenericParameters(this GenericInstanceType parentReference, TypeReference childReference)
public static GenericInstanceType MatchGenericParameters(this GenericInstanceType parentReference, ModuleDefinition module, TypeReference childReference)
{
if (!parentReference.IsGenericInstance)
throw new InvalidOperationException("Can't make non generic type into generic");
Expand All @@ -163,7 +171,7 @@ public static GenericInstanceType MatchGenericParameters(this GenericInstanceTyp
var arg = FindMatchingGenericArgument(childReference, name);

// import just to be safe
var imported = parentReference.Module.ImportReference(arg);
var imported = module.ImportReference(arg);
// set arg on generic, parent ref will be Base<int> instead of just Base<T>
generic.GenericArguments[i] = imported;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters p
parameters = new ReaderParameters(ReadingMode.Deferred);

parameters.AssemblyResolver = this;
// Weaver also weaves base types from referenced assemblies in memory, because the references are not post processed yet.
// Without this they use cecil's default importer, so typeof imports there are scoped to the weaver's
// System.Private.CoreLib and that reference leaks into the weaved assembly when it calls those base methods
parameters.ReflectionImporterProvider = new PostProcessorReflectionImporterProvider();

var ms = MemoryStreamFor(fileName);

Expand Down
43 changes: 43 additions & 0 deletions Assets/Mirage/Weaver/Mirage.CecilExtensions/WeaverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public abstract class WeaverBase
public readonly WeaverDiagnosticsTimer timer;

private AssemblyDefinition _assemblyDefinition;
/// <summary>Names of assemblies the weaved assembly is allowed to reference, see <see cref="CheckAssemblyReferences"/></summary>
private HashSet<string> _allowedReferences;

public WeaverBase(IWeaverLogger logger = null)
{
Expand All @@ -56,6 +58,7 @@ public Result Process(ICompiledAssembly compiledAssembly)
{
_assemblyDefinition = ReadAssembly(compiledAssembly);
}
_allowedReferences = GetAllowedReferences(compiledAssembly, _assemblyDefinition);

var result = Process(_assemblyDefinition, compiledAssembly);
var ilResult = CreateResults(result);
Expand Down Expand Up @@ -100,6 +103,9 @@ private ILPostProcessResult CreateResults(ResultType result)

private ILPostProcessResult Success()
{
if (!CheckAssemblyReferences())
return Failed();

// write assembly to file on success
var pe = new MemoryStream();
var pdb = new MemoryStream();
Expand All @@ -120,6 +126,43 @@ private ILPostProcessResult Failed()
return new ILPostProcessResult(null, logger.GetDiagnostics());
}

/// <summary>
/// Weaver can add references that the compiler didn't need (eg UnityEditor for InitializeOnLoadMethod),
/// but only to assemblies this one was compiled against, or that it already referenced
/// </summary>
private static HashSet<string> GetAllowedReferences(ICompiledAssembly compiledAssembly, AssemblyDefinition assemblyDefinition)
{
var allowed = new HashSet<string>();
foreach (var reference in assemblyDefinition.MainModule.AssemblyReferences)
allowed.Add(reference.Name);

foreach (var path in compiledAssembly.References)
allowed.Add(Path.GetFileNameWithoutExtension(path));

return allowed;
}

/// <summary>
/// Errors if weaving added a reference outside of <see cref="_allowedReferences"/>.
/// <para>
/// A bad import (eg the weaver's own System.Private.CoreLib, or this assembly referencing itself) still writes fine and
/// works until the editor restarts, then unity refuses to load the assembly. Failing here points at the weaver instead
/// </para>
/// </summary>
private bool CheckAssemblyReferences()
{
var valid = true;
foreach (var reference in _assemblyDefinition.MainModule.AssemblyReferences)
{
if (_allowedReferences.Contains(reference.Name))
continue;

logger.Error($"Weaver added reference to '{reference.FullName}' which {_assemblyDefinition.Name.Name} was not compiled against. Unity will not load the assembly after an editor restart, this is a bug in the weaver");
valid = false;
}
return valid;
}

private void InsertDebugIfErrors(List<DiagnosticMessage> diag, ICompiledAssembly compiledAssembly)
{
var errorCount = diag.Where(x => x.DiagnosticType == DiagnosticType.Error).Count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void AddMethod()

protected virtual void WriteBaseCall()
{
var baseMethod = _typeDefinition.BaseType.GetMethodInBaseType(MethodName);
var baseMethod = _typeDefinition.BaseType.GetMethodInBaseType(_module, MethodName);
if (baseMethod == null)
return;

Expand Down
28 changes: 16 additions & 12 deletions Assets/Mirage/Weaver/Processors/PropertySiteProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private Instruction ProcessInstruction(MethodDefinition md, Instruction instr, S
}

// this instruction sets the value of a field. cache the field reference.
ProcessInstructionSetterField(instr, resolved);
ProcessInstructionSetterField(md.Module, instr, resolved);
}

if (instr.OpCode == OpCodes.Ldfld && instr.Operand is FieldReference opFieldld)
Expand All @@ -61,7 +61,7 @@ private Instruction ProcessInstruction(MethodDefinition md, Instruction instr, S
}

// this instruction gets the value of a field. cache the field reference.
ProcessInstructionGetterField(instr, resolved);
ProcessInstructionGetterField(md.Module, instr, resolved);
}

if (instr.OpCode == OpCodes.Ldflda && instr.Operand is FieldReference opFieldlda)
Expand All @@ -81,7 +81,7 @@ private Instruction ProcessInstruction(MethodDefinition md, Instruction instr, S
}

// replaces syncvar write access with the NetworkXYZ.get property calls
private void ProcessInstructionSetterField(Instruction i, FieldReference opField)
private void ProcessInstructionSetterField(ModuleDefinition module, Instruction i, FieldReference opField)
{
// does it set a field that we replaced?
if (Setters.TryGetValue(opField, out var replacement))
Expand All @@ -91,19 +91,20 @@ private void ProcessInstructionSetterField(Instruction i, FieldReference opField
var newField = i.Operand as FieldReference;
var genericType = (GenericInstanceType)newField.DeclaringType;
i.OpCode = OpCodes.Callvirt;
i.Operand = replacement.MakeHostInstanceGeneric(genericType);
i.Operand = replacement.MakeHostInstanceGeneric(module, genericType);
}
else
{
//replace with property
i.OpCode = OpCodes.Call;
i.Operand = replacement;
// replacement is from another module when the syncvar is on a base type in another assembly
i.Operand = module.ImportReference(replacement);
}
}
}

// replaces syncvar read access with the NetworkXYZ.get property calls
private void ProcessInstructionGetterField(Instruction i, FieldReference opField)
private void ProcessInstructionGetterField(ModuleDefinition module, Instruction i, FieldReference opField)
{
// does it set a field that we replaced?
if (Getters.TryGetValue(opField, out var replacement))
Expand All @@ -113,13 +114,14 @@ private void ProcessInstructionGetterField(Instruction i, FieldReference opField
var newField = i.Operand as FieldReference;
var genericType = (GenericInstanceType)newField.DeclaringType;
i.OpCode = OpCodes.Callvirt;
i.Operand = replacement.MakeHostInstanceGeneric(genericType);
i.Operand = replacement.MakeHostInstanceGeneric(module, genericType);
}
else
{
//replace with property
i.OpCode = OpCodes.Call;
i.Operand = replacement;
// replacement is from another module when the syncvar is on a base type in another assembly
i.Operand = module.ImportReference(replacement);
}
}
}
Expand All @@ -139,20 +141,22 @@ private Instruction ProcessInstructionLoadAddress(MethodDefinition md, Instructi
// var tmp = new MyStruct();
// this.set_Networkxxxx(tmp);
var worker = md.Body.GetILProcessor();
var tmpVariable = md.AddLocal(opField.FieldType);
// opField and replacement are from another module when the syncvar is on a base type in another assembly
var fieldType = md.Module.ImportReference(opField.FieldType);
var tmpVariable = md.AddLocal(fieldType);

// Avoid removing insturctions incase any br/etc that branch/exception targets point to
// instead Mutate instructions in-place, and insert new instructions after it
// instead Mutate instructions in-place, and insert new instructions after it
instr.OpCode = OpCodes.Ldloca;
instr.Operand = tmpVariable;

nextInstr.OpCode = OpCodes.Initobj;
nextInstr.Operand = opField.FieldType;
nextInstr.Operand = fieldType;

var ldlocInstr = worker.Create(OpCodes.Ldloc, tmpVariable);
worker.InsertAfter(nextInstr, ldlocInstr);

var callInstr = worker.Create(OpCodes.Call, replacement);
var callInstr = worker.Create(OpCodes.Call, md.Module.ImportReference(replacement));
worker.InsertAfter(ldlocInstr, callInstr);

return callInstr;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Weaver/Processors/RegisterRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private static MethodReference CreateRpcDelegate(MethodDefinition func)

var constructorRef = func.Module.ImportReference(constructor);

return constructorRef.MakeHostInstanceGeneric(delegateInstance);
return constructorRef.MakeHostInstanceGeneric(func.Module, delegateInstance);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Weaver/Processors/RpcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ private void FixRemoteCallToBaseMethod(TypeDefinition type, MethodDefinition met
var targetName = UserCodeMethodName(calledMethod);
// check this type and base types for methods
// if the calledMethod is an rpc, then it will have a UserCode_ method generated for it
var userCodeReplacement = type.GetMethodInBaseType(targetName);
var userCodeReplacement = type.GetMethodInBaseType(generatedMethod.Module, targetName);

if (userCodeReplacement == null)
{
Expand Down
Loading
Loading