[cfe-dev] -ftrapv flag leads to compilation error

David Chisnall csdavec at swan.ac.uk
Tue May 11 15:28:40 PDT 2010


If you are using -ftrapv, then you must implement the overflow handler.  This can be used to throw an exception, abort, or some other behaviour (I use it in LanguageKit, for example, to box the result in an arbitrary-precision integer object, and we've also used it to implement some proposed C1x overflow handling drafts).  

The signature for this function is: 

long long overflow_handler(long long val, long long otherval, char op, char width);

If this function returns, then the result will be truncated and used in place of the real result.  The op and width arguments represent the operation and the size of the arguments.

The function should be assigned to a function pointer declared like this, in some library that your code links against:

long long (*__overflow_handler)(long long a, long long b, char op, char width);

A sample implementation of this was part of the original patch that I sent implementing -ftrapv, but it was not added to svn because there was no clang runtime library.  If you check the list archives, then you can find it. 

You are correct that this performs run-time overflow checking.  Compile-time overflow checking should be a default option, and not dependent on -ftrapv, but is only possible in a relatively limited number of cases.  This option is intended for use in functions like calloc() that need to do some special handling if the result of multiplying two values together is 0.  A trivial handler for this case might simply return 0.  You would then implement a test-for-0 after the multiply and return NULL for 0-sized allocations.  

One of the things that I never got around to adding was support for per-compilation-unit handler functions. Ideally, it would be nice to be able to do -ftrapv=my_handler and have my_handler() called on overflow.

David

On 11 May 2010, at 23:09, peng li wrote:

> Hi there
> 
> First I used command "clang -o add add.c -ftrapv" to compile the add.c 
> which implements two signed integers add operation leading to overflow. 
> But clang can not compile successfully. The concrete error
> message is pasted as follows:
> 
> /tmp/cc-oMjvJr.o: In function `main':
> add.c:(.text+0x3b): undefined reference to `__overflow_handler'
> collect2: ld returned 1 exit status
> clang: error: linker command failed with exit code 1 (use -v to see 
> invocation)
> 
> 
> Then I also extended the command to "clang -o add add.c -ftrapv -S 
> -emit-llvm" and found the dynamic checks have been expressed and 
> instrumented into the corresponding LLVM code. I think those checks, 
> such as "overflow", "overflow.continue" should also be considered as the 
> dynamic undefined behavior checks, correct?? Since resorting to those 
> checks, clang will not dump the warnings at compile time.
> 
> 
> Regards
> 
> 
> Peng
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


-- Sent from my Cray X1





More information about the cfe-dev mailing list