0xd4d / dnlib

Reads and writes .NET assemblies and modules
MIT License
2.14k stars 583 forks source link

dnlib.DotNet.Writer.ModuleWriterException #548

Closed CatLover248 closed 7 months ago

CatLover248 commented 7 months ago

Hello!

I am having some issues. I am writing to a cilBody then merging the il. But when I try to write it with module.Write() I get an error.


var cilBody = method.Body;
method.Body.Instructions.Clear();
cilBody.Instructions.Add(new Instruction......
method.Body.UpdateInstructionOffsets();
method.Body.KeepOldMaxStack = true;

dnlib.DotNet.Writer.ModuleWriterException HResult=0x80131500 Message=Instruction is null. Error occurred after metadata event BeginWriteMethodBodies during writing method 'System.Void ConsoleAppTestForObfuscation.Program::Main(System.String[])' (0x06000001). Source=dnlib StackTrace: at dnlib.DotNet.DummyLogger.Log(Object sender, LoggerEvent loggerEvent, String format, Object[] args) at dnlib.DotNet.Writer.ModuleWriterBase.dnlib.DotNet.ILogger.Log(Object sender, LoggerEvent loggerEvent, String format, Object[] args) at dnlib.DotNet.Writer.Metadata.Error(String message, Object[] args) at dnlib.DotNet.Writer.Metadata.dnlib.DotNet.Writer.IWriterError.Error(String message) at dnlib.DotNet.Writer.MethodBodyWriter.ErrorImpl(String message) at dnlib.DotNet.Writer.MethodBodyWriterBase.Error(String message) at dnlib.DotNet.Writer.MethodBodyWriterBase.GetOffset(Instruction instr) at dnlib.DotNet.Writer.MethodBodyWriterBase.WriteShortInlineBrTarget(ArrayWriter& writer, Instruction instr) at dnlib.DotNet.Writer.MethodBodyWriterBase.WriteOperand(ArrayWriter& writer, Instruction instr) at dnlib.DotNet.Writer.MethodBodyWriterBase.WriteInstruction(ArrayWriter& writer, Instruction instr) at dnlib.DotNet.Writer.MethodBodyWriterBase.WriteInstructions(ArrayWriter& writer) at dnlib.DotNet.Writer.MethodBodyWriter.WriteFatHeader() at dnlib.DotNet.Writer.MethodBodyWriter.Write() at dnlib.DotNet.Writer.Metadata.WriteMethodBodies() at dnlib.DotNet.Writer.Metadata.Create() at dnlib.DotNet.Writer.Metadata.CreateTables() at dnlib.DotNet.Writer.ModuleWriter.WriteImpl() at dnlib.DotNet.Writer.ModuleWriterBase.Write(Stream dest) at dnlib.DotNet.Writer.ModuleWriterBase.Write(String fileName) at dnlib.DotNet.ModuleDef.Write(String filename, ModuleWriterOptions options) at dnlib.DotNet.ModuleDef.Write(String filename) at MeteorObfuscation.Meteor.MeteorObfuscation.init(String path) in C:\Users\hampter\source\repos\MeteorObfuscation\MeteorObfuscation\Meteor\MeteorObfuscation.cs:line 37 at MeteorObfuscation.Form1.button1_Click(Object sender, EventArgs e) in C:\Users\hampter\source\repos\MeteorObfuscation\MeteorObfuscation\Form1.cs:line 27 at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(HWND hWnd, MessageId msg, WPARAM wparam, LPARAM lparam)

wtfsck commented 7 months ago

The message says the instruction is null so check which instruction is null. Also check exception handlers and other places that could have instruction references.

CatLover248 commented 7 months ago

Thank you for your help!

Ive been able to identify the null instruction by checking if the individual instructions are null.