- FFI: add RVM/Program APIs, execution state accessors, HostAwait handling, and buffer/result helpers in rvm.rs, common.rs, engine.rs. - Compiler: emit HostAwait for __builtin_host_await in function_calls.rs. - RVM tests: add HostAwait regression cases and extend harness for suspend/resume responses in host_await.yaml and mod.rs. - C/C++: add RVM tests/examples and wrapper updates in rvm_tests.c, rvm_tests.cpp, regorus.hpp, plus CMake wiring. - C#: add Program/Rvm bindings, SafeHandle/PInvoke, tests, and example usage in Regorus, RvmProgramTests.cs, Program.cs, and README updates. - Go: add Program/Rvm bindings, tests, and examples in rvm.go, rvm_test.go, main.go. - Java: add Program/Rvm bindings, JNI glue, and examples in lib.rs, regorus, Test.java. - Python: add Program/Rvm bindings and examples in lib.rs, test.py. - WASM: add Program/Rvm bindings and examples in lib.rs, test.js. - Tooling: wire binding tests in xtask and ignore generated Java artifacts in .gitignore. Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
4.3 KiB
Regorus CSharp
Regorus is
- Rego-Rus(t) - A fast, light-weight Rego interpreter written in Rust.
- Rigorous - A rigorous enforcer of well-defined Rego semantics.
See main Regorus page for more details about the project.
Building
Github Actions
The simplest way to build a Nuget for Regorus' C# bindings is to use Github Actions. The action to do so is named bindings/csharp and is defined in .github/workflows/test-csharp.yml.
There are two ways to trigger a Nuget build.
- Runs are triggered automatically whenever a push or pull request is made to the
mainbranch. - A run can be triggered manually by navigating to the action in the Github UI and clicking
Run workflow. This option allows you to generate a Nuget for any branch, which is useful when testing the integration of in-progress changes to Regorus with other projects. Nuget files that are generated via this flow will have amanualtriggersuffix appended to their version number, making it easy to distinguish them from Nugets generated using themainbranch.
Once the workflow run completes, the generated Nuget can be downloaded by following these steps:
- Open the run.
- Click on
Build Regorus nugeton the left. - Expand the
Upload Regorus nugetstep. - Click the
Artifact download URLlink at the bottom. - Save and extract the downloaded zip file to find the
.nupkgfile.
Local
The cargo xtask runner provides helpers for local builds:
cargo xtask ffibuilds thebindings/fficrate for the host platform in debug mode. Add--target <triple>(repeatable) to cross-compile, or--releaseto produce optimised artefacts. Results land underbindings/ffi/target/<triple>/<profile>.cargo xtask nugetreuses those artefacts to pack the C# library. It defaults to debug builds for the host but accepts--target,--release,--artifacts-dir <path>to reuse existing binaries, and--enforce-artifactsto require every officially supported platform.cargo xtask test-csharpensures a NuGet is available (rebuilding when required or when--force-nugetis passed) and then runsRegorus.Tests,TestApp, andTargetExampleAppagainst it. The command accepts the same build flags ascargo xtask nuget.
Memory Usage Safeguards
The C# bindings expose allocator-backed memory tracking utilities via the static Regorus.MemoryLimits helper. Typical usage:
// Restrict total allocations to 128 MiB for the process
Regorus.MemoryLimits.SetGlobalMemoryLimit(128 * 1024 * 1024);
// Optional: tune how frequently each thread flushes its allocation counters
Regorus.MemoryLimits.SetThreadFlushThresholdOverride(256 * 1024);
// Engine operations throw InvalidOperationException with the allocator message if the budget is exceeded
using var engine = new Regorus.Engine();
var veryLargeJson = new string('x', 128 * 1024);
try
{
engine.SetInputJson(veryLargeJson);
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"Allocator reported: {ex.Message}");
}
// Restore defaults once done
Regorus.MemoryLimits.SetGlobalMemoryLimit(null);
Regorus.MemoryLimits.SetThreadFlushThresholdOverride(null);
See bindings/csharp/Regorus.Tests/RegorusTests.cs for scenario coverage and bindings/csharp/TargetExampleApp/Program.cs for end-to-end usage.
RVM Usage Example
The RVM API lets you compile a program from modules/entrypoints and execute it in a VM:
using Regorus;
const string Policy = """
package demo
default allow = false
allow if {
input.user == "alice"
some role in data.roles[input.user]
role == "admin"
}
""";
const string Data = """
{ "roles": { "alice": ["admin"] } }
""";
const string Input = """
{ "user": "alice" }
""";
var modules = new[] { new PolicyModule("demo.rego", Policy) };
var entryPoints = new[] { "data.demo.allow" };
using var program = Program.CompileFromModules(Data, modules, entryPoints);
var listing = program.GenerateListing();
using var vm = new Rvm();
vm.LoadProgram(program);
vm.SetDataJson(Data);
vm.SetInputJson(Input);
var result = vm.Execute();
Console.WriteLine($"allow: {result}");