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

[VAN-800] prevent stack overflow #72

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions circom/tests/calls/recurse.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma circom 2.0.0;
// REQUIRES: circom
// RUN: rm -rf %t && mkdir %t && %circom --llvm -o %t %s | sed -n 's/.*Written successfully:.* \(.*\)/\1/p' | xargs cat | FileCheck %s --enable-var-scope

function Recurse(i, n) {
if (n == 0) {
return i;
}
return Recurse(i, n-1);
}

template FnAssign() {
signal input inp;
signal output outp;

outp <== Recurse(inp, 20);
}

component main = FnAssign();

//CHECK-LABEL: define{{.*}} i256 @Recurse_{{[0-9]+}}(i256* %0){{.*}} {
//CHECK-LABEL: define{{.*}} void @FnAssign_{{[0-9]+}}_run([0 x i256]* %0){{.*}} {
26 changes: 17 additions & 9 deletions circuit_passes/src/bucket_interpreter/observed_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::cell::RefCell;
use std::collections::HashSet;
use code_producers::llvm_elements::fr::BUILT_IN_NAMES;
use compiler::intermediate_representation::InstructionPointer;
use compiler::intermediate_representation::ir_interface::*;
Expand All @@ -7,11 +9,14 @@ use super::observer::Observer;
pub struct ObservedVisitor<'a, S> {
observer: &'a dyn Observer<S>,
libs: Option<&'a dyn LibraryAccess>,
// Wrapped in a RefCell because the reference to the static analysis is immutable but we need mutability
/// Keep track of those already visited to prevent stack overflow.
visited_funcs: RefCell<HashSet<String>>,
}

impl<'a, S> ObservedVisitor<'a, S> {
pub fn new(observer: &'a dyn Observer<S>, libs: Option<&'a dyn LibraryAccess>) -> Self {
ObservedVisitor { observer, libs }
ObservedVisitor { observer, libs, visited_funcs: Default::default() }
}

pub fn visit_address_type(&self, addr_type: &AddressType, state: &S, observe: bool) {
Expand Down Expand Up @@ -48,16 +53,19 @@ impl<'a, S> ObservedVisitor<'a, S> {
self.visit_address_type(&fd.dest_address_type, state, observe);
self.visit_location_rule(&fd.dest, state, observe);
}
// Visit the callee function body if LibraryAccess was provided
// Visit the callee function body if it was not visited before and if given LibraryAccess
if let Some(libs) = self.libs {
let name = &bucket.symbol;
// Skip those that cannot be visited (i.e. not yet in Circuit.functions)
if !BUILT_IN_NAMES.with(|f| f.contains(name.as_str())) {
self.visit_instructions(
&libs.get_function(name).body,
state,
observe && !self.observer.ignore_call(name),
);
if self.visited_funcs.borrow_mut().insert(name.clone()) {
// Skip those that cannot be visited (i.e. not yet in Circuit.functions)
if !BUILT_IN_NAMES.with(|f| f.contains(name.as_str())) {
println!("[FINDME] visiting function {}", name);

Choose a reason for hiding this comment

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

Can we make this println debug only?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, thanks. I meant to remove it actually.

self.visit_instructions(
&libs.get_function(name).body,
state,
observe && !self.observer.ignore_call(name),
);
}
}
}
}
Expand Down
Loading