2016/Fundamentals4

From CS61
Jump to: navigation, search

9/13/16: Fundamentals 4: Arena Allocation II

On sanitizers

At the end of lecture we introduced a feature from recent C compilers, the sanitizers. Modern C compilers can compile code in a way that includes checks for various problems, including undefined behavior and out-of-bounds memory accesses.

Our current makefiles for cs61-exercises and cs61-psets enable the sanitizers on request. If you compile like this:

make SANITIZE=1

the relevant sanitizer flags will be provided.

The CS61 VM, unfortunately, has a relatively old compiler. GCC-4.9 is the earliest GCC including the undefined behavior sanitizer, and the VM ships with GCC-4.8. The default Mac OS X C compiler also is a little too old.

Here’s how to upgrade the CS61 VM to GCC-6, a more modern compiler.

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-6
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-6 50

At this point make SANITIZE=1 should work, and give you both sanitizers. Check this by running:

cd cs61-exercises/fundamentals4
git pull
make SANITIZE=1
./add 0x7fffffff 1

It should print something like:

add.c:6:5: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
add: add.c:6: add: Assertion `a + 1 > a' failed.
Aborted (core dumped)

On Mac OS X, install a newer compiler with Homebrew. I've installed the gcc-6 package. Then you can specify a specific compiler like so:

make SANITIZE=1 CC=gcc-6

Post-class survey: Please fill this out.