mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Compare commits
79 Commits
regorus-v0
...
regorus-v0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0e9e34a519 | ||
|
|
3f7a5496dc | ||
|
|
0316ccd90c | ||
|
|
10eebfe54c | ||
|
|
e688806ca0 | ||
|
|
394625d4bc | ||
|
|
e68e852ee3 | ||
|
|
2b1434b3ac | ||
|
|
fd59bb5a91 | ||
|
|
80686d6ed1 | ||
|
|
9426b2ec02 | ||
|
|
740db8a0f5 | ||
|
|
d626f75421 | ||
|
|
5afbd96159 | ||
|
|
28891ef883 | ||
|
|
49958c2ece | ||
|
|
08a5e00960 | ||
|
|
1d71df30b6 | ||
|
|
249dcd0b43 | ||
|
|
604591a0f7 | ||
|
|
dbfb8e38a8 | ||
|
|
273a80571e | ||
|
|
3f29eb2fa6 | ||
|
|
889a02ddd6 | ||
|
|
70f63a0982 | ||
|
|
6bc1249dc8 | ||
|
|
5d0cf95332 | ||
|
|
fd4bb3081f | ||
|
|
93a633750c | ||
|
|
52b56f4214 | ||
|
|
8b84d4ce12 | ||
|
|
ecf95833f9 | ||
|
|
9fa8036ce4 | ||
|
|
a232b13e50 | ||
|
|
e9a50bcfd5 | ||
|
|
d0fa639bb8 | ||
|
|
a514e8da83 | ||
|
|
252ae0e312 | ||
|
|
ce85e0102d | ||
|
|
a8f5ac6117 | ||
|
|
632f64b2ce | ||
|
|
c41f289b19 | ||
|
|
2a75b3b0b6 | ||
|
|
3962b3c38d | ||
|
|
d4b7d1ff6c | ||
|
|
d6cd738822 | ||
|
|
36e75d3e49 | ||
|
|
befe131048 | ||
|
|
bedf667adc | ||
|
|
8269968c4a | ||
|
|
e3d23766ae | ||
|
|
1d627f3798 | ||
|
|
b7b3d3ec87 | ||
|
|
30bd134a0b | ||
|
|
5aefd51cb6 | ||
|
|
e060e43a6c | ||
|
|
12c083e29e | ||
|
|
a8a3a9809b | ||
|
|
14deaaa5b6 | ||
|
|
ed360879a6 | ||
|
|
4988bda647 | ||
|
|
92b9ec8fa8 | ||
|
|
a3a20a1235 | ||
|
|
688e6128d4 | ||
|
|
ad8c543fb5 | ||
|
|
49bd3c22f3 | ||
|
|
6dc505c88b | ||
|
|
091bbb2e5c | ||
|
|
1e4ff952e6 | ||
|
|
25a7ddad0a | ||
|
|
5d8387f4d9 | ||
|
|
9604fe86f1 | ||
|
|
ac388684bc | ||
|
|
57f2e7703c | ||
|
|
4ec9e76440 | ||
|
|
1b0c2d4072 | ||
|
|
85753aaf37 | ||
|
|
c43c94559a | ||
|
|
2a0b4ae6b5 |
2
.cargo/config.toml
Normal file
2
.cargo/config.toml
Normal file
@@ -0,0 +1,2 @@
|
||||
[alias]
|
||||
xtask = "run --package xtask --"
|
||||
29
.github/actions/toolchains/rust/action.yml
vendored
Normal file
29
.github/actions/toolchains/rust/action.yml
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
name: rust-toolchain
|
||||
description: Setup Rust toolchain with specified version and components
|
||||
inputs:
|
||||
toolchain:
|
||||
description: 'Rust toolchain version'
|
||||
required: false
|
||||
default: '1.92.0'
|
||||
components:
|
||||
description: 'Additional components to install'
|
||||
required: false
|
||||
default: 'clippy rustfmt'
|
||||
targets:
|
||||
description: 'Target architectures to install'
|
||||
required: false
|
||||
default: ''
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- shell: bash
|
||||
run: |
|
||||
rustup override set ${{ inputs.toolchain }}
|
||||
if [ -n "${{ inputs.components }}" ]; then
|
||||
rustup component add ${{ inputs.components }}
|
||||
fi
|
||||
if [ -n "${{ inputs.targets }}" ]; then
|
||||
rustup target add ${{ inputs.targets }}
|
||||
fi
|
||||
cargo --version
|
||||
rustc --version
|
||||
191
.github/workflows/codeql.yml
vendored
Normal file
191
.github/workflows/codeql.yml
vendored
Normal file
@@ -0,0 +1,191 @@
|
||||
name: "CodeQL Security Analysis"
|
||||
|
||||
on:
|
||||
schedule:
|
||||
# Run weekly on Wednesdays at 3:17 AM UTC
|
||||
- cron: '17 3 * * 3'
|
||||
workflow_dispatch:
|
||||
# Allow manual triggering
|
||||
push:
|
||||
branches: [ "main" ]
|
||||
pull_request:
|
||||
branches: [ "main" ]
|
||||
|
||||
jobs:
|
||||
analyze:
|
||||
name: Analyze (${{ matrix.language }})
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 60
|
||||
permissions:
|
||||
# required for all workflows
|
||||
security-events: write
|
||||
# required to fetch internal or private CodeQL packs
|
||||
packages: read
|
||||
# only required for workflows in private repositories
|
||||
actions: read
|
||||
contents: read
|
||||
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
include:
|
||||
# Rust analysis for main crate and Rust-based bindings
|
||||
- language: rust
|
||||
build-mode: none
|
||||
working-directory: .
|
||||
# C/C++ analysis for FFI bindings
|
||||
- language: c-cpp
|
||||
build-mode: manual
|
||||
working-directory: bindings/ffi
|
||||
# Python analysis for Python bindings
|
||||
- language: python
|
||||
build-mode: none
|
||||
working-directory: bindings/python
|
||||
# Java analysis for Java bindings
|
||||
- language: java-kotlin
|
||||
build-mode: manual
|
||||
working-directory: bindings/java
|
||||
# Go analysis for Go bindings
|
||||
- language: go
|
||||
build-mode: manual
|
||||
working-directory: bindings/go
|
||||
# C# analysis for C# bindings
|
||||
- language: csharp
|
||||
build-mode: manual
|
||||
working-directory: bindings/csharp
|
||||
# JavaScript analysis for WASM bindings
|
||||
- language: javascript-typescript
|
||||
build-mode: none
|
||||
working-directory: bindings/wasm
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
# Setup language-specific dependencies BEFORE CodeQL init for proper tracing setup
|
||||
- name: Setup Rust
|
||||
uses: ./.github/actions/toolchains/rust
|
||||
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
|
||||
- name: Fetch workspace dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Fetch FFI crate dependencies
|
||||
if: matrix.language == 'c-cpp' || matrix.language == 'go' || matrix.language == 'csharp'
|
||||
run: cargo fetch --locked --manifest-path bindings/ffi/Cargo.toml
|
||||
|
||||
- name: Fetch Java crate dependencies
|
||||
if: matrix.language == 'java-kotlin'
|
||||
run: cargo fetch --locked --manifest-path bindings/java/Cargo.toml
|
||||
|
||||
- name: Setup Python
|
||||
if: matrix.language == 'python'
|
||||
uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.10'
|
||||
|
||||
- name: Setup Java
|
||||
if: matrix.language == 'java-kotlin'
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: 'corretto'
|
||||
java-version: '8'
|
||||
|
||||
- name: Setup Go
|
||||
if: matrix.language == 'go'
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.21'
|
||||
|
||||
- name: Setup .NET
|
||||
if: matrix.language == 'csharp'
|
||||
uses: actions/setup-dotnet@v4
|
||||
with:
|
||||
global-json-file: ./bindings/csharp/global.json
|
||||
|
||||
- name: Invoke dotnet directly
|
||||
if: matrix.language == 'csharp'
|
||||
run: dotnet --info
|
||||
|
||||
- name: Setup Node.js
|
||||
if: matrix.language == 'javascript-typescript'
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '18'
|
||||
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v3
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
build-mode: ${{ matrix.build-mode }}
|
||||
|
||||
# Install additional build dependencies
|
||||
- name: Install system dependencies
|
||||
if: matrix.language == 'rust' || matrix.language == 'c-cpp'
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y build-essential cmake
|
||||
|
||||
- name: Install Python build dependencies
|
||||
if: matrix.language == 'python'
|
||||
working-directory: ${{ matrix.working-directory }}
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install maturin[patchelf] pytest
|
||||
|
||||
- name: Setup Ruby
|
||||
if: matrix.language == 'rust' && contains(matrix.working-directory, 'ruby')
|
||||
uses: ruby/setup-ruby@v1
|
||||
with:
|
||||
ruby-version: '3.4.2'
|
||||
bundler-cache: true
|
||||
working-directory: bindings/ruby
|
||||
|
||||
- name: Install WASM build dependencies
|
||||
if: matrix.language == 'javascript-typescript'
|
||||
run: |
|
||||
cargo install wasm-pack
|
||||
|
||||
# Manual build steps for different languages
|
||||
- name: Build C/C++ bindings via xtask
|
||||
if: matrix.language == 'c-cpp'
|
||||
run: |
|
||||
cargo xtask test-c --release --frozen
|
||||
cargo xtask test-cpp --release --frozen --skip-ffi
|
||||
cargo xtask test-c-no-std --release --frozen --skip-ffi
|
||||
|
||||
- name: Build Java bindings via xtask
|
||||
if: matrix.language == 'java-kotlin'
|
||||
run: cargo xtask test-java --release --frozen
|
||||
|
||||
- name: Build Go bindings via xtask
|
||||
if: matrix.language == 'go'
|
||||
run: cargo xtask test-go --release --frozen
|
||||
|
||||
- name: Build C# bindings manually
|
||||
if: matrix.language == 'csharp'
|
||||
working-directory: ${{ matrix.working-directory }}
|
||||
run: |
|
||||
# Temporary workaround: CodeQL's tracer replaces dotnet with a missing shim when cargo xtask test-csharp runs,
|
||||
# so invoke dotnet directly here until the upstream fix lands.
|
||||
# Ideal command once fixed: cargo xtask test-csharp --release
|
||||
# Build the FFI library that C# bindings access via P/Invoke
|
||||
cd ../ffi
|
||||
cargo build --release --locked
|
||||
cd ../csharp
|
||||
# Restore NuGet packages and build .NET assemblies in release mode
|
||||
dotnet restore Regorus/Regorus.csproj
|
||||
dotnet build Regorus/Regorus.csproj --no-restore /p:Configuration=Release /p:IgnoreMissingArtifacts=true
|
||||
|
||||
- name: Build WASM bindings via xtask
|
||||
if: matrix.language == 'javascript-typescript'
|
||||
run: cargo xtask build-wasm --release
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v3
|
||||
with:
|
||||
category: "/language:${{matrix.language}}"
|
||||
29
.github/workflows/pr-extensions.yml
vendored
29
.github/workflows/pr-extensions.yml
vendored
@@ -18,19 +18,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Build only std
|
||||
run: cargo build -r --example regorus --no-default-features --features "std,rego-extensions"
|
||||
- name: Doc Tests
|
||||
run: cargo test -r --doc --features rego-extensions
|
||||
- name: Run tests
|
||||
run: cargo test -r --features rego-extensions
|
||||
- name: Run example
|
||||
run: cargo run --example regorus --features rego-extensions -- eval -d examples/server/allowed_server.rego -i examples/server/input.json data.example
|
||||
- name: Run tests (ACI)
|
||||
run: cargo test -r --test aci --features rego-extensions
|
||||
- name: Run tests (KATA)
|
||||
run: cargo test -r --test kata --features rego-extensions
|
||||
- name: Run tests (OPA Conformance)
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
- name: Setup Rust toolchain
|
||||
uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
- name: Run rego extensions CI suite
|
||||
run: >-
|
||||
cargo test -r --test opa --features opa-testutil,serde_json/arbitrary_precision,rego-extensions -- $(tr '\n' ' ' < tests/opa.passing)
|
||||
cargo xtask ci-release --frozen --features rego-extensions
|
||||
--skip-all-features-build --skip-no-default-features-tests
|
||||
--skip-azure-policy --skip-azure-rbac
|
||||
--opa-features "opa-testutil,serde_json/arbitrary_precision,rego-extensions"
|
||||
|
||||
40
.github/workflows/pr.yml
vendored
40
.github/workflows/pr.yml
vendored
@@ -18,32 +18,14 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Format Check
|
||||
run: cargo fmt --check
|
||||
- name: Fetch
|
||||
run: cargo fetch
|
||||
- name: Build (all features)
|
||||
run: cargo build -r --all-features --frozen
|
||||
- name: Build
|
||||
run: cargo build -r --frozen
|
||||
- name: Test no_std
|
||||
run: cargo test -r --no-default-features --frozen
|
||||
- name: Build only std
|
||||
run: cargo build -r --example regorus --no-default-features --features "std" --frozen
|
||||
- name: Doc Tests
|
||||
run: cargo test -r --doc --frozen
|
||||
- name: Run tests
|
||||
run: cargo test -r --frozen
|
||||
- name: Run example
|
||||
run: cargo run --example regorus --frozen -- eval -d examples/server/allowed_server.rego -i examples/server/input.json data.example
|
||||
- name: Run tests (ACI)
|
||||
run: cargo test -r --test aci --frozen
|
||||
- name: Run tests (KATA)
|
||||
run: cargo test -r --test kata --frozen
|
||||
- name: Run tests (OPA Conformance)
|
||||
run: >-
|
||||
cargo test -r --test opa --frozen --features opa-testutil,serde_json/arbitrary_precision -- $(tr '\n' ' ' < tests/opa.passing)
|
||||
- name: Run tests (Azure Policy)
|
||||
run: >-
|
||||
cargo test --frozen --features azure_policy
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
- name: Setup Rust toolchain
|
||||
uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
- name: Run release CI suite
|
||||
run: cargo xtask ci-release --frozen
|
||||
|
||||
21
.github/workflows/publish-java.yml
vendored
21
.github/workflows/publish-java.yml
vendored
@@ -32,27 +32,28 @@ jobs:
|
||||
os: windows-latest
|
||||
extension: dll
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- uses: actions/setup-java@v4
|
||||
- uses: actions/setup-java@dded0888837ed1f317902acf8a20df0ad188d165 # v5.0.0
|
||||
with:
|
||||
java-version: 8
|
||||
distribution: "corretto"
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
with:
|
||||
targets: ${{ matrix.target }}
|
||||
- if: ${{ matrix.build_cmd == 'zigbuild' }}
|
||||
uses: actions/setup-python@v5
|
||||
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
||||
with:
|
||||
python-version: "3.11"
|
||||
- if: ${{ matrix.build_cmd == 'zigbuild' }}
|
||||
run: pip install cargo-zigbuild
|
||||
- run: cargo fetch
|
||||
- run: cargo fetch --locked
|
||||
- run: cargo fetch --locked --manifest-path bindings/java/Cargo.toml
|
||||
- run: cargo ${{ matrix.build_cmd || 'build' }} --release --frozen --target ${{ matrix.target }}${{ matrix.glibc && format('.{0}', matrix.glibc) || '' }} --manifest-path ./bindings/java/Cargo.toml
|
||||
- run: mkdir -p native/${{ matrix.target }}
|
||||
- run: mv target/${{ matrix.target }}/release/*.${{ matrix.extension }} ./native/${{ matrix.target }}/
|
||||
- uses: actions/upload-artifact@v4
|
||||
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: native-libraries-${{ matrix.target }}
|
||||
path: native/
|
||||
@@ -62,24 +63,24 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
needs: build
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- uses: actions/setup-java@v4
|
||||
- uses: actions/setup-java@dded0888837ed1f317902acf8a20df0ad188d165 # v5.0.0
|
||||
with:
|
||||
java-version: 8
|
||||
distribution: "corretto"
|
||||
server-id: ossrh
|
||||
server-username: MAVEN_USERNAME
|
||||
server-password: MAVEN_PASSWORD
|
||||
- uses: actions/download-artifact@v4
|
||||
- uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
|
||||
with:
|
||||
pattern: native-libraries-*
|
||||
merge-multiple: true
|
||||
path: ./bindings/java/native/
|
||||
- run: mvn package
|
||||
working-directory: ./bindings/java
|
||||
- uses: actions/upload-artifact@v4
|
||||
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: built-jars
|
||||
path: ./bindings/java/target/regorus-java-*.jar
|
||||
|
||||
39
.github/workflows/publish-python.yml
vendored
39
.github/workflows/publish-python.yml
vendored
@@ -18,14 +18,15 @@ jobs:
|
||||
matrix:
|
||||
target: [x86_64, x86, aarch64, armv7, s390x, ppc64le]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-python@v4
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
||||
with:
|
||||
python-version: '3.10'
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
|
||||
- name: Build Python extension
|
||||
run: |
|
||||
cargo fetch
|
||||
cargo fetch --locked
|
||||
cargo clippy --all-targets --no-deps -- -Dwarnings
|
||||
cargo build --release --target ${{ matrix.target }} --frozen
|
||||
working-directory: bindings/python
|
||||
@@ -38,9 +39,9 @@ jobs:
|
||||
sccache: 'true'
|
||||
manylinux: auto
|
||||
- name: Upload wheels
|
||||
uses: actions/upload-artifact@v3
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: wheels
|
||||
name: wheels-linux-${{ matrix.target }}
|
||||
path: dist
|
||||
|
||||
windows:
|
||||
@@ -49,15 +50,16 @@ jobs:
|
||||
matrix:
|
||||
target: [x64, x86]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-python@v4
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
||||
with:
|
||||
python-version: '3.10'
|
||||
architecture: ${{ matrix.target }}
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
|
||||
- name: Build Python extension
|
||||
run: |
|
||||
cargo fetch
|
||||
cargo fetch --locked
|
||||
cargo clippy --all-targets --no-deps -- -Dwarnings
|
||||
cargo build --release --target ${{ matrix.host.target }} --frozen
|
||||
working-directory: bindings/python
|
||||
@@ -69,9 +71,9 @@ jobs:
|
||||
args: --release --out dist --manifest-path bindings/python/Cargo.toml --frozen --strip
|
||||
sccache: 'true'
|
||||
- name: Upload wheels
|
||||
uses: actions/upload-artifact@v3
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: wheels
|
||||
name: wheels-windows-${{ matrix.target }}
|
||||
path: dist
|
||||
|
||||
macos:
|
||||
@@ -80,14 +82,15 @@ jobs:
|
||||
matrix:
|
||||
target: [x86_64, aarch64, universal2-apple-darwin]
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/setup-python@v4
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
||||
with:
|
||||
python-version: '3.10'
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
|
||||
- name: Build Python extension
|
||||
run: |
|
||||
cargo fetch
|
||||
cargo fetch --locked
|
||||
cargo clippy --all-targets --no-deps -- -Dwarnings
|
||||
cargo build --release --target ${{ matrix.host.target }} --frozen
|
||||
working-directory: bindings/python
|
||||
@@ -99,9 +102,9 @@ jobs:
|
||||
args: --release --out dist --manifest-path bindings/python/Cargo.toml --offline --strip
|
||||
sccache: 'true'
|
||||
- name: Upload wheels
|
||||
uses: actions/upload-artifact@v3
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: wheels
|
||||
name: wheels-macos-${{ matrix.host.target }}
|
||||
path: dist
|
||||
|
||||
release:
|
||||
@@ -111,9 +114,11 @@ jobs:
|
||||
# if: "startsWith(github.ref, 'refs/tags/')"
|
||||
needs: [linux, windows, macos]
|
||||
steps:
|
||||
- uses: actions/download-artifact@v3
|
||||
- uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
|
||||
with:
|
||||
name: wheels
|
||||
pattern: wheels-*
|
||||
merge-multiple: true
|
||||
path: wheels
|
||||
- name: Publish to PyPI
|
||||
uses: PyO3/maturin-action@63b75c597b83e247fbf4fb7719801cc4220ae9f3 # v1.43.0
|
||||
env:
|
||||
|
||||
4
.github/workflows/publish-wasm.yml
vendored
4
.github/workflows/publish-wasm.yml
vendored
@@ -12,11 +12,11 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
# Setup .npmrc file to publish to npm
|
||||
- uses: actions/setup-node@v4
|
||||
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
|
||||
with:
|
||||
node-version: '20.x'
|
||||
registry-url: 'https://registry.npmjs.org'
|
||||
|
||||
4
.github/workflows/release-plz.yml
vendored
4
.github/workflows/release-plz.yml
vendored
@@ -14,11 +14,11 @@ jobs:
|
||||
contents: write
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Install Rust toolchain
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
uses: ./.github/actions/toolchains/rust
|
||||
- name: Run release-plz
|
||||
uses: MarcoIeni/release-plz-action@8724d33cd97b8295051102e2e19ca592962238f5 #v0.5.108
|
||||
env:
|
||||
|
||||
23
.github/workflows/rust-clippy.yml
vendored
23
.github/workflows/rust-clippy.yml
vendored
@@ -30,32 +30,27 @@ jobs:
|
||||
actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
|
||||
- name: Install Rust toolchain
|
||||
uses: actions-rs/toolchain@16499b5e05bf2e26879000db0c1d13f7e13fa3af #@v1
|
||||
- name: Setup Rust toolchain
|
||||
uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
components: clippy
|
||||
override: true
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
|
||||
- name: Install required cargo
|
||||
run: cargo install clippy-sarif sarif-fmt
|
||||
|
||||
- name: Fetch
|
||||
run: cargo fetch
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Run rust-clippy
|
||||
run:
|
||||
cargo clippy
|
||||
--all-features
|
||||
--message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
|
||||
--frozen
|
||||
run: cargo xtask clippy --sarif rust-clippy-results.sarif
|
||||
continue-on-error: true
|
||||
|
||||
- name: Upload analysis results to GitHub
|
||||
uses: github/codeql-action/upload-sarif@v1
|
||||
uses: github/codeql-action/upload-sarif@c298edae2d512d807fe4bdc57c0ac5a036f61501 # v3.29.11
|
||||
with:
|
||||
sarif_file: rust-clippy-results.sarif
|
||||
wait-for-processing: true
|
||||
|
||||
46
.github/workflows/test-c-cpp.yml
vendored
46
.github/workflows/test-c-cpp.yml
vendored
@@ -14,39 +14,29 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Fetch FFI crate dependencies
|
||||
run: cargo fetch --locked --manifest-path bindings/ffi/Cargo.toml
|
||||
|
||||
- name: Setup gcc, g++, cmake, ninja
|
||||
run: sudo apt update && sudo apt install -y gcc g++ cmake ninja-build
|
||||
|
||||
- name: Workaround to ensure that regorus.h is generated
|
||||
run: |
|
||||
cargo fetch
|
||||
cargo build -r --frozen
|
||||
working-directory: ./bindings/ffi
|
||||
|
||||
- name: Test c binding
|
||||
run: |
|
||||
mkdir bindings/c/build
|
||||
cd bindings/c/build
|
||||
cmake -G Ninja ..
|
||||
ninja
|
||||
./regorus_test
|
||||
- name: Test C binding via xtask
|
||||
run: cargo xtask test-c --release --frozen
|
||||
|
||||
- name: Test c-nostd binding
|
||||
run: |
|
||||
mkdir bindings/c-nostd/build
|
||||
cd bindings/c-nostd/build
|
||||
cmake -G Ninja ..
|
||||
ninja
|
||||
./regorus_test
|
||||
- name: Test C (no-std) binding via xtask
|
||||
run: cargo xtask test-c-nostd --release --frozen --skip-ffi
|
||||
|
||||
- name: Test cpp binding
|
||||
run: |
|
||||
mkdir bindings/cpp/build
|
||||
cd bindings/cpp/build
|
||||
cmake -G Ninja ..
|
||||
ninja
|
||||
./regorus_test
|
||||
- name: Test C++ binding via xtask
|
||||
run: cargo xtask test-cpp --release --frozen --skip-ffi
|
||||
|
||||
112
.github/workflows/test-csharp.yml
vendored
112
.github/workflows/test-csharp.yml
vendored
@@ -38,28 +38,25 @@ jobs:
|
||||
# **/release/libregorus_ffi.dylib
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Fetch crates
|
||||
run: cargo fetch
|
||||
working-directory: ./bindings/ffi
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Check Regorus binding formatting
|
||||
run: cargo fmt --check
|
||||
working-directory: ./bindings/ffi
|
||||
- name: Fetch FFI crate dependencies
|
||||
run: cargo fetch --locked --manifest-path bindings/ffi/Cargo.toml --target ${{ matrix.runtime.target }}
|
||||
|
||||
- name: Check Clippy linting for Regorus binding
|
||||
run: cargo clippy --frozen -- -D warnings
|
||||
working-directory: ./bindings/ffi
|
||||
|
||||
- name: Build Regorus binding
|
||||
run: cargo build -r --target ${{ matrix.runtime.target }} --locked
|
||||
working-directory: ./bindings/ffi
|
||||
- name: Build Regorus FFI via xtask
|
||||
run: cargo xtask build-ffi --release --target ${{ matrix.runtime.target }}
|
||||
|
||||
- name: Upload regorus ffi shared library
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: regorus-ffi-artifacts-${{ matrix.runtime.target }}
|
||||
# Note: The full path of each artifact relative to . is preserved.
|
||||
@@ -67,23 +64,32 @@ jobs:
|
||||
if-no-files-found: error
|
||||
retention-days: 1
|
||||
|
||||
build-nuget:
|
||||
build-csharp:
|
||||
name: 'Build Regorus nuget'
|
||||
runs-on: ubuntu-latest
|
||||
needs: build-ffi
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- uses: actions/setup-dotnet@v4
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
- uses: actions/setup-dotnet@3e891b0cb619bf60e2c25674b222b8940e2c1c25 # v4.1.0
|
||||
with:
|
||||
global-json-file: ./bindings/csharp/global.json
|
||||
|
||||
- run: echo '${{ steps.stepid.outputs.dotnet-version }}'
|
||||
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Download regorus ffi shared libraries
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
|
||||
with:
|
||||
pattern: regorus-ffi-artifacts-*
|
||||
merge-multiple: true
|
||||
@@ -92,17 +98,11 @@ jobs:
|
||||
- name: Display regorus ffi artifacts
|
||||
run: ls -R ./bindings/csharp/Regorus/tmp
|
||||
|
||||
# Note that we need to supply the target folder within the folder where artifacts are downloaded.
|
||||
- name: Build Regorus binding
|
||||
run: dotnet build /p:Configuration=Release /p:RegorusFFIArtifactsDir=./tmp/bindings/ffi/target
|
||||
working-directory: ./bindings/csharp/Regorus
|
||||
|
||||
- name: Pack
|
||||
run: dotnet pack /p:RegorusFFIArtifactsDir=./tmp/bindings/ffi/target
|
||||
working-directory: ./bindings/csharp/Regorus
|
||||
- name: Build Regorus nuget via xtask
|
||||
run: cargo xtask build-csharp --release --clean --artifacts-dir ./bindings/csharp/Regorus/tmp/bindings/ffi/target --enforce-artifacts
|
||||
|
||||
- name: Upload Regorus nuget
|
||||
uses: actions/upload-artifact@v4
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: regorus-nuget
|
||||
path: bindings/csharp/Regorus/bin/Release/Regorus*.nupkg
|
||||
@@ -111,7 +111,7 @@ jobs:
|
||||
|
||||
test-nuget:
|
||||
name: 'Test Regorus Nuget: (${{ matrix.runtime.target }})'
|
||||
needs: build-nuget
|
||||
needs: build-csharp
|
||||
runs-on: ${{ matrix.runtime.os }}
|
||||
strategy:
|
||||
# let us get failures from other jobs even if one fails
|
||||
@@ -126,52 +126,36 @@ jobs:
|
||||
# target: aarch64-apple-darwin
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- uses: actions/setup-dotnet@v4
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
|
||||
- uses: actions/setup-dotnet@3e891b0cb619bf60e2c25674b222b8940e2c1c25 # v4.1.0
|
||||
with:
|
||||
global-json-file: ./bindings/csharp/global.json
|
||||
|
||||
- run: echo '${{ steps.stepid.outputs.dotnet-version }}'
|
||||
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Download regorus nuget
|
||||
uses: actions/download-artifact@v4
|
||||
uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5.0.0
|
||||
with:
|
||||
name: regorus-nuget
|
||||
path: ./bindings/csharp/regorus-nuget/
|
||||
path: ./bindings/csharp/Regorus/bin/Release
|
||||
|
||||
- name: Restore Regorus.Tests
|
||||
run: dotnet restore /p:RestoreAdditionalProjectSources=../regorus-nuget
|
||||
working-directory: ./bindings/csharp/Regorus.Tests
|
||||
- name: Display regorus nuget
|
||||
run: ls -R ./bindings/csharp/Regorus/bin/Release
|
||||
|
||||
- name: Run Regorus.Tests
|
||||
run: dotnet test --no-restore
|
||||
working-directory: ./bindings/csharp/Regorus.Tests
|
||||
|
||||
- name: Restore TestApp
|
||||
run: dotnet restore /p:RestoreAdditionalProjectSources=../regorus-nuget
|
||||
working-directory: ./bindings/csharp/TestApp
|
||||
|
||||
- name: Build TestApp
|
||||
run: dotnet build --no-restore
|
||||
working-directory: ./bindings/csharp/TestApp
|
||||
|
||||
- name: Run TestApp
|
||||
run: dotnet run --no-build --framework net8.0
|
||||
working-directory: ./bindings/csharp/TestApp
|
||||
|
||||
- name: Restore TargetExampleApp
|
||||
run: dotnet restore /p:RestoreAdditionalProjectSources=../regorus-nuget
|
||||
working-directory: ./bindings/csharp/TargetExampleApp
|
||||
|
||||
- name: Build TargetExampleApp
|
||||
run: dotnet build --no-restore
|
||||
working-directory: ./bindings/csharp/TargetExampleApp
|
||||
|
||||
- name: Run TargetExampleApp
|
||||
run: dotnet run --no-build --framework net8.0
|
||||
working-directory: ./bindings/csharp/TargetExampleApp
|
||||
- name: Run C# tests via xtask
|
||||
run: cargo xtask test-csharp --release --clean --nuget-dir bindings/csharp/Regorus/bin/Release
|
||||
|
||||
|
||||
|
||||
18
.github/workflows/test-ffi.yml
vendored
18
.github/workflows/test-ffi.yml
vendored
@@ -14,13 +14,19 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Fetch FFI crate dependencies
|
||||
run: cargo fetch --locked --manifest-path bindings/ffi/Cargo.toml
|
||||
|
||||
- name: Test FFI
|
||||
run: |
|
||||
cargo fetch
|
||||
cargo build -r --frozen
|
||||
cargo clippy --all-targets --no-deps -- -Dwarnings
|
||||
working-directory: ./bindings/ffi
|
||||
run: cargo xtask test-ffi --release --frozen
|
||||
|
||||
29
.github/workflows/test-go.yml
vendored
29
.github/workflows/test-go.yml
vendored
@@ -14,22 +14,23 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
|
||||
- uses: actions/setup-go@v5
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Fetch FFI crate dependencies
|
||||
run: cargo fetch --locked --manifest-path bindings/ffi/Cargo.toml
|
||||
|
||||
- uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
|
||||
with:
|
||||
architecture: x64
|
||||
|
||||
- name: Build ffi
|
||||
run: cargo build -r
|
||||
working-directory: ./bindings/ffi
|
||||
|
||||
- name: Test go
|
||||
run: |
|
||||
go mod tidy
|
||||
go build
|
||||
LD_LIBRARY_PATH=../ffi/target/release ./regorus_test
|
||||
working-directory: ./bindings/go
|
||||
- name: Test Go binding via xtask
|
||||
run: cargo xtask test-go --release --frozen
|
||||
|
||||
29
.github/workflows/test-java.yml
vendored
29
.github/workflows/test-java.yml
vendored
@@ -14,27 +14,24 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- uses: actions/setup-java@v4
|
||||
- uses: actions/setup-java@dded0888837ed1f317902acf8a20df0ad188d165 # v5.0.0
|
||||
with:
|
||||
java-version: 8
|
||||
distribution: "corretto"
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Building binding
|
||||
run: |
|
||||
cargo clippy --all-targets --no-deps -- -Dwarnings
|
||||
cargo build --release --manifest-path bindings/java/Cargo.toml --locked
|
||||
- name: Fetch Java crate dependencies
|
||||
run: cargo fetch --locked --manifest-path bindings/java/Cargo.toml
|
||||
|
||||
- name: Build jar
|
||||
run: mvn package
|
||||
working-directory: ./bindings/java
|
||||
|
||||
- name: Test jar
|
||||
run: |
|
||||
javac -cp target/regorus-java-0.2.2.jar Test.java
|
||||
java -Djava.library.path=target/release -cp target/regorus-java-0.2.2.jar:. Test
|
||||
working-directory: ./bindings/java
|
||||
- name: Run Java smoke tests via xtask
|
||||
run: cargo xtask test-java --release --frozen
|
||||
|
||||
30
.github/workflows/test-musl.yml
vendored
30
.github/workflows/test-musl.yml
vendored
@@ -18,21 +18,19 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Add musl target
|
||||
run: rustup target add x86_64-unknown-linux-musl
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
with:
|
||||
targets: x86_64-unknown-linux-musl
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
- name: Fetch MUSL target dependencies
|
||||
run: cargo fetch --locked --target x86_64-unknown-linux-musl
|
||||
- name: Install musl-gcc
|
||||
run: sudo apt update && sudo apt install -y musl-tools
|
||||
- name: Fetch
|
||||
run: cargo fetch
|
||||
- name: Build (MUSL)
|
||||
run: cargo build --verbose --all-targets --target x86_64-unknown-linux-musl --frozen
|
||||
- name: Run tests (MUSL)
|
||||
run: cargo test -r --verbose --target x86_64-unknown-linux-musl --frozen
|
||||
- name: Run tests (MUSL ACI)
|
||||
run: cargo test -r --test aci --target x86_64-unknown-linux-musl --frozen
|
||||
- name: Run tests (KATA ACI)
|
||||
run: cargo test -r --test kata --target x86_64-unknown-linux-musl --frozen
|
||||
- name: Run tests (MUSL OPA Conformance)
|
||||
run: >-
|
||||
cargo test -r --test opa --frozen --features opa-testutil,serde_json/arbitrary_precision --target x86_64-unknown-linux-musl -- $(tr '\n' ' ' < tests/opa.passing)
|
||||
- name: Run MUSL suite via xtask
|
||||
run: cargo xtask test-musl --release --frozen --target x86_64-unknown-linux-musl
|
||||
|
||||
22
.github/workflows/test-no-std.yml
vendored
22
.github/workflows/test-no-std.yml
vendored
@@ -18,12 +18,18 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Add no_std target
|
||||
run: rustup target add thumbv7m-none-eabi
|
||||
- name: Fetch
|
||||
run: cargo fetch
|
||||
- name: Build
|
||||
run: cargo build -r --target thumbv7m-none-eabi --frozen
|
||||
working-directory: ./tests/ensure_no_std
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
with:
|
||||
targets: thumbv7m-none-eabi
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
- name: Fetch ensure_no_std crate dependencies
|
||||
run: cargo fetch --locked --manifest-path tests/ensure_no_std/Cargo.toml --target thumbv7m-none-eabi
|
||||
- name: Test no-std
|
||||
run: cargo xtask test-no-std --release --frozen
|
||||
|
||||
|
||||
87
.github/workflows/test-python.yml
vendored
87
.github/workflows/test-python.yml
vendored
@@ -9,9 +9,6 @@ on:
|
||||
# Run at 8:00 AM every day
|
||||
- cron: "0 8 * * *"
|
||||
|
||||
env:
|
||||
PYTHON_VERSION: "3.10"
|
||||
|
||||
jobs:
|
||||
build:
|
||||
strategy:
|
||||
@@ -24,69 +21,69 @@ jobs:
|
||||
runs-on: ${{ matrix.host.name }}
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- uses: actions/setup-python@v4
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
with:
|
||||
python-version: ${{ env.PYTHON_VERSION }}
|
||||
targets: ${{ matrix.host.target }}
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Fetch Python crate dependencies
|
||||
run: cargo fetch --locked --manifest-path bindings/python/Cargo.toml --target ${{ matrix.host.target }}
|
||||
|
||||
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
||||
with:
|
||||
python-version: "3.10"
|
||||
architecture: x64
|
||||
|
||||
- name: Build Python extension
|
||||
run: |
|
||||
cargo fetch
|
||||
cargo clippy --all-targets --no-deps -- -Dwarnings
|
||||
cargo build --release --target ${{ matrix.host.target }} --frozen
|
||||
working-directory: bindings/python
|
||||
- name: Install maturin
|
||||
run: python -m pip install maturin==1.5.1
|
||||
|
||||
- name: Build Wheel
|
||||
uses: PyO3/maturin-action@63b75c597b83e247fbf4fb7719801cc4220ae9f3 # v1.43.0
|
||||
with:
|
||||
target: x86_64
|
||||
args: --release --out dist --manifest-path bindings/python/Cargo.toml --offline --strip
|
||||
sccache: 'true'
|
||||
- name: Build Python wheel via xtask
|
||||
run: cargo xtask build-python --release --target ${{ matrix.host.target }} --target-dir bindings/python/dist --frozen
|
||||
|
||||
- name: Upload Wheel
|
||||
uses: actions/upload-artifact@v4
|
||||
- name: Upload wheel artefacts
|
||||
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
|
||||
with:
|
||||
name: regorus-wheel-${{ matrix.host.name }}
|
||||
path: dist/regorus-*.whl
|
||||
path: bindings/python/dist/regorus-*.whl
|
||||
|
||||
test:
|
||||
needs: build
|
||||
strategy:
|
||||
matrix:
|
||||
host: [ubuntu-24.04, ubuntu-22.04, windows-latest]
|
||||
python-version: ["3.10", "3.11", "3.12", "3.13"]
|
||||
host:
|
||||
- name: ubuntu-24.04
|
||||
wheel: regorus-0.5.0-cp310-abi3-manylinux_2_34_x86_64.whl
|
||||
- name: ubuntu-22.04
|
||||
wheel: regorus-0.5.0-cp310-abi3-manylinux_2_34_x86_64.whl
|
||||
- name: windows-latest
|
||||
wheel: regorus-0.5.0-cp310-abi3-win_amd64.whl
|
||||
|
||||
needs: build
|
||||
runs-on: ${{ matrix.host.name }}
|
||||
runs-on: ${{ matrix.host }}
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Download Regorus wheel
|
||||
uses: actions/download-artifact@v4
|
||||
- uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
path: wheels
|
||||
pattern: regorus-wheel-*
|
||||
merge-multiple: true
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- uses: actions/setup-python@v4
|
||||
- name: Fetch Python crate dependencies
|
||||
run: cargo fetch --locked --manifest-path bindings/python/Cargo.toml
|
||||
|
||||
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
||||
with:
|
||||
python-version: ${{ matrix.python-version }}
|
||||
architecture: x64
|
||||
|
||||
- name: Test Wheel
|
||||
run: |
|
||||
pip3 install ../../wheels/${{ matrix.host.wheel }}
|
||||
python3 test.py
|
||||
working-directory: bindings/python
|
||||
- name: Install maturin
|
||||
run: python -m pip install maturin==1.5.1
|
||||
|
||||
- name: Run Python smoke tests via xtask
|
||||
run: cargo xtask test-python --release --python python
|
||||
|
||||
21
.github/workflows/test-ruby.yml
vendored
21
.github/workflows/test-ruby.yml
vendored
@@ -8,10 +8,11 @@ on:
|
||||
|
||||
jobs:
|
||||
test:
|
||||
if: false # temporarily disabled
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
@@ -26,10 +27,16 @@ jobs:
|
||||
cargo-cache: true
|
||||
working-directory: "bindings/ruby"
|
||||
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Fetch Ruby crate dependencies
|
||||
run: cargo fetch --locked --manifest-path bindings/ruby/Cargo.toml
|
||||
|
||||
- name: Run ruby tests
|
||||
run: |
|
||||
cd bindings/ruby
|
||||
gem install bundler
|
||||
bundle install
|
||||
cargo clippy --all-targets --no-deps -- -Dwarnings
|
||||
bundle exec rake
|
||||
run: cargo xtask test-ruby --release --frozen
|
||||
|
||||
32
.github/workflows/test-wasm.yml
vendored
32
.github/workflows/test-wasm.yml
vendored
@@ -14,25 +14,29 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Setup Node
|
||||
uses: actions/setup-node@v4
|
||||
- name: Setup Rust toolchain
|
||||
uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
node-version: 18
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
|
||||
- name: Fetch WASM crate dependencies
|
||||
run: cargo fetch --locked --manifest-path bindings/wasm/Cargo.toml
|
||||
|
||||
- name: Setup Node
|
||||
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
|
||||
with:
|
||||
node-version: 22
|
||||
|
||||
- name: Install wasmlpack
|
||||
run: cargo install wasm-pack
|
||||
|
||||
- name: Test wasm binding
|
||||
run: |
|
||||
cd bindings/wasm
|
||||
cargo fetch
|
||||
cargo clippy --all-targets --no-deps -- -Dwarnings
|
||||
wasm-pack build --target nodejs --release
|
||||
# Enable when upstream issue is fixed.
|
||||
# https://github.com/microsoft/regorus/issues/371
|
||||
# wasm-pack test --release --node
|
||||
node test.js
|
||||
- name: Test wasm binding via xtask
|
||||
run: cargo xtask test-wasm --release --frozen --node node
|
||||
|
||||
33
.github/workflows/tests-debug.yml
vendored
33
.github/workflows/tests-debug.yml
vendored
@@ -18,25 +18,14 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: Fetch
|
||||
run: cargo fetch
|
||||
- name: Build (all features)
|
||||
run: cargo build --all-features --frozen
|
||||
- name: Build
|
||||
run: cargo build --frozen
|
||||
- name: Test no_std
|
||||
run: cargo test --no-default-features --frozen
|
||||
- name: Build only std
|
||||
run: cargo build --example regorus --no-default-features --features "std" --frozen
|
||||
- name: Doc Tests
|
||||
run: cargo test --doc --frozen
|
||||
- name: Run tests
|
||||
run: cargo test --frozen
|
||||
- name: Run tests (ACI)
|
||||
run: cargo test --test aci --frozen
|
||||
- name: Run tests (KATA)
|
||||
run: cargo test --test kata --frozen
|
||||
- name: Run tests (OPA Conformance)
|
||||
run: >-
|
||||
cargo test --test opa --frozen --features opa-testutil,serde_json/arbitrary_precision -- $(tr '\n' ' ' < tests/opa.passing)
|
||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955 # v4
|
||||
- name: Setup Rust toolchain
|
||||
uses: ./.github/actions/toolchains/rust
|
||||
- name: Cache cargo
|
||||
uses: Swatinem/rust-cache@779680da715d629ac1d338a641029a2f4372abb5 # v2.8.2
|
||||
with:
|
||||
shared-key: ${{ runner.os }}-regorus
|
||||
- name: Fetch dependencies
|
||||
run: cargo fetch --locked
|
||||
- name: Run debug CI suite
|
||||
run: cargo xtask ci-debug --frozen
|
||||
|
||||
14
.gitignore
vendored
14
.gitignore
vendored
@@ -28,9 +28,23 @@ bindings/*/target
|
||||
# C# build folders
|
||||
**bin
|
||||
**obj
|
||||
bindings/csharp/.nuget/
|
||||
|
||||
# Bundler binstubs regenerated during ruby setup
|
||||
bindings/ruby/bin/
|
||||
|
||||
# Visual Studio folders
|
||||
**/*.vs
|
||||
|
||||
# Visual Studio solution files
|
||||
*.sln
|
||||
|
||||
# JetBrains IDEs files
|
||||
.idea/
|
||||
|
||||
# Java build artifacts
|
||||
**/*.class
|
||||
**/*.jar
|
||||
bindings/java/.classpath
|
||||
bindings/java/.project
|
||||
bindings/java/.settings/
|
||||
|
||||
890
Cargo.lock
generated
890
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
39
Cargo.toml
39
Cargo.toml
@@ -2,14 +2,15 @@
|
||||
|
||||
members = [
|
||||
"tests/ensure_no_std",
|
||||
"xtask",
|
||||
]
|
||||
|
||||
[package]
|
||||
name = "regorus"
|
||||
description = "A fast, lightweight Rego (OPA policy language) interpreter"
|
||||
version = "0.5.0"
|
||||
version = "0.9.0"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
license = "MIT AND Apache-2.0 AND BSD-3-Clause"
|
||||
repository = "https://github.com/microsoft/regorus"
|
||||
keywords = ["interpreter", "no_std", "opa", "policy-as-code", "rego"]
|
||||
|
||||
@@ -19,11 +20,12 @@ keywords = ["interpreter", "no_std", "opa", "policy-as-code", "rego"]
|
||||
doctest = false
|
||||
|
||||
[features]
|
||||
default = ["full-opa", "arc"]
|
||||
default = ["full-opa", "arc", "rvm"]
|
||||
|
||||
arc = ["scientific/arc"]
|
||||
arc = []
|
||||
ast = []
|
||||
azure_policy = ["dep:jsonschema", "arc", "dashmap"]
|
||||
azure-rbac = []
|
||||
base64 = ["dep:data-encoding"]
|
||||
base64url = ["dep:data-encoding"]
|
||||
coverage = []
|
||||
@@ -32,11 +34,14 @@ http = []
|
||||
glob = ["dep:globset"]
|
||||
graph = []
|
||||
jsonschema = ["dep:jsonschema"]
|
||||
net = []
|
||||
mimalloc = ["dep:mimalloc"]
|
||||
net = ["dep:ipnet"]
|
||||
no_std = ["lazy_static/spin_no_std"]
|
||||
opa-runtime = []
|
||||
regex = ["dep:regex"]
|
||||
rvm = ["dep:bincode", "dep:indexmap"]
|
||||
semver = ["dep:semver"]
|
||||
allocator-memory-limits = ["std", "mimalloc", "mimalloc/allocator-memory-limits"]
|
||||
std = ["rand/std", "rand/std_rng", "serde_json/std", "msvc_spectre_libs" ]
|
||||
time = ["dep:chrono", "dep:chrono-tz"]
|
||||
uuid = ["dep:uuid"]
|
||||
@@ -51,6 +56,8 @@ full-opa = [
|
||||
"hex",
|
||||
"http",
|
||||
"jsonschema",
|
||||
"allocator-memory-limits",
|
||||
"mimalloc",
|
||||
"net",
|
||||
"opa-runtime",
|
||||
"regex",
|
||||
@@ -90,13 +97,15 @@ rand = ["dep:rand"]
|
||||
|
||||
[dependencies]
|
||||
anyhow = { version = "1.0.45", default-features = false }
|
||||
serde = {version = "1.0.150", default-features = false, features = ["derive", "rc"] }
|
||||
serde = {version = "1.0.150", default-features = false, features = ["derive", "rc", "alloc"] }
|
||||
serde_json = { version = "1.0.89", default-features = false, features = ["alloc"] }
|
||||
lazy_static = { version = "1.4.0", default-features = false }
|
||||
thiserror = { version = "2.0", default-features = false }
|
||||
|
||||
data-encoding = { version = "2.8.0", optional = true, default-features=false, features = ["alloc"] }
|
||||
scientific = { version = "0.5.3" }
|
||||
num-bigint = { version = "0.4", default-features = false }
|
||||
num-traits = { version = "0.2", default-features = false }
|
||||
spin = { version = "0.9.8", default-features = false, features = ["mutex", "spin_mutex"] }
|
||||
|
||||
globset = { version = "0.4.16", features = ["simd-accel"], default-features = false, optional = true }
|
||||
regex = {version = "1.11.1", optional = true, default-features = false }
|
||||
@@ -106,6 +115,7 @@ uuid = { version = "1.15.1", default-features = false, features = ["v4", "fast-r
|
||||
jsonschema = { version = "0.30.0", default-features = false, optional = true }
|
||||
chrono = { version = "0.4.40", optional = true }
|
||||
chrono-tz = { version = "0.10.1", optional = true }
|
||||
ipnet = { version = "2.11.0", optional = true, default-features = false }
|
||||
|
||||
serde_yaml = {version = "0.9.16", default-features = false, optional = true }
|
||||
# Specify thread_rng for in order to use random_range
|
||||
@@ -114,16 +124,21 @@ rand = { version = "0.9.0", default-features = false, features = ["thread_rng"],
|
||||
# Causes the project to link with the Spectre-mitigated CRT and libs.
|
||||
msvc_spectre_libs = { version = "0.1", features = ["error"], optional = true }
|
||||
dashmap = { version = "6.1", default-features = false, optional = true }
|
||||
mimalloc = { package = "regorus-mimalloc", path = "mimalloc", version = "2.2.6", optional = true }
|
||||
|
||||
# rvm related deps
|
||||
indexmap = { version = "2.12.1", default-features = false, features = ["serde"], optional = true }
|
||||
bincode = { version = "2.0.1", default-features = false, features = ["alloc", "serde"], optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
anyhow = "1.0.45"
|
||||
cfg-if = "1.0.0"
|
||||
clap = { version = "4.5.45", features = ["derive"] }
|
||||
prettydiff = { version = "0.8.0", default-features = false }
|
||||
clap = { version = "4.5.53", features = ["derive"] }
|
||||
prettydiff = { version = "0.9.0", default-features = false }
|
||||
serde_yaml = "0.9.16"
|
||||
test-generator = "0.3.1"
|
||||
walkdir = "2.3.2"
|
||||
criterion = { version = "0.7" }
|
||||
criterion = { version = "0.8" }
|
||||
|
||||
num_cpus = "1.16"
|
||||
|
||||
@@ -170,6 +185,10 @@ name = "compiled_policy_evaluation_benchmark"
|
||||
path = "benches/evaluation/compiled_policy_evaluation_benchmark.rs"
|
||||
harness = false
|
||||
|
||||
[[bench]]
|
||||
name = "aci_benchmark"
|
||||
harness = false
|
||||
|
||||
[[example]]
|
||||
name="regorus"
|
||||
harness=false
|
||||
|
||||
235
LICENSE
235
LICENSE
@@ -19,3 +19,238 @@
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE
|
||||
|
||||
The file src/builtins/time/diff.rs contains code derived from Go's `time`
|
||||
package, which carries the following license:
|
||||
|
||||
Copyright (c) 2009 The Go Authors. All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
* Neither the name of Google Inc. nor the names of its
|
||||
contributors may be used to endorse or promote products derived from
|
||||
this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
Some files are licensed Apache-2.0 (LICENSE-2.0.txt).
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
18
README.md
18
README.md
@@ -3,7 +3,7 @@
|
||||
**Regorus** is
|
||||
|
||||
- *Rego*-*Rus(t)* - A fast, light-weight [Rego](https://www.openpolicyagent.org/docs/latest/policy-language/)
|
||||
interpreter written in Rust.
|
||||
interpreter written in Rust.
|
||||
- *Rigorous* - A rigorous enforcer of well-defined Rego semantics.
|
||||
|
||||
Regorus is also
|
||||
@@ -274,6 +274,19 @@ Benchmark 1: opa eval -b tests/aci -d tests/aci/data.json -i tests/aci/input.jso
|
||||
Range (min … max): 43.8 ms … 46.7 ms 62 runs
|
||||
|
||||
```
|
||||
|
||||
## Contributor Workflow
|
||||
|
||||
Regorus uses a small companion CLI under the `xtask` package to keep CI and local development in sync.
|
||||
The commands mirror our GitHub Actions jobs, making it easy to dry-run CI steps before sending a pull request.
|
||||
|
||||
- Run the full release pipeline with `cargo xtask ci-release` and the debug checks with `cargo xtask ci-debug`.
|
||||
- Exercise language bindings through focused helpers such as `cargo xtask test-java --release --frozen` or `cargo xtask test-go`.
|
||||
- Use `cargo xtask test-musl --release --frozen` for the cross-compilation matrix and `cargo xtask test-no-std` for embedded targets.
|
||||
- Formatting (`cargo xtask fmt`) and linting (`cargo xtask clippy --sarif`) wrap the usual Cargo tooling while matching CI defaults.
|
||||
|
||||
The workflows in `.github/workflows` invoke the same commands, so keeping local runs green is usually enough to satisfy the checks enforced on `main`.
|
||||
|
||||
## OPA Conformance
|
||||
|
||||
Regorus has been verified to be compliant with [OPA v1.2.0](https://github.com/open-policy-agent/opa/releases/tag/v1.2.0)
|
||||
@@ -303,11 +316,8 @@ The following test suites don't pass fully due to missing builtins:
|
||||
- `jwtverifyhs384`
|
||||
- `jwtverifyhs512`
|
||||
- `jwtverifyrsa`
|
||||
- `netcidrcontains`
|
||||
- `netcidrcontainsmatches`
|
||||
- `netcidrexpand`
|
||||
- `netcidrintersects`
|
||||
- `netcidrisvalid`
|
||||
- `netcidrmerge`
|
||||
- `netcidroverlap`
|
||||
- `netlookupipaddr`
|
||||
|
||||
80
benches/aci_benchmark.rs
Normal file
80
benches/aci_benchmark.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
use regorus::{Engine, Value};
|
||||
|
||||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
struct TestCase {
|
||||
note: String,
|
||||
data: Value,
|
||||
input: Value,
|
||||
modules: Vec<String>,
|
||||
query: String,
|
||||
want_result: Value,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
struct YamlTest {
|
||||
cases: Vec<TestCase>,
|
||||
}
|
||||
|
||||
fn aci_policy_eval(c: &mut Criterion) {
|
||||
let dir = Path::new("tests/aci");
|
||||
for entry in WalkDir::new(dir)
|
||||
.sort_by_file_name()
|
||||
.into_iter()
|
||||
.filter_map(|e| e.ok())
|
||||
{
|
||||
let path = entry.path();
|
||||
if !path.to_string_lossy().ends_with(".yaml") {
|
||||
continue;
|
||||
}
|
||||
|
||||
let yaml = std::fs::read(path).expect("failed to read yaml test");
|
||||
let yaml = String::from_utf8_lossy(&yaml);
|
||||
let test: YamlTest = serde_yaml::from_str(&yaml).expect("failed to deserialize yaml test");
|
||||
|
||||
for case in &test.cases {
|
||||
let rule = case.query.replace("=x", "");
|
||||
c.bench_with_input(
|
||||
BenchmarkId::new("case ", format!("{} {}", &case.note, &rule)),
|
||||
&case,
|
||||
|b, case| {
|
||||
let mut engine = Engine::new();
|
||||
engine.set_rego_v0(true);
|
||||
|
||||
engine
|
||||
.add_data(case.data.clone())
|
||||
.expect("failed to add data");
|
||||
engine.set_input(case.input.clone());
|
||||
|
||||
for (idx, rego) in case.modules.iter().enumerate() {
|
||||
if rego.ends_with(".rego") {
|
||||
let path = dir.join(rego);
|
||||
let path = path.to_str().expect("not a valid path");
|
||||
engine
|
||||
.add_policy_from_file(path)
|
||||
.expect("failed to add policy");
|
||||
} else {
|
||||
engine
|
||||
.add_policy(format!("rego{idx}.rego"), rego.clone())
|
||||
.expect("failed to add policy");
|
||||
}
|
||||
}
|
||||
|
||||
b.iter(|| {
|
||||
engine.eval_rule(rule.clone()).unwrap();
|
||||
})
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
criterion_group!(aci_benches, aci_policy_eval);
|
||||
criterion_main!(aci_benches);
|
||||
@@ -5,6 +5,7 @@
|
||||
- **CPU**: 16 cores
|
||||
- **Architecture**: ARM64 (aarch64-apple-darwin)
|
||||
- **Rust Version**: 1.82.0
|
||||
- **Allocator**: mimalloc (default allocator)
|
||||
- **Benchmark Framework**: Criterion.rs
|
||||
- **Test Data**: 20,000 inputs per evaluation (1000 per thread)
|
||||
- **Policy**: Complex authorization policy with nested rules
|
||||
@@ -25,111 +26,135 @@ The compiled policy evaluation benchmark tests Regorus compiled policy performan
|
||||
### Compiled Shared Policies, Cloned Inputs (Best Performance)
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 3.30 | 303 |
|
||||
| 2 | 8.53 | 234 |
|
||||
| 4 | 18.78 | 213 |
|
||||
| 6 | 32.35 | 186 |
|
||||
| 8 | 73.12 | 109 |
|
||||
| 10 | 108.97 | 92 |
|
||||
| 12 | 145.56 | 82 |
|
||||
| 14 | 196.14 | 71 |
|
||||
| 16 | 248.77 | 64 |
|
||||
| 18 | 290.01 | 62 |
|
||||
| 20 | 317.16 | 63 |
|
||||
| 22 | 348.83 | 63 |
|
||||
| 24 | 361.05 | 66 |
|
||||
| 26 | 389.70 | 67 |
|
||||
| 28 | 418.66 | 67 |
|
||||
| 30 | 444.40 | 68 |
|
||||
| 32 | 476.53 | 67 |
|
||||
| 1 | 2.35 | 426 |
|
||||
| 2 | 5.36 | 373 |
|
||||
| 4 | 11.70 | 342 |
|
||||
| 6 | 20.33 | 295 |
|
||||
| 8 | 43.26 | 185 |
|
||||
| 10 | 61.93 | 162 |
|
||||
| 12 | 79.30 | 151 |
|
||||
| 14 | 94.45 | 148 |
|
||||
| 16 | 113.39 | 141 |
|
||||
| 18 | 154.41 | 117 |
|
||||
| 20 | 184.37 | 108 |
|
||||
| 22 | 204.00 | 108 |
|
||||
| 24 | 220.45 | 109 |
|
||||
| 26 | 237.07 | 110 |
|
||||
| 28 | 252.58 | 111 |
|
||||
| 30 | 273.57 | 110 |
|
||||
| 32 | 292.69 | 109 |
|
||||
|
||||
### Compiled Shared Policies, Fresh Inputs
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 4.51 | 222 |
|
||||
| 2 | 9.77 | 205 |
|
||||
| 4 | 23.36 | 171 |
|
||||
| 6 | 38.12 | 157 |
|
||||
| 8 | 85.02 | 94 |
|
||||
| 10 | 133.66 | 75 |
|
||||
| 12 | 180.46 | 66 |
|
||||
| 14 | 238.23 | 59 |
|
||||
| 16 | 318.78 | 50 |
|
||||
| 18 | 353.15 | 51 |
|
||||
| 20 | 389.29 | 51 |
|
||||
| 22 | 459.61 | 48 |
|
||||
| 24 | 507.62 | 47 |
|
||||
| 26 | 539.43 | 48 |
|
||||
| 28 | 554.99 | 50 |
|
||||
| 30 | 625.57 | 48 |
|
||||
| 32 | 690.55 | 46 |
|
||||
| 1 | 3.34 | 299 |
|
||||
| 2 | 7.29 | 274 |
|
||||
| 4 | 15.19 | 263 |
|
||||
| 6 | 24.90 | 241 |
|
||||
| 8 | 49.22 | 163 |
|
||||
| 10 | 68.45 | 146 |
|
||||
| 12 | 86.55 | 139 |
|
||||
| 14 | 104.77 | 134 |
|
||||
| 16 | 136.07 | 118 |
|
||||
| 18 | 169.05 | 106 |
|
||||
| 20 | 198.25 | 101 |
|
||||
| 22 | 217.05 | 101 |
|
||||
| 24 | 234.75 | 102 |
|
||||
| 26 | 254.53 | 102 |
|
||||
| 28 | 276.06 | 101 |
|
||||
| 30 | 296.12 | 101 |
|
||||
| 32 | 318.81 | 100 |
|
||||
|
||||
### Compiled Per Iteration, Cloned Inputs
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 22.68 | 44 |
|
||||
| 2 | 47.99 | 42 |
|
||||
| 4 | 108.09 | 37 |
|
||||
| 6 | 167.62 | 36 |
|
||||
| 8 | 283.17 | 28 |
|
||||
| 10 | 418.25 | 24 |
|
||||
| 12 | 546.24 | 22 |
|
||||
| 14 | 688.79 | 20 |
|
||||
| 16 | 951.72 | 17 |
|
||||
| 18 | 1060.20 | 17 |
|
||||
| 20 | 1223.60 | 16 |
|
||||
| 22 | 1342.50 | 16 |
|
||||
| 24 | 1445.70 | 17 |
|
||||
| 26 | 1676.50 | 15 |
|
||||
| 28 | 1765.20 | 16 |
|
||||
| 30 | 1939.00 | 15 |
|
||||
| 32 | 2197.30 | 15 |
|
||||
| 1 | 18.11 | 55 |
|
||||
| 2 | 36.89 | 54 |
|
||||
| 4 | 75.46 | 53 |
|
||||
| 6 | 114.66 | 52 |
|
||||
| 8 | 152.80 | 52 |
|
||||
| 10 | 192.17 | 52 |
|
||||
| 12 | 232.32 | 52 |
|
||||
| 14 | 301.47 | 46 |
|
||||
| 16 | 380.36 | 42 |
|
||||
| 18 | 424.64 | 42 |
|
||||
| 20 | 484.76 | 41 |
|
||||
| 22 | 531.62 | 41 |
|
||||
| 24 | 582.88 | 41 |
|
||||
| 26 | 631.39 | 41 |
|
||||
| 28 | 671.99 | 42 |
|
||||
| 30 | 717.65 | 42 |
|
||||
| 32 | 766.05 | 42 |
|
||||
|
||||
### Compiled Per Iteration, Fresh Inputs
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 23.95 | 42 |
|
||||
| 2 | 49.53 | 40 |
|
||||
| 4 | 116.42 | 34 |
|
||||
| 6 | 197.35 | 30 |
|
||||
| 8 | 293.04 | 27 |
|
||||
| 10 | 385.90 | 26 |
|
||||
| 12 | 508.82 | 24 |
|
||||
| 14 | 679.23 | 21 |
|
||||
| 16 | 913.02 | 18 |
|
||||
| 18 | 1075.90 | 17 |
|
||||
| 20 | 1209.80 | 17 |
|
||||
| 22 | 1358.90 | 16 |
|
||||
| 24 | 1523.90 | 16 |
|
||||
| 26 | 1700.20 | 15 |
|
||||
| 28 | 1966.90 | 14 |
|
||||
| 30 | 2179.30 | 14 |
|
||||
| 32 | 2327.70 | 14 |
|
||||
| 1 | 19.07 | 52 |
|
||||
| 2 | 38.89 | 51 |
|
||||
| 4 | 79.52 | 50 |
|
||||
| 6 | 120.89 | 50 |
|
||||
| 8 | 161.08 | 50 |
|
||||
| 10 | 202.37 | 49 |
|
||||
| 12 | 244.04 | 49 |
|
||||
| 14 | 316.66 | 44 |
|
||||
| 16 | 398.02 | 40 |
|
||||
| 18 | 449.54 | 40 |
|
||||
| 20 | 500.57 | 40 |
|
||||
| 22 | 557.97 | 39 |
|
||||
| 24 | 605.71 | 40 |
|
||||
| 26 | 656.88 | 40 |
|
||||
| 28 | 710.03 | 39 |
|
||||
| 30 | 741.09 | 40 |
|
||||
| 32 | 801.26 | 40 |
|
||||
|
||||
## Analysis
|
||||
|
||||
The compiled policy benchmark demonstrates the following performance characteristics:
|
||||
The compiled policy benchmark demonstrates the following performance characteristics with mimalloc as the default allocator:
|
||||
|
||||
1. **Best Performance**: Compiled shared policies with cloned inputs provide the highest throughput
|
||||
2. **Compilation Impact**:
|
||||
- Pre-compiled policies: Significantly faster than per-iteration compilation
|
||||
- Per-iteration compilation: Major overhead (~7x slower than pre-compiled)
|
||||
3. **Scaling Patterns**:
|
||||
- Per-iteration compilation: Major overhead (~7-8x slower than pre-compiled)
|
||||
3. **Scaling Patterns with mimalloc**:
|
||||
- Best throughput achieved at 1 thread for shared policy configurations
|
||||
- Higher thread counts show performance degradation due to contention
|
||||
- mimalloc provides better thread scaling characteristics compared to the default allocator
|
||||
- Higher thread counts show performance degradation due to contention, but less severe with mimalloc
|
||||
- Per-iteration compilation shows poor scaling across all thread counts
|
||||
4. **Input Processing**: Fresh inputs add ~25-30% overhead across all configurations
|
||||
5. **Thread Performance**:
|
||||
4. **Input Processing**: Fresh inputs add ~30% overhead across all configurations
|
||||
5. **Thread Performance with mimalloc**:
|
||||
- Peak performance at 1 thread for most configurations
|
||||
- Reasonable performance maintained up to 12-16 threads for shared policies
|
||||
- Compiled policies show better thread scaling than per-iteration compilation
|
||||
- mimalloc helps reduce allocation-related contention in multi-threaded scenarios
|
||||
|
||||
## Comparison with Engine Evaluation
|
||||
|
||||
| Configuration | Compiled Policy (1 thread) | Engine Evaluation (1 thread) | Performance Ratio |
|
||||
|:---------------------|:--------------------------------|:--------------------------------|------------------:|
|
||||
| Shared/Cloned | Best performance | Higher throughput | 0.67x-0.92x |
|
||||
| Shared/Fresh | ~27% reduction from optimal | ~30% reduction from optimal | 0.62x-0.97x |
|
||||
| Per-iteration/Cloned | ~85% reduction from optimal | ~86% reduction from optimal | 0.80x-0.98x |
|
||||
| Per-iteration/Fresh | ~86% reduction from optimal | ~87% reduction from optimal | 0.78x-1.00x |
|
||||
### Multi-Thread Performance Comparison
|
||||
|
||||
| Configuration | 1 Thread (Kelem/s) | 4 Threads (Kelem/s) | 8 Threads (Kelem/s) |
|
||||
|:---------------------|:-------------------|:--------------------|:--------------------|
|
||||
| | CP / EE | CP / EE | CP / EE |
|
||||
| Shared/Cloned | 426 / 423 | 342 / 406 | 185 / 341 |
|
||||
| Shared/Fresh | 299 / 309 | 263 / 297 | 163 / 266 |
|
||||
| Per-iteration/Cloned | 55 / 56 | 53 / 54 | 52 / 53 |
|
||||
| Per-iteration/Fresh | 52 / 53 | 50 / 51 | 50 / 51 |
|
||||
|
||||
### Threading Efficiency Analysis
|
||||
|
||||
| Configuration | Low Contention (1-4t) | Medium Contention (6-12t) | High Contention (16+t) |
|
||||
|:---------------------|:----------------------|:--------------------------|:-----------------------|
|
||||
| | Avg CP / EE | Avg CP / EE | Avg CP / EE |
|
||||
| Shared/Cloned | 384 / 414 | 203 / 329 | 123 / 250 |
|
||||
| Shared/Fresh | 284 / 302 | 176 / 235 | 108 / 201 |
|
||||
| Per-iteration/Cloned | 54 / 55 | 50 / 52 | 42 / 42 |
|
||||
| Per-iteration/Fresh | 51 / 52 | 47 / 50 | 40 / 40 |
|
||||
|
||||
The compiled policy evaluation shows performance characteristics that are generally comparable to engine evaluation, though with some notable differences. While single-threaded performance is very close between the systems, there are observable impacts from the compilation approach that become more apparent under different threading scenarios.
|
||||
|
||||
**Key Observations:**
|
||||
- **Single-threaded performance**: Very close parity between systems, though results may vary between runs
|
||||
- **Threading behavior**: Engine evaluation demonstrates better scaling characteristics under higher thread contention (4+ threads)
|
||||
- **Multi-threaded impact**: Compiled policies show more pronounced performance degradation under thread contention in shared policy configurations
|
||||
- **Contention resistance**: Per-iteration compilation shows more consistent (though lower absolute) performance across thread counts
|
||||
- **Optimal usage**: Both systems achieve best results with minimal threading (1-4 threads), though engine evaluation maintains better performance at higher thread counts for shared configurations
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
- **CPU**: 16 cores
|
||||
- **Architecture**: ARM64 (aarch64-apple-darwin)
|
||||
- **Rust Version**: 1.82.0
|
||||
- **Allocator**: mimalloc (default allocator)
|
||||
- **Benchmark Framework**: Criterion.rs
|
||||
- **Test Data**: 20,000 inputs per evaluation (1000 per thread)
|
||||
- **Policy**: Complex authorization policy with nested rules
|
||||
@@ -25,101 +26,102 @@ The engine evaluation benchmark tests Regorus policy evaluation performance acro
|
||||
### Cloned Engines, Cloned Inputs (Best Performance)
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 3.05 | 328 |
|
||||
| 2 | 7.46 | 268 |
|
||||
| 4 | 16.10 | 248 |
|
||||
| 6 | 25.94 | 231 |
|
||||
| 8 | 50.18 | 159 |
|
||||
| 10 | 80.27 | 125 |
|
||||
| 12 | 106.31 | 113 |
|
||||
| 14 | 137.31 | 102 |
|
||||
| 16 | 163.91 | 98 |
|
||||
| 18 | 182.06 | 99 |
|
||||
| 20 | 191.36 | 105 |
|
||||
| 22 | 201.51 | 109 |
|
||||
| 24 | 217.65 | 110 |
|
||||
| 26 | 228.11 | 114 |
|
||||
| 28 | 248.17 | 113 |
|
||||
| 30 | 264.15 | 114 |
|
||||
| 32 | 314.27 | 102 |
|
||||
| 1 | 2.36 | 423 |
|
||||
| 2 | 4.85 | 412 |
|
||||
| 4 | 9.86 | 406 |
|
||||
| 6 | 15.02 | 399 |
|
||||
| 8 | 23.46 | 341 |
|
||||
| 10 | 33.34 | 300 |
|
||||
| 12 | 40.69 | 295 |
|
||||
| 14 | 48.26 | 290 |
|
||||
| 16 | 58.61 | 273 |
|
||||
| 18 | 77.35 | 233 |
|
||||
| 20 | 86.74 | 231 |
|
||||
| 22 | 94.17 | 234 |
|
||||
| 24 | 102.58 | 234 |
|
||||
| 26 | 110.17 | 236 |
|
||||
| 28 | 118.97 | 235 |
|
||||
| 30 | 126.54 | 237 |
|
||||
| 32 | 135.89 | 235 |
|
||||
|
||||
### Cloned Engines, Fresh Inputs
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 4.36 | 229 |
|
||||
| 2 | 10.34 | 194 |
|
||||
| 4 | 21.98 | 182 |
|
||||
| 6 | 34.05 | 176 |
|
||||
| 8 | 66.47 | 120 |
|
||||
| 10 | 100.78 | 99 |
|
||||
| 12 | 141.69 | 85 |
|
||||
| 14 | 188.53 | 74 |
|
||||
| 16 | 261.27 | 61 |
|
||||
| 18 | 285.29 | 63 |
|
||||
| 20 | 312.14 | 64 |
|
||||
| 22 | 329.42 | 67 |
|
||||
| 24 | 347.97 | 69 |
|
||||
| 26 | 370.24 | 70 |
|
||||
| 28 | 394.75 | 71 |
|
||||
| 30 | 419.30 | 72 |
|
||||
| 32 | 433.58 | 74 |
|
||||
| 1 | 3.24 | 309 |
|
||||
| 2 | 6.57 | 304 |
|
||||
| 4 | 13.47 | 297 |
|
||||
| 6 | 20.42 | 294 |
|
||||
| 8 | 30.01 | 266 |
|
||||
| 10 | 40.99 | 244 |
|
||||
| 12 | 49.99 | 240 |
|
||||
| 14 | 60.09 | 233 |
|
||||
| 16 | 73.95 | 216 |
|
||||
| 18 | 95.94 | 188 |
|
||||
| 20 | 105.24 | 190 |
|
||||
| 22 | 114.30 | 192 |
|
||||
| 24 | 124.67 | 193 |
|
||||
| 26 | 134.76 | 193 |
|
||||
| 28 | 145.16 | 193 |
|
||||
| 30 | 155.23 | 193 |
|
||||
| 32 | 165.42 | 193 |
|
||||
|
||||
### Fresh Engines, Cloned Inputs
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 22.39 | 45 |
|
||||
| 2 | 49.22 | 41 |
|
||||
| 4 | 98.09 | 41 |
|
||||
| 6 | 160.21 | 37 |
|
||||
| 8 | 281.26 | 28 |
|
||||
| 10 | 413.61 | 24 |
|
||||
| 12 | 578.15 | 21 |
|
||||
| 14 | 746.34 | 19 |
|
||||
| 16 | 961.44 | 17 |
|
||||
| 18 | 1127.70 | 16 |
|
||||
| 20 | 1248.40 | 16 |
|
||||
| 22 | 1386.90 | 16 |
|
||||
| 24 | 1559.70 | 15 |
|
||||
| 26 | 1736.30 | 15 |
|
||||
| 28 | 1891.80 | 15 |
|
||||
| 30 | 2077.00 | 14 |
|
||||
| 32 | 2289.30 | 14 |
|
||||
| 1 | 17.88 | 56 |
|
||||
| 2 | 36.32 | 55 |
|
||||
| 4 | 74.45 | 54 |
|
||||
| 6 | 112.95 | 53 |
|
||||
| 8 | 150.24 | 53 |
|
||||
| 10 | 189.61 | 53 |
|
||||
| 12 | 228.25 | 53 |
|
||||
| 14 | 297.37 | 47 |
|
||||
| 16 | 373.61 | 43 |
|
||||
| 18 | 426.46 | 42 |
|
||||
| 20 | 477.80 | 42 |
|
||||
| 22 | 523.00 | 42 |
|
||||
| 24 | 570.74 | 42 |
|
||||
| 26 | 619.92 | 42 |
|
||||
| 28 | 670.24 | 42 |
|
||||
| 30 | 717.47 | 42 |
|
||||
| 32 | 748.25 | 43 |
|
||||
|
||||
### Fresh Engines, Fresh Inputs
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 23.63 | 42 |
|
||||
| 2 | 48.82 | 41 |
|
||||
| 4 | 102.32 | 39 |
|
||||
| 6 | 160.09 | 37 |
|
||||
| 8 | 271.21 | 29 |
|
||||
| 10 | 397.39 | 25 |
|
||||
| 12 | 489.09 | 25 |
|
||||
| 14 | 670.33 | 21 |
|
||||
| 16 | 884.83 | 18 |
|
||||
| 18 | 1044.00 | 17 |
|
||||
| 20 | 1174.20 | 17 |
|
||||
| 22 | 1330.40 | 17 |
|
||||
| 24 | 1480.90 | 16 |
|
||||
| 26 | 1679.50 | 15 |
|
||||
| 28 | 1873.90 | 15 |
|
||||
| 30 | 2070.90 | 14 |
|
||||
| 32 | 2325.40 | 14 |
|
||||
| 1 | 18.69 | 53 |
|
||||
| 2 | 38.03 | 53 |
|
||||
| 4 | 77.82 | 51 |
|
||||
| 6 | 118.30 | 51 |
|
||||
| 8 | 157.65 | 51 |
|
||||
| 10 | 197.97 | 51 |
|
||||
| 12 | 239.05 | 50 |
|
||||
| 14 | 310.06 | 45 |
|
||||
| 16 | 391.36 | 41 |
|
||||
| 18 | 441.63 | 41 |
|
||||
| 20 | 495.88 | 40 |
|
||||
| 22 | 543.69 | 40 |
|
||||
| 24 | 591.51 | 41 |
|
||||
| 26 | 645.98 | 40 |
|
||||
| 28 | 697.37 | 40 |
|
||||
| 30 | 749.37 | 40 |
|
||||
| 32 | 784.63 | 41 |
|
||||
|
||||
## Analysis
|
||||
|
||||
The benchmark results demonstrate the following performance characteristics:
|
||||
The benchmark results demonstrate the following performance characteristics with mimalloc as the default allocator:
|
||||
|
||||
1. **Best Performance**: Cloned engines with cloned inputs consistently deliver the highest throughput
|
||||
2. **Configuration Performance Hierarchy**:
|
||||
- Cloned engines, cloned inputs: Best performance (optimal configuration)
|
||||
- Cloned engines, fresh inputs: ~30% reduction from optimal
|
||||
- Fresh engines, cloned inputs: ~86% reduction from optimal
|
||||
- Cloned engines, fresh inputs: ~27% reduction from optimal
|
||||
- Fresh engines, cloned inputs: ~87% reduction from optimal
|
||||
- Fresh engines, fresh inputs: ~87% reduction from optimal
|
||||
3. **Scaling Patterns**:
|
||||
- Performance degrades with increased thread count due to contention
|
||||
3. **Scaling Patterns with mimalloc**:
|
||||
- Performance degrades with increased thread count due to contention, but mimalloc provides better thread scaling characteristics
|
||||
- Best throughput achieved at 1 thread for cloned engine configurations
|
||||
- Fresh engine configurations show poor scaling across all thread counts
|
||||
- The use of mimalloc as the default allocator has improved multi-threaded performance and reduced contention
|
||||
4. **Engine Creation Overhead**: Fresh engine creation is a significant performance bottleneck (~7-8x slower than cloned engines)
|
||||
5. **Input Processing**: Fresh input generation adds moderate overhead (~30% impact compared to cloned inputs)
|
||||
6. **Thread Contention**: Performance degradation occurs with higher thread counts across all configurations
|
||||
5. **Input Processing**: Fresh input generation adds moderate overhead (~27% impact compared to cloned inputs)
|
||||
6. **Thread Contention**: Performance degradation occurs with higher thread counts across all configurations, though mimalloc helps mitigate some allocation-related contention
|
||||
@@ -141,11 +141,46 @@ fn clone(c: &mut Criterion) {
|
||||
});
|
||||
}
|
||||
|
||||
fn aci_policy_eval(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("ACI Policy Eval");
|
||||
let rules = ["data.policy.mount_overlay", "data.policy.mount_device"];
|
||||
for rule in rules {
|
||||
group.bench_with_input(BenchmarkId::new("rule", rule), &rule, |b, rule| {
|
||||
let mut engine = Engine::new();
|
||||
engine.set_rego_v0(true);
|
||||
|
||||
engine
|
||||
.add_policy_from_file("tests/aci/api.rego")
|
||||
.expect("failed to add api.rego");
|
||||
engine
|
||||
.add_policy_from_file("tests/aci/framework.rego")
|
||||
.expect("failed to add framework.rego");
|
||||
engine
|
||||
.add_policy_from_file("tests/aci/policy.rego")
|
||||
.expect("failed to add policy.rego");
|
||||
engine
|
||||
.add_data(
|
||||
Value::from_json_file("tests/aci/data.json").expect("failed to load data.json"),
|
||||
)
|
||||
.expect("failed to add data");
|
||||
let input =
|
||||
Value::from_json_file("tests/aci/input.json").expect("failed to load input.json");
|
||||
engine.set_input(input.clone());
|
||||
engine.eval_rule(rule.to_string()).unwrap();
|
||||
b.iter(|| {
|
||||
engine.eval_rule(rule.to_string()).unwrap();
|
||||
})
|
||||
});
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group!(
|
||||
benches,
|
||||
allow_with_simple_equality,
|
||||
allow_with_simple_membership,
|
||||
clone
|
||||
clone,
|
||||
aci_policy_eval
|
||||
);
|
||||
|
||||
criterion_main!(benches);
|
||||
|
||||
@@ -269,7 +269,7 @@ fn bench_mixed_type_array(c: &mut Criterion) {
|
||||
}
|
||||
});
|
||||
let schema = Schema::from_serde_json_value(schema_json).unwrap();
|
||||
let value = Value::from(json!(["hello", 42, true, "world", 3.14, false]));
|
||||
let value = Value::from(json!(["hello", 42, true, "world", 99.5, false]));
|
||||
|
||||
c.bench_function("validate_mixed_type_array", |b| {
|
||||
b.iter(|| {
|
||||
|
||||
@@ -1,70 +1,100 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#if defined(_WIN32)
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
#include "regorus.h"
|
||||
|
||||
|
||||
// Regorus has been built for no_std and cannot access files.
|
||||
char* file_to_string(const char* file) {
|
||||
char * buffer = 0;
|
||||
char *file_to_string(const char *file)
|
||||
{
|
||||
char *buffer = 0;
|
||||
long length;
|
||||
FILE * f = fopen (file, "rb");
|
||||
FILE *f = fopen(file, "rb");
|
||||
|
||||
if (f)
|
||||
{
|
||||
fseek (f, 0, SEEK_END);
|
||||
length = ftell (f);
|
||||
fseek (f, 0, SEEK_SET);
|
||||
buffer = malloc (length + 1);
|
||||
buffer[length] = '\0';
|
||||
if (buffer)
|
||||
{
|
||||
fread (buffer, 1, length, f);
|
||||
}
|
||||
fclose (f);
|
||||
fseek(f, 0, SEEK_END);
|
||||
length = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
buffer = malloc(length + 1);
|
||||
buffer[length] = '\0';
|
||||
if (buffer)
|
||||
{
|
||||
fread(buffer, 1, length, f);
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
// If regorus is built with custom-allocator, then provide implementation.
|
||||
uint8_t* regorus_aligned_alloc(size_t alignment, size_t size) {
|
||||
return (uint8_t*) aligned_alloc(alignment, size);
|
||||
uint8_t *regorus_aligned_alloc(size_t alignment, size_t size)
|
||||
{
|
||||
// Aligned allocations must respect platform quirks: Windows offers
|
||||
// _aligned_malloc/_aligned_free, while macOS/Linux reject aligned_alloc
|
||||
// calls when size is not a multiple of alignment, so we rely on
|
||||
// posix_memalign for the no_std build.
|
||||
#if defined(_WIN32)
|
||||
return (uint8_t *)_aligned_malloc(size, alignment);
|
||||
#else
|
||||
void *ptr = NULL;
|
||||
// posix_memalign requires alignment to be at least sizeof(void*)
|
||||
// and a power of two; normalize here so small requests succeed.
|
||||
if (alignment < sizeof(void *))
|
||||
{
|
||||
alignment = sizeof(void *);
|
||||
}
|
||||
|
||||
if (posix_memalign(&ptr, alignment, size) != 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return (uint8_t *)ptr;
|
||||
#endif
|
||||
}
|
||||
|
||||
void regorus_free(uint8_t* ptr) {
|
||||
void regorus_free(uint8_t *ptr)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
_aligned_free(ptr);
|
||||
#else
|
||||
free(ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
// Create engine.
|
||||
RegorusEngine* engine = regorus_engine_new();
|
||||
RegorusEngine *engine = regorus_engine_new();
|
||||
RegorusResult r;
|
||||
char* buffer = NULL;
|
||||
char *buffer = NULL;
|
||||
|
||||
// Turn on rego v0 since policy uses v0.
|
||||
r = regorus_engine_set_rego_v0(engine, true);
|
||||
if (r.status != Ok)
|
||||
goto error;
|
||||
goto error;
|
||||
|
||||
// Load policies.
|
||||
r = regorus_engine_add_policy(engine, "framework.rego", (buffer = file_to_string("../../../tests/aci/framework.rego")));
|
||||
free(buffer);
|
||||
if (r.status != Ok)
|
||||
goto error;
|
||||
goto error;
|
||||
printf("Loaded package %s\n", r.output);
|
||||
regorus_result_drop(r);
|
||||
|
||||
r = regorus_engine_add_policy(engine, "api.rego", (buffer = file_to_string("../../../tests/aci/api.rego")));
|
||||
free(buffer);
|
||||
if (r.status != Ok)
|
||||
goto error;
|
||||
goto error;
|
||||
printf("Loaded package %s\n", r.output);
|
||||
regorus_result_drop(r);
|
||||
|
||||
r = regorus_engine_add_policy(engine, "policy.rego", (buffer = file_to_string("../../../tests/aci/policy.rego")));
|
||||
free(buffer);
|
||||
if (r.status != Ok)
|
||||
goto error;
|
||||
goto error;
|
||||
printf("Loaded package %s\n", r.output);
|
||||
regorus_result_drop(r);
|
||||
|
||||
@@ -72,26 +102,25 @@ int main() {
|
||||
r = regorus_engine_add_data_json(engine, (buffer = file_to_string("../../../tests/aci/data.json")));
|
||||
free(buffer);
|
||||
if (r.status != Ok)
|
||||
goto error;
|
||||
goto error;
|
||||
regorus_result_drop(r);
|
||||
|
||||
// Set input
|
||||
r = regorus_engine_set_input_json(engine, (buffer = file_to_string("../../../tests/aci/input.json")));
|
||||
free(buffer);
|
||||
if (r.status != Ok)
|
||||
goto error;
|
||||
goto error;
|
||||
regorus_result_drop(r);
|
||||
|
||||
// Eval rule.
|
||||
r = regorus_engine_eval_rule(engine, "data.framework.mount_overlay");
|
||||
if (r.status != Ok)
|
||||
goto error;
|
||||
goto error;
|
||||
|
||||
// Print output
|
||||
printf("%s", r.output);
|
||||
regorus_result_drop(r);
|
||||
|
||||
|
||||
// Free the engine.
|
||||
regorus_engine_drop(engine);
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ FetchContent_Declare(
|
||||
FetchContent_MakeAvailable(Corrosion)
|
||||
|
||||
project("regorus-test")
|
||||
enable_testing()
|
||||
|
||||
corrosion_import_crate(
|
||||
# Path to <regorus-source-folder>/bindings/ffi/Cargo.toml
|
||||
@@ -35,3 +36,10 @@ add_executable(regorus_test main.c)
|
||||
# Add path to <regorus-source-folder>/bindings/ffi
|
||||
target_include_directories(regorus_test PRIVATE "../ffi")
|
||||
target_link_libraries(regorus_test regorus_ffi)
|
||||
|
||||
add_executable(regorus_rvm_test rvm_tests.c)
|
||||
target_include_directories(regorus_rvm_test PRIVATE "../ffi")
|
||||
target_link_libraries(regorus_rvm_test regorus_ffi)
|
||||
|
||||
add_test(NAME regorus_c_engine COMMAND regorus_test)
|
||||
add_test(NAME regorus_c_rvm COMMAND regorus_rvm_test)
|
||||
|
||||
289
bindings/c/rvm_tests.c
Normal file
289
bindings/c/rvm_tests.c
Normal file
@@ -0,0 +1,289 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "regorus.h"
|
||||
|
||||
static int assert_ok(RegorusResult r, const char* message) {
|
||||
if (r.status != Ok) {
|
||||
fprintf(stderr, "%s: %s\n", message, r.error_message ? r.error_message : "(no error)");
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
RegorusResult result = {0};
|
||||
bool result_valid = false;
|
||||
RegorusProgram* program = NULL;
|
||||
RegorusBuffer* buffer = NULL;
|
||||
RegorusProgram* program2 = NULL;
|
||||
RegorusRvm* vm = NULL;
|
||||
RegorusProgram* host_program = NULL;
|
||||
RegorusRvm* host_vm = NULL;
|
||||
bool is_partial = false;
|
||||
int exit_code = 1;
|
||||
|
||||
const char* data_json =
|
||||
"{"
|
||||
" \"roles\": {"
|
||||
" \"alice\": [\"admin\", \"reader\"]"
|
||||
" }"
|
||||
"}";
|
||||
const char* input_json =
|
||||
"{"
|
||||
" \"user\": \"alice\","
|
||||
" \"actions\": [\"read\"]"
|
||||
"}";
|
||||
const char* module_text =
|
||||
"package demo\n"
|
||||
"default allow = false\n"
|
||||
"allow if {\n"
|
||||
" input.user == \"alice\"\n"
|
||||
" some role in data.roles[input.user]\n"
|
||||
" role == \"admin\"\n"
|
||||
" count(input.actions) > 0\n"
|
||||
"}\n";
|
||||
|
||||
const char* host_data_json = "{}";
|
||||
const char* host_input_json = "{\"account\":{\"id\":\"acct-1\",\"active\":true}}";
|
||||
const char* host_module_text =
|
||||
"package demo\n"
|
||||
"import rego.v1\n"
|
||||
"default allow := false\n"
|
||||
"allow if {\n"
|
||||
" input.account.active == true\n"
|
||||
" details := __builtin_host_await(input.account.id, \"account\")\n"
|
||||
" details.tier == \"gold\"\n"
|
||||
"}\n";
|
||||
|
||||
RegorusPolicyModule module;
|
||||
module.id = "demo.rego";
|
||||
module.content = module_text;
|
||||
|
||||
const char* entry_points[] = {"data.demo.allow"};
|
||||
printf("Rego policy:\n%s\n", module_text);
|
||||
printf("Compiling program from modules...\n");
|
||||
result = regorus_program_compile_from_modules(
|
||||
data_json,
|
||||
&module,
|
||||
1,
|
||||
entry_points,
|
||||
1
|
||||
);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "compile program")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
program = (RegorusProgram*)result.pointer_value;
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
printf("Generating assembly listing...\n");
|
||||
result = regorus_program_generate_listing(program);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "generate listing")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
printf("Assembly listing:\n%s\n", result.output ? result.output : "(null)");
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
printf("Serializing program...\n");
|
||||
result = regorus_program_serialize_binary(program);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "serialize program")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
buffer = (RegorusBuffer*)result.pointer_value;
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
printf("Deserializing program (%zu bytes)...\n", buffer->len);
|
||||
result = regorus_program_deserialize_binary(
|
||||
buffer->data,
|
||||
buffer->len,
|
||||
&is_partial
|
||||
);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "deserialize program")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
if (is_partial) {
|
||||
fprintf(stderr, "deserialized program marked partial\n");
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
program2 = (RegorusProgram*)result.pointer_value;
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
printf("Creating VM...\n");
|
||||
vm = regorus_rvm_new();
|
||||
if (!vm) {
|
||||
fprintf(stderr, "failed to allocate VM\n");
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
printf("Loading program into VM...\n");
|
||||
result = regorus_rvm_load_program(vm, program2);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "load program")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
printf("Setting data...\n");
|
||||
result = regorus_rvm_set_data(vm, data_json);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "set data")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
printf("Setting input...\n");
|
||||
result = regorus_rvm_set_input(vm, input_json);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "set input")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
printf("Executing entry point...\n");
|
||||
result = regorus_rvm_execute(vm);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "execute")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
printf("Execution result (data.demo.allow): %s\n",
|
||||
result.output ? result.output : "(null)");
|
||||
printf("Decision: user=alice action=read -> allow=%s\n",
|
||||
result.output ? result.output : "(null)");
|
||||
if (!result.output || strcmp(result.output, "true") != 0) {
|
||||
fprintf(stderr, "unexpected result: %s\n", result.output);
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
printf("\n--- HostAwait example (suspendable execution) ---\n");
|
||||
RegorusPolicyModule host_module;
|
||||
host_module.id = "host_await.rego";
|
||||
host_module.content = host_module_text;
|
||||
|
||||
const char* host_entry_points[] = {"data.demo.allow"};
|
||||
result = regorus_program_compile_from_modules(
|
||||
host_data_json,
|
||||
&host_module,
|
||||
1,
|
||||
host_entry_points,
|
||||
1
|
||||
);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "compile host await program")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
host_program = (RegorusProgram*)result.pointer_value;
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
host_vm = regorus_rvm_new();
|
||||
if (!host_vm) {
|
||||
fprintf(stderr, "failed to allocate host await VM\n");
|
||||
goto Cleanup;
|
||||
}
|
||||
|
||||
result = regorus_rvm_set_execution_mode(host_vm, 1);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "set execution mode")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
result = regorus_rvm_load_program(host_vm, host_program);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "load host await program")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
result = regorus_rvm_set_data(host_vm, host_data_json);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "set host data")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
result = regorus_rvm_set_input(host_vm, host_input_json);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "set host input")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
result = regorus_rvm_execute(host_vm);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "execute host await")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
printf("HostAwait initial result: %s\n", result.output ? result.output : "(null)");
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
result = regorus_rvm_get_execution_state(host_vm);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "get execution state")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
printf("Execution state: %s\n", result.output ? result.output : "(null)");
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
result = regorus_rvm_resume(host_vm, "{\"tier\":\"gold\"}", true);
|
||||
result_valid = true;
|
||||
if (!assert_ok(result, "resume host await")) {
|
||||
goto Cleanup;
|
||||
}
|
||||
printf("HostAwait resumed result: %s\n", result.output ? result.output : "(null)");
|
||||
|
||||
if (!result.output || strcmp(result.output, "true") != 0) {
|
||||
fprintf(stderr, "unexpected host await result\n");
|
||||
goto Cleanup;
|
||||
}
|
||||
regorus_result_drop(result);
|
||||
result_valid = false;
|
||||
|
||||
exit_code = 0;
|
||||
|
||||
Cleanup:
|
||||
if (result_valid) {
|
||||
regorus_result_drop(result);
|
||||
}
|
||||
if (host_vm) {
|
||||
regorus_rvm_drop(host_vm);
|
||||
}
|
||||
if (host_program) {
|
||||
regorus_program_drop(host_program);
|
||||
}
|
||||
if (vm) {
|
||||
regorus_rvm_drop(vm);
|
||||
}
|
||||
if (program2) {
|
||||
regorus_program_drop(program2);
|
||||
}
|
||||
if (buffer) {
|
||||
regorus_buffer_drop(buffer);
|
||||
}
|
||||
if (program) {
|
||||
regorus_program_drop(program);
|
||||
}
|
||||
return exit_code;
|
||||
}
|
||||
@@ -14,6 +14,7 @@ FetchContent_MakeAvailable(Corrosion)
|
||||
|
||||
project("regorus-test")
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
enable_testing()
|
||||
|
||||
# installable ffi target
|
||||
|
||||
@@ -83,3 +84,9 @@ install(FILES
|
||||
|
||||
add_executable(regorus_test main.cpp)
|
||||
target_link_libraries(regorus_test regorus_ffi::regorus_ffi)
|
||||
|
||||
add_executable(regorus_rvm_test rvm_tests.cpp)
|
||||
target_link_libraries(regorus_rvm_test regorus_ffi::regorus_ffi)
|
||||
|
||||
add_test(NAME regorus_cpp_engine COMMAND regorus_test)
|
||||
add_test(NAME regorus_cpp_rvm COMMAND regorus_rvm_test)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef REGORUS_WRAPPER_HPP
|
||||
#define REGORUS_WRAPPER_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
|
||||
@@ -8,8 +10,11 @@
|
||||
|
||||
namespace regorus {
|
||||
|
||||
class Result {
|
||||
public:
|
||||
class Buffer;
|
||||
class Program;
|
||||
|
||||
class Result {
|
||||
public:
|
||||
|
||||
operator bool() const { return result.status == RegorusStatus::Ok; }
|
||||
bool operator !() const { return result.status != RegorusStatus::Ok; }
|
||||
@@ -30,18 +35,39 @@ namespace regorus {
|
||||
}
|
||||
}
|
||||
|
||||
void* pointer() const {
|
||||
return result.pointer_value;
|
||||
}
|
||||
|
||||
Program program() const;
|
||||
Buffer buffer() const;
|
||||
|
||||
Result(RegorusResult r) : result(r) {}
|
||||
Result(Result&& other) noexcept : result(other.result) {
|
||||
other.result.output = nullptr;
|
||||
other.result.error_message = nullptr;
|
||||
other.result.pointer_value = nullptr;
|
||||
}
|
||||
Result& operator=(Result&& other) noexcept {
|
||||
if (this != &other) {
|
||||
regorus_result_drop(result);
|
||||
result = other.result;
|
||||
other.result.output = nullptr;
|
||||
other.result.error_message = nullptr;
|
||||
other.result.pointer_value = nullptr;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Result() {
|
||||
regorus_result_drop(result);
|
||||
}
|
||||
|
||||
private:
|
||||
friend class Engine;
|
||||
RegorusResult result;
|
||||
|
||||
Result(RegorusResult r) : result(r) {}
|
||||
private:
|
||||
Result(const Result&) = delete;
|
||||
Result(Result&&) = delete;
|
||||
Result& operator=(const Result&) = delete;
|
||||
|
||||
};
|
||||
@@ -109,6 +135,10 @@ namespace regorus {
|
||||
~Engine() {
|
||||
regorus_engine_drop(engine);
|
||||
}
|
||||
|
||||
RegorusEngine* raw() const {
|
||||
return engine;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
@@ -119,6 +149,247 @@ namespace regorus {
|
||||
Engine(Engine&&) = delete;
|
||||
Engine& operator=(const Engine&) = delete;
|
||||
};
|
||||
|
||||
class CompiledPolicy {
|
||||
public:
|
||||
explicit CompiledPolicy(RegorusCompiledPolicy* p) : policy(p) {}
|
||||
|
||||
Result eval_with_input(const char* input_json) {
|
||||
return Result(regorus_compiled_policy_eval_with_input(policy, input_json));
|
||||
}
|
||||
|
||||
Result get_policy_info() {
|
||||
return Result(regorus_compiled_policy_get_policy_info(policy));
|
||||
}
|
||||
|
||||
RegorusCompiledPolicy* raw() const {
|
||||
return policy;
|
||||
}
|
||||
|
||||
~CompiledPolicy() {
|
||||
if (policy) {
|
||||
regorus_compiled_policy_drop(policy);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
RegorusCompiledPolicy* policy;
|
||||
CompiledPolicy(const CompiledPolicy&) = delete;
|
||||
CompiledPolicy(CompiledPolicy&&) = delete;
|
||||
CompiledPolicy& operator=(const CompiledPolicy&) = delete;
|
||||
};
|
||||
|
||||
class Buffer {
|
||||
public:
|
||||
Buffer() : buffer(nullptr) {}
|
||||
explicit Buffer(RegorusBuffer* b) : buffer(b) {}
|
||||
|
||||
const std::uint8_t* data() const {
|
||||
return buffer ? buffer->data : nullptr;
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
return buffer ? buffer->len : 0;
|
||||
}
|
||||
|
||||
RegorusBuffer* raw() const {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
~Buffer() {
|
||||
if (buffer) {
|
||||
regorus_buffer_drop(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
RegorusBuffer* buffer;
|
||||
Buffer(const Buffer&) = delete;
|
||||
Buffer(Buffer&&) = delete;
|
||||
Buffer& operator=(const Buffer&) = delete;
|
||||
};
|
||||
|
||||
class Program {
|
||||
public:
|
||||
Program() : program(regorus_program_new()) {}
|
||||
explicit Program(RegorusProgram* p) : program(p) {}
|
||||
|
||||
static Result compile_from_policy(
|
||||
RegorusCompiledPolicy* compiled_policy,
|
||||
const char* const* entry_points,
|
||||
size_t entry_points_len
|
||||
) {
|
||||
return Result(regorus_program_compile_from_policy(
|
||||
compiled_policy,
|
||||
entry_points,
|
||||
entry_points_len
|
||||
));
|
||||
}
|
||||
|
||||
static Result compile_from_modules(
|
||||
const char* data_json,
|
||||
const RegorusPolicyModule* modules,
|
||||
size_t modules_len,
|
||||
const char* const* entry_points,
|
||||
size_t entry_points_len
|
||||
) {
|
||||
return Result(regorus_program_compile_from_modules(
|
||||
data_json,
|
||||
modules,
|
||||
modules_len,
|
||||
entry_points,
|
||||
entry_points_len
|
||||
));
|
||||
}
|
||||
|
||||
static Result compile_from_engine(
|
||||
RegorusEngine* engine,
|
||||
const char* const* entry_points,
|
||||
size_t entry_points_len
|
||||
) {
|
||||
return Result(regorus_engine_compile_program_with_entrypoints(
|
||||
engine,
|
||||
entry_points,
|
||||
entry_points_len
|
||||
));
|
||||
}
|
||||
|
||||
Result serialize_binary() const {
|
||||
return Result(regorus_program_serialize_binary(program));
|
||||
}
|
||||
|
||||
static Result deserialize_binary(
|
||||
const std::uint8_t* data,
|
||||
size_t len,
|
||||
bool* is_partial
|
||||
) {
|
||||
return Result(regorus_program_deserialize_binary(data, len, is_partial));
|
||||
}
|
||||
|
||||
Result generate_listing() const {
|
||||
return Result(regorus_program_generate_listing(program));
|
||||
}
|
||||
|
||||
Result generate_tabular_listing() const {
|
||||
return Result(regorus_program_generate_tabular_listing(program));
|
||||
}
|
||||
|
||||
RegorusProgram* raw() const {
|
||||
return program;
|
||||
}
|
||||
|
||||
~Program() {
|
||||
if (program) {
|
||||
regorus_program_drop(program);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
RegorusProgram* program;
|
||||
Program(const Program&) = delete;
|
||||
Program(Program&&) = delete;
|
||||
Program& operator=(const Program&) = delete;
|
||||
};
|
||||
|
||||
inline Program Result::program() const {
|
||||
return Program(reinterpret_cast<RegorusProgram*>(result.pointer_value));
|
||||
}
|
||||
|
||||
inline Buffer Result::buffer() const {
|
||||
return Buffer(reinterpret_cast<RegorusBuffer*>(result.pointer_value));
|
||||
}
|
||||
|
||||
class Rvm {
|
||||
public:
|
||||
Rvm() : vm(regorus_rvm_new()) {}
|
||||
explicit Rvm(RegorusRvm* v) : vm(v) {}
|
||||
|
||||
static Result create_with_policy(RegorusCompiledPolicy* compiled_policy) {
|
||||
return Result(regorus_rvm_new_with_policy(compiled_policy));
|
||||
}
|
||||
|
||||
Result load_program(const Program& program) {
|
||||
return Result(regorus_rvm_load_program(vm, program.raw()));
|
||||
}
|
||||
|
||||
Result set_data(const char* data_json) {
|
||||
return Result(regorus_rvm_set_data(vm, data_json));
|
||||
}
|
||||
|
||||
Result set_input(const char* input_json) {
|
||||
return Result(regorus_rvm_set_input(vm, input_json));
|
||||
}
|
||||
|
||||
Result set_max_instructions(size_t max_instructions) {
|
||||
return Result(regorus_rvm_set_max_instructions(vm, max_instructions));
|
||||
}
|
||||
|
||||
Result set_strict_builtin_errors(bool strict) {
|
||||
return Result(regorus_rvm_set_strict_builtin_errors(vm, strict));
|
||||
}
|
||||
|
||||
Result set_execution_mode(std::uint8_t mode) {
|
||||
return Result(regorus_rvm_set_execution_mode(vm, mode));
|
||||
}
|
||||
|
||||
Result set_step_mode(bool enabled) {
|
||||
return Result(regorus_rvm_set_step_mode(vm, enabled));
|
||||
}
|
||||
|
||||
Result set_execution_timer_config(bool has_config, RegorusExecutionTimerConfig config) {
|
||||
return Result(regorus_rvm_set_execution_timer_config(vm, has_config, config));
|
||||
}
|
||||
|
||||
Result execute() {
|
||||
return Result(regorus_rvm_execute(vm));
|
||||
}
|
||||
|
||||
Result execute_entry_point_by_name(const char* entry_point) {
|
||||
return Result(regorus_rvm_execute_entry_point_by_name(vm, entry_point));
|
||||
}
|
||||
|
||||
Result execute_entry_point_by_index(size_t index) {
|
||||
return Result(regorus_rvm_execute_entry_point_by_index(vm, index));
|
||||
}
|
||||
|
||||
Result resume(const char* resume_value_json, bool has_value) {
|
||||
return Result(regorus_rvm_resume(vm, resume_value_json, has_value));
|
||||
}
|
||||
|
||||
Result get_execution_state() {
|
||||
return Result(regorus_rvm_get_execution_state(vm));
|
||||
}
|
||||
|
||||
RegorusRvm* raw() const {
|
||||
return vm;
|
||||
}
|
||||
|
||||
~Rvm() {
|
||||
if (vm) {
|
||||
regorus_rvm_drop(vm);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
RegorusRvm* vm;
|
||||
Rvm(const Rvm&) = delete;
|
||||
Rvm(Rvm&&) = delete;
|
||||
Rvm& operator=(const Rvm&) = delete;
|
||||
};
|
||||
|
||||
inline Result compile_policy_with_entrypoint(
|
||||
const char* data_json,
|
||||
const RegorusPolicyModule* modules,
|
||||
size_t modules_len,
|
||||
const char* entry_point
|
||||
) {
|
||||
return Result(regorus_compile_policy_with_entrypoint(
|
||||
data_json,
|
||||
modules,
|
||||
modules_len,
|
||||
entry_point
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
#endif // REGORUS_WRAPPER_HPP
|
||||
|
||||
261
bindings/cpp/rvm_tests.cpp
Normal file
261
bindings/cpp/rvm_tests.cpp
Normal file
@@ -0,0 +1,261 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "regorus.hpp"
|
||||
|
||||
int main() {
|
||||
const char* data_json =
|
||||
"{"
|
||||
" \"roles\": {"
|
||||
" \"alice\": [\"admin\", \"reader\"]"
|
||||
" }"
|
||||
"}";
|
||||
const char* input_json =
|
||||
"{"
|
||||
" \"user\": \"alice\","
|
||||
" \"actions\": [\"read\"]"
|
||||
"}";
|
||||
const char* module_text =
|
||||
"package demo\n"
|
||||
"default allow = false\n"
|
||||
"allow if {\n"
|
||||
" input.user == \"alice\"\n"
|
||||
" some role in data.roles[input.user]\n"
|
||||
" role == \"admin\"\n"
|
||||
" count(input.actions) > 0\n"
|
||||
"}\n";
|
||||
|
||||
const char* host_data_json = "{}";
|
||||
const char* host_input_json = "{\"account\":{\"id\":\"acct-1\",\"active\":true}}";
|
||||
const char* host_module_text =
|
||||
"package demo\n"
|
||||
"import rego.v1\n"
|
||||
"default allow := false\n"
|
||||
"allow if {\n"
|
||||
" input.account.active == true\n"
|
||||
" details := __builtin_host_await(input.account.id, \"account\")\n"
|
||||
" details.tier == \"gold\"\n"
|
||||
"}\n";
|
||||
|
||||
RegorusPolicyModule module;
|
||||
module.id = "demo.rego";
|
||||
module.content = module_text;
|
||||
|
||||
const char* entry_points[] = {"data.demo.allow"};
|
||||
std::cout << "Rego policy:\n" << module_text << std::endl;
|
||||
std::cout << "Compiling program from modules..." << std::endl;
|
||||
auto program_result = regorus::Program::compile_from_modules(
|
||||
data_json,
|
||||
&module,
|
||||
1,
|
||||
entry_points,
|
||||
1
|
||||
);
|
||||
if (!program_result) {
|
||||
std::cerr << "compile program (modules): " << program_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
regorus::Program program = program_result.program();
|
||||
|
||||
std::cout << "Generating assembly listing..." << std::endl;
|
||||
auto listing_result = program.generate_listing();
|
||||
if (!listing_result) {
|
||||
std::cerr << "generate listing: " << listing_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Assembly listing:\n" << listing_result.output() << std::endl;
|
||||
|
||||
std::cout << "Serializing program..." << std::endl;
|
||||
auto serialize_result = program.serialize_binary();
|
||||
if (!serialize_result) {
|
||||
std::cerr << "serialize program: " << serialize_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
regorus::Buffer buffer(reinterpret_cast<RegorusBuffer*>(serialize_result.pointer()));
|
||||
bool is_partial = false;
|
||||
std::cout << "Deserializing program (" << buffer.size() << " bytes)..." << std::endl;
|
||||
auto deserialize_result = regorus::Program::deserialize_binary(
|
||||
buffer.data(),
|
||||
buffer.size(),
|
||||
&is_partial
|
||||
);
|
||||
if (!deserialize_result) {
|
||||
std::cerr << "deserialize program: " << deserialize_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (is_partial) {
|
||||
std::cerr << "deserialized program marked partial" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
regorus::Program program2 = deserialize_result.program();
|
||||
|
||||
{
|
||||
std::cout << "Creating VM..." << std::endl;
|
||||
regorus::Rvm vm;
|
||||
auto load_result = vm.load_program(program2);
|
||||
if (!load_result) {
|
||||
std::cerr << "load program: " << load_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Setting data..." << std::endl;
|
||||
auto data_result = vm.set_data(data_json);
|
||||
if (!data_result) {
|
||||
std::cerr << "set data: " << data_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Setting input..." << std::endl;
|
||||
auto input_result = vm.set_input(input_json);
|
||||
if (!input_result) {
|
||||
std::cerr << "set input: " << input_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Executing entry point..." << std::endl;
|
||||
auto exec_result = vm.execute();
|
||||
if (!exec_result) {
|
||||
std::cerr << "execute: " << exec_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Execution result (data.demo.allow): " << exec_result.output() << std::endl;
|
||||
std::cout << "Decision: user=alice action=read -> allow=" << exec_result.output() << std::endl;
|
||||
if (std::string(exec_result.output()) != "true") {
|
||||
std::cerr << "unexpected result: " << exec_result.output() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
regorus::Engine engine;
|
||||
std::cout << "Compiling program from engine..." << std::endl;
|
||||
auto add_policy_result = engine.add_policy("demo.rego", module_text);
|
||||
if (!add_policy_result) {
|
||||
std::cerr << "engine add policy: " << add_policy_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto engine_program_result = regorus::Program::compile_from_engine(
|
||||
engine.raw(),
|
||||
entry_points,
|
||||
1
|
||||
);
|
||||
if (!engine_program_result) {
|
||||
std::cerr << "compile program (engine): " << engine_program_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
regorus::Program engine_program = engine_program_result.program();
|
||||
|
||||
regorus::Rvm engine_vm;
|
||||
auto engine_load_result = engine_vm.load_program(engine_program);
|
||||
if (!engine_load_result) {
|
||||
std::cerr << "engine load program: " << engine_load_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Setting engine data..." << std::endl;
|
||||
auto engine_data_result = engine_vm.set_data(data_json);
|
||||
if (!engine_data_result) {
|
||||
std::cerr << "engine set data: " << engine_data_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Setting engine input..." << std::endl;
|
||||
auto engine_input_result = engine_vm.set_input(input_json);
|
||||
if (!engine_input_result) {
|
||||
std::cerr << "engine set input: " << engine_input_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Executing engine entry point..." << std::endl;
|
||||
auto engine_exec_result = engine_vm.execute();
|
||||
if (!engine_exec_result) {
|
||||
std::cerr << "engine execute: " << engine_exec_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Engine execution result (data.demo.allow): " << engine_exec_result.output() << std::endl;
|
||||
std::cout << "Decision: user=alice action=read -> allow=" << engine_exec_result.output() << std::endl;
|
||||
if (std::string(engine_exec_result.output()) != "true") {
|
||||
std::cerr << "unexpected engine result: " << engine_exec_result.output() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "\n--- HostAwait example (suspendable execution) ---" << std::endl;
|
||||
RegorusPolicyModule host_module;
|
||||
host_module.id = "host_await.rego";
|
||||
host_module.content = host_module_text;
|
||||
const char* host_entry_points[] = {"data.demo.allow"};
|
||||
|
||||
auto host_program_result = regorus::Program::compile_from_modules(
|
||||
host_data_json,
|
||||
&host_module,
|
||||
1,
|
||||
host_entry_points,
|
||||
1
|
||||
);
|
||||
if (!host_program_result) {
|
||||
std::cerr << "compile host await program: " << host_program_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
regorus::Program host_program = host_program_result.program();
|
||||
regorus::Rvm host_vm;
|
||||
auto host_mode_result = host_vm.set_execution_mode(1);
|
||||
if (!host_mode_result) {
|
||||
std::cerr << "set execution mode: " << host_mode_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto host_load_result = host_vm.load_program(host_program);
|
||||
if (!host_load_result) {
|
||||
std::cerr << "load host await program: " << host_load_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto host_data_result = host_vm.set_data(host_data_json);
|
||||
if (!host_data_result) {
|
||||
std::cerr << "set host data: " << host_data_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto host_input_result = host_vm.set_input(host_input_json);
|
||||
if (!host_input_result) {
|
||||
std::cerr << "set host input: " << host_input_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto host_exec_result = host_vm.execute();
|
||||
if (!host_exec_result) {
|
||||
std::cerr << "execute host await: " << host_exec_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "HostAwait initial result: " << host_exec_result.output() << std::endl;
|
||||
|
||||
auto host_state_result = host_vm.get_execution_state();
|
||||
if (!host_state_result) {
|
||||
std::cerr << "get execution state: " << host_state_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "Execution state: " << host_state_result.output() << std::endl;
|
||||
|
||||
auto host_resume_result = host_vm.resume("{\"tier\":\"gold\"}", true);
|
||||
if (!host_resume_result) {
|
||||
std::cerr << "resume host await: " << host_resume_result.error() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
std::cout << "HostAwait resumed result: " << host_resume_result.output() << std::endl;
|
||||
if (std::string(host_resume_result.output()) != "true") {
|
||||
std::cerr << "unexpected host await result: " << host_resume_result.output() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -16,7 +16,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Regorus" Version="0.6.0$(RegorusPackageVersionSuffix)"/>
|
||||
<PackageReference Include="Regorus" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -129,12 +129,12 @@ namespace Benchmarks
|
||||
Console.WriteLine($"Warming up with {threads} threads for {warmupSeconds} seconds...");
|
||||
|
||||
// Warmup phase
|
||||
var (_, _, _) = RunBenchmarkPhase(threads, warmupSeconds, policiesWithInputs, compiledPolicies, useSharedPolicies, isWarmup: true);
|
||||
var (_, _, _, _) = RunBenchmarkPhase(threads, warmupSeconds, policiesWithInputs, compiledPolicies, useSharedPolicies, isWarmup: true);
|
||||
|
||||
Console.WriteLine($"Running benchmark with {threads} threads for {durationSeconds} seconds...");
|
||||
|
||||
// Actual benchmark phase
|
||||
var (totalEvaluations, evaluationTime, policyCounters) = RunBenchmarkPhase(threads, durationSeconds, policiesWithInputs, compiledPolicies, useSharedPolicies, isWarmup: false);
|
||||
var (totalEvaluations, evaluationTime, policyCounters, allocatedBytes) = RunBenchmarkPhase(threads, durationSeconds, policiesWithInputs, compiledPolicies, useSharedPolicies, isWarmup: false);
|
||||
|
||||
// Calculate throughput based on pure evaluation time (consistent with Rust benchmark)
|
||||
var evalsPerSecond = totalEvaluations / evaluationTime.TotalSeconds;
|
||||
@@ -144,6 +144,12 @@ namespace Benchmarks
|
||||
Console.WriteLine($" time: [{evaluationTime.TotalMilliseconds:F2} ms]");
|
||||
Console.WriteLine($" thrpt: [{kelemsPerSecond:F2} Kelem/s]");
|
||||
|
||||
if (totalEvaluations > 0)
|
||||
{
|
||||
var bytesPerEval = allocatedBytes / (double)totalEvaluations;
|
||||
Console.WriteLine($" alloc: [{bytesPerEval:F2} B/op] (total {allocatedBytes} B)");
|
||||
}
|
||||
|
||||
// Clean up compiled policies if we created them
|
||||
if (compiledPolicies != null)
|
||||
{
|
||||
@@ -166,7 +172,7 @@ namespace Benchmarks
|
||||
}
|
||||
}
|
||||
|
||||
private static (int totalEvaluations, TimeSpan evaluationTime, Dictionary<string, int> policyCounters) RunBenchmarkPhase(
|
||||
private static (int totalEvaluations, TimeSpan evaluationTime, Dictionary<string, int> policyCounters, long allocatedBytes) RunBenchmarkPhase(
|
||||
int threads,
|
||||
int durationSeconds,
|
||||
List<(string Policy, string[] Inputs)> policiesWithInputs,
|
||||
@@ -180,6 +186,7 @@ namespace Benchmarks
|
||||
var evaluationTimes = new Dictionary<int, TimeSpan>();
|
||||
var lockObject = new object();
|
||||
var stopExecution = false;
|
||||
long allocatedBytes = 0;
|
||||
|
||||
// Initialize counters
|
||||
foreach (var policyName in PolicyNames)
|
||||
@@ -194,6 +201,12 @@ namespace Benchmarks
|
||||
int tid = threadId;
|
||||
tasks[threadId] = Task.Run(() =>
|
||||
{
|
||||
long allocationStart = 0;
|
||||
if (!isWarmup)
|
||||
{
|
||||
allocationStart = GC.GetAllocatedBytesForCurrentThread();
|
||||
}
|
||||
|
||||
barrier.SignalAndWait();
|
||||
|
||||
int evaluationCount = 0;
|
||||
@@ -256,6 +269,9 @@ namespace Benchmarks
|
||||
evaluationTimes[tid] = TimeSpan.Zero;
|
||||
evaluationTimes[tid] = localEvaluationTime;
|
||||
}
|
||||
|
||||
var allocationEnd = GC.GetAllocatedBytesForCurrentThread();
|
||||
System.Threading.Interlocked.Add(ref allocatedBytes, allocationEnd - allocationStart);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -272,7 +288,7 @@ namespace Benchmarks
|
||||
// Use pure evaluation time (consistent with Rust benchmark)
|
||||
var evaluationTime = totalEvaluationTime == TimeSpan.Zero ? stopwatch.Elapsed : totalEvaluationTime;
|
||||
|
||||
return (totalEvaluations, evaluationTime, policyCounters);
|
||||
return (totalEvaluations, evaluationTime, policyCounters, allocatedBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
- **CPU**: 16 cores
|
||||
- **Architecture**: ARM64 (aarch64-apple-darwin)
|
||||
- **.NET Version**: 8.0
|
||||
- **Allocator**: mimalloc (default allocator for Rust FFI)
|
||||
- **Benchmark Framework**: Custom time-based benchmarking
|
||||
- **Test Data**: 20,000 inputs per evaluation (distributed across threads)
|
||||
- **Policy**: Complex authorization policy with nested rules
|
||||
@@ -27,77 +28,113 @@ The C# compiled policy evaluation benchmark tests Regorus compiled policy perfor
|
||||
### Compiled Shared Policies (Best Performance)
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 2928.81 | 211 |
|
||||
| 2 | 5892.53 | 146 |
|
||||
| 4 | 11750.71 | 155 |
|
||||
| 6 | 17686.92 | 134 |
|
||||
| 8 | 23543.53 | 90 |
|
||||
| 10 | 29503.80 | 72 |
|
||||
| 12 | 35494.81 | 58 |
|
||||
| 14 | 41408.36 | 50 |
|
||||
| 16 | 47333.65 | 44 |
|
||||
| 18 | 53050.24 | 38 |
|
||||
| 20 | 58807.20 | 34 |
|
||||
| 22 | 406022.45 | 32 |
|
||||
| 24 | 65480.69 | 32 |
|
||||
| 26 | 70952.34 | 30 |
|
||||
| 28 | 72064.03 | 30 |
|
||||
| 30 | 492405.74 | 27 |
|
||||
| 32 | 81210.83 | 27 |
|
||||
| 1 | 2905.41 | 273 |
|
||||
| 2 | 5808.07 | 240 |
|
||||
| 4 | 11631.23 | 227 |
|
||||
| 6 | 17431.95 | 216 |
|
||||
| 8 | 23183.42 | 126 |
|
||||
| 10 | 28886.11 | 118 |
|
||||
| 12 | 34659.87 | 108 |
|
||||
| 14 | 40564.07 | 84 |
|
||||
| 16 | 46446.38 | 72 |
|
||||
| 18 | 52047.06 | 63 |
|
||||
| 20 | 56983.45 | 58 |
|
||||
| 22 | 404931.47 | 55 |
|
||||
| 24 | 61673.71 | 55 |
|
||||
| 26 | 64370.41 | 51 |
|
||||
| 28 | 56897.04 | 59 |
|
||||
| 30 | 406850.06 | 52 |
|
||||
| 32 | 56786.24 | 58 |
|
||||
|
||||
### Compiled Per Iteration
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 2984.00 | 39 |
|
||||
| 2 | 5969.45 | 38 |
|
||||
| 4 | 11948.28 | 32 |
|
||||
| 6 | 17927.24 | 30 |
|
||||
| 8 | 23889.01 | 24 |
|
||||
| 10 | 29882.38 | 20 |
|
||||
| 12 | 35865.06 | 18 |
|
||||
| 14 | 41838.70 | 15 |
|
||||
| 16 | 47800.92 | 14 |
|
||||
| 18 | 53257.22 | 10 |
|
||||
| 20 | 59596.93 | 11 |
|
||||
| 22 | 435853.41 | 10 |
|
||||
| 24 | 70870.86 | 9 |
|
||||
| 26 | 76120.59 | 9 |
|
||||
| 28 | 80717.51 | 8 |
|
||||
| 30 | 544207.96 | 8 |
|
||||
| 32 | 91540.91 | 7 |
|
||||
| 1 | 2978.06 | 49 |
|
||||
| 2 | 5965.09 | 47 |
|
||||
| 4 | 11928.23 | 46 |
|
||||
| 6 | 17892.58 | 45 |
|
||||
| 8 | 23773.82 | 43 |
|
||||
| 10 | 29705.61 | 42 |
|
||||
| 12 | 35631.97 | 40 |
|
||||
| 14 | 41563.35 | 34 |
|
||||
| 16 | 47452.93 | 31 |
|
||||
| 18 | 53505.42 | 27 |
|
||||
| 20 | 59393.86 | 25 |
|
||||
| 22 | 436115.28 | 23 |
|
||||
| 24 | 71088.08 | 21 |
|
||||
| 26 | 76928.70 | 19 |
|
||||
| 28 | 82759.27 | 18 |
|
||||
| 30 | 560658.97 | 17 |
|
||||
| 32 | 93949.39 | 16 |
|
||||
|
||||
## Analysis
|
||||
|
||||
The C# compiled policy benchmark demonstrates important performance characteristics:
|
||||
The C# compiled policy benchmark demonstrates important performance characteristics with mimalloc as the default allocator:
|
||||
|
||||
1. **Compilation Strategy Impact**: Shared compiled policies significantly outperform per-iteration compilation (~5.4x at 1 thread)
|
||||
2. **Scaling Patterns**:
|
||||
1. **Compilation Strategy Impact**: Shared compiled policies significantly outperform per-iteration compilation (~5.6x at 1 thread)
|
||||
2. **Scaling Patterns with mimalloc**:
|
||||
- Best throughput achieved at 1 thread for shared policies
|
||||
- Performance generally degrades with increased thread count
|
||||
- Performance generally degrades with increased thread count, but mimalloc provides better allocation efficiency
|
||||
3. **Performance Hierarchy**:
|
||||
- Shared compiled policies: Best performance (optimal configuration)
|
||||
- Per-iteration compilation: ~82% reduction from optimal
|
||||
4. **Compilation Overhead**: Per-iteration compilation creates substantial overhead, similar to fresh engine creation
|
||||
5. **Thread Contention**: Significant performance degradation beyond 8 threads for both configurations
|
||||
5. **Thread Contention**: Significant performance degradation beyond 8 threads for both configurations, though mimalloc helps mitigate some allocation-related issues
|
||||
|
||||
## Comparison with Rust Compiled Policy Evaluation
|
||||
|
||||
| Configuration | C# Performance (1 thread) | Rust Performance (1 thread) | Relative Performance |
|
||||
|:-----------------|:----------------------------|:-----------------------------|---------------------:|
|
||||
| Shared Policies | Best performance | Higher throughput | 0.40x-0.70x |
|
||||
| Per-iteration | ~82% reduction from optimal | ~85% reduction from optimal | 0.47x-0.89x |
|
||||
### Multi-Thread Performance Comparison
|
||||
|
||||
| Configuration | 1 Thread (Kelem/s) | 4 Threads (Kelem/s) | 8 Threads (Kelem/s) |
|
||||
|:-----------------|:-------------------|:--------------------|:--------------------|
|
||||
| | C# / Rust | C# / Rust | C# / Rust |
|
||||
| Shared Policies | 273 / 426 | 227 / 342 | 126 / 185 |
|
||||
| Per-iteration | 49 / 55 | 46 / 50 | 43 / 50 |
|
||||
|
||||
### Threading Efficiency Analysis
|
||||
|
||||
| Configuration | Low Contention (1-4t) | Medium Contention (6-12t) | High Contention (16+t) |
|
||||
|:-----------------|:----------------------|:--------------------------|:-----------------------|
|
||||
| | Avg C# / Rust | Avg C# / Rust | Avg C# / Rust |
|
||||
| Shared Policies | 249 / 384 | 150 / 203 | 58 / 123 |
|
||||
| Per-iteration | 47 / 54 | 40 / 50 | 22 / 42 |
|
||||
|
||||
**Key Observations:**
|
||||
- **Single-threaded performance**: C# achieves 64% of Rust performance for shared policies, 89% for per-iteration
|
||||
- **Threading scaling**: Both platforms show similar degradation patterns, but Rust maintains better absolute performance
|
||||
- **Contention resistance**: Per-iteration compilation shows more consistent relative performance across thread counts
|
||||
- **Platform differences**: C# shows more pronounced performance drops at higher thread counts, particularly for shared policies
|
||||
|
||||
*Note: Rust benchmarks include additional input data variations (cloned vs fresh inputs) that are not present in the C# implementation.*
|
||||
|
||||
## Comparison with C# Engine Evaluation
|
||||
|
||||
| Configuration | Compiled Policy (1 thread) | Engine Evaluation (1 thread) | Performance Ratio |
|
||||
|:---------------|:----------------------------|:------------------------------|------------------:|
|
||||
| Optimal Config | Best performance | Slightly higher throughput | 0.96x |
|
||||
### Multi-Thread Performance Comparison
|
||||
|
||||
| Configuration | 1 Thread (Kelem/s) | 4 Threads (Kelem/s) | 8 Threads (Kelem/s) |
|
||||
|:----------------|:-------------------|:--------------------|:--------------------|
|
||||
| | CP / EE | CP / EE | CP / EE |
|
||||
| Shared Policies | 273 / 279 | 227 / 217 | 126 / 114 |
|
||||
| Per-iteration | 49 / 50 | 46 / 47 | 43 / 45 |
|
||||
|
||||
### Threading Efficiency Analysis
|
||||
|
||||
| Configuration | Low Contention (1-4t) | Medium Contention (6-12t) | High Contention (16+t) |
|
||||
|:----------------|:----------------------|:--------------------------|:-----------------------|
|
||||
| | Avg CP / EE | Avg CP / EE | Avg CP / EE |
|
||||
| Shared Policies | 249 / 248 | 150 / 128 | 58 / 54 |
|
||||
| Per-iteration | 47 / 48 | 40 / 39 | 22 / 27 |
|
||||
|
||||
**Key Observations:**
|
||||
- **Single-threaded parity**: Both systems perform nearly identically at 1 thread
|
||||
- **Threading behavior**: Compiled policies slightly outperform engine evaluation at higher thread counts for shared policies
|
||||
- **Contention resistance**: Per-iteration configurations show very similar performance characteristics across all thread counts
|
||||
- **Platform consistency**: Both C# implementations show similar scaling patterns and contention behavior
|
||||
|
||||
## Performance Insights
|
||||
|
||||
1. **Compilation Efficiency**: Pre-compiled policies provide massive performance benefits over per-iteration compilation
|
||||
2. **C# Performance Gap**: C# compiled policies achieve 40%-70% of Rust performance for shared policies
|
||||
3. **Engine vs Compiled**: In C#, engine evaluation slightly outperforms compiled policies (96%-104% range)
|
||||
1. **C# vs Rust Performance**: C# compiled policies achieve 65% average performance of Rust for shared policies, 87% average for per-iteration across low contention scenarios
|
||||
2. **Engine vs Compiled**: In C#, engine and compiled policy evaluation show very similar average performance (compiled policies achieve 100% of engine performance for shared policies, 98% for per-iteration)
|
||||
3. **mimalloc Impact**: The use of mimalloc as the default allocator in the underlying Rust FFI provides better memory allocation efficiency and improved threading characteristics
|
||||
4. **Threading Scaling**: Both C# configurations demonstrate similar contention patterns, with shared policies showing more pronounced degradation under high thread contention compared to per-iteration compilation
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
- **CPU**: 16 cores
|
||||
- **Architecture**: ARM64 (aarch64-apple-darwin)
|
||||
- **.NET Version**: 8.0
|
||||
- **Allocator**: mimalloc (default allocator for Rust FFI)
|
||||
- **Benchmark Framework**: Custom time-based benchmarking
|
||||
- **Test Data**: 20,000 inputs per evaluation (distributed across threads)
|
||||
- **Policy**: Complex authorization policy with nested rules
|
||||
@@ -27,73 +28,91 @@ The C# engine evaluation benchmark tests Regorus policy evaluation performance a
|
||||
### Cloned Engines (Best Performance)
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 2930.56 | 219 |
|
||||
| 2 | 5868.46 | 177 |
|
||||
| 4 | 11771.01 | 146 |
|
||||
| 6 | 17682.52 | 129 |
|
||||
| 8 | 23633.65 | 78 |
|
||||
| 10 | 29489.12 | 67 |
|
||||
| 12 | 35455.23 | 57 |
|
||||
| 14 | 41353.65 | 47 |
|
||||
| 16 | 47378.91 | 42 |
|
||||
| 18 | 52750.68 | 36 |
|
||||
| 20 | 58131.31 | 35 |
|
||||
| 22 | 62964.88 | 31 |
|
||||
| 24 | 64337.75 | 34 |
|
||||
| 26 | 70044.96 | 29 |
|
||||
| 28 | 72553.98 | 28 |
|
||||
| 30 | 79323.25 | 26 |
|
||||
| 32 | 78624.33 | 26 |
|
||||
| 1 | 2903.43 | 279 |
|
||||
| 2 | 5808.35 | 227 |
|
||||
| 4 | 11645.08 | 217 |
|
||||
| 6 | 17469.69 | 207 |
|
||||
| 8 | 23268.07 | 114 |
|
||||
| 10 | 28996.14 | 104 |
|
||||
| 12 | 34808.60 | 98 |
|
||||
| 14 | 40703.21 | 72 |
|
||||
| 16 | 46488.23 | 63 |
|
||||
| 18 | 52078.52 | 56 |
|
||||
| 20 | 57014.31 | 51 |
|
||||
| 22 | 60482.22 | 47 |
|
||||
| 24 | 62445.67 | 46 |
|
||||
| 26 | 65128.74 | 45 |
|
||||
| 28 | 58001.92 | 50 |
|
||||
| 30 | 66154.78 | 42 |
|
||||
| 32 | 64999.03 | 45 |
|
||||
|
||||
### Fresh Engines
|
||||
| Threads | Total Evaluation Time (ms) | Throughput (Kelem/s) |
|
||||
|--------:|---------------------------:|---------------------:|
|
||||
| 1 | 2985.49 | 41 |
|
||||
| 2 | 5968.13 | 38 |
|
||||
| 4 | 11942.10 | 34 |
|
||||
| 6 | 17918.75 | 32 |
|
||||
| 8 | 23873.57 | 25 |
|
||||
| 10 | 29863.85 | 20 |
|
||||
| 12 | 35823.98 | 19 |
|
||||
| 14 | 41811.53 | 16 |
|
||||
| 16 | 47819.89 | 14 |
|
||||
| 18 | 53478.32 | 13 |
|
||||
| 20 | 59191.93 | 12 |
|
||||
| 22 | 64630.71 | 11 |
|
||||
| 24 | 70215.54 | 10 |
|
||||
| 26 | 75732.06 | 9 |
|
||||
| 28 | 80897.59 | 9 |
|
||||
| 30 | 949904.84 | 8 |
|
||||
| 32 | 92592.64 | 8 |
|
||||
| 1 | 2982.28 | 50 |
|
||||
| 2 | 5962.62 | 48 |
|
||||
| 4 | 11917.94 | 47 |
|
||||
| 6 | 17874.77 | 46 |
|
||||
| 8 | 23729.94 | 45 |
|
||||
| 10 | 29635.17 | 42 |
|
||||
| 12 | 35574.71 | 38 |
|
||||
| 14 | 41482.61 | 34 |
|
||||
| 16 | 47425.16 | 32 |
|
||||
| 18 | 53248.87 | 29 |
|
||||
| 20 | 58424.34 | 27 |
|
||||
| 22 | 61302.24 | 26 |
|
||||
| 24 | 67430.08 | 23 |
|
||||
| 26 | 65226.79 | 24 |
|
||||
| 28 | 73118.48 | 22 |
|
||||
| 30 | 326472.94 | 23 |
|
||||
| 32 | 63805.03 | 24 |
|
||||
|
||||
## Analysis
|
||||
|
||||
The C# benchmark results demonstrate important performance characteristics:
|
||||
The C# benchmark results demonstrate important performance characteristics with mimalloc as the default allocator:
|
||||
|
||||
1. **Engine Reuse Impact**: Cloned engines significantly outperform fresh engines (~5.3x at 1 thread)
|
||||
2. **Scaling Patterns**:
|
||||
1. **Engine Reuse Impact**: Cloned engines significantly outperform fresh engines (~5.6x at 1 thread)
|
||||
2. **Scaling Patterns with mimalloc**:
|
||||
- Best throughput achieved at 1 thread for both configurations
|
||||
- Performance degrades with increased thread count due to contention
|
||||
- Performance degrades with increased thread count due to contention, but mimalloc provides better allocation efficiency
|
||||
- Cloned engines show better relative scaling characteristics
|
||||
3. **Performance Hierarchy**:
|
||||
- Cloned engines: Best performance (optimal configuration)
|
||||
- Fresh engines: ~81% reduction from optimal
|
||||
4. **Thread Contention**: Significant performance drop beyond 8 threads, especially for fresh engines
|
||||
5. **C# vs Rust Performance**: C# shows ~67% of Rust performance for equivalent cloned engine configuration
|
||||
- Fresh engines: ~82% reduction from optimal
|
||||
4. **Thread Contention**: Significant performance drop beyond 8 threads, especially for fresh engines, though mimalloc helps mitigate some allocation-related issues
|
||||
5. **C# vs Rust Performance**: C# shows ~66% of Rust performance for equivalent cloned engine configuration
|
||||
|
||||
## Comparison with Rust Engine Evaluation
|
||||
|
||||
| Configuration | C# Performance (1 thread) | Rust Performance (1 thread) | Relative Performance |
|
||||
|:---------------|:---------------------------|:-----------------------------|---------------------:|
|
||||
| Cloned Engines | Best performance | Higher throughput | 0.67x-0.92x |
|
||||
| Fresh Engines | ~81% reduction from optimal| ~87% reduction from optimal | 0.75x-0.95x |
|
||||
### Multi-Thread Performance Comparison
|
||||
|
||||
| Configuration | 1 Thread (Kelem/s) | 4 Threads (Kelem/s) | 8 Threads (Kelem/s) |
|
||||
|:---------------|:-------------------|:--------------------|:--------------------|
|
||||
| | C# / Rust | C# / Rust | C# / Rust |
|
||||
| Cloned Engines | 279 / 423 | 217 / 406 | 114 / 341 |
|
||||
| Fresh Engines | 50 / 56 | 47 / 54 | 45 / 53 |
|
||||
|
||||
### Threading Efficiency Analysis
|
||||
|
||||
| Configuration | Low Contention (1-4t) | Medium Contention (6-12t) | High Contention (16+t) |
|
||||
|:---------------|:----------------------|:--------------------------|:-----------------------|
|
||||
| | Avg C# / Rust | Avg C# / Rust | Avg C# / Rust |
|
||||
| Cloned Engines | 253 / 414 | 128 / 329 | 54 / 250 |
|
||||
| Fresh Engines | 48 / 55 | 39 / 52 | 27 / 42 |
|
||||
|
||||
**Key Observations:**
|
||||
- **Single-threaded performance**: C# achieves 66% of Rust performance for cloned engines, 89% for fresh engines
|
||||
- **Threading scaling**: Both platforms show similar degradation patterns, but Rust maintains better absolute performance
|
||||
- **Contention resistance**: Fresh engines show more consistent relative performance across thread counts
|
||||
- **Platform differences**: C# shows more pronounced performance drops at higher thread counts, particularly for cloned engines
|
||||
|
||||
*Note: Rust benchmarks include additional input data variations (cloned vs fresh inputs) that are not present in the C# implementation.*
|
||||
|
||||
## Performance Insights
|
||||
|
||||
1. **Engine Creation Overhead**: Fresh engine creation has massive performance impact in C# (~5.3x slower)
|
||||
2. **Thread Scaling**: C# shows more significant thread contention than Rust implementation
|
||||
3. **Memory Management**: .NET garbage collection may contribute to performance variations
|
||||
4. **Interop Overhead**: C# bindings add measurable overhead compared to native Rust
|
||||
1. **Engine Creation Overhead**: Fresh engine creation has significant performance impact in C# (~5.6x slower than cloned engines)
|
||||
2. **Thread Scaling**: C# shows moderate thread contention with better characteristics when using mimalloc
|
||||
3. **Memory Management**: .NET garbage collection patterns combined with mimalloc allocation efficiency
|
||||
4. **Interop Performance**: C# bindings achieve 66% of Rust performance for cloned engines, demonstrating effective FFI implementation
|
||||
5. **mimalloc Benefits**: The use of mimalloc as the default allocator in the underlying Rust FFI provides improved memory allocation efficiency and better threading characteristics
|
||||
|
||||
|
||||
14
bindings/csharp/Directory.Packages.props
Normal file
14
bindings/csharp/Directory.Packages.props
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project>
|
||||
<PropertyGroup>
|
||||
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
|
||||
<RegorusPackageVersion>0.9.0</RegorusPackageVersion>
|
||||
<RegorusPackageVersionSuffix Condition="'$(VersionSuffix)' != ''">-$(VersionSuffix)</RegorusPackageVersionSuffix>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<!-- Centralize Regorus package version with optional CI suffix -->
|
||||
<PackageVersion Include="Regorus" Version="$(RegorusPackageVersion)$(RegorusPackageVersionSuffix)" />
|
||||
<PackageVersion Include="MSTest" Version="3.8.2" />
|
||||
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -29,4 +29,78 @@ Once the workflow run completes, the generated Nuget can be downloaded by follow
|
||||
|
||||
## Local
|
||||
|
||||
TODO
|
||||
The `cargo xtask` runner provides helpers for local builds:
|
||||
|
||||
1. `cargo xtask ffi` builds the `bindings/ffi` crate for the host platform in debug mode. Add `--target <triple>` (repeatable) to cross-compile, or `--release` to produce optimised artefacts. Results land under `bindings/ffi/target/<triple>/<profile>`.
|
||||
2. `cargo xtask nuget` reuses 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-artifacts` to require every officially supported platform.
|
||||
3. `cargo xtask test-csharp` ensures a NuGet is available (rebuilding when required or when `--force-nuget` is passed) and then runs `Regorus.Tests`, `TestApp`, and `TargetExampleApp` against it. The command accepts the same build flags as `cargo xtask nuget`.
|
||||
|
||||
## Memory Usage Safeguards
|
||||
|
||||
The C# bindings expose allocator-backed memory tracking utilities via the static `Regorus.MemoryLimits` helper. Typical usage:
|
||||
|
||||
```csharp
|
||||
// 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:
|
||||
|
||||
```csharp
|
||||
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}");
|
||||
```
|
||||
|
||||
131
bindings/csharp/Regorus.Tests/ExecutionTimerTests.cs
Normal file
131
bindings/csharp/Regorus.Tests/ExecutionTimerTests.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Regorus;
|
||||
|
||||
namespace Regorus.Tests;
|
||||
|
||||
[DoNotParallelize] // Uses global fallback config; must run sequentially.
|
||||
[TestClass]
|
||||
public class ExecutionTimerTests
|
||||
{
|
||||
private const string Policy = @"
|
||||
package limits.timer
|
||||
import rego.v1
|
||||
|
||||
triplet_count := count([1 |
|
||||
x := data.values[_]
|
||||
y := data.values[_]
|
||||
z := data.values[_]
|
||||
])
|
||||
";
|
||||
|
||||
private const string Query = "data.limits.timer.triplet_count";
|
||||
private const int ValueCount = 160;
|
||||
|
||||
[TestMethod]
|
||||
public void Engine_limit_enforced()
|
||||
{
|
||||
Engine.ClearFallbackExecutionTimerConfig();
|
||||
using var engine = CreateEngine(ValueCount);
|
||||
var config = new ExecutionTimerConfig(TimeSpan.FromMilliseconds(2), checkInterval: 1);
|
||||
engine.SetExecutionTimerConfig(config);
|
||||
|
||||
var ex = Assert.ThrowsException<InvalidOperationException>(() => engine.EvalRule(Query));
|
||||
StringAssert.Contains(ex.Message, "execution exceeded time limit");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Fallback_applies_to_new_engines()
|
||||
{
|
||||
var fallback = new ExecutionTimerConfig(TimeSpan.FromMilliseconds(2), checkInterval: 1);
|
||||
Engine.SetFallbackExecutionTimerConfig(fallback);
|
||||
try
|
||||
{
|
||||
using var engine = CreateEngine(ValueCount);
|
||||
var ex = Assert.ThrowsException<InvalidOperationException>(() => engine.EvalRule(Query));
|
||||
StringAssert.Contains(ex.Message, "execution exceeded time limit");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Engine.ClearFallbackExecutionTimerConfig();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Engine_override_relaxes_fallback()
|
||||
{
|
||||
var fallback = new ExecutionTimerConfig(TimeSpan.FromMilliseconds(2), checkInterval: 1);
|
||||
Engine.SetFallbackExecutionTimerConfig(fallback);
|
||||
try
|
||||
{
|
||||
using var engine = CreateEngine(ValueCount);
|
||||
var relaxed = new ExecutionTimerConfig(TimeSpan.FromSeconds(12), checkInterval: 1);
|
||||
engine.SetExecutionTimerConfig(relaxed);
|
||||
|
||||
var resultJson = engine.EvalRule(Query);
|
||||
var result = JsonSerializer.Deserialize<int>(resultJson!);
|
||||
Assert.IsTrue(result > 0, "Expected a positive triplet count when limit is relaxed.");
|
||||
|
||||
engine.ClearExecutionTimerConfig();
|
||||
var ex = Assert.ThrowsException<InvalidOperationException>(() => engine.EvalRule(Query));
|
||||
StringAssert.Contains(ex.Message, "execution exceeded time limit");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Engine.ClearFallbackExecutionTimerConfig();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CompiledPolicy_limit_enforced()
|
||||
{
|
||||
var fallback = new ExecutionTimerConfig(TimeSpan.FromMilliseconds(2), checkInterval: 1);
|
||||
Engine.SetFallbackExecutionTimerConfig(fallback);
|
||||
try
|
||||
{
|
||||
using var policy = CreateCompiledPolicy(ValueCount);
|
||||
var ex = Assert.ThrowsException<InvalidOperationException>(() => policy.EvalWithInput("null"));
|
||||
StringAssert.Contains(ex.Message, "execution exceeded time limit");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Engine.ClearFallbackExecutionTimerConfig();
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CompiledPolicy_uses_engine_limits_only()
|
||||
{
|
||||
// Compiled policies no longer store per-policy execution timers; limits are managed by Engine.
|
||||
Engine.ClearFallbackExecutionTimerConfig();
|
||||
using var policy = CreateCompiledPolicy(ValueCount);
|
||||
var resultJson = policy.EvalWithInput("null");
|
||||
var result = JsonSerializer.Deserialize<int>(resultJson!);
|
||||
Assert.IsTrue(result > 0, "CompiledPolicy should evaluate using engine defaults without its own timer");
|
||||
}
|
||||
|
||||
private static Engine CreateEngine(int valueCount)
|
||||
{
|
||||
var engine = new Engine();
|
||||
engine.AddPolicy("limits_timer.rego", Policy);
|
||||
engine.AddDataJson(CreateData(valueCount));
|
||||
return engine;
|
||||
}
|
||||
|
||||
private static CompiledPolicy CreateCompiledPolicy(int valueCount)
|
||||
{
|
||||
var modules = new[] { new PolicyModule("limits_timer.rego", Policy) };
|
||||
return Compiler.CompilePolicyWithEntrypoint(CreateData(valueCount), modules, Query);
|
||||
}
|
||||
|
||||
private static string CreateData(int valueCount)
|
||||
{
|
||||
var payload = new { values = Enumerable.Range(0, valueCount).ToArray() };
|
||||
return JsonSerializer.Serialize(payload);
|
||||
}
|
||||
}
|
||||
84
bindings/csharp/Regorus.Tests/PanicGuardTests.cs
Normal file
84
bindings/csharp/Regorus.Tests/PanicGuardTests.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
#if REGORUS_FFI_TEST_HOOKS
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Regorus.Internal;
|
||||
|
||||
namespace Regorus.Tests;
|
||||
|
||||
[TestClass]
|
||||
public sealed class PanicGuardTests
|
||||
{
|
||||
[TestInitialize]
|
||||
public void Initialize()
|
||||
{
|
||||
API.regorus_engine_test_reset_poison();
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
API.regorus_engine_test_reset_poison();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Panic_produces_invalid_operation_exception()
|
||||
{
|
||||
var panic = Assert.ThrowsException<InvalidOperationException>(TriggerPanic);
|
||||
StringAssert.Contains(panic.Message, "panicked", "panic message should capture payload");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Poison_flag_blocks_subsequent_calls()
|
||||
{
|
||||
_ = Assert.ThrowsException<InvalidOperationException>(TriggerPanic);
|
||||
var poisoned = Assert.ThrowsException<InvalidOperationException>(TriggerPanic);
|
||||
StringAssert.Contains(poisoned.Message, "poisoned", "poisoned message should explain guard state");
|
||||
}
|
||||
|
||||
private static unsafe void TriggerPanic()
|
||||
{
|
||||
var result = API.regorus_engine_test_trigger_panic();
|
||||
try
|
||||
{
|
||||
if (result.status == RegorusStatus.Ok)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var message = PtrToStringUtf8((IntPtr)result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
|
||||
private static string? PtrToStringUtf8(IntPtr ptr)
|
||||
{
|
||||
#if NETSTANDARD2_1
|
||||
return Marshal.PtrToStringUTF8(ptr);
|
||||
#else
|
||||
if (ptr == IntPtr.Zero)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var len = 0;
|
||||
while (Marshal.ReadByte(ptr, len) != 0)
|
||||
{
|
||||
len++;
|
||||
}
|
||||
|
||||
var buffer = new byte[len];
|
||||
Marshal.Copy(ptr, buffer, 0, buffer.Length);
|
||||
return System.Text.Encoding.UTF8.GetString(buffer);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -6,6 +6,7 @@
|
||||
<!-- More info about dotnet test integration https://learn.microsoft.com/dotnet/core/testing/unit-testing-platform-integration-dotnet-test -->
|
||||
<TestingPlatformDotnetTestSupport>true</TestingPlatformDotnetTestSupport>
|
||||
<TestingPlatformShowTestsFailure>true</TestingPlatformShowTestsFailure>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
@@ -18,10 +19,10 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MSTest" Version="3.8.2" />
|
||||
<PackageReference Include="MSTest" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Regorus" Version="0.6.0$(RegorusPackageVersionSuffix)"/>
|
||||
<PackageReference Include="Regorus" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -1,14 +1,19 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
namespace Regorus.Tests;
|
||||
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Regorus;
|
||||
|
||||
namespace Regorus.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class RegorusTests
|
||||
{
|
||||
private static readonly object LimitLock = new();
|
||||
|
||||
[TestMethod]
|
||||
public void Basic_evaluation_succeeds()
|
||||
{
|
||||
@@ -212,4 +217,174 @@ public class RegorusTests
|
||||
Assert.AreEqual("a", parameters![0]["parameters"][0]["name"].ToString());
|
||||
Assert.AreEqual("b", parameters![0]["modifiers"][0]["name"].ToString());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Global_memory_limit_can_be_set_and_cleared()
|
||||
{
|
||||
lock (LimitLock)
|
||||
{
|
||||
using var guard = new MemoryLimitScope();
|
||||
|
||||
MemoryLimits.SetGlobalMemoryLimit(null);
|
||||
Assert.IsNull(MemoryLimits.GetGlobalMemoryLimit());
|
||||
|
||||
const ulong limit = 32 * 1024;
|
||||
MemoryLimits.SetGlobalMemoryLimit(limit);
|
||||
Assert.AreEqual(limit, MemoryLimits.GetGlobalMemoryLimit());
|
||||
|
||||
MemoryLimits.SetGlobalMemoryLimit(null);
|
||||
Assert.IsNull(MemoryLimits.GetGlobalMemoryLimit());
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Memory_limit_violations_surface_from_engine_calls()
|
||||
{
|
||||
lock (LimitLock)
|
||||
{
|
||||
using var guard = new MemoryLimitScope();
|
||||
using var engine = new Engine();
|
||||
|
||||
const ulong limit = 1;
|
||||
var payload = new string('x', 128 * 1024);
|
||||
|
||||
MemoryLimits.FlushThreadMemoryCounters();
|
||||
MemoryLimits.SetGlobalMemoryLimit(limit);
|
||||
|
||||
try
|
||||
{
|
||||
var ex = Assert.ThrowsException<InvalidOperationException>(
|
||||
() => engine.SetInputJson($"{{\"payload\":\"{payload}\"}}"));
|
||||
StringAssert.Contains(ex.Message, "execution exceeded memory limit");
|
||||
}
|
||||
finally
|
||||
{
|
||||
MemoryLimits.SetGlobalMemoryLimit(null);
|
||||
MemoryLimits.FlushThreadMemoryCounters();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Evaluation_fails_when_input_pushes_policy_over_global_limit()
|
||||
{
|
||||
lock (LimitLock)
|
||||
{
|
||||
using var guard = new MemoryLimitScope();
|
||||
using var engine = new Engine();
|
||||
|
||||
const string policy = """
|
||||
package memorylimit
|
||||
|
||||
import rego.v1
|
||||
|
||||
stretched := concat("", [input.block | numbers.range(0, input.repeat - 1)[_]])
|
||||
""";
|
||||
|
||||
engine.AddPolicy("memorylimit.rego", policy);
|
||||
|
||||
MemoryLimits.FlushThreadMemoryCounters();
|
||||
const ulong limit = 4 * 1024 * 1024;
|
||||
MemoryLimits.SetGlobalMemoryLimit(limit);
|
||||
|
||||
var block = new string('x', 16 * 1024);
|
||||
|
||||
var smallInput = JsonSerializer.Serialize(new { block, repeat = 16 });
|
||||
engine.SetInputJson(smallInput);
|
||||
var smallResult = engine.EvalRule("data.memorylimit.stretched");
|
||||
Assert.IsNotNull(smallResult);
|
||||
var stretched = JsonSerializer.Deserialize<string>(smallResult);
|
||||
Assert.IsNotNull(stretched, "Policy should return a string result.");
|
||||
Assert.AreEqual(block.Length * 16, stretched!.Length, "Policy should expand the payload under the limit.");
|
||||
|
||||
var largeInput = JsonSerializer.Serialize(new { block, repeat = 4096 });
|
||||
engine.SetInputJson(largeInput);
|
||||
|
||||
var ex = Assert.ThrowsException<InvalidOperationException>(
|
||||
() => engine.EvalRule("data.memorylimit.stretched"));
|
||||
StringAssert.Contains(ex.Message, "execution exceeded memory limit");
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Thread_flush_threshold_roundtrips()
|
||||
{
|
||||
lock (LimitLock)
|
||||
{
|
||||
var original = MemoryLimits.GetThreadMemoryFlushThreshold();
|
||||
try
|
||||
{
|
||||
const ulong threshold = 256 * 1024;
|
||||
MemoryLimits.SetThreadFlushThresholdOverride(threshold);
|
||||
Assert.AreEqual(threshold, MemoryLimits.GetThreadMemoryFlushThreshold());
|
||||
|
||||
MemoryLimits.SetThreadFlushThresholdOverride(null);
|
||||
var restored = MemoryLimits.GetThreadMemoryFlushThreshold();
|
||||
Assert.IsTrue(restored.HasValue, "Clearing override should restore allocator default.");
|
||||
if (original.HasValue)
|
||||
{
|
||||
Assert.AreEqual(original, restored);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
MemoryLimits.SetThreadFlushThresholdOverride(original);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SetInputJson_has_negligible_allocations_after_warmup()
|
||||
{
|
||||
using var engine = new Engine();
|
||||
const string payload = "{}";
|
||||
|
||||
// Warm up the engine and JIT to ensure subsequent measurements are representative.
|
||||
for (int i = 0; i < 16; i++)
|
||||
{
|
||||
engine.SetInputJson(payload);
|
||||
}
|
||||
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
GC.Collect();
|
||||
|
||||
const int iterations = 256;
|
||||
var before = GC.GetAllocatedBytesForCurrentThread();
|
||||
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
engine.SetInputJson(payload);
|
||||
}
|
||||
|
||||
var after = GC.GetAllocatedBytesForCurrentThread();
|
||||
var allocated = Math.Max(0, after - before);
|
||||
var bytesPerOp = allocated / (double)iterations;
|
||||
|
||||
// Runtime bookkeeping (delegate caches, GC write barriers) differs across platforms, so
|
||||
// we measure bytes per call rather than absolute totals and allow a small budget.
|
||||
// CI will flag regressions where marshalling starts allocating per invocation.
|
||||
|
||||
// Allow a small budget for delegates and runtime bookkeeping while still flagging regressions.
|
||||
Assert.IsTrue(
|
||||
bytesPerOp <= 512,
|
||||
$"Expected ≤512 B/op after warmup, but observed {bytesPerOp:F2} B/op (total {allocated} bytes)."
|
||||
);
|
||||
}
|
||||
|
||||
private sealed class MemoryLimitScope : IDisposable
|
||||
{
|
||||
private readonly ulong? _originalLimit;
|
||||
|
||||
public MemoryLimitScope()
|
||||
{
|
||||
_originalLimit = MemoryLimits.GetGlobalMemoryLimit();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
MemoryLimits.SetGlobalMemoryLimit(_originalLimit);
|
||||
MemoryLimits.FlushThreadMemoryCounters();
|
||||
}
|
||||
}
|
||||
}
|
||||
119
bindings/csharp/Regorus.Tests/RvmProgramTests.cs
Normal file
119
bindings/csharp/Regorus.Tests/RvmProgramTests.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace Regorus.Tests;
|
||||
|
||||
[TestClass]
|
||||
public sealed class RvmProgramTests
|
||||
{
|
||||
private const string Policy = """
|
||||
package demo
|
||||
default allow = false
|
||||
allow if {
|
||||
input.user == "alice"
|
||||
some role in data.roles[input.user]
|
||||
role == "admin"
|
||||
count(input.actions) > 0
|
||||
}
|
||||
""";
|
||||
|
||||
private const string Data = """
|
||||
{
|
||||
"roles": {
|
||||
"alice": ["admin", "reader"]
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
private const string Input = """
|
||||
{
|
||||
"user": "alice",
|
||||
"actions": ["read"]
|
||||
}
|
||||
""";
|
||||
|
||||
private const string HostAwaitPolicy = """
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.account.active == true
|
||||
details := __builtin_host_await(input.account.id, "account")
|
||||
details.tier == "gold"
|
||||
}
|
||||
""";
|
||||
|
||||
private const string HostAwaitInput = """
|
||||
{
|
||||
"account": {
|
||||
"id": "acct-1",
|
||||
"active": true
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
[TestMethod]
|
||||
public void Program_compile_and_execute_succeeds()
|
||||
{
|
||||
var modules = new[] { new PolicyModule("demo.rego", Policy) };
|
||||
var entryPoints = new[] { "data.demo.allow" };
|
||||
|
||||
var program = Program.CompileFromModules(Data, modules, entryPoints);
|
||||
var listing = program.GenerateListing();
|
||||
Assert.IsFalse(string.IsNullOrWhiteSpace(listing), "listing should be generated");
|
||||
|
||||
var binary = program.SerializeBinary();
|
||||
var rehydrated = Program.DeserializeBinary(binary, out var isPartial);
|
||||
Assert.IsFalse(isPartial, "program should be fully deserialized");
|
||||
|
||||
using var vm = new Rvm();
|
||||
vm.LoadProgram(rehydrated);
|
||||
vm.SetDataJson(Data);
|
||||
vm.SetInputJson(Input);
|
||||
|
||||
var result = vm.Execute();
|
||||
Assert.AreEqual("true", result, "expected allow=true");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Program_compile_from_engine_succeeds()
|
||||
{
|
||||
using var engine = new Engine();
|
||||
engine.AddPolicy("demo.rego", Policy);
|
||||
|
||||
var program = Program.CompileFromEngine(engine, new[] { "data.demo.allow" });
|
||||
using var vm = new Rvm();
|
||||
vm.LoadProgram(program);
|
||||
vm.SetDataJson(Data);
|
||||
vm.SetInputJson(Input);
|
||||
|
||||
var result = vm.Execute();
|
||||
Assert.AreEqual("true", result, "expected allow=true");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Program_host_await_suspend_and_resume_succeeds()
|
||||
{
|
||||
var modules = new[] { new PolicyModule("host_await.rego", HostAwaitPolicy) };
|
||||
var entryPoints = new[] { "data.demo.allow" };
|
||||
|
||||
using var program = Program.CompileFromModules("{}", modules, entryPoints);
|
||||
using var vm = new Rvm();
|
||||
vm.SetExecutionMode(1);
|
||||
vm.LoadProgram(program);
|
||||
vm.SetInputJson(HostAwaitInput);
|
||||
|
||||
var initial = vm.Execute();
|
||||
var state = vm.GetExecutionState();
|
||||
Assert.IsNotNull(state, "execution state should be available");
|
||||
StringAssert.Contains(state!, "HostAwait", "expected HostAwait suspension");
|
||||
|
||||
var resumed = vm.Resume("{\"tier\":\"gold\"}");
|
||||
Assert.AreEqual("true", resumed, "expected allow=true after resume");
|
||||
}
|
||||
}
|
||||
3
bindings/csharp/Regorus/AssemblyInfo.cs
Normal file
3
bindings/csharp/Regorus/AssemblyInfo.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("Regorus.Tests")]
|
||||
@@ -2,8 +2,9 @@
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using Regorus.Internal;
|
||||
|
||||
#nullable enable
|
||||
namespace Regorus
|
||||
@@ -23,13 +24,14 @@ namespace Regorus
|
||||
/// </summary>
|
||||
public unsafe sealed class CompiledPolicy : IDisposable
|
||||
{
|
||||
private Internal.RegorusCompiledPolicy* _policy;
|
||||
private int _isDisposed;
|
||||
private int _activeEvaluations;
|
||||
private RegorusCompiledPolicyHandle? _handle;
|
||||
private readonly ManualResetEventSlim _idleEvent = new(initialState: true);
|
||||
private int _isDisposed;
|
||||
private int _activeEvaluations;
|
||||
|
||||
internal CompiledPolicy(Internal.RegorusCompiledPolicy* policy)
|
||||
internal CompiledPolicy(RegorusCompiledPolicyHandle handle)
|
||||
{
|
||||
_policy = policy;
|
||||
_handle = handle ?? throw new ArgumentNullException(nameof(handle));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -44,21 +46,34 @@ namespace Regorus
|
||||
public string? EvalWithInput(string inputJson)
|
||||
{
|
||||
// Increment active evaluations count
|
||||
System.Threading.Interlocked.Increment(ref _activeEvaluations);
|
||||
var active = System.Threading.Interlocked.Increment(ref _activeEvaluations);
|
||||
if (active == 1)
|
||||
{
|
||||
_idleEvent.Reset();
|
||||
}
|
||||
try
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
|
||||
var inputBytes = Encoding.UTF8.GetBytes(inputJson + char.MinValue);
|
||||
fixed (byte* inputPtr = inputBytes)
|
||||
|
||||
return Internal.Utf8Marshaller.WithUtf8(inputJson, inputPtr =>
|
||||
{
|
||||
return CheckAndDropResult(Internal.API.regorus_compiled_policy_eval_with_input(_policy, inputPtr));
|
||||
}
|
||||
return UseHandle(policyPtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Internal.API.regorus_compiled_policy_eval_with_input((Internal.RegorusCompiledPolicy*)policyPtr, (byte*)inputPtr));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Decrement active evaluations count
|
||||
System.Threading.Interlocked.Decrement(ref _activeEvaluations);
|
||||
var remaining = System.Threading.Interlocked.Decrement(ref _activeEvaluations);
|
||||
if (remaining == 0)
|
||||
{
|
||||
_idleEvent.Set();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +87,13 @@ namespace Regorus
|
||||
public PolicyInfo GetPolicyInfo()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
var jsonResult = CheckAndDropResult(Internal.API.regorus_compiled_policy_get_policy_info(_policy));
|
||||
var jsonResult = UseHandle(policyPtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Internal.API.regorus_compiled_policy_get_policy_info((Internal.RegorusCompiledPolicy*)policyPtr));
|
||||
}
|
||||
});
|
||||
|
||||
if (string.IsNullOrEmpty(jsonResult))
|
||||
{
|
||||
@@ -105,58 +126,42 @@ namespace Regorus
|
||||
{
|
||||
if (System.Threading.Interlocked.CompareExchange(ref _isDisposed, 1, 0) == 0)
|
||||
{
|
||||
if (_policy != null)
|
||||
var handle = _handle;
|
||||
if (handle != null)
|
||||
{
|
||||
// Wait for all active evaluations to complete
|
||||
while (System.Threading.Volatile.Read(ref _activeEvaluations) > 0)
|
||||
{
|
||||
System.Threading.Thread.Yield();
|
||||
}
|
||||
_idleEvent.Wait();
|
||||
|
||||
Internal.API.regorus_compiled_policy_drop(_policy);
|
||||
_policy = null;
|
||||
handle.Dispose();
|
||||
_handle = null;
|
||||
}
|
||||
|
||||
_idleEvent.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
~CompiledPolicy() => Dispose(disposing: false);
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
if (_isDisposed != 0)
|
||||
if (_isDisposed != 0 || _handle is null || _handle.IsClosed)
|
||||
throw new ObjectDisposedException(nameof(CompiledPolicy));
|
||||
}
|
||||
|
||||
private string? StringFromUTF8(IntPtr ptr)
|
||||
{
|
||||
#if NETSTANDARD2_1
|
||||
return System.Runtime.InteropServices.Marshal.PtrToStringUTF8(ptr);
|
||||
#else
|
||||
int len = 0;
|
||||
while (System.Runtime.InteropServices.Marshal.ReadByte(ptr, len) != 0) { ++len; }
|
||||
byte[] buffer = new byte[len];
|
||||
System.Runtime.InteropServices.Marshal.Copy(ptr, buffer, 0, buffer.Length);
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
private string? CheckAndDropResult(Internal.RegorusResult result)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != Internal.RegorusStatus.Ok)
|
||||
{
|
||||
var message = StringFromUTF8((IntPtr)result.error_message);
|
||||
throw new Exception(message ?? "Unknown error occurred");
|
||||
var message = Internal.Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
return result.data_type switch
|
||||
{
|
||||
Internal.RegorusDataType.String => StringFromUTF8((IntPtr)result.output),
|
||||
Internal.RegorusDataType.String => Internal.Utf8Marshaller.FromUtf8(result.output),
|
||||
Internal.RegorusDataType.Boolean => result.bool_value.ToString().ToLowerInvariant(),
|
||||
Internal.RegorusDataType.Integer => result.int_value.ToString(),
|
||||
Internal.RegorusDataType.None => null,
|
||||
_ => StringFromUTF8((IntPtr)result.output)
|
||||
_ => Internal.Utf8Marshaller.FromUtf8(result.output)
|
||||
};
|
||||
}
|
||||
finally
|
||||
@@ -164,5 +169,53 @@ namespace Regorus
|
||||
Internal.API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
|
||||
private RegorusCompiledPolicyHandle GetHandleForUse()
|
||||
{
|
||||
var handle = _handle;
|
||||
if (handle is null || handle.IsClosed || handle.IsInvalid)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(CompiledPolicy));
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
internal T UseHandle<T>(Func<IntPtr, T> func)
|
||||
{
|
||||
var handle = GetHandleForUse();
|
||||
bool addedRef = false;
|
||||
try
|
||||
{
|
||||
handle.DangerousAddRef(ref addedRef);
|
||||
var pointer = handle.DangerousGetHandle();
|
||||
if (pointer == IntPtr.Zero)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(CompiledPolicy));
|
||||
}
|
||||
|
||||
return func(pointer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (addedRef)
|
||||
{
|
||||
handle.DangerousRelease();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal T UseHandleForInterop<T>(Func<IntPtr, T> func)
|
||||
{
|
||||
return UseHandle(func);
|
||||
}
|
||||
|
||||
private void UseHandle(Action<IntPtr> action)
|
||||
{
|
||||
UseHandle<object?>(handlePtr =>
|
||||
{
|
||||
action(handlePtr);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Regorus.Internal;
|
||||
|
||||
#nullable enable
|
||||
namespace Regorus
|
||||
@@ -54,49 +53,48 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when compilation fails</exception>
|
||||
public static CompiledPolicy CompilePolicyWithEntrypoint(string dataJson, IEnumerable<PolicyModule> modules, string entryPointRule)
|
||||
{
|
||||
var dataBytes = Encoding.UTF8.GetBytes(dataJson + char.MinValue);
|
||||
var entryPointBytes = Encoding.UTF8.GetBytes(entryPointRule + char.MinValue);
|
||||
var modulesArray = modules.ToArray();
|
||||
|
||||
// Convert C# modules to native structs
|
||||
var nativeModules = new Internal.RegorusPolicyModule[modulesArray.Length];
|
||||
var pinnedHandles = new List<GCHandle>();
|
||||
var pinnedStrings = new List<Utf8Marshaller.PinnedUtf8>(modulesArray.Length * 2);
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < modulesArray.Length; i++)
|
||||
{
|
||||
var idBytes = Encoding.UTF8.GetBytes(modulesArray[i].Id + char.MinValue);
|
||||
var contentBytes = Encoding.UTF8.GetBytes(modulesArray[i].Content + char.MinValue);
|
||||
|
||||
var idHandle = GCHandle.Alloc(idBytes, GCHandleType.Pinned);
|
||||
var contentHandle = GCHandle.Alloc(contentBytes, GCHandleType.Pinned);
|
||||
pinnedHandles.Add(idHandle);
|
||||
pinnedHandles.Add(contentHandle);
|
||||
var idPinned = Utf8Marshaller.Pin(modulesArray[i].Id);
|
||||
var contentPinned = Utf8Marshaller.Pin(modulesArray[i].Content);
|
||||
pinnedStrings.Add(idPinned);
|
||||
pinnedStrings.Add(contentPinned);
|
||||
|
||||
nativeModules[i] = new Internal.RegorusPolicyModule
|
||||
{
|
||||
id = (byte*)idHandle.AddrOfPinnedObject(),
|
||||
content = (byte*)contentHandle.AddrOfPinnedObject()
|
||||
id = idPinned.Pointer,
|
||||
content = contentPinned.Pointer
|
||||
};
|
||||
}
|
||||
|
||||
fixed (byte* dataPtr = dataBytes)
|
||||
fixed (byte* entryPointPtr = entryPointBytes)
|
||||
fixed (Internal.RegorusPolicyModule* modulesPtr = nativeModules)
|
||||
{
|
||||
var result = Internal.API.regorus_compile_policy_with_entrypoint(
|
||||
dataPtr, modulesPtr, (UIntPtr)modulesArray.Length, entryPointPtr);
|
||||
return Utf8Marshaller.WithUtf8(dataJson, dataPtr =>
|
||||
Utf8Marshaller.WithUtf8(entryPointRule, entryPointPtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
fixed (Internal.RegorusPolicyModule* modulesPtr = nativeModules)
|
||||
{
|
||||
var result = Internal.API.regorus_compile_policy_with_entrypoint(
|
||||
(byte*)dataPtr, modulesPtr, (UIntPtr)modulesArray.Length, (byte*)entryPointPtr);
|
||||
|
||||
var policy = GetCompiledPolicyResult(result);
|
||||
return policy;
|
||||
}
|
||||
var policy = GetCompiledPolicyResult(result);
|
||||
return policy;
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach (var handle in pinnedHandles)
|
||||
foreach (var pinned in pinnedStrings)
|
||||
{
|
||||
handle.Free();
|
||||
pinned.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,72 +110,59 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when compilation fails</exception>
|
||||
public static CompiledPolicy CompilePolicyForTarget(string dataJson, IEnumerable<PolicyModule> modules)
|
||||
{
|
||||
var dataBytes = Encoding.UTF8.GetBytes(dataJson + char.MinValue);
|
||||
var modulesArray = modules.ToArray();
|
||||
|
||||
// Convert C# modules to native structs
|
||||
var nativeModules = new Internal.RegorusPolicyModule[modulesArray.Length];
|
||||
var pinnedHandles = new List<GCHandle>();
|
||||
var pinnedStrings = new List<Utf8Marshaller.PinnedUtf8>(modulesArray.Length * 2);
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < modulesArray.Length; i++)
|
||||
{
|
||||
var idBytes = Encoding.UTF8.GetBytes(modulesArray[i].Id + char.MinValue);
|
||||
var contentBytes = Encoding.UTF8.GetBytes(modulesArray[i].Content + char.MinValue);
|
||||
|
||||
var idHandle = GCHandle.Alloc(idBytes, GCHandleType.Pinned);
|
||||
var contentHandle = GCHandle.Alloc(contentBytes, GCHandleType.Pinned);
|
||||
pinnedHandles.Add(idHandle);
|
||||
pinnedHandles.Add(contentHandle);
|
||||
var idPinned = Utf8Marshaller.Pin(modulesArray[i].Id);
|
||||
var contentPinned = Utf8Marshaller.Pin(modulesArray[i].Content);
|
||||
pinnedStrings.Add(idPinned);
|
||||
pinnedStrings.Add(contentPinned);
|
||||
|
||||
nativeModules[i] = new Internal.RegorusPolicyModule
|
||||
{
|
||||
id = (byte*)idHandle.AddrOfPinnedObject(),
|
||||
content = (byte*)contentHandle.AddrOfPinnedObject()
|
||||
id = idPinned.Pointer,
|
||||
content = contentPinned.Pointer
|
||||
};
|
||||
}
|
||||
|
||||
fixed (byte* dataPtr = dataBytes)
|
||||
fixed (Internal.RegorusPolicyModule* modulesPtr = nativeModules)
|
||||
return Utf8Marshaller.WithUtf8(dataJson, dataPtr =>
|
||||
{
|
||||
var result = Internal.API.regorus_compile_policy_for_target(
|
||||
dataPtr, modulesPtr, (UIntPtr)modulesArray.Length);
|
||||
unsafe
|
||||
{
|
||||
fixed (Internal.RegorusPolicyModule* modulesPtr = nativeModules)
|
||||
{
|
||||
var result = Internal.API.regorus_compile_policy_for_target(
|
||||
(byte*)dataPtr, modulesPtr, (UIntPtr)modulesArray.Length);
|
||||
|
||||
var policy = GetCompiledPolicyResult(result);
|
||||
return policy;
|
||||
}
|
||||
var policy = GetCompiledPolicyResult(result);
|
||||
return policy;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach (var handle in pinnedHandles)
|
||||
foreach (var pinned in pinnedStrings)
|
||||
{
|
||||
handle.Free();
|
||||
pinned.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string? StringFromUTF8(IntPtr ptr)
|
||||
{
|
||||
#if NETSTANDARD2_1
|
||||
return System.Runtime.InteropServices.Marshal.PtrToStringUTF8(ptr);
|
||||
#else
|
||||
int len = 0;
|
||||
while (System.Runtime.InteropServices.Marshal.ReadByte(ptr, len) != 0) { ++len; }
|
||||
byte[] buffer = new byte[len];
|
||||
System.Runtime.InteropServices.Marshal.Copy(ptr, buffer, 0, buffer.Length);
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
private static CompiledPolicy GetCompiledPolicyResult(Internal.RegorusResult result)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != Internal.RegorusStatus.Ok)
|
||||
{
|
||||
var message = StringFromUTF8((IntPtr)result.error_message);
|
||||
throw new Exception(message ?? "Unknown compilation error occurred");
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
if (result.data_type != Internal.RegorusDataType.Pointer || result.pointer_value == null)
|
||||
@@ -185,7 +170,8 @@ namespace Regorus
|
||||
throw new Exception("Expected compiled policy pointer but got different data type");
|
||||
}
|
||||
|
||||
return new CompiledPolicy((Internal.RegorusCompiledPolicy*)result.pointer_value);
|
||||
var handle = RegorusCompiledPolicyHandle.FromPointer((IntPtr)result.pointer_value);
|
||||
return new CompiledPolicy(handle);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Regorus.Internal;
|
||||
|
||||
|
||||
#nullable enable
|
||||
@@ -15,17 +16,25 @@ namespace Regorus
|
||||
/// Cloning is cheap and involves only incrementing reference counts for shared immutable objects like parsed policies,
|
||||
/// data etc. Mutable state is deep copied as needed.
|
||||
/// </summary>
|
||||
public unsafe sealed class Engine : System.IDisposable
|
||||
public unsafe sealed class Engine : IDisposable
|
||||
{
|
||||
private Regorus.Internal.RegorusEngine* E;
|
||||
// Detect redundant Dispose() calls in a thread-safe manner.
|
||||
// _isDisposed == 0 means Dispose(bool) has not been called yet.
|
||||
// _isDisposed == 1 means Dispose(bool) has been already called.
|
||||
private int isDisposed;
|
||||
private RegorusEngineHandle? _handle;
|
||||
private int _isDisposed;
|
||||
|
||||
public Engine()
|
||||
{
|
||||
E = Regorus.Internal.API.regorus_engine_new();
|
||||
_handle = RegorusEngineHandle.Create();
|
||||
}
|
||||
|
||||
public static void SetFallbackExecutionTimerConfig(ExecutionTimerConfig config)
|
||||
{
|
||||
var nativeConfig = config.ToNative();
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_set_fallback_execution_timer_config(nativeConfig));
|
||||
}
|
||||
|
||||
public static void ClearFallbackExecutionTimerConfig()
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_clear_fallback_execution_timer_config());
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
@@ -49,189 +58,347 @@ namespace Regorus
|
||||
// other objects. Only unmanaged resources can be disposed.
|
||||
void Dispose(bool disposing)
|
||||
{
|
||||
// In case _isDisposed is 0, atomically set it to 1.
|
||||
// Enter the branch only if the original value is 0.
|
||||
if (System.Threading.Interlocked.CompareExchange(ref isDisposed, 1, 0) == 0)
|
||||
if (System.Threading.Interlocked.CompareExchange(ref _isDisposed, 1, 0) == 0)
|
||||
{
|
||||
// If disposing equals true, dispose all managed
|
||||
// and unmanaged resources.
|
||||
if (disposing)
|
||||
{
|
||||
// No managed resource to dispose.
|
||||
}
|
||||
|
||||
// Call the appropriate methods to clean up
|
||||
// unmanaged resources here.
|
||||
// If disposing is false,
|
||||
// only the following code is executed.
|
||||
if (E != null)
|
||||
{
|
||||
Regorus.Internal.API.regorus_engine_drop(E);
|
||||
E = null;
|
||||
}
|
||||
|
||||
_handle?.Dispose();
|
||||
_handle = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Use C# finalizer syntax for finalization code.
|
||||
// This finalizer will run only if the Dispose method
|
||||
// does not get called.
|
||||
~Engine() => Dispose(disposing: false);
|
||||
|
||||
// Helper for implementing Clone
|
||||
private Engine(Internal.RegorusEngine* engine)
|
||||
private Engine(RegorusEngineHandle handle)
|
||||
{
|
||||
this.E = engine;
|
||||
_handle = handle ?? throw new ArgumentNullException(nameof(handle));
|
||||
}
|
||||
|
||||
public Engine Clone() => new(Internal.API.regorus_engine_clone(E));
|
||||
public Engine Clone()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var clonePtr = Regorus.Internal.API.regorus_engine_clone((Regorus.Internal.RegorusEngine*)enginePtr);
|
||||
if (clonePtr is null)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to clone Regorus engine.");
|
||||
}
|
||||
|
||||
var handle = RegorusEngineHandle.FromPointer((IntPtr)clonePtr);
|
||||
return new Engine(handle);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void SetStrictBuiltinErrors(bool strict)
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_strict_builtin_errors(E, strict));
|
||||
}
|
||||
byte[] NullTerminatedUTF8Bytes(string s)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(s + char.MinValue);
|
||||
ThrowIfDisposed();
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_strict_builtin_errors((Regorus.Internal.RegorusEngine*)enginePtr, strict));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void SetExecutionTimerConfig(ExecutionTimerConfig config)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
var nativeConfig = config.ToNative();
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var localConfig = nativeConfig;
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_execution_timer_config((Regorus.Internal.RegorusEngine*)enginePtr, &localConfig));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void ClearExecutionTimerConfig()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_clear_execution_timer_config((Regorus.Internal.RegorusEngine*)enginePtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
public string? AddPolicy(string path, string rego)
|
||||
{
|
||||
var pathBytes = NullTerminatedUTF8Bytes(path);
|
||||
var regoBytes = NullTerminatedUTF8Bytes(rego);
|
||||
|
||||
|
||||
fixed (byte* pathPtr = pathBytes)
|
||||
{
|
||||
fixed (byte* regoPtr = regoBytes)
|
||||
ThrowIfDisposed();
|
||||
return Utf8Marshaller.WithUtf8(path, pathPtr =>
|
||||
Utf8Marshaller.WithUtf8(rego, regoPtr =>
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_add_policy(E, pathPtr, regoPtr));
|
||||
}
|
||||
}
|
||||
|
||||
unsafe
|
||||
{
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_add_policy((Regorus.Internal.RegorusEngine*)enginePtr, (byte*)pathPtr, (byte*)regoPtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public void SetRegoV0(bool enable)
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_rego_v0(E, enable));
|
||||
ThrowIfDisposed();
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_rego_v0((Regorus.Internal.RegorusEngine*)enginePtr, enable));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string? AddPolicyFromFile(string path)
|
||||
{
|
||||
var pathBytes = NullTerminatedUTF8Bytes(path);
|
||||
fixed (byte* pathPtr = pathBytes)
|
||||
ThrowIfDisposed();
|
||||
return Utf8Marshaller.WithUtf8(path, pathPtr =>
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_add_policy_from_file(E, pathPtr));
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_add_policy_from_file((Regorus.Internal.RegorusEngine*)enginePtr, (byte*)pathPtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void AddDataJson(string data)
|
||||
{
|
||||
var dataBytes = NullTerminatedUTF8Bytes(data);
|
||||
fixed (byte* dataPtr = dataBytes)
|
||||
ThrowIfDisposed();
|
||||
Utf8Marshaller.WithUtf8(data, dataPtr =>
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_add_data_json(E, dataPtr));
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_add_data_json((Regorus.Internal.RegorusEngine*)enginePtr, (byte*)dataPtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void AddDataFromJsonFile(string path)
|
||||
{
|
||||
var pathBytes = NullTerminatedUTF8Bytes(path);
|
||||
fixed (byte* pathPtr = pathBytes)
|
||||
ThrowIfDisposed();
|
||||
Utf8Marshaller.WithUtf8(path, pathPtr =>
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_add_data_from_json_file(E, pathPtr));
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_add_data_from_json_file((Regorus.Internal.RegorusEngine*)enginePtr, (byte*)pathPtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
public void SetInputJson(string input)
|
||||
{
|
||||
var inputBytes = NullTerminatedUTF8Bytes(input);
|
||||
fixed (byte* inputPtr = inputBytes)
|
||||
ThrowIfDisposed();
|
||||
Utf8Marshaller.WithUtf8(input, inputPtr =>
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_input_json(E, inputPtr));
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_input_json((Regorus.Internal.RegorusEngine*)enginePtr, (byte*)inputPtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void SetInputFromJsonFile(string path)
|
||||
{
|
||||
var pathBytes = NullTerminatedUTF8Bytes(path);
|
||||
fixed (byte* pathPtr = pathBytes)
|
||||
ThrowIfDisposed();
|
||||
Utf8Marshaller.WithUtf8(path, pathPtr =>
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_input_from_json_file(E, pathPtr));
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_input_from_json_file((Regorus.Internal.RegorusEngine*)enginePtr, (byte*)pathPtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string? EvalQuery(string query)
|
||||
{
|
||||
var queryBytes = NullTerminatedUTF8Bytes(query);
|
||||
fixed (byte* queryPtr = queryBytes)
|
||||
ThrowIfDisposed();
|
||||
return Utf8Marshaller.WithUtf8(query, queryPtr =>
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_eval_query(E, queryPtr));
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_eval_query((Regorus.Internal.RegorusEngine*)enginePtr, (byte*)queryPtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string? EvalRule(string rule)
|
||||
{
|
||||
var ruleBytes = NullTerminatedUTF8Bytes(rule);
|
||||
fixed (byte* rulePtr = ruleBytes)
|
||||
ThrowIfDisposed();
|
||||
return Utf8Marshaller.WithUtf8(rule, rulePtr =>
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_eval_rule(E, rulePtr));
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_eval_rule((Regorus.Internal.RegorusEngine*)enginePtr, (byte*)rulePtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void SetEnableCoverage(bool enable)
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_enable_coverage(E, enable));
|
||||
ThrowIfDisposed();
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_enable_coverage((Regorus.Internal.RegorusEngine*)enginePtr, enable));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void ClearCoverageData()
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_clear_coverage_data(E));
|
||||
ThrowIfDisposed();
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_clear_coverage_data((Regorus.Internal.RegorusEngine*)enginePtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string? GetCoverageReport()
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_coverage_report(E));
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_coverage_report((Regorus.Internal.RegorusEngine*)enginePtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string? GetCoverageReportPretty()
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_coverage_report_pretty(E));
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_coverage_report_pretty((Regorus.Internal.RegorusEngine*)enginePtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void SetGatherPrints(bool enable)
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_gather_prints(E, enable));
|
||||
ThrowIfDisposed();
|
||||
UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Regorus.Internal.API.regorus_engine_set_gather_prints((Regorus.Internal.RegorusEngine*)enginePtr, enable));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string? TakePrints()
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_take_prints(E));
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_take_prints((Regorus.Internal.RegorusEngine*)enginePtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string? GetAstAsJson()
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_ast_as_json(E));
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_ast_as_json((Regorus.Internal.RegorusEngine*)enginePtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string? GetPolicyPackageNames()
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_policy_package_names(E));
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_policy_package_names((Regorus.Internal.RegorusEngine*)enginePtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public string? GetPolicyParameters()
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_policy_parameters(E));
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(enginePtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return CheckAndDropResult(Regorus.Internal.API.regorus_engine_get_policy_parameters((Regorus.Internal.RegorusEngine*)enginePtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
string? StringFromUTF8(IntPtr ptr)
|
||||
private static string? StringFromUtf8(IntPtr ptr)
|
||||
{
|
||||
|
||||
#if NETSTANDARD2_1
|
||||
return System.Runtime.InteropServices.Marshal.PtrToStringUTF8(ptr);
|
||||
return Marshal.PtrToStringUTF8(ptr);
|
||||
#else
|
||||
int len = 0;
|
||||
while (Marshal.ReadByte(ptr, len) != 0) { ++len; }
|
||||
@@ -241,23 +408,85 @@ namespace Regorus
|
||||
#endif
|
||||
}
|
||||
|
||||
string? CheckAndDropResult(Regorus.Internal.RegorusResult result)
|
||||
private static string? CheckAndDropResult(Regorus.Internal.RegorusResult result)
|
||||
{
|
||||
if (result.status != Regorus.Internal.RegorusStatus.Ok)
|
||||
try
|
||||
{
|
||||
var message = StringFromUTF8((IntPtr)result.error_message);
|
||||
var ex = new Exception(message);
|
||||
Regorus.Internal.API.regorus_result_drop(result);
|
||||
throw ex;
|
||||
}
|
||||
if (result.status != Regorus.Internal.RegorusStatus.Ok)
|
||||
{
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
var resultString = "";
|
||||
if (result.output is not null)
|
||||
{
|
||||
resultString = StringFromUTF8((IntPtr)result.output);
|
||||
return result.data_type switch
|
||||
{
|
||||
Regorus.Internal.RegorusDataType.String => Utf8Marshaller.FromUtf8(result.output),
|
||||
Regorus.Internal.RegorusDataType.Boolean => result.bool_value.ToString().ToLowerInvariant(),
|
||||
Regorus.Internal.RegorusDataType.Integer => result.int_value.ToString(),
|
||||
Regorus.Internal.RegorusDataType.None => null,
|
||||
_ => Utf8Marshaller.FromUtf8(result.output)
|
||||
};
|
||||
}
|
||||
Regorus.Internal.API.regorus_result_drop(result);
|
||||
return resultString;
|
||||
finally
|
||||
{
|
||||
Regorus.Internal.API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
if (_isDisposed != 0 || _handle is null || _handle.IsClosed)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Engine));
|
||||
}
|
||||
}
|
||||
|
||||
internal RegorusEngineHandle GetHandleForUse()
|
||||
{
|
||||
var handle = _handle;
|
||||
if (handle is null || handle.IsClosed || handle.IsInvalid)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Engine));
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
internal void UseHandle(Action<IntPtr> action)
|
||||
{
|
||||
UseHandle<object?>(handlePtr =>
|
||||
{
|
||||
action(handlePtr);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
internal T UseHandle<T>(Func<IntPtr, T> func)
|
||||
{
|
||||
var handle = GetHandleForUse();
|
||||
bool addedRef = false;
|
||||
try
|
||||
{
|
||||
handle.DangerousAddRef(ref addedRef);
|
||||
var pointer = handle.DangerousGetHandle();
|
||||
if (pointer == IntPtr.Zero)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Engine));
|
||||
}
|
||||
|
||||
return func(pointer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (addedRef)
|
||||
{
|
||||
handle.DangerousRelease();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal T UseHandleForInterop<T>(Func<IntPtr, T> func)
|
||||
{
|
||||
return UseHandle(func);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
62
bindings/csharp/Regorus/ExecutionTimerConfig.cs
Normal file
62
bindings/csharp/Regorus/ExecutionTimerConfig.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
|
||||
namespace Regorus
|
||||
{
|
||||
/// <summary>
|
||||
/// Managed representation of the execution timer configuration used by the engine.
|
||||
/// </summary>
|
||||
public readonly struct ExecutionTimerConfig
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ExecutionTimerConfig"/> struct.
|
||||
/// </summary>
|
||||
/// <param name="limit">Maximum wall-clock duration allowed for evaluation. Must be non-negative.</param>
|
||||
/// <param name="checkInterval">Number of work units between timer checks. Must be non-zero.</param>
|
||||
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="limit"/> is negative or <paramref name="checkInterval"/> is zero.</exception>
|
||||
public ExecutionTimerConfig(TimeSpan limit, uint checkInterval)
|
||||
{
|
||||
if (limit < TimeSpan.Zero)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(limit), "Execution timer limit must be non-negative.");
|
||||
}
|
||||
|
||||
if (checkInterval == 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(checkInterval), "Execution timer check interval must be non-zero.");
|
||||
}
|
||||
|
||||
Limit = limit;
|
||||
CheckInterval = checkInterval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Maximum wall-clock duration allowed for an evaluation.
|
||||
/// </summary>
|
||||
public TimeSpan Limit { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Number of work units between timer checks.
|
||||
/// </summary>
|
||||
public uint CheckInterval { get; }
|
||||
|
||||
internal Regorus.Internal.RegorusExecutionTimerConfig ToNative()
|
||||
{
|
||||
if (Limit < TimeSpan.Zero)
|
||||
{
|
||||
throw new InvalidOperationException("Execution timer limit must be non-negative.");
|
||||
}
|
||||
|
||||
ulong ticks = checked((ulong)Limit.Ticks);
|
||||
ulong limitNanoseconds = checked(ticks * 100UL);
|
||||
|
||||
return new Regorus.Internal.RegorusExecutionTimerConfig
|
||||
{
|
||||
limit_ns = limitNanoseconds,
|
||||
check_interval = CheckInterval,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
126
bindings/csharp/Regorus/MemoryLimits.cs
Normal file
126
bindings/csharp/Regorus/MemoryLimits.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using Regorus.Internal;
|
||||
|
||||
#nullable enable
|
||||
namespace Regorus
|
||||
{
|
||||
/// <summary>
|
||||
/// Helpers for configuring and inspecting Regorus memory limits via the native allocator bridge.
|
||||
/// </summary>
|
||||
public static class MemoryLimits
|
||||
{
|
||||
/// <summary>
|
||||
/// Configure the process-wide global memory limit in bytes. Pass <c>null</c> to remove the limit.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Maximum number of bytes the allocator may reserve before signalling an error.</param>
|
||||
public static void SetGlobalMemoryLimit(ulong? bytes)
|
||||
{
|
||||
var result = API.regorus_set_global_memory_limit(bytes ?? 0, bytes.HasValue);
|
||||
EnsureSuccess(result, nameof(SetGlobalMemoryLimit));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the currently configured global memory limit, if any.
|
||||
/// </summary>
|
||||
public static ulong? GetGlobalMemoryLimit()
|
||||
{
|
||||
var result = API.regorus_get_global_memory_limit();
|
||||
return ExtractOptionalU64(result, "Failed to get global memory limit");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Forces the allocator to flush this thread's pending counters into the global aggregates.
|
||||
/// </summary>
|
||||
public static void FlushThreadMemoryCounters()
|
||||
{
|
||||
var result = API.regorus_flush_thread_memory_counters();
|
||||
EnsureSuccess(result, nameof(FlushThreadMemoryCounters));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Immediately checks the global memory limit and throws if the allocator reports exhaustion.
|
||||
/// </summary>
|
||||
public static void CheckGlobalMemoryLimit()
|
||||
{
|
||||
var result = API.regorus_check_global_memory_limit();
|
||||
EnsureSuccess(result, nameof(CheckGlobalMemoryLimit));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Override the per-thread automatic flush threshold in bytes. Pass <c>null</c> to restore the default.
|
||||
/// </summary>
|
||||
public static void SetThreadFlushThresholdOverride(ulong? bytes)
|
||||
{
|
||||
var result = API.regorus_set_thread_flush_threshold_override(bytes ?? 0, bytes.HasValue);
|
||||
EnsureSuccess(result, nameof(SetThreadFlushThresholdOverride));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the per-thread flush threshold, if automatic flushing is enabled.
|
||||
/// </summary>
|
||||
public static ulong? GetThreadMemoryFlushThreshold()
|
||||
{
|
||||
var result = API.regorus_get_thread_memory_flush_threshold();
|
||||
return ExtractOptionalU64(result, "Failed to get thread memory flush threshold");
|
||||
}
|
||||
|
||||
private static unsafe ulong? ExtractOptionalU64(RegorusResult result, string errorContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != RegorusStatus.Ok)
|
||||
{
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message) ?? $"{errorContext}: native call failed";
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
if (!result.bool_value)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (result.data_type != RegorusDataType.Integer)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"{errorContext}: native call returned {result.data_type} ({(int)result.data_type}) with bool_value={result.bool_value}"
|
||||
);
|
||||
}
|
||||
|
||||
if (result.int_value < 0)
|
||||
{
|
||||
throw new OverflowException($"{errorContext}: native value was negative ({result.int_value})");
|
||||
}
|
||||
|
||||
return (ulong)result.int_value;
|
||||
}
|
||||
finally
|
||||
{
|
||||
API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
|
||||
private static void EnsureSuccess(RegorusResult result, string operation)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != RegorusStatus.Ok)
|
||||
{
|
||||
string? message;
|
||||
unsafe
|
||||
{
|
||||
message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
}
|
||||
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,53 @@ namespace Regorus.Internal
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_result_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern void regorus_result_drop(RegorusResult result);
|
||||
|
||||
/// <summary>
|
||||
/// Drop a RegorusBuffer.
|
||||
/// data is not valid after drop.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_buffer_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern void regorus_buffer_drop(RegorusBuffer* buffer);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Memory Limit Methods
|
||||
|
||||
/// <summary>
|
||||
/// Set the global memory limit. Pass hasLimit=false to clear the limit.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_set_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_set_global_memory_limit(ulong limit, [MarshalAs(UnmanagedType.U1)] bool hasLimit);
|
||||
|
||||
/// <summary>
|
||||
/// Get the current global memory limit. bool_value indicates whether a limit is set.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_get_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_get_global_memory_limit();
|
||||
|
||||
/// <summary>
|
||||
/// Check the global memory limit immediately.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_check_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_check_global_memory_limit();
|
||||
|
||||
/// <summary>
|
||||
/// Flush the current thread's pending allocation counters into global aggregates.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_flush_thread_memory_counters", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_flush_thread_memory_counters();
|
||||
|
||||
/// <summary>
|
||||
/// Set the per-thread flush threshold override. Pass hasThreshold=false to restore defaults.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_set_thread_flush_threshold_override", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_set_thread_flush_threshold_override(ulong threshold, [MarshalAs(UnmanagedType.U1)] bool hasThreshold);
|
||||
|
||||
/// <summary>
|
||||
/// Get the per-thread flush threshold. bool_value indicates whether a threshold is configured.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_get_thread_memory_flush_threshold", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_get_thread_memory_flush_threshold();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Engine Methods
|
||||
@@ -45,6 +92,12 @@ namespace Regorus.Internal
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_engine_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusEngine* regorus_engine_clone(RegorusEngine* engine);
|
||||
|
||||
/// <summary>
|
||||
/// Compile an RVM program from the engine state with entry points.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_engine_compile_program_with_entrypoints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_engine_compile_program_with_entrypoints(RegorusEngine* engine, byte** entryPoints, UIntPtr entryPointsLen);
|
||||
|
||||
/// <summary>
|
||||
/// Drop a RegorusEngine.
|
||||
/// </summary>
|
||||
@@ -52,6 +105,138 @@ namespace Regorus.Internal
|
||||
internal static extern void regorus_engine_drop(RegorusEngine* engine);
|
||||
|
||||
/// <summary>
|
||||
|
||||
/// <summary>
|
||||
/// Compile an RVM program from data/modules and entry points.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_program_compile_from_modules", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_program_compile_from_modules(byte* data_json, RegorusPolicyModule* modules, UIntPtr modules_len, byte** entry_points, UIntPtr entry_points_len);
|
||||
|
||||
/// <summary>
|
||||
/// Construct a new empty program.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_program_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusProgram* regorus_program_new();
|
||||
|
||||
/// <summary>
|
||||
/// Drop a program handle.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_program_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern void regorus_program_drop(RegorusProgram* program);
|
||||
|
||||
/// <summary>
|
||||
/// Serialize a program to binary format.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_program_serialize_binary", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_program_serialize_binary(RegorusProgram* program);
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize a program from binary format.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_program_deserialize_binary", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_program_deserialize_binary(byte* data, UIntPtr len, byte* is_partial);
|
||||
|
||||
/// <summary>
|
||||
/// Generate a readable assembly listing for the program.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_program_generate_listing", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_program_generate_listing(RegorusProgram* program);
|
||||
|
||||
/// <summary>
|
||||
/// Create a new RVM instance.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusRvm* regorus_rvm_new();
|
||||
|
||||
/// <summary>
|
||||
/// Create a new RVM instance from a compiled policy.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_new_with_policy", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_new_with_policy(RegorusCompiledPolicy* compiled_policy);
|
||||
|
||||
/// <summary>
|
||||
/// Drop an RVM instance.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern void regorus_rvm_drop(RegorusRvm* vm);
|
||||
|
||||
/// <summary>
|
||||
/// Load a program into the RVM.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_load_program", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_load_program(RegorusRvm* vm, RegorusProgram* program);
|
||||
|
||||
/// <summary>
|
||||
/// Set the data document for the RVM.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_set_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_set_data(RegorusRvm* vm, byte* data_json);
|
||||
|
||||
/// <summary>
|
||||
/// Set the input document for the RVM.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_set_input", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_set_input(RegorusRvm* vm, byte* input_json);
|
||||
|
||||
/// <summary>
|
||||
/// Execute the program.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_execute", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_execute(RegorusRvm* vm);
|
||||
|
||||
/// <summary>
|
||||
/// Execute an entry point by name.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_execute_entry_point_by_name", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_execute_entry_point_by_name(RegorusRvm* vm, byte* entry_point);
|
||||
|
||||
/// <summary>
|
||||
/// Execute an entry point by index.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_execute_entry_point_by_index", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_execute_entry_point_by_index(RegorusRvm* vm, UIntPtr index);
|
||||
|
||||
/// <summary>
|
||||
/// Resume execution.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_resume", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_resume(RegorusRvm* vm, byte* resume_value_json, [MarshalAs(UnmanagedType.I1)] bool has_value);
|
||||
|
||||
/// <summary>
|
||||
/// Get the current execution state.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_get_execution_state", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_get_execution_state(RegorusRvm* vm);
|
||||
|
||||
/// <summary>
|
||||
/// Set the maximum instruction limit.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_set_max_instructions", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_set_max_instructions(RegorusRvm* vm, UIntPtr max_instructions);
|
||||
|
||||
/// <summary>
|
||||
/// Set strict builtin error handling.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_set_strict_builtin_errors", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_set_strict_builtin_errors(RegorusRvm* vm, [MarshalAs(UnmanagedType.I1)] bool strict);
|
||||
|
||||
/// <summary>
|
||||
/// Set execution mode (0 run-to-completion, 1 suspendable).
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_set_execution_mode", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_set_execution_mode(RegorusRvm* vm, byte mode);
|
||||
|
||||
/// <summary>
|
||||
/// Set step mode for suspendable execution.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_set_step_mode", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_set_step_mode(RegorusRvm* vm, [MarshalAs(UnmanagedType.I1)] bool enabled);
|
||||
|
||||
/// <summary>
|
||||
/// Set execution timer configuration.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_rvm_set_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_rvm_set_execution_timer_config(RegorusRvm* vm, [MarshalAs(UnmanagedType.I1)] bool has_config, RegorusExecutionTimerConfig config);
|
||||
/// Add a policy.
|
||||
/// The policy is parsed into AST.
|
||||
/// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.add_policy
|
||||
@@ -217,6 +402,48 @@ namespace Regorus.Internal
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_engine_compile_with_entrypoint", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_engine_compile_with_entrypoint(RegorusEngine* engine, byte* rule);
|
||||
|
||||
#if REGORUS_FFI_TEST_HOOKS
|
||||
/// <summary>
|
||||
/// Trigger a panic inside the engine for testing purposes.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_engine_test_trigger_panic", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_engine_test_trigger_panic();
|
||||
|
||||
/// <summary>
|
||||
/// Reset the engine poison flag for testing.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_engine_test_reset_poison", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern void regorus_engine_test_reset_poison();
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Configure the execution timer for a specific engine instance.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_engine_set_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_engine_set_execution_timer_config(RegorusEngine* engine, RegorusExecutionTimerConfig* config);
|
||||
|
||||
/// <summary>
|
||||
/// Clear the execution timer configuration for a specific engine instance.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_engine_clear_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_engine_clear_execution_timer_config(RegorusEngine* engine);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Execution Timer Global Methods
|
||||
|
||||
/// <summary>
|
||||
/// Set the process-wide fallback execution timer configuration.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_set_fallback_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_set_fallback_execution_timer_config(RegorusExecutionTimerConfig config);
|
||||
|
||||
/// <summary>
|
||||
/// Clear the process-wide fallback execution timer configuration.
|
||||
/// </summary>
|
||||
[DllImport(LibraryName, EntryPoint = "regorus_clear_fallback_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
|
||||
internal static extern RegorusResult regorus_clear_fallback_execution_timer_config();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Compilation Methods
|
||||
@@ -472,6 +699,14 @@ namespace Regorus.Internal
|
||||
/// Invalid policy content.
|
||||
/// </summary>
|
||||
InvalidPolicy,
|
||||
/// <summary>
|
||||
/// The engine panicked and cannot be reused until reset.
|
||||
/// </summary>
|
||||
Panic,
|
||||
/// <summary>
|
||||
/// The engine remains poisoned because a previous panic was detected.
|
||||
/// </summary>
|
||||
Poisoned,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -498,6 +733,7 @@ namespace Regorus.Internal
|
||||
/// Boolean value.
|
||||
/// Valid when data_type is Boolean.
|
||||
/// </summary>
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool bool_value;
|
||||
/// <summary>
|
||||
/// Integer value.
|
||||
@@ -516,6 +752,27 @@ namespace Regorus.Internal
|
||||
public byte* error_message;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// FFI representation of the execution timer configuration.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal struct RegorusExecutionTimerConfig
|
||||
{
|
||||
public ulong limit_ns;
|
||||
public uint check_interval;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Byte buffer returned from FFI.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal unsafe struct RegorusBuffer
|
||||
{
|
||||
public byte* data;
|
||||
public UIntPtr len;
|
||||
public UIntPtr capacity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wrapper for regorus::Engine.
|
||||
/// </summary>
|
||||
@@ -532,6 +789,22 @@ namespace Regorus.Internal
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wrapper for regorus::rvm::Program.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal unsafe partial struct RegorusProgram
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wrapper for regorus::rvm::RegoVM.
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal unsafe partial struct RegorusRvm
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// FFI wrapper for PolicyModule struct.
|
||||
/// </summary>
|
||||
|
||||
333
bindings/csharp/Regorus/Program.cs
Normal file
333
bindings/csharp/Regorus/Program.cs
Normal file
@@ -0,0 +1,333 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using Regorus.Internal;
|
||||
|
||||
#nullable enable
|
||||
namespace Regorus
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a compiled RVM program.
|
||||
/// </summary>
|
||||
public unsafe sealed class Program : IDisposable
|
||||
{
|
||||
private RegorusProgramHandle? _handle;
|
||||
private int _isDisposed;
|
||||
|
||||
private Program(RegorusProgramHandle handle)
|
||||
{
|
||||
_handle = handle ?? throw new ArgumentNullException(nameof(handle));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an empty program.
|
||||
/// </summary>
|
||||
public static Program CreateEmpty()
|
||||
{
|
||||
return new Program(RegorusProgramHandle.Create());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compile an RVM program from modules and entry points.
|
||||
/// </summary>
|
||||
public static Program CompileFromModules(string dataJson, IEnumerable<PolicyModule> modules, IEnumerable<string> entryPoints)
|
||||
{
|
||||
var modulesArray = modules.ToArray();
|
||||
var entryPointsArray = entryPoints.ToArray();
|
||||
if (entryPointsArray.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("At least one entry point is required.", nameof(entryPoints));
|
||||
}
|
||||
|
||||
var nativeModules = new RegorusPolicyModule[modulesArray.Length];
|
||||
var pinnedStrings = new List<Utf8Marshaller.PinnedUtf8>(modulesArray.Length * 2 + entryPointsArray.Length);
|
||||
var entryPointers = new IntPtr[entryPointsArray.Length];
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < modulesArray.Length; i++)
|
||||
{
|
||||
var idPinned = Utf8Marshaller.Pin(modulesArray[i].Id);
|
||||
var contentPinned = Utf8Marshaller.Pin(modulesArray[i].Content);
|
||||
pinnedStrings.Add(idPinned);
|
||||
pinnedStrings.Add(contentPinned);
|
||||
|
||||
nativeModules[i] = new RegorusPolicyModule
|
||||
{
|
||||
id = idPinned.Pointer,
|
||||
content = contentPinned.Pointer
|
||||
};
|
||||
}
|
||||
|
||||
for (int i = 0; i < entryPointsArray.Length; i++)
|
||||
{
|
||||
var entryPinned = Utf8Marshaller.Pin(entryPointsArray[i]);
|
||||
pinnedStrings.Add(entryPinned);
|
||||
entryPointers[i] = (IntPtr)entryPinned.Pointer;
|
||||
}
|
||||
|
||||
return Utf8Marshaller.WithUtf8(dataJson, dataPtr =>
|
||||
{
|
||||
fixed (RegorusPolicyModule* modulesPtr = nativeModules)
|
||||
fixed (IntPtr* entryPtr = entryPointers)
|
||||
{
|
||||
var result = API.regorus_program_compile_from_modules(
|
||||
(byte*)dataPtr,
|
||||
modulesPtr,
|
||||
(UIntPtr)modulesArray.Length,
|
||||
(byte**)entryPtr,
|
||||
(UIntPtr)entryPointsArray.Length);
|
||||
|
||||
return GetProgramResult(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach (var pinned in pinnedStrings)
|
||||
{
|
||||
pinned.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compile an RVM program from an engine instance and entry points.
|
||||
/// </summary>
|
||||
public static Program CompileFromEngine(Engine engine, IEnumerable<string> entryPoints)
|
||||
{
|
||||
if (engine is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(engine));
|
||||
}
|
||||
|
||||
var entryPointsArray = entryPoints.ToArray();
|
||||
if (entryPointsArray.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("At least one entry point is required.", nameof(entryPoints));
|
||||
}
|
||||
|
||||
var pinnedStrings = new List<Utf8Marshaller.PinnedUtf8>(entryPointsArray.Length);
|
||||
var entryPointers = new IntPtr[entryPointsArray.Length];
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < entryPointsArray.Length; i++)
|
||||
{
|
||||
var entryPinned = Utf8Marshaller.Pin(entryPointsArray[i]);
|
||||
pinnedStrings.Add(entryPinned);
|
||||
entryPointers[i] = (IntPtr)entryPinned.Pointer;
|
||||
}
|
||||
|
||||
return engine.UseHandleForInterop(enginePtr =>
|
||||
{
|
||||
fixed (IntPtr* entryPtr = entryPointers)
|
||||
{
|
||||
var result = API.regorus_engine_compile_program_with_entrypoints(
|
||||
(RegorusEngine*)enginePtr,
|
||||
(byte**)entryPtr,
|
||||
(UIntPtr)entryPointsArray.Length);
|
||||
|
||||
return GetProgramResult(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach (var pinned in pinnedStrings)
|
||||
{
|
||||
pinned.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize an RVM program from binary format.
|
||||
/// </summary>
|
||||
public static Program DeserializeBinary(byte[] data, out bool isPartial)
|
||||
{
|
||||
if (data is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
|
||||
byte partialFlag = 0;
|
||||
fixed (byte* dataPtr = data)
|
||||
{
|
||||
var result = API.regorus_program_deserialize_binary(dataPtr, (UIntPtr)data.Length, &partialFlag);
|
||||
var program = GetProgramResult(result);
|
||||
isPartial = partialFlag != 0;
|
||||
return program;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize the program to binary format.
|
||||
/// </summary>
|
||||
public byte[] SerializeBinary()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(programPtr =>
|
||||
{
|
||||
var result = API.regorus_program_serialize_binary((RegorusProgram*)programPtr);
|
||||
return ExtractBuffer(result);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate a readable assembly listing.
|
||||
/// </summary>
|
||||
public string? GenerateListing()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(programPtr =>
|
||||
{
|
||||
return CheckAndDropResult(API.regorus_program_generate_listing((RegorusProgram*)programPtr));
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (System.Threading.Interlocked.CompareExchange(ref _isDisposed, 1, 0) == 0)
|
||||
{
|
||||
_handle?.Dispose();
|
||||
_handle = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
if (_isDisposed != 0 || _handle is null || _handle.IsClosed)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Program));
|
||||
}
|
||||
}
|
||||
|
||||
internal RegorusProgramHandle GetHandleForUse()
|
||||
{
|
||||
var handle = _handle;
|
||||
if (handle is null || handle.IsClosed || handle.IsInvalid)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Program));
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
internal T UseHandle<T>(Func<IntPtr, T> func)
|
||||
{
|
||||
var handle = GetHandleForUse();
|
||||
bool addedRef = false;
|
||||
try
|
||||
{
|
||||
handle.DangerousAddRef(ref addedRef);
|
||||
var pointer = handle.DangerousGetHandle();
|
||||
if (pointer == IntPtr.Zero)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Program));
|
||||
}
|
||||
|
||||
return func(pointer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (addedRef)
|
||||
{
|
||||
handle.DangerousRelease();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Program GetProgramResult(RegorusResult result)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != RegorusStatus.Ok)
|
||||
{
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
if (result.data_type != RegorusDataType.Pointer || result.pointer_value == null)
|
||||
{
|
||||
throw new Exception("Expected program pointer but got different data type");
|
||||
}
|
||||
|
||||
var handle = RegorusProgramHandle.FromPointer((IntPtr)result.pointer_value);
|
||||
return new Program(handle);
|
||||
}
|
||||
finally
|
||||
{
|
||||
API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
|
||||
private static string? CheckAndDropResult(RegorusResult result)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != RegorusStatus.Ok)
|
||||
{
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
return result.data_type switch
|
||||
{
|
||||
RegorusDataType.String => Utf8Marshaller.FromUtf8(result.output),
|
||||
RegorusDataType.Boolean => result.bool_value.ToString().ToLowerInvariant(),
|
||||
RegorusDataType.Integer => result.int_value.ToString(),
|
||||
RegorusDataType.None => null,
|
||||
_ => Utf8Marshaller.FromUtf8(result.output)
|
||||
};
|
||||
}
|
||||
finally
|
||||
{
|
||||
API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
|
||||
private static byte[] ExtractBuffer(RegorusResult result)
|
||||
{
|
||||
RegorusBuffer* buffer = null;
|
||||
try
|
||||
{
|
||||
if (result.status != RegorusStatus.Ok)
|
||||
{
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
if (result.data_type != RegorusDataType.Pointer || result.pointer_value == null)
|
||||
{
|
||||
throw new Exception("Expected buffer pointer but got different data type");
|
||||
}
|
||||
|
||||
buffer = (RegorusBuffer*)result.pointer_value;
|
||||
var length = checked((int)buffer->len);
|
||||
var data = new byte[length];
|
||||
if (length > 0)
|
||||
{
|
||||
Marshal.Copy((IntPtr)buffer->data, data, 0, length);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (buffer != null)
|
||||
{
|
||||
API.regorus_buffer_drop(buffer);
|
||||
}
|
||||
API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,22 +8,30 @@
|
||||
<LangVersion>10.0</LangVersion>
|
||||
|
||||
<!-- See https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-pack -->
|
||||
<VersionPrefix>0.7.0</VersionPrefix>
|
||||
<VersionPrefix>0.9.0</VersionPrefix>
|
||||
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<RegorusFFIArtifactsProfile Condition="'$(RegorusFFIArtifactsProfile)' == ''">release</RegorusFFIArtifactsProfile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Text.Json" Version="8.0.5" />
|
||||
<PackageReference Include="System.Text.Json" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(EnableRegorusTestHooks)' == 'true'">
|
||||
<DefineConstants>$(DefineConstants);REGORUS_FFI_TEST_HOOKS</DefineConstants>
|
||||
</PropertyGroup>
|
||||
|
||||
<!--
|
||||
$(RegorusFFIArtifactsDir) is the location where regorus shared libraries have been
|
||||
built for various platforms and copied to. RegorusFFIArtifactsDir is passed in
|
||||
by the publishing pipeline.
|
||||
|
||||
For each target triple, `Pack` expects the regorus ffi shared library
|
||||
to be found in $(RegorusFFIArtifactsDir)/<target-triple>/release.
|
||||
to be found in $(RegorusFFIArtifactsDir)/<target-triple>/$(RegorusFFIArtifactsProfile).
|
||||
|
||||
If $(IgnoreMissingArtifacts) is not set, ensure that the binaries for officially supported platforms exists.
|
||||
-->
|
||||
@@ -31,27 +39,27 @@
|
||||
<Error Text="RegorusFFIArtifactsDir must be supplied." Condition="$(RegorusFFIArtifactsDir) == ''" />
|
||||
|
||||
<!-- Ensure that the binaries for officially supported platforms exists. -->
|
||||
<Error Text="$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/release/regorus_ffi.dll missing."
|
||||
Condition="!Exists('$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/release/regorus_ffi.dll')" />
|
||||
<Error Text="$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/release/regorus_ffi.pdb missing."
|
||||
Condition="!Exists('$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/release/regorus_ffi.pdb')" />
|
||||
<Error Text="$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/$(RegorusFFIArtifactsProfile)/regorus_ffi.dll missing."
|
||||
Condition="!Exists('$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/$(RegorusFFIArtifactsProfile)/regorus_ffi.dll')" />
|
||||
<Error Text="$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/$(RegorusFFIArtifactsProfile)/regorus_ffi.pdb missing."
|
||||
Condition="!Exists('$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/$(RegorusFFIArtifactsProfile)/regorus_ffi.pdb')" />
|
||||
|
||||
<Error Text="$(RegorusFFIArtifactsDir)/x86_64-unknown-linux-gnu/release/libregorus_ffi.so missing."
|
||||
Condition="!Exists('$(RegorusFFIArtifactsDir)/x86_64-unknown-linux-gnu/release/libregorus_ffi.so')" />
|
||||
<Error Text="$(RegorusFFIArtifactsDir)/x86_64-unknown-linux-gnu/$(RegorusFFIArtifactsProfile)/libregorus_ffi.so missing."
|
||||
Condition="!Exists('$(RegorusFFIArtifactsDir)/x86_64-unknown-linux-gnu/$(RegorusFFIArtifactsProfile)/libregorus_ffi.so')" />
|
||||
</Target>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="docs/README.md" Pack="true" PackagePath="/" />
|
||||
|
||||
<!-- Copy each binary to expected location within the package -->
|
||||
<None Include="$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/release/*.dll" Pack="true" PackagePath="runtimes/win-x64/native/" />
|
||||
<None Include="$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/release/*.pdb" Pack="true" PackagePath="runtimes/win-x64/native/" />
|
||||
<None Include="$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/$(RegorusFFIArtifactsProfile)/*.dll" Pack="true" PackagePath="runtimes/win-x64/native/" />
|
||||
<None Include="$(RegorusFFIArtifactsDir)/x86_64-pc-windows-msvc/$(RegorusFFIArtifactsProfile)/*.pdb" Pack="true" PackagePath="runtimes/win-x64/native/" />
|
||||
|
||||
<None Include="$(RegorusFFIArtifactsDir)/aarch64-pc-windows-msvc/release/*.dll" Pack="true" PackagePath="runtimes/win-arm64/native/" />
|
||||
<None Include="$(RegorusFFIArtifactsDir)/aarch64-pc-windows-msvc/release/*.pdb" Pack="true" PackagePath="runtimes/win-arm64/native/" />
|
||||
<None Include="$(RegorusFFIArtifactsDir)/aarch64-pc-windows-msvc/$(RegorusFFIArtifactsProfile)/*.dll" Pack="true" PackagePath="runtimes/win-arm64/native/" />
|
||||
<None Include="$(RegorusFFIArtifactsDir)/aarch64-pc-windows-msvc/$(RegorusFFIArtifactsProfile)/*.pdb" Pack="true" PackagePath="runtimes/win-arm64/native/" />
|
||||
|
||||
<None Include="$(RegorusFFIArtifactsDir)/x86_64-unknown-linux-gnu/release/lib*.so" Pack="true" PackagePath="runtimes/linux-x64/native/" />
|
||||
<None Include="$(RegorusFFIArtifactsDir)/x86_64-unknown-linux-gnu/$(RegorusFFIArtifactsProfile)/lib*.so" Pack="true" PackagePath="runtimes/linux-x64/native/" />
|
||||
|
||||
<None Include="$(RegorusFFIArtifactsDir)/aarch64-apple-darwin/release/lib*.dylib" Pack="true" PackagePath="runtimes/osx-arm64/native/" />
|
||||
<None Include="$(RegorusFFIArtifactsDir)/aarch64-apple-darwin/$(RegorusFFIArtifactsProfile)/lib*.dylib" Pack="true" PackagePath="runtimes/osx-arm64/native/" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
292
bindings/csharp/Regorus/Rvm.cs
Normal file
292
bindings/csharp/Regorus/Rvm.cs
Normal file
@@ -0,0 +1,292 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using Regorus.Internal;
|
||||
|
||||
#nullable enable
|
||||
namespace Regorus
|
||||
{
|
||||
/// <summary>
|
||||
/// Wrapper for the Regorus RVM runtime.
|
||||
/// </summary>
|
||||
public unsafe sealed class Rvm : IDisposable
|
||||
{
|
||||
private RegorusRvmHandle? _handle;
|
||||
private int _isDisposed;
|
||||
|
||||
public Rvm()
|
||||
{
|
||||
_handle = RegorusRvmHandle.Create();
|
||||
}
|
||||
|
||||
private Rvm(RegorusRvmHandle handle)
|
||||
{
|
||||
_handle = handle ?? throw new ArgumentNullException(nameof(handle));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create an RVM instance backed by a compiled policy (for default rule evaluation).
|
||||
/// </summary>
|
||||
public static Rvm CreateWithPolicy(CompiledPolicy policy)
|
||||
{
|
||||
if (policy is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(policy));
|
||||
}
|
||||
|
||||
return policy.UseHandleForInterop(policyPtr =>
|
||||
{
|
||||
var result = API.regorus_rvm_new_with_policy((RegorusCompiledPolicy*)policyPtr);
|
||||
return GetRvmResult(result);
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load a program into the VM.
|
||||
/// </summary>
|
||||
public void LoadProgram(Program program)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (program is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(program));
|
||||
}
|
||||
|
||||
program.UseHandle(programPtr =>
|
||||
{
|
||||
UseHandle(vmPtr =>
|
||||
{
|
||||
CheckAndDropResult(API.regorus_rvm_load_program((RegorusRvm*)vmPtr, (RegorusProgram*)programPtr));
|
||||
return 0;
|
||||
});
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the data document for the VM.
|
||||
/// </summary>
|
||||
public void SetDataJson(string dataJson)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
Utf8Marshaller.WithUtf8(dataJson, dataPtr =>
|
||||
{
|
||||
UseHandle(vmPtr =>
|
||||
{
|
||||
CheckAndDropResult(API.regorus_rvm_set_data((RegorusRvm*)vmPtr, (byte*)dataPtr));
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the input document for the VM.
|
||||
/// </summary>
|
||||
public void SetInputJson(string inputJson)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
Utf8Marshaller.WithUtf8(inputJson, inputPtr =>
|
||||
{
|
||||
UseHandle(vmPtr =>
|
||||
{
|
||||
CheckAndDropResult(API.regorus_rvm_set_input((RegorusRvm*)vmPtr, (byte*)inputPtr));
|
||||
return 0;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set the execution mode (0 = run-to-completion, 1 = suspendable).
|
||||
/// </summary>
|
||||
public void SetExecutionMode(byte mode)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
UseHandle(vmPtr =>
|
||||
{
|
||||
CheckAndDropResult(API.regorus_rvm_set_execution_mode((RegorusRvm*)vmPtr, mode));
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute the program and return the JSON result.
|
||||
/// </summary>
|
||||
public string? Execute()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(vmPtr =>
|
||||
{
|
||||
return CheckAndDropResult(API.regorus_rvm_execute((RegorusRvm*)vmPtr));
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute a named entry point.
|
||||
/// </summary>
|
||||
public string? ExecuteEntryPoint(string entryPoint)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return Utf8Marshaller.WithUtf8(entryPoint, entryPtr =>
|
||||
{
|
||||
return UseHandle(vmPtr =>
|
||||
{
|
||||
return CheckAndDropResult(API.regorus_rvm_execute_entry_point_by_name((RegorusRvm*)vmPtr, (byte*)entryPtr));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Execute an entry point by index.
|
||||
/// </summary>
|
||||
public string? ExecuteEntryPoint(ulong index)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(vmPtr =>
|
||||
{
|
||||
return CheckAndDropResult(API.regorus_rvm_execute_entry_point_by_index((RegorusRvm*)vmPtr, (UIntPtr)index));
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resume execution with an optional value.
|
||||
/// </summary>
|
||||
public string? Resume(string? resumeValueJson)
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
if (resumeValueJson is null)
|
||||
{
|
||||
return UseHandle(vmPtr =>
|
||||
{
|
||||
return CheckAndDropResult(API.regorus_rvm_resume((RegorusRvm*)vmPtr, null, has_value: false));
|
||||
});
|
||||
}
|
||||
|
||||
return Utf8Marshaller.WithUtf8(resumeValueJson, valuePtr =>
|
||||
{
|
||||
return UseHandle(vmPtr =>
|
||||
{
|
||||
return CheckAndDropResult(API.regorus_rvm_resume((RegorusRvm*)vmPtr, (byte*)valuePtr, has_value: true));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current execution state.
|
||||
/// </summary>
|
||||
public string? GetExecutionState()
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return UseHandle(vmPtr =>
|
||||
{
|
||||
return CheckAndDropResult(API.regorus_rvm_get_execution_state((RegorusRvm*)vmPtr));
|
||||
});
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing: true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
if (System.Threading.Interlocked.CompareExchange(ref _isDisposed, 1, 0) == 0)
|
||||
{
|
||||
_handle?.Dispose();
|
||||
_handle = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
if (_isDisposed != 0 || _handle is null || _handle.IsClosed)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Rvm));
|
||||
}
|
||||
}
|
||||
|
||||
internal RegorusRvmHandle GetHandleForUse()
|
||||
{
|
||||
var handle = _handle;
|
||||
if (handle is null || handle.IsClosed || handle.IsInvalid)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Rvm));
|
||||
}
|
||||
return handle;
|
||||
}
|
||||
|
||||
internal T UseHandle<T>(Func<IntPtr, T> func)
|
||||
{
|
||||
var handle = GetHandleForUse();
|
||||
bool addedRef = false;
|
||||
try
|
||||
{
|
||||
handle.DangerousAddRef(ref addedRef);
|
||||
var pointer = handle.DangerousGetHandle();
|
||||
if (pointer == IntPtr.Zero)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(Rvm));
|
||||
}
|
||||
|
||||
return func(pointer);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (addedRef)
|
||||
{
|
||||
handle.DangerousRelease();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Rvm GetRvmResult(RegorusResult result)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != RegorusStatus.Ok)
|
||||
{
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
if (result.data_type != RegorusDataType.Pointer || result.pointer_value == null)
|
||||
{
|
||||
throw new Exception("Expected RVM pointer but got different data type");
|
||||
}
|
||||
|
||||
var handle = RegorusRvmHandle.FromPointer((IntPtr)result.pointer_value);
|
||||
return new Rvm(handle);
|
||||
}
|
||||
finally
|
||||
{
|
||||
API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
|
||||
private static string? CheckAndDropResult(RegorusResult result)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != RegorusStatus.Ok)
|
||||
{
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
return result.data_type switch
|
||||
{
|
||||
RegorusDataType.String => Utf8Marshaller.FromUtf8(result.output),
|
||||
RegorusDataType.Boolean => result.bool_value.ToString().ToLowerInvariant(),
|
||||
RegorusDataType.Integer => result.int_value.ToString(),
|
||||
RegorusDataType.None => null,
|
||||
_ => Utf8Marshaller.FromUtf8(result.output)
|
||||
};
|
||||
}
|
||||
finally
|
||||
{
|
||||
API.regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
186
bindings/csharp/Regorus/SafeHandles.cs
Normal file
186
bindings/csharp/Regorus/SafeHandles.cs
Normal file
@@ -0,0 +1,186 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Win32.SafeHandles;
|
||||
|
||||
#nullable enable
|
||||
namespace Regorus
|
||||
{
|
||||
internal sealed class RegorusEngineHandle : SafeHandleZeroOrMinusOneIsInvalid
|
||||
{
|
||||
private RegorusEngineHandle() : base(ownsHandle: true)
|
||||
{
|
||||
}
|
||||
|
||||
internal static RegorusEngineHandle Create()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var raw = Internal.API.regorus_engine_new();
|
||||
if (raw is null)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to create Regorus engine.");
|
||||
}
|
||||
|
||||
var handle = new RegorusEngineHandle();
|
||||
handle.SetHandle((IntPtr)raw);
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
|
||||
internal static RegorusEngineHandle FromPointer(IntPtr pointer)
|
||||
{
|
||||
if (pointer == IntPtr.Zero)
|
||||
{
|
||||
throw new ArgumentException("Pointer cannot be zero.", nameof(pointer));
|
||||
}
|
||||
|
||||
var handle = new RegorusEngineHandle();
|
||||
handle.SetHandle(pointer);
|
||||
return handle;
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
if (!IsInvalid && !IsClosed)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
Internal.API.regorus_engine_drop((Internal.RegorusEngine*)handle);
|
||||
}
|
||||
SetHandle(IntPtr.Zero);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class RegorusCompiledPolicyHandle : SafeHandleZeroOrMinusOneIsInvalid
|
||||
{
|
||||
private RegorusCompiledPolicyHandle() : base(ownsHandle: true)
|
||||
{
|
||||
}
|
||||
|
||||
internal static RegorusCompiledPolicyHandle FromPointer(IntPtr pointer)
|
||||
{
|
||||
if (pointer == IntPtr.Zero)
|
||||
{
|
||||
throw new ArgumentException("Pointer cannot be zero.", nameof(pointer));
|
||||
}
|
||||
|
||||
var handle = new RegorusCompiledPolicyHandle();
|
||||
handle.SetHandle(pointer);
|
||||
return handle;
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
if (!IsInvalid && !IsClosed)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
Internal.API.regorus_compiled_policy_drop((Internal.RegorusCompiledPolicy*)handle);
|
||||
}
|
||||
SetHandle(IntPtr.Zero);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class RegorusProgramHandle : SafeHandleZeroOrMinusOneIsInvalid
|
||||
{
|
||||
private RegorusProgramHandle() : base(ownsHandle: true)
|
||||
{
|
||||
}
|
||||
|
||||
internal static RegorusProgramHandle Create()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var raw = Internal.API.regorus_program_new();
|
||||
if (raw is null)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to create Regorus program.");
|
||||
}
|
||||
|
||||
var handle = new RegorusProgramHandle();
|
||||
handle.SetHandle((IntPtr)raw);
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
|
||||
internal static RegorusProgramHandle FromPointer(IntPtr pointer)
|
||||
{
|
||||
if (pointer == IntPtr.Zero)
|
||||
{
|
||||
throw new ArgumentException("Pointer cannot be zero.", nameof(pointer));
|
||||
}
|
||||
|
||||
var handle = new RegorusProgramHandle();
|
||||
handle.SetHandle(pointer);
|
||||
return handle;
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
if (!IsInvalid && !IsClosed)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
Internal.API.regorus_program_drop((Internal.RegorusProgram*)handle);
|
||||
}
|
||||
SetHandle(IntPtr.Zero);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class RegorusRvmHandle : SafeHandleZeroOrMinusOneIsInvalid
|
||||
{
|
||||
private RegorusRvmHandle() : base(ownsHandle: true)
|
||||
{
|
||||
}
|
||||
|
||||
internal static RegorusRvmHandle Create()
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
var raw = Internal.API.regorus_rvm_new();
|
||||
if (raw is null)
|
||||
{
|
||||
throw new InvalidOperationException("Failed to create Regorus RVM.");
|
||||
}
|
||||
|
||||
var handle = new RegorusRvmHandle();
|
||||
handle.SetHandle((IntPtr)raw);
|
||||
return handle;
|
||||
}
|
||||
}
|
||||
|
||||
internal static RegorusRvmHandle FromPointer(IntPtr pointer)
|
||||
{
|
||||
if (pointer == IntPtr.Zero)
|
||||
{
|
||||
throw new ArgumentException("Pointer cannot be zero.", nameof(pointer));
|
||||
}
|
||||
|
||||
var handle = new RegorusRvmHandle();
|
||||
handle.SetHandle(pointer);
|
||||
return handle;
|
||||
}
|
||||
|
||||
protected override bool ReleaseHandle()
|
||||
{
|
||||
if (!IsInvalid && !IsClosed)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
Internal.API.regorus_rvm_drop((Internal.RegorusRvm*)handle);
|
||||
}
|
||||
SetHandle(IntPtr.Zero);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using Regorus.Internal;
|
||||
|
||||
#nullable enable
|
||||
namespace Regorus
|
||||
@@ -21,14 +21,16 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when schema registration fails</exception>
|
||||
public static void RegisterResource(string name, string schemaJson)
|
||||
{
|
||||
var nameBytes = Encoding.UTF8.GetBytes(name + char.MinValue);
|
||||
var schemaBytes = Encoding.UTF8.GetBytes(schemaJson + char.MinValue);
|
||||
|
||||
fixed (byte* namePtr = nameBytes)
|
||||
fixed (byte* schemaPtr = schemaBytes)
|
||||
Utf8Marshaller.WithUtf8(name, namePtr =>
|
||||
{
|
||||
CheckAndDropResult(Internal.API.regorus_resource_schema_register(namePtr, schemaPtr));
|
||||
}
|
||||
Utf8Marshaller.WithUtf8(schemaJson, schemaPtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Internal.API.regorus_resource_schema_register((byte*)namePtr, (byte*)schemaPtr));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -39,12 +41,14 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when the operation fails</exception>
|
||||
public static bool ContainsResource(string name)
|
||||
{
|
||||
var nameBytes = Encoding.UTF8.GetBytes(name + char.MinValue);
|
||||
fixed (byte* namePtr = nameBytes)
|
||||
return Utf8Marshaller.WithUtf8(name, namePtr =>
|
||||
{
|
||||
var result = Internal.API.regorus_resource_schema_contains(namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
var result = Internal.API.regorus_resource_schema_contains((byte*)namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -93,12 +97,14 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when the operation fails</exception>
|
||||
public static bool RemoveResource(string name)
|
||||
{
|
||||
var nameBytes = Encoding.UTF8.GetBytes(name + char.MinValue);
|
||||
fixed (byte* namePtr = nameBytes)
|
||||
return Utf8Marshaller.WithUtf8(name, namePtr =>
|
||||
{
|
||||
var result = Internal.API.regorus_resource_schema_remove(namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
var result = Internal.API.regorus_resource_schema_remove((byte*)namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -118,14 +124,16 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when schema registration fails</exception>
|
||||
public static void RegisterEffect(string name, string schemaJson)
|
||||
{
|
||||
var nameBytes = Encoding.UTF8.GetBytes(name + char.MinValue);
|
||||
var schemaBytes = Encoding.UTF8.GetBytes(schemaJson + char.MinValue);
|
||||
|
||||
fixed (byte* namePtr = nameBytes)
|
||||
fixed (byte* schemaPtr = schemaBytes)
|
||||
Utf8Marshaller.WithUtf8(name, namePtr =>
|
||||
{
|
||||
CheckAndDropResult(Internal.API.regorus_effect_schema_register(namePtr, schemaPtr));
|
||||
}
|
||||
Utf8Marshaller.WithUtf8(schemaJson, schemaPtr =>
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Internal.API.regorus_effect_schema_register((byte*)namePtr, (byte*)schemaPtr));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -136,12 +144,14 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when the operation fails</exception>
|
||||
public static bool ContainsEffect(string name)
|
||||
{
|
||||
var nameBytes = Encoding.UTF8.GetBytes(name + char.MinValue);
|
||||
fixed (byte* namePtr = nameBytes)
|
||||
return Utf8Marshaller.WithUtf8(name, namePtr =>
|
||||
{
|
||||
var result = Internal.API.regorus_effect_schema_contains(namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
var result = Internal.API.regorus_effect_schema_contains((byte*)namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -190,12 +200,14 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when the operation fails</exception>
|
||||
public static bool RemoveEffect(string name)
|
||||
{
|
||||
var nameBytes = Encoding.UTF8.GetBytes(name + char.MinValue);
|
||||
fixed (byte* namePtr = nameBytes)
|
||||
return Utf8Marshaller.WithUtf8(name, namePtr =>
|
||||
{
|
||||
var result = Internal.API.regorus_effect_schema_remove(namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
var result = Internal.API.regorus_effect_schema_remove((byte*)namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -207,36 +219,23 @@ namespace Regorus
|
||||
CheckAndDropResult(Internal.API.regorus_effect_schema_clear());
|
||||
}
|
||||
|
||||
private static string? StringFromUTF8(IntPtr ptr)
|
||||
{
|
||||
#if NETSTANDARD2_1
|
||||
return System.Runtime.InteropServices.Marshal.PtrToStringUTF8(ptr);
|
||||
#else
|
||||
int len = 0;
|
||||
while (System.Runtime.InteropServices.Marshal.ReadByte(ptr, len) != 0) { ++len; }
|
||||
byte[] buffer = new byte[len];
|
||||
System.Runtime.InteropServices.Marshal.Copy(ptr, buffer, 0, buffer.Length);
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
private static string? CheckAndDropResult(Internal.RegorusResult result)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != Internal.RegorusStatus.Ok)
|
||||
{
|
||||
var message = StringFromUTF8((IntPtr)result.error_message);
|
||||
throw new Exception(message ?? "Unknown error occurred");
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
return result.data_type switch
|
||||
{
|
||||
Internal.RegorusDataType.String => StringFromUTF8((IntPtr)result.output),
|
||||
Internal.RegorusDataType.String => Utf8Marshaller.FromUtf8(result.output),
|
||||
Internal.RegorusDataType.Boolean => result.bool_value.ToString().ToLowerInvariant(),
|
||||
Internal.RegorusDataType.Integer => result.int_value.ToString(),
|
||||
Internal.RegorusDataType.None => null,
|
||||
_ => StringFromUTF8((IntPtr)result.output)
|
||||
_ => Utf8Marshaller.FromUtf8(result.output)
|
||||
};
|
||||
}
|
||||
finally
|
||||
@@ -251,8 +250,8 @@ namespace Regorus
|
||||
{
|
||||
if (result.status != Internal.RegorusStatus.Ok)
|
||||
{
|
||||
var message = StringFromUTF8((IntPtr)result.error_message);
|
||||
throw new Exception(message ?? "Unknown error occurred");
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
return result.data_type == Internal.RegorusDataType.Boolean ? result.bool_value : false;
|
||||
@@ -269,8 +268,8 @@ namespace Regorus
|
||||
{
|
||||
if (result.status != Internal.RegorusStatus.Ok)
|
||||
{
|
||||
var message = StringFromUTF8((IntPtr)result.error_message);
|
||||
throw new Exception(message ?? "Unknown error occurred");
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
return result.data_type == Internal.RegorusDataType.Integer ? result.int_value : 0;
|
||||
|
||||
24
bindings/csharp/Regorus/StatusExtensions.cs
Normal file
24
bindings/csharp/Regorus/StatusExtensions.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
|
||||
#nullable enable
|
||||
|
||||
namespace Regorus.Internal
|
||||
{
|
||||
internal static class StatusExtensions
|
||||
{
|
||||
internal static Exception CreateException(this RegorusStatus status, string? message)
|
||||
{
|
||||
var details = string.IsNullOrWhiteSpace(message) ? "Regorus call failed." : message;
|
||||
|
||||
return status switch
|
||||
{
|
||||
RegorusStatus.Panic => new InvalidOperationException($"Regorus engine panicked: {details}"),
|
||||
RegorusStatus.Poisoned => new InvalidOperationException($"Regorus engine is poisoned: {details}"),
|
||||
_ => new InvalidOperationException(details),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Text;
|
||||
using Regorus.Internal;
|
||||
|
||||
#nullable enable
|
||||
namespace Regorus
|
||||
@@ -22,11 +22,13 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when target registration fails</exception>
|
||||
public static void RegisterFromJson(string targetJson)
|
||||
{
|
||||
var targetBytes = Encoding.UTF8.GetBytes(targetJson + char.MinValue);
|
||||
fixed (byte* targetPtr = targetBytes)
|
||||
Utf8Marshaller.WithUtf8(targetJson, targetPtr =>
|
||||
{
|
||||
CheckAndDropResult(Internal.API.regorus_register_target_from_json(targetPtr));
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
CheckAndDropResult(Internal.API.regorus_register_target_from_json((byte*)targetPtr));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -37,12 +39,14 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when the operation fails</exception>
|
||||
public static bool Contains(string name)
|
||||
{
|
||||
var nameBytes = Encoding.UTF8.GetBytes(name + char.MinValue);
|
||||
fixed (byte* namePtr = nameBytes)
|
||||
return Utf8Marshaller.WithUtf8(name, namePtr =>
|
||||
{
|
||||
var result = Internal.API.regorus_target_registry_contains(namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
var result = Internal.API.regorus_target_registry_contains((byte*)namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -63,12 +67,14 @@ namespace Regorus
|
||||
/// <exception cref="Exception">Thrown when the operation fails</exception>
|
||||
public static bool Remove(string name)
|
||||
{
|
||||
var nameBytes = Encoding.UTF8.GetBytes(name + char.MinValue);
|
||||
fixed (byte* namePtr = nameBytes)
|
||||
return Utf8Marshaller.WithUtf8(name, namePtr =>
|
||||
{
|
||||
var result = Internal.API.regorus_target_registry_remove(namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
unsafe
|
||||
{
|
||||
var result = Internal.API.regorus_target_registry_remove((byte*)namePtr);
|
||||
return GetBoolResult(result);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -108,36 +114,23 @@ namespace Regorus
|
||||
}
|
||||
}
|
||||
|
||||
private static string? StringFromUTF8(IntPtr ptr)
|
||||
{
|
||||
#if NETSTANDARD2_1
|
||||
return System.Runtime.InteropServices.Marshal.PtrToStringUTF8(ptr);
|
||||
#else
|
||||
int len = 0;
|
||||
while (System.Runtime.InteropServices.Marshal.ReadByte(ptr, len) != 0) { ++len; }
|
||||
byte[] buffer = new byte[len];
|
||||
System.Runtime.InteropServices.Marshal.Copy(ptr, buffer, 0, buffer.Length);
|
||||
return Encoding.UTF8.GetString(buffer);
|
||||
#endif
|
||||
}
|
||||
|
||||
private static string? CheckAndDropResult(Internal.RegorusResult result)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (result.status != Internal.RegorusStatus.Ok)
|
||||
{
|
||||
var message = StringFromUTF8((IntPtr)result.error_message);
|
||||
throw new Exception(message ?? "Unknown error occurred");
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
return result.data_type switch
|
||||
{
|
||||
Internal.RegorusDataType.String => StringFromUTF8((IntPtr)result.output),
|
||||
Internal.RegorusDataType.String => Utf8Marshaller.FromUtf8(result.output),
|
||||
Internal.RegorusDataType.Boolean => result.bool_value.ToString().ToLowerInvariant(),
|
||||
Internal.RegorusDataType.Integer => result.int_value.ToString(),
|
||||
Internal.RegorusDataType.None => null,
|
||||
_ => StringFromUTF8((IntPtr)result.output)
|
||||
_ => Utf8Marshaller.FromUtf8(result.output)
|
||||
};
|
||||
}
|
||||
finally
|
||||
@@ -152,8 +145,8 @@ namespace Regorus
|
||||
{
|
||||
if (result.status != Internal.RegorusStatus.Ok)
|
||||
{
|
||||
var message = StringFromUTF8((IntPtr)result.error_message);
|
||||
throw new Exception(message ?? "Unknown error occurred");
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
return result.data_type == Internal.RegorusDataType.Boolean ? result.bool_value : false;
|
||||
@@ -170,8 +163,8 @@ namespace Regorus
|
||||
{
|
||||
if (result.status != Internal.RegorusStatus.Ok)
|
||||
{
|
||||
var message = StringFromUTF8((IntPtr)result.error_message);
|
||||
throw new Exception(message ?? "Unknown error occurred");
|
||||
var message = Utf8Marshaller.FromUtf8(result.error_message);
|
||||
throw result.status.CreateException(message);
|
||||
}
|
||||
|
||||
return result.data_type == Internal.RegorusDataType.Integer ? result.int_value : 0;
|
||||
|
||||
197
bindings/csharp/Regorus/Utf8Marshaller.cs
Normal file
197
bindings/csharp/Regorus/Utf8Marshaller.cs
Normal file
@@ -0,0 +1,197 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
#nullable enable
|
||||
namespace Regorus.Internal
|
||||
{
|
||||
/// <summary>
|
||||
/// Helpers for marshaling managed strings to null-terminated UTF-8 buffers.
|
||||
/// Provides stack-based storage for short lived conversions and pooled backing
|
||||
/// for longer lived pinned buffers.
|
||||
/// </summary>
|
||||
internal static class Utf8Marshaller
|
||||
{
|
||||
// Mirrors BCL patterns (e.g., System.Text.Json encoding helpers) by stackalloc'ing
|
||||
// up to 512 bytes to cover common short strings while keeping the stack usage well
|
||||
// below typical per-frame limits; larger payloads fall back to pooled buffers.
|
||||
private const int StackAllocThreshold = 512;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a pooled and pinned UTF-8 buffer suitable for scenarios where
|
||||
/// the pointer must remain stable beyond the immediate call site (for example,
|
||||
/// when referenced by another buffer passed to native code).
|
||||
/// </summary>
|
||||
internal sealed class PinnedUtf8 : IDisposable
|
||||
{
|
||||
private GCHandle _handle;
|
||||
private byte[]? _buffer;
|
||||
private bool _disposed;
|
||||
|
||||
internal unsafe PinnedUtf8(string value)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
var byteCount = Encoding.UTF8.GetByteCount(value);
|
||||
_buffer = ArrayPool<byte>.Shared.Rent(byteCount + 1);
|
||||
|
||||
try
|
||||
{
|
||||
var written = Encoding.UTF8.GetBytes(value, 0, value.Length, _buffer, 0);
|
||||
_buffer[written] = 0;
|
||||
|
||||
_handle = GCHandle.Alloc(_buffer, GCHandleType.Pinned);
|
||||
Pointer = (byte*)_handle.AddrOfPinnedObject();
|
||||
Length = written + 1;
|
||||
}
|
||||
catch
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(_buffer);
|
||||
_buffer = null;
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe byte* Pointer { get; }
|
||||
|
||||
internal int Length { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (_handle.IsAllocated)
|
||||
{
|
||||
_handle.Free();
|
||||
}
|
||||
|
||||
if (_buffer != null)
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(_buffer);
|
||||
_buffer = null;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
internal unsafe delegate void Utf8PointerAction(byte* pointer);
|
||||
|
||||
internal static unsafe void WithUtf8(string value, Utf8PointerAction action)
|
||||
{
|
||||
if (action is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(action));
|
||||
}
|
||||
|
||||
WithUtf8<object?>(value, ptr =>
|
||||
{
|
||||
action((byte*)ptr);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
internal static T WithUtf8<T>(string value, Func<IntPtr, T> func)
|
||||
{
|
||||
if (value is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
if (func is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(func));
|
||||
}
|
||||
|
||||
var byteCount = Encoding.UTF8.GetByteCount(value);
|
||||
var required = byteCount + 1;
|
||||
|
||||
if (required <= StackAllocThreshold)
|
||||
{
|
||||
Span<byte> buffer = stackalloc byte[required];
|
||||
return Invoke(value, func, buffer, byteCount);
|
||||
}
|
||||
|
||||
var rented = ArrayPool<byte>.Shared.Rent(required);
|
||||
try
|
||||
{
|
||||
Span<byte> buffer = rented;
|
||||
return Invoke(value, func, buffer, byteCount);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(rented);
|
||||
}
|
||||
}
|
||||
|
||||
private static unsafe T Invoke<T>(string value, Func<IntPtr, T> func, Span<byte> buffer, int byteCount)
|
||||
{
|
||||
fixed (char* charPtr = value)
|
||||
fixed (byte* bytePtr = buffer)
|
||||
{
|
||||
var written = Encoding.UTF8.GetBytes(charPtr, value.Length, bytePtr, byteCount);
|
||||
bytePtr[written] = 0;
|
||||
return func((IntPtr)bytePtr);
|
||||
}
|
||||
}
|
||||
|
||||
internal static PinnedUtf8 Pin(string value)
|
||||
{
|
||||
return new PinnedUtf8(value);
|
||||
}
|
||||
|
||||
internal static unsafe string? FromUtf8(byte* pointer)
|
||||
{
|
||||
if (pointer is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
#if NETSTANDARD2_1
|
||||
return Marshal.PtrToStringUTF8((IntPtr)pointer);
|
||||
#else
|
||||
var intPtr = (IntPtr)pointer;
|
||||
var length = 0;
|
||||
while (Marshal.ReadByte(intPtr, length) != 0)
|
||||
{
|
||||
length++;
|
||||
}
|
||||
|
||||
if (length == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var buffer = ArrayPool<byte>.Shared.Rent(length);
|
||||
try
|
||||
{
|
||||
Marshal.Copy(intPtr, buffer, 0, length);
|
||||
return Encoding.UTF8.GetString(buffer, 0, length);
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(buffer);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
internal static string? FromUtf8(IntPtr pointer)
|
||||
{
|
||||
unsafe
|
||||
{
|
||||
return FromUtf8((byte*)pointer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace TargetExampleApp;
|
||||
@@ -50,6 +51,69 @@ import rego.v1
|
||||
parameters.requiredTLSVersion = ""TLS1_2""
|
||||
parameters.allowedPorts = [""22"", ""3389""]";
|
||||
|
||||
private const string EXECUTION_TIMER_POLICY = @"
|
||||
package limits.timer
|
||||
import rego.v1
|
||||
|
||||
triplet_count := count([1 |
|
||||
x := data.values[_]
|
||||
y := data.values[_]
|
||||
z := data.values[_]
|
||||
])
|
||||
";
|
||||
|
||||
private const string EXECUTION_TIMER_QUERY = "data.limits.timer.triplet_count";
|
||||
private const int EXECUTION_TIMER_VALUE_COUNT = 40;
|
||||
|
||||
private const string RVM_POLICY = """
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.user == "alice"
|
||||
some role in data.roles[input.user]
|
||||
role == "admin"
|
||||
}
|
||||
""";
|
||||
|
||||
private const string RVM_DATA = """
|
||||
{
|
||||
"roles": {
|
||||
"alice": ["admin", "reader"]
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
private const string RVM_INPUT = """
|
||||
{
|
||||
"user": "alice"
|
||||
}
|
||||
""";
|
||||
|
||||
private const string HOST_AWAIT_POLICY = """
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.account.active == true
|
||||
details := __builtin_host_await(input.account.id, "account")
|
||||
details.tier == "gold"
|
||||
}
|
||||
""";
|
||||
|
||||
private const string HOST_AWAIT_INPUT = """
|
||||
{
|
||||
"account": {
|
||||
"id": "acct-1",
|
||||
"active": true
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
// Test data constants
|
||||
private const string COMPLIANT_STORAGE_ACCOUNT = @"{
|
||||
""type"": ""Microsoft.Storage/storageAccounts"",
|
||||
@@ -156,6 +220,18 @@ parameters.allowedPorts = [""22"", ""3389""]";
|
||||
// 4. Demonstrate thread-safe concurrent evaluation
|
||||
Console.WriteLine("\n4. Testing concurrent evaluation from multiple threads:");
|
||||
DemonstrateConcurrentEvaluation(compiledPolicy);
|
||||
|
||||
Console.WriteLine("\n5. Execution timer configuration:");
|
||||
DemonstrateExecutionTimer();
|
||||
|
||||
Console.WriteLine("\n6. RVM program execution:");
|
||||
DemonstrateRvmUsage();
|
||||
|
||||
Console.WriteLine("\n7. RVM program compilation from engine:");
|
||||
DemonstrateRvmCompileFromEngine();
|
||||
|
||||
Console.WriteLine("\n8. RVM host await (suspend/resume):");
|
||||
DemonstrateRvmHostAwait();
|
||||
}
|
||||
|
||||
static void DemonstrateConcurrentEvaluation(Regorus.CompiledPolicy compiledPolicy)
|
||||
@@ -288,4 +364,130 @@ parameters.allowedPorts = [""22"", ""3389""]";
|
||||
Console.WriteLine($"✗ Failed to get policy info: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
static void DemonstrateExecutionTimer()
|
||||
{
|
||||
var dataJson = JsonSerializer.Serialize(new
|
||||
{
|
||||
values = Enumerable.Range(0, EXECUTION_TIMER_VALUE_COUNT).ToArray()
|
||||
});
|
||||
|
||||
var fallback = new Regorus.ExecutionTimerConfig(TimeSpan.FromMilliseconds(2), checkInterval: 1);
|
||||
var relaxed = new Regorus.ExecutionTimerConfig(TimeSpan.FromMilliseconds(1000), checkInterval: 1);
|
||||
|
||||
Console.WriteLine($" Configuring fallback timer (limit={fallback.Limit.TotalMilliseconds:F0} ms, interval={fallback.CheckInterval})...");
|
||||
|
||||
Regorus.Engine.SetFallbackExecutionTimerConfig(fallback);
|
||||
try
|
||||
{
|
||||
using var engine = new Regorus.Engine();
|
||||
engine.AddPolicy("limits_timer.rego", EXECUTION_TIMER_POLICY);
|
||||
engine.AddDataJson(dataJson);
|
||||
|
||||
Console.WriteLine(" Evaluating under fallback limit (expected failure)...");
|
||||
try
|
||||
{
|
||||
engine.EvalRule(EXECUTION_TIMER_QUERY);
|
||||
Console.WriteLine(" ⚠ Evaluation unexpectedly succeeded under fallback limit.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($" ✓ Fallback enforced: {ex.Message}");
|
||||
}
|
||||
|
||||
Console.WriteLine($" Applying per-engine override ({relaxed.Limit.TotalMilliseconds:F0} ms) and retrying...");
|
||||
engine.SetExecutionTimerConfig(relaxed);
|
||||
var result = engine.EvalRule(EXECUTION_TIMER_QUERY);
|
||||
Console.WriteLine($" ✓ Override succeeded; triplet_count = {result}");
|
||||
|
||||
Console.WriteLine(" Clearing engine override to restore fallback...");
|
||||
engine.ClearExecutionTimerConfig();
|
||||
try
|
||||
{
|
||||
engine.EvalRule(EXECUTION_TIMER_QUERY);
|
||||
Console.WriteLine(" ⚠ Evaluation unexpectedly succeeded after clearing override.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($" ✓ Fallback restored: {ex.Message}");
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
Regorus.Engine.ClearFallbackExecutionTimerConfig();
|
||||
}
|
||||
}
|
||||
|
||||
static void DemonstrateRvmUsage()
|
||||
{
|
||||
var modules = new List<Regorus.PolicyModule>
|
||||
{
|
||||
new Regorus.PolicyModule("demo.rego", RVM_POLICY)
|
||||
};
|
||||
var entryPoints = new[] { "data.demo.allow" };
|
||||
|
||||
using var program = Regorus.Program.CompileFromModules(RVM_DATA, modules, entryPoints);
|
||||
var binary = program.SerializeBinary();
|
||||
using var rehydrated = Regorus.Program.DeserializeBinary(binary, out var isPartial);
|
||||
if (isPartial)
|
||||
{
|
||||
throw new InvalidOperationException("RVM program deserialization returned a partial program.");
|
||||
}
|
||||
|
||||
Console.WriteLine($"Serialized program size: {binary.Length} bytes");
|
||||
|
||||
var listing = rehydrated.GenerateListing();
|
||||
|
||||
Console.WriteLine("RVM listing:");
|
||||
Console.WriteLine(listing);
|
||||
|
||||
using var vm = new Regorus.Rvm();
|
||||
vm.LoadProgram(rehydrated);
|
||||
vm.SetDataJson(RVM_DATA);
|
||||
vm.SetInputJson(RVM_INPUT);
|
||||
|
||||
var result = vm.Execute();
|
||||
Console.WriteLine($"RVM result: {result}");
|
||||
}
|
||||
|
||||
static void DemonstrateRvmCompileFromEngine()
|
||||
{
|
||||
using var engine = new Regorus.Engine();
|
||||
engine.AddPolicy("demo.rego", RVM_POLICY);
|
||||
engine.AddDataJson(RVM_DATA);
|
||||
|
||||
var entryPoints = new[] { "data.demo.allow" };
|
||||
using var program = Regorus.Program.CompileFromEngine(engine, entryPoints);
|
||||
|
||||
using var vm = new Regorus.Rvm();
|
||||
vm.LoadProgram(program);
|
||||
vm.SetDataJson(RVM_DATA);
|
||||
vm.SetInputJson(RVM_INPUT);
|
||||
|
||||
var result = vm.ExecuteEntryPoint("data.demo.allow");
|
||||
Console.WriteLine($"RVM result from engine-compiled program: {result}");
|
||||
}
|
||||
|
||||
static void DemonstrateRvmHostAwait()
|
||||
{
|
||||
var modules = new List<Regorus.PolicyModule>
|
||||
{
|
||||
new Regorus.PolicyModule("host_await.rego", HOST_AWAIT_POLICY)
|
||||
};
|
||||
var entryPoints = new[] { "data.demo.allow" };
|
||||
|
||||
using var program = Regorus.Program.CompileFromModules("{}", modules, entryPoints);
|
||||
using var vm = new Regorus.Rvm();
|
||||
vm.SetExecutionMode(1);
|
||||
vm.LoadProgram(program);
|
||||
vm.SetInputJson(HOST_AWAIT_INPUT);
|
||||
|
||||
var initial = vm.Execute();
|
||||
var state = vm.GetExecutionState();
|
||||
Console.WriteLine($"HostAwait initial result: {initial}");
|
||||
Console.WriteLine($"Execution state: {state}");
|
||||
|
||||
var resumed = vm.Resume("{\"tier\":\"gold\"}");
|
||||
Console.WriteLine($"HostAwait resumed result: {resumed}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,10 +11,15 @@
|
||||
<PropertyGroup>
|
||||
<!-- If the environment variable is set (such as in a Github Action run), append the suffix to the version number -->
|
||||
<RegorusPackageVersionSuffix Condition="'$(VersionSuffix)' != ''">-$(VersionSuffix)</RegorusPackageVersionSuffix>
|
||||
<UseLocalRegorus Condition="'$(UseLocalRegorus)' == ''">false</UseLocalRegorus>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Regorus" Version="0.6.0$(RegorusPackageVersionSuffix)"/>
|
||||
<ItemGroup Condition="'$(UseLocalRegorus)' == 'true'">
|
||||
<ProjectReference Include="../Regorus/Regorus.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(UseLocalRegorus)' != 'true'">
|
||||
<PackageReference Include="Regorus" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -10,7 +10,17 @@
|
||||
<LangVersion>10.0</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="regorus" Version="0.5.0"/>
|
||||
<PropertyGroup>
|
||||
<!-- Allow CI to append the version suffix for locally built packages -->
|
||||
<RegorusPackageVersionSuffix Condition="'$(VersionSuffix)' != ''">-$(VersionSuffix)</RegorusPackageVersionSuffix>
|
||||
<UseLocalRegorus Condition="'$(UseLocalRegorus)' == ''">false</UseLocalRegorus>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup Condition="'$(UseLocalRegorus)' == 'true'">
|
||||
<ProjectReference Include="../Regorus/Regorus.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup Condition="'$(UseLocalRegorus)' != 'true'">
|
||||
<PackageReference Include="Regorus" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
726
bindings/ffi/Cargo.lock
generated
726
bindings/ffi/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,7 @@
|
||||
|
||||
[package]
|
||||
name = "regorus-ffi"
|
||||
version = "0.5.0"
|
||||
version = "0.9.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
@@ -13,6 +13,7 @@ crate-type = ["cdylib", "staticlib"]
|
||||
anyhow = "1.0"
|
||||
regorus = { path = "../..", default-features = false }
|
||||
serde_json = "1.0.140"
|
||||
parking_lot = { version = "0.12", optional = true }
|
||||
|
||||
[profile.release]
|
||||
# Enable full debug info for optimized builds.
|
||||
@@ -23,12 +24,25 @@ lto = true
|
||||
codegen-units = 1
|
||||
|
||||
[features]
|
||||
default = ["ast", "azure_policy", "std", "coverage", "regorus/arc", "regorus/full-opa"]
|
||||
default = [
|
||||
"ast",
|
||||
"azure_policy",
|
||||
"std",
|
||||
"coverage",
|
||||
"allocator-memory-limits",
|
||||
"rvm",
|
||||
"regorus/arc",
|
||||
"regorus/full-opa",
|
||||
"contention_checks",
|
||||
]
|
||||
ast = ["regorus/ast"]
|
||||
azure_policy = ["regorus/azure_policy"]
|
||||
std = ["regorus/std"]
|
||||
coverage = ["regorus/coverage"]
|
||||
allocator-memory-limits = ["regorus/allocator-memory-limits"]
|
||||
contention_checks = ["parking_lot"]
|
||||
rvm = ["regorus/rvm"]
|
||||
custom_allocator = []
|
||||
|
||||
[build-dependencies]
|
||||
cbindgen = "0.28.0"
|
||||
cbindgen = "0.29.2"
|
||||
|
||||
@@ -9,7 +9,7 @@ extern "C" {
|
||||
|
||||
#[cfg(feature = "custom_allocator")]
|
||||
mod allocator {
|
||||
use std::alloc::{GlobalAlloc, Layout};
|
||||
use core::alloc::{GlobalAlloc, Layout};
|
||||
|
||||
struct RegorusAllocator {}
|
||||
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use alloc::ffi::CString;
|
||||
use alloc::format;
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::vec::Vec;
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::os::raw::{c_char, c_longlong};
|
||||
use core::ffi::{c_char, c_longlong, c_void, CStr};
|
||||
use core::{mem, ptr};
|
||||
|
||||
/// Status of a call on `RegorusEngine`.
|
||||
#[repr(C)]
|
||||
@@ -31,6 +36,12 @@ pub enum RegorusStatus {
|
||||
|
||||
/// Invalid policy content.
|
||||
InvalidPolicy,
|
||||
|
||||
/// The engine panicked and cannot be reused until reset.
|
||||
Panic,
|
||||
|
||||
/// The engine remains poisoned because a previous panic was detected.
|
||||
Poisoned,
|
||||
}
|
||||
|
||||
/// Type of data contained in RegorusResult
|
||||
@@ -74,24 +85,37 @@ pub struct RegorusResult {
|
||||
|
||||
/// Pointer value.
|
||||
/// Valid when data_type is Pointer.
|
||||
pub(crate) pointer_value: *mut std::os::raw::c_void,
|
||||
pub(crate) pointer_value: *mut c_void,
|
||||
|
||||
/// Errors produced by the call.
|
||||
/// Owned by Rust.
|
||||
pub(crate) error_message: *mut c_char,
|
||||
}
|
||||
|
||||
/// Byte buffer returned from FFI for binary payloads.
|
||||
///
|
||||
/// Must be freed using `regorus_buffer_drop`.
|
||||
#[repr(C)]
|
||||
pub struct RegorusBuffer {
|
||||
/// Pointer to byte buffer data.
|
||||
pub data: *mut u8,
|
||||
/// Number of bytes stored in `data`.
|
||||
pub len: usize,
|
||||
/// Capacity of the allocation backing `data`.
|
||||
pub capacity: usize,
|
||||
}
|
||||
|
||||
impl RegorusResult {
|
||||
/// Create a successful result with no data.
|
||||
pub(crate) fn ok_void() -> Self {
|
||||
Self {
|
||||
status: RegorusStatus::Ok,
|
||||
data_type: RegorusDataType::None,
|
||||
output: std::ptr::null_mut(),
|
||||
output: ptr::null_mut(),
|
||||
bool_value: false,
|
||||
int_value: 0,
|
||||
pointer_value: std::ptr::null_mut(),
|
||||
error_message: std::ptr::null_mut(),
|
||||
pointer_value: ptr::null_mut(),
|
||||
error_message: ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,8 +127,8 @@ impl RegorusResult {
|
||||
output: to_c_str(output),
|
||||
bool_value: false,
|
||||
int_value: 0,
|
||||
pointer_value: std::ptr::null_mut(),
|
||||
error_message: std::ptr::null_mut(),
|
||||
pointer_value: ptr::null_mut(),
|
||||
error_message: ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,11 +138,11 @@ impl RegorusResult {
|
||||
Self {
|
||||
status: RegorusStatus::Ok,
|
||||
data_type: RegorusDataType::Boolean,
|
||||
output: std::ptr::null_mut(),
|
||||
output: ptr::null_mut(),
|
||||
bool_value: value,
|
||||
int_value: 0,
|
||||
pointer_value: std::ptr::null_mut(),
|
||||
error_message: std::ptr::null_mut(),
|
||||
pointer_value: ptr::null_mut(),
|
||||
error_message: ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,24 +152,24 @@ impl RegorusResult {
|
||||
Self {
|
||||
status: RegorusStatus::Ok,
|
||||
data_type: RegorusDataType::Integer,
|
||||
output: std::ptr::null_mut(),
|
||||
output: ptr::null_mut(),
|
||||
bool_value: false,
|
||||
int_value: value as c_longlong,
|
||||
pointer_value: std::ptr::null_mut(),
|
||||
error_message: std::ptr::null_mut(),
|
||||
pointer_value: ptr::null_mut(),
|
||||
error_message: ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a successful result with pointer value.
|
||||
pub(crate) fn ok_pointer(pointer: *mut std::os::raw::c_void) -> Self {
|
||||
pub(crate) fn ok_pointer(pointer: *mut c_void) -> Self {
|
||||
Self {
|
||||
status: RegorusStatus::Ok,
|
||||
data_type: RegorusDataType::Pointer,
|
||||
output: std::ptr::null_mut(),
|
||||
output: ptr::null_mut(),
|
||||
bool_value: false,
|
||||
int_value: 0,
|
||||
pointer_value: pointer,
|
||||
error_message: std::ptr::null_mut(),
|
||||
error_message: ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,11 +178,11 @@ impl RegorusResult {
|
||||
Self {
|
||||
status,
|
||||
data_type: RegorusDataType::None,
|
||||
output: std::ptr::null_mut(),
|
||||
output: ptr::null_mut(),
|
||||
bool_value: false,
|
||||
int_value: 0,
|
||||
pointer_value: std::ptr::null_mut(),
|
||||
error_message: std::ptr::null_mut(),
|
||||
pointer_value: ptr::null_mut(),
|
||||
error_message: ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,15 +191,27 @@ impl RegorusResult {
|
||||
Self {
|
||||
status,
|
||||
data_type: RegorusDataType::None,
|
||||
output: std::ptr::null_mut(),
|
||||
output: ptr::null_mut(),
|
||||
bool_value: false,
|
||||
int_value: 0,
|
||||
pointer_value: std::ptr::null_mut(),
|
||||
pointer_value: ptr::null_mut(),
|
||||
error_message: to_c_str(message),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RegorusBuffer {
|
||||
pub(crate) fn from_vec(mut data: Vec<u8>) -> *mut RegorusBuffer {
|
||||
let buffer = RegorusBuffer {
|
||||
data: data.as_mut_ptr(),
|
||||
len: data.len(),
|
||||
capacity: data.capacity(),
|
||||
};
|
||||
mem::forget(data);
|
||||
Box::into_raw(Box::new(buffer))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn to_c_str(s: String) -> *mut c_char {
|
||||
match CString::new(s) {
|
||||
Ok(cs) => cs.into_raw(),
|
||||
@@ -213,6 +249,21 @@ pub(crate) fn to_regorus_string_result(r: Result<String>) -> RegorusResult {
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop a `RegorusBuffer`.
|
||||
///
|
||||
/// `data` is not valid after drop.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_buffer_drop(buffer: *mut RegorusBuffer) {
|
||||
if let Ok(buffer) = to_ref(buffer) {
|
||||
unsafe {
|
||||
if !buffer.data.is_null() {
|
||||
let _ = Vec::from_raw_parts(buffer.data, buffer.len, buffer.capacity);
|
||||
}
|
||||
let _ = Box::from_raw(ptr::from_mut(buffer));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop a `RegorusResult`.
|
||||
///
|
||||
/// `output` and `error_message` strings are not valid after drop.
|
||||
|
||||
@@ -2,13 +2,16 @@
|
||||
// Licensed under the MIT License.
|
||||
use crate::common::{from_c_str, RegorusResult, RegorusStatus};
|
||||
use crate::compiled_policy::RegorusCompiledPolicy;
|
||||
use crate::panic_guard::with_unwind_guard;
|
||||
use alloc::boxed::Box;
|
||||
use alloc::format;
|
||||
use alloc::vec::Vec;
|
||||
use core::ffi::{c_char, c_void};
|
||||
use regorus::{compile_policy_with_entrypoint, PolicyModule, Value};
|
||||
|
||||
#[cfg(feature = "azure_policy")]
|
||||
use regorus::compile_policy_for_target;
|
||||
|
||||
use std::os::raw::c_char;
|
||||
|
||||
/// FFI wrapper for PolicyModule struct.
|
||||
#[repr(C)]
|
||||
pub struct RegorusPolicyModule {
|
||||
@@ -41,55 +44,54 @@ pub extern "C" fn regorus_compile_policy_with_entrypoint(
|
||||
modules_len: usize,
|
||||
entry_point_rule: *const c_char,
|
||||
) -> RegorusResult {
|
||||
let data_str = match from_c_str(data_json) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Invalid data JSON string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
with_unwind_guard(|| {
|
||||
let data_str = match from_c_str(data_json) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Invalid data JSON string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let entry_rule = match from_c_str(entry_point_rule) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidEntrypoint,
|
||||
format!("Invalid entry point rule string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
let entry_rule = match from_c_str(entry_point_rule) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidEntrypoint,
|
||||
format!("Invalid entry point rule string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Parse data JSON
|
||||
let data = match Value::from_json_str(&data_str) {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Failed to parse data JSON: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
let data = match Value::from_json_str(&data_str) {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Failed to parse data JSON: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Convert C modules array to Rust Vec
|
||||
let policy_modules = match convert_c_modules_to_rust(modules, modules_len) {
|
||||
Ok(modules) => modules,
|
||||
Err(status) => return RegorusResult::err(status),
|
||||
};
|
||||
let policy_modules = match convert_c_modules_to_rust(modules, modules_len) {
|
||||
Ok(modules) => modules,
|
||||
Err(status) => return RegorusResult::err(status),
|
||||
};
|
||||
|
||||
// Call the convenience function
|
||||
match compile_policy_with_entrypoint(data, &policy_modules, entry_rule.into()) {
|
||||
Ok(compiled_policy) => {
|
||||
let wrapped_policy = RegorusCompiledPolicy { compiled_policy };
|
||||
let boxed_policy = Box::new(wrapped_policy);
|
||||
RegorusResult::ok_pointer(Box::into_raw(boxed_policy) as *mut std::os::raw::c_void)
|
||||
match compile_policy_with_entrypoint(data, &policy_modules, entry_rule.into()) {
|
||||
Ok(compiled_policy) => {
|
||||
let wrapped_policy = RegorusCompiledPolicy { compiled_policy };
|
||||
let boxed_policy = Box::new(wrapped_policy);
|
||||
RegorusResult::ok_pointer(Box::into_raw(boxed_policy) as *mut c_void)
|
||||
}
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("Policy compilation failed: {e}"),
|
||||
),
|
||||
}
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("Policy compilation failed: {e}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Compiles a target-aware policy from data and modules.
|
||||
@@ -120,45 +122,44 @@ pub extern "C" fn regorus_compile_policy_for_target(
|
||||
modules: *const RegorusPolicyModule,
|
||||
modules_len: usize,
|
||||
) -> RegorusResult {
|
||||
let data_str = match from_c_str(data_json) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Invalid data JSON string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
with_unwind_guard(|| {
|
||||
let data_str = match from_c_str(data_json) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Invalid data JSON string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Parse data JSON
|
||||
let data = match Value::from_json_str(&data_str) {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Failed to parse data JSON: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
let data = match Value::from_json_str(&data_str) {
|
||||
Ok(data) => data,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Failed to parse data JSON: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Convert C modules array to Rust Vec
|
||||
let policy_modules = match convert_c_modules_to_rust(modules, modules_len) {
|
||||
Ok(modules) => modules,
|
||||
Err(status) => return RegorusResult::err(status),
|
||||
};
|
||||
let policy_modules = match convert_c_modules_to_rust(modules, modules_len) {
|
||||
Ok(modules) => modules,
|
||||
Err(status) => return RegorusResult::err(status),
|
||||
};
|
||||
|
||||
// Call the convenience function
|
||||
match compile_policy_for_target(data, &policy_modules) {
|
||||
Ok(compiled_policy) => {
|
||||
let wrapped_policy = RegorusCompiledPolicy { compiled_policy };
|
||||
let boxed_policy = Box::new(wrapped_policy);
|
||||
RegorusResult::ok_pointer(Box::into_raw(boxed_policy) as *mut std::os::raw::c_void)
|
||||
match compile_policy_for_target(data, &policy_modules) {
|
||||
Ok(compiled_policy) => {
|
||||
let wrapped_policy = RegorusCompiledPolicy { compiled_policy };
|
||||
let boxed_policy = Box::new(wrapped_policy);
|
||||
RegorusResult::ok_pointer(Box::into_raw(boxed_policy) as *mut c_void)
|
||||
}
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("Target-aware policy compilation failed: {e}"),
|
||||
),
|
||||
}
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("Target-aware policy compilation failed: {e}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Helper function to convert C module array to Rust Vec<PolicyModule>.
|
||||
@@ -184,7 +185,7 @@ fn convert_c_modules_to_rust(
|
||||
let id = match from_c_str(module_ref.id) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
eprintln!("Invalid module ID at index {}: {}", i, e);
|
||||
report_module_error(i, "module ID", &e);
|
||||
return Err(RegorusStatus::InvalidModuleId);
|
||||
}
|
||||
};
|
||||
@@ -192,7 +193,7 @@ fn convert_c_modules_to_rust(
|
||||
let content = match from_c_str(module_ref.content) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
eprintln!("Invalid module content at index {}: {}", i, e);
|
||||
report_module_error(i, "module content", &e);
|
||||
return Err(RegorusStatus::InvalidPolicy);
|
||||
}
|
||||
};
|
||||
@@ -206,3 +207,11 @@ fn convert_c_modules_to_rust(
|
||||
|
||||
Ok(policy_modules)
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn report_module_error(index: usize, kind: &str, err: &anyhow::Error) {
|
||||
eprintln!("Invalid {} at index {}: {}", kind, index, err);
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
fn report_module_error(_index: usize, _kind: &str, _err: &anyhow::Error) {}
|
||||
|
||||
@@ -2,8 +2,12 @@
|
||||
// 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 std::os::raw::c_char;
|
||||
use core::ffi::c_char;
|
||||
use core::ptr;
|
||||
|
||||
/// Wrapper for `regorus::CompiledPolicy`.
|
||||
#[derive(Clone)]
|
||||
@@ -16,7 +20,7 @@ pub struct RegorusCompiledPolicy {
|
||||
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(std::ptr::from_mut(cp));
|
||||
let _ = Box::from_raw(ptr::from_mut(cp));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -32,20 +36,23 @@ pub extern "C" fn regorus_compiled_policy_eval_with_input(
|
||||
compiled_policy: *mut RegorusCompiledPolicy,
|
||||
input: *const c_char,
|
||||
) -> RegorusResult {
|
||||
let output = || -> Result<String> {
|
||||
let input_value = regorus::Value::from_json_str(&from_c_str(input)?)?;
|
||||
let result = to_ref(compiled_policy)?
|
||||
.compiled_policy
|
||||
.eval_with_input(input_value)?;
|
||||
result.to_json_str()
|
||||
}();
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let input_value = regorus::Value::from_json_str(&from_c_str(input)?)?;
|
||||
let result = to_ref(compiled_policy)?
|
||||
.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)),
|
||||
}
|
||||
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.
|
||||
///
|
||||
@@ -56,14 +63,16 @@ pub extern "C" fn regorus_compiled_policy_eval_with_input(
|
||||
pub extern "C" fn regorus_compiled_policy_get_policy_info(
|
||||
compiled_policy: *mut RegorusCompiledPolicy,
|
||||
) -> RegorusResult {
|
||||
let output = || -> Result<String> {
|
||||
let info = to_ref(compiled_policy)?.compiled_policy.get_policy_info()?;
|
||||
serde_json::to_string(&info)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to serialize policy info: {}", e))
|
||||
}();
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let info = to_ref(compiled_policy)?.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)),
|
||||
}
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#![cfg(feature = "azure_policy")]
|
||||
use crate::common::{from_c_str, RegorusResult, RegorusStatus};
|
||||
use crate::panic_guard::with_unwind_guard;
|
||||
use regorus::{registry::schemas, Schema};
|
||||
|
||||
use std::os::raw::c_char;
|
||||
@@ -29,45 +30,45 @@ pub extern "C" fn regorus_effect_schema_register(
|
||||
name: *const c_char,
|
||||
schema_json: *const c_char,
|
||||
) -> RegorusResult {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid effect schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
with_unwind_guard(|| {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid effect schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let schema_str = match from_c_str(schema_json) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Invalid effect schema JSON string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
let schema_str = match from_c_str(schema_json) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Invalid effect schema JSON string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Parse schema from JSON
|
||||
let schema = match Schema::from_json_str(&schema_str) {
|
||||
Ok(schema) => schema,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Failed to parse effect schema JSON: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
let schema = match Schema::from_json_str(&schema_str) {
|
||||
Ok(schema) => schema,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Failed to parse effect schema JSON: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Register the schema
|
||||
match schemas::effect::register(schema_name, schema.into()) {
|
||||
Ok(()) => RegorusResult::ok_pointer(std::ptr::null_mut()),
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::Error,
|
||||
format!("Failed to register effect schema: {e}"),
|
||||
),
|
||||
}
|
||||
match schemas::effect::register(schema_name, schema.into()) {
|
||||
Ok(()) => RegorusResult::ok_pointer(std::ptr::null_mut()),
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::Error,
|
||||
format!("Failed to register effect schema: {e}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Check if an effect schema with the given name exists.
|
||||
@@ -83,18 +84,20 @@ pub extern "C" fn regorus_effect_schema_register(
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_effect_schema_contains(name: *const c_char) -> RegorusResult {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid effect schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
with_unwind_guard(|| {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid effect schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let contains = schemas::effect::contains(&schema_name);
|
||||
RegorusResult::ok_bool(contains)
|
||||
let contains = schemas::effect::contains(&schema_name);
|
||||
RegorusResult::ok_bool(contains)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the number of registered effect schemas.
|
||||
@@ -104,8 +107,10 @@ pub extern "C" fn regorus_effect_schema_contains(name: *const c_char) -> Regorus
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_effect_schema_len() -> RegorusResult {
|
||||
let count = schemas::effect::len();
|
||||
RegorusResult::ok_int(count as i64)
|
||||
with_unwind_guard(|| {
|
||||
let count = schemas::effect::len();
|
||||
RegorusResult::ok_int(count as i64)
|
||||
})
|
||||
}
|
||||
|
||||
/// Check if the effect schema registry is empty.
|
||||
@@ -115,8 +120,10 @@ pub extern "C" fn regorus_effect_schema_len() -> RegorusResult {
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_effect_schema_is_empty() -> RegorusResult {
|
||||
let is_empty = schemas::effect::is_empty();
|
||||
RegorusResult::ok_bool(is_empty)
|
||||
with_unwind_guard(|| {
|
||||
let is_empty = schemas::effect::is_empty();
|
||||
RegorusResult::ok_bool(is_empty)
|
||||
})
|
||||
}
|
||||
|
||||
/// List all registered effect schema names as a JSON array.
|
||||
@@ -126,14 +133,16 @@ pub extern "C" fn regorus_effect_schema_is_empty() -> RegorusResult {
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_effect_schema_list_names() -> RegorusResult {
|
||||
let names = schemas::effect::list_names();
|
||||
match serde_json::to_string(&names) {
|
||||
Ok(json_str) => RegorusResult::ok_string(json_str),
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::Error,
|
||||
format!("Failed to serialize effect schema names to JSON: {e}"),
|
||||
),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let names = schemas::effect::list_names();
|
||||
match serde_json::to_string(&names) {
|
||||
Ok(json_str) => RegorusResult::ok_string(json_str),
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::Error,
|
||||
format!("Failed to serialize effect schema names to JSON: {e}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Remove an effect schema by name.
|
||||
@@ -149,18 +158,20 @@ pub extern "C" fn regorus_effect_schema_list_names() -> RegorusResult {
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_effect_schema_remove(name: *const c_char) -> RegorusResult {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid effect schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
with_unwind_guard(|| {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid effect schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let removed = schemas::effect::remove(&schema_name).is_some();
|
||||
RegorusResult::ok_bool(removed)
|
||||
let removed = schemas::effect::remove(&schema_name).is_some();
|
||||
RegorusResult::ok_bool(removed)
|
||||
})
|
||||
}
|
||||
|
||||
/// Clear all effect schemas from the registry.
|
||||
@@ -170,6 +181,8 @@ pub extern "C" fn regorus_effect_schema_remove(name: *const c_char) -> RegorusRe
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_effect_schema_clear() -> RegorusResult {
|
||||
schemas::effect::clear();
|
||||
RegorusResult::ok_pointer(std::ptr::null_mut())
|
||||
with_unwind_guard(|| {
|
||||
schemas::effect::clear();
|
||||
RegorusResult::ok_pointer(std::ptr::null_mut())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -5,13 +5,170 @@ use crate::common::{
|
||||
from_c_str, to_ref, to_regorus_result, to_regorus_string_result, RegorusResult, RegorusStatus,
|
||||
};
|
||||
use crate::compiled_policy::RegorusCompiledPolicy;
|
||||
use anyhow::Result;
|
||||
use std::os::raw::c_char;
|
||||
use crate::limits::RegorusExecutionTimerConfig;
|
||||
use crate::lock::{new_handle, read, try_read, try_write, Handle, ReadGuard, WriteGuard};
|
||||
use crate::panic_guard::with_unwind_guard;
|
||||
use alloc::boxed::Box;
|
||||
use alloc::format;
|
||||
use alloc::string::String;
|
||||
#[cfg(feature = "rvm")]
|
||||
use alloc::sync::Arc;
|
||||
use alloc::vec::Vec;
|
||||
use anyhow::{anyhow, Result};
|
||||
use core::ffi::{c_char, c_void};
|
||||
use core::ptr;
|
||||
#[cfg(feature = "rvm")]
|
||||
use regorus::languages::rego::compiler::Compiler;
|
||||
#[cfg(feature = "rvm")]
|
||||
use regorus::rvm::program::Program;
|
||||
|
||||
/// Wrapper for `regorus::Engine`.
|
||||
#[derive(Clone)]
|
||||
pub struct RegorusEngine {
|
||||
engine: ::regorus::Engine,
|
||||
engine: Handle<::regorus::Engine>,
|
||||
}
|
||||
|
||||
impl RegorusEngine {
|
||||
fn new(engine: ::regorus::Engine) -> Self {
|
||||
Self {
|
||||
engine: new_handle(engine),
|
||||
}
|
||||
}
|
||||
|
||||
fn contention_error() -> anyhow::Error {
|
||||
anyhow!(
|
||||
"regorus engine handle is already in use; clone the engine before sharing across threads"
|
||||
)
|
||||
}
|
||||
|
||||
fn try_write(&self) -> Result<WriteGuard<'_, ::regorus::Engine>> {
|
||||
try_write(&self.engine).ok_or_else(Self::contention_error)
|
||||
}
|
||||
|
||||
fn try_read(&self) -> Result<ReadGuard<'_, ::regorus::Engine>> {
|
||||
try_read(&self.engine).ok_or_else(Self::contention_error)
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for RegorusEngine {
|
||||
fn clone(&self) -> Self {
|
||||
let guard = read(&self.engine);
|
||||
Self::new((*guard).clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "contention_checks", feature = "std"))]
|
||||
mod tests {
|
||||
use super::RegorusEngine;
|
||||
|
||||
#[test]
|
||||
fn detects_handle_contention() {
|
||||
let engine = RegorusEngine::new(::regorus::Engine::new());
|
||||
|
||||
let _first_guard = engine.try_write().expect("initial lock should succeed");
|
||||
let err = engine
|
||||
.try_write()
|
||||
.expect_err("contention detection must reject the second lock");
|
||||
|
||||
assert!(
|
||||
err.to_string().contains("engine handle is already in use"),
|
||||
"unexpected error message: {err}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "std"))]
|
||||
mod panic_tests {
|
||||
use super::{
|
||||
regorus_engine_drop, regorus_engine_eval_query, regorus_engine_get_policies,
|
||||
regorus_engine_new,
|
||||
};
|
||||
use crate::common::{regorus_result_drop, RegorusStatus};
|
||||
use crate::panic_guard::{is_poisoned, reset_poison};
|
||||
use alloc::boxed::Box;
|
||||
use regorus::Value;
|
||||
use std::ffi::{CStr, CString};
|
||||
|
||||
#[test]
|
||||
fn catches_extension_panics_and_marks_poison() {
|
||||
reset_poison();
|
||||
|
||||
let engine_ptr = regorus_engine_new();
|
||||
assert!(!engine_ptr.is_null(), "engine allocation must succeed");
|
||||
assert!(!is_poisoned(), "guard must start unpoisoned");
|
||||
|
||||
unsafe {
|
||||
let engine = &mut *engine_ptr;
|
||||
{
|
||||
let mut guard = engine
|
||||
.try_write()
|
||||
.expect("exclusive access to configure engine");
|
||||
guard
|
||||
.add_extension(
|
||||
"panic_extension".to_string(),
|
||||
0,
|
||||
Box::new(|_| -> anyhow::Result<Value> { panic!("ffi extension panic") }),
|
||||
)
|
||||
.expect("extension registration must succeed");
|
||||
guard
|
||||
.add_policy(
|
||||
"panic.rego".to_string(),
|
||||
"package panic\n\ndefault allow = false\n\nallow if {\n panic_extension()\n}"
|
||||
.to_string(),
|
||||
)
|
||||
.expect("policy registration must succeed");
|
||||
}
|
||||
}
|
||||
|
||||
let query = CString::new("data.panic.allow").expect("valid query string");
|
||||
let panic_result = regorus_engine_eval_query(engine_ptr, query.as_ptr());
|
||||
assert!(matches!(panic_result.status, RegorusStatus::Panic));
|
||||
unsafe {
|
||||
assert!(
|
||||
!panic_result.error_message.is_null(),
|
||||
"panic details must be present"
|
||||
);
|
||||
let message = CStr::from_ptr(panic_result.error_message)
|
||||
.to_str()
|
||||
.expect("error message utf8");
|
||||
assert!(
|
||||
message.contains("ffi extension panic"),
|
||||
"panic payload must bubble across guard"
|
||||
);
|
||||
}
|
||||
regorus_result_drop(panic_result);
|
||||
assert!(is_poisoned(), "engine must be marked poisoned after panic");
|
||||
|
||||
let poisoned_result = regorus_engine_get_policies(engine_ptr);
|
||||
assert!(matches!(poisoned_result.status, RegorusStatus::Poisoned));
|
||||
unsafe {
|
||||
assert!(
|
||||
!poisoned_result.error_message.is_null(),
|
||||
"poison message must be present"
|
||||
);
|
||||
let message = CStr::from_ptr(poisoned_result.error_message)
|
||||
.to_str()
|
||||
.expect("poison message utf8");
|
||||
assert!(
|
||||
message.contains("regorus is poisoned"),
|
||||
"poison message must inform callers"
|
||||
);
|
||||
}
|
||||
regorus_result_drop(poisoned_result);
|
||||
|
||||
regorus_engine_drop(engine_ptr);
|
||||
reset_poison();
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "std")]
|
||||
pub extern "C" fn regorus_engine_test_trigger_panic() -> RegorusResult {
|
||||
with_unwind_guard(|| panic!("regorus ffi test panic"))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_engine_test_reset_poison() {
|
||||
crate::panic_guard::reset_poison();
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -25,7 +182,7 @@ pub extern "C" fn regorus_engine_new() -> *mut RegorusEngine {
|
||||
// instead of raising errors in certain failure scenarios.
|
||||
engine.set_strict_builtin_errors(false);
|
||||
|
||||
Box::into_raw(Box::new(RegorusEngine { engine }))
|
||||
Box::into_raw(Box::new(RegorusEngine::new(engine)))
|
||||
}
|
||||
|
||||
/// Clone a [`RegorusEngine`]
|
||||
@@ -37,7 +194,7 @@ pub extern "C" fn regorus_engine_new() -> *mut RegorusEngine {
|
||||
pub extern "C" fn regorus_engine_clone(engine: *mut RegorusEngine) -> *mut RegorusEngine {
|
||||
match to_ref(engine) {
|
||||
Ok(e) => Box::into_raw(Box::new(e.clone())),
|
||||
_ => std::ptr::null_mut(),
|
||||
_ => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,7 +202,7 @@ pub extern "C" fn regorus_engine_clone(engine: *mut RegorusEngine) -> *mut Regor
|
||||
pub extern "C" fn regorus_engine_drop(engine: *mut RegorusEngine) {
|
||||
if let Ok(e) = to_ref(engine) {
|
||||
unsafe {
|
||||
let _ = Box::from_raw(std::ptr::from_mut(e));
|
||||
let _ = Box::from_raw(ptr::from_mut(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,11 +220,13 @@ pub extern "C" fn regorus_engine_add_policy(
|
||||
path: *const c_char,
|
||||
rego: *const c_char,
|
||||
) -> RegorusResult {
|
||||
to_regorus_string_result(|| -> Result<String> {
|
||||
to_ref(engine)?
|
||||
.engine
|
||||
.add_policy(from_c_str(path)?, from_c_str(rego)?)
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_string_result(|| -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.add_policy(from_c_str(path)?, from_c_str(rego)?)
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
@@ -76,11 +235,13 @@ pub extern "C" fn regorus_engine_add_policy_from_file(
|
||||
engine: *mut RegorusEngine,
|
||||
path: *const c_char,
|
||||
) -> RegorusResult {
|
||||
to_regorus_string_result(|| -> Result<String> {
|
||||
to_ref(engine)?
|
||||
.engine
|
||||
.add_policy_from_file(from_c_str(path)?)
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_string_result(|| -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.add_policy_from_file(from_c_str(path)?)
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Add policy data.
|
||||
@@ -92,11 +253,13 @@ pub extern "C" fn regorus_engine_add_data_json(
|
||||
engine: *mut RegorusEngine,
|
||||
data: *const c_char,
|
||||
) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
to_ref(engine)?
|
||||
.engine
|
||||
.add_data(regorus::Value::from_json_str(&from_c_str(data)?)?)
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.add_data(regorus::Value::from_json_str(&from_c_str(data)?)?)
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Get list of loaded Rego packages as JSON.
|
||||
@@ -104,10 +267,13 @@ pub extern "C" fn regorus_engine_add_data_json(
|
||||
/// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_packages
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_engine_get_packages(engine: *mut RegorusEngine) -> RegorusResult {
|
||||
to_regorus_string_result(|| -> Result<String> {
|
||||
serde_json::to_string_pretty(&to_ref(engine)?.engine.get_packages()?)
|
||||
.map_err(anyhow::Error::msg)
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_string_result(|| -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let guard = engine.try_read()?;
|
||||
serde_json::to_string_pretty(&guard.get_packages()?).map_err(anyhow::Error::msg)
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Get list of policies as JSON.
|
||||
@@ -115,9 +281,13 @@ pub extern "C" fn regorus_engine_get_packages(engine: *mut RegorusEngine) -> Reg
|
||||
/// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_policies
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_engine_get_policies(engine: *mut RegorusEngine) -> RegorusResult {
|
||||
to_regorus_string_result(|| -> Result<String> {
|
||||
to_ref(engine)?.engine.get_policies_as_json()
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_string_result(|| -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let guard = engine.try_read()?;
|
||||
guard.get_policies_as_json()
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
@@ -126,11 +296,13 @@ pub extern "C" fn regorus_engine_add_data_from_json_file(
|
||||
engine: *mut RegorusEngine,
|
||||
path: *const c_char,
|
||||
) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
to_ref(engine)?
|
||||
.engine
|
||||
.add_data(regorus::Value::from_json_file(from_c_str(path)?)?)
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.add_data(regorus::Value::from_json_file(from_c_str(path)?)?)
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Clear policy data.
|
||||
@@ -138,10 +310,14 @@ pub extern "C" fn regorus_engine_add_data_from_json_file(
|
||||
/// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.clear_data
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_engine_clear_data(engine: *mut RegorusEngine) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
to_ref(engine)?.engine.clear_data();
|
||||
Ok(())
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.clear_data();
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Set input.
|
||||
@@ -153,12 +329,14 @@ pub extern "C" fn regorus_engine_set_input_json(
|
||||
engine: *mut RegorusEngine,
|
||||
input: *const c_char,
|
||||
) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
to_ref(engine)?
|
||||
.engine
|
||||
.set_input(regorus::Value::from_json_str(&from_c_str(input)?)?);
|
||||
Ok(())
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.set_input(regorus::Value::from_json_str(&from_c_str(input)?)?);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
@@ -167,12 +345,14 @@ pub extern "C" fn regorus_engine_set_input_from_json_file(
|
||||
engine: *mut RegorusEngine,
|
||||
path: *const c_char,
|
||||
) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
to_ref(engine)?
|
||||
.engine
|
||||
.set_input(regorus::Value::from_json_file(from_c_str(path)?)?);
|
||||
Ok(())
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.set_input(regorus::Value::from_json_file(from_c_str(path)?)?);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Evaluate query.
|
||||
@@ -184,16 +364,18 @@ pub extern "C" fn regorus_engine_eval_query(
|
||||
engine: *mut RegorusEngine,
|
||||
query: *const c_char,
|
||||
) -> RegorusResult {
|
||||
let output = || -> Result<String> {
|
||||
let results = to_ref(engine)?
|
||||
.engine
|
||||
.eval_query(from_c_str(query)?, false)?;
|
||||
Ok(serde_json::to_string_pretty(&results)?)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
let results = guard.eval_query(from_c_str(query)?, false)?;
|
||||
Ok(serde_json::to_string_pretty(&results)?)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Evaluate specified rule.
|
||||
@@ -205,16 +387,17 @@ pub extern "C" fn regorus_engine_eval_rule(
|
||||
engine: *mut RegorusEngine,
|
||||
rule: *const c_char,
|
||||
) -> RegorusResult {
|
||||
let output = || -> Result<String> {
|
||||
to_ref(engine)?
|
||||
.engine
|
||||
.eval_rule(from_c_str(rule)?)?
|
||||
.to_json_str()
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.eval_rule(from_c_str(rule)?)?.to_json_str()
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Enable/disable coverage.
|
||||
@@ -227,10 +410,14 @@ pub extern "C" fn regorus_engine_set_enable_coverage(
|
||||
engine: *mut RegorusEngine,
|
||||
enable: bool,
|
||||
) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
to_ref(engine)?.engine.set_enable_coverage(enable);
|
||||
Ok(())
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.set_enable_coverage(enable);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Get coverage report.
|
||||
@@ -239,15 +426,17 @@ pub extern "C" fn regorus_engine_set_enable_coverage(
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "coverage")]
|
||||
pub extern "C" fn regorus_engine_get_coverage_report(engine: *mut RegorusEngine) -> RegorusResult {
|
||||
let output = || -> Result<String> {
|
||||
Ok(serde_json::to_string_pretty(
|
||||
&to_ref(engine)?.engine.get_coverage_report()?,
|
||||
)?)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let guard = engine.try_read()?;
|
||||
Ok(serde_json::to_string_pretty(&guard.get_coverage_report()?)?)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Enable/disable strict builtin errors.
|
||||
@@ -258,9 +447,46 @@ pub extern "C" fn regorus_engine_get_coverage_report(engine: *mut RegorusEngine)
|
||||
pub extern "C" fn regorus_engine_set_strict_builtin_errors(
|
||||
engine: *mut RegorusEngine,
|
||||
strict: bool,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.set_strict_builtin_errors(strict);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// Configure the execution timer for a specific engine instance.
|
||||
pub extern "C" fn regorus_engine_set_execution_timer_config(
|
||||
engine: *mut RegorusEngine,
|
||||
config: *const RegorusExecutionTimerConfig,
|
||||
) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
to_ref(engine)?.engine.set_strict_builtin_errors(strict);
|
||||
let engine = to_ref(engine)?;
|
||||
let config = unsafe {
|
||||
config
|
||||
.as_ref()
|
||||
.copied()
|
||||
.ok_or_else(|| anyhow!("execution timer config pointer is null"))?
|
||||
};
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.set_execution_timer_config(config.to_execution_timer_config()?);
|
||||
Ok(())
|
||||
}())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// Clear the engine-specific execution timer configuration.
|
||||
pub extern "C" fn regorus_engine_clear_execution_timer_config(
|
||||
engine: *mut RegorusEngine,
|
||||
) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.clear_execution_timer_config();
|
||||
Ok(())
|
||||
}())
|
||||
}
|
||||
@@ -273,16 +499,17 @@ pub extern "C" fn regorus_engine_set_strict_builtin_errors(
|
||||
pub extern "C" fn regorus_engine_get_coverage_report_pretty(
|
||||
engine: *mut RegorusEngine,
|
||||
) -> RegorusResult {
|
||||
let output = || -> Result<String> {
|
||||
to_ref(engine)?
|
||||
.engine
|
||||
.get_coverage_report()?
|
||||
.to_string_pretty()
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let guard = engine.try_read()?;
|
||||
guard.get_coverage_report()?.to_string_pretty()
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Clear coverage data.
|
||||
@@ -291,10 +518,14 @@ pub extern "C" fn regorus_engine_get_coverage_report_pretty(
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "coverage")]
|
||||
pub extern "C" fn regorus_engine_clear_coverage_data(engine: *mut RegorusEngine) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
to_ref(engine)?.engine.clear_coverage_data();
|
||||
Ok(())
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.clear_coverage_data();
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether to gather output of print statements.
|
||||
@@ -306,10 +537,14 @@ pub extern "C" fn regorus_engine_set_gather_prints(
|
||||
engine: *mut RegorusEngine,
|
||||
enable: bool,
|
||||
) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
to_ref(engine)?.engine.set_gather_prints(enable);
|
||||
Ok(())
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.set_gather_prints(enable);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Take all the gathered print statements.
|
||||
@@ -317,15 +552,17 @@ pub extern "C" fn regorus_engine_set_gather_prints(
|
||||
/// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.take_prints
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_engine_take_prints(engine: *mut RegorusEngine) -> RegorusResult {
|
||||
let output = || -> Result<String> {
|
||||
Ok(serde_json::to_string_pretty(
|
||||
&to_ref(engine)?.engine.take_prints()?,
|
||||
)?)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
Ok(serde_json::to_string_pretty(&guard.take_prints()?)?)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Get AST of policies.
|
||||
@@ -334,11 +571,17 @@ pub extern "C" fn regorus_engine_take_prints(engine: *mut RegorusEngine) -> Rego
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "ast")]
|
||||
pub extern "C" fn regorus_engine_get_ast_as_json(engine: *mut RegorusEngine) -> RegorusResult {
|
||||
let output = || -> Result<String> { to_ref(engine)?.engine.get_ast_as_json() }();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let guard = engine.try_read()?;
|
||||
guard.get_ast_as_json()
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Gets the package names defined in each policy added to the engine.
|
||||
@@ -349,14 +592,18 @@ pub extern "C" fn regorus_engine_get_ast_as_json(engine: *mut RegorusEngine) ->
|
||||
pub extern "C" fn regorus_engine_get_policy_package_names(
|
||||
engine: *mut RegorusEngine,
|
||||
) -> RegorusResult {
|
||||
let output = || -> Result<String> {
|
||||
serde_json::to_string_pretty(&to_ref(engine)?.engine.get_policy_package_names()?)
|
||||
.map_err(anyhow::Error::msg)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let guard = engine.try_read()?;
|
||||
serde_json::to_string_pretty(&guard.get_policy_package_names()?)
|
||||
.map_err(anyhow::Error::msg)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Gets the parameters defined in each policy added to the engine.
|
||||
@@ -367,14 +614,18 @@ pub extern "C" fn regorus_engine_get_policy_package_names(
|
||||
pub extern "C" fn regorus_engine_get_policy_parameters(
|
||||
engine: *mut RegorusEngine,
|
||||
) -> RegorusResult {
|
||||
let output = || -> Result<String> {
|
||||
serde_json::to_string_pretty(&to_ref(engine)?.engine.get_policy_parameters()?)
|
||||
.map_err(anyhow::Error::msg)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let engine = to_ref(engine)?;
|
||||
let guard = engine.try_read()?;
|
||||
serde_json::to_string_pretty(&guard.get_policy_parameters()?)
|
||||
.map_err(anyhow::Error::msg)
|
||||
}();
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Enable/disable rego v1.
|
||||
@@ -385,14 +636,18 @@ pub extern "C" fn regorus_engine_set_rego_v0(
|
||||
engine: *mut RegorusEngine,
|
||||
enable: bool,
|
||||
) -> RegorusResult {
|
||||
let output = || -> Result<()> {
|
||||
to_ref(engine)?.engine.set_rego_v0(enable);
|
||||
Ok(())
|
||||
}();
|
||||
match output {
|
||||
Ok(()) => RegorusResult::ok_void(),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<()> {
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
guard.set_rego_v0(enable);
|
||||
Ok(())
|
||||
}();
|
||||
match output {
|
||||
Ok(()) => RegorusResult::ok_void(),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Compile a target-aware policy from the current engine state.
|
||||
@@ -404,23 +659,39 @@ pub extern "C" fn regorus_engine_set_rego_v0(
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "azure_policy")]
|
||||
pub extern "C" fn regorus_engine_compile_for_target(engine: *mut RegorusEngine) -> RegorusResult {
|
||||
match to_ref(engine) {
|
||||
Ok(e) => match e.engine.compile_for_target() {
|
||||
with_unwind_guard(|| {
|
||||
let engine = match to_ref(engine) {
|
||||
Ok(engine) => engine,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Failed to get engine reference: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let mut guard = match engine.try_write() {
|
||||
Ok(guard) => guard,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::Error,
|
||||
format!("Failed to lock engine: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
match guard.compile_for_target() {
|
||||
Ok(compiled_policy) => {
|
||||
let wrapped_policy = RegorusCompiledPolicy { compiled_policy };
|
||||
let boxed_policy = Box::new(wrapped_policy);
|
||||
RegorusResult::ok_pointer(Box::into_raw(boxed_policy) as *mut std::os::raw::c_void)
|
||||
RegorusResult::ok_pointer(Box::into_raw(boxed_policy) as *mut c_void)
|
||||
}
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("Failed to compile for target: {e}"),
|
||||
),
|
||||
},
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Failed to get engine reference: {e}"),
|
||||
),
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Compile a policy with a specific entry point rule.
|
||||
@@ -434,21 +705,87 @@ pub extern "C" fn regorus_engine_compile_with_entrypoint(
|
||||
engine: *mut RegorusEngine,
|
||||
rule: *const c_char,
|
||||
) -> RegorusResult {
|
||||
let result = || -> Result<RegorusCompiledPolicy> {
|
||||
let rule_str = from_c_str(rule)?;
|
||||
let rule_rc: regorus::Rc<str> = rule_str.into();
|
||||
let compiled_policy = to_ref(engine)?.engine.compile_with_entrypoint(&rule_rc)?;
|
||||
Ok(RegorusCompiledPolicy { compiled_policy })
|
||||
}();
|
||||
with_unwind_guard(|| {
|
||||
let result = || -> Result<RegorusCompiledPolicy> {
|
||||
let rule_str = from_c_str(rule)?;
|
||||
let rule_rc: regorus::Rc<str> = rule_str.into();
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
let compiled_policy = guard.compile_with_entrypoint(&rule_rc)?;
|
||||
Ok(RegorusCompiledPolicy { compiled_policy })
|
||||
}();
|
||||
|
||||
match result {
|
||||
Ok(wrapped_policy) => {
|
||||
let boxed_policy = Box::new(wrapped_policy);
|
||||
RegorusResult::ok_pointer(Box::into_raw(boxed_policy) as *mut std::os::raw::c_void)
|
||||
match result {
|
||||
Ok(wrapped_policy) => {
|
||||
let boxed_policy = Box::new(wrapped_policy);
|
||||
RegorusResult::ok_pointer(Box::into_raw(boxed_policy) as *mut c_void)
|
||||
}
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("Failed to compile with entrypoint: {e}"),
|
||||
),
|
||||
}
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("Failed to compile with entrypoint: {e}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Compile an RVM program from the engine state with entry points.
|
||||
///
|
||||
/// * `entry_points` - Array of entry point rule paths
|
||||
/// * `entry_points_len` - Number of entry points
|
||||
#[cfg(feature = "rvm")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_engine_compile_program_with_entrypoints(
|
||||
engine: *mut RegorusEngine,
|
||||
entry_points: *const *const c_char,
|
||||
entry_points_len: usize,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let result = || -> Result<Arc<Program>> {
|
||||
if entry_points_len == 0 {
|
||||
return Err(anyhow!("entry_points must contain at least one entry"));
|
||||
}
|
||||
|
||||
if entry_points.is_null() && entry_points_len > 0 {
|
||||
return Err(anyhow!("null entry_points pointer"));
|
||||
}
|
||||
|
||||
let mut entry_points_vec = Vec::with_capacity(entry_points_len);
|
||||
for i in 0..entry_points_len {
|
||||
unsafe {
|
||||
let entry_ptr = entry_points.add(i);
|
||||
if entry_ptr.is_null() {
|
||||
return Err(anyhow!("null entry point at index {i}"));
|
||||
}
|
||||
let entry = from_c_str(*entry_ptr)?;
|
||||
entry_points_vec.push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
let entry_points_ref: Vec<&str> = entry_points_vec.iter().map(|s| s.as_str()).collect();
|
||||
|
||||
let rule = entry_points_ref
|
||||
.first()
|
||||
.ok_or_else(|| anyhow!("entry_points must contain at least one entry"))?;
|
||||
let rule_rc: regorus::Rc<str> = (*rule).into();
|
||||
|
||||
let engine = to_ref(engine)?;
|
||||
let mut guard = engine.try_write()?;
|
||||
let compiled_policy = guard.compile_with_entrypoint(&rule_rc)?;
|
||||
|
||||
let program = Compiler::compile_from_policy(&compiled_policy, &entry_points_ref)?;
|
||||
Ok(program)
|
||||
}();
|
||||
|
||||
match result {
|
||||
Ok(program) => {
|
||||
let wrapped = crate::rvm::RegorusProgram { program };
|
||||
let boxed = Box::new(wrapped);
|
||||
RegorusResult::ok_pointer(Box::into_raw(boxed) as *mut c_void)
|
||||
}
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("Failed to compile RVM program: {e}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
mod allocator;
|
||||
mod common;
|
||||
mod compile;
|
||||
mod compiled_policy;
|
||||
mod effect_registry;
|
||||
mod engine;
|
||||
mod limits;
|
||||
mod lock;
|
||||
mod panic_guard;
|
||||
#[cfg(feature = "rvm")]
|
||||
pub(crate) mod rvm;
|
||||
mod schema_registry;
|
||||
mod target_registry;
|
||||
|
||||
217
bindings/ffi/src/limits.rs
Normal file
217
bindings/ffi/src/limits.rs
Normal file
@@ -0,0 +1,217 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
use crate::common::{to_regorus_result, RegorusResult, RegorusStatus};
|
||||
use alloc::format;
|
||||
use anyhow::{anyhow, Result};
|
||||
use core::num::NonZeroU32;
|
||||
use core::time::Duration;
|
||||
use regorus::utils::limits::{self, ExecutionTimerConfig};
|
||||
|
||||
#[cfg(feature = "allocator-memory-limits")]
|
||||
fn some_or_none(flag: bool, value: u64) -> Option<u64> {
|
||||
if flag {
|
||||
Some(value)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "allocator-memory-limits")]
|
||||
fn optional_u64_to_result(value: Option<u64>) -> RegorusResult {
|
||||
match value {
|
||||
Some(bytes) => {
|
||||
if bytes > i64::MAX as u64 {
|
||||
RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!(
|
||||
"value {bytes} exceeds i64::MAX ({max}) bridge limit",
|
||||
max = i64::MAX
|
||||
),
|
||||
)
|
||||
} else {
|
||||
let mut result = RegorusResult::ok_int(bytes as i64);
|
||||
result.bool_value = true;
|
||||
result
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let mut result = RegorusResult::ok_void();
|
||||
result.bool_value = false;
|
||||
result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "allocator-memory-limits")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_set_global_memory_limit(limit: u64, has_limit: bool) -> RegorusResult {
|
||||
::regorus::set_global_memory_limit(some_or_none(has_limit, limit));
|
||||
RegorusResult::ok_void()
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "allocator-memory-limits"))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_set_global_memory_limit(_limit: u64, _has_limit: bool) -> RegorusResult {
|
||||
feature_disabled("regorus_set_global_memory_limit")
|
||||
}
|
||||
|
||||
#[cfg(feature = "allocator-memory-limits")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_get_global_memory_limit() -> RegorusResult {
|
||||
optional_u64_to_result(::regorus::global_memory_limit())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "allocator-memory-limits"))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_get_global_memory_limit() -> RegorusResult {
|
||||
feature_disabled("regorus_get_global_memory_limit")
|
||||
}
|
||||
|
||||
#[cfg(feature = "allocator-memory-limits")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_check_global_memory_limit() -> RegorusResult {
|
||||
match ::regorus::check_global_memory_limit() {
|
||||
Ok(()) => RegorusResult::ok_void(),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, format!("{err}")),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "allocator-memory-limits"))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_check_global_memory_limit() -> RegorusResult {
|
||||
feature_disabled("regorus_check_global_memory_limit")
|
||||
}
|
||||
|
||||
#[cfg(feature = "allocator-memory-limits")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_flush_thread_memory_counters() -> RegorusResult {
|
||||
::regorus::flush_thread_memory_counters();
|
||||
RegorusResult::ok_void()
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "allocator-memory-limits"))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_flush_thread_memory_counters() -> RegorusResult {
|
||||
feature_disabled("regorus_flush_thread_memory_counters")
|
||||
}
|
||||
|
||||
#[cfg(feature = "allocator-memory-limits")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_set_thread_flush_threshold_override(
|
||||
bytes: u64,
|
||||
has_threshold: bool,
|
||||
) -> RegorusResult {
|
||||
::regorus::set_thread_flush_threshold_override(some_or_none(has_threshold, bytes));
|
||||
RegorusResult::ok_void()
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "allocator-memory-limits"))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_set_thread_flush_threshold_override(
|
||||
_bytes: u64,
|
||||
_has_threshold: bool,
|
||||
) -> RegorusResult {
|
||||
feature_disabled("regorus_set_thread_flush_threshold_override")
|
||||
}
|
||||
|
||||
#[cfg(feature = "allocator-memory-limits")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_get_thread_memory_flush_threshold() -> RegorusResult {
|
||||
optional_u64_to_result(::regorus::thread_memory_flush_threshold())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "allocator-memory-limits"))]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_get_thread_memory_flush_threshold() -> RegorusResult {
|
||||
feature_disabled("regorus_get_thread_memory_flush_threshold")
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "allocator-memory-limits"))]
|
||||
fn feature_disabled(function: &str) -> RegorusResult {
|
||||
RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("{function} unavailable: regorus built without allocator-memory-limits feature"),
|
||||
)
|
||||
}
|
||||
|
||||
/// FFI representation of [`ExecutionTimerConfig`].
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct RegorusExecutionTimerConfig {
|
||||
/// Wall-clock limit expressed in nanoseconds.
|
||||
pub limit_ns: u64,
|
||||
/// Number of work units between timer checks (must be non-zero).
|
||||
pub check_interval: u32,
|
||||
}
|
||||
|
||||
impl RegorusExecutionTimerConfig {
|
||||
pub fn to_execution_timer_config(self) -> Result<ExecutionTimerConfig> {
|
||||
let check_interval = NonZeroU32::new(self.check_interval)
|
||||
.ok_or_else(|| anyhow!("execution_timer.check_interval must be non-zero"))?;
|
||||
let limit = Duration::from_nanos(self.limit_ns);
|
||||
|
||||
Ok(ExecutionTimerConfig {
|
||||
limit,
|
||||
check_interval,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_set_fallback_execution_timer_config(
|
||||
config: RegorusExecutionTimerConfig,
|
||||
) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
limits::set_fallback_execution_timer_config(Some(config.to_execution_timer_config()?));
|
||||
Ok(())
|
||||
}())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_clear_fallback_execution_timer_config() -> RegorusResult {
|
||||
limits::set_fallback_execution_timer_config(None);
|
||||
RegorusResult::ok_void()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
optional_u64_to_result, regorus_get_global_memory_limit, regorus_set_global_memory_limit,
|
||||
};
|
||||
use crate::common::{regorus_result_drop, RegorusDataType, RegorusStatus};
|
||||
|
||||
#[test]
|
||||
fn optional_some_returns_integer() {
|
||||
let result = optional_u64_to_result(Some(123));
|
||||
assert!(result.bool_value);
|
||||
assert!(matches!(result.data_type, RegorusDataType::Integer));
|
||||
assert_eq!(result.int_value, 123);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn optional_none_returns_void() {
|
||||
let result = optional_u64_to_result(None);
|
||||
assert!(!result.bool_value);
|
||||
assert!(matches!(result.data_type, RegorusDataType::None));
|
||||
assert_eq!(result.int_value, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ffi_roundtrips_global_limit() {
|
||||
let limit = 456_u64;
|
||||
let result = regorus_set_global_memory_limit(limit, true);
|
||||
assert!(matches!(result.status, RegorusStatus::Ok));
|
||||
regorus_result_drop(result);
|
||||
|
||||
let result = regorus_get_global_memory_limit();
|
||||
assert!(matches!(result.status, RegorusStatus::Ok));
|
||||
assert!(result.bool_value);
|
||||
assert!(matches!(result.data_type, RegorusDataType::Integer));
|
||||
assert_eq!(result.int_value, 456);
|
||||
regorus_result_drop(result);
|
||||
|
||||
let result = regorus_set_global_memory_limit(0, false);
|
||||
regorus_result_drop(result);
|
||||
}
|
||||
}
|
||||
103
bindings/ffi/src/lock.rs
Normal file
103
bindings/ffi/src/lock.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
//! Abstractions over synchronization primitives used by the FFI layer.
|
||||
//!
|
||||
//! For `std` builds we rely on `parking_lot::RwLock` so we can detect
|
||||
//! contention across threads. For `no_std` builds we fall back to
|
||||
//! `RefCell`, which still lets us detect aliasing within a single thread.
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
extern crate alloc;
|
||||
|
||||
#[cfg(all(feature = "std", feature = "contention_checks"))]
|
||||
mod locking {
|
||||
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(crate) type Handle<T> = Arc<RwLock<T>>;
|
||||
pub(crate) type ReadGuard<'a, T> = RwLockReadGuard<'a, T>;
|
||||
pub(crate) type WriteGuard<'a, T> = RwLockWriteGuard<'a, T>;
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn new_handle<T>(value: T) -> Handle<T> {
|
||||
Arc::new(RwLock::new(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn try_write<'a, T>(handle: &'a Handle<T>) -> Option<WriteGuard<'a, T>> {
|
||||
handle.try_write()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn try_read<'a, T>(handle: &'a Handle<T>) -> Option<ReadGuard<'a, T>> {
|
||||
handle.try_read()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn read<'a, T>(handle: &'a Handle<T>) -> ReadGuard<'a, T> {
|
||||
handle.read()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "std", not(feature = "contention_checks")))]
|
||||
mod locking {
|
||||
use std::cell::{Ref, RefCell, RefMut};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub(crate) type Handle<T> = Rc<RefCell<T>>;
|
||||
pub(crate) type ReadGuard<'a, T> = Ref<'a, T>;
|
||||
pub(crate) type WriteGuard<'a, T> = RefMut<'a, T>;
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn new_handle<T>(value: T) -> Handle<T> {
|
||||
Rc::new(RefCell::new(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn try_write<'a, T>(handle: &'a Handle<T>) -> Option<WriteGuard<'a, T>> {
|
||||
handle.try_borrow_mut().ok()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn try_read<'a, T>(handle: &'a Handle<T>) -> Option<ReadGuard<'a, T>> {
|
||||
handle.try_borrow().ok()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn read<'a, T>(handle: &'a Handle<T>) -> ReadGuard<'a, T> {
|
||||
handle.borrow()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
mod locking {
|
||||
use alloc::rc::Rc;
|
||||
use core::cell::{Ref, RefCell, RefMut};
|
||||
|
||||
pub(crate) type Handle<T> = Rc<RefCell<T>>;
|
||||
pub(crate) type ReadGuard<'a, T> = Ref<'a, T>;
|
||||
pub(crate) type WriteGuard<'a, T> = RefMut<'a, T>;
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn new_handle<T>(value: T) -> Handle<T> {
|
||||
Rc::new(RefCell::new(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn try_write<'a, T>(handle: &'a Handle<T>) -> Option<WriteGuard<'a, T>> {
|
||||
handle.try_borrow_mut().ok()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn try_read<'a, T>(handle: &'a Handle<T>) -> Option<ReadGuard<'a, T>> {
|
||||
handle.try_borrow().ok()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn read<'a, T>(handle: &'a Handle<T>) -> ReadGuard<'a, T> {
|
||||
handle.borrow()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) use locking::{new_handle, read, try_read, try_write, Handle, ReadGuard, WriteGuard};
|
||||
159
bindings/ffi/src/panic_guard.rs
Normal file
159
bindings/ffi/src/panic_guard.rs
Normal file
@@ -0,0 +1,159 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
//! Minimal helpers for catching panics inside the FFI layer.
|
||||
//!
|
||||
//! These are not yet wired into the exported functions; they will
|
||||
//! be used once the integration work is complete.
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use crate::common::{RegorusResult, RegorusStatus};
|
||||
|
||||
use alloc::string::String;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
#[cfg(feature = "std")]
|
||||
use std::{
|
||||
backtrace::Backtrace,
|
||||
cell::RefCell,
|
||||
panic::{self, AssertUnwindSafe},
|
||||
};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
thread_local! {
|
||||
// Stashes the formatted panic + backtrace for whichever call last panicked on this thread.
|
||||
static PANIC_BACKTRACE: RefCell<Option<String>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
type PanicHook = dyn Fn(&panic::PanicHookInfo<'_>) + Sync + Send + 'static;
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
/// RAII helper that installs a per-call panic hook and restores the prior hook on drop.
|
||||
struct PanicHookGuard {
|
||||
previous: Option<Box<PanicHook>>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl PanicHookGuard {
|
||||
fn install() -> Self {
|
||||
// Remember whatever hook the embedding application already registered.
|
||||
let previous = panic::take_hook();
|
||||
PANIC_BACKTRACE.with(|slot| {
|
||||
slot.replace(None);
|
||||
});
|
||||
// Install our temporary hook so we can capture a backtrace for this invocation.
|
||||
panic::set_hook(Box::new(|info| {
|
||||
let backtrace = Backtrace::force_capture();
|
||||
PANIC_BACKTRACE.with(|slot| {
|
||||
slot.replace(Some(format!(
|
||||
"panic hook observed: {}\nbacktrace:\n{:#?}",
|
||||
info, backtrace
|
||||
)));
|
||||
});
|
||||
}));
|
||||
Self {
|
||||
previous: Some(previous),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl Drop for PanicHookGuard {
|
||||
fn drop(&mut self) {
|
||||
if let Some(previous) = self.previous.take() {
|
||||
// Restore the original panic hook before we return control to the host.
|
||||
panic::set_hook(previous);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static POISONED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// Result of attempting to run `f` while guarding against unwinding.
|
||||
pub(crate) enum GuardResult<T> {
|
||||
/// Closure completed successfully.
|
||||
Success(T),
|
||||
/// Closure panicked; contains a best-effort string payload.
|
||||
Panic(String),
|
||||
}
|
||||
|
||||
pub(crate) fn with_unwind_guard<F>(f: F) -> RegorusResult
|
||||
where
|
||||
F: FnOnce() -> RegorusResult,
|
||||
{
|
||||
if is_poisoned() {
|
||||
return poisoned_result();
|
||||
}
|
||||
|
||||
// The closure passed across this boundary closes over raw pointers and lock guards.
|
||||
// These types are not unwind safe by default and may become poisoned if a panic occurs.
|
||||
// We therefore use AssertUnwindSafe to get the compiler to accept the closure.
|
||||
// Upon unwind, we mark regorus as poisoned and disallow further use.
|
||||
#[cfg(feature = "std")]
|
||||
{
|
||||
let outcome = {
|
||||
let _hook_guard = PanicHookGuard::install();
|
||||
match panic::catch_unwind(AssertUnwindSafe(f)) {
|
||||
Ok(value) => GuardResult::Success(value),
|
||||
Err(payload) => GuardResult::Panic(panic_message_to_string(payload)),
|
||||
}
|
||||
};
|
||||
finalize(outcome)
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
return finalize(GuardResult::Success(f()));
|
||||
}
|
||||
|
||||
fn finalize(outcome: GuardResult<RegorusResult>) -> RegorusResult {
|
||||
match outcome {
|
||||
GuardResult::Success(result) => result,
|
||||
GuardResult::Panic(message) => {
|
||||
trip_poison();
|
||||
RegorusResult::err_with_message(RegorusStatus::Panic, message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn panic_message_to_string(payload: Box<dyn core::any::Any + Send + 'static>) -> String {
|
||||
let mut message = if let Some(s) = payload.downcast_ref::<&str>() {
|
||||
(*s).into()
|
||||
} else if let Some(s) = payload.downcast_ref::<String>() {
|
||||
s.clone()
|
||||
} else {
|
||||
String::from("regorus encountered panic")
|
||||
};
|
||||
|
||||
if let Some(backtrace) = take_panic_backtrace() {
|
||||
message.push('\n');
|
||||
message.push_str(&backtrace);
|
||||
}
|
||||
|
||||
message
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
fn take_panic_backtrace() -> Option<String> {
|
||||
PANIC_BACKTRACE.with(|slot| slot.borrow_mut().take())
|
||||
}
|
||||
|
||||
fn poisoned_result() -> RegorusResult {
|
||||
RegorusResult::err_with_message(
|
||||
RegorusStatus::Poisoned,
|
||||
String::from("regorus is poisoned after a previous panic"),
|
||||
)
|
||||
}
|
||||
|
||||
fn trip_poison() {
|
||||
POISONED.store(true, Ordering::Release);
|
||||
}
|
||||
|
||||
pub(crate) fn is_poisoned() -> bool {
|
||||
POISONED.load(Ordering::Acquire)
|
||||
}
|
||||
|
||||
pub(crate) fn reset_poison() {
|
||||
POISONED.store(false, Ordering::Release);
|
||||
}
|
||||
606
bindings/ffi/src/rvm.rs
Normal file
606
bindings/ffi/src/rvm.rs
Normal file
@@ -0,0 +1,606 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
use crate::common::{
|
||||
from_c_str, to_ref, to_regorus_result, RegorusBuffer, RegorusResult, RegorusStatus,
|
||||
};
|
||||
use crate::compile::RegorusPolicyModule;
|
||||
use crate::compiled_policy::RegorusCompiledPolicy;
|
||||
use crate::limits::RegorusExecutionTimerConfig;
|
||||
use crate::lock::{new_handle, try_read, try_write, Handle, ReadGuard, WriteGuard};
|
||||
use crate::panic_guard::with_unwind_guard;
|
||||
use alloc::boxed::Box;
|
||||
use alloc::format;
|
||||
use alloc::string::String;
|
||||
use alloc::sync::Arc;
|
||||
use alloc::vec::Vec;
|
||||
use anyhow::{anyhow, Result};
|
||||
use core::ffi::{c_char, c_void};
|
||||
use core::ptr;
|
||||
use regorus::languages::rego::compiler::Compiler;
|
||||
use regorus::rvm::program::{
|
||||
generate_assembly_listing, generate_tabular_assembly_listing, AssemblyListingConfig,
|
||||
DeserializationResult, Program,
|
||||
};
|
||||
use regorus::rvm::vm::{ExecutionMode, ExecutionState, RegoVM};
|
||||
use regorus::PolicyModule;
|
||||
use regorus::Value;
|
||||
|
||||
/// Wrapper for `regorus::rvm::Program`.
|
||||
#[derive(Clone)]
|
||||
pub struct RegorusProgram {
|
||||
pub(crate) program: Arc<Program>,
|
||||
}
|
||||
|
||||
/// Wrapper for `regorus::rvm::RegoVM`.
|
||||
pub struct RegorusRvm {
|
||||
vm: Handle<RegoVM>,
|
||||
}
|
||||
|
||||
impl RegorusRvm {
|
||||
fn new(vm: RegoVM) -> Self {
|
||||
Self { vm: new_handle(vm) }
|
||||
}
|
||||
|
||||
fn contention_error() -> anyhow::Error {
|
||||
anyhow!("regorus rvm handle is already in use; create a separate VM per thread")
|
||||
}
|
||||
|
||||
fn try_write(&self) -> Result<WriteGuard<'_, RegoVM>> {
|
||||
try_write(&self.vm).ok_or_else(Self::contention_error)
|
||||
}
|
||||
|
||||
fn try_read(&self) -> Result<ReadGuard<'_, RegoVM>> {
|
||||
try_read(&self.vm).ok_or_else(Self::contention_error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop a `RegorusProgram`.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_program_drop(program: *mut RegorusProgram) {
|
||||
if let Ok(program) = to_ref(program) {
|
||||
unsafe {
|
||||
let _ = Box::from_raw(ptr::from_mut(program));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop a `RegorusRvm`.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_drop(vm: *mut RegorusRvm) {
|
||||
if let Ok(vm) = to_ref(vm) {
|
||||
unsafe {
|
||||
let _ = Box::from_raw(ptr::from_mut(vm));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Compile a compiled policy into an RVM program.
|
||||
///
|
||||
/// * `compiled_policy` - Compiled policy handle
|
||||
/// * `entry_points` - Array of entry point rule paths
|
||||
/// * `entry_points_len` - Number of entry points
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_program_compile_from_policy(
|
||||
compiled_policy: *mut RegorusCompiledPolicy,
|
||||
entry_points: *const *const c_char,
|
||||
entry_points_len: usize,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<*mut RegorusProgram> {
|
||||
if entry_points.is_null() && entry_points_len > 0 {
|
||||
return Err(anyhow!("null entry_points pointer"));
|
||||
}
|
||||
|
||||
let mut entry_points_vec = Vec::with_capacity(entry_points_len);
|
||||
for i in 0..entry_points_len {
|
||||
unsafe {
|
||||
let entry_ptr = entry_points.add(i);
|
||||
if entry_ptr.is_null() {
|
||||
return Err(anyhow!("null entry point at index {i}"));
|
||||
}
|
||||
let entry = from_c_str(*entry_ptr)?;
|
||||
entry_points_vec.push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
let entry_points_ref: Vec<&str> = entry_points_vec.iter().map(|s| s.as_str()).collect();
|
||||
|
||||
let compiled_policy = &to_ref(compiled_policy)?.compiled_policy;
|
||||
let program = Compiler::compile_from_policy(compiled_policy, &entry_points_ref)?;
|
||||
Ok(Box::into_raw(Box::new(RegorusProgram { program })))
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(program) => RegorusResult::ok_pointer(program as *mut c_void),
|
||||
Err(err) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("RVM compilation failed: {err}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Compile an RVM program from data/modules and entry points.
|
||||
///
|
||||
/// * `data_json` - JSON string containing static data for policy evaluation
|
||||
/// * `modules` - Array of policy modules to compile
|
||||
/// * `modules_len` - Number of modules in the array
|
||||
/// * `entry_points` - Array of entry point rule paths
|
||||
/// * `entry_points_len` - Number of entry points
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_program_compile_from_modules(
|
||||
data_json: *const c_char,
|
||||
modules: *const RegorusPolicyModule,
|
||||
modules_len: usize,
|
||||
entry_points: *const *const c_char,
|
||||
entry_points_len: usize,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<*mut RegorusProgram> {
|
||||
if entry_points_len == 0 {
|
||||
return Err(anyhow!("entry_points must contain at least one entry"));
|
||||
}
|
||||
|
||||
let data_str = from_c_str(data_json)?;
|
||||
let data = Value::from_json_str(&data_str)?;
|
||||
let policy_modules = convert_c_modules_to_rust(modules, modules_len)?;
|
||||
|
||||
let entry_points_vec = convert_c_entry_points(entry_points, entry_points_len)?;
|
||||
let entry_points_ref: Vec<&str> = entry_points_vec.iter().map(|s| s.as_str()).collect();
|
||||
|
||||
let entry_rule = entry_points_ref
|
||||
.first()
|
||||
.ok_or_else(|| anyhow!("entry_points must contain at least one entry"))?;
|
||||
|
||||
let compiled_policy = regorus::compile_policy_with_entrypoint(
|
||||
data,
|
||||
&policy_modules,
|
||||
(*entry_rule).into(),
|
||||
)?;
|
||||
|
||||
let program = Compiler::compile_from_policy(&compiled_policy, &entry_points_ref)?;
|
||||
Ok(Box::into_raw(Box::new(RegorusProgram { program })))
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(program) => RegorusResult::ok_pointer(program as *mut c_void),
|
||||
Err(err) => RegorusResult::err_with_message(
|
||||
RegorusStatus::CompilationFailed,
|
||||
format!("RVM compilation failed: {err}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a new, empty RVM program.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_program_new() -> *mut RegorusProgram {
|
||||
let program = Program::new();
|
||||
Box::into_raw(Box::new(RegorusProgram {
|
||||
program: Arc::new(program),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Serialize a program to the binary RVM format.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_program_serialize_binary(program: *mut RegorusProgram) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<*mut RegorusBuffer> {
|
||||
let program = &to_ref(program)?.program;
|
||||
let bytes = program.serialize_binary().map_err(|e| anyhow!(e))?;
|
||||
Ok(RegorusBuffer::from_vec(bytes))
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(buffer) => RegorusResult::ok_pointer(buffer as *mut c_void),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, format!("{err}")),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Deserialize a program from the binary RVM format.
|
||||
///
|
||||
/// Returns a `RegorusProgram` handle and sets `is_partial` to true when the
|
||||
/// program requires recompilation.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_program_deserialize_binary(
|
||||
data: *const u8,
|
||||
len: usize,
|
||||
is_partial: *mut bool,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<(*mut RegorusProgram, bool)> {
|
||||
if data.is_null() && len > 0 {
|
||||
return Err(anyhow!("null data pointer"));
|
||||
}
|
||||
let data = unsafe { core::slice::from_raw_parts(data, len) };
|
||||
let (program, partial) =
|
||||
match Program::deserialize_binary(data).map_err(|e| anyhow!(e))? {
|
||||
DeserializationResult::Complete(program) => (program, false),
|
||||
DeserializationResult::Partial(program) => (program, true),
|
||||
};
|
||||
Ok((
|
||||
Box::into_raw(Box::new(RegorusProgram {
|
||||
program: Arc::new(program),
|
||||
})),
|
||||
partial,
|
||||
))
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok((program, partial)) => {
|
||||
if !is_partial.is_null() {
|
||||
unsafe {
|
||||
*is_partial = partial;
|
||||
}
|
||||
}
|
||||
RegorusResult::ok_pointer(program as *mut c_void)
|
||||
}
|
||||
Err(err) => {
|
||||
RegorusResult::err_with_message(RegorusStatus::InvalidDataFormat, err.to_string())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Generate a default assembly listing for the program.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_program_generate_listing(program: *mut RegorusProgram) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let program = &to_ref(program)?.program;
|
||||
Ok(generate_assembly_listing(
|
||||
program,
|
||||
&AssemblyListingConfig::default(),
|
||||
))
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(listing) => RegorusResult::ok_string(listing),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, format!("{err}")),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Generate a tabular assembly listing for the program.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_program_generate_tabular_listing(
|
||||
program: *mut RegorusProgram,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let program = &to_ref(program)?.program;
|
||||
Ok(generate_tabular_assembly_listing(
|
||||
program,
|
||||
&AssemblyListingConfig::default(),
|
||||
))
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(listing) => RegorusResult::ok_string(listing),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, format!("{err}")),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Construct a new RVM instance.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_new() -> *mut RegorusRvm {
|
||||
Box::into_raw(Box::new(RegorusRvm::new(RegoVM::new())))
|
||||
}
|
||||
|
||||
/// Construct a new RVM instance with a compiled policy for default rule evaluation.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_new_with_policy(
|
||||
compiled_policy: *mut RegorusCompiledPolicy,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<*mut RegorusRvm> {
|
||||
let policy = to_ref(compiled_policy)?.compiled_policy.clone();
|
||||
Ok(Box::into_raw(Box::new(RegorusRvm::new(
|
||||
RegoVM::new_with_policy(policy),
|
||||
))))
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(vm) => RegorusResult::ok_pointer(vm as *mut c_void),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, err.to_string()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Load a program into the RVM.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_load_program(
|
||||
vm: *mut RegorusRvm,
|
||||
program: *mut RegorusProgram,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
let program = to_ref(program)?.program.clone();
|
||||
guard.load_program(program);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Set the VM data document from JSON.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_set_data(vm: *mut RegorusRvm, data: *const c_char) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
let data_value = Value::from_json_str(&from_c_str(data)?)?;
|
||||
guard.set_data(data_value)?;
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Set the VM input document from JSON.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_set_input(
|
||||
vm: *mut RegorusRvm,
|
||||
input: *const c_char,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
let input_value = Value::from_json_str(&from_c_str(input)?)?;
|
||||
guard.set_input(input_value);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Set the maximum number of instructions that can execute.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_set_max_instructions(
|
||||
vm: *mut RegorusRvm,
|
||||
max_instructions: usize,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
guard.set_max_instructions(max_instructions);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Configure strict builtin error behavior.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_set_strict_builtin_errors(
|
||||
vm: *mut RegorusRvm,
|
||||
strict: bool,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
guard.set_strict_builtin_errors(strict);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Configure the execution mode (0 = run-to-completion, 1 = suspendable).
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_set_execution_mode(vm: *mut RegorusRvm, mode: u8) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
let mode = match mode {
|
||||
0 => ExecutionMode::RunToCompletion,
|
||||
1 => ExecutionMode::Suspendable,
|
||||
_ => return Err(anyhow!("invalid execution mode: {mode}")),
|
||||
};
|
||||
guard.set_execution_mode(mode);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Enable or disable step mode when running suspendable execution.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_set_step_mode(vm: *mut RegorusRvm, enabled: bool) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
guard.set_step_mode(enabled);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Configure the per-VM execution timer override.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_set_execution_timer_config(
|
||||
vm: *mut RegorusRvm,
|
||||
has_config: bool,
|
||||
config: RegorusExecutionTimerConfig,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
if has_config {
|
||||
guard.set_execution_timer_config(Some(config.to_execution_timer_config()?));
|
||||
} else {
|
||||
guard.set_execution_timer_config(None);
|
||||
}
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Execute the program's main entry point.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_execute(vm: *mut RegorusRvm) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
let result = guard.execute()?;
|
||||
result.to_json_str()
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(json) => RegorusResult::ok_string(json),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, err.to_string()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Execute a named entry point.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_execute_entry_point_by_name(
|
||||
vm: *mut RegorusRvm,
|
||||
entry_point: *const c_char,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
let name = from_c_str(entry_point)?;
|
||||
let result = guard.execute_entry_point_by_name(&name)?;
|
||||
result.to_json_str()
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(json) => RegorusResult::ok_string(json),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, err.to_string()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Execute an entry point by index.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_execute_entry_point_by_index(
|
||||
vm: *mut RegorusRvm,
|
||||
index: usize,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
let result = guard.execute_entry_point_by_index(index)?;
|
||||
result.to_json_str()
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(json) => RegorusResult::ok_string(json),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, err.to_string()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Resume execution for suspendable runs.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_resume(
|
||||
vm: *mut RegorusRvm,
|
||||
resume_value_json: *const c_char,
|
||||
has_value: bool,
|
||||
) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let vm = to_ref(vm)?;
|
||||
let mut guard = vm.try_write()?;
|
||||
let value = if has_value {
|
||||
Some(Value::from_json_str(&from_c_str(resume_value_json)?)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let result = guard.resume(value)?;
|
||||
result.to_json_str()
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(json) => RegorusResult::ok_string(json),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, err.to_string()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the current execution state of the VM.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_rvm_get_execution_state(vm: *mut RegorusRvm) -> RegorusResult {
|
||||
with_unwind_guard(|| {
|
||||
let output = || -> Result<String> {
|
||||
let vm = to_ref(vm)?;
|
||||
let guard = vm.try_read()?;
|
||||
let state: ExecutionState = guard.execution_state().clone();
|
||||
Ok(format!("{:?}", state))
|
||||
}();
|
||||
|
||||
match output {
|
||||
Ok(json) => RegorusResult::ok_string(json),
|
||||
Err(err) => RegorusResult::err_with_message(RegorusStatus::Error, err.to_string()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn convert_c_entry_points(
|
||||
entry_points: *const *const c_char,
|
||||
entry_points_len: usize,
|
||||
) -> Result<Vec<String>> {
|
||||
if entry_points.is_null() && entry_points_len > 0 {
|
||||
return Err(anyhow!("null entry_points pointer"));
|
||||
}
|
||||
|
||||
let mut entry_points_vec = Vec::with_capacity(entry_points_len);
|
||||
for i in 0..entry_points_len {
|
||||
unsafe {
|
||||
let entry_ptr = entry_points.add(i);
|
||||
if entry_ptr.is_null() {
|
||||
return Err(anyhow!("null entry point at index {i}"));
|
||||
}
|
||||
let entry = from_c_str(*entry_ptr)?;
|
||||
entry_points_vec.push(entry);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entry_points_vec)
|
||||
}
|
||||
|
||||
fn convert_c_modules_to_rust(
|
||||
modules: *const RegorusPolicyModule,
|
||||
modules_len: usize,
|
||||
) -> Result<Vec<PolicyModule>> {
|
||||
if modules.is_null() && modules_len > 0 {
|
||||
return Err(anyhow!("null modules pointer"));
|
||||
}
|
||||
|
||||
let mut policy_modules = Vec::with_capacity(modules_len);
|
||||
|
||||
for i in 0..modules_len {
|
||||
unsafe {
|
||||
let module = modules.add(i);
|
||||
if module.is_null() {
|
||||
return Err(anyhow!("null module at index {i}"));
|
||||
}
|
||||
|
||||
let module_ref = &*module;
|
||||
|
||||
let id = from_c_str(module_ref.id)
|
||||
.map_err(|e| anyhow!("invalid module id at index {i}: {e}"))?;
|
||||
let content = from_c_str(module_ref.content)
|
||||
.map_err(|e| anyhow!("invalid module content at index {i}: {e}"))?;
|
||||
|
||||
policy_modules.push(PolicyModule {
|
||||
id: id.into(),
|
||||
content: content.into(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(policy_modules)
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
#![cfg(feature = "azure_policy")]
|
||||
|
||||
use crate::common::{from_c_str, RegorusResult, RegorusStatus};
|
||||
use crate::panic_guard::with_unwind_guard;
|
||||
use regorus::{registry::schemas, Schema};
|
||||
|
||||
use std::os::raw::c_char;
|
||||
@@ -32,45 +33,45 @@ pub extern "C" fn regorus_resource_schema_register(
|
||||
name: *const c_char,
|
||||
schema_json: *const c_char,
|
||||
) -> RegorusResult {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
with_unwind_guard(|| {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let schema_str = match from_c_str(schema_json) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Invalid schema JSON string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
let schema_str = match from_c_str(schema_json) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Invalid schema JSON string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Parse schema from JSON
|
||||
let schema = match Schema::from_json_str(&schema_str) {
|
||||
Ok(schema) => schema,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Failed to parse schema JSON: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
let schema = match Schema::from_json_str(&schema_str) {
|
||||
Ok(schema) => schema,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidDataFormat,
|
||||
format!("Failed to parse schema JSON: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
// Register the schema
|
||||
match schemas::resource::register(schema_name, schema.into()) {
|
||||
Ok(()) => RegorusResult::ok_pointer(std::ptr::null_mut()),
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::Error,
|
||||
format!("Failed to register schema: {e}"),
|
||||
),
|
||||
}
|
||||
match schemas::resource::register(schema_name, schema.into()) {
|
||||
Ok(()) => RegorusResult::ok_pointer(std::ptr::null_mut()),
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::Error,
|
||||
format!("Failed to register schema: {e}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Check if a resource schema with the given name exists.
|
||||
@@ -86,18 +87,20 @@ pub extern "C" fn regorus_resource_schema_register(
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_resource_schema_contains(name: *const c_char) -> RegorusResult {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
with_unwind_guard(|| {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let contains = schemas::resource::contains(&schema_name);
|
||||
RegorusResult::ok_bool(contains)
|
||||
let contains = schemas::resource::contains(&schema_name);
|
||||
RegorusResult::ok_bool(contains)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the number of registered resource schemas.
|
||||
@@ -107,8 +110,10 @@ pub extern "C" fn regorus_resource_schema_contains(name: *const c_char) -> Regor
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_resource_schema_len() -> RegorusResult {
|
||||
let count = schemas::resource::len();
|
||||
RegorusResult::ok_int(count as i64)
|
||||
with_unwind_guard(|| {
|
||||
let count = schemas::resource::len();
|
||||
RegorusResult::ok_int(count as i64)
|
||||
})
|
||||
}
|
||||
|
||||
/// Check if the resource schema registry is empty.
|
||||
@@ -118,8 +123,10 @@ pub extern "C" fn regorus_resource_schema_len() -> RegorusResult {
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_resource_schema_is_empty() -> RegorusResult {
|
||||
let is_empty = schemas::resource::is_empty();
|
||||
RegorusResult::ok_bool(is_empty)
|
||||
with_unwind_guard(|| {
|
||||
let is_empty = schemas::resource::is_empty();
|
||||
RegorusResult::ok_bool(is_empty)
|
||||
})
|
||||
}
|
||||
|
||||
/// List all registered resource schema names as a JSON array.
|
||||
@@ -129,14 +136,16 @@ pub extern "C" fn regorus_resource_schema_is_empty() -> RegorusResult {
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_resource_schema_list_names() -> RegorusResult {
|
||||
let names = schemas::resource::list_names();
|
||||
match serde_json::to_string(&names) {
|
||||
Ok(json_str) => RegorusResult::ok_string(json_str),
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::Error,
|
||||
format!("Failed to serialize schema names to JSON: {e}"),
|
||||
),
|
||||
}
|
||||
with_unwind_guard(|| {
|
||||
let names = schemas::resource::list_names();
|
||||
match serde_json::to_string(&names) {
|
||||
Ok(json_str) => RegorusResult::ok_string(json_str),
|
||||
Err(e) => RegorusResult::err_with_message(
|
||||
RegorusStatus::Error,
|
||||
format!("Failed to serialize schema names to JSON: {e}"),
|
||||
),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Remove a resource schema by name.
|
||||
@@ -152,18 +161,20 @@ pub extern "C" fn regorus_resource_schema_list_names() -> RegorusResult {
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_resource_schema_remove(name: *const c_char) -> RegorusResult {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
with_unwind_guard(|| {
|
||||
let schema_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid schema name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let removed = schemas::resource::remove(&schema_name).is_some();
|
||||
RegorusResult::ok_bool(removed)
|
||||
let removed = schemas::resource::remove(&schema_name).is_some();
|
||||
RegorusResult::ok_bool(removed)
|
||||
})
|
||||
}
|
||||
|
||||
/// Clear all resource schemas from the registry.
|
||||
@@ -173,6 +184,8 @@ pub extern "C" fn regorus_resource_schema_remove(name: *const c_char) -> Regorus
|
||||
#[cfg(feature = "azure_policy")]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_resource_schema_clear() -> RegorusResult {
|
||||
schemas::resource::clear();
|
||||
RegorusResult::ok_pointer(std::ptr::null_mut())
|
||||
with_unwind_guard(|| {
|
||||
schemas::resource::clear();
|
||||
RegorusResult::ok_pointer(std::ptr::null_mut())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#![cfg(feature = "azure_policy")]
|
||||
|
||||
use crate::common::*;
|
||||
use crate::panic_guard::with_unwind_guard;
|
||||
use anyhow::Result;
|
||||
use std::os::raw::c_char;
|
||||
|
||||
@@ -16,12 +17,14 @@ use std::os::raw::c_char;
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "azure_policy")]
|
||||
pub extern "C" fn regorus_register_target_from_json(target_json: *const c_char) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let target_str = from_c_str(target_json)?;
|
||||
let target = regorus::Target::from_json_str(&target_str)?;
|
||||
regorus::registry::targets::register(regorus::Rc::new(target))?;
|
||||
Ok(())
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let target_str = from_c_str(target_json)?;
|
||||
let target = regorus::Target::from_json_str(&target_str)?;
|
||||
regorus::registry::targets::register(regorus::Rc::new(target))?;
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Check if a target is registered.
|
||||
@@ -36,31 +39,35 @@ pub extern "C" fn regorus_register_target_from_json(target_json: *const c_char)
|
||||
/// The name parameter must be a valid null-terminated UTF-8 string.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn regorus_target_registry_contains(name: *const c_char) -> RegorusResult {
|
||||
let target_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid target name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
with_unwind_guard(|| {
|
||||
let target_name = match from_c_str(name) {
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return RegorusResult::err_with_message(
|
||||
RegorusStatus::InvalidArgument,
|
||||
format!("Invalid target name string: {e}"),
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
let contains = regorus::registry::targets::contains(&target_name);
|
||||
RegorusResult::ok_bool(contains)
|
||||
let contains = regorus::registry::targets::contains(&target_name);
|
||||
RegorusResult::ok_bool(contains)
|
||||
})
|
||||
}
|
||||
|
||||
/// Get a list of all registered target names as JSON array.
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "azure_policy")]
|
||||
pub extern "C" fn regorus_target_registry_list_names() -> RegorusResult {
|
||||
let names = regorus::registry::targets::list_names();
|
||||
let output = serde_json::to_string_pretty(&names).map_err(anyhow::Error::msg);
|
||||
with_unwind_guard(|| {
|
||||
let names = regorus::registry::targets::list_names();
|
||||
let output = serde_json::to_string_pretty(&names).map_err(anyhow::Error::msg);
|
||||
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
match output {
|
||||
Ok(out) => RegorusResult::ok_string(out),
|
||||
Err(e) => to_regorus_result(Err(e)),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Remove a target from the registry by name.
|
||||
@@ -69,19 +76,23 @@ pub extern "C" fn regorus_target_registry_list_names() -> RegorusResult {
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "azure_policy")]
|
||||
pub extern "C" fn regorus_target_registry_remove(name: *const c_char) -> RegorusResult {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let name_str = from_c_str(name)?;
|
||||
regorus::registry::targets::remove(&name_str);
|
||||
Ok(())
|
||||
}())
|
||||
with_unwind_guard(|| {
|
||||
to_regorus_result(|| -> Result<()> {
|
||||
let name_str = from_c_str(name)?;
|
||||
regorus::registry::targets::remove(&name_str);
|
||||
Ok(())
|
||||
}())
|
||||
})
|
||||
}
|
||||
|
||||
/// Clear all targets from the registry.
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "azure_policy")]
|
||||
pub extern "C" fn regorus_target_registry_clear() -> RegorusResult {
|
||||
regorus::registry::targets::clear();
|
||||
RegorusResult::ok_void()
|
||||
with_unwind_guard(|| {
|
||||
regorus::registry::targets::clear();
|
||||
RegorusResult::ok_void()
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the number of registered targets.
|
||||
@@ -91,8 +102,10 @@ pub extern "C" fn regorus_target_registry_clear() -> RegorusResult {
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "azure_policy")]
|
||||
pub extern "C" fn regorus_target_registry_len() -> RegorusResult {
|
||||
let count = regorus::registry::targets::len();
|
||||
RegorusResult::ok_int(count as i64)
|
||||
with_unwind_guard(|| {
|
||||
let count = regorus::registry::targets::len();
|
||||
RegorusResult::ok_int(count as i64)
|
||||
})
|
||||
}
|
||||
|
||||
/// Check if the target registry is empty.
|
||||
@@ -102,6 +115,8 @@ pub extern "C" fn regorus_target_registry_len() -> RegorusResult {
|
||||
#[no_mangle]
|
||||
#[cfg(feature = "azure_policy")]
|
||||
pub extern "C" fn regorus_target_registry_is_empty() -> RegorusResult {
|
||||
let is_empty = regorus::registry::targets::is_empty();
|
||||
RegorusResult::ok_bool(is_empty)
|
||||
with_unwind_guard(|| {
|
||||
let is_empty = regorus::registry::targets::is_empty();
|
||||
RegorusResult::ok_bool(is_empty)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -100,4 +100,138 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("%s\n", output)
|
||||
|
||||
// RVM regular example (compile, serialize, execute)
|
||||
const regularPolicy = `
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.user == "alice"
|
||||
input.active == true
|
||||
}
|
||||
`
|
||||
const regularInput = `{"user":"alice","active":true}`
|
||||
|
||||
regularModules := []regorus.PolicyModule{{Id: "demo.rego", Content: regularPolicy}}
|
||||
regularEntryPoints := []string{"data.demo.allow"}
|
||||
regularProgram, err := regorus.CompileProgramFromModules("{}", regularModules, regularEntryPoints)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer regularProgram.Close()
|
||||
|
||||
listing, err := regularProgram.GenerateListing()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("RVM listing:\n%s\n", listing)
|
||||
|
||||
binary, err := regularProgram.SerializeBinary()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
regularProgram.Close()
|
||||
|
||||
rehydrated, isPartial, err := regorus.DeserializeProgram(binary)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if isPartial {
|
||||
fmt.Fprintf(os.Stderr, "error: program marked partial\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
defer rehydrated.Close()
|
||||
|
||||
regularVm, err := regorus.NewRvm()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer regularVm.Close()
|
||||
|
||||
if err := regularVm.LoadProgram(rehydrated); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := regularVm.SetInputJson(regularInput); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
regularResult, err := regularVm.Execute()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("RVM regular result: %s\n", regularResult)
|
||||
|
||||
// RVM HostAwait example
|
||||
const rvmPolicy = `
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.account.active == true
|
||||
details := __builtin_host_await(input.account.id, "account")
|
||||
details.tier == "gold"
|
||||
}
|
||||
`
|
||||
const rvmInput = `{"account":{"id":"acct-1","active":true}}`
|
||||
|
||||
modules := []regorus.PolicyModule{{Id: "demo.rego", Content: rvmPolicy}}
|
||||
entryPoints := []string{"data.demo.allow"}
|
||||
program, err := regorus.CompileProgramFromModules("{}", modules, entryPoints)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer program.Close()
|
||||
|
||||
vm, err := regorus.NewRvm()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer vm.Close()
|
||||
|
||||
if err := vm.SetExecutionMode(1); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := vm.LoadProgram(program); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if err := vm.SetInputJson(rvmInput); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if _, err := vm.Execute(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
state, err := vm.GetExecutionState()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("HostAwait state: %s\n", state)
|
||||
|
||||
result, err := vm.Resume(`{"tier":"gold"}`, true)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("HostAwait result: %s\n", result)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package regorus
|
||||
|
||||
// #cgo LDFLAGS: -L ../../../ffi/target/release -lregorus_ffi
|
||||
// #cgo LDFLAGS: -L ../../../ffi/target/release -L ../../../ffi/target/debug -lregorus_ffi
|
||||
// #include "../../../ffi/regorus.h"
|
||||
import "C"
|
||||
import (
|
||||
|
||||
283
bindings/go/pkg/regorus/rvm.go
Normal file
283
bindings/go/pkg/regorus/rvm.go
Normal file
@@ -0,0 +1,283 @@
|
||||
package regorus
|
||||
|
||||
// #cgo LDFLAGS: -L ../../../ffi/target/release -L ../../../ffi/target/debug -lregorus_ffi
|
||||
// #include "../../../ffi/regorus.h"
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type PolicyModule struct {
|
||||
Id string
|
||||
Content string
|
||||
}
|
||||
|
||||
type Program struct {
|
||||
p *C.RegorusProgram
|
||||
}
|
||||
|
||||
type Rvm struct {
|
||||
vm *C.RegorusRvm
|
||||
}
|
||||
|
||||
type Buffer struct {
|
||||
b *C.RegorusBuffer
|
||||
}
|
||||
|
||||
func (b *Buffer) Close() {
|
||||
if b != nil && b.b != nil {
|
||||
C.regorus_buffer_drop(b.b)
|
||||
b.b = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Buffer) Bytes() []byte {
|
||||
if b == nil || b.b == nil || b.b.data == nil || b.b.len == 0 {
|
||||
return nil
|
||||
}
|
||||
return C.GoBytes(unsafe.Pointer(b.b.data), C.int(b.b.len))
|
||||
}
|
||||
|
||||
func (p *Program) Close() {
|
||||
if p != nil && p.p != nil {
|
||||
C.regorus_program_drop(p.p)
|
||||
p.p = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Program) SerializeBinary() ([]byte, error) {
|
||||
result := C.regorus_program_serialize_binary(p.p)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return nil, fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
buffer := &Buffer{b: (*C.RegorusBuffer)(result.pointer_value)}
|
||||
defer buffer.Close()
|
||||
return buffer.Bytes(), nil
|
||||
}
|
||||
|
||||
func (p *Program) GenerateListing() (string, error) {
|
||||
result := C.regorus_program_generate_listing(p.p)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return C.GoString(result.output), nil
|
||||
}
|
||||
|
||||
func (p *Program) GenerateTabularListing() (string, error) {
|
||||
result := C.regorus_program_generate_tabular_listing(p.p)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return C.GoString(result.output), nil
|
||||
}
|
||||
|
||||
func DeserializeProgram(data []byte) (*Program, bool, error) {
|
||||
if len(data) == 0 {
|
||||
return nil, false, fmt.Errorf("empty program data")
|
||||
}
|
||||
var isPartial C.bool
|
||||
result := C.regorus_program_deserialize_binary((*C.uchar)(unsafe.Pointer(&data[0])), C.ulong(len(data)), (*C.bool)(unsafe.Pointer(&isPartial)))
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return nil, false, fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return &Program{p: (*C.RegorusProgram)(result.pointer_value)}, bool(isPartial), nil
|
||||
}
|
||||
|
||||
func CompileProgramFromModules(data string, modules []PolicyModule, entryPoints []string) (*Program, error) {
|
||||
dataC := C.CString(data)
|
||||
defer C.free(unsafe.Pointer(dataC))
|
||||
|
||||
cModules := make([]C.RegorusPolicyModule, len(modules))
|
||||
moduleIdPtrs := make([]*C.char, len(modules))
|
||||
moduleContentPtrs := make([]*C.char, len(modules))
|
||||
for i, module := range modules {
|
||||
idC := C.CString(module.Id)
|
||||
contentC := C.CString(module.Content)
|
||||
moduleIdPtrs[i] = idC
|
||||
moduleContentPtrs[i] = contentC
|
||||
cModules[i].id = idC
|
||||
cModules[i].content = contentC
|
||||
}
|
||||
defer func() {
|
||||
for i := range moduleIdPtrs {
|
||||
if moduleIdPtrs[i] != nil {
|
||||
C.free(unsafe.Pointer(moduleIdPtrs[i]))
|
||||
}
|
||||
if moduleContentPtrs[i] != nil {
|
||||
C.free(unsafe.Pointer(moduleContentPtrs[i]))
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
entryPtrs := make([]*C.char, len(entryPoints))
|
||||
for i, entry := range entryPoints {
|
||||
entryPtrs[i] = C.CString(entry)
|
||||
}
|
||||
defer func() {
|
||||
for _, ptr := range entryPtrs {
|
||||
C.free(unsafe.Pointer(ptr))
|
||||
}
|
||||
}()
|
||||
|
||||
var modulesPtr *C.RegorusPolicyModule
|
||||
if len(cModules) > 0 {
|
||||
modulesPtr = (*C.RegorusPolicyModule)(unsafe.Pointer(&cModules[0]))
|
||||
}
|
||||
var entryPtr **C.char
|
||||
if len(entryPtrs) > 0 {
|
||||
entryPtr = (**C.char)(unsafe.Pointer(&entryPtrs[0]))
|
||||
}
|
||||
|
||||
result := C.regorus_program_compile_from_modules(
|
||||
dataC,
|
||||
modulesPtr,
|
||||
C.ulong(len(cModules)),
|
||||
entryPtr,
|
||||
C.ulong(len(entryPtrs)),
|
||||
)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return nil, fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return &Program{p: (*C.RegorusProgram)(result.pointer_value)}, nil
|
||||
}
|
||||
|
||||
func CompileProgramFromEngine(engine *Engine, entryPoints []string) (*Program, error) {
|
||||
entryPtrs := make([]*C.char, len(entryPoints))
|
||||
for i, entry := range entryPoints {
|
||||
entryPtrs[i] = C.CString(entry)
|
||||
}
|
||||
defer func() {
|
||||
for _, ptr := range entryPtrs {
|
||||
C.free(unsafe.Pointer(ptr))
|
||||
}
|
||||
}()
|
||||
|
||||
var entryPtr **C.char
|
||||
if len(entryPtrs) > 0 {
|
||||
entryPtr = (**C.char)(unsafe.Pointer(&entryPtrs[0]))
|
||||
}
|
||||
|
||||
result := C.regorus_engine_compile_program_with_entrypoints(
|
||||
engine.e,
|
||||
entryPtr,
|
||||
C.ulong(len(entryPtrs)),
|
||||
)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return nil, fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return &Program{p: (*C.RegorusProgram)(result.pointer_value)}, nil
|
||||
}
|
||||
|
||||
func NewRvm() (*Rvm, error) {
|
||||
vm := C.regorus_rvm_new()
|
||||
if vm == nil {
|
||||
return nil, fmt.Errorf("failed to create RVM")
|
||||
}
|
||||
return &Rvm{vm: vm}, nil
|
||||
}
|
||||
|
||||
func (r *Rvm) Close() {
|
||||
if r != nil && r.vm != nil {
|
||||
C.regorus_rvm_drop(r.vm)
|
||||
r.vm = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Rvm) LoadProgram(program *Program) error {
|
||||
result := C.regorus_rvm_load_program(r.vm, program.p)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Rvm) SetDataJson(data string) error {
|
||||
dataC := C.CString(data)
|
||||
defer C.free(unsafe.Pointer(dataC))
|
||||
result := C.regorus_rvm_set_data(r.vm, dataC)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Rvm) SetInputJson(input string) error {
|
||||
inputC := C.CString(input)
|
||||
defer C.free(unsafe.Pointer(inputC))
|
||||
result := C.regorus_rvm_set_input(r.vm, inputC)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Rvm) SetExecutionMode(mode byte) error {
|
||||
result := C.regorus_rvm_set_execution_mode(r.vm, C.uchar(mode))
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Rvm) Execute() (string, error) {
|
||||
result := C.regorus_rvm_execute(r.vm)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return C.GoString(result.output), nil
|
||||
}
|
||||
|
||||
func (r *Rvm) ExecuteEntryPoint(name string) (string, error) {
|
||||
nameC := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(nameC))
|
||||
result := C.regorus_rvm_execute_entry_point_by_name(r.vm, nameC)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return C.GoString(result.output), nil
|
||||
}
|
||||
|
||||
func (r *Rvm) ExecuteEntryPointIndex(index uint64) (string, error) {
|
||||
result := C.regorus_rvm_execute_entry_point_by_index(r.vm, C.ulong(index))
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return C.GoString(result.output), nil
|
||||
}
|
||||
|
||||
func (r *Rvm) Resume(resumeValue string, hasValue bool) (string, error) {
|
||||
var valueC *C.char
|
||||
if hasValue {
|
||||
valueC = C.CString(resumeValue)
|
||||
defer C.free(unsafe.Pointer(valueC))
|
||||
}
|
||||
result := C.regorus_rvm_resume(r.vm, valueC, C.bool(hasValue))
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return C.GoString(result.output), nil
|
||||
}
|
||||
|
||||
func (r *Rvm) GetExecutionState() (string, error) {
|
||||
result := C.regorus_rvm_get_execution_state(r.vm)
|
||||
defer C.regorus_result_drop(result)
|
||||
if result.status != C.Ok {
|
||||
return "", fmt.Errorf("%s", C.GoString(result.error_message))
|
||||
}
|
||||
return C.GoString(result.output), nil
|
||||
}
|
||||
128
bindings/go/pkg/regorus/rvm_test.go
Normal file
128
bindings/go/pkg/regorus/rvm_test.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package regorus
|
||||
|
||||
import "testing"
|
||||
|
||||
const rvmPolicy = `
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.account.active == true
|
||||
details := __builtin_host_await(input.account.id, "account")
|
||||
details.tier == "gold"
|
||||
}
|
||||
`
|
||||
|
||||
const rvmInput = `{"account":{"id":"acct-1","active":true}}`
|
||||
|
||||
const rvmRegularPolicy = `
|
||||
package demo
|
||||
import rego.v1
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if {
|
||||
input.user == "alice"
|
||||
input.active == true
|
||||
}
|
||||
`
|
||||
|
||||
const rvmRegularInput = `{"user":"alice","active":true}`
|
||||
|
||||
func TestRvmProgramCompileAndExecute(t *testing.T) {
|
||||
modules := []PolicyModule{{Id: "demo.rego", Content: rvmRegularPolicy}}
|
||||
entryPoints := []string{"data.demo.allow"}
|
||||
program, err := CompileProgramFromModules("{}", modules, entryPoints)
|
||||
if err != nil {
|
||||
t.Fatalf("compile program: %v", err)
|
||||
}
|
||||
defer program.Close()
|
||||
|
||||
listing, err := program.GenerateListing()
|
||||
if err != nil || listing == "" {
|
||||
t.Fatalf("listing failed: %v", err)
|
||||
}
|
||||
|
||||
binary, err := program.SerializeBinary()
|
||||
if err != nil {
|
||||
t.Fatalf("serialize program: %v", err)
|
||||
}
|
||||
|
||||
rehydrated, isPartial, err := DeserializeProgram(binary)
|
||||
if err != nil {
|
||||
t.Fatalf("deserialize program: %v", err)
|
||||
}
|
||||
if isPartial {
|
||||
t.Fatalf("deserialized program marked partial")
|
||||
}
|
||||
defer rehydrated.Close()
|
||||
|
||||
vm, err := NewRvm()
|
||||
if err != nil {
|
||||
t.Fatalf("new vm: %v", err)
|
||||
}
|
||||
defer vm.Close()
|
||||
|
||||
if err := vm.LoadProgram(rehydrated); err != nil {
|
||||
t.Fatalf("load program: %v", err)
|
||||
}
|
||||
if err := vm.SetInputJson(rvmRegularInput); err != nil {
|
||||
t.Fatalf("set input: %v", err)
|
||||
}
|
||||
|
||||
result, err := vm.Execute()
|
||||
if err != nil {
|
||||
t.Fatalf("execute: %v", err)
|
||||
}
|
||||
if result != "true" {
|
||||
t.Fatalf("expected allow=true, got %s", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRvmHostAwaitSuspendResume(t *testing.T) {
|
||||
modules := []PolicyModule{{Id: "host_await.rego", Content: rvmPolicy}}
|
||||
entryPoints := []string{"data.demo.allow"}
|
||||
program, err := CompileProgramFromModules("{}", modules, entryPoints)
|
||||
if err != nil {
|
||||
t.Fatalf("compile program: %v", err)
|
||||
}
|
||||
defer program.Close()
|
||||
|
||||
vm, err := NewRvm()
|
||||
if err != nil {
|
||||
t.Fatalf("new vm: %v", err)
|
||||
}
|
||||
defer vm.Close()
|
||||
|
||||
if err := vm.SetExecutionMode(1); err != nil {
|
||||
t.Fatalf("set execution mode: %v", err)
|
||||
}
|
||||
if err := vm.LoadProgram(program); err != nil {
|
||||
t.Fatalf("load program: %v", err)
|
||||
}
|
||||
if err := vm.SetInputJson(rvmInput); err != nil {
|
||||
t.Fatalf("set input: %v", err)
|
||||
}
|
||||
|
||||
if _, err := vm.Execute(); err != nil {
|
||||
t.Fatalf("execute in suspendable mode failed: %v", err)
|
||||
}
|
||||
|
||||
state, err := vm.GetExecutionState()
|
||||
if err != nil {
|
||||
t.Fatalf("get execution state: %v", err)
|
||||
}
|
||||
if state == "" {
|
||||
t.Fatalf("expected non-empty execution state")
|
||||
}
|
||||
|
||||
result, err := vm.Resume(`{"tier":"gold"}`, true)
|
||||
if err != nil {
|
||||
t.Fatalf("resume: %v", err)
|
||||
}
|
||||
if result != "true" {
|
||||
t.Fatalf("expected allow=true, got %s", result)
|
||||
}
|
||||
}
|
||||
BIN
bindings/go/regorus_test
Executable file
BIN
bindings/go/regorus_test
Executable file
Binary file not shown.
628
bindings/java/Cargo.lock
generated
628
bindings/java/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,7 @@
|
||||
|
||||
[package]
|
||||
name = "regorus-java"
|
||||
version = "0.5.0"
|
||||
version = "0.9.0"
|
||||
edition = "2021"
|
||||
repository = "https://github.com/microsoft/regorus/bindings/java"
|
||||
description = "Java bindings for Regorus - a fast, lightweight Rego interpreter written in Rust"
|
||||
@@ -21,4 +21,4 @@ ast = ["regorus/ast"]
|
||||
anyhow = "1.0"
|
||||
serde_json = "1.0.112"
|
||||
jni = "0.21.1"
|
||||
regorus = { path = "../..", default-features = false, features = ["arc"] }
|
||||
regorus = { path = "../..", default-features = false, features = ["arc", "rvm"] }
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Regorus Java
|
||||
|
||||
<!-- Trivial change to trigger version bump test -->
|
||||
**Regorus** is
|
||||
|
||||
- *Rego*-*Rus(t)* - A fast, light-weight [Rego](https://www.openpolicyagent.org/docs/latest/policy-language/)
|
||||
@@ -32,6 +32,13 @@ $ mvn package
|
||||
|
||||
And you will have a JAR at `./target/regorus-java-0.1.5.jar`.
|
||||
|
||||
### Automation
|
||||
|
||||
The repository exposes helper commands for local workflows:
|
||||
|
||||
- `cargo xtask build-java` runs `mvn package` with quiet output helpers.
|
||||
- `cargo xtask test-java` rebuilds the native library via the Maven exec plugin and executes the binding tests.
|
||||
|
||||
## Usage
|
||||
|
||||
You can use Regorus Java bindings as:
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
// Licensed under the MIT License.
|
||||
|
||||
import com.microsoft.regorus.Engine;
|
||||
import com.microsoft.regorus.PolicyModule;
|
||||
import com.microsoft.regorus.Program;
|
||||
import com.microsoft.regorus.Rvm;
|
||||
|
||||
public class Test {
|
||||
|
||||
@@ -44,5 +47,70 @@ public class Test {
|
||||
"package world\nx { true }"
|
||||
);
|
||||
}
|
||||
|
||||
String regularPolicy = String.join("\n",
|
||||
"package demo",
|
||||
"import rego.v1",
|
||||
"",
|
||||
"default allow := false",
|
||||
"",
|
||||
"allow if {",
|
||||
" input.user == \"alice\"",
|
||||
" input.active == true",
|
||||
"}"
|
||||
);
|
||||
String regularInput = "{\"user\":\"alice\",\"active\":true}";
|
||||
|
||||
{
|
||||
PolicyModule module = new PolicyModule("demo.rego", regularPolicy);
|
||||
Program program = Program.compileFromModules("{}", new PolicyModule[]{module}, new String[]{"data.demo.allow"});
|
||||
System.out.println("RVM listing:\n" + program.generateListing());
|
||||
|
||||
byte[] binary = program.serializeBinary();
|
||||
program.close();
|
||||
|
||||
boolean[] isPartial = new boolean[1];
|
||||
Program rehydrated = Program.deserializeBinary(binary, isPartial);
|
||||
if (isPartial[0]) {
|
||||
throw new IllegalStateException("Deserialized program marked partial");
|
||||
}
|
||||
|
||||
try (Rvm vm = new Rvm()) {
|
||||
vm.loadProgram(rehydrated);
|
||||
vm.setInputJson(regularInput);
|
||||
String result = vm.execute();
|
||||
System.out.println("RVM regular result: " + result);
|
||||
}
|
||||
rehydrated.close();
|
||||
}
|
||||
|
||||
String awaitPolicy = String.join("\n",
|
||||
"package demo",
|
||||
"import rego.v1",
|
||||
"",
|
||||
"default allow := false",
|
||||
"",
|
||||
"allow if {",
|
||||
" input.account.active == true",
|
||||
" details := __builtin_host_await(input.account.id, \"account\")",
|
||||
" details.tier == \"gold\"",
|
||||
"}"
|
||||
);
|
||||
String awaitInput = "{\"account\":{\"id\":\"acct-1\",\"active\":true}}";
|
||||
|
||||
{
|
||||
PolicyModule module = new PolicyModule("await.rego", awaitPolicy);
|
||||
Program program = Program.compileFromModules("{}", new PolicyModule[]{module}, new String[]{"data.demo.allow"});
|
||||
try (Rvm vm = new Rvm()) {
|
||||
vm.setExecutionMode((byte) 1);
|
||||
vm.loadProgram(program);
|
||||
vm.setInputJson(awaitInput);
|
||||
vm.execute();
|
||||
System.out.println("HostAwait state: " + vm.getExecutionState());
|
||||
String resumed = vm.resume("{\"tier\":\"gold\"}");
|
||||
System.out.println("HostAwait result: " + resumed);
|
||||
}
|
||||
program.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
<groupId>com.microsoft.regorus</groupId>
|
||||
<artifactId>regorus-java</artifactId>
|
||||
<version>0.2.2</version>
|
||||
<version>0.9.0</version>
|
||||
|
||||
<name>Regorus Java</name>
|
||||
<description>Java bindings for Regorus - a fast, lightweight Rego interpreter written in Rust</description>
|
||||
|
||||
@@ -2,11 +2,17 @@
|
||||
// Licensed under the MIT License.
|
||||
|
||||
use anyhow::Result;
|
||||
use jni::objects::{JClass, JObject, JString};
|
||||
use jni::sys::{jlong, jstring};
|
||||
use jni::objects::{JBooleanArray, JByteArray, JClass, JObject, JObjectArray, JString};
|
||||
use jni::sys::{jboolean, jbooleanArray, jbyteArray, jlong, jobjectArray, jstring};
|
||||
use jni::JNIEnv;
|
||||
|
||||
use regorus::{Engine, Value};
|
||||
use regorus::languages::rego::compiler::Compiler;
|
||||
use regorus::rvm::program::{
|
||||
generate_assembly_listing, AssemblyListingConfig, DeserializationResult, Program as RvmProgram,
|
||||
};
|
||||
use regorus::rvm::vm::{ExecutionMode, RegoVM};
|
||||
use regorus::{compile_policy_with_entrypoint, Engine, PolicyModule, Rc, Value};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Engine_nativeNewEngine(
|
||||
@@ -370,6 +376,336 @@ pub extern "system" fn Java_com_microsoft_regorus_Engine_nativeDestroyEngine(
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Program_nativeCompileFromModules(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
data_json: JString,
|
||||
module_ids: jobjectArray,
|
||||
module_contents: jobjectArray,
|
||||
entry_points: jobjectArray,
|
||||
) -> jlong {
|
||||
let res = throw_err(env, |env| {
|
||||
let data_json: String = env.get_string(&data_json)?.into();
|
||||
let data = Value::from_json_str(&data_json)?;
|
||||
|
||||
let ids = get_string_array(env, module_ids)?;
|
||||
let contents = get_string_array(env, module_contents)?;
|
||||
if ids.len() != contents.len() {
|
||||
return Err(anyhow::anyhow!("module id/content length mismatch"));
|
||||
}
|
||||
|
||||
let mut modules = Vec::with_capacity(ids.len());
|
||||
for (id, content) in ids.into_iter().zip(contents.into_iter()) {
|
||||
modules.push(PolicyModule {
|
||||
id: Rc::from(id.as_str()),
|
||||
content: Rc::from(content.as_str()),
|
||||
});
|
||||
}
|
||||
|
||||
let entry_points_vec = get_string_array(env, entry_points)?;
|
||||
if entry_points_vec.is_empty() {
|
||||
return Err(anyhow::anyhow!(
|
||||
"entry_points must contain at least one entry"
|
||||
));
|
||||
}
|
||||
let entry_points_ref: Vec<&str> = entry_points_vec.iter().map(|s| s.as_str()).collect();
|
||||
let entry_rule = entry_points_ref[0];
|
||||
|
||||
let compiled = compile_policy_with_entrypoint(data, &modules, Rc::from(entry_rule))?;
|
||||
let program = Compiler::compile_from_policy(&compiled, &entry_points_ref)?;
|
||||
Ok(Box::into_raw(Box::new(program)) as jlong)
|
||||
});
|
||||
|
||||
res.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Program_nativeCompileFromEngine(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
engine_ptr: jlong,
|
||||
entry_points: jobjectArray,
|
||||
) -> jlong {
|
||||
let res = throw_err(env, |env| {
|
||||
let engine = unsafe { &mut *(engine_ptr as *mut Engine) };
|
||||
let entry_points_vec = get_string_array(env, entry_points)?;
|
||||
if entry_points_vec.is_empty() {
|
||||
return Err(anyhow::anyhow!(
|
||||
"entry_points must contain at least one entry"
|
||||
));
|
||||
}
|
||||
let entry_points_ref: Vec<&str> = entry_points_vec.iter().map(|s| s.as_str()).collect();
|
||||
let entry_rule = Rc::from(entry_points_ref[0]);
|
||||
let compiled = engine.compile_with_entrypoint(&entry_rule)?;
|
||||
let program = Compiler::compile_from_policy(&compiled, &entry_points_ref)?;
|
||||
Ok(Box::into_raw(Box::new(program)) as jlong)
|
||||
});
|
||||
|
||||
res.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Program_nativeGenerateListing(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
program_ptr: jlong,
|
||||
) -> jstring {
|
||||
let res = throw_err(env, |env| {
|
||||
let program = unsafe { &*(program_ptr as *mut Arc<RvmProgram>) };
|
||||
let listing =
|
||||
generate_assembly_listing(program.as_ref(), &AssemblyListingConfig::default());
|
||||
let output = env.new_string(&listing)?;
|
||||
Ok(output.into_raw())
|
||||
});
|
||||
|
||||
match res {
|
||||
Ok(val) => val,
|
||||
Err(_) => JObject::null().into_raw(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Program_nativeSerializeBinary(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
program_ptr: jlong,
|
||||
) -> jbyteArray {
|
||||
let res = throw_err(env, |env| {
|
||||
let program = unsafe { &*(program_ptr as *mut Arc<RvmProgram>) };
|
||||
let bytes = program.serialize_binary().map_err(|e| anyhow::anyhow!(e))?;
|
||||
let array = env.byte_array_from_slice(&bytes)?;
|
||||
Ok(array.into_raw())
|
||||
});
|
||||
|
||||
match res {
|
||||
Ok(val) => val,
|
||||
Err(_) => JObject::null().into_raw(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// # Safety
|
||||
///
|
||||
/// The `data` and `is_partial` pointers must be valid JNI array references
|
||||
/// for the duration of the call. They must come from the JVM for the current
|
||||
/// thread and not be used after this function returns.
|
||||
pub unsafe extern "system" fn Java_com_microsoft_regorus_Program_nativeDeserializeBinary(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
data: jbyteArray,
|
||||
is_partial: jbooleanArray,
|
||||
) -> jlong {
|
||||
let res = throw_err(env, |env| {
|
||||
if data.is_null() {
|
||||
return Err(anyhow::anyhow!("data must not be null"));
|
||||
}
|
||||
|
||||
let data = unsafe { JByteArray::from_raw(data) };
|
||||
let bytes = env.convert_byte_array(&data)?;
|
||||
let (program, partial) =
|
||||
match RvmProgram::deserialize_binary(&bytes).map_err(|e| anyhow::anyhow!(e))? {
|
||||
DeserializationResult::Complete(program) => (program, false),
|
||||
DeserializationResult::Partial(program) => (program, true),
|
||||
};
|
||||
|
||||
if !is_partial.is_null() {
|
||||
let is_partial = unsafe { JBooleanArray::from_raw(is_partial) };
|
||||
let len = env.get_array_length(&is_partial)?;
|
||||
if len > 0 {
|
||||
let value: [jboolean; 1] = [if partial { 1 } else { 0 }];
|
||||
env.set_boolean_array_region(&is_partial, 0, &value)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Box::into_raw(Box::new(Arc::new(program))) as jlong)
|
||||
});
|
||||
|
||||
res.unwrap_or_default()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Program_nativeDrop(
|
||||
_env: JNIEnv,
|
||||
_class: JClass,
|
||||
program_ptr: jlong,
|
||||
) {
|
||||
unsafe {
|
||||
let _program = Box::from_raw(program_ptr as *mut Arc<RvmProgram>);
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeNew(
|
||||
_env: JNIEnv,
|
||||
_class: JClass,
|
||||
) -> jlong {
|
||||
let vm = RegoVM::new();
|
||||
Box::into_raw(Box::new(vm)) as jlong
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeLoadProgram(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
vm_ptr: jlong,
|
||||
program_ptr: jlong,
|
||||
) {
|
||||
let _ = throw_err(env, |_env| {
|
||||
let vm = unsafe { &mut *(vm_ptr as *mut RegoVM) };
|
||||
let program = unsafe { &*(program_ptr as *mut Arc<RvmProgram>) };
|
||||
vm.load_program(program.clone());
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeSetDataJson(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
vm_ptr: jlong,
|
||||
data_json: JString,
|
||||
) {
|
||||
let _ = throw_err(env, |env| {
|
||||
let vm = unsafe { &mut *(vm_ptr as *mut RegoVM) };
|
||||
let data_json: String = env.get_string(&data_json)?.into();
|
||||
let data = Value::from_json_str(&data_json)?;
|
||||
vm.set_data(data)?;
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeSetInputJson(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
vm_ptr: jlong,
|
||||
input_json: JString,
|
||||
) {
|
||||
let _ = throw_err(env, |env| {
|
||||
let vm = unsafe { &mut *(vm_ptr as *mut RegoVM) };
|
||||
let input_json: String = env.get_string(&input_json)?.into();
|
||||
let input = Value::from_json_str(&input_json)?;
|
||||
vm.set_input(input);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeSetExecutionMode(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
vm_ptr: jlong,
|
||||
mode: u8,
|
||||
) {
|
||||
let _ = throw_err(env, |_env| {
|
||||
let vm = unsafe { &mut *(vm_ptr as *mut RegoVM) };
|
||||
let mode = match mode {
|
||||
0 => ExecutionMode::RunToCompletion,
|
||||
1 => ExecutionMode::Suspendable,
|
||||
_ => return Err(anyhow::anyhow!("invalid execution mode")),
|
||||
};
|
||||
vm.set_execution_mode(mode);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeExecute(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
vm_ptr: jlong,
|
||||
) -> jstring {
|
||||
let res = throw_err(env, |env| {
|
||||
let vm = unsafe { &mut *(vm_ptr as *mut RegoVM) };
|
||||
let result = vm.execute()?;
|
||||
let output = env.new_string(result.to_json_str()?)?;
|
||||
Ok(output.into_raw())
|
||||
});
|
||||
|
||||
match res {
|
||||
Ok(val) => val,
|
||||
Err(_) => JObject::null().into_raw(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeExecuteEntryPoint(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
vm_ptr: jlong,
|
||||
entry_point: JString,
|
||||
) -> jstring {
|
||||
let res = throw_err(env, |env| {
|
||||
let vm = unsafe { &mut *(vm_ptr as *mut RegoVM) };
|
||||
let entry_point: String = env.get_string(&entry_point)?.into();
|
||||
let result = vm.execute_entry_point_by_name(&entry_point)?;
|
||||
let output = env.new_string(result.to_json_str()?)?;
|
||||
Ok(output.into_raw())
|
||||
});
|
||||
|
||||
match res {
|
||||
Ok(val) => val,
|
||||
Err(_) => JObject::null().into_raw(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeResume(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
vm_ptr: jlong,
|
||||
resume_json: JString,
|
||||
has_value: bool,
|
||||
) -> jstring {
|
||||
let res = throw_err(env, |env| {
|
||||
let vm = unsafe { &mut *(vm_ptr as *mut RegoVM) };
|
||||
let value = if has_value {
|
||||
let resume_json: String = env.get_string(&resume_json)?.into();
|
||||
Some(Value::from_json_str(&resume_json)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let result = vm.resume(value)?;
|
||||
let output = env.new_string(result.to_json_str()?)?;
|
||||
Ok(output.into_raw())
|
||||
});
|
||||
|
||||
match res {
|
||||
Ok(val) => val,
|
||||
Err(_) => JObject::null().into_raw(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeGetExecutionState(
|
||||
env: JNIEnv,
|
||||
_class: JClass,
|
||||
vm_ptr: jlong,
|
||||
) -> jstring {
|
||||
let res = throw_err(env, |env| {
|
||||
let vm = unsafe { &mut *(vm_ptr as *mut RegoVM) };
|
||||
let output = env.new_string(format!("{:?}", vm.execution_state()))?;
|
||||
Ok(output.into_raw())
|
||||
});
|
||||
|
||||
match res {
|
||||
Ok(val) => val,
|
||||
Err(_) => JObject::null().into_raw(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "system" fn Java_com_microsoft_regorus_Rvm_nativeDrop(
|
||||
_env: JNIEnv,
|
||||
_class: JClass,
|
||||
vm_ptr: jlong,
|
||||
) {
|
||||
unsafe {
|
||||
let _vm = Box::from_raw(vm_ptr as *mut RegoVM);
|
||||
}
|
||||
}
|
||||
|
||||
fn throw_err<T>(mut env: JNIEnv, mut f: impl FnMut(&mut JNIEnv) -> Result<T>) -> Result<T> {
|
||||
match f(&mut env) {
|
||||
Ok(val) => Ok(val),
|
||||
@@ -379,3 +715,19 @@ fn throw_err<T>(mut env: JNIEnv, mut f: impl FnMut(&mut JNIEnv) -> Result<T>) ->
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_string_array(env: &mut JNIEnv, array: jobjectArray) -> Result<Vec<String>> {
|
||||
if array.is_null() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let array = unsafe { JObjectArray::from_raw(array) };
|
||||
let len = env.get_array_length(&array)?;
|
||||
let mut values = Vec::with_capacity(len as usize);
|
||||
for i in 0..len {
|
||||
let obj = env.get_object_array_element(&array, i)?;
|
||||
let jstr = JString::from(obj);
|
||||
let value: String = env.get_string(&jstr)?.into();
|
||||
values.push(value);
|
||||
}
|
||||
Ok(values)
|
||||
}
|
||||
|
||||
@@ -221,6 +221,8 @@ public class Engine implements AutoCloseable, Cloneable {
|
||||
|
||||
/**
|
||||
* Get coverage report as json string.
|
||||
*
|
||||
* @return Coverage report as a JSON string.
|
||||
*
|
||||
*/
|
||||
public String getCoverageReport() {
|
||||
@@ -229,6 +231,8 @@ public class Engine implements AutoCloseable, Cloneable {
|
||||
|
||||
/**
|
||||
* Get coverage report as ANSI color coded string.
|
||||
*
|
||||
* @return Coverage report formatted for console output.
|
||||
*
|
||||
*/
|
||||
public String getCoverageReportPretty() {
|
||||
@@ -247,12 +251,18 @@ public class Engine implements AutoCloseable, Cloneable {
|
||||
|
||||
/**
|
||||
* Take gathered prints.
|
||||
*
|
||||
* @return Collected print output as JSON.
|
||||
*
|
||||
*/
|
||||
public String takePrints() {
|
||||
return nativeTakePrints(enginePtr);
|
||||
}
|
||||
|
||||
long getPtr() {
|
||||
return enginePtr;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
**/
|
||||
|
||||
package com.microsoft.regorus;
|
||||
|
||||
/**
|
||||
* Represents a Rego module used for RVM program compilation.
|
||||
*/
|
||||
public final class PolicyModule {
|
||||
/**
|
||||
* Module identifier or filename.
|
||||
*/
|
||||
public final String id;
|
||||
|
||||
/**
|
||||
* Rego policy content.
|
||||
*/
|
||||
public final String content;
|
||||
|
||||
/**
|
||||
* Create a new policy module.
|
||||
*
|
||||
* @param id Module identifier or filename.
|
||||
* @param content Rego policy content.
|
||||
*/
|
||||
public PolicyModule(String id, String content) {
|
||||
this.id = id;
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
102
bindings/java/src/main/java/com/microsoft/regorus/Program.java
Normal file
102
bindings/java/src/main/java/com/microsoft/regorus/Program.java
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
**/
|
||||
|
||||
package com.microsoft.regorus;
|
||||
|
||||
/**
|
||||
* Represents a compiled RVM program.
|
||||
*/
|
||||
public final class Program implements AutoCloseable {
|
||||
private static native long nativeCompileFromModules(
|
||||
String dataJson,
|
||||
String[] moduleIds,
|
||||
String[] moduleContents,
|
||||
String[] entryPoints);
|
||||
|
||||
private static native long nativeCompileFromEngine(long enginePtr, String[] entryPoints);
|
||||
private static native String nativeGenerateListing(long programPtr);
|
||||
private static native byte[] nativeSerializeBinary(long programPtr);
|
||||
private static native long nativeDeserializeBinary(byte[] data, boolean[] isPartial);
|
||||
private static native void nativeDrop(long programPtr);
|
||||
|
||||
private final long programPtr;
|
||||
|
||||
Program(long ptr) {
|
||||
this.programPtr = ptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile a program from modules and entry points.
|
||||
*
|
||||
* @param dataJson JSON document to merge as static data.
|
||||
* @param modules Policy modules to compile.
|
||||
* @param entryPoints Entry point rule paths.
|
||||
* @return Compiled program instance.
|
||||
*/
|
||||
public static Program compileFromModules(String dataJson, PolicyModule[] modules, String[] entryPoints) {
|
||||
String[] ids = new String[modules.length];
|
||||
String[] contents = new String[modules.length];
|
||||
for (int i = 0; i < modules.length; i++) {
|
||||
ids[i] = modules[i].id;
|
||||
contents[i] = modules[i].content;
|
||||
}
|
||||
long ptr = nativeCompileFromModules(dataJson, ids, contents, entryPoints);
|
||||
return new Program(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile a program from an engine and entry points.
|
||||
*
|
||||
* @param engine Engine with loaded policies.
|
||||
* @param entryPoints Entry point rule paths.
|
||||
* @return Compiled program instance.
|
||||
*/
|
||||
public static Program compileFromEngine(Engine engine, String[] entryPoints) {
|
||||
long ptr = nativeCompileFromEngine(engine.getPtr(), entryPoints);
|
||||
return new Program(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a readable assembly listing.
|
||||
*
|
||||
* @return Listing text.
|
||||
*/
|
||||
public String generateListing() {
|
||||
return nativeGenerateListing(programPtr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize the program to binary format.
|
||||
*
|
||||
* @return Serialized bytes.
|
||||
*/
|
||||
public byte[] serializeBinary() {
|
||||
return nativeSerializeBinary(programPtr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a program from binary format.
|
||||
*
|
||||
* @param data Serialized program bytes.
|
||||
* @param isPartial Optional array to receive the partial flag (index 0).
|
||||
* @return Deserialized program instance.
|
||||
*/
|
||||
public static Program deserializeBinary(byte[] data, boolean[] isPartial) {
|
||||
if (data == null || data.length == 0) {
|
||||
throw new IllegalArgumentException("data must not be empty");
|
||||
}
|
||||
long ptr = nativeDeserializeBinary(data, isPartial);
|
||||
return new Program(ptr);
|
||||
}
|
||||
|
||||
long getPtr() {
|
||||
return programPtr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
nativeDrop(programPtr);
|
||||
}
|
||||
}
|
||||
110
bindings/java/src/main/java/com/microsoft/regorus/Rvm.java
Normal file
110
bindings/java/src/main/java/com/microsoft/regorus/Rvm.java
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
**/
|
||||
|
||||
package com.microsoft.regorus;
|
||||
|
||||
/**
|
||||
* Wrapper for the Regorus RVM runtime.
|
||||
*/
|
||||
public final class Rvm implements AutoCloseable {
|
||||
private static native long nativeNew();
|
||||
private static native void nativeDrop(long vmPtr);
|
||||
private static native void nativeLoadProgram(long vmPtr, long programPtr);
|
||||
private static native void nativeSetDataJson(long vmPtr, String dataJson);
|
||||
private static native void nativeSetInputJson(long vmPtr, String inputJson);
|
||||
private static native void nativeSetExecutionMode(long vmPtr, byte mode);
|
||||
private static native String nativeExecute(long vmPtr);
|
||||
private static native String nativeExecuteEntryPoint(long vmPtr, String entryPoint);
|
||||
private static native String nativeResume(long vmPtr, String resumeJson, boolean hasValue);
|
||||
private static native String nativeGetExecutionState(long vmPtr);
|
||||
|
||||
private final long vmPtr;
|
||||
|
||||
/**
|
||||
* Create a new RVM instance.
|
||||
*/
|
||||
public Rvm() {
|
||||
this.vmPtr = nativeNew();
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a program into the VM.
|
||||
*
|
||||
* @param program Compiled program.
|
||||
*/
|
||||
public void loadProgram(Program program) {
|
||||
nativeLoadProgram(vmPtr, program.getPtr());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set data JSON for the VM.
|
||||
*
|
||||
* @param dataJson JSON data document.
|
||||
*/
|
||||
public void setDataJson(String dataJson) {
|
||||
nativeSetDataJson(vmPtr, dataJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set input JSON for the VM.
|
||||
*
|
||||
* @param inputJson JSON input document.
|
||||
*/
|
||||
public void setInputJson(String inputJson) {
|
||||
nativeSetInputJson(vmPtr, inputJson);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set execution mode (0 = run-to-completion, 1 = suspendable).
|
||||
*
|
||||
* @param mode Execution mode.
|
||||
*/
|
||||
public void setExecutionMode(byte mode) {
|
||||
nativeSetExecutionMode(vmPtr, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the program.
|
||||
*
|
||||
* @return JSON result string.
|
||||
*/
|
||||
public String execute() {
|
||||
return nativeExecute(vmPtr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a named entry point.
|
||||
*
|
||||
* @param entryPoint Entry point rule path.
|
||||
* @return JSON result string.
|
||||
*/
|
||||
public String executeEntryPoint(String entryPoint) {
|
||||
return nativeExecuteEntryPoint(vmPtr, entryPoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resume execution with an optional JSON value.
|
||||
*
|
||||
* @param resumeJson JSON value to resume with, or null for no value.
|
||||
* @return JSON result string.
|
||||
*/
|
||||
public String resume(String resumeJson) {
|
||||
return nativeResume(vmPtr, resumeJson, resumeJson != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current execution state.
|
||||
*
|
||||
* @return Execution state string.
|
||||
*/
|
||||
public String getExecutionState() {
|
||||
return nativeGetExecutionState(vmPtr);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
nativeDrop(vmPtr);
|
||||
}
|
||||
}
|
||||
544
bindings/python/Cargo.lock
generated
544
bindings/python/Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -2,7 +2,7 @@
|
||||
|
||||
[package]
|
||||
name = "regoruspy"
|
||||
version = "0.5.0"
|
||||
version = "0.9.0"
|
||||
edition = "2021"
|
||||
repository = "https://github.com/microsoft/regorus/bindings/python"
|
||||
description = "Python bindings for Regorus - a fast, lightweight Rego interpreter written in Rust"
|
||||
@@ -22,6 +22,6 @@ coverage = ["regorus/coverage"]
|
||||
anyhow = "1.0"
|
||||
ordered-float = "5.0.0"
|
||||
pyo3 = { version = "0.24.1", features = ["abi3-py310", "anyhow", "extension-module"] }
|
||||
regorus = { path = "../..", default-features = false, features = ["arc"] }
|
||||
regorus = { path = "../..", default-features = false, features = ["arc", "rvm"] }
|
||||
serde_json = "1.0.140"
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ Regorus can be used in Python via `regorus` package. (It is not yet available in
|
||||
|
||||
See [Repository](https://github.com/microsoft/regorus).
|
||||
|
||||
## Automation
|
||||
|
||||
Run `cargo xtask build-python` to produce wheels via maturin, or `cargo xtask test-python` to reinstall the package locally and execute the sample script and pytest suite.
|
||||
|
||||
To build this binding, see [building](https://github.com/microsoft/regorus/blob/main/bindings/python/building.md)
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -8,7 +8,14 @@ use pyo3::IntoPyObjectExt;
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use ::regorus::Value;
|
||||
use ::regorus::languages::rego::compiler::Compiler;
|
||||
use ::regorus::rvm::program::{
|
||||
generate_assembly_listing, generate_tabular_assembly_listing, AssemblyListingConfig,
|
||||
DeserializationResult, Program as RvmProgram,
|
||||
};
|
||||
use ::regorus::rvm::vm::{ExecutionMode, RegoVM};
|
||||
use ::regorus::{compile_policy_with_entrypoint, PolicyModule, Rc, Value};
|
||||
use std::sync::Arc;
|
||||
|
||||
/// Regorus engine.
|
||||
#[pyclass(unsendable)]
|
||||
@@ -16,6 +23,18 @@ pub struct Engine {
|
||||
engine: ::regorus::Engine,
|
||||
}
|
||||
|
||||
/// RVM program wrapper.
|
||||
#[pyclass(unsendable)]
|
||||
pub struct Program {
|
||||
program: Arc<RvmProgram>,
|
||||
}
|
||||
|
||||
/// RVM runtime wrapper.
|
||||
#[pyclass(unsendable)]
|
||||
pub struct Rvm {
|
||||
vm: RegoVM,
|
||||
}
|
||||
|
||||
impl Default for Engine {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
@@ -384,7 +403,151 @@ impl Engine {
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl Program {
|
||||
/// Compile an RVM program from modules and entry points.
|
||||
#[staticmethod]
|
||||
pub fn compile_from_modules(
|
||||
data_json: String,
|
||||
modules: Vec<(String, String)>,
|
||||
entry_points: Vec<String>,
|
||||
) -> Result<Self> {
|
||||
if entry_points.is_empty() {
|
||||
return Err(anyhow!("entry_points must contain at least one entry"));
|
||||
}
|
||||
|
||||
let data = Value::from_json_str(&data_json)?;
|
||||
let policy_modules: Vec<PolicyModule> = modules
|
||||
.into_iter()
|
||||
.map(|(id, content)| PolicyModule {
|
||||
id: Rc::from(id.as_str()),
|
||||
content: Rc::from(content.as_str()),
|
||||
})
|
||||
.collect();
|
||||
|
||||
let entry_points_ref: Vec<&str> = entry_points.iter().map(|s| s.as_str()).collect();
|
||||
let entry_rule = Rc::from(entry_points_ref[0]);
|
||||
let compiled = compile_policy_with_entrypoint(data, &policy_modules, entry_rule)?;
|
||||
let program = Compiler::compile_from_policy(&compiled, &entry_points_ref)?;
|
||||
Ok(Self { program })
|
||||
}
|
||||
|
||||
/// Deserialize an RVM program from binary data.
|
||||
#[staticmethod]
|
||||
pub fn deserialize_binary(data: Vec<u8>) -> Result<(Self, bool)> {
|
||||
let (program, is_partial) =
|
||||
match RvmProgram::deserialize_binary(&data).map_err(|e: String| anyhow!(e))? {
|
||||
DeserializationResult::Complete(program) => (program, false),
|
||||
DeserializationResult::Partial(program) => (program, true),
|
||||
};
|
||||
Ok((
|
||||
Self {
|
||||
program: Arc::new(program),
|
||||
},
|
||||
is_partial,
|
||||
))
|
||||
}
|
||||
|
||||
/// Serialize a program to binary format.
|
||||
pub fn serialize_binary(&self) -> Result<Vec<u8>> {
|
||||
self.program
|
||||
.serialize_binary()
|
||||
.map_err(|e: String| anyhow!(e))
|
||||
}
|
||||
|
||||
/// Generate a readable assembly listing.
|
||||
pub fn generate_listing(&self) -> Result<String> {
|
||||
Ok(generate_assembly_listing(
|
||||
self.program.as_ref(),
|
||||
&AssemblyListingConfig::default(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Generate a tabular assembly listing.
|
||||
pub fn generate_tabular_listing(&self) -> Result<String> {
|
||||
Ok(generate_tabular_assembly_listing(
|
||||
self.program.as_ref(),
|
||||
&AssemblyListingConfig::default(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Rvm {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl Rvm {
|
||||
#[new]
|
||||
pub fn new() -> Self {
|
||||
Self { vm: RegoVM::new() }
|
||||
}
|
||||
|
||||
/// Load an RVM program into the VM.
|
||||
pub fn load_program(&mut self, program: &Program) -> Result<()> {
|
||||
self.vm.load_program(program.program.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set data JSON for the VM.
|
||||
pub fn set_data_json(&mut self, data_json: String) -> Result<()> {
|
||||
let data = Value::from_json_str(&data_json)?;
|
||||
self.vm.set_data(data)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set input JSON for the VM.
|
||||
pub fn set_input_json(&mut self, input_json: String) -> Result<()> {
|
||||
let input = Value::from_json_str(&input_json)?;
|
||||
self.vm.set_input(input);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set execution mode (0 = run-to-completion, 1 = suspendable).
|
||||
pub fn set_execution_mode(&mut self, mode: u8) -> Result<()> {
|
||||
let mode = match mode {
|
||||
0 => ExecutionMode::RunToCompletion,
|
||||
1 => ExecutionMode::Suspendable,
|
||||
_ => return Err(anyhow!("invalid execution mode")),
|
||||
};
|
||||
self.vm.set_execution_mode(mode);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Execute the program and return the JSON result.
|
||||
pub fn execute(&mut self) -> Result<String> {
|
||||
self.vm.execute()?.to_json_str()
|
||||
}
|
||||
|
||||
/// Execute an entry point by name and return the JSON result.
|
||||
pub fn execute_entry_point(&mut self, entry_point: String) -> Result<String> {
|
||||
self.vm
|
||||
.execute_entry_point_by_name(&entry_point)?
|
||||
.to_json_str()
|
||||
}
|
||||
|
||||
/// Resume execution with an optional JSON value.
|
||||
pub fn resume(&mut self, resume_json: Option<String>) -> Result<String> {
|
||||
let value = if let Some(json) = resume_json {
|
||||
Some(Value::from_json_str(&json)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
self.vm.resume(value)?.to_json_str()
|
||||
}
|
||||
|
||||
/// Get the execution state as a string.
|
||||
pub fn get_execution_state(&self) -> Result<String> {
|
||||
Ok(format!("{:?}", self.vm.execution_state()))
|
||||
}
|
||||
}
|
||||
|
||||
#[pymodule]
|
||||
pub fn regorus(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_class::<crate::Engine>()
|
||||
m.add_class::<crate::Engine>()?;
|
||||
m.add_class::<crate::Program>()?;
|
||||
m.add_class::<crate::Rvm>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user