Skip to content

Commit d6b2ce0

Browse files
authored
Custom function call context (#55)
* improve custom function context * minor fix doc * minor fix * typo
1 parent b2e0929 commit d6b2ce0

File tree

6 files changed

+36
-28
lines changed

6 files changed

+36
-28
lines changed

docs/environment_variables.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
- `USE_FAST=1`: use fast mode to compile the program. It includes branch counting, getting the feedback of the fuzzing constraint (the output of its function).
44
- `USE_TRACK=1`: use taint tracking and collect all constraints.
55
- `USE_DFSAN=1`: use taint tracking.
6-
- `ANGORA_DISABLE_CONTEXT=1` : Disable function call based contexts in compiling.
7-
- `ANGORA_DIRECT_FN_CONTEXT=1` : Use only the last function call location as the context.
6+
- `ANGORA_CUSTOM_FN_CONTEXT=k` : Use only the last k ( 0 <= k <= 32) function call location as the context, e.g. `ANGORA_CUSTOM_FN_CONTEXT=8`. Angora disables context if k is 0.
87
- `ANGORA_GEN_ID_RANDOM=1` : Generate ids for predicates randomly instead of the hash of their locations.
98
- `ANGORA_OUTPUT_COND_LOC=1` : (Debug option) Output the location of each predicate during compiling.
109
- `ANGORA_TAINT_CUSTOM_RULE=/path/to/object` : object contains those proxy function (how to propagate taints), e.g. `ANGORA_TAINT_CUSTOM_RULE=~/angora/bin/zlib-func.o` . You should add it as custom type in the file passed by `ANGORA_TAINT_RULE_LIST` first.

fuzzer/src/stats/chart.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl ChartStats {
126126
impl fmt::Display for ChartStats {
127127
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
128128
if self.density.0 > 10.0 {
129-
warn!("Density is too large (> 10%). Please increase `MAP_SIZE_POW2` in `llvm_mode/config.h` and `MAP_LENGTH` in `common/src/config.rs`. Or disable function-call context by compiling with `ANGORA_DISABLE_CONTEXT=1` or `ANGORA_DIRECT_FN_CONTEXT=1` environment variable.");
129+
warn!("Density is too large (> 10%). Please increase `MAP_SIZE_POW2` in `llvm_mode/config.h` and `MAP_LENGTH` in `common/src/config.rs`. Or disable function-call context(density > 50%) by compiling with `ANGORA_CUSTOM_FN_CONTEXT=k` (k is an integer and 0 <= k <= 32) environment variable. Angora disables context if k is 0.");
130130
}
131131

132132
if self.search.multiple_inconsist() {

llvm_mode/angora-clang.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,13 @@ int main(int argc, char **argv) {
431431
find_obj(argv[0]);
432432

433433
edit_params(argc, argv);
434-
434+
/*
435435
for (int i = 0; i < cc_par_cnt; i++) {
436436
printf("%s ", cc_params[i]);
437437
}
438438
printf("\n");
439-
439+
*/
440+
440441
execvp(cc_params[0], (char **)cc_params);
441442

442443
FATAL("Oops, failed to execute '%s' - check your PATH", cc_params[0]);

llvm_mode/angora-llvm-pass.so.cc

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ class AngoraLLVMPass : public ModulePass {
6868
DenseSet<u32> UniqCidSet;
6969

7070
// Configurations
71-
bool enable_ctx;
7271
bool gen_id_random;
7372
bool output_cond_loc;
74-
bool direct_fn_ctx;
73+
int num_fn_ctx;
7574

7675
MDNode *ColdCallWeights;
7776

@@ -333,17 +332,25 @@ void AngoraLLVMPass::initVariables(Module &M) {
333332
ClExploitListFiles.end());
334333
ExploitList.set(SpecialCaseList::createOrDie(AllExploitListFiles));
335334

336-
enable_ctx = !getenv(DISABLE_CTX_VAR);
337-
direct_fn_ctx = !!getenv(DIRECT_FN_CTX);
338335
gen_id_random = !!getenv(GEN_ID_RANDOM_VAR);
339336
output_cond_loc = !!getenv(OUTPUT_COND_LOC_VAR);
340337

341-
if (!enable_ctx) {
342-
errs() << "disable context\n";
338+
num_fn_ctx = -1;
339+
char* custom_fn_ctx = getenv(CUSTOM_FN_CTX);
340+
if (custom_fn_ctx) {
341+
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";
344+
exit(1);
345+
}
343346
}
344347

345-
if (direct_fn_ctx) {
346-
errs() << "use direct function call context\n";
348+
if (num_fn_ctx == 0) {
349+
errs() << "disable context\n";
350+
}
351+
352+
if (num_fn_ctx > 0) {
353+
errs() << "use custom function call context: " << num_fn_ctx << "\n";
347354
}
348355

349356
if (gen_id_random) {
@@ -400,7 +407,7 @@ void AngoraLLVMPass::countEdge(Module &M, BasicBlock &BB) {
400407
IRB.CreateStore(IncRet, MapPtrIdx)->setMetadata(NoSanMetaId, NoneMetaNode);
401408

402409
Value *NewPrevLoc = NULL;
403-
if (enable_ctx) { // Call-based context
410+
if (num_fn_ctx != 0) { // Call-based context
404411
// Load ctx
405412
LoadInst *CtxVal = IRB.CreateLoad(AngoraContext);
406413
setInsNonSan(CtxVal);
@@ -410,7 +417,7 @@ void AngoraLLVMPass::countEdge(Module &M, BasicBlock &BB) {
410417
// Udate PrevLoc
411418
NewPrevLoc =
412419
IRB.CreateXor(CtxValCasted, ConstantInt::get(Int32Ty, cur_loc >> 1));
413-
} else {
420+
} else { // disable context
414421
NewPrevLoc = ConstantInt::get(Int32Ty, cur_loc >> 1);
415422
}
416423
setValueNonSan(NewPrevLoc);
@@ -421,6 +428,9 @@ void AngoraLLVMPass::countEdge(Module &M, BasicBlock &BB) {
421428

422429

423430
void AngoraLLVMPass::addFnWrap(Function &F) {
431+
432+
if (num_fn_ctx == 0) return;
433+
424434
// *** Pre Fn ***
425435
BasicBlock *BB = &F.getEntryBlock();
426436
Instruction *InsertPoint = &(*(BB->getFirstInsertionPt()));
@@ -439,8 +449,8 @@ void AngoraLLVMPass::addFnWrap(Function &F) {
439449
// by `xor` with the same value
440450
// Implementation of function context for AFL by heiko eissfeldt:
441451
// https://github.com/vanhauser-thc/afl-patches/blob/master/afl-fuzz-context_sensitive.diff
442-
if (direct_fn_ctx) {
443-
OriCtxVal = IRB.CreateLShr(OriCtxVal, 6);
452+
if (num_fn_ctx > 0) {
453+
OriCtxVal = IRB.CreateLShr(OriCtxVal, 32 / num_fn_ctx);
444454
setValueNonSan(OriCtxVal);
445455
}
446456

@@ -471,11 +481,11 @@ void AngoraLLVMPass::processCall(Instruction *Inst) {
471481

472482
// if (ABIList.isIn(*Callee, "uninstrumented"))
473483
// return;
474-
475-
IRBuilder<> IRB(Inst);
476-
Constant* CallSite = ConstantInt::get(Int32Ty, getRandomContextId());
477-
IRB.CreateStore(CallSite, AngoraCallSite)->setMetadata(NoSanMetaId, NoneMetaNode);
478-
484+
if (num_fn_ctx != 0) {
485+
IRBuilder<> IRB(Inst);
486+
Constant* CallSite = ConstantInt::get(Int32Ty, getRandomContextId());
487+
IRB.CreateStore(CallSite, AngoraCallSite)->setMetadata(NoSanMetaId, NoneMetaNode);
488+
}
479489
}
480490

481491
void AngoraLLVMPass::visitCallInst(Instruction *Inst) {

llvm_mode/config.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
#define MAP_SIZE_POW2 20
55
#define MAP_SIZE (1 << MAP_SIZE_POW2)
6-
#define MAX_FUNCALL_LEVEL 25
76
#define ENABLE_UNFOLD_BRANCH 1
87

9-
#define VERSION "1.10"
8+
#define VERSION "1.2.0"
109

1110
// Without taint tracking
1211
#define CLANG_FAST_TYPE 0
@@ -32,10 +31,7 @@
3231
} while (0)
3332
#endif
3433

35-
#define SHM_ENV_VAR "ANGORA_BRANCHES_SHM_ID"
36-
#define ENABLE_FORKSRV "ANGORA_ENABLE_FORKSRV"
37-
#define DISABLE_CTX_VAR "ANGORA_DISABLE_CONTEXT"
38-
#define DIRECT_FN_CTX "ANGORA_DIRECT_FN_CONTEXT"
34+
#define CUSTOM_FN_CTX "ANGORA_CUSTOM_FN_CONTEXT"
3935
#define GEN_ID_RANDOM_VAR "ANGORA_GEN_ID_RANDOM"
4036
#define OUTPUT_COND_LOC_VAR "ANGORA_OUTPUT_COND_LOC"
4137
#define TAINT_CUSTOM_RULE_VAR "ANGORA_TAINT_CUSTOM_RULE"

tests/test.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ target=${name}/${name}
4141

4242
rm -f ${target}.fast ${target}.cmp ${target}.taint
4343

44+
# export ANGORA_CUSTOM_FN_CONTEXT=0
45+
4446
bin_dir=../bin/
4547
ANGORA_USE_ASAN=1 USE_FAST=1 ${bin_dir}/angora-clang ${target}.c -lz -o ${target}.fast
4648
USE_TRACK=1 ${bin_dir}/angora-clang ${target}.c -lz -o ${target}.taint

0 commit comments

Comments
 (0)