Open Wartori54 opened 4 months ago
It is important to point out here that the default reader settings were used, thus ReadingMode.Deferred
was used in all my tests.
Obviously using ReadingMode.Immediate
fixes all of these issues and is currently the "workaround" we are using.
But this brings up the question, is patching assemblies in that default mode fully supported (either currently or planned in the future) by Cecil?
Follow up issue from #931, since the problem that it exposed is not restricted to Cecil 0.11.5, and has been there for a long time. I have made a small program where this issue can be demonstrated, its obvious side effect is the linking of the generic parameter with a wrong constraint list, which #931 described.
With the following target dll to patch:
And the following cecil procedure:
The output for the above snippet is the following (compiled with .net sdk 8.0.106):
The issue here is the appearance of any IL instructions that may have its operand serialized as a
TypeSpec
. In this specific case the offender isinitobj !!0
inside theSave
method (and for #931 it was abox !!0
). When reading the operand for that instructionGetTypeSpecification
will be called, which, if the token for the generic parameter it references was uninitialized or cleared (which is what replacing the generic parameter effectively does), it will try to fix it (source):It can be seen that when the condition is full filled the metadata token for that generic parameter will point to the
TypeSpec
table. The issue with it is when the constraints for that sameGenericParameter
are read, it is assumed that the generic parameter RID will point to theGenericParam
table, which is not correct and as such it will point to an arbitrary generic constraint list in the inverse mappings generated by cecil (the dictionary namedGenericConstraints
) (source):Possible fixes for the issue may be:
GenericParameter
does not have binary representation in the read file, and as such no token can be assigned to it until it is serialized.Now that the issue has been exposed in full detail I want to also explain why the commit
c4cfe16
had an impact into this issue manifesting more often.That commit eliminates the removal of mappings when they have been read, since they should be cached afterwards. But a side effect that it had was making it harder for a
GenericParameter
with a wrong metadata token linking itself into some generic constraint list. This is why using Cecil 0.11.5 with MonoMod very easily triggers this issue.