[cfe-dev] -ftrapv

David Chisnall csdavec at swansea.ac.uk
Wed Apr 1 08:54:21 PDT 2009


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang.diff
Type: application/octet-stream
Size: 7108 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090401/1cd482b4/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: overflowlib.c
Type: application/octet-stream
Size: 483 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090401/1cd482b4/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: overflow.c
Type: application/octet-stream
Size: 254 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20090401/1cd482b4/attachment-0002.obj>


More information about the cfe-dev mailing list