Skip to content

Commit ddfd466

Browse files
committed
add llvm instrumentation
1 parent e32bc80 commit ddfd466

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1881
-932
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ ENV HOPPER_BIN=/hopper/hopper \
1212
RUN apt-get update \
1313
&& apt-get -y upgrade \
1414
&& apt-get -y install build-essential wget curl cmake git unzip xxd protobuf-compiler libprotobuf-dev \
15-
&& apt-get -y install llvm-dev libclang-dev clang \
15+
&& apt-get -y install llvm llvm-dev libclang-dev clang \
1616
&& apt-get clean
1717

1818
# ENV RUSTUP_DIST_SERVER="https://mirrors.ustc.edu.cn/rust-static"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ To learn more about Hopper, check out our [paper](https://arxiv.org/pdf/2309.034
2121
```
2222

2323
The script will create a `install` directory in hopper's root directory, then you can use `hopper`.
24-
To use the command anywhere, you can set your project directory in your PATH variable.
24+
To use the command anywhere, you can set the project directory in your PATH variable.
2525

2626
### Using Docker
2727
You can choose to use the Dockerfile, which build the requirements and Hopper.
@@ -38,7 +38,7 @@ hopper compile --header ./cJSON.h --library ./libcjson.so --output output
3838

3939
Use `hopper compile --help` to see detailed usage. If the compiling reports errors about header file, refer to the usage of [rust-bindgen](https://rust-lang.github.io/rust-bindgen/), which we used for parsing header file.
4040
You may wrap the header file with the missing definitions.
41-
Hopper uses [E9Patch](https://github.com/GJDuck/e9patch) to instrument binaries by default.
41+
Hopper uses [E9Patch](https://github.com/GJDuck/e9patch) to instrument binaries by default. Optionally, you can use [LLVM](./hopper-instrument/llvm-mode/) for source code instrumentation.
4242

4343
After running `compile`, you will find that it generates the following files in the output directory:
4444
- `bin/hopper-fuzzer`: generates inputs, maintatins states, and use `harness` to excuted the inputs.
@@ -97,7 +97,7 @@ echo core | sudo tee /proc/sys/kernel/core_pattern
9797
```
9898

9999
### Function pattern
100-
Hopper generates inputs for all functions in libiries by default. However, there are two ways to filter functions in Hopper: exlucding functions or including functions. This way, it can be focus on intersting functions.
100+
Hopper generates inputs for all functions that appear in both headers and libiries by default. However, there are two ways to filter functions in Hopper: exlucding functions or including functions. This way, it can be focus on intersting functions.
101101

