aranya_runtime/vm_policy.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
//! VmPolicy implements a [Policy] that evaluates actions and commands via the [Policy
//! VM](../../policy_vm/index.html).
//!
//! ## Creating a `VmPolicy` instance
//!
//! To use `VmPolicy` in your [`Engine`](super::Engine), you need to provide a Policy VM
//! [`Machine`], a [`aranya_crypto::Engine`], and a Vec of Boxed FFI implementations. The Machine
//! will be created by either compiling a policy document (see
//! [`parse_policy_document()`](../../policy_lang/lang/fn.parse_policy_document.html) and
//! [`Compiler`](../../policy_compiler/struct.Compiler.html)), or loading a compiled policy
//! module (see [`Machine::from_module()`]). The crypto engine comes from your favorite
//! implementation
//! ([`DefaultEngine::from_entropy()`](aranya_crypto::default::DefaultEngine::from_entropy) is a
//! good choice for testing). The list of FFIs is a list of things that implement
//! [`FfiModule`](aranya_policy_vm::ffi::FfiModule), most likely via the [ffi attribute
//! macro](../../policy_vm/ffi/attr.ffi.html). The list of FFI modules _must_ be in the same
//! order as the FFI schemas given during VM construction.
//!
//! ```ignore
//! // Create a `Machine` by compiling policy from source.
//! let ast = parse_policy_document(policy_doc).unwrap();
//! let machine = Compiler::new(&ast)
//! .ffi_modules(&[TestFfiEnvelope::SCHEMA])
//! .compile()
//! .unwrap();
//! // Create a `aranya_crypto::Engine` implementation
//! let (eng, _) = DefaultEngine::from_entropy(Rng);
//! // Create a list of FFI module implementations
//! let ffi_modules = vec![Box::from(TestFfiEnvelope {
//! user: UserId::random(&mut Rng),
//! })];
//! // And finally, create the VmPolicy
//! let policy = VmPolicy::new(machine, eng, ffi_modules).unwrap();
//! ```
//!
//! ## Actions and Effects
//!
//! The VM represents actions as a kind of function, which has a name and a list of
//! parameters. [`VmPolicy`] represents those actions as [`VmAction`]. Calling an action
//! via [`call_action()`](VmPolicy::call_action) requires you to give it an action of
//! that type. You can use the [`vm_action!()`](crate::vm_action) macro to create this
//! more comfortably.
//!
//! The VM represents effects as a named struct containing a set of fields. `VmPolicy`
//! represents this as [`VmEffect`]. Effects captured via [`Sink`]s will have this type.
//! You can use the [`vm_effect!()`](crate::vm_effect) macro to create effects.
//!
//! ## The "init" command and action
//!
//! To create a graph, there must be a command that is the ancestor of all commands in that
//! graph - the "init" command. In `VmPolicy`, that command is created via a special action
//! given as the second argument to
//! [`ClientState::new_graph()`](crate::ClientState::new_graph). The first command produced
//! by that action becomes the "init" command. It has basically all the same properties as
//! any other command, except it has no parent.
//!
//! So for this example policy:
//!
//! ```policy
//! command Init {
//! fields {
//! nonce int,
//! }
//! seal { ... }
//! open { ... }
//! policy {
//! finish {}
//! }
//! }
//!
//! action init(nonce int) {
//! publish Init {
//! nonce: nonce,
//! }
//! }
//! ```
//!
//! This is an example of initializing a graph with `new_graph()`:
//!
//! ```ignore
//! let engine = MyEngine::new();
//! let provider = MyStorageProvider::new();
//! let mut cs = ClientState::new(engine, provider);
//! let mut sink = MySink::new();
//!
//! let storage_id = cs
//! .new_graph(&[0u8], vm_action!(init(0)), &mut sink)
//! .expect("could not create graph");
//! ```
//!
//! Because the ID of this initial command is also the storage ID of the resulting graph,
//! some data within the command must be present to ensure that multiple initial commands
//! create distinct IDs for each graph. If no other suitable data exists, it is good
//! practice to add a nonce field that is distinct for each graph.
//!
//! ## Priorities
//!
//! `VmPolicy` uses the policy language's attributes system to report command priorities to
//! the runtime. You can specify the priority of a command by adding the `priority`
//! attribute. It should be an `int` literal.
//!
//! ```policy
//! command Foo {
//! attributes {
//! priority: 3
//! }
//! // ... fields, policy, etc.
//! }
//! ```
//!
//! ## Policy Interface Generator
//!
//! A more comfortable way to use `VmPolicy` is via the [Policy Interface
//! Generator](../../policy_ifgen/index.html). It creates a Rust interface for actions and
//! effects from a policy document.
extern crate alloc;
use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, string::String, sync::Arc, vec::Vec};
use core::fmt;
use aranya_buggy::bug;
use aranya_policy_vm::{
ActionContext, CommandContext, ExitReason, KVPair, Machine, MachineIO, MachineStack,
OpenContext, PolicyContext, RunState, SealContext, Struct, Value,
};
use spin::Mutex;
use tracing::{error, info, instrument};
use crate::{
command::{Command, CommandId},
engine::{EngineError, NullSink, Policy, Sink},
CommandRecall, FactPerspective, MergeIds, Perspective, Prior,
};
mod error;
mod io;
mod protocol;
pub mod testing;
pub use error::*;
pub use io::*;
pub use protocol::*;
/// Creates a [`VmAction`].
///
/// This must be used directly to avoid lifetime issues, not assigned to a variable.
///
/// # Example
///
/// ```ignore
/// let x = 42;
/// let y = String::from("asdf");
/// client.action(storage_id, sink, vm_action!(foobar(x, y)))
/// ```
#[macro_export]
macro_rules! vm_action {
($name:ident($($arg:expr),* $(,)?)) => {
$crate::VmAction {
name: stringify!($name),
args: [$(::aranya_policy_vm::Value::from($arg)),*].as_slice().into(),
}
};
}
/// Creates a [`VmEffectData`].
///
/// This is mostly useful for testing expected effects, and is expected to be compared
/// against a [`VmEffect`].
///
/// # Example
///
/// ```ignore
/// let val = 3;
/// sink.add_expectation(vm_effect!(StuffHappened { x: 1, y: val }));
///
/// client.action(storage_id, sink, vm_action!(create(val)))
/// ```
#[macro_export]
macro_rules! vm_effect {
($name:ident { $($field:ident : $val:expr),* $(,)? }) => {
$crate::VmEffectData {
name: stringify!($name).into(),
fields: vec![$(
::aranya_policy_vm::KVPair::new(stringify!($field), $val.into())
),*],
}
};
}
/// A [Policy] implementation that uses the Policy VM.
pub struct VmPolicy<E> {
machine: Machine,
engine: Mutex<E>,
ffis: Mutex<Vec<Box<dyn FfiCallable<E> + Send + 'static>>>,
// TODO(chip): replace or fill this with priorities from attributes
priority_map: Arc<BTreeMap<String, u32>>,
}
impl<E> VmPolicy<E> {
/// Create a new `VmPolicy` from a [Machine]
pub fn new(
machine: Machine,
engine: E,
ffis: Vec<Box<dyn FfiCallable<E> + Send + 'static>>,
) -> Result<Self, VmPolicyError> {
let priority_map = VmPolicy::<E>::get_command_priorities(&machine)?;
Ok(Self {
machine,
engine: Mutex::from(engine),
ffis: Mutex::from(ffis),
priority_map: Arc::new(priority_map),
})
}
fn source_location<M>(&self, rs: &RunState<'_, M>) -> String
where
M: MachineIO<MachineStack>,
{
rs.source_location()
.unwrap_or(String::from("(unknown location)"))
}
/// Scans command attributes for priorities and creates the priority map from them.
fn get_command_priorities(machine: &Machine) -> Result<BTreeMap<String, u32>, VmPolicyError> {
let mut priority_map = BTreeMap::new();
for (name, attrs) in &machine.command_attributes {
if let Some(Value::Int(p)) = attrs.get("priority") {
let pv = (*p).try_into().map_err(|e| {
error!(
?e,
"Priority out of range in {name}: {p} does not fit in u32"
);
VmPolicyError::Unknown
})?;
priority_map.insert(name.clone(), pv);
}
}
Ok(priority_map)
}
}
impl<E: aranya_crypto::Engine> VmPolicy<E> {
#[allow(clippy::too_many_arguments)]
#[instrument(skip_all, fields(name = name))]
fn evaluate_rule<'a, P>(
&self,
name: &str,
fields: &[KVPair],
envelope: Envelope<'_>,
facts: &'a mut P,
sink: &'a mut impl Sink<VmEffect>,
ctx: &CommandContext<'_>,
recall: CommandRecall,
) -> Result<(), EngineError>
where
P: FactPerspective,
{
let mut ffis = self.ffis.lock();
let mut eng = self.engine.lock();
let mut io = VmPolicyIO::new(facts, sink, &mut *eng, &mut ffis);
let mut rs = self.machine.create_run_state(&mut io, ctx);
let self_data = Struct::new(name, fields);
match rs.call_command_policy(&self_data.name, &self_data, envelope.clone().into()) {
Ok(reason) => match reason {
ExitReason::Normal => Ok(()),
ExitReason::Check => {
info!("Check {}", self.source_location(&rs));
// Construct a new recall context from the policy context
let CommandContext::Policy(policy_ctx) = ctx else {
error!("Non-policy context while evaluating rule: {ctx:?}");
return Err(EngineError::InternalError);
};
let recall_ctx = CommandContext::Recall(policy_ctx.clone());
rs.set_context(&recall_ctx);
self.recall_internal(recall, &mut rs, name, &self_data, envelope)
}
ExitReason::Panic => {
info!("Panicked {}", self.source_location(&rs));
Err(EngineError::Panic)
}
},
Err(e) => {
error!("\n{e}");
Err(EngineError::InternalError)
}
}
}
fn recall_internal<M>(
&self,
recall: CommandRecall,
rs: &mut RunState<'_, M>,
name: &str,
self_data: &Struct,
envelope: Envelope<'_>,
) -> Result<(), EngineError>
where
M: MachineIO<MachineStack>,
{
match recall {
CommandRecall::None => Err(EngineError::Check),
CommandRecall::OnCheck => {
match rs.call_command_recall(name, self_data, envelope.into()) {
Ok(ExitReason::Normal) => Err(EngineError::Check),
Ok(ExitReason::Check) => {
info!("Recall failed: {}", self.source_location(rs));
Err(EngineError::Check)
}
Ok(ExitReason::Panic) | Err(_) => {
info!("Recall panicked: {}", self.source_location(rs));
Err(EngineError::Panic)
}
}
}
}
}
#[instrument(skip_all, fields(name = name))]
fn open_command<P>(
&self,
name: &str,
envelope: Envelope<'_>,
facts: &mut P,
) -> Result<Struct, EngineError>
where
P: FactPerspective,
{
let mut sink = NullSink;
let mut ffis = self.ffis.lock();
let mut eng = self.engine.lock();
let mut io = VmPolicyIO::new(facts, &mut sink, &mut *eng, &mut ffis);
let ctx = CommandContext::Open(OpenContext { name });
let mut rs = self.machine.create_run_state(&mut io, &ctx);
let status = rs.call_open(name, envelope.into());
match status {
Ok(reason) => match reason {
ExitReason::Normal => {
let v = rs.consume_return().map_err(|e| {
error!("Could not pull envelope from stack: {e}");
EngineError::InternalError
})?;
Ok(v.try_into().map_err(|e| {
error!("Envelope is not a struct: {e}");
EngineError::InternalError
})?)
}
ExitReason::Check => {
info!("Check {}", self.source_location(&rs));
Err(EngineError::Check)
}
ExitReason::Panic => {
info!("Panicked {}", self.source_location(&rs));
Err(EngineError::Check)
}
},
Err(e) => {
error!("\n{e}");
Err(EngineError::InternalError)
}
}
}
#[instrument(skip_all, fields(name = name))]
fn seal_command(
&self,
name: &str,
fields: impl IntoIterator<Item = impl Into<(String, Value)>>,
ctx_parent: CommandId,
facts: &mut impl FactPerspective,
) -> Result<Envelope<'static>, EngineError> {
let mut sink = NullSink;
let mut ffis = self.ffis.lock();
let mut eng = self.engine.lock();
let mut io = VmPolicyIO::new(facts, &mut sink, &mut *eng, &mut ffis);
let ctx = CommandContext::Seal(SealContext {
name,
head_id: ctx_parent.into(),
});
let mut rs = self.machine.create_run_state(&mut io, &ctx);
let command_struct = Struct::new(name, fields);
let status = rs.call_seal(name, &command_struct);
match status {
Ok(reason) => match reason {
ExitReason::Normal => {
let v = rs.consume_return().map_err(|e| {
error!("Could not pull envelope from stack: {e}");
EngineError::InternalError
})?;
let strukt = Struct::try_from(v).map_err(|e| {
error!("Envelope is not a struct: {e}");
EngineError::InternalError
})?;
let envelope = Envelope::try_from(strukt).map_err(|e| {
error!("Malformed Envelope: {e}");
EngineError::InternalError
})?;
Ok(envelope)
}
ExitReason::Check => {
info!("Check {}", self.source_location(&rs));
Err(EngineError::Check)
}
ExitReason::Panic => {
info!("Panicked {}", self.source_location(&rs));
Err(EngineError::Panic)
}
},
Err(e) => {
error!("\n{e}");
Err(EngineError::InternalError)
}
}
}
}
/// [`VmPolicy`]'s actions.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VmAction<'a> {
/// The name of the action.
pub name: &'a str,
/// The arguments of the action.
pub args: Cow<'a, [Value]>,
}
/// A partial version of [`VmEffect`] containing only the data. Created by
/// [`vm_effect!`] and used to compare only the name and fields against the full
/// `VmEffect`.
#[derive(Debug)]
pub struct VmEffectData {
/// The name of the effect.
pub name: String,
/// The fields of the effect.
pub fields: Vec<KVPair>,
}
impl PartialEq<VmEffect> for VmEffectData {
fn eq(&self, other: &VmEffect) -> bool {
self.name == other.name && self.fields == other.fields
}
}
impl PartialEq<VmEffectData> for VmEffect {
fn eq(&self, other: &VmEffectData) -> bool {
self.name == other.name && self.fields == other.fields
}
}
/// [`VmPolicy`]'s effects.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct VmEffect {
/// The name of the effect.
pub name: String,
/// The fields of the effect.
pub fields: Vec<KVPair>,
/// The command ID that produced this effect
pub command: CommandId,
/// Was this produced from a recall block?
pub recalled: bool,
}
impl<E: aranya_crypto::Engine> Policy for VmPolicy<E> {
type Action<'a> = VmAction<'a>;
type Effect = VmEffect;
type Command<'a> = VmProtocol<'a>;
fn serial(&self) -> u32 {
// TODO(chip): Implement an actual serial number
0u32
}
#[instrument(skip_all)]
fn call_rule(
&self,
command: &impl Command,
facts: &mut impl FactPerspective,
sink: &mut impl Sink<Self::Effect>,
recall: CommandRecall,
) -> Result<(), EngineError> {
let unpacked: VmProtocolData<'_> = postcard::from_bytes(command.bytes()).map_err(|e| {
error!("Could not deserialize: {e:?}");
EngineError::Read
})?;
match unpacked {
VmProtocolData::Init {
author_id,
kind,
serialized_fields,
signature,
..
} => {
let envelope = Envelope {
parent_id: CommandId::default(),
author_id,
command_id: command.id(),
payload: Cow::Borrowed(serialized_fields),
signature: Cow::Borrowed(signature),
};
let command_struct = self.open_command(kind, envelope.clone(), facts)?;
let fields: Vec<KVPair> = command_struct
.fields
.into_iter()
.map(|(k, v)| KVPair::new(&k, v))
.collect();
let ctx = CommandContext::Policy(PolicyContext {
name: kind,
id: command.id().into(),
author: author_id,
version: CommandId::default().into(),
});
self.evaluate_rule(kind, fields.as_slice(), envelope, facts, sink, &ctx, recall)?
}
VmProtocolData::Basic {
parent,
kind,
author_id,
serialized_fields,
signature,
} => {
let envelope = Envelope {
parent_id: parent.id,
author_id,
command_id: command.id(),
payload: Cow::Borrowed(serialized_fields),
signature: Cow::Borrowed(signature),
};
let command_struct = self.open_command(kind, envelope.clone(), facts)?;
let fields: Vec<KVPair> = command_struct
.fields
.into_iter()
.map(|(k, v)| KVPair::new(&k, v))
.collect();
let ctx = CommandContext::Policy(PolicyContext {
name: kind,
id: command.id().into(),
author: author_id,
version: CommandId::default().into(),
});
self.evaluate_rule(kind, fields.as_slice(), envelope, facts, sink, &ctx, recall)?
}
// Merges always pass because they're an artifact of the graph
_ => (),
}
Ok(())
}
#[instrument(skip_all, fields(name = action.name))]
fn call_action(
&self,
action: Self::Action<'_>,
facts: &mut impl Perspective,
sink: &mut impl Sink<Self::Effect>,
) -> Result<(), EngineError> {
let VmAction { name, args } = action;
let parent = match facts.head_address()? {
Prior::None => None,
Prior::Single(id) => Some(id),
Prior::Merge(_, _) => bug!("cannot have a merge parent in call_action"),
};
// FIXME(chip): This is kind of wrong, but it avoids having to
// plumb Option<Id> into the VM and FFI
let ctx_parent = parent.unwrap_or_default();
let publish_stack = {
let mut ffis = self.ffis.lock();
let mut eng = self.engine.lock();
let mut io = VmPolicyIO::new(facts, sink, &mut *eng, &mut ffis);
let ctx = CommandContext::Action(ActionContext {
name,
head_id: ctx_parent.id.into(),
});
{
let mut rs = self.machine.create_run_state(&mut io, &ctx);
let exit_reason = match args {
Cow::Borrowed(args) => rs.call_action(name, args.iter().cloned()),
Cow::Owned(args) => rs.call_action(name, args),
}
.map_err(|e| {
error!("\n{e}");
EngineError::InternalError
})?;
match exit_reason {
ExitReason::Normal => {}
ExitReason::Check => {
info!("Check {}", self.source_location(&rs));
return Err(EngineError::Check);
}
ExitReason::Panic => {
info!("Panicked {}", self.source_location(&rs));
return Err(EngineError::Panic);
}
};
}
io.into_publish_stack()
};
for (name, fields) in publish_stack {
let envelope = self.seal_command(&name, fields, ctx_parent.id, facts)?;
let data = match parent {
None => VmProtocolData::Init {
// TODO(chip): where does the policy value come from?
policy: 0u64.to_le_bytes(),
author_id: envelope.author_id,
kind: &name,
serialized_fields: &envelope.payload,
signature: &envelope.signature,
},
Some(parent) => VmProtocolData::Basic {
author_id: envelope.author_id,
parent,
kind: &name,
serialized_fields: &envelope.payload,
signature: &envelope.signature,
},
};
let wrapped = postcard::to_allocvec(&data)?;
let new_command = VmProtocol::new(
&wrapped,
envelope.command_id,
data,
Arc::clone(&self.priority_map),
);
self.call_rule(&new_command, facts, sink, CommandRecall::None)?;
facts.add_command(&new_command).map_err(|e| {
error!("{e}");
EngineError::Write
})?;
}
Ok(())
}
fn merge<'a>(
&self,
target: &'a mut [u8],
ids: MergeIds,
) -> Result<Self::Command<'a>, EngineError> {
let (left, right) = ids.into();
let c = VmProtocolData::Merge { left, right };
let data = postcard::to_slice(&c, target).map_err(|e| {
error!("{e}");
EngineError::Write
})?;
let id = CommandId::hash_for_testing_only(data);
Ok(VmProtocol::new(data, id, c, Arc::clone(&self.priority_map)))
}
}
impl fmt::Display for VmAction<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_tuple(self.name);
for arg in self.args.as_ref() {
d.field(&DebugViaDisplay(arg));
}
d.finish()
}
}
impl fmt::Display for VmEffect {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct(&self.name);
for field in &self.fields {
d.field(field.key(), &DebugViaDisplay(field.value()));
}
d.finish()
}
}
/// Implements `Debug` via `T`'s `Display` impl.
struct DebugViaDisplay<T>(T);
impl<T: fmt::Display> fmt::Debug for DebugViaDisplay<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}