-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sql: avoid stack overflow on deeply nested ASTs
In the SQL planner, use the same technique as the SQL parser for automatically growing the stack when presented with a deeply nested AST. The stack-checking code is generalized and moved to a new `ore::stack` module. Fix #9103.
- Loading branch information
Showing
8 changed files
with
179 additions
and
64 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright Materialize, Inc. and contributors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License in the LICENSE file at the | ||
// root of this repository, or online at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Stack management utilities. | ||
|
||
use std::cell::RefCell; | ||
use std::error::Error; | ||
use std::fmt; | ||
|
||
// The red zone is the amount of stack space that must be available on | ||
// the current stack in order to call `f` without allocating a new | ||
// stack. | ||
const STACK_RED_ZONE: usize = 32 << 10; // 32KiB | ||
|
||
// The size of any freshly-allocated stacks. It was chosen to match the | ||
// default stack size for threads in Rust. | ||
const STACK_SIZE: usize = 2 << 20; // 2MiB | ||
|
||
/// A trait for types which support recursion without stack overflow. | ||
pub trait CheckedRecursion { | ||
/// Extracts a reference to the recursion guard embedded within the type. | ||
fn recursion_guard(&self) -> &RecursionGuard; | ||
|
||
/// Checks whether it is safe to recur and calls `f` if so. | ||
/// | ||
/// If the recursion limit `LIMIT` has been reached, returns a | ||
/// `RecursionLimitError`. Otherwise, it will call `f`, possibly growing | ||
/// the stack if necessary. | ||
/// | ||
/// Calls to this function must be manually inserted at any point that | ||
/// mutual recursion occurs. | ||
fn checked_recur<F, T, E>(&self, f: F) -> Result<T, E> | ||
where | ||
F: FnOnce(&Self) -> Result<T, E>, | ||
E: From<RecursionLimitError>, | ||
{ | ||
self.recursion_guard().descend()?; | ||
let out = stacker::maybe_grow(STACK_RED_ZONE, STACK_SIZE, || f(self)); | ||
self.recursion_guard().ascend(); | ||
out | ||
} | ||
|
||
/// Like [`CheckedRecursion::checked_recur`], but operates on a mutable | ||
/// reference to `Self`. | ||
fn checked_recur_mut<F, T, E>(&mut self, f: F) -> Result<T, E> | ||
where | ||
F: FnOnce(&mut Self) -> Result<T, E>, | ||
E: From<RecursionLimitError>, | ||
{ | ||
self.recursion_guard().descend()?; | ||
let out = stacker::maybe_grow(STACK_RED_ZONE, STACK_SIZE, || f(self)); | ||
self.recursion_guard().ascend(); | ||
out | ||
} | ||
} | ||
|
||
/// A guard which is embedded in a type that implements [`CheckedRecursion`] | ||
/// to track the depth of recursion. | ||
#[derive(Default, Debug, Clone)] | ||
pub struct RecursionGuard { | ||
depth: RefCell<usize>, | ||
limit: usize, | ||
} | ||
|
||
impl RecursionGuard { | ||
/// Constructs a new recursion guard with the specified recursion | ||
/// limit. | ||
pub fn with_limit(limit: usize) -> RecursionGuard { | ||
RecursionGuard { | ||
depth: RefCell::new(0), | ||
limit, | ||
} | ||
} | ||
|
||
fn descend(&self) -> Result<(), RecursionLimitError> { | ||
let mut depth = self.depth.borrow_mut(); | ||
if *depth < self.limit { | ||
*depth += 1; | ||
Ok(()) | ||
} else { | ||
Err(RecursionLimitError) | ||
} | ||
} | ||
|
||
fn ascend(&self) { | ||
*self.depth.borrow_mut() -= 1; | ||
} | ||
} | ||
|
||
/// A [`RecursionGuard`]'s recursion limit was reached. | ||
#[derive(Clone, Debug)] | ||
pub struct RecursionLimitError; | ||
|
||
impl fmt::Display for RecursionLimitError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.write_str("recursion limit exceeded") | ||
} | ||
} | ||
|
||
impl Error for RecursionLimitError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.