Fuzzing on POWER Linux with AFL

Fuzzing has got a lot of attention lately and if you write C or C++ code and haven’t thought about fuzzing, you really should. It’s common to find dozens of bugs when first applying it to a project.

To get familiar with the latest in fuzzing, I decided to fuzz DTC, our tool for manipulating flattened device trees. Fuzzing is very effective at testing tools that take input from files, so DTC fits the bill. For other projects, you can either build a test harness that takes file input or use the LLVM fuzzing library.

As for the tool, I chose American Fuzzy Lop (AFL). AFL is a fuzzer that is both powerful and easy to use—you can be up and running in minutes. It instruments the code and uses that feedback in order to discover new significant test cases. It also comes with an LLVM plugin which works well on POWER Linux.

Here are the steps I took to fuzz DTC. I’m starting from a ppc64le Ubuntu 15.10 Docker image, so the first step is to install some required packages:

apt-get install build-essential wget git llvm clang flex bison

Now download and build AFL, including the LLVM plugin:

mkdir -p $HOME/afl
cd $HOME/afl
wget -N http://lcamtuf.coredump.cx/afl/releases/afl-latest.tgz
tar xzf afl-latest.tgz --strip-components=1
make AFL_NOX86=1
cd llvm_mode
cd ../

Download and build DTC using the LLVM plugin:

git clone git://git.kernel.org/pub/scm/utils/dtc/dtc.git
cd dtc
make CC=$HOME/afl/afl-clang-fast

Create an input and output directory, and seed the input directory. I just chose one of the DTC test cases for this. These will be mutated to produce new test cases. You can add as many as you want but keep them reasonably small:

mkdir -p in out
cp tests/fdtdump.dts in

Run it!

$HOME/afl/afl-fuzz -i in -o out -- ./dtc -I dts @@

Notice how @@ is used to specify the name of the input file to be tested. AFL will feed the file to STDIN otherwise.

That simple setup has found quite a number of bugs. Thanks to David Gibson, the maintainer of DTC, for fixing them all!

AFL also produces a very useful test corpus in out/queue which you can use for more heavyweight testing, eg valgrind or the LLVM sanitizers.


Swift on POWER Linux

Apple open sourced the Swift language recently, and I’ve been meaning to take a look at how much work it would be to port it to POWER Linux.

As with many languages that use LLVM, it turned out to be relatively straightforward. I submitted a patch a few days ago, and thanks to some great assistance from Dmitri Gribenko in reviewing my work, support for little endian PowerPC64 is already upstream.

A couple of things made the port go smoothly. Firstly the compiler is written in C++ and not Swift. It seems rewrite the Swift compiler in Swift is a common request, but sticking to C++ makes the bootstrap process so much easier. I’ve had to bootstrap other compilers (Rust) that are written in their target language, and that requires a cross build from a supported architecture which never goes smoothly.

More often than not, languages need to link to C objects and libraries, so they need knowledge of the C calling convention rules. Much of this knowledge is not in LLVM (it’s in the Clang frontend), and as a result most LLVM based languages end up duplicating it (Rust, Julia). Uli Weigand pointed out to me that Swift pulls in Clang to provide this, which really helps. With all the duplication across languages, it might make sense to move this logic into an LLVM library instead of Clang.

Even though the port is upstream, there is one required LLVM change here – Swift has some calling conventions that are different to C, and I’m hoping one of the POWER LLVM team will take pity on me and sort it out.

Building Swift on POWER is straightforward on Ubuntu 15.10, just remember to add the LLVM patch above after checking out the source.

The testsuite runs with only a couple of failures:

Failing Tests (2):
    Swift :: 1_stdlib/VarArgs.swift
    Swift :: IRGen/c_layout.sil

  Expected Passes    : 7061
  Expected Failures  : 78
  Unsupported Tests  : 699
  Unexpected Failures: 2

And more importantly, hello world works:

# uname -m

# cat hello.swift
print("Hello, world!")

# swift hello.swift
Hello, world!

There is still work to be done to get big endian PowerPC64 going, but little endian seems solid for any of your hello world needs.


Using perf, the Linux Performance Analysis tool on Ubuntu Karmic

A lot has been going on with Linux performance counters (now called performance events), but there is enough functionality in the 2.6.31 kernel that ships with Ubuntu karmic to be able to use some of the features available in perf. I recently found it useful when debugging a performance issue on my mythtv frontend.

To build perf, first install the dependencies:

sudo apt-get install libelf-dev binutils-dev

Then grab a recent kernel source tree and build perf:

wget http://www.kernel.org/pub/linux/kernel/v2.6/testing/linux-2.6.33-rc3.tar.bz2
tar xjf linux-2.6.33-rc3.tar.bz2
cd linux-2.6.33-rc3/tools/perf
make install

It will warn that libdwarf-dev is not installed, but the version in karmic is too old and regardless libdwarf is only required for event tracing that appeared in more recent kernels. perf installs into ~/bin/perf. You should then be able to use the top, stat, list, record, report and annotate options.