Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Fuzzer to Check Debug Locations #200

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/fastalloc_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ fuzz_target!(|testcase: TestCase| {

let mut checker = Checker::new(&func, &env);
checker.prepare(&out);
checker.init_debug_locations(&out);
checker.run().expect("checker failed");
});
1 change: 1 addition & 0 deletions fuzz/fuzz_targets/ion_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ fuzz_target!(|testcase: TestCase| {

let mut checker = Checker::new(&func, &env);
checker.prepare(&ctx.borrow().output);
checker.init_debug_locations(&ctx.borrow().output);
checker.run().expect("checker failed");
});
});
168 changes: 164 additions & 4 deletions src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
use crate::{
Allocation, AllocationKind, Block, Edit, Function, FxHashMap, FxHashSet, Inst, InstOrEdit,
InstPosition, MachineEnv, Operand, OperandConstraint, OperandKind, OperandPos, Output, PReg,
PRegSet, VReg,
PRegSet, ProgPoint, VReg,
};
use alloc::vec::Vec;
use alloc::{format, vec};
Expand All @@ -110,11 +110,11 @@ use smallvec::{smallvec, SmallVec};
/// A set of errors detected by the regalloc checker.
#[derive(Clone, Debug)]
pub struct CheckerErrors {
errors: Vec<CheckerError>,
pub errors: Vec<CheckerError>,
}

/// A single error detected by the regalloc checker.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub enum CheckerError {
MissingAllocation {
inst: Inst,
Expand Down Expand Up @@ -166,6 +166,13 @@ pub enum CheckerError {
into: Allocation,
from: Allocation,
},
ExpectedValueForDebug {
point: ProgPoint,
alloc: Allocation,
vreg: VReg,
found: CheckerValue,
label: u32,
},
}

/// Abstract state for an allocation.
Expand All @@ -174,7 +181,7 @@ pub enum CheckerError {
/// universe-set as top and empty set as bottom lattice element. The
/// meet-function is thus set intersection.
#[derive(Clone, Debug, PartialEq, Eq)]
enum CheckerValue {
pub enum CheckerValue {
/// The lattice top-value: this value could be equivalent to any
/// vreg (i.e., the universe set).
Universe,
Expand Down Expand Up @@ -690,6 +697,128 @@ pub(crate) enum CheckerInst {
},
}

#[derive(Debug)]
struct DebugLocationEntry {
vreg: VReg,
alloc: Allocation,
label: u32,
}

#[derive(Debug)]
struct DebugLocations {
expected_vreg_locations: FxHashMap<(VReg, (ProgPoint, ProgPoint), u32), Allocation>,
}

impl DebugLocations {
fn ranges_overlaps(
(start_point, end_point): (ProgPoint, ProgPoint),
(start_inst, end_inst): (Inst, Inst),
) -> Option<(ProgPoint, ProgPoint)> {
if end_inst <= start_point.inst() || start_inst >= end_point.inst() {
None
} else {
let point0 = if start_point.inst() >= start_inst {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar here to below -- std::cmp::min / std::cmp::max?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And can we name this intersects, and factor it out to a common method on ProgPoint shared by the other use below? I see the args are slightly different -- ProgPoint/Inst here, Inst/ProgPoint below, but unless there are subtleties around the handling of begin/end, we could take ProgPoints for both ranges and convert Insts to ProgPoints as needed at callsites I think?

start_point
} else {
ProgPoint::before(start_inst)
};
let point1 = if end_point.inst() < end_inst {
end_point
} else {
ProgPoint::before(end_inst)
};
Some((point0, point1))
}
}

fn new<F: Function>(f: &F, output: &Output) -> Self {
let mut expected_vreg_locations = FxHashMap::default();
for (label, start_point, end_point, alloc) in &output.debug_locations {
for (vreg, start_inst, end_inst, in_label) in f.debug_value_labels() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried that this could result in quadratic runtime during fuzzing (and that the fuzzer will discover and exercise this worst case) -- could we optimize this somehow by building a map? For example we could have a label -> [array of allocs] dense-map, where [array of allocs] is indexed by the ProgPoint's index; then when processing debug_value_labels we can cross-reference that.

if in_label != label {
continue;
}
if let Some(range) =
Self::ranges_overlaps((*start_point, *end_point), (*start_inst, *end_inst))
{
expected_vreg_locations.insert((*vreg, range, *label), *alloc);
}
}
}
Self {
expected_vreg_locations,
}
}

fn points_covers_inst(
&self,
inst: Inst,
start_point: ProgPoint,
end_point: ProgPoint,
) -> (bool, bool) {
let start_inst = start_point.inst();
let end_inst = end_point.inst();
if inst > start_inst && inst < end_inst {
return (true, true);
}
if inst == start_inst && start_point.pos() == InstPosition::Before {
return (true, true);
}
// Don't check for the case where inst == start and pos == after
// because it may be edit instructions after inst that are responsible
// for moving the vreg into the expected allocation.
if inst == end_inst && end_point.pos() == InstPosition::After {
return (true, false);
}
(false, false)
}

fn entries_covering(&self, inst: Inst) -> Vec<(bool, bool, DebugLocationEntry)> {
let mut entries = vec![];
for entry in self.expected_vreg_locations.keys().copied() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is another place I'm a bit worried about checker-time blowup: we're iterating over all vreg locations (which is O(|num vregs| * |program size|)) once per inst, so if |vregs| ~= |insts|, this is approximately cubic overall, right?

let (vreg, (start_point, end_point), label) = entry;
let (before, after) = self.points_covers_inst(inst, start_point, end_point);
if before || after {
entries.push((
before,
after,
DebugLocationEntry {
vreg,
alloc: self.expected_vreg_locations[&entry],
label,
},
));
}
}
entries
}

fn check_locations_covering_inst(
&self,
inst: Inst,
pos: InstPosition,
state: &CheckerState,
errors: &mut Vec<CheckerError>,
) {
for (before, after, entry) in self.entries_covering(inst) {
if before && pos == InstPosition::Before || pos == InstPosition::After && after {
let default_val = Default::default();
let val = state.get_value(&entry.alloc).unwrap_or(&default_val);
match val {
CheckerValue::VRegs(vregs) if vregs.contains(&entry.vreg) => (),
_ => errors.push(CheckerError::ExpectedValueForDebug {
point: ProgPoint::new(inst, pos),
alloc: entry.alloc,
vreg: entry.vreg,
found: val.clone(),
label: entry.label,
}),
};
}
}
}
}

