mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
86b4a279fa
* fix(ffi): eliminate aliasing UB via to_shared_ref migration Add to_shared_ref() helper that creates &T (shared reference) from raw pointers instead of &mut T. This eliminates undefined behavior caused by violating Rust's aliasing invariant when C# SafeHandle permits concurrent FFI calls on the same handle. With &mut T, the compiler may assume exclusive (noalias) access and reorder or elide reads/writes — a miscompilation risk when another thread holds a reference to the same object. Switching to &T removes that assumption; actual mutation is mediated by the interior RwLock inside Handle<T>, which is the sole synchronization mechanism. Migrated sites: - rvm.rs: 20 non-drop call sites - engine.rs: 30 non-drop call sites + with_unwind_guard for timer fns - compiled_policy.rs: 2 call sites - Fix null-data UB in regorus_program_deserialize_binary Drop paths retain to_ref() where exclusive access is guaranteed by the caller contract (preventing use-after-free). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * feat(ffi): add Azure Policy JSON compilation FFI and C# bindings - AliasRegistry builder pattern: RegorusAliasRegistryBuilder (mutable, single-threaded) + RegorusAliasRegistry (immutable, Arc-wrapped) - Azure Policy JSON compilation: regorus_compile_azure_policy_rule and regorus_compile_azure_policy_definition with alias registry support - regorus_rvm_set_context for host-supplied ambient data - C# AliasRegistryBuilder and AliasRegistry classes with convenience factories (FromJson, FromManifest, Empty) - C# AzurePolicyCompiler static class for policy rule/definition compilation - Compile functions take *const RegorusAliasRegistry (read-only via to_shared_ref for concurrent compilation safety) - Fix pre-existing clippy warnings across multiple crates Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
81 lines
2.7 KiB
Rust
81 lines
2.7 KiB
Rust
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
use crate::common::*;
|
|
use crate::panic_guard::with_unwind_guard;
|
|
use alloc::boxed::Box;
|
|
use alloc::string::String;
|
|
use anyhow::Result;
|
|
use core::ffi::c_char;
|
|
use core::ptr;
|
|
|
|
/// Wrapper for `regorus::CompiledPolicy`.
|
|
#[derive(Clone)]
|
|
pub struct RegorusCompiledPolicy {
|
|
pub(crate) compiled_policy: regorus::CompiledPolicy,
|
|
}
|
|
|
|
/// Drop a `RegorusCompiledPolicy`.
|
|
#[no_mangle]
|
|
pub extern "C" fn regorus_compiled_policy_drop(compiled_policy: *mut RegorusCompiledPolicy) {
|
|
if let Ok(cp) = to_ref(compiled_policy) {
|
|
unsafe {
|
|
let _ = Box::from_raw(ptr::from_mut(cp));
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Evaluate the compiled policy with the given input.
|
|
///
|
|
/// For target policies, evaluates the target's effect rule.
|
|
/// For regular policies, evaluates the originally compiled rule.
|
|
///
|
|
/// * `input`: JSON encoded input data (resource) to validate against the policy.
|
|
#[no_mangle]
|
|
pub extern "C" fn regorus_compiled_policy_eval_with_input(
|
|
compiled_policy: *mut RegorusCompiledPolicy,
|
|
input: *const c_char,
|
|
) -> RegorusResult {
|
|
with_unwind_guard(|| {
|
|
let output = || -> Result<String> {
|
|
let input_value = regorus::Value::from_json_str(&from_c_str(input)?)?;
|
|
let result = to_shared_ref(compiled_policy as *const RegorusCompiledPolicy)?
|
|
.compiled_policy
|
|
.eval_with_input(input_value)?;
|
|
result.to_json_str()
|
|
}();
|
|
|
|
match output {
|
|
Ok(out) => RegorusResult::ok_string(out),
|
|
Err(e) => to_regorus_result(Err(e)),
|
|
}
|
|
})
|
|
}
|
|
|
|
/// Configure the execution timer for evaluations of this compiled policy.
|
|
/// Get information about the compiled policy including metadata about modules,
|
|
/// target configuration, and resource types.
|
|
///
|
|
/// Returns a JSON-encoded `PolicyInfo` struct containing comprehensive
|
|
/// information about the compiled policy such as module IDs, target name,
|
|
/// applicable resource types, entry point rule, and parameters.
|
|
#[no_mangle]
|
|
pub extern "C" fn regorus_compiled_policy_get_policy_info(
|
|
compiled_policy: *mut RegorusCompiledPolicy,
|
|
) -> RegorusResult {
|
|
with_unwind_guard(|| {
|
|
let output = || -> Result<String> {
|
|
let info = to_shared_ref(compiled_policy as *const RegorusCompiledPolicy)?
|
|
.compiled_policy
|
|
.get_policy_info()?;
|
|
serde_json::to_string(&info)
|
|
.map_err(|e| anyhow::anyhow!("Failed to serialize policy info: {}", e))
|
|
}();
|
|
|
|
match output {
|
|
Ok(out) => RegorusResult::ok_string(out),
|
|
Err(e) => to_regorus_result(Err(e)),
|
|
}
|
|
})
|
|
}
|