Files
regorus/src/builtins/opa.rs
T
Anand Krishnamoorthi fd59bb5a91 feat(memory): Allocator-backed global memory limits (#544)
Policy evaluation at scale needs to be able to set memory limits
so that a bad policy does not hog memory or to ensure that
policy evaluation itself does not use too much memory which could
cause other components to suffer.

This PR introduces capability to set and enforce global memory limits.
It also lays the groundwork for enabling per evaluation limits in future.

Once a global memory limit is set, Regorus maintains per thread counters
to track memory activity (allocation, deallocation) of a thread.
These counters are periodically flushed to global memory counters.
Per thread counters avoid the contention that updating global counters
on each alloc/free would cause.

Policy evaluation periodically checks these counters and raises errors
if allocated memory has exceeded the configured limit.

Currently memory limit capability is exposed only to FFI and C#.

Also update mimalloc to v2.2.6

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
2026-01-24 07:08:54 +05:30

113 lines
3.1 KiB
Rust

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use crate::ast::{Expr, Ref};
use crate::builtins;
use crate::builtins::utils::{enforce_limit, ensure_args_count};
use crate::*;
use crate::lexer::Span;
use crate::value::Value;
use alloc::collections::BTreeMap;
use anyhow::Result;
pub fn register(m: &mut builtins::BuiltinsMap<&'static str, builtins::BuiltinFcn>) {
m.insert("opa.runtime", (opa_runtime, 0));
}
fn opa_runtime(span: &Span, params: &[Ref<Expr>], args: &[Value], _strict: bool) -> Result<Value> {
let name = "opa.runtime";
ensure_args_count(span, name, params, args, 0)?;
let mut obj = BTreeMap::new();
obj.insert(
Value::String("commit".into()),
Value::String(env!("GIT_HASH").into()),
);
obj.insert(
Value::String("regorus-version".into()),
Value::String(env!("CARGO_PKG_VERSION").into()),
);
obj.insert(
Value::String("version".into()),
Value::String("0.60.0".into()),
);
// Emitting environment variables could lead to confidential data being leaked.
#[cfg(feature = "std")]
if false {
obj.insert(
Value::String("env".into()),
Value::from_map(
std::env::vars()
.map(|(k, v)| (Value::String(k.into()), Value::String(v.into())))
.collect(),
),
);
}
let features = [
#[cfg(feature = "base64")]
"base64",
#[cfg(feature = "base64url")]
"base64url",
#[cfg(feature = "glob")]
"glob",
#[cfg(feature = "graph")]
"graph",
#[cfg(feature = "hex")]
"hex",
#[cfg(feature = "http")]
"http",
#[cfg(feature = "jsonschema")]
"jsonschema",
#[cfg(feature = "opa-runtime")]
"opa-runtime",
#[cfg(feature = "regex")]
"regex",
#[cfg(feature = "semver")]
"semver",
#[cfg(feature = "time")]
"time",
#[cfg(feature = "uuid")]
"uuid",
#[cfg(feature = "urlquery")]
"urlquery",
#[cfg(feature = "yaml")]
"yaml",
"",
];
let features = &features[..features.len() - 1];
let mut feature_values = Vec::with_capacity(features.len());
for feature in features.iter() {
feature_values.push(Value::String((*feature).to_string().into()));
// Guard feature list growth while reporting enabled capabilities.
enforce_limit()?;
}
obj.insert(
Value::String("features".into()),
Value::from_array(feature_values),
);
let mut builtins: Vec<&&str> = builtins::BUILTINS.keys().collect();
builtins.sort();
let mut builtin_values = Vec::with_capacity(builtins.len());
for builtin in builtins.iter() {
builtin_values.push(Value::String((**builtin).to_string().into()));
// Guard builtin list growth while reporting registered functions.
enforce_limit()?;
}
obj.insert(
Value::String("builtins".into()),
Value::from_array(builtin_values),
);
Ok(Value::from_map(obj))
}