byte vs sbyte

View: New views
4 Messages — Rating Filter:   Alert me  

byte vs sbyte

by sergiy.sakharov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello,

Yesterday I've stumbled upon weird errors while instrumenting existing assembly:
"Common Language Runtime detected an invalid program. while compiling method"
After some digging I found out te problem was in an overflow of conditional IL operator:
IL_00...:  brtrue.s   IL_ffffffc9
(actually there were a number of overflows not only with brtrue.s operator..)

This happened when I inserted some instructions in the middle of method and therefore brtrue.s ' int8 size of offset was not enough any more, so now I have to create myself some checks and replace small conditional instructions in such cases, but that's not the story...

During this investigation I've found a place in CodeWriter.cs that could be changed a bit:

Line 215:
from m_codeWriter.Write ((byte) (((Instruction) instr.Operand).Offset -
to m_codeWriter.Write (checked((sbyte) (((Instruction) instr.Operand).Offset -

Firstly - sbyte is actually INT8 which is ecpected as an operant of ".s" operators, and secondly - checked will actually throw exception in case of overflow.
I know that "Cecil does absolutely no verification on what it emits.", but just in case...

Best Regards.
Sergiy

Re: byte vs sbyte

by Simon Goldschmidt :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hi,

maybe the mail from cygin on march, 18th helps you:

> MethodBody.Simplify() simplifies the body by turning short
> instructions into normal ones (e.g. leave_s to leave)
> After that, you can inject instructions without overflowing the
> offsets.
> Finally, you can use MethodBody.Optimize() to turn all into short form
> when possible.

Regard,
Simon


sergiy.sakharov wrote:

> Hello,
>
> Yesterday I've stumbled upon weird errors while instrumenting existing
> assembly:
> "Common Language Runtime detected an invalid program. while compiling
> method"
> After some digging I found out te problem was in an overflow of conditional
> IL operator:
> IL_00...:  brtrue.s   IL_ffffffc9
> (actually there were a number of overflows not only with brtrue.s
> operator..)
>
> This happened when I inserted some instructions in the middle of method and
> therefore brtrue.s ' int8 size of offset was not enough any more, so now I
> have to create myself some checks and replace small conditional instructions
> in such cases, but that's not the story...
>
> During this investigation I've found a place in CodeWriter.cs that could be
> changed a bit:
>
> Line 215:
> from m_codeWriter.Write ((byte) (((Instruction) instr.Operand).Offset -
> to m_codeWriter.Write (checked((sbyte) (((Instruction)
> instr.Operand).Offset -
>
> Firstly - sbyte is actually INT8 which is ecpected as an operant of ".s"
> operators, and secondly - checked will actually throw exception in case of
> overflow.
> I know that "Cecil does absolutely no verification on what it emits.", but
> just in case...
>
> Best Regards.
> Sergiy
>
>  


--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---


Re: byte vs sbyte

by Jb Evain-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Hey,

On 8/1/09, sergiy.sakharov <sakharov@...> wrote:
>  Firstly - sbyte is actually INT8 which is ecpected as an operant of ".s"
>  operators, and secondly - checked will actually throw exception in case of
>  overflow.

Nope that's not true. You can have negative operands to jump backwards.

>  I know that "Cecil does absolutely no verification on what it emits.", but
>  just in case...

Indeed, and just as Simon says, the solution is to call Simplify
before instrumenting, then calling optimize.

--
Jb Evain  <jb@...>

--~--~---------~--~----~------------~-------~--~----~
--
mono-cecil
-~----------~----~----~----~------~----~------~--~---


Re: byte vs sbyte

by sergiy.sakharov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

Jb Evain-2 wrote:
Hey,

On 8/1/09, sergiy.sakharov <sakharov@gmail.com> wrote:
>  Firstly - sbyte is actually INT8 which is ecpected as an operant of ".s"
>  operators, and secondly - checked will actually throw exception in case of
>  overflow.

Nope that's not true. You can have negative operands to jump backwards.
But SByte value type represents integers with values ranging from negative 128 to positive 127. While byte is the one who is always positive?

As for solution - it sounds great for me, I'll check i t out..

Best Regards,
Sergiy