Jump to content
Tuts 4 You

MethodSig to LocalSig conversion problem - resolve Local variables from byte array


CodeExplorer

Recommended Posts

CodeExplorer
Posted

 Hi. I have problems with restoring local variable from byte array, when I get variables Signature and I try to convert them to LocalSig I get and exception because can't convert MethodSig to LocalSig. Any help would be great.

                            {
                                using (BinaryReader binaryReader4 = new BinaryReader(new MemoryStream(this.DataStructure.LEH)))
                                {
                                    binaryReader4.BaseStream.Position = (long)hInt;
                                    MethodBodySize1 = binaryReader4.ReadInt32() >> 8;  // 104
                                    short LocalsSize2 = binaryReader4.ReadInt16();
                                    short EHSize2 = binaryReader4.ReadInt16();
                                    binaryReader4.ReadInt16();
                                    methodInfo2.MethodData = binaryReader.ReadBytes(MethodBodySize1);
                                    var SignatureRead = SignatureReader.ReadSig(this.module, binaryReader4.ReadBytes((int)LocalsSize2));
                                    IList<TypeSig> locals;
                                    if (SignatureRead is LocalSig)
                                    {
                                    locals = ((LocalSig)(SignatureRead)).GetLocals();
                                    for (int k = 0; k < locals.Count; k++)
                                        methodInfo2.Method_Locals.Add(new Local(locals[k]));
                                    }
                                    else if (SignatureRead is MethodSig)
                                    {
                                        MethodSig SignatureRead_MS = SignatureRead as MethodSig;
                                        
                                        //locals = ((MethodSig)(SignatureRead)).GetLocals();                    
                                    }
                                    
                                    methodInfo2.MethodEH = ((EHSize2 == 0) ? null : binaryReader4.ReadBytes((int)EHSize2));
                                }
                            }

 

  • Like 1
Posted

how about explore/educate from the de4dot code?

class MethodBodyReader.... 

  • Like 1
Posted

here's how i do in jitdumper anonymoose when using dnlib

image.png.38097bd14616293f0319965003d5fc5d.png

  • Like 1
CodeExplorer
Posted

Actually there is no error with the above code, it is just that bytes signatures was wrong so it will throw exception;
throwing error is naturally in such case I think!
Here is the final code used:

                        IList<TypeSig> locals = ((LocalSig)SignatureReader.ReadSig(this.module, binaryReader4.ReadBytes((int)LocalsSize2))).GetLocals();
                            for (int l = 0; l < locals.Count; l++)
                            {
                                methodInfo2.Method_Locals.Add(new Local(locals[l]));
                            }


 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...