Skip to main content

forward_slice

Function forward_slice 

Source
pub fn forward_slice(
    graph: &SqliteGraph,
    cdg_result: &ControlDependenceResult,
    source: i64,
) -> Result<SliceResult, SqliteGraphError>
Expand description

Computes forward program slice: “what does this node affect?”

Returns all nodes influenced by the source node. Combines control dependence (what branches does this control?) and forward reachability (where does data flow from here?).

The slice is computed as:

  • Control: Follow CDG edges forward (what does source control?)
  • Data: Follow forward reachability (what can source reach?)
  • Union: Control nodes + Data nodes = complete forward slice

§Arguments

  • graph - The control flow graph to analyze
  • cdg_result - Pre-computed control dependence result (from control_dependence_graph)
  • source - The source node ID (slicing criterion)

§Returns

SliceResult containing all nodes affected by the source, separated by control/data dependence.

§Complexity

  • Time: O(|V| + |E|) - BFS for control + BFS for data
  • Space: O(|V|) - for visited sets and slice result

§Algorithm Steps

  1. Initialize slice: Add source to slice_nodes, control_nodes, data_nodes
  2. Control dependence BFS:
    • Start from source
    • Follow cdg edges (what does each node control?)
    • Add controlled nodes to control_nodes and slice_nodes
    • Continue until queue exhausted (visited set prevents cycles)
  3. Data dependence: Call reachable_from(graph, source) for data flow
  4. Merge: Add data flow nodes to data_nodes and slice_nodes
  5. Return: SliceResult with size = slice_nodes.len()

§Self-Inclusion

The source node is always included in the slice (self-inclusion requirement).

§Example

use sqlitegraph::{SqliteGraph, algo::{control_dependence_from_exit, forward_slice}};

let graph = SqliteGraph::open_in_memory()?;
// ... build CFG ...

let cdg = control_dependence_from_exit(&graph)?;
let slice = forward_slice(&graph, &cdg, source_node)?;

println!("Forward slice from {}: {} nodes", source_node, slice.size);
println!("Control nodes: {:?}", slice.sorted_control_nodes());
println!("Data nodes: {:?}", slice.sorted_data_nodes());

§References

  • Bergeretti & Carre “Information-flow and data-flow analysis” ACM TOPLAS 1985
  • Silva “A Vocabulary of Program Slicing” 2007