From fb79b47b38325a9bf1a94b1bcd5f175731a42e60 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Fri, 8 Apr 2022 09:18:34 +0800 Subject: [PATCH] escape analysis: fixed infinite loop detection --- nac3core/src/toplevel/composer.rs | 2 +- .../src/typecheck/escape_analysis/lifetime.rs | 22 ++++++++++--------- nac3core/src/typecheck/escape_analysis/mod.rs | 5 ++++- nac3standalone/src/main.rs | 6 ++++- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/nac3core/src/toplevel/composer.rs b/nac3core/src/toplevel/composer.rs index 46b8a7be8..61448c685 100644 --- a/nac3core/src/toplevel/composer.rs +++ b/nac3core/src/toplevel/composer.rs @@ -1928,7 +1928,7 @@ impl TopLevelComposer { &args, &fun_body, ast.as_ref().unwrap().location, - ).map_err(|e| format!("Escape analysis error: {}\n in function {}", e, name))?; + ).map_err(|e| format!("Escape analysis error: {}\n ... in function {}", e, name))?; } instance_to_stmt.insert( diff --git a/nac3core/src/typecheck/escape_analysis/lifetime.rs b/nac3core/src/typecheck/escape_analysis/lifetime.rs index ff4ee047c..942527e64 100644 --- a/nac3core/src/typecheck/escape_analysis/lifetime.rs +++ b/nac3core/src/typecheck/escape_analysis/lifetime.rs @@ -123,7 +123,7 @@ impl LifetimeIRBuilder { LifetimeIR::Branch { targets } => { destination_mapping.insert(i, targets); } - _ => unreachable!(), + _ => () } } } @@ -155,28 +155,30 @@ impl LifetimeIRBuilder { pub fn analyze(&self) -> Result<(), String> { let mut analyzers = HashMap::new(); - analyzers.insert(0, LifetimeAnalyzer::new()); + analyzers.insert(0, (0, true, LifetimeAnalyzer::new())); let mut worklist = vec![0]; - let mut counter = 0; while let Some(bb) = worklist.pop() { - counter += 1; - if counter > 100 { - return Err(format!("escape analyzer stuck in infinite loop?")); + let (counter, updated, analyzer) = analyzers.get_mut(&bb).unwrap(); + *counter += 1; + if *counter > 100 { + return Err(format!("infinite loop detected at basic block {}", bb)); } - let mut analyzer = analyzers.get(&bb).unwrap().clone(); + *updated = false; + let mut analyzer = analyzer.clone(); let block = &self.basic_blocks[bb]; let ir_iter = block.iter().filter_map(|&id| { self.irs[id].as_ref().map(|(ir, loc)| (LifetimeId(id), ir, *loc)) }); if let Some(branch) = analyzer.analyze_basic_block(ir_iter)? { for &target in branch.iter() { - if let Some(successor) = analyzers.get_mut(&target.0) { - if successor.merge(&analyzer) { + if let Some((_, updated, successor)) = analyzers.get_mut(&target.0) { + if successor.merge(&analyzer) && !*updated { // changed worklist.push(target.0); + *updated = true; } } else { - analyzers.insert(target.0, analyzer.clone()); + analyzers.insert(target.0, (0, true, analyzer.clone())); worklist.push(target.0); } } diff --git a/nac3core/src/typecheck/escape_analysis/mod.rs b/nac3core/src/typecheck/escape_analysis/mod.rs index 971cd119c..9bd255f6d 100644 --- a/nac3core/src/typecheck/escape_analysis/mod.rs +++ b/nac3core/src/typecheck/escape_analysis/mod.rs @@ -67,7 +67,10 @@ impl<'a> EscapeAnalyzer<'a> { } } zelf.handle_statements(body)?; - zelf.builder.analyze() + zelf.builder.remove_empty_bb(); + zelf.builder.analyze().map_err(|e| { + format!("{}\nIR: {}", e, zelf.builder.print_ir()) + }) } fn need_alloca(&mut self, ty: Type) -> bool { diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index 37925157a..a87b94947 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -220,7 +220,11 @@ fn main() { let signature = store.from_signature(&mut composer.unifier, &primitive, &signature, &mut cache); let signature = store.add_cty(signature); - composer.start_analysis(true).unwrap(); + if let Err(e) = composer.start_analysis(true) { + eprintln!("{}", e); + std::process::exit(1); + } + let top_level = Arc::new(composer.make_top_level_context());