1#!/bin/sh
2#
3# american fuzzy lop++ - clang assembly normalizer
4# ----------------------------------------------
5#
6# Originally written by Michal Zalewski
7# The idea for this wrapper comes from Ryan Govostes.
8#
9# Copyright 2013, 2014 Google Inc. All rights reserved.
10#
11# Licensed under the Apache License, Version 2.0 (the "License");
12# you may not use this file except in compliance with the License.
13# You may obtain a copy of the License at:
14#
15#   http://www.apache.org/licenses/LICENSE-2.0
16#
17# This 'as' wrapper should allow you to instrument unruly, hand-written
18# assembly with afl-as.
19#
20# Usage:
21#
22# export AFL_REAL_PATH=/path/to/directory/with/afl-as/
23# AFL_PATH=/path/to/this/directory/ make clean all
24
25if [ "$#" -lt "2" ]; then
26  echo "[-] Error: this utility can't be called directly." 1>&2
27  exit 1
28fi
29
30if [ "$AFL_REAL_PATH" = "" ]; then
31  echo "[-] Error: AFL_REAL_PATH not set!" 1>&2
32  exit 1
33fi
34
35if [ ! -x "$AFL_REAL_PATH/afl-as" ]; then
36  echo "[-] Error: AFL_REAL_PATH does not contain the 'afl-as' binary." 1>&2
37  exit 1
38fi
39
40unset __AFL_AS_CMDLINE __AFL_FNAME
41
42while [ ! "$#" = "0" ]; do
43
44  if [ "$#" = "1" ]; then
45    __AFL_FNAME="$1"
46  else
47    __AFL_AS_CMDLINE="${__AFL_AS_CMDLINE} $1"
48  fi
49
50  shift
51
52done
53
54test "$TMPDIR" = "" && TMPDIR=/tmp
55
56TMPFILE=`mktemp $TMPDIR/.afl-XXXXXXXXXX.s`
57
58test "$TMPFILE" = "" && exit 1
59
60clang -cc1as -filetype asm -output-asm-variant 0 "${__AFL_FNAME}" >"$TMPFILE"
61
62ERR="$?"
63
64if [ ! "$ERR" = "0" ]; then
65  rm -f "$TMPFILE"
66  exit $ERR
67fi
68
69"$AFL_REAL_PATH/afl-as" ${__AFL_AS_CMDLINE} "$TMPFILE"
70
71ERR="$?"
72
73rm -f "$TMPFILE"
74
75exit "$ERR"
76