question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

dnlib x64 exhausts all memory when read method body.

See original GitHub issue

test.zip

static void Main(string[] args) {
        Console.WriteLine(IntPtr.Size); // Can only reproduce if current program is AnyCPU/x64
	var module = ModuleDefMD.Load("test.exe");
	var method = module.ResolveMethod(0x11d);
	var body = method.Body; // here
        Console.WriteLine(body);
	Console.ReadKey();
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:26 (26 by maintainers)

github_iconTop GitHub Comments

1reaction
CreateAndInjectcommented, Jan 4, 2022

But the first parameter of GenericParamUser is ushort number rather than uint number

Yes but it doesn’t mean the runtime will throw or report an error if the signature has > 0x10000 generic arguments. Have you tested this? If the runtime doesn’t do anything and the code just runs, we need to support it too.

Yes, I test it, when run the output Test.exe .NET will throw TypeLoadException when greater than 65535: Internal limit: There’re so many generic arguments. (I don’t know the exact message, I translate from Chinese) EDIT: TypeLoadException even if 0-65535 without overflow

var module = new ModuleDefUser("Test.exe");
new AssemblyDefUser("Test").Modules.Add(module);
module.Kind = ModuleKind.Console;
var type = new TypeDefUser("Program", module.CorLibTypes.Object.TypeDefOrRef);
var method = new MethodDefUser("Main", MethodSig.CreateStatic(module.CorLibTypes.Void), MethodAttributes.Public | MethodAttributes.Static);
module.EntryPoint = method;
method.Body = new CilBody();
var instrs = method.Body.Instructions;
var generic = new TypeDefUser("Generic", module.CorLibTypes.Object.TypeDefOrRef);
int max = 65536;
for (int i = 0; i < max; i++) {
	ushort number = (ushort)i;
	generic.GenericParameters.Add(new GenericParamUser(number, GenericParamAttributes.NonVariant, "T" + i));
}
module.Types.Add(generic);
instrs.Add(OpCodes.Ldtoken.ToInstruction(generic));
instrs.Add(OpCodes.Call.ToInstruction(new MemberRefUser(module, "GetTypeFromHandle", MethodSig.CreateStatic(new ClassSig(module.CorLibTypes.GetTypeRef("System", "Type")), new ValueTypeSig(module.CorLibTypes.GetTypeRef("System", "RuntimeTypeHandle"))), module.CorLibTypes.GetTypeRef("System", "Type"))));
instrs.Add(OpCodes.Call.ToInstruction(new MemberRefUser(module, "WriteLine", MethodSig.CreateStatic(module.CorLibTypes.Void, module.CorLibTypes.Object), module.CorLibTypes.GetTypeRef("System", "Console"))));
instrs.Add(OpCodes.Call.ToInstruction(new MemberRefUser(module, "ReadLine", MethodSig.CreateStatic(module.CorLibTypes.String), module.CorLibTypes.GetTypeRef("System", "Console"))));
instrs.Add(OpCodes.Ret.ToInstruction());
method.DeclaringType = type;
module.Types.Add(type);
module.Write("Test.exe");
Console.WriteLine("OK");
Console.ReadKey();
1reaction
wwh1004commented, Dec 29, 2021
>	dnlib.dll!dnlib.DotNet.SignatureReader.ReadSig<dnlib.DotNet.MethodSig>(dnlib.DotNet.MethodSig methodSig) Line 493	C#
 	dnlib.dll!dnlib.DotNet.SignatureReader.ReadMethod(dnlib.DotNet.CallingConvention callingConvention) Line 474	C#
 	dnlib.dll!dnlib.DotNet.SignatureReader.ReadSig() Line 434	C#
 	dnlib.dll!dnlib.DotNet.SignatureReader.ReadSig(dnlib.DotNet.ModuleDefMD readerModule, uint sig, dnlib.DotNet.GenericParamContext gpContext) Line 70	C#
 	dnlib.dll!dnlib.DotNet.ModuleDefMD.ReadSignature(uint sig, dnlib.DotNet.GenericParamContext gpContext) Line 1219	C#
 	dnlib.dll!dnlib.DotNet.StandAloneSigMD.StandAloneSigMD(dnlib.DotNet.ModuleDefMD readerModule, uint rid, dnlib.DotNet.GenericParamContext gpContext) Line 174	C#

its metadata is invalid. bp at line ‘if (!reader.TryReadCompressedUInt32(out uint numParams))’ you will see ‘numParams’ is a extremely large number.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dnlib
dnlib is a library that can read, write and create .NET assemblies and modules. Become a Bounty Hunter
Read more >
0xd4d/dnlib: Reads and writes .NET assemblies and ...
First of all, the important namespaces are dnlib.DotNet and dnlib.DotNet.Emit . dnlib.DotNet.Emit is only needed if you intend to read/write method bodies.
Read more >
Untitled
Star stationery shop dunedin, Uber ride code, Too fat for memory foam, ... Re-reading or rereading, Djvijayraj, Skittles wedding dress commercial, ...
Read more >
Untitled
Gift cards online canada, Crontab every 15 minutes aix, #Education in 19th ... Siphano fk 2 episode 3, Arylide grove eglinton, F body...
Read more >
Untitled
Lenny venito family, Libdb 4.3 so 64bit, Ben hoitink canberra, Pen window launcher 1.5, ... Fantastic mr fox roald dahl read online, Wood...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found