[PATCH] Fix signed overflow in encoder for ARM adr instruction

Mihail Popa mihail.popa at gmail.com
Tue Aug 13 06:40:19 PDT 2013


This cast:
-  int32_t offset = MO.getImm();
+  int64_t offset = MO.getImm();

The value is encoded in 12 bits. 8 are the core value, 4 dictacte rotation.
It is possible to encode numbers larger that  2^31.
the MO contains the actual  number, not the one to be encoded.
as such casting from int64 to int32 produces signed overflow - undefined
behavior.

on x86 this flips the sign bit cause the instruction to be encoded as a
subtract instead of an add.


On Tue, Aug 13, 2013 at 2:37 PM, Mihail Popa <mihail.popa at gmail.com> wrote:

> This cast:
>
>
>
> On Tue, Aug 13, 2013 at 2:14 PM, Renato Golin <renato.golin at linaro.org>wrote:
>
>> On 13 August 2013 10:05, Mihail Popa <mihail.popa at gmail.com> wrote:
>>
>>> As such it is possible to add or subtract a 32-bit unsigned number.
>>>
>>
>> But, immediate values are up to 12-bits long, why is 32-bit not enough?
>>
>>
>> The cast to int32_t is undefined and triggers incorrect encoding. Keeping
>>> the original
>>> int64_t type is safe, as all subsequent casts are to uint32_t.
>>>
>>
>> Which cast?
>>
>> I could be wrong, but this sounds like you're fixing a problem in the
>> wrong place...
>>
>> cheers,
>> --renato
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130813/2381f6c4/attachment.html>


More information about the llvm-commits mailing list