Skip to content

Commit fd62923

Browse files
authored
Make cmplog exported functions more consistent (#3297)
* Make cmplog export functions more consistent * Fix features dependency issue * Fix features dependency issue * Rename checked extended
1 parent 00e494b commit fd62923

File tree

5 files changed

+60
-17
lines changed

5 files changed

+60
-17
lines changed

libafl/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ ahash = { workspace = true } # The hash function already used in hashbrown
239239
meminterval = { workspace = true, features = ["serde"] }
240240
backtrace = { workspace = true, optional = true } # Used to get the stacktrace in StacktraceObserver
241241
typed-builder = { workspace = true, optional = true } # Implement the builder pattern at compiletime
242-
send_wrapper = { version="0.6.0", optional = true } # To move data between threads
242+
send_wrapper = { version = "0.6.0", optional = true } # To move data between threads
243243

244244
serde_json = { workspace = true, optional = true, default-features = false, features = [
245245
"alloc",

libafl_targets/src/cmplog.c

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void __libafl_targets_cmplog_instructions(uintptr_t k, uint8_t shape,
105105
// Very generic afl++ style cmplog instructions callback
106106
void __libafl_targets_cmplog_instructions_extended(uintptr_t k, uint8_t shape,
107107
uint64_t arg1, uint64_t arg2) {
108-
cmplog_instructions_extended_checked(k, shape, arg1, arg2, 0);
108+
cmplog_instructions_checked_extended(k, shape, arg1, arg2, 0);
109109
}
110110

111111
// Very generic cmplog routines callback
@@ -133,8 +133,43 @@ void __libafl_targets_cmplog_routines_len(uintptr_t k, const uint8_t *ptr1,
133133
return;
134134
}
135135

136+
if (len >= CMPLOG_RTN_LEN) {
137+
len = CMPLOG_RTN_LEN - 1;
138+
}
139+
136140
cmplog_routines_checked(k, ptr1, ptr2, len);
137141
}
142+
143+
void __libafl_targets_cmplog_routines_extended(uintptr_t k, const uint8_t *ptr1,
144+
const uint8_t *ptr2) {
145+
if (!libafl_cmplog_enabled) { return; }
146+
147+
int l1, l2;
148+
if ((l1 = area_is_valid(ptr1, CMPLOG_RTN_LEN)) <= 0 ||
149+
(l2 = area_is_valid(ptr2, CMPLOG_RTN_LEN)) <= 0) {
150+
return;
151+
}
152+
int len = MIN(l1, l2);
153+
154+
cmplog_routines_checked_extended(k, ptr1, ptr2, len);
155+
}
156+
157+
void __libafl_targets_cmplog_routines_extended_len(uintptr_t k, const uint8_t *ptr1,
158+
const uint8_t *ptr2, size_t len) {
159+
if (!libafl_cmplog_enabled) { return; }
160+
161+
if ((area_is_valid(ptr1, CMPLOG_RTN_LEN)) <= 0 ||
162+
(area_is_valid(ptr2, CMPLOG_RTN_LEN)) <= 0) {
163+
return;
164+
}
165+
166+
if (len >= CMPLOG_RTN_LEN) {
167+
len = CMPLOG_RTN_LEN - 1;
168+
}
169+
170+
cmplog_routines_checked_extended(k, ptr1, ptr2, len);
171+
}
172+
138173
/*
139174
CMPLOG Callback for instructions
140175
*/
@@ -144,7 +179,7 @@ void __cmplog_ins_hook1_extended(uint8_t arg1, uint8_t arg2, uint8_t attr) {
144179
k = (k >> 4) ^ (k << 8);
145180
k &= CMPLOG_MAP_W - 1;
146181

147-
cmplog_instructions_extended_checked(k, 0, arg1, arg2, attr);
182+
cmplog_instructions_checked_extended(k, 0, arg1, arg2, attr);
148183
}
149184
void __cmplog_ins_hook1(uint8_t arg1, uint8_t arg2) {
150185
uintptr_t k = RETADDR;
@@ -159,7 +194,7 @@ void __cmplog_ins_hook2_extended(uint16_t arg1, uint16_t arg2, uint8_t attr) {
159194
k = (k >> 4) ^ (k << 8);
160195
k &= CMPLOG_MAP_W - 1;
161196

162-
cmplog_instructions_extended_checked(k, 1, arg1, arg2, attr);
197+
cmplog_instructions_checked_extended(k, 1, arg1, arg2, attr);
163198
}
164199
void __cmplog_ins_hook2(uint16_t arg1, uint16_t arg2) {
165200
uintptr_t k = RETADDR;
@@ -174,7 +209,7 @@ void __cmplog_ins_hook4_extended(uint32_t arg1, uint32_t arg2, uint8_t attr) {
174209
k = (k >> 4) ^ (k << 8);
175210
k &= CMPLOG_MAP_W - 1;
176211

177-
cmplog_instructions_extended_checked(k, 3, arg1, arg2, attr);
212+
cmplog_instructions_checked_extended(k, 3, arg1, arg2, attr);
178213
}
179214
void __cmplog_ins_hook4(uint32_t arg1, uint32_t arg2) {
180215
uintptr_t k = RETADDR;
@@ -189,7 +224,7 @@ void __cmplog_ins_hook8_extended(uint64_t arg1, uint64_t arg2, uint8_t attr) {
189224
k = (k >> 4) ^ (k << 8);
190225
k &= CMPLOG_MAP_W - 1;
191226

192-
cmplog_instructions_extended_checked(k, 7, arg1, arg2, attr);
227+
cmplog_instructions_checked_extended(k, 7, arg1, arg2, attr);
193228
}
194229
void __cmplog_ins_hook8(uint64_t arg1, uint64_t arg2) {
195230
uintptr_t k = RETADDR;
@@ -206,7 +241,7 @@ void __cmplog_ins_hook16_extended(uint128_t arg1, uint128_t arg2,
206241
k = (k >> 4) ^ (k << 8);
207242
k &= CMPLOG_MAP_W - 1;
208243

209-
cmplog_instructions_extended_checked(k, 15, arg1, arg2, attr);
244+
cmplog_instructions_checked_extended(k, 15, arg1, arg2, attr);
210245
}
211246
void __cmplog_ins_hook16(uint128_t arg1, uint128_t arg2) {
212247
uintptr_t k = RETADDR;
@@ -222,7 +257,7 @@ void __cmplog_ins_hookN_extended(uint128_t arg1, uint128_t arg2, uint8_t attr,
222257
k = (k >> 4) ^ (k << 8);
223258
k &= CMPLOG_MAP_W - 1;
224259

225-
cmplog_instructions_extended_checked(k, size - 1, arg1, arg2, attr);
260+
cmplog_instructions_checked_extended(k, size - 1, arg1, arg2, attr);
226261
}
227262
void __cmplog_ins_hookN(uint128_t arg1, uint128_t arg2, uint8_t size) {
228263
uintptr_t k = RETADDR;

libafl_targets/src/cmplog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ extern uint8_t libafl_cmplog_enabled;
102102

103103
// 5 of CMPLOG inner APIs, we static inline everything
104104
// area_is_valid, cmplog_instructions_checked,
105-
// cmplog_instructions_extended_checked,
105+
// cmplog_instructions_checked_extended,
106106
// cmplog_routines_checked,
107107
// cmplog_routines_checked_extended
108108

@@ -132,7 +132,7 @@ static inline void cmplog_instructions_checked(uintptr_t k, uint8_t shape,
132132
libafl_cmplog_enabled = true;
133133
}
134134

135-
static inline void cmplog_instructions_extended_checked(
135+
static inline void cmplog_instructions_checked_extended(
136136
uintptr_t k, uint8_t shape, uint64_t arg1, uint64_t arg2, uint8_t attr) {
137137
#ifdef CMPLOG_EXTENDED
138138
if (!libafl_cmplog_enabled) { return; }

libafl_targets/src/cmps/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub const CMPLOG_KIND_RTN: u8 = 1;
4545

4646
// EXTERNS, GLOBALS
4747

48-
#[cfg(feature = "cmplog")]
48+
#[cfg(any(feature = "cmplog", feature = "sancov_cmplog", feature = "sancov_value_profile"))]
4949
// void __libafl_targets_cmplog_instructions(uintptr_t k, uint8_t shape, uint64_t arg1, uint64_t arg2)
5050
unsafe extern "C" {
5151
/// Logs an instruction for feedback during fuzzing
@@ -57,13 +57,24 @@ unsafe extern "C" {
5757
/// Logs a routine for feedback during fuzzing
5858
pub fn __libafl_targets_cmplog_routines(k: usize, ptr1: *const u8, ptr2: *const u8);
5959

60+
/// Cmplog routines but with len specified.
61+
pub fn __libafl_targets_cmplog_routines_len(k: usize, ptr1: *const u8, ptr2: *const u8, len: usize);
62+
6063
/// Pointer to the `CmpLog` map
6164
pub static mut libafl_cmplog_map_ptr: *mut CmpLogMap;
6265

6366
/// Pointer to the extended `CmpLog` map
6467
pub static mut libafl_cmplog_map_extended_ptr: *mut CmpLogMap;
6568
}
6669

70+
#[cfg(feature = "cmplog_extended_instrumentation")]
71+
unsafe extern "C" {
72+
/// Logs an AFL++ style routine for feedback during fuzzing
73+
pub fn __libafl_targets_cmplog_routines_extended(k: usize, ptr1: *const u8, ptr2: *const u8);
74+
/// Extended cmplog routines but with len specified.
75+
pub fn __libafl_targets_cmplog_routines_extended_len(k: usize, ptr1: *const u8, ptr2: *const u8, len: usize);
76+
}
77+
6778
#[cfg(feature = "cmplog_extended_instrumentation")]
6879
pub use libafl_cmplog_map_extended_ptr as EXTENDED_CMPLOG_MAP_PTR;
6980
#[cfg(feature = "cmplog")]

libafl_targets/src/sancov_cmp.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ unsafe extern "C" {
3030

3131
/// Trace a switch statement
3232
pub fn __sanitizer_cov_trace_switch(val: u64, cases: *const u64);
33-
34-
/// cmplog internal api
35-
pub fn __libafl_targets_cmplog_routines_len(k: usize, s1: *const u8, s2: *const u8, len: usize);
3633
}
3734

3835
/// overriding `__sanitizer_weak_hook_memcmp`
@@ -51,7 +48,7 @@ pub unsafe extern "C" fn __sanitizer_weak_hook_memcmp(
5148
let k: usize = called_pc as usize;
5249
let k = (k >> 4) ^ (k << 8);
5350
let k = k & (CMPLOG_MAP_W - 1);
54-
__libafl_targets_cmplog_routines_len(
51+
crate::__libafl_targets_cmplog_routines_len(
5552
k,
5653
s1 as *const u8,
5754
s2 as *const u8,
@@ -89,7 +86,7 @@ pub unsafe extern "C" fn __sanitizer_weak_hook_strncmp(
8986
}
9087
actual_len += 1;
9188
}
92-
__libafl_targets_cmplog_routines_len(k, s1 as *const u8, s2 as *const u8, actual_len);
89+
crate::__libafl_targets_cmplog_routines_len(k, s1 as *const u8, s2 as *const u8, actual_len);
9390
}
9491
}
9592
}
@@ -135,7 +132,7 @@ pub unsafe extern "C" fn __sanitizer_weak_hook_strcmp(
135132
}
136133
actual_len += 1;
137134
}
138-
__libafl_targets_cmplog_routines_len(k, s1 as *const u8, s2 as *const u8, actual_len);
135+
crate::__libafl_targets_cmplog_routines_len(k, s1 as *const u8, s2 as *const u8, actual_len);
139136
}
140137
}
141138
}

0 commit comments

Comments
 (0)