#[derive(Debug)]
pub struct Checker<'a, F: Function> {
f: &'a F,
Expand All @@ -698,6 +827,7 @@ pub struct Checker<'a, F: Function> {
edge_insts: FxHashMap<(Block, Block), Vec<CheckerInst>>,
machine_env: &'a MachineEnv,
stack_pregs: PRegSet,
debug_locations: Option<DebugLocations>,
}

impl<'a, F: Function> Checker<'a, F> {
Expand Down Expand Up @@ -733,6 +863,7 @@ impl<'a, F: Function> Checker<'a, F> {
edge_insts,
machine_env,
stack_pregs,
debug_locations: None,
}
}

Expand All @@ -756,6 +887,10 @@ impl<'a, F: Function> Checker<'a, F> {
}
}

pub fn init_debug_locations(&mut self, out: &Output) {
self.debug_locations = Some(DebugLocations::new(self.f, out));
}

/// For each original instruction, create an `Op`.
fn handle_inst(&mut self, block: Block, inst: Inst, out: &Output) {
// Skip normal checks if this is a branch: the blockparams do
Expand Down Expand Up @@ -888,6 +1023,21 @@ impl<'a, F: Function> Checker<'a, F> {
for (block, input) in &self.bb_in {
let mut state = input.clone();
for inst in self.bb_insts.get(block).unwrap() {
let orig_inst = match inst {
CheckerInst::Op { inst, .. } => Some(inst),
_ => None,
};

if let (Some(debug_locations), Some(orig_inst)) = (&self.debug_locations, orig_inst)
{
debug_locations.check_locations_covering_inst(
*orig_inst,
InstPosition::Before,
&state,
&mut errors,
);
}

if let Err(e) = state.check(InstPosition::Before, inst, self) {
trace!("Checker error: {:?}", e);
errors.push(e);
Expand All @@ -897,6 +1047,16 @@ impl<'a, F: Function> Checker<'a, F> {
trace!("Checker error: {:?}", e);
errors.push(e);
}

if let (Some(debug_locations), Some(orig_inst)) = (&self.debug_locations, orig_inst)
{
debug_locations.check_locations_covering_inst(
*orig_inst,
InstPosition::After,
&state,
&mut errors,
);
}
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/fastalloc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,11 +1209,35 @@ impl<'a, F: Function> Env<'a, F> {
Ok(())
}

fn point_range_intersect(
&self,
(start_inst, end_inst): (Inst, Inst),
(point_start, point_end): (ProgPoint, ProgPoint),
) -> Option<(ProgPoint, ProgPoint)> {
if end_inst <= point_start.inst() || start_inst >= point_end.inst() {
None
} else {
let point0 = if point_start.inst() >= start_inst {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ProgPoint implements Ord, so could we use std::cmp::min / std::cmp::max here?

point_start
} else {
ProgPoint::new(start_inst, InstPosition::Before)
};
let point1 = if point_end.inst() < end_inst {
point_end
} else {
ProgPoint::new(end_inst, InstPosition::Before)
};
Some((point0, point1))
}
}

fn build_debug_info(&mut self) {
trace!("Building debug location info");
for &(vreg, start, end, label) in self.func.debug_value_labels() {
let (point_start, point_end, alloc) = self.vreg_to_live_inst_range[vreg.vreg()];
if point_start.inst() <= start && end <= point_end.inst().next() {
if let Some((point_start, point_end)) =
self.point_range_intersect((start, end), (point_start, point_end))
{
self.debug_locations
.push((label, point_start, point_end, alloc));
}
Expand Down
Loading
Loading