use tree_sitter::Node;
pub type BlockId = usize;
#[derive(Debug, Clone)]
pub struct BasicBlock {
pub id: BlockId,
pub statements: Vec<(usize, usize)>,
pub byte_range: (usize, usize),
pub condition_range: Option<(usize, usize)>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CfgEdge {
Fallthrough,
TrueBranch,
FalseBranch,
BackEdge,
Return,
Break,
Continue,
}
#[derive(Debug, Clone)]
pub struct FunctionCfg {
pub blocks: Vec<BasicBlock>,
pub edges: Vec<(BlockId, BlockId, CfgEdge)>,
pub entry: BlockId,
pub exits: Vec<BlockId>,
}
impl FunctionCfg {
pub fn successors(&self, id: BlockId) -> Vec<(BlockId, CfgEdge)> {
self.edges
.iter()
.filter(|(from, _, _)| *from == id)
.map(|(_, to, edge)| (*to, *edge))
.collect()
}
pub fn predecessors(&self, id: BlockId) -> Vec<(BlockId, CfgEdge)> {
self.edges
.iter()
.filter(|(_, to, _)| *to == id)
.map(|(from, _, edge)| (*from, *edge))
.collect()
}
pub fn block_count(&self) -> usize {
self.blocks.len()
}
pub fn get_block(&self, id: BlockId) -> Option<&BasicBlock> {
self.blocks.get(id)
}
}
struct Shapes {
if_kinds: &'static [&'static str],
while_kinds: &'static [&'static str],
for_kinds: &'static [&'static str],
unconditional_loop_kinds: &'static [&'static str],
do_while_kinds: &'static [&'static str],
return_kinds: &'static [&'static str],
break_kinds: &'static [&'static str],
continue_kinds: &'static [&'static str],
block_kinds: &'static [&'static str],
stmt_wrapper_kinds: &'static [&'static str],
else_wrapper_kinds: &'static [&'static str],
}
const C_SHAPES: Shapes = Shapes {
if_kinds: &["if_statement"],
while_kinds: &["while_statement"],
for_kinds: &["for_statement"],
unconditional_loop_kinds: &[],
do_while_kinds: &["do_statement"],
return_kinds: &["return_statement"],
break_kinds: &["break_statement"],
continue_kinds: &["continue_statement"],
block_kinds: &["compound_statement"],
stmt_wrapper_kinds: &[],
else_wrapper_kinds: &["else_clause"],
};
const RUST_SHAPES: Shapes = Shapes {
if_kinds: &["if_expression"],
while_kinds: &["while_expression"],
for_kinds: &["for_expression"],
unconditional_loop_kinds: &["loop_expression"],
do_while_kinds: &[],
return_kinds: &["return_expression"],
break_kinds: &["break_expression"],
continue_kinds: &["continue_expression"],
block_kinds: &["block"],
stmt_wrapper_kinds: &["expression_statement"],
else_wrapper_kinds: &["else_clause"],
};
fn shapes_for(key: &str) -> Option<&'static Shapes> {
match key {
"c" | "cpp" => Some(&C_SHAPES),
"rust" => Some(&RUST_SHAPES),
_ => None,
}
}
pub fn build_function_cfg<'a>(
func_node: Node<'a>,
source: &'a [u8],
key: &str,
) -> Option<FunctionCfg> {
let shapes = shapes_for(key)?;
let body = func_node.child_by_field_name("body")?;
let mut builder = CfgBuilder::new(shapes, body.start_byte());
builder.visit(body, source);
Some(builder.finish())
}
enum Task<'a> {
Visit(Node<'a>),
AfterIfTrue {
if_node: Node<'a>,
header: BlockId,
},
AfterIfFalse {
if_node: Node<'a>,
true_end: BlockId,
},
AfterWhileBody {
header: BlockId,
after: BlockId,
},
AfterForBody {
for_node: Node<'a>,
header: BlockId,
after: BlockId,
},
AfterUnconditionalLoopBody {
header: BlockId,
after: BlockId,
},
AfterDoWhileBody {
do_node: Node<'a>,
body_start: BlockId,
footer: BlockId,
after: BlockId,
},
}
struct CfgBuilder {
shapes: &'static Shapes,
blocks: Vec<BasicBlock>,
edges: Vec<(BlockId, BlockId, CfgEdge)>,
terminated: Vec<bool>,
current: BlockId,
loop_stack: Vec<(BlockId, BlockId)>,
}
impl CfgBuilder {
fn new(shapes: &'static Shapes, start: usize) -> Self {
let entry = BasicBlock {
id: 0,
statements: Vec::new(),
byte_range: (start, start),
condition_range: None,
};
Self {
shapes,
blocks: vec![entry],
edges: Vec::new(),
terminated: vec![false],
current: 0,
loop_stack: Vec::new(),
}
}
fn new_block(&mut self, start: usize) -> BlockId {
let id = self.blocks.len();
self.blocks.push(BasicBlock {
id,
statements: Vec::new(),
byte_range: (start, start),
condition_range: None,
});
self.terminated.push(false);
id
}
fn add_edge(&mut self, from: BlockId, to: BlockId, edge: CfgEdge) {
let key = (from, to, edge);
if !self.edges.contains(&key) {
self.edges.push(key);
}
}
fn join(&mut self, from: BlockId, to: BlockId, edge: CfgEdge) {
if !self.terminated[from] {
self.add_edge(from, to, edge);
}
}
fn append_stmt(&mut self, node: Node) {
let cur = self.current;
let block = &mut self.blocks[cur];
if block.statements.is_empty() {
block.byte_range.0 = node.start_byte();
}
block.byte_range.1 = node.end_byte();
block.statements.push((node.start_byte(), node.end_byte()));
}
fn unwrap<'a>(&self, mut node: Node<'a>, wrappers: &[&str]) -> Node<'a> {
while wrappers.contains(&node.kind()) {
match node.named_child(0) {
Some(inner) => node = inner,
None => break,
}
}
node
}
fn visit(&mut self, node: Node, source: &[u8]) {
let mut stack = vec![Task::Visit(node)];
while let Some(task) = stack.pop() {
match task {
Task::Visit(node) => self.dispatch(node, source, &mut stack),
Task::AfterIfTrue { if_node, header } => {
let true_end = self.current;
let false_start = self.new_block(if_node.start_byte());
self.add_edge(header, false_start, CfgEdge::FalseBranch);
self.current = false_start;
stack.push(Task::AfterIfFalse { if_node, true_end });
if let Some(alt) = if_node.child_by_field_name("alternative") {
let alt = self.unwrap(alt, self.shapes.else_wrapper_kinds);
stack.push(Task::Visit(alt));
}
}
Task::AfterIfFalse { if_node, true_end } => {
let false_end = self.current;
let join_block = self.new_block(if_node.end_byte());
self.join(true_end, join_block, CfgEdge::Fallthrough);
self.join(false_end, join_block, CfgEdge::Fallthrough);
self.current = join_block;
}
Task::AfterWhileBody { header, after } => {
self.loop_stack.pop();
self.join(self.current, header, CfgEdge::BackEdge);
self.current = after;
}
Task::AfterForBody {
for_node,
header,
after,
} => {
self.loop_stack.pop();
if let Some(update) = for_node.child_by_field_name("update") {
if !self.terminated[self.current] {
self.append_stmt(update);
}
}
self.join(self.current, header, CfgEdge::BackEdge);
self.current = after;
}
Task::AfterUnconditionalLoopBody { header, after } => {
self.loop_stack.pop();
self.join(self.current, header, CfgEdge::BackEdge);
self.current = after;
}
Task::AfterDoWhileBody {
do_node,
body_start,
footer,
after,
} => {
self.loop_stack.pop();
self.join(self.current, footer, CfgEdge::Fallthrough);
if let Some(cond) = do_node.child_by_field_name("condition") {
self.blocks[footer].condition_range =
Some((cond.start_byte(), cond.end_byte()));
}
self.add_edge(footer, body_start, CfgEdge::BackEdge);
self.add_edge(footer, after, CfgEdge::FalseBranch);
self.current = after;
}
}
}
}
fn dispatch<'a>(&mut self, node: Node<'a>, _source: &[u8], stack: &mut Vec<Task<'a>>) {
let node = self.unwrap(node, self.shapes.stmt_wrapper_kinds);
let kind = node.kind();
let shapes = self.shapes;
if shapes.block_kinds.contains(&kind) {
self.push_block_children(node, stack);
} else if shapes.if_kinds.contains(&kind) {
self.process_if(node, stack);
} else if shapes.while_kinds.contains(&kind) {
self.process_while(node, stack);
} else if shapes.for_kinds.contains(&kind) {
self.process_for(node, stack);
} else if shapes.unconditional_loop_kinds.contains(&kind) {
self.process_unconditional_loop(node, stack);
} else if shapes.do_while_kinds.contains(&kind) {
self.process_do_while(node, stack);
} else if shapes.return_kinds.contains(&kind) {
self.append_stmt(node);
let next = self.new_block(node.end_byte());
self.add_edge(self.current, next, CfgEdge::Return);
self.terminated[self.current] = true;
self.current = next;
self.terminated[next] = true;
} else if shapes.break_kinds.contains(&kind) {
self.append_stmt(node);
if let Some(&(_, brk)) = self.loop_stack.last() {
self.add_edge(self.current, brk, CfgEdge::Break);
}
self.terminated[self.current] = true;
self.current = self.new_block(node.end_byte());
self.terminated[self.current] = true;
} else if shapes.continue_kinds.contains(&kind) {
self.append_stmt(node);
if let Some(&(cont, _)) = self.loop_stack.last() {
self.add_edge(self.current, cont, CfgEdge::Continue);
}
self.terminated[self.current] = true;
self.current = self.new_block(node.end_byte());
self.terminated[self.current] = true;
} else {
self.append_stmt(node);
}
}
fn push_block_children<'a>(&self, node: Node<'a>, stack: &mut Vec<Task<'a>>) {
let mut cursor = node.walk();
let children: Vec<Node<'a>> = node
.children(&mut cursor)
.filter(|c| c.is_named())
.collect();
stack.extend(children.into_iter().rev().map(Task::Visit));
}
fn process_if<'a>(&mut self, node: Node<'a>, stack: &mut Vec<Task<'a>>) {
let header = self.current;
if let Some(cond) = node.child_by_field_name("condition") {
self.blocks[header].condition_range = Some((cond.start_byte(), cond.end_byte()));
}
let true_start = self.new_block(node.start_byte());
self.add_edge(header, true_start, CfgEdge::TrueBranch);
self.current = true_start;
stack.push(Task::AfterIfTrue {
if_node: node,
header,
});
if let Some(cons) = node.child_by_field_name("consequence") {
stack.push(Task::Visit(cons));
}
}
fn process_while<'a>(&mut self, node: Node<'a>, stack: &mut Vec<Task<'a>>) {
let prev = self.current;
let header = self.new_block(node.start_byte());
self.join(prev, header, CfgEdge::Fallthrough);
if let Some(cond) = node.child_by_field_name("condition") {
self.blocks[header].condition_range = Some((cond.start_byte(), cond.end_byte()));
}
let after = self.new_block(node.end_byte());
self.add_edge(header, after, CfgEdge::FalseBranch);
let body_start = self.new_block(node.start_byte());
self.add_edge(header, body_start, CfgEdge::TrueBranch);
self.current = body_start;
self.loop_stack.push((header, after));
stack.push(Task::AfterWhileBody { header, after });
if let Some(body) = node.child_by_field_name("body") {
stack.push(Task::Visit(body));
}
}
fn process_for<'a>(&mut self, node: Node<'a>, stack: &mut Vec<Task<'a>>) {
if let Some(init) = node.child_by_field_name("initializer") {
self.append_stmt(init);
}
let prev = self.current;
let header = self.new_block(node.start_byte());
self.join(prev, header, CfgEdge::Fallthrough);
if let Some(cond) = node.child_by_field_name("condition") {
self.blocks[header].condition_range = Some((cond.start_byte(), cond.end_byte()));
}
let after = self.new_block(node.end_byte());
self.add_edge(header, after, CfgEdge::FalseBranch);
let body_start = self.new_block(node.start_byte());
self.add_edge(header, body_start, CfgEdge::TrueBranch);
self.current = body_start;
self.loop_stack.push((header, after));
stack.push(Task::AfterForBody {
for_node: node,
header,
after,
});
if let Some(body) = node.child_by_field_name("body") {
stack.push(Task::Visit(body));
}
}
fn process_unconditional_loop<'a>(&mut self, node: Node<'a>, stack: &mut Vec<Task<'a>>) {
let prev = self.current;
let header = self.new_block(node.start_byte());
self.join(prev, header, CfgEdge::Fallthrough);
let after = self.new_block(node.end_byte());
self.current = header;
self.loop_stack.push((header, after));
stack.push(Task::AfterUnconditionalLoopBody { header, after });
if let Some(body) = node.child_by_field_name("body") {
stack.push(Task::Visit(body));
}
}
fn process_do_while<'a>(&mut self, node: Node<'a>, stack: &mut Vec<Task<'a>>) {
let prev = self.current;
let body_start = self.new_block(node.start_byte());
self.join(prev, body_start, CfgEdge::Fallthrough);
let footer = self.new_block(node.end_byte());
let after = self.new_block(node.end_byte());
self.current = body_start;
self.loop_stack.push((footer, after));
stack.push(Task::AfterDoWhileBody {
do_node: node,
body_start,
footer,
after,
});
if let Some(body) = node.child_by_field_name("body") {
stack.push(Task::Visit(body));
}
}
fn finish(self) -> FunctionCfg {
let last = self.blocks.len() - 1;
let mut exits: Vec<BlockId> = self
.edges
.iter()
.filter(|(_, _, edge)| *edge == CfgEdge::Return)
.map(|(_, to, _)| *to)
.collect();
let last_has_outgoing = self.edges.iter().any(|(from, _, _)| *from == last);
if !last_has_outgoing && !exits.contains(&last) {
exits.push(last);
}
exits.sort_unstable();
exits.dedup();
FunctionCfg {
blocks: self.blocks,
edges: self.edges,
entry: 0,
exits,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn parse(source: &str, language: tree_sitter::Language) -> tree_sitter::Tree {
let mut parser = tree_sitter::Parser::new();
parser.set_language(&language).unwrap();
parser.parse(source, None).unwrap()
}
fn find<'a>(node: Node<'a>, kind: &str) -> Node<'a> {
fn search<'a>(node: Node<'a>, kind: &str) -> Option<Node<'a>> {
if node.kind() == kind {
return Some(node);
}
let mut cursor = node.walk();
for child in node.children(&mut cursor) {
if let Some(found) = search(child, kind) {
return Some(found);
}
}
None
}
search(node, kind).unwrap_or_else(|| panic!("no {kind} node found"))
}
#[cfg(feature = "lang-c")]
fn c_cfg(source: &str) -> FunctionCfg {
let tree = parse(source, tree_sitter_c::LANGUAGE.into());
let bytes = source.as_bytes();
let func = find(tree.root_node(), "function_definition");
build_function_cfg(func, bytes, "c").unwrap()
}
#[cfg(feature = "lang-rust")]
fn rust_cfg(source: &str) -> FunctionCfg {
let tree = parse(source, tree_sitter_rust::LANGUAGE.into());
let bytes = source.as_bytes();
let func = find(tree.root_node(), "function_item");
build_function_cfg(func, bytes, "rust").unwrap()
}
#[cfg(feature = "lang-cpp")]
fn cpp_cfg(source: &str) -> FunctionCfg {
let tree = parse(source, tree_sitter_cpp::LANGUAGE.into());
let bytes = source.as_bytes();
let func = find(tree.root_node(), "function_definition");
build_function_cfg(func, bytes, "cpp").unwrap()
}
fn edge_kinds(cfg: &FunctionCfg) -> Vec<CfgEdge> {
cfg.edges.iter().map(|(_, _, e)| *e).collect()
}
#[test]
#[cfg(feature = "lang-python")]
fn unmapped_language_returns_none() {
let tree = parse(
"def f():\n return 1\n",
tree_sitter_python::LANGUAGE.into(),
);
let func = find(tree.root_node(), "function_definition");
assert!(build_function_cfg(func, "".as_bytes(), "python").is_none());
}
#[test]
#[cfg(feature = "lang-c")]
fn c_straight_line_is_a_single_block() {
let cfg = c_cfg("int f(void) { int x = 1; int y = 2; }");
assert_eq!(cfg.block_count(), 1);
assert_eq!(cfg.entry, 0);
assert_eq!(cfg.exits, vec![0]);
}
#[test]
#[cfg(feature = "lang-c")]
fn c_if_else_has_true_false_and_fallthrough_join() {
let cfg = c_cfg("int f(int x) { if (x) { x = 1; } else { x = 2; } }");
let kinds = edge_kinds(&cfg);
assert!(kinds.contains(&CfgEdge::TrueBranch));
assert!(kinds.contains(&CfgEdge::FalseBranch));
assert_eq!(
kinds.iter().filter(|k| **k == CfgEdge::Fallthrough).count(),
2
);
}
#[test]
#[cfg(feature = "lang-c")]
fn c_if_without_else_still_joins() {
let cfg = c_cfg("int f(int x) { if (x) { x = 1; } x = 2; }");
let kinds = edge_kinds(&cfg);
assert!(kinds.contains(&CfgEdge::TrueBranch));
assert!(kinds.contains(&CfgEdge::FalseBranch));
}
#[test]
#[cfg(feature = "lang-c")]
fn c_return_in_branch_does_not_join() {
let cfg = c_cfg("int f(int x) { if (x) { return 1; } x = 2; }");
let kinds = edge_kinds(&cfg);
assert!(kinds.contains(&CfgEdge::Return));
assert_eq!(
kinds.iter().filter(|k| **k == CfgEdge::Fallthrough).count(),
1
);
}
#[test]
#[cfg(feature = "lang-c")]
fn c_while_with_break_and_backedge() {
let cfg = c_cfg("int f(int x) { while (x) { if (x) { break; } x = x - 1; } }");
let kinds = edge_kinds(&cfg);
assert!(kinds.contains(&CfgEdge::BackEdge));
assert!(kinds.contains(&CfgEdge::Break));
assert!(kinds.contains(&CfgEdge::FalseBranch));
}
#[test]
#[cfg(feature = "lang-c")]
fn c_for_loop_has_condition_and_backedge() {
let cfg = c_cfg("int f(void) { for (int i = 0; i < 10; i = i + 1) { continue; } }");
let kinds = edge_kinds(&cfg);
assert!(kinds.contains(&CfgEdge::TrueBranch));
assert!(kinds.contains(&CfgEdge::FalseBranch));
assert!(kinds.contains(&CfgEdge::Continue));
}
#[test]
#[cfg(feature = "lang-c")]
fn c_do_while_body_runs_before_condition_check() {
let cfg = c_cfg("int f(int x) { do { x = x - 1; } while (x); }");
let kinds = edge_kinds(&cfg);
assert!(kinds.contains(&CfgEdge::BackEdge));
assert!(kinds.contains(&CfgEdge::FalseBranch));
}
#[test]
#[cfg(feature = "lang-c")]
fn c_nested_compound_statement_flattens() {
let cfg = c_cfg("int f(void) { { int x = 1; } { int y = 2; } }");
assert_eq!(cfg.block_count(), 1);
}
#[test]
#[cfg(feature = "lang-cpp")]
fn cpp_if_else_and_loop_share_c_shapes() {
let cfg = cpp_cfg(
"int f(int x) { if (x) { return 1; } else { while (x) { break; } } return 0; }",
);
let kinds = edge_kinds(&cfg);
assert!(kinds.contains(&CfgEdge::TrueBranch));
assert!(kinds.contains(&CfgEdge::FalseBranch));
assert!(kinds.contains(&CfgEdge::Break));
assert_eq!(kinds.iter().filter(|k| **k == CfgEdge::Return).count(), 2);
}
#[test]
#[cfg(feature = "lang-c")]
fn c_deeply_nested_if_chain_does_not_overflow_the_stack() {
let depth = 20_000;
let mut source = String::new();
source.push_str("int f(int x) {\n");
for _ in 0..depth {
source.push_str("if (x) {\n");
}
source.push_str("return 1;\n");
for _ in 0..depth {
source.push_str("}\n");
}
source.push('}');
let cfg = c_cfg(&source);
let kinds = edge_kinds(&cfg);
assert_eq!(
kinds.iter().filter(|k| **k == CfgEdge::TrueBranch).count(),
depth
);
}
#[test]
#[cfg(feature = "lang-rust")]
fn rust_if_else_unwraps_expression_statement_and_else_clause() {
let cfg = rust_cfg("fn f(x: i32) -> i32 { if x > 0 { return x; } else { return -x; } }");
let kinds = edge_kinds(&cfg);
assert!(kinds.contains(&CfgEdge::TrueBranch));
assert!(kinds.contains(&CfgEdge::FalseBranch));
assert_eq!(kinds.iter().filter(|k| **k == CfgEdge::Return).count(), 2);
}
#[test]
#[cfg(feature = "lang-rust")]
fn rust_for_loop_models_implicit_test() {
let cfg = rust_cfg("fn f() { for i in 0..10 { continue; } }");
let kinds = edge_kinds(&cfg);
assert!(kinds.contains(&CfgEdge::TrueBranch));
assert!(kinds.contains(&CfgEdge::FalseBranch));
assert!(kinds.contains(&CfgEdge::Continue));
}
#[test]
#[cfg(feature = "lang-rust")]
fn rust_unconditional_loop_has_no_false_branch() {
let cfg = rust_cfg("fn f(mut x: i32) { loop { if x > 0 { break; } x = x + 1; } }");
let kinds = edge_kinds(&cfg);
assert_eq!(
kinds.iter().filter(|k| **k == CfgEdge::FalseBranch).count(),
1
);
assert!(kinds.contains(&CfgEdge::Break));
assert!(kinds.contains(&CfgEdge::BackEdge));
}
#[test]
#[cfg(feature = "lang-rust")]
fn rust_unconditional_loop_exit_only_reachable_via_break() {
let cfg = rust_cfg("fn f() -> i32 { loop { return 1; } }");
let has_unreachable_after = cfg.blocks.iter().any(|b| {
b.id != cfg.entry && cfg.predecessors(b.id).is_empty() && b.statements.is_empty()
});
assert!(
has_unreachable_after,
"unreachable after-loop block should exist with no predecessors"
);
}
}