[cfe-dev] -ftrapv

David Chisnall csdavec at swansea.ac.uk
Wed Apr 1 15:54:32 PDT 2009


Missed a bit.  The overflow-aware code is called on += and so on, but  
didn't know about these cases.  This patch fixes it.

I'm now using this with Smalltalk for testing for integer overflow in  
small integers and promoting them to real objects automatically.  It  
seems to be working perfectly, in a lot less code and with better  
accuracy than the ad-hoc code it replaces.

David

-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang.diff
Type: application/octet-stream
Size: 1504 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090401/c7b2d191/attachment.obj>
-------------- next part --------------


On 1 Apr 2009, at 16:54, David Chisnall wrote:

> I've attached a diff which adds -ftrapv to clang-cc (not to clang;  
> the driver code scares me) which uses the new LLVM overflow-checked  
> operations for +, - and * (not ++ or -- yet).  When an overflow is  
> detected, it calls a function in a supporting library to handle it.   
> I've provided a default implementation in overflowlib.c.  This can  
> be replaced in compiled code by some other function if required.   
> For 100% GCC compatibility just add:
>
> __overflow_handler = abort;
>
> somewhere before the operations that may overflow.
>
> The overflow.c file provided here shows a simple case where an  
> overflow occurs (the volatiles are just there to make absolutely  
> sure no optimisation passes decide to calculate the result at  
> compile time).  When you run this (linked against overflowlib.c),  
> you get:
>
> $ ./a.out
> Integer overflow!  Op unsigned add on 32-bit values, Aborting!
> Abort trap: 6 (core dumped)
>
> David
>
> <clang.diff><overflowlib.c><overflow.c>



More information about the cfe-dev mailing list