Skip to content

Commit 34a940f

Browse files
committed
cranelift: Revert back to fuzzing udivi64
1 parent 32cc761 commit 34a940f

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

cranelift/fuzzgen/src/function_generator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,9 +833,9 @@ where
833833
let signature = self.generate_signature()?;
834834
(name, signature)
835835
} else {
836-
// Use ishli64 as an example of a libcall function.
836+
// Use udivi64 as an example of a libcall function.
837837
// TODO: Expand this to more libcall's
838-
let libcall = LibCall::IshlI64;
838+
let libcall = LibCall::UdivI64;
839839
let signature = libcall.signature(CallConv::Fast);
840840
(ExternalName::LibCall(libcall), signature)
841841
};

cranelift/interpreter/src/interpreter.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use cranelift_codegen::data_value::DataValue;
1313
use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
1414
use cranelift_codegen::ir::{
1515
ArgumentPurpose, Block, FuncRef, Function, GlobalValue, GlobalValueData, Heap, LibCall,
16-
StackSlot, Type, Value as ValueRef,
16+
StackSlot, TrapCode, Type, Value as ValueRef,
1717
};
1818
use log::trace;
1919
use smallvec::SmallVec;
@@ -192,7 +192,7 @@ pub enum HeapInit {
192192
FromBacking(HeapBacking),
193193
}
194194

195-
pub type LibCallHandler<'a, V> = &'a dyn Fn(SmallVec<[V; 1]>) -> SmallVec<[V; 1]>;
195+
pub type LibCallHandler<'a, V> = &'a dyn Fn(SmallVec<[V; 1]>) -> Result<SmallVec<[V; 1]>, TrapCode>;
196196

197197
/// Maintains the [Interpreter]'s state, implementing the [State] trait.
198198
pub struct InterpreterState<'a> {
@@ -1083,10 +1083,10 @@ mod tests {
10831083
let state = InterpreterState::default()
10841084
.with_function_store(env)
10851085
.with_libcall(LibCall::UdivI64, &|args| {
1086-
smallvec![match &args[..] {
1086+
Ok(smallvec![match &args[..] {
10871087
[DataValue::I64(a), DataValue::I64(b)] => DataValue::I64(a / b),
10881088
_ => panic!("Unexpected args"),
1089-
}]
1089+
}])
10901090
});
10911091

10921092
let result = Interpreter::new(state)

cranelift/interpreter/src/step.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,10 @@ where
349349

350350
// We don't transfer control to a libcall, we just execute it and return the results
351351
let res = handler(args);
352+
let res = match res {
353+
Err(trap) => return Ok(ControlFlow::Trap(CraneliftTrap::User(trap))),
354+
Ok(rets) => rets,
355+
};
352356

353357
// Check that what the handler returned is what we expect.
354358
let rets_diff = res

fuzz/fuzz_targets/cranelift-fuzzgen.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use libfuzzer_sys::fuzz_target;
44

55
use cranelift_codegen::data_value::DataValue;
6-
use cranelift_codegen::ir::LibCall;
6+
use cranelift_codegen::ir::{LibCall, TrapCode};
77
use cranelift_codegen::settings;
88
use cranelift_codegen::settings::Configurable;
99
use cranelift_filetests::function_runner::{CompiledFunction, SingleFunctionCompiler};
@@ -61,9 +61,11 @@ fuzz_target!(|testcase: TestCase| {
6161

6262
let state = InterpreterState::default()
6363
.with_function_store(env)
64-
.with_libcall(LibCall::IshlI64, &|args| match &args[..] {
65-
[DataValue::I64(_), DataValue::I64(b)] if *b > 63 => smallvec![DataValue::I64(0)],
66-
[DataValue::I64(a), DataValue::I64(b)] => smallvec![DataValue::I64(a.shl(b))],
64+
.with_libcall(LibCall::UdivI64, &|args| match &args[..] {
65+
[DataValue::I64(a), DataValue::I64(b)] => a
66+
.checked_div(b)
67+
.map(|res| Ok(smallvec![DataValue::I64(res)]))
68+
.unwrap_or(Err(TrapCode::IntegerDivisionByZero)),
6769
_ => unreachable!(),
6870
});
6971
let interpreter = Interpreter::new(state).with_fuel(Some(INTERPRETER_FUEL));

0 commit comments

Comments
 (0)