Can BitConverter be used to reliably extract multi-byte values from an IL byte stream (as returned by MethodBody.GetILAsByteArray)?

StackOverflow https://stackoverflow.com/questions/11921824

Question

I am working on some code that parses IL byte arrays as returned by MethodBody.GetILAsByteArray.

Lets say I want to read a metadata token or a 32-bit integer constant from such an IL byte stream. At first I thought using BitConverter.ToInt32(byteArray, offset) would make this easy. However I'm now worried that this won't work on big-endian machines.

As far as I know, IL always uses little-endian encoding for multi-byte values:

"All argument numbers are encoded least-significant-byte-at-smallest-address (a pattern commonly termed 'little-endian')."The Common Language Infrastructure Annotated Standard, Partition III, ch. 1.2 (p. 482).

Since BitConverter's conversion methods honour the computer architecture's endianness (which can be discovered through BitConverter.IsLittleEndian), I conclude that BitConverter should not be used to extract multi-byte values from an IL byte stream, because this would give wrong results on big-endian machines.

Is this conclusion correct?

  • If yes: Is there any way to tell BitConverter which endianness to use for conversions, or is there any other class in the BCL that offers this functionality, or do I have to write my own conversion code?

  • If no: Where am I wrong? What is the proper way of extracting e.g. a Int32 operand value from an IL byte array?

Was it helpful?

Solution

You should always do this on a little endian array before passing it:

// Array is little. Are we on big?
if (!BitConverter.IsLittleEndian)
{
    // Then flip it
    Array.Reverse(array);
}
int val = BitConverter.ToInt32(...);

However as you mention an IL stream. The bytecode is this (AFAIK):

(OPCODE:(1|2):little) (VARIABLES:x:little)

So I would read a byte, check its opcode, then read the appropriate bytes and flip the array if necessary using the above code. Can I ask what you are doing?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top