locked
Adding custom local variables in header of function

    Întrebare

  • Hi,
    I have been instrumenting IL code in web apps for my project. I add custom IL code with local variables in methods with no local variables. I have tried ways to add my custom local variables into methods which are already have local variables in header by could not succeed.. 

    I have used 
    COR_ILMETHOD_DECODER decoder((COR_ILMETHOD *) pMethod);
    m_LocalSigToken = decoder.GetLocalVarSigTok();

    to get the local variable sigranture token. 

    Now i need to add my custom variables in this local variables token... How can this be achieved..?

    mdSignature signature;
    metaDataEmit->GetTokenFromSig(localSignature, sizeof(localSignature), &signature); 

    I use GetTokenFromSig to convert my local variable structure in bytes to the token.

    I am not used to byte manipulation in C++ so i am stuck as to how to manipulate the bytes to get the correct local signature with my custom variables.

    Can anyone help me out please. Thank you.

    -Jasper




    ..JPJ..

    6 martie 2012 06:41

Răspunsuri

Toate mesajele

  • DavidBr wrote an article (code sample) on that 

    http://blogs.msdn.com/b/davbr/archive/2006/05/18/601315.aspx 

    after much manipulation he appends his new local

    rgbNewSig[iNewSig++] = ELEMENT_TYPE_VALUETYPE; 

    and then rebuilds a new token to replace the one in your header.

    • Marcat ca răspuns de Jasper paul 7 martie 2012 05:29
    7 martie 2012 02:21
  • Thanks Shaun for the link.

    I'll try this. 

    rgbNewSig[iNewSig++] = ELEMENT_TYPE_VALUETYPE; code is the new local variables which will be added into the existing one.

    What is 0x01000002 that is used..? 

    mentioned in 

    // You'll need to replace 0x01000002 with the appropriate token that describes
        // the type of this local (which, in turn, is the type of the return value
        // you're copying into that local).  This can be either a TypeDef or TypeRef,
        // and it must be encoded (compressed).
        if (iNewSig + 4 > sizeof(rgbNewSig))
        {
            // CorSigCompressToken will write up to 4 bytes but no room!
            return 0;
        }
        cbLocalType = CorSigCompressToken(0x01000002,
                                          &rgbNewSig[iNewSig]);

    Any idea?


    ..JPJ..


    7 martie 2012 05:29
  • I think it's a public token of the metadata.

    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us

    7 martie 2012 08:51
    Moderator
  • How do I get the public token...? Any helpers?  



    ..JPJ..

    7 martie 2012 09:00
  • From code, I don't think we can get it as far as know.

    But we can use the IL DASM or the refletor tool to find the token.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us

    7 martie 2012 09:20
    Moderator
  • hmm ok, Which public token are we talking here..? the local variables that is going to be appended along with the original locals..?

    How can I get that? Since I don't have .net code and obviously ILDASM can only disassemble .net code. 

    Are are we both talking different things here..?

    Sorry, if I misunderstood... Can u give a short explanation probably with an eg? It would help the continuity of the post on what the public token of the metadata u r talking about.


    ..JPJ..

    7 martie 2012 09:38
  • The following links about Metadata Tokens for your reference:

    http://msdn.microsoft.com/en-us/library/ms404456.aspx


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us

    7 martie 2012 09:41
    Moderator
  • App crashes when trying to use code from the link.. 

    I just cut paste the code from the DavidBr forum link and added the object array locals in the end.
    The app crashes at this stage.. obviously there is something wrong.. What is going wrong i can't figure out.

        // Here's a buffer into which we will write out the modified signature.  This sample
        // code just bails out if it hits signatures that are too big.  Just one of many reasons
        // why you use this code AT YOUR OWN RISK!
        COR_SIGNATURE rgbNewSig[4096];

        rgbNewSig[iNewSig++] = IMAGE_CEE_CS_CALLCONV_LOCAL_SIG;
    rgbNewSig[iNewSig++] = ELEMENT_TYPE_CHAR;
    rgbNewSig[iNewSig++] = ELEMENT_TYPE_STRING;
    rgbNewSig[iNewSig++] = ELEMENT_TYPE_ARRAY;
    rgbNewSig[iNewSig++] = ELEMENT_TYPE_OBJECT;
    rgbNewSig[iNewSig++] = 01;
    rgbNewSig[iNewSig++] = 00;
    rgbNewSig[iNewSig++] = 00;
    rgbNewSig[iNewSig++] = ELEMENT_TYPE_ARRAY;
    rgbNewSig[iNewSig++] = ELEMENT_TYPE_OBJECT;
    rgbNewSig[iNewSig++] = 01;
    rgbNewSig[iNewSig++] = 00;
    rgbNewSig[iNewSig++] = 00;

    //CorSigCompressToken(0x01000002, &rgbNewSig[iNewSig]); I have not used this, because I don't have exception in my local sig.  How can this be solved, any clues, ideas.

    I use OpenCover for instrumentation.

    -Jasper


    ..JPJ..

    12 martie 2012 10:43