Skip to content

Commit 8bbeaa9

Browse files
authored
Merge pull request #69 from AngoraFuzzer/dev
Implement never-zero counter and inst_ratio
2 parents 92fba70 + 1a57e26 commit 8bbeaa9

File tree

7 files changed

+48
-21
lines changed

7 files changed

+48
-21
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,3 @@ For more information, please refer to the documentation under the
112112
- [Environment variables](./docs/environment_variables.md)
113113
- [UI Terminology](./docs/ui.md)
114114
- [Troubleshoot](./docs/troubleshoot.md)
115-
116-
--------
117-
Angora is maintained by [ByteDance AI Lab](https://ailab.bytedance.com/) now.

docs/environment_variables.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- `ANGORA_OUTPUT_COND_LOC=1` : (Debug option) Output the location of each predicate during compiling.
99
- `ANGORA_TAINT_CUSTOM_RULE=/path/to/object` : object contains those proxy function (how to propagate taints), e.g. `ANGORA_TAINT_CUSTOM_RULE=~/angora/bin/lib/zlib-func.o` . You should add it as custom type in the file passed by `ANGORA_TAINT_RULE_LIST` first.
1010
- `ANGORA_TAINT_RULE_LIST=/path/to/list` : DataFlowSanitizer’s [ABI list](https://clang.llvm.org/docs/DataFlowSanitizer.html), e.g. `ANGORA_TAINT_RULE_LIST=~/angora/bin/rules/zlib_abilist.txt`.
11+
- `ANGORA_INST_RATIO`:
1112

1213
# Environment variables for running
1314

fuzzer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ unstable = []
1111
clap = "2.32"
1212
log = "0.4"
1313
pretty_env_logger = "0.3"
14-
rand = "0.6"
14+
rand = "0.7"
1515
libc = "0.2"
1616
wait-timeout = "0.2"
1717
ctrlc = { version = "3.1", features = ["termination"] }

fuzzer/src/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl CommandOpt {
8484
let clang_lib = Command::new("llvm-config")
8585
.arg("--libdir")
8686
.output()
87-
.unwrap()
87+
.expect("Can't find llvm-config")
8888
.stdout;
8989
let clang_lib = String::from_utf8(clang_lib).unwrap();
9090
let ld_library = "$LD_LIBRARY_PATH:".to_string() + clang_lib.trim();

llvm_mode/compiler/angora_clang.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,12 @@ static void edit_params(u32 argc, char **argv) {
378378
break;
379379
case 32:
380380
/* if (access(cc_params[cc_par_cnt - 1], R_OK)) */
381-
FATAL("-m32 is not supported by your compiler");
381+
// FATAL("-m32 is not supported by your compiler");
382382
break;
383383

384384
case 64:
385385
/* if (access(cc_params[cc_par_cnt - 1], R_OK)) */
386-
FATAL("-m64 is not supported by your compiler");
386+
// FATAL("-m64 is not supported by your compiler");
387387
break;
388388
}
389389
}

llvm_mode/libcxx/compile.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env bash
22

3+
BIN_PATH=$(readlink -f "$0")
4+
ROOT_DIR=$(dirname $(dirname $(dirname $BIN_PATH)))
5+
36
LLVM_VERSION=7.0.0
47

58
NINJA_B=`which ninja 2>/dev/null`
@@ -62,7 +65,7 @@ ninja cxx cxxabi
6265
cd ..
6366
mkdir build_track && cd build_track/
6467

65-
CC=~/angora/bin/angora-clang CXX=~/angora/bin/angora-clang++ cmake -G Ninja ../llvm_src -DLIBCXXABI_ENABLE_SHARED=NO -DLIBCXX_ENABLE_SHARED=NO -DLIBCXX_CXX_ABI=libcxxabi
68+
CC=${ROOT_DIR}/bin/angora-clang CXX=${ROOT_DIR}/bin/angora-clang++ cmake -G Ninja ../llvm_src -DLIBCXXABI_ENABLE_SHARED=NO -DLIBCXX_ENABLE_SHARED=NO -DLIBCXX_CXX_ABI=libcxxabi
6669
#-DLLVM_FORCE_USE_OLD_TOOLCHAIN=YES
6770
USE_DFSAN=1 ninja cxx cxxabi
6871

llvm_mode/pass/AngoraPass.cc

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class AngoraLLVMPass : public ModulePass {
6363
u32 CidCounter;
6464
unsigned long int RandSeed = 1;
6565
bool is_bc;
66+
unsigned int inst_ratio = 100;
6667

6768
// Const Variables
6869
DenseSet<u32> UniqCidSet;
@@ -117,6 +118,7 @@ class AngoraLLVMPass : public ModulePass {
117118
bool runOnModule(Module &M) override;
118119
u32 getInstructionId(Instruction *Inst);
119120
u32 getRandomBasicBlockId();
121+
bool skipBasicBlock();
120122
u32 getRandomNum();
121123
void setRandomNumSeed(u32 seed);
122124
u32 getRandomContextId();
@@ -145,6 +147,8 @@ char AngoraLLVMPass::ID = 0;
145147

146148
u32 AngoraLLVMPass::getRandomBasicBlockId() { return random() % MAP_SIZE; }
147149

150+
bool AngoraLLVMPass::skipBasicBlock() { return (random() % 100) >= inst_ratio; }
151+
148152
// http://pubs.opengroup.org/onlinepubs/009695399/functions/rand.html
149153
u32 AngoraLLVMPass::getRandomNum() {
150154
RandSeed = RandSeed * 1103515245 + 12345;
@@ -222,6 +226,14 @@ void AngoraLLVMPass::initVariables(Module &M) {
222226
errs() << "Input is LLVM bitcode\n";
223227
}
224228

229+
char* inst_ratio_str = getenv("ANGORA_INST_RATIO");
230+
if (inst_ratio_str) {
231+
if (sscanf(inst_ratio_str, "%u", &inst_ratio) != 1 || !inst_ratio ||
232+
inst_ratio > 100)
233+
FATAL("Bad value of ANGORA_INST_RATIO (must be between 1 and 100)");
234+
}
235+
errs() << "inst_ratio: " << inst_ratio << "\n";
236+
225237
// set seed
226238
srandom(ModId);
227239
setRandomNumSeed(ModId);
@@ -339,8 +351,8 @@ void AngoraLLVMPass::initVariables(Module &M) {
339351
char* custom_fn_ctx = getenv(CUSTOM_FN_CTX);
340352
if (custom_fn_ctx) {
341353
num_fn_ctx = atoi(custom_fn_ctx);
342-
if (num_fn_ctx < 0 || num_fn_ctx > 32) {
343-
errs() << "custom context should be: >= 0 && <=32 \n";
354+
if (num_fn_ctx < 0 || num_fn_ctx >= 32) {
355+
errs() << "custom context should be: >= 0 && < 32 \n";
344356
exit(1);
345357
}
346358
}
@@ -365,9 +377,9 @@ void AngoraLLVMPass::initVariables(Module &M) {
365377
// Coverage statistics: AFL's Branch count
366378
// Angora enable function-call context.
367379
void AngoraLLVMPass::countEdge(Module &M, BasicBlock &BB) {
368-
if (!FastMode)
380+
if (!FastMode || skipBasicBlock())
369381
return;
370-
382+
371383
// LLVMContext &C = M.getContext();
372384
unsigned int cur_loc = getRandomBasicBlockId();
373385
ConstantInt *CurLoc = ConstantInt::get(Int32Ty, cur_loc);
@@ -394,13 +406,25 @@ void AngoraLLVMPass::countEdge(Module &M, BasicBlock &BB) {
394406
LoadInst *Counter = IRB.CreateLoad(MapPtrIdx);
395407
setInsNonSan(Counter);
396408

397-
// Avoid overflow
398-
Value *CmpOF = IRB.CreateICmpNE(Counter, ConstantInt::get(Int8Ty, -1));
399-
setValueNonSan(CmpOF);
400-
401-
Value *IncVal = IRB.CreateZExt(CmpOF, Int8Ty);
409+
// Implementation of saturating counter.
410+
// Value *CmpOF = IRB.CreateICmpNE(Counter, ConstantInt::get(Int8Ty, -1));
411+
// setValueNonSan(CmpOF);
412+
// Value *IncVal = IRB.CreateZExt(CmpOF, Int8Ty);
413+
// setValueNonSan(IncVal);
414+
// Value *IncRet = IRB.CreateAdd(Counter, IncVal);
415+
// setValueNonSan(IncRet);
416+
417+
// Implementation of Never-zero counter
418+
// The idea is from Marc and Heiko in AFLPlusPlus
419+
// Reference: : https://github.com/vanhauser-thc/AFLplusplus/blob/master/llvm_mode/README.neverzero and https://github.com/vanhauser-thc/AFLplusplus/issues/10
420+
421+
Value *IncRet = IRB.CreateAdd(Counter, ConstantInt::get(Int8Ty, 1));
422+
setValueNonSan(IncRet);
423+
Value *IsZero = IRB.CreateICmpEQ(IncRet, ConstantInt::get(Int8Ty, 0));
424+
setValueNonSan(IsZero);
425+
Value *IncVal = IRB.CreateZExt(IsZero, Int8Ty);
402426
setValueNonSan(IncVal);
403-
Value *IncRet = IRB.CreateAdd(Counter, IncVal);
427+
IncRet = IRB.CreateAdd(IncRet, IncVal);
404428
setValueNonSan(IncRet);
405429

406430
// Store Back Map[idx]
@@ -607,10 +631,12 @@ void AngoraLLVMPass::processCmp(Instruction *Cond, Constant *Cid,
607631
OpArg[1] = castArgType(IRB, OpArg[1]);
608632
Value *CondExt = IRB.CreateZExt(Cond, Int32Ty);
609633
setValueNonSan(CondExt);
634+
LoadInst *CurCtx = IRB.CreateLoad(AngoraContext);
635+
setInsNonSan(CurCtx);
610636
CallInst *ProxyCall =
611-
IRB.CreateCall(TraceCmp, {CondExt, Cid, OpArg[0], OpArg[1]});
637+
IRB.CreateCall(TraceCmp, {CondExt, Cid, CurCtx, OpArg[0], OpArg[1]});
612638
setInsNonSan(ProxyCall);
613-
*/
639+
*/
614640
LoadInst *CurCid = IRB.CreateLoad(AngoraCondId);
615641
setInsNonSan(CurCid);
616642
Value *CmpEq = IRB.CreateICmpEQ(Cid, CurCid);
@@ -841,7 +867,7 @@ bool AngoraLLVMPass::runOnModule(Module &M) {
841867
return true;
842868

843869
for (auto &F : M) {
844-
if (F.isDeclaration())
870+
if (F.isDeclaration() || F.getName().startswith(StringRef("asan.module")))
845871
continue;
846872

847873
addFnWrap(F);

0 commit comments

Comments
 (0)