========================================================================t
Building and Installing a Checking Clang

1. Get the appropriate LLVM/Clang revision from SVN

2. Go to "llvm/tools/clang" 

3. Patch the code:

  patch -p0 < clang-<version>.patch

4. Build LLVM and Clang:
   1). cd ../.. (back to llvm)
   2). ./configure --enable-optimized
   3). make
   4). make install

Probably you'll want to make sure the new clang and clang++ are in
your path

5. Build the trap handler:

  clang -O2 -Wall -fPIC -c -o libtrapub.so trap_handler.c

6. Install the trap handler

Copy libtrapub.so to a directory in LD_LIBRARY_PATH, such as
/usr/local/lib

=========================================================================
Using the Checking Clang

Simply add this flag to clang / clang++ invocations:

    -fcatch-undefined-behavior
or  -fcatch-undefined-ansic-behavior
or  -fcatch-undefined-c99-behavior
or  -fcatch-undefined-cxx0x-behavior
or  -fcatch-undefined-cxx98-behavior 

The flags above only target integer arithmetic operations. Besides, our undefined
behavior checker still supports some other checks. To activate them, please add
"-fcatch-undefined-nonarith-behavior" option in addition to the flags above.

If users wish to invoke the GCC's intrinsic checks to examine the potential
overflows for addition, subtraction and multiplication operations, they
could add "-fuse-intrinsic" flag during compilation; otherwise, the ub checker
will only apply our own checks for basic arithmetic operations instead.

By default, the checked program will print a message to STDERR before
executing any operation that fails an undefined behavior check.  To
direct the output elsewhere, define the CLANG_UNDEFINED_LOG_METHOD
environment variable to one of STDOUT, STDERR, SYSLOG, or NONE.

By default, every instance of undefined behavior results in a message
being printed.  In a long checked execution, the output may become
large.  To suppress excessive messages, set the environment variable
CLANG_UNDEFINED_MESSAGE_LIMIT to a positive integer.  The number of
error messages per program point and kind of error will not exceed
this number.

By default, execution continues after an operating with undefined
behavior is performed.  The result of this operation is undefined,
clang or clang++ does whatever it wants (probably it acts the same as
it did without the -fcatch-undefined-behavior flag, but we cannot
guarantee this).  To terminate execution following an operation with
undefined behavior, set the CLANG_UNDEFINED_ABORT environment variable
to any value.

=========================================================================
An Example

Copy this code into a file undef.c:

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>

static int big_r (void)
{
  int a = rand();
  if ((rand()%2)==0)
    return a;
  else 
    return -a;
}

static int small_r (void)
{
  int a = rand();
  a &= 0xff;
  if ((rand()%2)==0)
    return a;
  else 
    return -a;  
}

int main (void)
{
  srand (time(NULL)+getpid());
  int i;
  int c = big_r();
  for (i=0; i<10000; i++) {
    c += big_r();
    c -= big_r();
    c *= big_r();
    c /= small_r();
    c %= small_r();
    c >>= small_r();
    c <<= small_r();
  }
  return c;
}

=========================================================================