102102
#### `--func-pattern`
103103
```

build.sh

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,28 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
1919

2020
if [ ! -x $INSTALL_DIR/patchelf ]; then
2121
info "download patchelf ..."
22-
cd install
22+
cd $INSTALL_DIR
2323
mkdir -p tmp
2424
cd tmp
2525
wget https://github.com/NixOS/patchelf/releases/download/${PATCHELF_VERSION}/patchelf-${PATCHELF_VERSION}-x86_64.tar.gz
2626
tar -xvf patchelf-${PATCHELF_VERSION}-x86_64.tar.gz
2727
cp bin/patchelf ../.
28-
cd ../../
2928
fi
3029
fi
3130

32-
# info "start install hopper's llvm plugins ..."
33-
# cd hopper-instrument/llvm-mode
34-
# make PREFIX=$INSTALL_DIR
31+
info "start install hopper's llvm plugins ..."
32+
cd $INSTALL_DIR
33+
rm -rf llvm_build
34+
mkdir llvm_build && cd llvm_build
35+
cmake -DHOPPER_BIN_DIR=$INSTALL_DIR $ROOT_DIR/hopper-instrument/llvm-mode
36+
make
37+
make install
3538

36-
BUILD_TYPE=${BUILD_TYPE:-debug}
37-
# BUILD_TYPE=${BUILD_TYPE:-release}
39+
# BUILD_TYPE=${BUILD_TYPE:-debug}
40+
BUILD_TYPE=${BUILD_TYPE:-release}
3841

3942
info "start build and install hopper fuzzer ..."
43+
cd $ROOT_DIR
4044
if [[ "$BUILD_TYPE" == "debug" ]]; then
4145
cargo build
4246
else

examples/re2/hopper.config

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ TEST_HEADER=/data/workspace/fuzzing_bench/re2/cre2.h
55

66
# Full path to shared library
77
TEST_LIB_DIR=/data/workspace/fuzzing_bench/re2/build/hopper_build/
8-
TEST_LIBRARY=${TEST_LIB_DIR}/cre2/lib/libcre2.so ${TEST_LIB_DIR}/lib/libre2.so.10.0.0
8+
TEST_LIBRARY=${TEST_LIB_DIR}/cre2/lib/libcre2.so ${TEST_LIB_DIR}/lib/libre2.so
99

1010
# Output directory
1111
OUT_DIR=output
@@ -19,8 +19,7 @@ HOPPER_MAP_SIZE_POW2=20
1919
# set seeds for hopper
2020
# HOPPER_SEED_DIR=seeds
2121

22-
2322
# target library
2423
HOPPER_TEST_LIBRARY=cre2/lib/libre2.so
2524

26-
HOPPER_CUSTOM_RULES=$SRC_DIR/../custom_rule
25+
HOPPER_CUSTOM_RULES=$SRC_DIR/../custom_rule

hopper

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ source ${ROOT_DIR}/tools/core_affinity.sh
3535
if [[ -e "hopper.config" ]]; then
3636
while IFS='=' read -r key value; do
3737
if [[ $key && $value ]]; then
38+
if [[ $key = \#* ]] ; then
39+
continue
40+
fi
3841
declare -x "$key=$value"
3942
echo "config file set: $key = $value"
4043
fi
@@ -69,6 +72,14 @@ check_output() {
6972
warn "$USAGE"
7073
exit 1
7174
fi
75+
if [ "$OUTPUT_DIR" == "" ];then
76+
error "output directory can't be empty!"
77+
exit 1
78+
fi
79+
if [ "$OUTPUT_DIR" == "/" ];then
80+
error "output directory can't be root directory '/'!"
81+
exit 1
82+
fi
7283
}
7384

7485
[ ! -d "${ROOT_DIR}/install" ] && warn "Please run ./build.sh to build hopper's code"
@@ -183,6 +194,10 @@ clean)
183194
find . -maxdepth 1 -type f ! -executable ! -name "test*" ! -name "*.log" ! -name "func_list" ! -name "custom_rule" ! -name "hopper.config" -delete
184195
info "clean files in '$OUTPUT_DIR' directory"
185196
;;
197+
clang)
198+
echo "CC=${ROOT_DIR}/install/hopper-clang"
199+
echo "CXX=${ROOT_DIR}/install/hopper-clang++"
200+
;;
186201
help)
187202
warn "$USAGE"
188203
exit 0

hopper-compiler/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ gimli = { version = "0.26", default-features = false, features = ["read"] }
1717
object = { version = "0.28", default-features = false, features = ["read"] }
1818
eyre = "0.6"
1919
twoway = "0.2"
20+
# NO GPL
21+
# patchelf = "0.2.1"
2022

2123
[features]
2224
default = ["elf", "dwarf"]

hopper-compiler/src/binary_info.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub struct BinaryInfo {
1111
pub str_list: Vec<String>,
1212
// exported API in the symbol table
1313
pub func_list: Vec<FuncInfo>,
14+
// needed libraries
15+
pub needed: Vec<String>
1416
}
1517

1618
#[derive(Debug)]
@@ -29,6 +31,7 @@ impl BinaryInfo {
2931
let lib_type;
3032
let mut str_list = vec![];
3133
let mut func_list = vec![];
34+
let mut needed = vec![];
3235
match result {
3336
Object::Elf(elf) => {
3437
lib_type = "elf";
@@ -67,6 +70,12 @@ impl BinaryInfo {
6770
})
6871
}
6972
}
73+
if let Some(dy) = elf.dynamic.as_ref() {
74+
for name in dy.get_libraries(&elf.dynstrtab) {
75+
needed.push(name.to_string());
76+
}
77+
}
78+
7079
}
7180
Object::PE(pe) => {
7281
lib_type = "pe";
@@ -112,6 +121,7 @@ impl BinaryInfo {
112121
lib_type,
113122
str_list,
114123
func_list,
124+
needed,
115125
})
116126
}
117127

hopper-compiler/src/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn output_lib_name(file: &str) -> String {
5252
pub fn check_llvm_runtime(libraries: &[String]) -> bool {
5353
libraries
5454
.iter()
55-
.any(|l| check_file_contains(l, "HOOPER_LLVM_MARK"))
55+
.any(|l| check_file_contains(l, "__hopper_area_ptr"))
5656
}
5757

5858
pub fn check_file_contains(target: &str, s: &str) -> bool {

hopper-compiler/src/main.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ fn instrument(library: &Path, output: &Path, config: &Config, lib_info: &BinaryI
7575
}
7676
}
7777
#[cfg(target_os = "linux")]
78-
patch::patchelf_set_so_name(&lib_name, output_lib.to_str().context("fail to be str")?)?;
78+
{
79+
let output_lib_path = output_lib.to_str().context("fail to be str")?;
80+
patch::patchelf_set_so_name(&lib_name, output_lib_path)?;
81+
patch::remove_prev_needed(&config.library, output_lib_path, lib_info)?;
82+
}
7983
Ok(output_lib)
8084
}
8185

0 commit comments

Comments
 (0)