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

runtime: Add env var for maximum stack size #2719

Merged
merged 1 commit into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ those.
corresponds to 1GB.
- `GRAPH_QUERY_CACHE_STALE_PERIOD`: Number of queries after which a cache
entry can be considered stale. Defaults to 100.
- `GRAPH_MAX_API_VERSION`: Maximum `apiVersion` supported, if a developer tries to create a subgraph
with a higher `apiVersion` than this in their mappings, they'll receive an error. Defaults to `0.0.5`.
- `GRAPH_RUNTIME_MAX_STACK_SIZE`: Maximum stack size for the WASM runtime, if exceeded the execution
stops and an error is thrown. Defaults to 512KiB.

## GraphQL

Expand Down
11 changes: 11 additions & 0 deletions runtime/wasm/src/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@ use std::collections::BTreeMap;
use std::sync::Arc;
use std::thread;

const ONE_MIB: usize = 1 << 20; // 1_048_576

lazy_static! {
/// Verbose logging of mapping inputs
pub static ref LOG_TRIGGER_DATA: bool = std::env::var("GRAPH_LOG_TRIGGER_DATA").is_ok();

/// Maximum stack size for the WASM runtime
pub static ref MAX_STACK_SIZE: usize = std::env::var("GRAPH_RUNTIME_MAX_STACK_SIZE")
.ok()
.and_then(|max_stack_size| max_stack_size.parse().ok())
// 512KiB
.unwrap_or(ONE_MIB / 2);
}

/// Spawn a wasm module in its own thread.
Expand Down Expand Up @@ -140,6 +149,8 @@ impl ValidModule {
config.interruptable(true); // For timeouts.
config.cranelift_nan_canonicalization(true); // For NaN determinism.
config.cranelift_opt_level(wasmtime::OptLevel::None);
config.max_wasm_stack(*MAX_STACK_SIZE).unwrap(); // Safe because this only panics if size passed is 0.

let engine = &wasmtime::Engine::new(&config)?;
let module = wasmtime::Module::from_binary(&engine, raw_module)?;

Expand Down